sqlite3.c revision 226128
1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.7.8.  By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit.  This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately.  Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite.  To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library.  (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file.  Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE 1
21#define SQLITE_AMALGAMATION 1
22#ifndef SQLITE_PRIVATE
23# define SQLITE_PRIVATE static
24#endif
25#ifndef SQLITE_API
26# define SQLITE_API
27#endif
28/************** Begin file sqliteInt.h ***************************************/
29/*
30** 2001 September 15
31**
32** The author disclaims copyright to this source code.  In place of
33** a legal notice, here is a blessing:
34**
35**    May you do good and not evil.
36**    May you find forgiveness for yourself and forgive others.
37**    May you share freely, never taking more than you give.
38**
39*************************************************************************
40** Internal interface definitions for SQLite.
41**
42*/
43#ifndef _SQLITEINT_H_
44#define _SQLITEINT_H_
45
46/*
47** These #defines should enable >2GB file support on POSIX if the
48** underlying operating system supports it.  If the OS lacks
49** large file support, or if the OS is windows, these should be no-ops.
50**
51** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
52** system #includes.  Hence, this block of code must be the very first
53** code in all source files.
54**
55** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
56** on the compiler command line.  This is necessary if you are compiling
57** on a recent machine (ex: Red Hat 7.2) but you want your code to work
58** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
59** without this option, LFS is enable.  But LFS does not exist in the kernel
60** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
61** portability you should omit LFS.
62**
63** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
64*/
65#ifndef SQLITE_DISABLE_LFS
66# define _LARGE_FILE       1
67# ifndef _FILE_OFFSET_BITS
68#   define _FILE_OFFSET_BITS 64
69# endif
70# define _LARGEFILE_SOURCE 1
71#endif
72
73/*
74** Include the configuration header output by 'configure' if we're using the
75** autoconf-based build
76*/
77#ifdef _HAVE_SQLITE_CONFIG_H
78#include "config.h"
79#endif
80
81/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
82/************** Begin file sqliteLimit.h *************************************/
83/*
84** 2007 May 7
85**
86** The author disclaims copyright to this source code.  In place of
87** a legal notice, here is a blessing:
88**
89**    May you do good and not evil.
90**    May you find forgiveness for yourself and forgive others.
91**    May you share freely, never taking more than you give.
92**
93*************************************************************************
94**
95** This file defines various limits of what SQLite can process.
96*/
97
98/*
99** The maximum length of a TEXT or BLOB in bytes.   This also
100** limits the size of a row in a table or index.
101**
102** The hard limit is the ability of a 32-bit signed integer
103** to count the size: 2^31-1 or 2147483647.
104*/
105#ifndef SQLITE_MAX_LENGTH
106# define SQLITE_MAX_LENGTH 1000000000
107#endif
108
109/*
110** This is the maximum number of
111**
112**    * Columns in a table
113**    * Columns in an index
114**    * Columns in a view
115**    * Terms in the SET clause of an UPDATE statement
116**    * Terms in the result set of a SELECT statement
117**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
118**    * Terms in the VALUES clause of an INSERT statement
119**
120** The hard upper limit here is 32676.  Most database people will
121** tell you that in a well-normalized database, you usually should
122** not have more than a dozen or so columns in any table.  And if
123** that is the case, there is no point in having more than a few
124** dozen values in any of the other situations described above.
125*/
126#ifndef SQLITE_MAX_COLUMN
127# define SQLITE_MAX_COLUMN 2000
128#endif
129
130/*
131** The maximum length of a single SQL statement in bytes.
132**
133** It used to be the case that setting this value to zero would
134** turn the limit off.  That is no longer true.  It is not possible
135** to turn this limit off.
136*/
137#ifndef SQLITE_MAX_SQL_LENGTH
138# define SQLITE_MAX_SQL_LENGTH 1000000000
139#endif
140
141/*
142** The maximum depth of an expression tree. This is limited to
143** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
144** want to place more severe limits on the complexity of an
145** expression.
146**
147** A value of 0 used to mean that the limit was not enforced.
148** But that is no longer true.  The limit is now strictly enforced
149** at all times.
150*/
151#ifndef SQLITE_MAX_EXPR_DEPTH
152# define SQLITE_MAX_EXPR_DEPTH 1000
153#endif
154
155/*
156** The maximum number of terms in a compound SELECT statement.
157** The code generator for compound SELECT statements does one
158** level of recursion for each term.  A stack overflow can result
159** if the number of terms is too large.  In practice, most SQL
160** never has more than 3 or 4 terms.  Use a value of 0 to disable
161** any limit on the number of terms in a compount SELECT.
162*/
163#ifndef SQLITE_MAX_COMPOUND_SELECT
164# define SQLITE_MAX_COMPOUND_SELECT 500
165#endif
166
167/*
168** The maximum number of opcodes in a VDBE program.
169** Not currently enforced.
170*/
171#ifndef SQLITE_MAX_VDBE_OP
172# define SQLITE_MAX_VDBE_OP 25000
173#endif
174
175/*
176** The maximum number of arguments to an SQL function.
177*/
178#ifndef SQLITE_MAX_FUNCTION_ARG
179# define SQLITE_MAX_FUNCTION_ARG 127
180#endif
181
182/*
183** The maximum number of in-memory pages to use for the main database
184** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
185*/
186#ifndef SQLITE_DEFAULT_CACHE_SIZE
187# define SQLITE_DEFAULT_CACHE_SIZE  2000
188#endif
189#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
190# define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
191#endif
192
193/*
194** The default number of frames to accumulate in the log file before
195** checkpointing the database in WAL mode.
196*/
197#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
198# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
199#endif
200
201/*
202** The maximum number of attached databases.  This must be between 0
203** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
204** is used internally to track attached databases.
205*/
206#ifndef SQLITE_MAX_ATTACHED
207# define SQLITE_MAX_ATTACHED 10
208#endif
209
210
211/*
212** The maximum value of a ?nnn wildcard that the parser will accept.
213*/
214#ifndef SQLITE_MAX_VARIABLE_NUMBER
215# define SQLITE_MAX_VARIABLE_NUMBER 999
216#endif
217
218/* Maximum page size.  The upper bound on this value is 65536.  This a limit
219** imposed by the use of 16-bit offsets within each page.
220**
221** Earlier versions of SQLite allowed the user to change this value at
222** compile time. This is no longer permitted, on the grounds that it creates
223** a library that is technically incompatible with an SQLite library
224** compiled with a different limit. If a process operating on a database
225** with a page-size of 65536 bytes crashes, then an instance of SQLite
226** compiled with the default page-size limit will not be able to rollback
227** the aborted transaction. This could lead to database corruption.
228*/
229#ifdef SQLITE_MAX_PAGE_SIZE
230# undef SQLITE_MAX_PAGE_SIZE
231#endif
232#define SQLITE_MAX_PAGE_SIZE 65536
233
234
235/*
236** The default size of a database page.
237*/
238#ifndef SQLITE_DEFAULT_PAGE_SIZE
239# define SQLITE_DEFAULT_PAGE_SIZE 1024
240#endif
241#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
242# undef SQLITE_DEFAULT_PAGE_SIZE
243# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
244#endif
245
246/*
247** Ordinarily, if no value is explicitly provided, SQLite creates databases
248** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
249** device characteristics (sector-size and atomic write() support),
250** SQLite may choose a larger value. This constant is the maximum value
251** SQLite will choose on its own.
252*/
253#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
254# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
255#endif
256#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
257# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
258# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
259#endif
260
261
262/*
263** Maximum number of pages in one database file.
264**
265** This is really just the default value for the max_page_count pragma.
266** This value can be lowered (or raised) at run-time using that the
267** max_page_count macro.
268*/
269#ifndef SQLITE_MAX_PAGE_COUNT
270# define SQLITE_MAX_PAGE_COUNT 1073741823
271#endif
272
273/*
274** Maximum length (in bytes) of the pattern in a LIKE or GLOB
275** operator.
276*/
277#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
278# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
279#endif
280
281/*
282** Maximum depth of recursion for triggers.
283**
284** A value of 1 means that a trigger program will not be able to itself
285** fire any triggers. A value of 0 means that no trigger programs at all
286** may be executed.
287*/
288#ifndef SQLITE_MAX_TRIGGER_DEPTH
289# define SQLITE_MAX_TRIGGER_DEPTH 1000
290#endif
291
292/************** End of sqliteLimit.h *****************************************/
293/************** Continuing where we left off in sqliteInt.h ******************/
294
295/* Disable nuisance warnings on Borland compilers */
296#if defined(__BORLANDC__)
297#pragma warn -rch /* unreachable code */
298#pragma warn -ccc /* Condition is always true or false */
299#pragma warn -aus /* Assigned value is never used */
300#pragma warn -csu /* Comparing signed and unsigned */
301#pragma warn -spa /* Suspicious pointer arithmetic */
302#endif
303
304/* Needed for various definitions... */
305#ifndef _GNU_SOURCE
306# define _GNU_SOURCE
307#endif
308
309/*
310** Include standard header files as necessary
311*/
312#ifdef HAVE_STDINT_H
313#include <stdint.h>
314#endif
315#ifdef HAVE_INTTYPES_H
316#include <inttypes.h>
317#endif
318
319/*
320** The number of samples of an index that SQLite takes in order to
321** construct a histogram of the table content when running ANALYZE
322** and with SQLITE_ENABLE_STAT2
323*/
324#define SQLITE_INDEX_SAMPLES 10
325
326/*
327** The following macros are used to cast pointers to integers and
328** integers to pointers.  The way you do this varies from one compiler
329** to the next, so we have developed the following set of #if statements
330** to generate appropriate macros for a wide range of compilers.
331**
332** The correct "ANSI" way to do this is to use the intptr_t type.
333** Unfortunately, that typedef is not available on all compilers, or
334** if it is available, it requires an #include of specific headers
335** that vary from one machine to the next.
336**
337** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
338** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
339** So we have to define the macros in different ways depending on the
340** compiler.
341*/
342#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
343# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
344# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
345#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
346# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
347# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
348#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
349# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
350# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
351#else                          /* Generates a warning - but it always works */
352# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
353# define SQLITE_PTR_TO_INT(X)  ((int)(X))
354#endif
355
356/*
357** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
358** 0 means mutexes are permanently disable and the library is never
359** threadsafe.  1 means the library is serialized which is the highest
360** level of threadsafety.  2 means the libary is multithreaded - multiple
361** threads can use SQLite as long as no two threads try to use the same
362** database connection at the same time.
363**
364** Older versions of SQLite used an optional THREADSAFE macro.
365** We support that for legacy.
366*/
367#if !defined(SQLITE_THREADSAFE)
368#if defined(THREADSAFE)
369# define SQLITE_THREADSAFE THREADSAFE
370#else
371# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
372#endif
373#endif
374
375/*
376** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
377** It determines whether or not the features related to
378** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
379** be overridden at runtime using the sqlite3_config() API.
380*/
381#if !defined(SQLITE_DEFAULT_MEMSTATUS)
382# define SQLITE_DEFAULT_MEMSTATUS 1
383#endif
384
385/*
386** Exactly one of the following macros must be defined in order to
387** specify which memory allocation subsystem to use.
388**
389**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
390**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
391**     SQLITE_MEMDEBUG               // Debugging version of system malloc()
392**
393** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
394** assert() macro is enabled, each call into the Win32 native heap subsystem
395** will cause HeapValidate to be called.  If heap validation should fail, an
396** assertion will be triggered.
397**
398** (Historical note:  There used to be several other options, but we've
399** pared it down to just these two.)
400**
401** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402** the default.
403*/
404#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
405# error "At most one of the following compile-time configuration options\
406 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
407#endif
408#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
409# define SQLITE_SYSTEM_MALLOC 1
410#endif
411
412/*
413** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
414** sizes of memory allocations below this value where possible.
415*/
416#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
417# define SQLITE_MALLOC_SOFT_LIMIT 1024
418#endif
419
420/*
421** We need to define _XOPEN_SOURCE as follows in order to enable
422** recursive mutexes on most Unix systems.  But Mac OS X is different.
423** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
424** so it is omitted there.  See ticket #2673.
425**
426** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
427** implemented on some systems.  So we avoid defining it at all
428** if it is already defined or if it is unneeded because we are
429** not doing a threadsafe build.  Ticket #2681.
430**
431** See also ticket #2741.
432*/
433#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
434#ifdef __sun
435#  define _XOPEN_SOURCE 600
436#else
437#  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
438#endif
439#endif
440
441/*
442** The TCL headers are only needed when compiling the TCL bindings.
443*/
444#if defined(SQLITE_TCL) || defined(TCLSH)
445# include <tcl.h>
446#endif
447
448/*
449** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
450** Setting NDEBUG makes the code smaller and run faster.  So the following
451** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
452** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
453** feature.
454*/
455#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
456# define NDEBUG 1
457#endif
458
459/*
460** The testcase() macro is used to aid in coverage testing.  When
461** doing coverage testing, the condition inside the argument to
462** testcase() must be evaluated both true and false in order to
463** get full branch coverage.  The testcase() macro is inserted
464** to help ensure adequate test coverage in places where simple
465** condition/decision coverage is inadequate.  For example, testcase()
466** can be used to make sure boundary values are tested.  For
467** bitmask tests, testcase() can be used to make sure each bit
468** is significant and used at least once.  On switch statements
469** where multiple cases go to the same block of code, testcase()
470** can insure that all cases are evaluated.
471**
472*/
473#ifdef SQLITE_COVERAGE_TEST
474SQLITE_PRIVATE   void sqlite3Coverage(int);
475# define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
476#else
477# define testcase(X)
478#endif
479
480/*
481** The TESTONLY macro is used to enclose variable declarations or
482** other bits of code that are needed to support the arguments
483** within testcase() and assert() macros.
484*/
485#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
486# define TESTONLY(X)  X
487#else
488# define TESTONLY(X)
489#endif
490
491/*
492** Sometimes we need a small amount of code such as a variable initialization
493** to setup for a later assert() statement.  We do not want this code to
494** appear when assert() is disabled.  The following macro is therefore
495** used to contain that setup code.  The "VVA" acronym stands for
496** "Verification, Validation, and Accreditation".  In other words, the
497** code within VVA_ONLY() will only run during verification processes.
498*/
499#ifndef NDEBUG
500# define VVA_ONLY(X)  X
501#else
502# define VVA_ONLY(X)
503#endif
504
505/*
506** The ALWAYS and NEVER macros surround boolean expressions which
507** are intended to always be true or false, respectively.  Such
508** expressions could be omitted from the code completely.  But they
509** are included in a few cases in order to enhance the resilience
510** of SQLite to unexpected behavior - to make the code "self-healing"
511** or "ductile" rather than being "brittle" and crashing at the first
512** hint of unplanned behavior.
513**
514** In other words, ALWAYS and NEVER are added for defensive code.
515**
516** When doing coverage testing ALWAYS and NEVER are hard-coded to
517** be true and false so that the unreachable code then specify will
518** not be counted as untested code.
519*/
520#if defined(SQLITE_COVERAGE_TEST)
521# define ALWAYS(X)      (1)
522# define NEVER(X)       (0)
523#elif !defined(NDEBUG)
524# define ALWAYS(X)      ((X)?1:(assert(0),0))
525# define NEVER(X)       ((X)?(assert(0),1):0)
526#else
527# define ALWAYS(X)      (X)
528# define NEVER(X)       (X)
529#endif
530
531/*
532** Return true (non-zero) if the input is a integer that is too large
533** to fit in 32-bits.  This macro is used inside of various testcase()
534** macros to verify that we have tested SQLite for large-file support.
535*/
536#define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
537
538/*
539** The macro unlikely() is a hint that surrounds a boolean
540** expression that is usually false.  Macro likely() surrounds
541** a boolean expression that is usually true.  GCC is able to
542** use these hints to generate better code, sometimes.
543*/
544#if defined(__GNUC__) && 0
545# define likely(X)    __builtin_expect((X),1)
546# define unlikely(X)  __builtin_expect((X),0)
547#else
548# define likely(X)    !!(X)
549# define unlikely(X)  !!(X)
550#endif
551
552/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
553/************** Begin file sqlite3.h *****************************************/
554/*
555** 2001 September 15
556**
557** The author disclaims copyright to this source code.  In place of
558** a legal notice, here is a blessing:
559**
560**    May you do good and not evil.
561**    May you find forgiveness for yourself and forgive others.
562**    May you share freely, never taking more than you give.
563**
564*************************************************************************
565** This header file defines the interface that the SQLite library
566** presents to client programs.  If a C-function, structure, datatype,
567** or constant definition does not appear in this file, then it is
568** not a published API of SQLite, is subject to change without
569** notice, and should not be referenced by programs that use SQLite.
570**
571** Some of the definitions that are in this file are marked as
572** "experimental".  Experimental interfaces are normally new
573** features recently added to SQLite.  We do not anticipate changes
574** to experimental interfaces but reserve the right to make minor changes
575** if experience from use "in the wild" suggest such changes are prudent.
576**
577** The official C-language API documentation for SQLite is derived
578** from comments in this file.  This file is the authoritative source
579** on how SQLite interfaces are suppose to operate.
580**
581** The name of this file under configuration management is "sqlite.h.in".
582** The makefile makes some minor changes to this file (such as inserting
583** the version number) and changes its name to "sqlite3.h" as
584** part of the build process.
585*/
586#ifndef _SQLITE3_H_
587#define _SQLITE3_H_
588#include <stdarg.h>     /* Needed for the definition of va_list */
589
590/*
591** Make sure we can call this stuff from C++.
592*/
593#if 0
594extern "C" {
595#endif
596
597
598/*
599** Add the ability to override 'extern'
600*/
601#ifndef SQLITE_EXTERN
602# define SQLITE_EXTERN extern
603#endif
604
605#ifndef SQLITE_API
606# define SQLITE_API
607#endif
608
609
610/*
611** These no-op macros are used in front of interfaces to mark those
612** interfaces as either deprecated or experimental.  New applications
613** should not use deprecated interfaces - they are support for backwards
614** compatibility only.  Application writers should be aware that
615** experimental interfaces are subject to change in point releases.
616**
617** These macros used to resolve to various kinds of compiler magic that
618** would generate warning messages when they were used.  But that
619** compiler magic ended up generating such a flurry of bug reports
620** that we have taken it all out and gone back to using simple
621** noop macros.
622*/
623#define SQLITE_DEPRECATED
624#define SQLITE_EXPERIMENTAL
625
626/*
627** Ensure these symbols were not defined by some previous header file.
628*/
629#ifdef SQLITE_VERSION
630# undef SQLITE_VERSION
631#endif
632#ifdef SQLITE_VERSION_NUMBER
633# undef SQLITE_VERSION_NUMBER
634#endif
635
636/*
637** CAPI3REF: Compile-Time Library Version Numbers
638**
639** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
640** evaluates to a string literal that is the SQLite version in the
641** format "X.Y.Z" where X is the major version number (always 3 for
642** SQLite3) and Y is the minor version number and Z is the release number.)^
643** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
644** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
645** numbers used in [SQLITE_VERSION].)^
646** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
647** be larger than the release from which it is derived.  Either Y will
648** be held constant and Z will be incremented or else Y will be incremented
649** and Z will be reset to zero.
650**
651** Since version 3.6.18, SQLite source code has been stored in the
652** <a href="http://www.fossil-scm.org/">Fossil configuration management
653** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
654** a string which identifies a particular check-in of SQLite
655** within its configuration management system.  ^The SQLITE_SOURCE_ID
656** string contains the date and time of the check-in (UTC) and an SHA1
657** hash of the entire source tree.
658**
659** See also: [sqlite3_libversion()],
660** [sqlite3_libversion_number()], [sqlite3_sourceid()],
661** [sqlite_version()] and [sqlite_source_id()].
662*/
663#define SQLITE_VERSION        "3.7.8"
664#define SQLITE_VERSION_NUMBER 3007008
665#define SQLITE_SOURCE_ID      "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
666
667/*
668** CAPI3REF: Run-Time Library Version Numbers
669** KEYWORDS: sqlite3_version, sqlite3_sourceid
670**
671** These interfaces provide the same information as the [SQLITE_VERSION],
672** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
673** but are associated with the library instead of the header file.  ^(Cautious
674** programmers might include assert() statements in their application to
675** verify that values returned by these interfaces match the macros in
676** the header, and thus insure that the application is
677** compiled with matching library and header files.
678**
679** <blockquote><pre>
680** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
681** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
682** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
683** </pre></blockquote>)^
684**
685** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
686** macro.  ^The sqlite3_libversion() function returns a pointer to the
687** to the sqlite3_version[] string constant.  The sqlite3_libversion()
688** function is provided for use in DLLs since DLL users usually do not have
689** direct access to string constants within the DLL.  ^The
690** sqlite3_libversion_number() function returns an integer equal to
691** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
692** a pointer to a string constant whose value is the same as the
693** [SQLITE_SOURCE_ID] C preprocessor macro.
694**
695** See also: [sqlite_version()] and [sqlite_source_id()].
696*/
697SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
698SQLITE_API const char *sqlite3_libversion(void);
699SQLITE_API const char *sqlite3_sourceid(void);
700SQLITE_API int sqlite3_libversion_number(void);
701
702/*
703** CAPI3REF: Run-Time Library Compilation Options Diagnostics
704**
705** ^The sqlite3_compileoption_used() function returns 0 or 1
706** indicating whether the specified option was defined at
707** compile time.  ^The SQLITE_ prefix may be omitted from the
708** option name passed to sqlite3_compileoption_used().
709**
710** ^The sqlite3_compileoption_get() function allows iterating
711** over the list of options that were defined at compile time by
712** returning the N-th compile time option string.  ^If N is out of range,
713** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
714** prefix is omitted from any strings returned by
715** sqlite3_compileoption_get().
716**
717** ^Support for the diagnostic functions sqlite3_compileoption_used()
718** and sqlite3_compileoption_get() may be omitted by specifying the
719** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
720**
721** See also: SQL functions [sqlite_compileoption_used()] and
722** [sqlite_compileoption_get()] and the [compile_options pragma].
723*/
724#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
725SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
726SQLITE_API const char *sqlite3_compileoption_get(int N);
727#endif
728
729/*
730** CAPI3REF: Test To See If The Library Is Threadsafe
731**
732** ^The sqlite3_threadsafe() function returns zero if and only if
733** SQLite was compiled mutexing code omitted due to the
734** [SQLITE_THREADSAFE] compile-time option being set to 0.
735**
736** SQLite can be compiled with or without mutexes.  When
737** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
738** are enabled and SQLite is threadsafe.  When the
739** [SQLITE_THREADSAFE] macro is 0,
740** the mutexes are omitted.  Without the mutexes, it is not safe
741** to use SQLite concurrently from more than one thread.
742**
743** Enabling mutexes incurs a measurable performance penalty.
744** So if speed is of utmost importance, it makes sense to disable
745** the mutexes.  But for maximum safety, mutexes should be enabled.
746** ^The default behavior is for mutexes to be enabled.
747**
748** This interface can be used by an application to make sure that the
749** version of SQLite that it is linking against was compiled with
750** the desired setting of the [SQLITE_THREADSAFE] macro.
751**
752** This interface only reports on the compile-time mutex setting
753** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
754** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
755** can be fully or partially disabled using a call to [sqlite3_config()]
756** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
757** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
758** sqlite3_threadsafe() function shows only the compile-time setting of
759** thread safety, not any run-time changes to that setting made by
760** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
761** is unchanged by calls to sqlite3_config().)^
762**
763** See the [threading mode] documentation for additional information.
764*/
765SQLITE_API int sqlite3_threadsafe(void);
766
767/*
768** CAPI3REF: Database Connection Handle
769** KEYWORDS: {database connection} {database connections}
770**
771** Each open SQLite database is represented by a pointer to an instance of
772** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
773** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
774** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
775** is its destructor.  There are many other interfaces (such as
776** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
777** [sqlite3_busy_timeout()] to name but three) that are methods on an
778** sqlite3 object.
779*/
780typedef struct sqlite3 sqlite3;
781
782/*
783** CAPI3REF: 64-Bit Integer Types
784** KEYWORDS: sqlite_int64 sqlite_uint64
785**
786** Because there is no cross-platform way to specify 64-bit integer types
787** SQLite includes typedefs for 64-bit signed and unsigned integers.
788**
789** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
790** The sqlite_int64 and sqlite_uint64 types are supported for backwards
791** compatibility only.
792**
793** ^The sqlite3_int64 and sqlite_int64 types can store integer values
794** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
795** sqlite3_uint64 and sqlite_uint64 types can store integer values
796** between 0 and +18446744073709551615 inclusive.
797*/
798#ifdef SQLITE_INT64_TYPE
799  typedef SQLITE_INT64_TYPE sqlite_int64;
800  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
801#elif defined(_MSC_VER) || defined(__BORLANDC__)
802  typedef __int64 sqlite_int64;
803  typedef unsigned __int64 sqlite_uint64;
804#else
805  typedef long long int sqlite_int64;
806  typedef unsigned long long int sqlite_uint64;
807#endif
808typedef sqlite_int64 sqlite3_int64;
809typedef sqlite_uint64 sqlite3_uint64;
810
811/*
812** If compiling for a processor that lacks floating point support,
813** substitute integer for floating-point.
814*/
815#ifdef SQLITE_OMIT_FLOATING_POINT
816# define double sqlite3_int64
817#endif
818
819/*
820** CAPI3REF: Closing A Database Connection
821**
822** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
823** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
824** successfully destroyed and all associated resources are deallocated.
825**
826** Applications must [sqlite3_finalize | finalize] all [prepared statements]
827** and [sqlite3_blob_close | close] all [BLOB handles] associated with
828** the [sqlite3] object prior to attempting to close the object.  ^If
829** sqlite3_close() is called on a [database connection] that still has
830** outstanding [prepared statements] or [BLOB handles], then it returns
831** SQLITE_BUSY.
832**
833** ^If [sqlite3_close()] is invoked while a transaction is open,
834** the transaction is automatically rolled back.
835**
836** The C parameter to [sqlite3_close(C)] must be either a NULL
837** pointer or an [sqlite3] object pointer obtained
838** from [sqlite3_open()], [sqlite3_open16()], or
839** [sqlite3_open_v2()], and not previously closed.
840** ^Calling sqlite3_close() with a NULL pointer argument is a
841** harmless no-op.
842*/
843SQLITE_API int sqlite3_close(sqlite3 *);
844
845/*
846** The type for a callback function.
847** This is legacy and deprecated.  It is included for historical
848** compatibility and is not documented.
849*/
850typedef int (*sqlite3_callback)(void*,int,char**, char**);
851
852/*
853** CAPI3REF: One-Step Query Execution Interface
854**
855** The sqlite3_exec() interface is a convenience wrapper around
856** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
857** that allows an application to run multiple statements of SQL
858** without having to use a lot of C code.
859**
860** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
861** semicolon-separate SQL statements passed into its 2nd argument,
862** in the context of the [database connection] passed in as its 1st
863** argument.  ^If the callback function of the 3rd argument to
864** sqlite3_exec() is not NULL, then it is invoked for each result row
865** coming out of the evaluated SQL statements.  ^The 4th argument to
866** sqlite3_exec() is relayed through to the 1st argument of each
867** callback invocation.  ^If the callback pointer to sqlite3_exec()
868** is NULL, then no callback is ever invoked and result rows are
869** ignored.
870**
871** ^If an error occurs while evaluating the SQL statements passed into
872** sqlite3_exec(), then execution of the current statement stops and
873** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
874** is not NULL then any error message is written into memory obtained
875** from [sqlite3_malloc()] and passed back through the 5th parameter.
876** To avoid memory leaks, the application should invoke [sqlite3_free()]
877** on error message strings returned through the 5th parameter of
878** of sqlite3_exec() after the error message string is no longer needed.
879** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
880** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
881** NULL before returning.
882**
883** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
884** routine returns SQLITE_ABORT without invoking the callback again and
885** without running any subsequent SQL statements.
886**
887** ^The 2nd argument to the sqlite3_exec() callback function is the
888** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
889** callback is an array of pointers to strings obtained as if from
890** [sqlite3_column_text()], one for each column.  ^If an element of a
891** result row is NULL then the corresponding string pointer for the
892** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
893** sqlite3_exec() callback is an array of pointers to strings where each
894** entry represents the name of corresponding result column as obtained
895** from [sqlite3_column_name()].
896**
897** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
898** to an empty string, or a pointer that contains only whitespace and/or
899** SQL comments, then no SQL statements are evaluated and the database
900** is not changed.
901**
902** Restrictions:
903**
904** <ul>
905** <li> The application must insure that the 1st parameter to sqlite3_exec()
906**      is a valid and open [database connection].
907** <li> The application must not close [database connection] specified by
908**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
909** <li> The application must not modify the SQL statement text passed into
910**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
911** </ul>
912*/
913SQLITE_API int sqlite3_exec(
914  sqlite3*,                                  /* An open database */
915  const char *sql,                           /* SQL to be evaluated */
916  int (*callback)(void*,int,char**,char**),  /* Callback function */
917  void *,                                    /* 1st argument to callback */
918  char **errmsg                              /* Error msg written here */
919);
920
921/*
922** CAPI3REF: Result Codes
923** KEYWORDS: SQLITE_OK {error code} {error codes}
924** KEYWORDS: {result code} {result codes}
925**
926** Many SQLite functions return an integer result code from the set shown
927** here in order to indicates success or failure.
928**
929** New error codes may be added in future versions of SQLite.
930**
931** See also: [SQLITE_IOERR_READ | extended result codes],
932** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
933*/
934#define SQLITE_OK           0   /* Successful result */
935/* beginning-of-error-codes */
936#define SQLITE_ERROR        1   /* SQL error or missing database */
937#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
938#define SQLITE_PERM         3   /* Access permission denied */
939#define SQLITE_ABORT        4   /* Callback routine requested an abort */
940#define SQLITE_BUSY         5   /* The database file is locked */
941#define SQLITE_LOCKED       6   /* A table in the database is locked */
942#define SQLITE_NOMEM        7   /* A malloc() failed */
943#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
944#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
945#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
946#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
947#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
948#define SQLITE_FULL        13   /* Insertion failed because database is full */
949#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
950#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
951#define SQLITE_EMPTY       16   /* Database is empty */
952#define SQLITE_SCHEMA      17   /* The database schema changed */
953#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
954#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
955#define SQLITE_MISMATCH    20   /* Data type mismatch */
956#define SQLITE_MISUSE      21   /* Library used incorrectly */
957#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
958#define SQLITE_AUTH        23   /* Authorization denied */
959#define SQLITE_FORMAT      24   /* Auxiliary database format error */
960#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
961#define SQLITE_NOTADB      26   /* File opened that is not a database file */
962#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
963#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
964/* end-of-error-codes */
965
966/*
967** CAPI3REF: Extended Result Codes
968** KEYWORDS: {extended error code} {extended error codes}
969** KEYWORDS: {extended result code} {extended result codes}
970**
971** In its default configuration, SQLite API routines return one of 26 integer
972** [SQLITE_OK | result codes].  However, experience has shown that many of
973** these result codes are too coarse-grained.  They do not provide as
974** much information about problems as programmers might like.  In an effort to
975** address this, newer versions of SQLite (version 3.3.8 and later) include
976** support for additional result codes that provide more detailed information
977** about errors. The extended result codes are enabled or disabled
978** on a per database connection basis using the
979** [sqlite3_extended_result_codes()] API.
980**
981** Some of the available extended result codes are listed here.
982** One may expect the number of extended result codes will be expand
983** over time.  Software that uses extended result codes should expect
984** to see new result codes in future releases of SQLite.
985**
986** The SQLITE_OK result code will never be extended.  It will always
987** be exactly zero.
988*/
989#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
990#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
991#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
992#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
993#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
994#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
995#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
996#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
997#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
998#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
999#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
1000#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
1001#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
1002#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
1003#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
1004#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
1005#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
1006#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
1007#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
1008#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
1009#define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
1010#define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
1011#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
1012#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
1013#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
1014#define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
1015#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
1016#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
1017
1018/*
1019** CAPI3REF: Flags For File Open Operations
1020**
1021** These bit values are intended for use in the
1022** 3rd parameter to the [sqlite3_open_v2()] interface and
1023** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1024*/
1025#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
1026#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
1027#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
1028#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
1029#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
1030#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
1031#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
1032#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
1033#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
1034#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
1035#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
1036#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
1037#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
1038#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
1039#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
1040#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
1041#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
1042#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
1043#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
1044
1045/* Reserved:                         0x00F00000 */
1046
1047/*
1048** CAPI3REF: Device Characteristics
1049**
1050** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1051** object returns an integer which is a vector of the these
1052** bit values expressing I/O characteristics of the mass storage
1053** device that holds the file that the [sqlite3_io_methods]
1054** refers to.
1055**
1056** The SQLITE_IOCAP_ATOMIC property means that all writes of
1057** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1058** mean that writes of blocks that are nnn bytes in size and
1059** are aligned to an address which is an integer multiple of
1060** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1061** that when data is appended to a file, the data is appended
1062** first then the size of the file is extended, never the other
1063** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1064** information is written to disk in the same order as calls
1065** to xWrite().
1066*/
1067#define SQLITE_IOCAP_ATOMIC                 0x00000001
1068#define SQLITE_IOCAP_ATOMIC512              0x00000002
1069#define SQLITE_IOCAP_ATOMIC1K               0x00000004
1070#define SQLITE_IOCAP_ATOMIC2K               0x00000008
1071#define SQLITE_IOCAP_ATOMIC4K               0x00000010
1072#define SQLITE_IOCAP_ATOMIC8K               0x00000020
1073#define SQLITE_IOCAP_ATOMIC16K              0x00000040
1074#define SQLITE_IOCAP_ATOMIC32K              0x00000080
1075#define SQLITE_IOCAP_ATOMIC64K              0x00000100
1076#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
1077#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
1078#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
1079
1080/*
1081** CAPI3REF: File Locking Levels
1082**
1083** SQLite uses one of these integer values as the second
1084** argument to calls it makes to the xLock() and xUnlock() methods
1085** of an [sqlite3_io_methods] object.
1086*/
1087#define SQLITE_LOCK_NONE          0
1088#define SQLITE_LOCK_SHARED        1
1089#define SQLITE_LOCK_RESERVED      2
1090#define SQLITE_LOCK_PENDING       3
1091#define SQLITE_LOCK_EXCLUSIVE     4
1092
1093/*
1094** CAPI3REF: Synchronization Type Flags
1095**
1096** When SQLite invokes the xSync() method of an
1097** [sqlite3_io_methods] object it uses a combination of
1098** these integer values as the second argument.
1099**
1100** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1101** sync operation only needs to flush data to mass storage.  Inode
1102** information need not be flushed. If the lower four bits of the flag
1103** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1104** If the lower four bits equal SQLITE_SYNC_FULL, that means
1105** to use Mac OS X style fullsync instead of fsync().
1106**
1107** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1108** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1109** settings.  The [synchronous pragma] determines when calls to the
1110** xSync VFS method occur and applies uniformly across all platforms.
1111** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1112** energetic or rigorous or forceful the sync operations are and
1113** only make a difference on Mac OSX for the default SQLite code.
1114** (Third-party VFS implementations might also make the distinction
1115** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1116** operating systems natively supported by SQLite, only Mac OSX
1117** cares about the difference.)
1118*/
1119#define SQLITE_SYNC_NORMAL        0x00002
1120#define SQLITE_SYNC_FULL          0x00003
1121#define SQLITE_SYNC_DATAONLY      0x00010
1122
1123/*
1124** CAPI3REF: OS Interface Open File Handle
1125**
1126** An [sqlite3_file] object represents an open file in the
1127** [sqlite3_vfs | OS interface layer].  Individual OS interface
1128** implementations will
1129** want to subclass this object by appending additional fields
1130** for their own use.  The pMethods entry is a pointer to an
1131** [sqlite3_io_methods] object that defines methods for performing
1132** I/O operations on the open file.
1133*/
1134typedef struct sqlite3_file sqlite3_file;
1135struct sqlite3_file {
1136  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
1137};
1138
1139/*
1140** CAPI3REF: OS Interface File Virtual Methods Object
1141**
1142** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1143** [sqlite3_file] object (or, more commonly, a subclass of the
1144** [sqlite3_file] object) with a pointer to an instance of this object.
1145** This object defines the methods used to perform various operations
1146** against the open file represented by the [sqlite3_file] object.
1147**
1148** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1149** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1150** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
1151** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1152** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1153** to NULL.
1154**
1155** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1156** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
1157** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
1158** flag may be ORed in to indicate that only the data of the file
1159** and not its inode needs to be synced.
1160**
1161** The integer values to xLock() and xUnlock() are one of
1162** <ul>
1163** <li> [SQLITE_LOCK_NONE],
1164** <li> [SQLITE_LOCK_SHARED],
1165** <li> [SQLITE_LOCK_RESERVED],
1166** <li> [SQLITE_LOCK_PENDING], or
1167** <li> [SQLITE_LOCK_EXCLUSIVE].
1168** </ul>
1169** xLock() increases the lock. xUnlock() decreases the lock.
1170** The xCheckReservedLock() method checks whether any database connection,
1171** either in this process or in some other process, is holding a RESERVED,
1172** PENDING, or EXCLUSIVE lock on the file.  It returns true
1173** if such a lock exists and false otherwise.
1174**
1175** The xFileControl() method is a generic interface that allows custom
1176** VFS implementations to directly control an open file using the
1177** [sqlite3_file_control()] interface.  The second "op" argument is an
1178** integer opcode.  The third argument is a generic pointer intended to
1179** point to a structure that may contain arguments or space in which to
1180** write return values.  Potential uses for xFileControl() might be
1181** functions to enable blocking locks with timeouts, to change the
1182** locking strategy (for example to use dot-file locks), to inquire
1183** about the status of a lock, or to break stale locks.  The SQLite
1184** core reserves all opcodes less than 100 for its own use.
1185** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
1186** Applications that define a custom xFileControl method should use opcodes
1187** greater than 100 to avoid conflicts.  VFS implementations should
1188** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1189** recognize.
1190**
1191** The xSectorSize() method returns the sector size of the
1192** device that underlies the file.  The sector size is the
1193** minimum write that can be performed without disturbing
1194** other bytes in the file.  The xDeviceCharacteristics()
1195** method returns a bit vector describing behaviors of the
1196** underlying device:
1197**
1198** <ul>
1199** <li> [SQLITE_IOCAP_ATOMIC]
1200** <li> [SQLITE_IOCAP_ATOMIC512]
1201** <li> [SQLITE_IOCAP_ATOMIC1K]
1202** <li> [SQLITE_IOCAP_ATOMIC2K]
1203** <li> [SQLITE_IOCAP_ATOMIC4K]
1204** <li> [SQLITE_IOCAP_ATOMIC8K]
1205** <li> [SQLITE_IOCAP_ATOMIC16K]
1206** <li> [SQLITE_IOCAP_ATOMIC32K]
1207** <li> [SQLITE_IOCAP_ATOMIC64K]
1208** <li> [SQLITE_IOCAP_SAFE_APPEND]
1209** <li> [SQLITE_IOCAP_SEQUENTIAL]
1210** </ul>
1211**
1212** The SQLITE_IOCAP_ATOMIC property means that all writes of
1213** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1214** mean that writes of blocks that are nnn bytes in size and
1215** are aligned to an address which is an integer multiple of
1216** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1217** that when data is appended to a file, the data is appended
1218** first then the size of the file is extended, never the other
1219** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1220** information is written to disk in the same order as calls
1221** to xWrite().
1222**
1223** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1224** in the unread portions of the buffer with zeros.  A VFS that
1225** fails to zero-fill short reads might seem to work.  However,
1226** failure to zero-fill short reads will eventually lead to
1227** database corruption.
1228*/
1229typedef struct sqlite3_io_methods sqlite3_io_methods;
1230struct sqlite3_io_methods {
1231  int iVersion;
1232  int (*xClose)(sqlite3_file*);
1233  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1234  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1235  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1236  int (*xSync)(sqlite3_file*, int flags);
1237  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1238  int (*xLock)(sqlite3_file*, int);
1239  int (*xUnlock)(sqlite3_file*, int);
1240  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1241  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1242  int (*xSectorSize)(sqlite3_file*);
1243  int (*xDeviceCharacteristics)(sqlite3_file*);
1244  /* Methods above are valid for version 1 */
1245  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1246  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1247  void (*xShmBarrier)(sqlite3_file*);
1248  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1249  /* Methods above are valid for version 2 */
1250  /* Additional methods may be added in future releases */
1251};
1252
1253/*
1254** CAPI3REF: Standard File Control Opcodes
1255**
1256** These integer constants are opcodes for the xFileControl method
1257** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1258** interface.
1259**
1260** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
1261** opcode causes the xFileControl method to write the current state of
1262** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1263** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1264** into an integer that the pArg argument points to. This capability
1265** is used during testing and only needs to be supported when SQLITE_TEST
1266** is defined.
1267**
1268** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1269** layer a hint of how large the database file will grow to be during the
1270** current transaction.  This hint is not guaranteed to be accurate but it
1271** is often close.  The underlying VFS might choose to preallocate database
1272** file space based on this hint in order to help writes to the database
1273** file run faster.
1274**
1275** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1276** extends and truncates the database file in chunks of a size specified
1277** by the user. The fourth argument to [sqlite3_file_control()] should
1278** point to an integer (type int) containing the new chunk-size to use
1279** for the nominated database. Allocating database file space in large
1280** chunks (say 1MB at a time), may reduce file-system fragmentation and
1281** improve performance on some systems.
1282**
1283** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1284** to the [sqlite3_file] object associated with a particular database
1285** connection.  See the [sqlite3_file_control()] documentation for
1286** additional information.
1287**
1288** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
1289** SQLite and sent to all VFSes in place of a call to the xSync method
1290** when the database connection has [PRAGMA synchronous] set to OFF.)^
1291** Some specialized VFSes need this signal in order to operate correctly
1292** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
1293** VFSes do not need this signal and should silently ignore this opcode.
1294** Applications should not call [sqlite3_file_control()] with this
1295** opcode as doing so may disrupt the operation of the specialized VFSes
1296** that do require it.
1297**
1298** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1299** retry counts and intervals for certain disk I/O operations for the
1300** windows [VFS] in order to work to provide robustness against
1301** anti-virus programs.  By default, the windows VFS will retry file read,
1302** file write, and file delete opertions up to 10 times, with a delay
1303** of 25 milliseconds before the first retry and with the delay increasing
1304** by an additional 25 milliseconds with each subsequent retry.  This
1305** opcode allows those to values (10 retries and 25 milliseconds of delay)
1306** to be adjusted.  The values are changed for all database connections
1307** within the same process.  The argument is a pointer to an array of two
1308** integers where the first integer i the new retry count and the second
1309** integer is the delay.  If either integer is negative, then the setting
1310** is not changed but instead the prior value of that setting is written
1311** into the array entry, allowing the current retry settings to be
1312** interrogated.  The zDbName parameter is ignored.
1313**
1314** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1315** persistent [WAL | Write AHead Log] setting.  By default, the auxiliary
1316** write ahead log and shared memory files used for transaction control
1317** are automatically deleted when the latest connection to the database
1318** closes.  Setting persistent WAL mode causes those files to persist after
1319** close.  Persisting the files is useful when other processes that do not
1320** have write permission on the directory containing the database file want
1321** to read the database file, as the WAL and shared memory files must exist
1322** in order for the database to be readable.  The fourth parameter to
1323** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1324** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1325** WAL mode.  If the integer is -1, then it is overwritten with the current
1326** WAL persistence setting.
1327**
1328*/
1329#define SQLITE_FCNTL_LOCKSTATE        1
1330#define SQLITE_GET_LOCKPROXYFILE      2
1331#define SQLITE_SET_LOCKPROXYFILE      3
1332#define SQLITE_LAST_ERRNO             4
1333#define SQLITE_FCNTL_SIZE_HINT        5
1334#define SQLITE_FCNTL_CHUNK_SIZE       6
1335#define SQLITE_FCNTL_FILE_POINTER     7
1336#define SQLITE_FCNTL_SYNC_OMITTED     8
1337#define SQLITE_FCNTL_WIN32_AV_RETRY   9
1338#define SQLITE_FCNTL_PERSIST_WAL     10
1339
1340/*
1341** CAPI3REF: Mutex Handle
1342**
1343** The mutex module within SQLite defines [sqlite3_mutex] to be an
1344** abstract type for a mutex object.  The SQLite core never looks
1345** at the internal representation of an [sqlite3_mutex].  It only
1346** deals with pointers to the [sqlite3_mutex] object.
1347**
1348** Mutexes are created using [sqlite3_mutex_alloc()].
1349*/
1350typedef struct sqlite3_mutex sqlite3_mutex;
1351
1352/*
1353** CAPI3REF: OS Interface Object
1354**
1355** An instance of the sqlite3_vfs object defines the interface between
1356** the SQLite core and the underlying operating system.  The "vfs"
1357** in the name of the object stands for "virtual file system".  See
1358** the [VFS | VFS documentation] for further information.
1359**
1360** The value of the iVersion field is initially 1 but may be larger in
1361** future versions of SQLite.  Additional fields may be appended to this
1362** object when the iVersion value is increased.  Note that the structure
1363** of the sqlite3_vfs object changes in the transaction between
1364** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1365** modified.
1366**
1367** The szOsFile field is the size of the subclassed [sqlite3_file]
1368** structure used by this VFS.  mxPathname is the maximum length of
1369** a pathname in this VFS.
1370**
1371** Registered sqlite3_vfs objects are kept on a linked list formed by
1372** the pNext pointer.  The [sqlite3_vfs_register()]
1373** and [sqlite3_vfs_unregister()] interfaces manage this list
1374** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1375** searches the list.  Neither the application code nor the VFS
1376** implementation should use the pNext pointer.
1377**
1378** The pNext field is the only field in the sqlite3_vfs
1379** structure that SQLite will ever modify.  SQLite will only access
1380** or modify this field while holding a particular static mutex.
1381** The application should never modify anything within the sqlite3_vfs
1382** object once the object has been registered.
1383**
1384** The zName field holds the name of the VFS module.  The name must
1385** be unique across all VFS modules.
1386**
1387** [[sqlite3_vfs.xOpen]]
1388** ^SQLite guarantees that the zFilename parameter to xOpen
1389** is either a NULL pointer or string obtained
1390** from xFullPathname() with an optional suffix added.
1391** ^If a suffix is added to the zFilename parameter, it will
1392** consist of a single "-" character followed by no more than
1393** 10 alphanumeric and/or "-" characters.
1394** ^SQLite further guarantees that
1395** the string will be valid and unchanged until xClose() is
1396** called. Because of the previous sentence,
1397** the [sqlite3_file] can safely store a pointer to the
1398** filename if it needs to remember the filename for some reason.
1399** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1400** must invent its own temporary name for the file.  ^Whenever the
1401** xFilename parameter is NULL it will also be the case that the
1402** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1403**
1404** The flags argument to xOpen() includes all bits set in
1405** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1406** or [sqlite3_open16()] is used, then flags includes at least
1407** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1408** If xOpen() opens a file read-only then it sets *pOutFlags to
1409** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1410**
1411** ^(SQLite will also add one of the following flags to the xOpen()
1412** call, depending on the object being opened:
1413**
1414** <ul>
1415** <li>  [SQLITE_OPEN_MAIN_DB]
1416** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1417** <li>  [SQLITE_OPEN_TEMP_DB]
1418** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1419** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1420** <li>  [SQLITE_OPEN_SUBJOURNAL]
1421** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1422** <li>  [SQLITE_OPEN_WAL]
1423** </ul>)^
1424**
1425** The file I/O implementation can use the object type flags to
1426** change the way it deals with files.  For example, an application
1427** that does not care about crash recovery or rollback might make
1428** the open of a journal file a no-op.  Writes to this journal would
1429** also be no-ops, and any attempt to read the journal would return
1430** SQLITE_IOERR.  Or the implementation might recognize that a database
1431** file will be doing page-aligned sector reads and writes in a random
1432** order and set up its I/O subsystem accordingly.
1433**
1434** SQLite might also add one of the following flags to the xOpen method:
1435**
1436** <ul>
1437** <li> [SQLITE_OPEN_DELETEONCLOSE]
1438** <li> [SQLITE_OPEN_EXCLUSIVE]
1439** </ul>
1440**
1441** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1442** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1443** will be set for TEMP databases and their journals, transient
1444** databases, and subjournals.
1445**
1446** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1447** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1448** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1449** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1450** SQLITE_OPEN_CREATE, is used to indicate that file should always
1451** be created, and that it is an error if it already exists.
1452** It is <i>not</i> used to indicate the file should be opened
1453** for exclusive access.
1454**
1455** ^At least szOsFile bytes of memory are allocated by SQLite
1456** to hold the  [sqlite3_file] structure passed as the third
1457** argument to xOpen.  The xOpen method does not have to
1458** allocate the structure; it should just fill it in.  Note that
1459** the xOpen method must set the sqlite3_file.pMethods to either
1460** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1461** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1462** element will be valid after xOpen returns regardless of the success
1463** or failure of the xOpen call.
1464**
1465** [[sqlite3_vfs.xAccess]]
1466** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1467** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1468** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1469** to test whether a file is at least readable.   The file can be a
1470** directory.
1471**
1472** ^SQLite will always allocate at least mxPathname+1 bytes for the
1473** output buffer xFullPathname.  The exact size of the output buffer
1474** is also passed as a parameter to both  methods. If the output buffer
1475** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1476** handled as a fatal error by SQLite, vfs implementations should endeavor
1477** to prevent this by setting mxPathname to a sufficiently large value.
1478**
1479** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1480** interfaces are not strictly a part of the filesystem, but they are
1481** included in the VFS structure for completeness.
1482** The xRandomness() function attempts to return nBytes bytes
1483** of good-quality randomness into zOut.  The return value is
1484** the actual number of bytes of randomness obtained.
1485** The xSleep() method causes the calling thread to sleep for at
1486** least the number of microseconds given.  ^The xCurrentTime()
1487** method returns a Julian Day Number for the current date and time as
1488** a floating point value.
1489** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1490** Day Number multiplied by 86400000 (the number of milliseconds in
1491** a 24-hour day).
1492** ^SQLite will use the xCurrentTimeInt64() method to get the current
1493** date and time if that method is available (if iVersion is 2 or
1494** greater and the function pointer is not NULL) and will fall back
1495** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1496**
1497** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1498** are not used by the SQLite core.  These optional interfaces are provided
1499** by some VFSes to facilitate testing of the VFS code. By overriding
1500** system calls with functions under its control, a test program can
1501** simulate faults and error conditions that would otherwise be difficult
1502** or impossible to induce.  The set of system calls that can be overridden
1503** varies from one VFS to another, and from one version of the same VFS to the
1504** next.  Applications that use these interfaces must be prepared for any
1505** or all of these interfaces to be NULL or for their behavior to change
1506** from one release to the next.  Applications must not attempt to access
1507** any of these methods if the iVersion of the VFS is less than 3.
1508*/
1509typedef struct sqlite3_vfs sqlite3_vfs;
1510typedef void (*sqlite3_syscall_ptr)(void);
1511struct sqlite3_vfs {
1512  int iVersion;            /* Structure version number (currently 3) */
1513  int szOsFile;            /* Size of subclassed sqlite3_file */
1514  int mxPathname;          /* Maximum file pathname length */
1515  sqlite3_vfs *pNext;      /* Next registered VFS */
1516  const char *zName;       /* Name of this virtual file system */
1517  void *pAppData;          /* Pointer to application-specific data */
1518  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1519               int flags, int *pOutFlags);
1520  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1521  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1522  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1523  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1524  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1525  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1526  void (*xDlClose)(sqlite3_vfs*, void*);
1527  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1528  int (*xSleep)(sqlite3_vfs*, int microseconds);
1529  int (*xCurrentTime)(sqlite3_vfs*, double*);
1530  int (*xGetLastError)(sqlite3_vfs*, int, char *);
1531  /*
1532  ** The methods above are in version 1 of the sqlite_vfs object
1533  ** definition.  Those that follow are added in version 2 or later
1534  */
1535  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1536  /*
1537  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1538  ** Those below are for version 3 and greater.
1539  */
1540  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1541  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1542  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1543  /*
1544  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1545  ** New fields may be appended in figure versions.  The iVersion
1546  ** value will increment whenever this happens.
1547  */
1548};
1549
1550/*
1551** CAPI3REF: Flags for the xAccess VFS method
1552**
1553** These integer constants can be used as the third parameter to
1554** the xAccess method of an [sqlite3_vfs] object.  They determine
1555** what kind of permissions the xAccess method is looking for.
1556** With SQLITE_ACCESS_EXISTS, the xAccess method
1557** simply checks whether the file exists.
1558** With SQLITE_ACCESS_READWRITE, the xAccess method
1559** checks whether the named directory is both readable and writable
1560** (in other words, if files can be added, removed, and renamed within
1561** the directory).
1562** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1563** [temp_store_directory pragma], though this could change in a future
1564** release of SQLite.
1565** With SQLITE_ACCESS_READ, the xAccess method
1566** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1567** currently unused, though it might be used in a future release of
1568** SQLite.
1569*/
1570#define SQLITE_ACCESS_EXISTS    0
1571#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1572#define SQLITE_ACCESS_READ      2   /* Unused */
1573
1574/*
1575** CAPI3REF: Flags for the xShmLock VFS method
1576**
1577** These integer constants define the various locking operations
1578** allowed by the xShmLock method of [sqlite3_io_methods].  The
1579** following are the only legal combinations of flags to the
1580** xShmLock method:
1581**
1582** <ul>
1583** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1584** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1585** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1586** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1587** </ul>
1588**
1589** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1590** was given no the corresponding lock.
1591**
1592** The xShmLock method can transition between unlocked and SHARED or
1593** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1594** and EXCLUSIVE.
1595*/
1596#define SQLITE_SHM_UNLOCK       1
1597#define SQLITE_SHM_LOCK         2
1598#define SQLITE_SHM_SHARED       4
1599#define SQLITE_SHM_EXCLUSIVE    8
1600
1601/*
1602** CAPI3REF: Maximum xShmLock index
1603**
1604** The xShmLock method on [sqlite3_io_methods] may use values
1605** between 0 and this upper bound as its "offset" argument.
1606** The SQLite core will never attempt to acquire or release a
1607** lock outside of this range
1608*/
1609#define SQLITE_SHM_NLOCK        8
1610
1611
1612/*
1613** CAPI3REF: Initialize The SQLite Library
1614**
1615** ^The sqlite3_initialize() routine initializes the
1616** SQLite library.  ^The sqlite3_shutdown() routine
1617** deallocates any resources that were allocated by sqlite3_initialize().
1618** These routines are designed to aid in process initialization and
1619** shutdown on embedded systems.  Workstation applications using
1620** SQLite normally do not need to invoke either of these routines.
1621**
1622** A call to sqlite3_initialize() is an "effective" call if it is
1623** the first time sqlite3_initialize() is invoked during the lifetime of
1624** the process, or if it is the first time sqlite3_initialize() is invoked
1625** following a call to sqlite3_shutdown().  ^(Only an effective call
1626** of sqlite3_initialize() does any initialization.  All other calls
1627** are harmless no-ops.)^
1628**
1629** A call to sqlite3_shutdown() is an "effective" call if it is the first
1630** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1631** an effective call to sqlite3_shutdown() does any deinitialization.
1632** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1633**
1634** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1635** is not.  The sqlite3_shutdown() interface must only be called from a
1636** single thread.  All open [database connections] must be closed and all
1637** other SQLite resources must be deallocated prior to invoking
1638** sqlite3_shutdown().
1639**
1640** Among other things, ^sqlite3_initialize() will invoke
1641** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1642** will invoke sqlite3_os_end().
1643**
1644** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1645** ^If for some reason, sqlite3_initialize() is unable to initialize
1646** the library (perhaps it is unable to allocate a needed resource such
1647** as a mutex) it returns an [error code] other than [SQLITE_OK].
1648**
1649** ^The sqlite3_initialize() routine is called internally by many other
1650** SQLite interfaces so that an application usually does not need to
1651** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1652** calls sqlite3_initialize() so the SQLite library will be automatically
1653** initialized when [sqlite3_open()] is called if it has not be initialized
1654** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1655** compile-time option, then the automatic calls to sqlite3_initialize()
1656** are omitted and the application must call sqlite3_initialize() directly
1657** prior to using any other SQLite interface.  For maximum portability,
1658** it is recommended that applications always invoke sqlite3_initialize()
1659** directly prior to using any other SQLite interface.  Future releases
1660** of SQLite may require this.  In other words, the behavior exhibited
1661** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1662** default behavior in some future release of SQLite.
1663**
1664** The sqlite3_os_init() routine does operating-system specific
1665** initialization of the SQLite library.  The sqlite3_os_end()
1666** routine undoes the effect of sqlite3_os_init().  Typical tasks
1667** performed by these routines include allocation or deallocation
1668** of static resources, initialization of global variables,
1669** setting up a default [sqlite3_vfs] module, or setting up
1670** a default configuration using [sqlite3_config()].
1671**
1672** The application should never invoke either sqlite3_os_init()
1673** or sqlite3_os_end() directly.  The application should only invoke
1674** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1675** interface is called automatically by sqlite3_initialize() and
1676** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1677** implementations for sqlite3_os_init() and sqlite3_os_end()
1678** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1679** When [custom builds | built for other platforms]
1680** (using the [SQLITE_OS_OTHER=1] compile-time
1681** option) the application must supply a suitable implementation for
1682** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1683** implementation of sqlite3_os_init() or sqlite3_os_end()
1684** must return [SQLITE_OK] on success and some other [error code] upon
1685** failure.
1686*/
1687SQLITE_API int sqlite3_initialize(void);
1688SQLITE_API int sqlite3_shutdown(void);
1689SQLITE_API int sqlite3_os_init(void);
1690SQLITE_API int sqlite3_os_end(void);
1691
1692/*
1693** CAPI3REF: Configuring The SQLite Library
1694**
1695** The sqlite3_config() interface is used to make global configuration
1696** changes to SQLite in order to tune SQLite to the specific needs of
1697** the application.  The default configuration is recommended for most
1698** applications and so this routine is usually not necessary.  It is
1699** provided to support rare applications with unusual needs.
1700**
1701** The sqlite3_config() interface is not threadsafe.  The application
1702** must insure that no other SQLite interfaces are invoked by other
1703** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1704** may only be invoked prior to library initialization using
1705** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1706** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1707** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1708** Note, however, that ^sqlite3_config() can be called as part of the
1709** implementation of an application-defined [sqlite3_os_init()].
1710**
1711** The first argument to sqlite3_config() is an integer
1712** [configuration option] that determines
1713** what property of SQLite is to be configured.  Subsequent arguments
1714** vary depending on the [configuration option]
1715** in the first argument.
1716**
1717** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1718** ^If the option is unknown or SQLite is unable to set the option
1719** then this routine returns a non-zero [error code].
1720*/
1721SQLITE_API int sqlite3_config(int, ...);
1722
1723/*
1724** CAPI3REF: Configure database connections
1725**
1726** The sqlite3_db_config() interface is used to make configuration
1727** changes to a [database connection].  The interface is similar to
1728** [sqlite3_config()] except that the changes apply to a single
1729** [database connection] (specified in the first argument).
1730**
1731** The second argument to sqlite3_db_config(D,V,...)  is the
1732** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1733** that indicates what aspect of the [database connection] is being configured.
1734** Subsequent arguments vary depending on the configuration verb.
1735**
1736** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1737** the call is considered successful.
1738*/
1739SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1740
1741/*
1742** CAPI3REF: Memory Allocation Routines
1743**
1744** An instance of this object defines the interface between SQLite
1745** and low-level memory allocation routines.
1746**
1747** This object is used in only one place in the SQLite interface.
1748** A pointer to an instance of this object is the argument to
1749** [sqlite3_config()] when the configuration option is
1750** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1751** By creating an instance of this object
1752** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1753** during configuration, an application can specify an alternative
1754** memory allocation subsystem for SQLite to use for all of its
1755** dynamic memory needs.
1756**
1757** Note that SQLite comes with several [built-in memory allocators]
1758** that are perfectly adequate for the overwhelming majority of applications
1759** and that this object is only useful to a tiny minority of applications
1760** with specialized memory allocation requirements.  This object is
1761** also used during testing of SQLite in order to specify an alternative
1762** memory allocator that simulates memory out-of-memory conditions in
1763** order to verify that SQLite recovers gracefully from such
1764** conditions.
1765**
1766** The xMalloc, xRealloc, and xFree methods must work like the
1767** malloc(), realloc() and free() functions from the standard C library.
1768** ^SQLite guarantees that the second argument to
1769** xRealloc is always a value returned by a prior call to xRoundup.
1770**
1771** xSize should return the allocated size of a memory allocation
1772** previously obtained from xMalloc or xRealloc.  The allocated size
1773** is always at least as big as the requested size but may be larger.
1774**
1775** The xRoundup method returns what would be the allocated size of
1776** a memory allocation given a particular requested size.  Most memory
1777** allocators round up memory allocations at least to the next multiple
1778** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1779** Every memory allocation request coming in through [sqlite3_malloc()]
1780** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1781** that causes the corresponding memory allocation to fail.
1782**
1783** The xInit method initializes the memory allocator.  (For example,
1784** it might allocate any require mutexes or initialize internal data
1785** structures.  The xShutdown method is invoked (indirectly) by
1786** [sqlite3_shutdown()] and should deallocate any resources acquired
1787** by xInit.  The pAppData pointer is used as the only parameter to
1788** xInit and xShutdown.
1789**
1790** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1791** the xInit method, so the xInit method need not be threadsafe.  The
1792** xShutdown method is only called from [sqlite3_shutdown()] so it does
1793** not need to be threadsafe either.  For all other methods, SQLite
1794** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1795** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1796** it is by default) and so the methods are automatically serialized.
1797** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1798** methods must be threadsafe or else make their own arrangements for
1799** serialization.
1800**
1801** SQLite will never invoke xInit() more than once without an intervening
1802** call to xShutdown().
1803*/
1804typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1805struct sqlite3_mem_methods {
1806  void *(*xMalloc)(int);         /* Memory allocation function */
1807  void (*xFree)(void*);          /* Free a prior allocation */
1808  void *(*xRealloc)(void*,int);  /* Resize an allocation */
1809  int (*xSize)(void*);           /* Return the size of an allocation */
1810  int (*xRoundup)(int);          /* Round up request size to allocation size */
1811  int (*xInit)(void*);           /* Initialize the memory allocator */
1812  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1813  void *pAppData;                /* Argument to xInit() and xShutdown() */
1814};
1815
1816/*
1817** CAPI3REF: Configuration Options
1818** KEYWORDS: {configuration option}
1819**
1820** These constants are the available integer configuration options that
1821** can be passed as the first argument to the [sqlite3_config()] interface.
1822**
1823** New configuration options may be added in future releases of SQLite.
1824** Existing configuration options might be discontinued.  Applications
1825** should check the return code from [sqlite3_config()] to make sure that
1826** the call worked.  The [sqlite3_config()] interface will return a
1827** non-zero [error code] if a discontinued or unsupported configuration option
1828** is invoked.
1829**
1830** <dl>
1831** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1832** <dd>There are no arguments to this option.  ^This option sets the
1833** [threading mode] to Single-thread.  In other words, it disables
1834** all mutexing and puts SQLite into a mode where it can only be used
1835** by a single thread.   ^If SQLite is compiled with
1836** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1837** it is not possible to change the [threading mode] from its default
1838** value of Single-thread and so [sqlite3_config()] will return
1839** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1840** configuration option.</dd>
1841**
1842** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1843** <dd>There are no arguments to this option.  ^This option sets the
1844** [threading mode] to Multi-thread.  In other words, it disables
1845** mutexing on [database connection] and [prepared statement] objects.
1846** The application is responsible for serializing access to
1847** [database connections] and [prepared statements].  But other mutexes
1848** are enabled so that SQLite will be safe to use in a multi-threaded
1849** environment as long as no two threads attempt to use the same
1850** [database connection] at the same time.  ^If SQLite is compiled with
1851** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1852** it is not possible to set the Multi-thread [threading mode] and
1853** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1854** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1855**
1856** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1857** <dd>There are no arguments to this option.  ^This option sets the
1858** [threading mode] to Serialized. In other words, this option enables
1859** all mutexes including the recursive
1860** mutexes on [database connection] and [prepared statement] objects.
1861** In this mode (which is the default when SQLite is compiled with
1862** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1863** to [database connections] and [prepared statements] so that the
1864** application is free to use the same [database connection] or the
1865** same [prepared statement] in different threads at the same time.
1866** ^If SQLite is compiled with
1867** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1868** it is not possible to set the Serialized [threading mode] and
1869** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1870** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1871**
1872** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1873** <dd> ^(This option takes a single argument which is a pointer to an
1874** instance of the [sqlite3_mem_methods] structure.  The argument specifies
1875** alternative low-level memory allocation routines to be used in place of
1876** the memory allocation routines built into SQLite.)^ ^SQLite makes
1877** its own private copy of the content of the [sqlite3_mem_methods] structure
1878** before the [sqlite3_config()] call returns.</dd>
1879**
1880** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1881** <dd> ^(This option takes a single argument which is a pointer to an
1882** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
1883** structure is filled with the currently defined memory allocation routines.)^
1884** This option can be used to overload the default memory allocation
1885** routines with a wrapper that simulations memory allocation failure or
1886** tracks memory usage, for example. </dd>
1887**
1888** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1889** <dd> ^This option takes single argument of type int, interpreted as a
1890** boolean, which enables or disables the collection of memory allocation
1891** statistics. ^(When memory allocation statistics are disabled, the
1892** following SQLite interfaces become non-operational:
1893**   <ul>
1894**   <li> [sqlite3_memory_used()]
1895**   <li> [sqlite3_memory_highwater()]
1896**   <li> [sqlite3_soft_heap_limit64()]
1897**   <li> [sqlite3_status()]
1898**   </ul>)^
1899** ^Memory allocation statistics are enabled by default unless SQLite is
1900** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1901** allocation statistics are disabled by default.
1902** </dd>
1903**
1904** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1905** <dd> ^This option specifies a static memory buffer that SQLite can use for
1906** scratch memory.  There are three arguments:  A pointer an 8-byte
1907** aligned memory buffer from which the scratch allocations will be
1908** drawn, the size of each scratch allocation (sz),
1909** and the maximum number of scratch allocations (N).  The sz
1910** argument must be a multiple of 16.
1911** The first argument must be a pointer to an 8-byte aligned buffer
1912** of at least sz*N bytes of memory.
1913** ^SQLite will use no more than two scratch buffers per thread.  So
1914** N should be set to twice the expected maximum number of threads.
1915** ^SQLite will never require a scratch buffer that is more than 6
1916** times the database page size. ^If SQLite needs needs additional
1917** scratch memory beyond what is provided by this configuration option, then
1918** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1919**
1920** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1921** <dd> ^This option specifies a static memory buffer that SQLite can use for
1922** the database page cache with the default page cache implementation.
1923** This configuration should not be used if an application-define page
1924** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1925** There are three arguments to this option: A pointer to 8-byte aligned
1926** memory, the size of each page buffer (sz), and the number of pages (N).
1927** The sz argument should be the size of the largest database page
1928** (a power of two between 512 and 32768) plus a little extra for each
1929** page header.  ^The page header size is 20 to 40 bytes depending on
1930** the host architecture.  ^It is harmless, apart from the wasted memory,
1931** to make sz a little too large.  The first
1932** argument should point to an allocation of at least sz*N bytes of memory.
1933** ^SQLite will use the memory provided by the first argument to satisfy its
1934** memory needs for the first N pages that it adds to cache.  ^If additional
1935** page cache memory is needed beyond what is provided by this option, then
1936** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1937** The pointer in the first argument must
1938** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1939** will be undefined.</dd>
1940**
1941** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1942** <dd> ^This option specifies a static memory buffer that SQLite will use
1943** for all of its dynamic memory allocation needs beyond those provided
1944** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1945** There are three arguments: An 8-byte aligned pointer to the memory,
1946** the number of bytes in the memory buffer, and the minimum allocation size.
1947** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1948** to using its default memory allocator (the system malloc() implementation),
1949** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1950** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1951** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1952** allocator is engaged to handle all of SQLites memory allocation needs.
1953** The first pointer (the memory pointer) must be aligned to an 8-byte
1954** boundary or subsequent behavior of SQLite will be undefined.
1955** The minimum allocation size is capped at 2^12. Reasonable values
1956** for the minimum allocation size are 2^5 through 2^8.</dd>
1957**
1958** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1959** <dd> ^(This option takes a single argument which is a pointer to an
1960** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
1961** alternative low-level mutex routines to be used in place
1962** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
1963** content of the [sqlite3_mutex_methods] structure before the call to
1964** [sqlite3_config()] returns. ^If SQLite is compiled with
1965** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1966** the entire mutexing subsystem is omitted from the build and hence calls to
1967** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1968** return [SQLITE_ERROR].</dd>
1969**
1970** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1971** <dd> ^(This option takes a single argument which is a pointer to an
1972** instance of the [sqlite3_mutex_methods] structure.  The
1973** [sqlite3_mutex_methods]
1974** structure is filled with the currently defined mutex routines.)^
1975** This option can be used to overload the default mutex allocation
1976** routines with a wrapper used to track mutex usage for performance
1977** profiling or testing, for example.   ^If SQLite is compiled with
1978** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1979** the entire mutexing subsystem is omitted from the build and hence calls to
1980** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1981** return [SQLITE_ERROR].</dd>
1982**
1983** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1984** <dd> ^(This option takes two arguments that determine the default
1985** memory allocation for the lookaside memory allocator on each
1986** [database connection].  The first argument is the
1987** size of each lookaside buffer slot and the second is the number of
1988** slots allocated to each database connection.)^  ^(This option sets the
1989** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1990** verb to [sqlite3_db_config()] can be used to change the lookaside
1991** configuration on individual connections.)^ </dd>
1992**
1993** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
1994** <dd> ^(This option takes a single argument which is a pointer to
1995** an [sqlite3_pcache_methods] object.  This object specifies the interface
1996** to a custom page cache implementation.)^  ^SQLite makes a copy of the
1997** object and uses it for page cache memory allocations.</dd>
1998**
1999** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
2000** <dd> ^(This option takes a single argument which is a pointer to an
2001** [sqlite3_pcache_methods] object.  SQLite copies of the current
2002** page cache implementation into that object.)^ </dd>
2003**
2004** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2005** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2006** function with a call signature of void(*)(void*,int,const char*),
2007** and a pointer to void. ^If the function pointer is not NULL, it is
2008** invoked by [sqlite3_log()] to process each logging event.  ^If the
2009** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2010** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2011** passed through as the first parameter to the application-defined logger
2012** function whenever that function is invoked.  ^The second parameter to
2013** the logger function is a copy of the first parameter to the corresponding
2014** [sqlite3_log()] call and is intended to be a [result code] or an
2015** [extended result code].  ^The third parameter passed to the logger is
2016** log message after formatting via [sqlite3_snprintf()].
2017** The SQLite logging interface is not reentrant; the logger function
2018** supplied by the application must not invoke any SQLite interface.
2019** In a multi-threaded application, the application-defined logger
2020** function must be threadsafe. </dd>
2021**
2022** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2023** <dd> This option takes a single argument of type int. If non-zero, then
2024** URI handling is globally enabled. If the parameter is zero, then URI handling
2025** is globally disabled. If URI handling is globally enabled, all filenames
2026** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
2027** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2028** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2029** connection is opened. If it is globally disabled, filenames are
2030** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2031** database connection is opened. By default, URI handling is globally
2032** disabled. The default value may be changed by compiling with the
2033** [SQLITE_USE_URI] symbol defined.
2034** </dl>
2035*/
2036#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
2037#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
2038#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
2039#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
2040#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
2041#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
2042#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
2043#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
2044#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
2045#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
2046#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
2047/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2048#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
2049#define SQLITE_CONFIG_PCACHE       14  /* sqlite3_pcache_methods* */
2050#define SQLITE_CONFIG_GETPCACHE    15  /* sqlite3_pcache_methods* */
2051#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
2052#define SQLITE_CONFIG_URI          17  /* int */
2053
2054/*
2055** CAPI3REF: Database Connection Configuration Options
2056**
2057** These constants are the available integer configuration options that
2058** can be passed as the second argument to the [sqlite3_db_config()] interface.
2059**
2060** New configuration options may be added in future releases of SQLite.
2061** Existing configuration options might be discontinued.  Applications
2062** should check the return code from [sqlite3_db_config()] to make sure that
2063** the call worked.  ^The [sqlite3_db_config()] interface will return a
2064** non-zero [error code] if a discontinued or unsupported configuration option
2065** is invoked.
2066**
2067** <dl>
2068** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2069** <dd> ^This option takes three additional arguments that determine the
2070** [lookaside memory allocator] configuration for the [database connection].
2071** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2072** pointer to a memory buffer to use for lookaside memory.
2073** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2074** may be NULL in which case SQLite will allocate the
2075** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2076** size of each lookaside buffer slot.  ^The third argument is the number of
2077** slots.  The size of the buffer in the first argument must be greater than
2078** or equal to the product of the second and third arguments.  The buffer
2079** must be aligned to an 8-byte boundary.  ^If the second argument to
2080** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2081** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
2082** configuration for a database connection can only be changed when that
2083** connection is not currently using lookaside memory, or in other words
2084** when the "current value" returned by
2085** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2086** Any attempt to change the lookaside memory configuration when lookaside
2087** memory is in use leaves the configuration unchanged and returns
2088** [SQLITE_BUSY].)^</dd>
2089**
2090** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2091** <dd> ^This option is used to enable or disable the enforcement of
2092** [foreign key constraints].  There should be two additional arguments.
2093** The first argument is an integer which is 0 to disable FK enforcement,
2094** positive to enable FK enforcement or negative to leave FK enforcement
2095** unchanged.  The second parameter is a pointer to an integer into which
2096** is written 0 or 1 to indicate whether FK enforcement is off or on
2097** following this call.  The second parameter may be a NULL pointer, in
2098** which case the FK enforcement setting is not reported back. </dd>
2099**
2100** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2101** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2102** There should be two additional arguments.
2103** The first argument is an integer which is 0 to disable triggers,
2104** positive to enable triggers or negative to leave the setting unchanged.
2105** The second parameter is a pointer to an integer into which
2106** is written 0 or 1 to indicate whether triggers are disabled or enabled
2107** following this call.  The second parameter may be a NULL pointer, in
2108** which case the trigger setting is not reported back. </dd>
2109**
2110** </dl>
2111*/
2112#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
2113#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
2114#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
2115
2116
2117/*
2118** CAPI3REF: Enable Or Disable Extended Result Codes
2119**
2120** ^The sqlite3_extended_result_codes() routine enables or disables the
2121** [extended result codes] feature of SQLite. ^The extended result
2122** codes are disabled by default for historical compatibility.
2123*/
2124SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2125
2126/*
2127** CAPI3REF: Last Insert Rowid
2128**
2129** ^Each entry in an SQLite table has a unique 64-bit signed
2130** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2131** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2132** names are not also used by explicitly declared columns. ^If
2133** the table has a column of type [INTEGER PRIMARY KEY] then that column
2134** is another alias for the rowid.
2135**
2136** ^This routine returns the [rowid] of the most recent
2137** successful [INSERT] into the database from the [database connection]
2138** in the first argument.  ^As of SQLite version 3.7.7, this routines
2139** records the last insert rowid of both ordinary tables and [virtual tables].
2140** ^If no successful [INSERT]s
2141** have ever occurred on that database connection, zero is returned.
2142**
2143** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2144** method, then this routine will return the [rowid] of the inserted
2145** row as long as the trigger or virtual table method is running.
2146** But once the trigger or virtual table method ends, the value returned
2147** by this routine reverts to what it was before the trigger or virtual
2148** table method began.)^
2149**
2150** ^An [INSERT] that fails due to a constraint violation is not a
2151** successful [INSERT] and does not change the value returned by this
2152** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2153** and INSERT OR ABORT make no changes to the return value of this
2154** routine when their insertion fails.  ^(When INSERT OR REPLACE
2155** encounters a constraint violation, it does not fail.  The
2156** INSERT continues to completion after deleting rows that caused
2157** the constraint problem so INSERT OR REPLACE will always change
2158** the return value of this interface.)^
2159**
2160** ^For the purposes of this routine, an [INSERT] is considered to
2161** be successful even if it is subsequently rolled back.
2162**
2163** This function is accessible to SQL statements via the
2164** [last_insert_rowid() SQL function].
2165**
2166** If a separate thread performs a new [INSERT] on the same
2167** database connection while the [sqlite3_last_insert_rowid()]
2168** function is running and thus changes the last insert [rowid],
2169** then the value returned by [sqlite3_last_insert_rowid()] is
2170** unpredictable and might not equal either the old or the new
2171** last insert [rowid].
2172*/
2173SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2174
2175/*
2176** CAPI3REF: Count The Number Of Rows Modified
2177**
2178** ^This function returns the number of database rows that were changed
2179** or inserted or deleted by the most recently completed SQL statement
2180** on the [database connection] specified by the first parameter.
2181** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
2182** or [DELETE] statement are counted.  Auxiliary changes caused by
2183** triggers or [foreign key actions] are not counted.)^ Use the
2184** [sqlite3_total_changes()] function to find the total number of changes
2185** including changes caused by triggers and foreign key actions.
2186**
2187** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
2188** are not counted.  Only real table changes are counted.
2189**
2190** ^(A "row change" is a change to a single row of a single table
2191** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
2192** are changed as side effects of [REPLACE] constraint resolution,
2193** rollback, ABORT processing, [DROP TABLE], or by any other
2194** mechanisms do not count as direct row changes.)^
2195**
2196** A "trigger context" is a scope of execution that begins and
2197** ends with the script of a [CREATE TRIGGER | trigger].
2198** Most SQL statements are
2199** evaluated outside of any trigger.  This is the "top level"
2200** trigger context.  If a trigger fires from the top level, a
2201** new trigger context is entered for the duration of that one
2202** trigger.  Subtriggers create subcontexts for their duration.
2203**
2204** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2205** not create a new trigger context.
2206**
2207** ^This function returns the number of direct row changes in the
2208** most recent INSERT, UPDATE, or DELETE statement within the same
2209** trigger context.
2210**
2211** ^Thus, when called from the top level, this function returns the
2212** number of changes in the most recent INSERT, UPDATE, or DELETE
2213** that also occurred at the top level.  ^(Within the body of a trigger,
2214** the sqlite3_changes() interface can be called to find the number of
2215** changes in the most recently completed INSERT, UPDATE, or DELETE
2216** statement within the body of the same trigger.
2217** However, the number returned does not include changes
2218** caused by subtriggers since those have their own context.)^
2219**
2220** See also the [sqlite3_total_changes()] interface, the
2221** [count_changes pragma], and the [changes() SQL function].
2222**
2223** If a separate thread makes changes on the same database connection
2224** while [sqlite3_changes()] is running then the value returned
2225** is unpredictable and not meaningful.
2226*/
2227SQLITE_API int sqlite3_changes(sqlite3*);
2228
2229/*
2230** CAPI3REF: Total Number Of Rows Modified
2231**
2232** ^This function returns the number of row changes caused by [INSERT],
2233** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2234** ^(The count returned by sqlite3_total_changes() includes all changes
2235** from all [CREATE TRIGGER | trigger] contexts and changes made by
2236** [foreign key actions]. However,
2237** the count does not include changes used to implement [REPLACE] constraints,
2238** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
2239** count does not include rows of views that fire an [INSTEAD OF trigger],
2240** though if the INSTEAD OF trigger makes changes of its own, those changes
2241** are counted.)^
2242** ^The sqlite3_total_changes() function counts the changes as soon as
2243** the statement that makes them is completed (when the statement handle
2244** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2245**
2246** See also the [sqlite3_changes()] interface, the
2247** [count_changes pragma], and the [total_changes() SQL function].
2248**
2249** If a separate thread makes changes on the same database connection
2250** while [sqlite3_total_changes()] is running then the value
2251** returned is unpredictable and not meaningful.
2252*/
2253SQLITE_API int sqlite3_total_changes(sqlite3*);
2254
2255/*
2256** CAPI3REF: Interrupt A Long-Running Query
2257**
2258** ^This function causes any pending database operation to abort and
2259** return at its earliest opportunity. This routine is typically
2260** called in response to a user action such as pressing "Cancel"
2261** or Ctrl-C where the user wants a long query operation to halt
2262** immediately.
2263**
2264** ^It is safe to call this routine from a thread different from the
2265** thread that is currently running the database operation.  But it
2266** is not safe to call this routine with a [database connection] that
2267** is closed or might close before sqlite3_interrupt() returns.
2268**
2269** ^If an SQL operation is very nearly finished at the time when
2270** sqlite3_interrupt() is called, then it might not have an opportunity
2271** to be interrupted and might continue to completion.
2272**
2273** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2274** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2275** that is inside an explicit transaction, then the entire transaction
2276** will be rolled back automatically.
2277**
2278** ^The sqlite3_interrupt(D) call is in effect until all currently running
2279** SQL statements on [database connection] D complete.  ^Any new SQL statements
2280** that are started after the sqlite3_interrupt() call and before the
2281** running statements reaches zero are interrupted as if they had been
2282** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2283** that are started after the running statement count reaches zero are
2284** not effected by the sqlite3_interrupt().
2285** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2286** SQL statements is a no-op and has no effect on SQL statements
2287** that are started after the sqlite3_interrupt() call returns.
2288**
2289** If the database connection closes while [sqlite3_interrupt()]
2290** is running then bad things will likely happen.
2291*/
2292SQLITE_API void sqlite3_interrupt(sqlite3*);
2293
2294/*
2295** CAPI3REF: Determine If An SQL Statement Is Complete
2296**
2297** These routines are useful during command-line input to determine if the
2298** currently entered text seems to form a complete SQL statement or
2299** if additional input is needed before sending the text into
2300** SQLite for parsing.  ^These routines return 1 if the input string
2301** appears to be a complete SQL statement.  ^A statement is judged to be
2302** complete if it ends with a semicolon token and is not a prefix of a
2303** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2304** string literals or quoted identifier names or comments are not
2305** independent tokens (they are part of the token in which they are
2306** embedded) and thus do not count as a statement terminator.  ^Whitespace
2307** and comments that follow the final semicolon are ignored.
2308**
2309** ^These routines return 0 if the statement is incomplete.  ^If a
2310** memory allocation fails, then SQLITE_NOMEM is returned.
2311**
2312** ^These routines do not parse the SQL statements thus
2313** will not detect syntactically incorrect SQL.
2314**
2315** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2316** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2317** automatically by sqlite3_complete16().  If that initialization fails,
2318** then the return value from sqlite3_complete16() will be non-zero
2319** regardless of whether or not the input SQL is complete.)^
2320**
2321** The input to [sqlite3_complete()] must be a zero-terminated
2322** UTF-8 string.
2323**
2324** The input to [sqlite3_complete16()] must be a zero-terminated
2325** UTF-16 string in native byte order.
2326*/
2327SQLITE_API int sqlite3_complete(const char *sql);
2328SQLITE_API int sqlite3_complete16(const void *sql);
2329
2330/*
2331** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2332**
2333** ^This routine sets a callback function that might be invoked whenever
2334** an attempt is made to open a database table that another thread
2335** or process has locked.
2336**
2337** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2338** is returned immediately upon encountering the lock.  ^If the busy callback
2339** is not NULL, then the callback might be invoked with two arguments.
2340**
2341** ^The first argument to the busy handler is a copy of the void* pointer which
2342** is the third argument to sqlite3_busy_handler().  ^The second argument to
2343** the busy handler callback is the number of times that the busy handler has
2344** been invoked for this locking event.  ^If the
2345** busy callback returns 0, then no additional attempts are made to
2346** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2347** ^If the callback returns non-zero, then another attempt
2348** is made to open the database for reading and the cycle repeats.
2349**
2350** The presence of a busy handler does not guarantee that it will be invoked
2351** when there is lock contention. ^If SQLite determines that invoking the busy
2352** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2353** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2354** Consider a scenario where one process is holding a read lock that
2355** it is trying to promote to a reserved lock and
2356** a second process is holding a reserved lock that it is trying
2357** to promote to an exclusive lock.  The first process cannot proceed
2358** because it is blocked by the second and the second process cannot
2359** proceed because it is blocked by the first.  If both processes
2360** invoke the busy handlers, neither will make any progress.  Therefore,
2361** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2362** will induce the first process to release its read lock and allow
2363** the second process to proceed.
2364**
2365** ^The default busy callback is NULL.
2366**
2367** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2368** when SQLite is in the middle of a large transaction where all the
2369** changes will not fit into the in-memory cache.  SQLite will
2370** already hold a RESERVED lock on the database file, but it needs
2371** to promote this lock to EXCLUSIVE so that it can spill cache
2372** pages into the database file without harm to concurrent
2373** readers.  ^If it is unable to promote the lock, then the in-memory
2374** cache will be left in an inconsistent state and so the error
2375** code is promoted from the relatively benign [SQLITE_BUSY] to
2376** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
2377** forces an automatic rollback of the changes.  See the
2378** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2379** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2380** this is important.
2381**
2382** ^(There can only be a single busy handler defined for each
2383** [database connection].  Setting a new busy handler clears any
2384** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2385** will also set or clear the busy handler.
2386**
2387** The busy callback should not take any actions which modify the
2388** database connection that invoked the busy handler.  Any such actions
2389** result in undefined behavior.
2390**
2391** A busy handler must not close the database connection
2392** or [prepared statement] that invoked the busy handler.
2393*/
2394SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2395
2396/*
2397** CAPI3REF: Set A Busy Timeout
2398**
2399** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2400** for a specified amount of time when a table is locked.  ^The handler
2401** will sleep multiple times until at least "ms" milliseconds of sleeping
2402** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2403** the handler returns 0 which causes [sqlite3_step()] to return
2404** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2405**
2406** ^Calling this routine with an argument less than or equal to zero
2407** turns off all busy handlers.
2408**
2409** ^(There can only be a single busy handler for a particular
2410** [database connection] any any given moment.  If another busy handler
2411** was defined  (using [sqlite3_busy_handler()]) prior to calling
2412** this routine, that other busy handler is cleared.)^
2413*/
2414SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2415
2416/*
2417** CAPI3REF: Convenience Routines For Running Queries
2418**
2419** This is a legacy interface that is preserved for backwards compatibility.
2420** Use of this interface is not recommended.
2421**
2422** Definition: A <b>result table</b> is memory data structure created by the
2423** [sqlite3_get_table()] interface.  A result table records the
2424** complete query results from one or more queries.
2425**
2426** The table conceptually has a number of rows and columns.  But
2427** these numbers are not part of the result table itself.  These
2428** numbers are obtained separately.  Let N be the number of rows
2429** and M be the number of columns.
2430**
2431** A result table is an array of pointers to zero-terminated UTF-8 strings.
2432** There are (N+1)*M elements in the array.  The first M pointers point
2433** to zero-terminated strings that  contain the names of the columns.
2434** The remaining entries all point to query results.  NULL values result
2435** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2436** string representation as returned by [sqlite3_column_text()].
2437**
2438** A result table might consist of one or more memory allocations.
2439** It is not safe to pass a result table directly to [sqlite3_free()].
2440** A result table should be deallocated using [sqlite3_free_table()].
2441**
2442** ^(As an example of the result table format, suppose a query result
2443** is as follows:
2444**
2445** <blockquote><pre>
2446**        Name        | Age
2447**        -----------------------
2448**        Alice       | 43
2449**        Bob         | 28
2450**        Cindy       | 21
2451** </pre></blockquote>
2452**
2453** There are two column (M==2) and three rows (N==3).  Thus the
2454** result table has 8 entries.  Suppose the result table is stored
2455** in an array names azResult.  Then azResult holds this content:
2456**
2457** <blockquote><pre>
2458**        azResult&#91;0] = "Name";
2459**        azResult&#91;1] = "Age";
2460**        azResult&#91;2] = "Alice";
2461**        azResult&#91;3] = "43";
2462**        azResult&#91;4] = "Bob";
2463**        azResult&#91;5] = "28";
2464**        azResult&#91;6] = "Cindy";
2465**        azResult&#91;7] = "21";
2466** </pre></blockquote>)^
2467**
2468** ^The sqlite3_get_table() function evaluates one or more
2469** semicolon-separated SQL statements in the zero-terminated UTF-8
2470** string of its 2nd parameter and returns a result table to the
2471** pointer given in its 3rd parameter.
2472**
2473** After the application has finished with the result from sqlite3_get_table(),
2474** it must pass the result table pointer to sqlite3_free_table() in order to
2475** release the memory that was malloced.  Because of the way the
2476** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2477** function must not try to call [sqlite3_free()] directly.  Only
2478** [sqlite3_free_table()] is able to release the memory properly and safely.
2479**
2480** The sqlite3_get_table() interface is implemented as a wrapper around
2481** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2482** to any internal data structures of SQLite.  It uses only the public
2483** interface defined here.  As a consequence, errors that occur in the
2484** wrapper layer outside of the internal [sqlite3_exec()] call are not
2485** reflected in subsequent calls to [sqlite3_errcode()] or
2486** [sqlite3_errmsg()].
2487*/
2488SQLITE_API int sqlite3_get_table(
2489  sqlite3 *db,          /* An open database */
2490  const char *zSql,     /* SQL to be evaluated */
2491  char ***pazResult,    /* Results of the query */
2492  int *pnRow,           /* Number of result rows written here */
2493  int *pnColumn,        /* Number of result columns written here */
2494  char **pzErrmsg       /* Error msg written here */
2495);
2496SQLITE_API void sqlite3_free_table(char **result);
2497
2498/*
2499** CAPI3REF: Formatted String Printing Functions
2500**
2501** These routines are work-alikes of the "printf()" family of functions
2502** from the standard C library.
2503**
2504** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2505** results into memory obtained from [sqlite3_malloc()].
2506** The strings returned by these two routines should be
2507** released by [sqlite3_free()].  ^Both routines return a
2508** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2509** memory to hold the resulting string.
2510**
2511** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2512** the standard C library.  The result is written into the
2513** buffer supplied as the second parameter whose size is given by
2514** the first parameter. Note that the order of the
2515** first two parameters is reversed from snprintf().)^  This is an
2516** historical accident that cannot be fixed without breaking
2517** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2518** returns a pointer to its buffer instead of the number of
2519** characters actually written into the buffer.)^  We admit that
2520** the number of characters written would be a more useful return
2521** value but we cannot change the implementation of sqlite3_snprintf()
2522** now without breaking compatibility.
2523**
2524** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2525** guarantees that the buffer is always zero-terminated.  ^The first
2526** parameter "n" is the total size of the buffer, including space for
2527** the zero terminator.  So the longest string that can be completely
2528** written will be n-1 characters.
2529**
2530** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2531**
2532** These routines all implement some additional formatting
2533** options that are useful for constructing SQL statements.
2534** All of the usual printf() formatting options apply.  In addition, there
2535** is are "%q", "%Q", and "%z" options.
2536**
2537** ^(The %q option works like %s in that it substitutes a null-terminated
2538** string from the argument list.  But %q also doubles every '\'' character.
2539** %q is designed for use inside a string literal.)^  By doubling each '\''
2540** character it escapes that character and allows it to be inserted into
2541** the string.
2542**
2543** For example, assume the string variable zText contains text as follows:
2544**
2545** <blockquote><pre>
2546**  char *zText = "It's a happy day!";
2547** </pre></blockquote>
2548**
2549** One can use this text in an SQL statement as follows:
2550**
2551** <blockquote><pre>
2552**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2553**  sqlite3_exec(db, zSQL, 0, 0, 0);
2554**  sqlite3_free(zSQL);
2555** </pre></blockquote>
2556**
2557** Because the %q format string is used, the '\'' character in zText
2558** is escaped and the SQL generated is as follows:
2559**
2560** <blockquote><pre>
2561**  INSERT INTO table1 VALUES('It''s a happy day!')
2562** </pre></blockquote>
2563**
2564** This is correct.  Had we used %s instead of %q, the generated SQL
2565** would have looked like this:
2566**
2567** <blockquote><pre>
2568**  INSERT INTO table1 VALUES('It's a happy day!');
2569** </pre></blockquote>
2570**
2571** This second example is an SQL syntax error.  As a general rule you should
2572** always use %q instead of %s when inserting text into a string literal.
2573**
2574** ^(The %Q option works like %q except it also adds single quotes around
2575** the outside of the total string.  Additionally, if the parameter in the
2576** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2577** single quotes).)^  So, for example, one could say:
2578**
2579** <blockquote><pre>
2580**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2581**  sqlite3_exec(db, zSQL, 0, 0, 0);
2582**  sqlite3_free(zSQL);
2583** </pre></blockquote>
2584**
2585** The code above will render a correct SQL statement in the zSQL
2586** variable even if the zText variable is a NULL pointer.
2587**
2588** ^(The "%z" formatting option works like "%s" but with the
2589** addition that after the string has been read and copied into
2590** the result, [sqlite3_free()] is called on the input string.)^
2591*/
2592SQLITE_API char *sqlite3_mprintf(const char*,...);
2593SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2594SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2595SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2596
2597/*
2598** CAPI3REF: Memory Allocation Subsystem
2599**
2600** The SQLite core uses these three routines for all of its own
2601** internal memory allocation needs. "Core" in the previous sentence
2602** does not include operating-system specific VFS implementation.  The
2603** Windows VFS uses native malloc() and free() for some operations.
2604**
2605** ^The sqlite3_malloc() routine returns a pointer to a block
2606** of memory at least N bytes in length, where N is the parameter.
2607** ^If sqlite3_malloc() is unable to obtain sufficient free
2608** memory, it returns a NULL pointer.  ^If the parameter N to
2609** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2610** a NULL pointer.
2611**
2612** ^Calling sqlite3_free() with a pointer previously returned
2613** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2614** that it might be reused.  ^The sqlite3_free() routine is
2615** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2616** to sqlite3_free() is harmless.  After being freed, memory
2617** should neither be read nor written.  Even reading previously freed
2618** memory might result in a segmentation fault or other severe error.
2619** Memory corruption, a segmentation fault, or other severe error
2620** might result if sqlite3_free() is called with a non-NULL pointer that
2621** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2622**
2623** ^(The sqlite3_realloc() interface attempts to resize a
2624** prior memory allocation to be at least N bytes, where N is the
2625** second parameter.  The memory allocation to be resized is the first
2626** parameter.)^ ^ If the first parameter to sqlite3_realloc()
2627** is a NULL pointer then its behavior is identical to calling
2628** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
2629** ^If the second parameter to sqlite3_realloc() is zero or
2630** negative then the behavior is exactly the same as calling
2631** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
2632** ^sqlite3_realloc() returns a pointer to a memory allocation
2633** of at least N bytes in size or NULL if sufficient memory is unavailable.
2634** ^If M is the size of the prior allocation, then min(N,M) bytes
2635** of the prior allocation are copied into the beginning of buffer returned
2636** by sqlite3_realloc() and the prior allocation is freed.
2637** ^If sqlite3_realloc() returns NULL, then the prior allocation
2638** is not freed.
2639**
2640** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
2641** is always aligned to at least an 8 byte boundary, or to a
2642** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2643** option is used.
2644**
2645** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2646** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2647** implementation of these routines to be omitted.  That capability
2648** is no longer provided.  Only built-in memory allocators can be used.
2649**
2650** The Windows OS interface layer calls
2651** the system malloc() and free() directly when converting
2652** filenames between the UTF-8 encoding used by SQLite
2653** and whatever filename encoding is used by the particular Windows
2654** installation.  Memory allocation errors are detected, but
2655** they are reported back as [SQLITE_CANTOPEN] or
2656** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2657**
2658** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2659** must be either NULL or else pointers obtained from a prior
2660** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2661** not yet been released.
2662**
2663** The application must not read or write any part of
2664** a block of memory after it has been released using
2665** [sqlite3_free()] or [sqlite3_realloc()].
2666*/
2667SQLITE_API void *sqlite3_malloc(int);
2668SQLITE_API void *sqlite3_realloc(void*, int);
2669SQLITE_API void sqlite3_free(void*);
2670
2671/*
2672** CAPI3REF: Memory Allocator Statistics
2673**
2674** SQLite provides these two interfaces for reporting on the status
2675** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2676** routines, which form the built-in memory allocation subsystem.
2677**
2678** ^The [sqlite3_memory_used()] routine returns the number of bytes
2679** of memory currently outstanding (malloced but not freed).
2680** ^The [sqlite3_memory_highwater()] routine returns the maximum
2681** value of [sqlite3_memory_used()] since the high-water mark
2682** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2683** [sqlite3_memory_highwater()] include any overhead
2684** added by SQLite in its implementation of [sqlite3_malloc()],
2685** but not overhead added by the any underlying system library
2686** routines that [sqlite3_malloc()] may call.
2687**
2688** ^The memory high-water mark is reset to the current value of
2689** [sqlite3_memory_used()] if and only if the parameter to
2690** [sqlite3_memory_highwater()] is true.  ^The value returned
2691** by [sqlite3_memory_highwater(1)] is the high-water mark
2692** prior to the reset.
2693*/
2694SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2695SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2696
2697/*
2698** CAPI3REF: Pseudo-Random Number Generator
2699**
2700** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2701** select random [ROWID | ROWIDs] when inserting new records into a table that
2702** already uses the largest possible [ROWID].  The PRNG is also used for
2703** the build-in random() and randomblob() SQL functions.  This interface allows
2704** applications to access the same PRNG for other purposes.
2705**
2706** ^A call to this routine stores N bytes of randomness into buffer P.
2707**
2708** ^The first time this routine is invoked (either internally or by
2709** the application) the PRNG is seeded using randomness obtained
2710** from the xRandomness method of the default [sqlite3_vfs] object.
2711** ^On all subsequent invocations, the pseudo-randomness is generated
2712** internally and without recourse to the [sqlite3_vfs] xRandomness
2713** method.
2714*/
2715SQLITE_API void sqlite3_randomness(int N, void *P);
2716
2717/*
2718** CAPI3REF: Compile-Time Authorization Callbacks
2719**
2720** ^This routine registers an authorizer callback with a particular
2721** [database connection], supplied in the first argument.
2722** ^The authorizer callback is invoked as SQL statements are being compiled
2723** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2724** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2725** points during the compilation process, as logic is being created
2726** to perform various actions, the authorizer callback is invoked to
2727** see if those actions are allowed.  ^The authorizer callback should
2728** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2729** specific action but allow the SQL statement to continue to be
2730** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2731** rejected with an error.  ^If the authorizer callback returns
2732** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2733** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2734** the authorizer will fail with an error message.
2735**
2736** When the callback returns [SQLITE_OK], that means the operation
2737** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2738** [sqlite3_prepare_v2()] or equivalent call that triggered the
2739** authorizer will fail with an error message explaining that
2740** access is denied.
2741**
2742** ^The first parameter to the authorizer callback is a copy of the third
2743** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2744** to the callback is an integer [SQLITE_COPY | action code] that specifies
2745** the particular action to be authorized. ^The third through sixth parameters
2746** to the callback are zero-terminated strings that contain additional
2747** details about the action to be authorized.
2748**
2749** ^If the action code is [SQLITE_READ]
2750** and the callback returns [SQLITE_IGNORE] then the
2751** [prepared statement] statement is constructed to substitute
2752** a NULL value in place of the table column that would have
2753** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2754** return can be used to deny an untrusted user access to individual
2755** columns of a table.
2756** ^If the action code is [SQLITE_DELETE] and the callback returns
2757** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2758** [truncate optimization] is disabled and all rows are deleted individually.
2759**
2760** An authorizer is used when [sqlite3_prepare | preparing]
2761** SQL statements from an untrusted source, to ensure that the SQL statements
2762** do not try to access data they are not allowed to see, or that they do not
2763** try to execute malicious statements that damage the database.  For
2764** example, an application may allow a user to enter arbitrary
2765** SQL queries for evaluation by a database.  But the application does
2766** not want the user to be able to make arbitrary changes to the
2767** database.  An authorizer could then be put in place while the
2768** user-entered SQL is being [sqlite3_prepare | prepared] that
2769** disallows everything except [SELECT] statements.
2770**
2771** Applications that need to process SQL from untrusted sources
2772** might also consider lowering resource limits using [sqlite3_limit()]
2773** and limiting database size using the [max_page_count] [PRAGMA]
2774** in addition to using an authorizer.
2775**
2776** ^(Only a single authorizer can be in place on a database connection
2777** at a time.  Each call to sqlite3_set_authorizer overrides the
2778** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2779** The authorizer is disabled by default.
2780**
2781** The authorizer callback must not do anything that will modify
2782** the database connection that invoked the authorizer callback.
2783** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2784** database connections for the meaning of "modify" in this paragraph.
2785**
2786** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2787** statement might be re-prepared during [sqlite3_step()] due to a
2788** schema change.  Hence, the application should ensure that the
2789** correct authorizer callback remains in place during the [sqlite3_step()].
2790**
2791** ^Note that the authorizer callback is invoked only during
2792** [sqlite3_prepare()] or its variants.  Authorization is not
2793** performed during statement evaluation in [sqlite3_step()], unless
2794** as stated in the previous paragraph, sqlite3_step() invokes
2795** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2796*/
2797SQLITE_API int sqlite3_set_authorizer(
2798  sqlite3*,
2799  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2800  void *pUserData
2801);
2802
2803/*
2804** CAPI3REF: Authorizer Return Codes
2805**
2806** The [sqlite3_set_authorizer | authorizer callback function] must
2807** return either [SQLITE_OK] or one of these two constants in order
2808** to signal SQLite whether or not the action is permitted.  See the
2809** [sqlite3_set_authorizer | authorizer documentation] for additional
2810** information.
2811**
2812** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2813** from the [sqlite3_vtab_on_conflict()] interface.
2814*/
2815#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2816#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2817
2818/*
2819** CAPI3REF: Authorizer Action Codes
2820**
2821** The [sqlite3_set_authorizer()] interface registers a callback function
2822** that is invoked to authorize certain SQL statement actions.  The
2823** second parameter to the callback is an integer code that specifies
2824** what action is being authorized.  These are the integer action codes that
2825** the authorizer callback may be passed.
2826**
2827** These action code values signify what kind of operation is to be
2828** authorized.  The 3rd and 4th parameters to the authorization
2829** callback function will be parameters or NULL depending on which of these
2830** codes is used as the second parameter.  ^(The 5th parameter to the
2831** authorizer callback is the name of the database ("main", "temp",
2832** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2833** is the name of the inner-most trigger or view that is responsible for
2834** the access attempt or NULL if this access attempt is directly from
2835** top-level SQL code.
2836*/
2837/******************************************* 3rd ************ 4th ***********/
2838#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2839#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2840#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2841#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2842#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2843#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2844#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2845#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2846#define SQLITE_DELETE                9   /* Table Name      NULL            */
2847#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2848#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2849#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2850#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2851#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2852#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2853#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2854#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2855#define SQLITE_INSERT               18   /* Table Name      NULL            */
2856#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2857#define SQLITE_READ                 20   /* Table Name      Column Name     */
2858#define SQLITE_SELECT               21   /* NULL            NULL            */
2859#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2860#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2861#define SQLITE_ATTACH               24   /* Filename        NULL            */
2862#define SQLITE_DETACH               25   /* Database Name   NULL            */
2863#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2864#define SQLITE_REINDEX              27   /* Index Name      NULL            */
2865#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2866#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2867#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2868#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2869#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2870#define SQLITE_COPY                  0   /* No longer used */
2871
2872/*
2873** CAPI3REF: Tracing And Profiling Functions
2874**
2875** These routines register callback functions that can be used for
2876** tracing and profiling the execution of SQL statements.
2877**
2878** ^The callback function registered by sqlite3_trace() is invoked at
2879** various times when an SQL statement is being run by [sqlite3_step()].
2880** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2881** SQL statement text as the statement first begins executing.
2882** ^(Additional sqlite3_trace() callbacks might occur
2883** as each triggered subprogram is entered.  The callbacks for triggers
2884** contain a UTF-8 SQL comment that identifies the trigger.)^
2885**
2886** ^The callback function registered by sqlite3_profile() is invoked
2887** as each SQL statement finishes.  ^The profile callback contains
2888** the original statement text and an estimate of wall-clock time
2889** of how long that statement took to run.  ^The profile callback
2890** time is in units of nanoseconds, however the current implementation
2891** is only capable of millisecond resolution so the six least significant
2892** digits in the time are meaningless.  Future versions of SQLite
2893** might provide greater resolution on the profiler callback.  The
2894** sqlite3_profile() function is considered experimental and is
2895** subject to change in future versions of SQLite.
2896*/
2897SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2898SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
2899   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2900
2901/*
2902** CAPI3REF: Query Progress Callbacks
2903**
2904** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2905** function X to be invoked periodically during long running calls to
2906** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2907** database connection D.  An example use for this
2908** interface is to keep a GUI updated during a large query.
2909**
2910** ^The parameter P is passed through as the only parameter to the
2911** callback function X.  ^The parameter N is the number of
2912** [virtual machine instructions] that are evaluated between successive
2913** invocations of the callback X.
2914**
2915** ^Only a single progress handler may be defined at one time per
2916** [database connection]; setting a new progress handler cancels the
2917** old one.  ^Setting parameter X to NULL disables the progress handler.
2918** ^The progress handler is also disabled by setting N to a value less
2919** than 1.
2920**
2921** ^If the progress callback returns non-zero, the operation is
2922** interrupted.  This feature can be used to implement a
2923** "Cancel" button on a GUI progress dialog box.
2924**
2925** The progress handler callback must not do anything that will modify
2926** the database connection that invoked the progress handler.
2927** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2928** database connections for the meaning of "modify" in this paragraph.
2929**
2930*/
2931SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2932
2933/*
2934** CAPI3REF: Opening A New Database Connection
2935**
2936** ^These routines open an SQLite database file as specified by the
2937** filename argument. ^The filename argument is interpreted as UTF-8 for
2938** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2939** order for sqlite3_open16(). ^(A [database connection] handle is usually
2940** returned in *ppDb, even if an error occurs.  The only exception is that
2941** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2942** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2943** object.)^ ^(If the database is opened (and/or created) successfully, then
2944** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2945** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2946** an English language description of the error following a failure of any
2947** of the sqlite3_open() routines.
2948**
2949** ^The default encoding for the database will be UTF-8 if
2950** sqlite3_open() or sqlite3_open_v2() is called and
2951** UTF-16 in the native byte order if sqlite3_open16() is used.
2952**
2953** Whether or not an error occurs when it is opened, resources
2954** associated with the [database connection] handle should be released by
2955** passing it to [sqlite3_close()] when it is no longer required.
2956**
2957** The sqlite3_open_v2() interface works like sqlite3_open()
2958** except that it accepts two additional parameters for additional control
2959** over the new database connection.  ^(The flags parameter to
2960** sqlite3_open_v2() can take one of
2961** the following three values, optionally combined with the
2962** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2963** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
2964**
2965** <dl>
2966** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2967** <dd>The database is opened in read-only mode.  If the database does not
2968** already exist, an error is returned.</dd>)^
2969**
2970** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
2971** <dd>The database is opened for reading and writing if possible, or reading
2972** only if the file is write protected by the operating system.  In either
2973** case the database must already exist, otherwise an error is returned.</dd>)^
2974**
2975** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
2976** <dd>The database is opened for reading and writing, and is created if
2977** it does not already exist. This is the behavior that is always used for
2978** sqlite3_open() and sqlite3_open16().</dd>)^
2979** </dl>
2980**
2981** If the 3rd parameter to sqlite3_open_v2() is not one of the
2982** combinations shown above optionally combined with other
2983** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
2984** then the behavior is undefined.
2985**
2986** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
2987** opens in the multi-thread [threading mode] as long as the single-thread
2988** mode has not been set at compile-time or start-time.  ^If the
2989** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
2990** in the serialized [threading mode] unless single-thread was
2991** previously selected at compile-time or start-time.
2992** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
2993** eligible to use [shared cache mode], regardless of whether or not shared
2994** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
2995** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
2996** participate in [shared cache mode] even if it is enabled.
2997**
2998** ^The fourth parameter to sqlite3_open_v2() is the name of the
2999** [sqlite3_vfs] object that defines the operating system interface that
3000** the new database connection should use.  ^If the fourth parameter is
3001** a NULL pointer then the default [sqlite3_vfs] object is used.
3002**
3003** ^If the filename is ":memory:", then a private, temporary in-memory database
3004** is created for the connection.  ^This in-memory database will vanish when
3005** the database connection is closed.  Future versions of SQLite might
3006** make use of additional special filenames that begin with the ":" character.
3007** It is recommended that when a database filename actually does begin with
3008** a ":" character you should prefix the filename with a pathname such as
3009** "./" to avoid ambiguity.
3010**
3011** ^If the filename is an empty string, then a private, temporary
3012** on-disk database will be created.  ^This private database will be
3013** automatically deleted as soon as the database connection is closed.
3014**
3015** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3016**
3017** ^If [URI filename] interpretation is enabled, and the filename argument
3018** begins with "file:", then the filename is interpreted as a URI. ^URI
3019** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3020** set in the fourth argument to sqlite3_open_v2(), or if it has
3021** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3022** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3023** As of SQLite version 3.7.7, URI filename interpretation is turned off
3024** by default, but future releases of SQLite might enable URI filename
3025** interpretation by default.  See "[URI filenames]" for additional
3026** information.
3027**
3028** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3029** authority, then it must be either an empty string or the string
3030** "localhost". ^If the authority is not an empty string or "localhost", an
3031** error is returned to the caller. ^The fragment component of a URI, if
3032** present, is ignored.
3033**
3034** ^SQLite uses the path component of the URI as the name of the disk file
3035** which contains the database. ^If the path begins with a '/' character,
3036** then it is interpreted as an absolute path. ^If the path does not begin
3037** with a '/' (meaning that the authority section is omitted from the URI)
3038** then the path is interpreted as a relative path.
3039** ^On windows, the first component of an absolute path
3040** is a drive specification (e.g. "C:").
3041**
3042** [[core URI query parameters]]
3043** The query component of a URI may contain parameters that are interpreted
3044** either by SQLite itself, or by a [VFS | custom VFS implementation].
3045** SQLite interprets the following three query parameters:
3046**
3047** <ul>
3048**   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3049**     a VFS object that provides the operating system interface that should
3050**     be used to access the database file on disk. ^If this option is set to
3051**     an empty string the default VFS object is used. ^Specifying an unknown
3052**     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3053**     present, then the VFS specified by the option takes precedence over
3054**     the value passed as the fourth parameter to sqlite3_open_v2().
3055**
3056**   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw" or
3057**     "rwc". Attempting to set it to any other value is an error)^.
3058**     ^If "ro" is specified, then the database is opened for read-only
3059**     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3060**     third argument to sqlite3_prepare_v2(). ^If the mode option is set to
3061**     "rw", then the database is opened for read-write (but not create)
3062**     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3063**     been set. ^Value "rwc" is equivalent to setting both
3064**     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If sqlite3_open_v2() is
3065**     used, it is an error to specify a value for the mode parameter that is
3066**     less restrictive than that specified by the flags passed as the third
3067**     parameter.
3068**
3069**   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3070**     "private". ^Setting it to "shared" is equivalent to setting the
3071**     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3072**     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3073**     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3074**     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3075**     a URI filename, its value overrides any behaviour requested by setting
3076**     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3077** </ul>
3078**
3079** ^Specifying an unknown parameter in the query component of a URI is not an
3080** error.  Future versions of SQLite might understand additional query
3081** parameters.  See "[query parameters with special meaning to SQLite]" for
3082** additional information.
3083**
3084** [[URI filename examples]] <h3>URI filename examples</h3>
3085**
3086** <table border="1" align=center cellpadding=5>
3087** <tr><th> URI filenames <th> Results
3088** <tr><td> file:data.db <td>
3089**          Open the file "data.db" in the current directory.
3090** <tr><td> file:/home/fred/data.db<br>
3091**          file:///home/fred/data.db <br>
3092**          file://localhost/home/fred/data.db <br> <td>
3093**          Open the database file "/home/fred/data.db".
3094** <tr><td> file://darkstar/home/fred/data.db <td>
3095**          An error. "darkstar" is not a recognized authority.
3096** <tr><td style="white-space:nowrap">
3097**          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3098**     <td> Windows only: Open the file "data.db" on fred's desktop on drive
3099**          C:. Note that the %20 escaping in this example is not strictly
3100**          necessary - space characters can be used literally
3101**          in URI filenames.
3102** <tr><td> file:data.db?mode=ro&cache=private <td>
3103**          Open file "data.db" in the current directory for read-only access.
3104**          Regardless of whether or not shared-cache mode is enabled by
3105**          default, use a private cache.
3106** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
3107**          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
3108** <tr><td> file:data.db?mode=readonly <td>
3109**          An error. "readonly" is not a valid option for the "mode" parameter.
3110** </table>
3111**
3112** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3113** query components of a URI. A hexadecimal escape sequence consists of a
3114** percent sign - "%" - followed by exactly two hexadecimal digits
3115** specifying an octet value. ^Before the path or query components of a
3116** URI filename are interpreted, they are encoded using UTF-8 and all
3117** hexadecimal escape sequences replaced by a single byte containing the
3118** corresponding octet. If this process generates an invalid UTF-8 encoding,
3119** the results are undefined.
3120**
3121** <b>Note to Windows users:</b>  The encoding used for the filename argument
3122** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3123** codepage is currently defined.  Filenames containing international
3124** characters must be converted to UTF-8 prior to passing them into
3125** sqlite3_open() or sqlite3_open_v2().
3126*/
3127SQLITE_API int sqlite3_open(
3128  const char *filename,   /* Database filename (UTF-8) */
3129  sqlite3 **ppDb          /* OUT: SQLite db handle */
3130);
3131SQLITE_API int sqlite3_open16(
3132  const void *filename,   /* Database filename (UTF-16) */
3133  sqlite3 **ppDb          /* OUT: SQLite db handle */
3134);
3135SQLITE_API int sqlite3_open_v2(
3136  const char *filename,   /* Database filename (UTF-8) */
3137  sqlite3 **ppDb,         /* OUT: SQLite db handle */
3138  int flags,              /* Flags */
3139  const char *zVfs        /* Name of VFS module to use */
3140);
3141
3142/*
3143** CAPI3REF: Obtain Values For URI Parameters
3144**
3145** This is a utility routine, useful to VFS implementations, that checks
3146** to see if a database file was a URI that contained a specific query
3147** parameter, and if so obtains the value of the query parameter.
3148**
3149** The zFilename argument is the filename pointer passed into the xOpen()
3150** method of a VFS implementation.  The zParam argument is the name of the
3151** query parameter we seek.  This routine returns the value of the zParam
3152** parameter if it exists.  If the parameter does not exist, this routine
3153** returns a NULL pointer.
3154**
3155** If the zFilename argument to this function is not a pointer that SQLite
3156** passed into the xOpen VFS method, then the behavior of this routine
3157** is undefined and probably undesirable.
3158*/
3159SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3160
3161
3162/*
3163** CAPI3REF: Error Codes And Messages
3164**
3165** ^The sqlite3_errcode() interface returns the numeric [result code] or
3166** [extended result code] for the most recent failed sqlite3_* API call
3167** associated with a [database connection]. If a prior API call failed
3168** but the most recent API call succeeded, the return value from
3169** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
3170** interface is the same except that it always returns the
3171** [extended result code] even when extended result codes are
3172** disabled.
3173**
3174** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3175** text that describes the error, as either UTF-8 or UTF-16 respectively.
3176** ^(Memory to hold the error message string is managed internally.
3177** The application does not need to worry about freeing the result.
3178** However, the error string might be overwritten or deallocated by
3179** subsequent calls to other SQLite interface functions.)^
3180**
3181** When the serialized [threading mode] is in use, it might be the
3182** case that a second error occurs on a separate thread in between
3183** the time of the first error and the call to these interfaces.
3184** When that happens, the second error will be reported since these
3185** interfaces always report the most recent result.  To avoid
3186** this, each thread can obtain exclusive use of the [database connection] D
3187** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
3188** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
3189** all calls to the interfaces listed here are completed.
3190**
3191** If an interface fails with SQLITE_MISUSE, that means the interface
3192** was invoked incorrectly by the application.  In that case, the
3193** error code and message may or may not be set.
3194*/
3195SQLITE_API int sqlite3_errcode(sqlite3 *db);
3196SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3197SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3198SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3199
3200/*
3201** CAPI3REF: SQL Statement Object
3202** KEYWORDS: {prepared statement} {prepared statements}
3203**
3204** An instance of this object represents a single SQL statement.
3205** This object is variously known as a "prepared statement" or a
3206** "compiled SQL statement" or simply as a "statement".
3207**
3208** The life of a statement object goes something like this:
3209**
3210** <ol>
3211** <li> Create the object using [sqlite3_prepare_v2()] or a related
3212**      function.
3213** <li> Bind values to [host parameters] using the sqlite3_bind_*()
3214**      interfaces.
3215** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3216** <li> Reset the statement using [sqlite3_reset()] then go back
3217**      to step 2.  Do this zero or more times.
3218** <li> Destroy the object using [sqlite3_finalize()].
3219** </ol>
3220**
3221** Refer to documentation on individual methods above for additional
3222** information.
3223*/
3224typedef struct sqlite3_stmt sqlite3_stmt;
3225
3226/*
3227** CAPI3REF: Run-time Limits
3228**
3229** ^(This interface allows the size of various constructs to be limited
3230** on a connection by connection basis.  The first parameter is the
3231** [database connection] whose limit is to be set or queried.  The
3232** second parameter is one of the [limit categories] that define a
3233** class of constructs to be size limited.  The third parameter is the
3234** new limit for that construct.)^
3235**
3236** ^If the new limit is a negative number, the limit is unchanged.
3237** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3238** [limits | hard upper bound]
3239** set at compile-time by a C preprocessor macro called
3240** [limits | SQLITE_MAX_<i>NAME</i>].
3241** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3242** ^Attempts to increase a limit above its hard upper bound are
3243** silently truncated to the hard upper bound.
3244**
3245** ^Regardless of whether or not the limit was changed, the
3246** [sqlite3_limit()] interface returns the prior value of the limit.
3247** ^Hence, to find the current value of a limit without changing it,
3248** simply invoke this interface with the third parameter set to -1.
3249**
3250** Run-time limits are intended for use in applications that manage
3251** both their own internal database and also databases that are controlled
3252** by untrusted external sources.  An example application might be a
3253** web browser that has its own databases for storing history and
3254** separate databases controlled by JavaScript applications downloaded
3255** off the Internet.  The internal databases can be given the
3256** large, default limits.  Databases managed by external sources can
3257** be given much smaller limits designed to prevent a denial of service
3258** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3259** interface to further control untrusted SQL.  The size of the database
3260** created by an untrusted script can be contained using the
3261** [max_page_count] [PRAGMA].
3262**
3263** New run-time limit categories may be added in future releases.
3264*/
3265SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3266
3267/*
3268** CAPI3REF: Run-Time Limit Categories
3269** KEYWORDS: {limit category} {*limit categories}
3270**
3271** These constants define various performance limits
3272** that can be lowered at run-time using [sqlite3_limit()].
3273** The synopsis of the meanings of the various limits is shown below.
3274** Additional information is available at [limits | Limits in SQLite].
3275**
3276** <dl>
3277** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3278** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3279**
3280** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3281** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3282**
3283** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3284** <dd>The maximum number of columns in a table definition or in the
3285** result set of a [SELECT] or the maximum number of columns in an index
3286** or in an ORDER BY or GROUP BY clause.</dd>)^
3287**
3288** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3289** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3290**
3291** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3292** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3293**
3294** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3295** <dd>The maximum number of instructions in a virtual machine program
3296** used to implement an SQL statement.  This limit is not currently
3297** enforced, though that might be added in some future release of
3298** SQLite.</dd>)^
3299**
3300** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3301** <dd>The maximum number of arguments on a function.</dd>)^
3302**
3303** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3304** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3305**
3306** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3307** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3308** <dd>The maximum length of the pattern argument to the [LIKE] or
3309** [GLOB] operators.</dd>)^
3310**
3311** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3312** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3313** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3314**
3315** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3316** <dd>The maximum depth of recursion for triggers.</dd>)^
3317** </dl>
3318*/
3319#define SQLITE_LIMIT_LENGTH                    0
3320#define SQLITE_LIMIT_SQL_LENGTH                1
3321#define SQLITE_LIMIT_COLUMN                    2
3322#define SQLITE_LIMIT_EXPR_DEPTH                3
3323#define SQLITE_LIMIT_COMPOUND_SELECT           4
3324#define SQLITE_LIMIT_VDBE_OP                   5
3325#define SQLITE_LIMIT_FUNCTION_ARG              6
3326#define SQLITE_LIMIT_ATTACHED                  7
3327#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3328#define SQLITE_LIMIT_VARIABLE_NUMBER           9
3329#define SQLITE_LIMIT_TRIGGER_DEPTH            10
3330
3331/*
3332** CAPI3REF: Compiling An SQL Statement
3333** KEYWORDS: {SQL statement compiler}
3334**
3335** To execute an SQL query, it must first be compiled into a byte-code
3336** program using one of these routines.
3337**
3338** The first argument, "db", is a [database connection] obtained from a
3339** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3340** [sqlite3_open16()].  The database connection must not have been closed.
3341**
3342** The second argument, "zSql", is the statement to be compiled, encoded
3343** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3344** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3345** use UTF-16.
3346**
3347** ^If the nByte argument is less than zero, then zSql is read up to the
3348** first zero terminator. ^If nByte is non-negative, then it is the maximum
3349** number of  bytes read from zSql.  ^When nByte is non-negative, the
3350** zSql string ends at either the first '\000' or '\u0000' character or
3351** the nByte-th byte, whichever comes first. If the caller knows
3352** that the supplied string is nul-terminated, then there is a small
3353** performance advantage to be gained by passing an nByte parameter that
3354** is equal to the number of bytes in the input string <i>including</i>
3355** the nul-terminator bytes.
3356**
3357** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3358** past the end of the first SQL statement in zSql.  These routines only
3359** compile the first statement in zSql, so *pzTail is left pointing to
3360** what remains uncompiled.
3361**
3362** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3363** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3364** to NULL.  ^If the input text contains no SQL (if the input is an empty
3365** string or a comment) then *ppStmt is set to NULL.
3366** The calling procedure is responsible for deleting the compiled
3367** SQL statement using [sqlite3_finalize()] after it has finished with it.
3368** ppStmt may not be NULL.
3369**
3370** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3371** otherwise an [error code] is returned.
3372**
3373** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3374** recommended for all new programs. The two older interfaces are retained
3375** for backwards compatibility, but their use is discouraged.
3376** ^In the "v2" interfaces, the prepared statement
3377** that is returned (the [sqlite3_stmt] object) contains a copy of the
3378** original SQL text. This causes the [sqlite3_step()] interface to
3379** behave differently in three ways:
3380**
3381** <ol>
3382** <li>
3383** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3384** always used to do, [sqlite3_step()] will automatically recompile the SQL
3385** statement and try to run it again.
3386** </li>
3387**
3388** <li>
3389** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3390** [error codes] or [extended error codes].  ^The legacy behavior was that
3391** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3392** and the application would have to make a second call to [sqlite3_reset()]
3393** in order to find the underlying cause of the problem. With the "v2" prepare
3394** interfaces, the underlying reason for the error is returned immediately.
3395** </li>
3396**
3397** <li>
3398** ^If the specific value bound to [parameter | host parameter] in the
3399** WHERE clause might influence the choice of query plan for a statement,
3400** then the statement will be automatically recompiled, as if there had been
3401** a schema change, on the first  [sqlite3_step()] call following any change
3402** to the [sqlite3_bind_text | bindings] of that [parameter].
3403** ^The specific value of WHERE-clause [parameter] might influence the
3404** choice of query plan if the parameter is the left-hand side of a [LIKE]
3405** or [GLOB] operator or if the parameter is compared to an indexed column
3406** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3407** the
3408** </li>
3409** </ol>
3410*/
3411SQLITE_API int sqlite3_prepare(
3412  sqlite3 *db,            /* Database handle */
3413  const char *zSql,       /* SQL statement, UTF-8 encoded */
3414  int nByte,              /* Maximum length of zSql in bytes. */
3415  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3416  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3417);
3418SQLITE_API int sqlite3_prepare_v2(
3419  sqlite3 *db,            /* Database handle */
3420  const char *zSql,       /* SQL statement, UTF-8 encoded */
3421  int nByte,              /* Maximum length of zSql in bytes. */
3422  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3423  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3424);
3425SQLITE_API int sqlite3_prepare16(
3426  sqlite3 *db,            /* Database handle */
3427  const void *zSql,       /* SQL statement, UTF-16 encoded */
3428  int nByte,              /* Maximum length of zSql in bytes. */
3429  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3430  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3431);
3432SQLITE_API int sqlite3_prepare16_v2(
3433  sqlite3 *db,            /* Database handle */
3434  const void *zSql,       /* SQL statement, UTF-16 encoded */
3435  int nByte,              /* Maximum length of zSql in bytes. */
3436  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3437  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3438);
3439
3440/*
3441** CAPI3REF: Retrieving Statement SQL
3442**
3443** ^This interface can be used to retrieve a saved copy of the original
3444** SQL text used to create a [prepared statement] if that statement was
3445** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3446*/
3447SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3448
3449/*
3450** CAPI3REF: Determine If An SQL Statement Writes The Database
3451**
3452** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3453** and only if the [prepared statement] X makes no direct changes to
3454** the content of the database file.
3455**
3456** Note that [application-defined SQL functions] or
3457** [virtual tables] might change the database indirectly as a side effect.
3458** ^(For example, if an application defines a function "eval()" that
3459** calls [sqlite3_exec()], then the following SQL statement would
3460** change the database file through side-effects:
3461**
3462** <blockquote><pre>
3463**    SELECT eval('DELETE FROM t1') FROM t2;
3464** </pre></blockquote>
3465**
3466** But because the [SELECT] statement does not change the database file
3467** directly, sqlite3_stmt_readonly() would still return true.)^
3468**
3469** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3470** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3471** since the statements themselves do not actually modify the database but
3472** rather they control the timing of when other statements modify the
3473** database.  ^The [ATTACH] and [DETACH] statements also cause
3474** sqlite3_stmt_readonly() to return true since, while those statements
3475** change the configuration of a database connection, they do not make
3476** changes to the content of the database files on disk.
3477*/
3478SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3479
3480/*
3481** CAPI3REF: Dynamically Typed Value Object
3482** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3483**
3484** SQLite uses the sqlite3_value object to represent all values
3485** that can be stored in a database table. SQLite uses dynamic typing
3486** for the values it stores.  ^Values stored in sqlite3_value objects
3487** can be integers, floating point values, strings, BLOBs, or NULL.
3488**
3489** An sqlite3_value object may be either "protected" or "unprotected".
3490** Some interfaces require a protected sqlite3_value.  Other interfaces
3491** will accept either a protected or an unprotected sqlite3_value.
3492** Every interface that accepts sqlite3_value arguments specifies
3493** whether or not it requires a protected sqlite3_value.
3494**
3495** The terms "protected" and "unprotected" refer to whether or not
3496** a mutex is held.  An internal mutex is held for a protected
3497** sqlite3_value object but no mutex is held for an unprotected
3498** sqlite3_value object.  If SQLite is compiled to be single-threaded
3499** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3500** or if SQLite is run in one of reduced mutex modes
3501** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3502** then there is no distinction between protected and unprotected
3503** sqlite3_value objects and they can be used interchangeably.  However,
3504** for maximum code portability it is recommended that applications
3505** still make the distinction between protected and unprotected
3506** sqlite3_value objects even when not strictly required.
3507**
3508** ^The sqlite3_value objects that are passed as parameters into the
3509** implementation of [application-defined SQL functions] are protected.
3510** ^The sqlite3_value object returned by
3511** [sqlite3_column_value()] is unprotected.
3512** Unprotected sqlite3_value objects may only be used with
3513** [sqlite3_result_value()] and [sqlite3_bind_value()].
3514** The [sqlite3_value_blob | sqlite3_value_type()] family of
3515** interfaces require protected sqlite3_value objects.
3516*/
3517typedef struct Mem sqlite3_value;
3518
3519/*
3520** CAPI3REF: SQL Function Context Object
3521**
3522** The context in which an SQL function executes is stored in an
3523** sqlite3_context object.  ^A pointer to an sqlite3_context object
3524** is always first parameter to [application-defined SQL functions].
3525** The application-defined SQL function implementation will pass this
3526** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3527** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3528** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3529** and/or [sqlite3_set_auxdata()].
3530*/
3531typedef struct sqlite3_context sqlite3_context;
3532
3533/*
3534** CAPI3REF: Binding Values To Prepared Statements
3535** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3536** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3537**
3538** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3539** literals may be replaced by a [parameter] that matches one of following
3540** templates:
3541**
3542** <ul>
3543** <li>  ?
3544** <li>  ?NNN
3545** <li>  :VVV
3546** <li>  @VVV
3547** <li>  $VVV
3548** </ul>
3549**
3550** In the templates above, NNN represents an integer literal,
3551** and VVV represents an alphanumeric identifier.)^  ^The values of these
3552** parameters (also called "host parameter names" or "SQL parameters")
3553** can be set using the sqlite3_bind_*() routines defined here.
3554**
3555** ^The first argument to the sqlite3_bind_*() routines is always
3556** a pointer to the [sqlite3_stmt] object returned from
3557** [sqlite3_prepare_v2()] or its variants.
3558**
3559** ^The second argument is the index of the SQL parameter to be set.
3560** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3561** SQL parameter is used more than once, second and subsequent
3562** occurrences have the same index as the first occurrence.
3563** ^The index for named parameters can be looked up using the
3564** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3565** for "?NNN" parameters is the value of NNN.
3566** ^The NNN value must be between 1 and the [sqlite3_limit()]
3567** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3568**
3569** ^The third argument is the value to bind to the parameter.
3570**
3571** ^(In those routines that have a fourth argument, its value is the
3572** number of bytes in the parameter.  To be clear: the value is the
3573** number of <u>bytes</u> in the value, not the number of characters.)^
3574** ^If the fourth parameter is negative, the length of the string is
3575** the number of bytes up to the first zero terminator.
3576**
3577** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3578** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3579** string after SQLite has finished with it.  ^The destructor is called
3580** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3581** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3582** ^If the fifth argument is
3583** the special value [SQLITE_STATIC], then SQLite assumes that the
3584** information is in static, unmanaged space and does not need to be freed.
3585** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3586** SQLite makes its own private copy of the data immediately, before
3587** the sqlite3_bind_*() routine returns.
3588**
3589** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3590** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3591** (just an integer to hold its size) while it is being processed.
3592** Zeroblobs are intended to serve as placeholders for BLOBs whose
3593** content is later written using
3594** [sqlite3_blob_open | incremental BLOB I/O] routines.
3595** ^A negative value for the zeroblob results in a zero-length BLOB.
3596**
3597** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3598** for the [prepared statement] or with a prepared statement for which
3599** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3600** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3601** routine is passed a [prepared statement] that has been finalized, the
3602** result is undefined and probably harmful.
3603**
3604** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3605** ^Unbound parameters are interpreted as NULL.
3606**
3607** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3608** [error code] if anything goes wrong.
3609** ^[SQLITE_RANGE] is returned if the parameter
3610** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3611**
3612** See also: [sqlite3_bind_parameter_count()],
3613** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3614*/
3615SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3616SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
3617SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
3618SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3619SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
3620SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
3621SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3622SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3623SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3624
3625/*
3626** CAPI3REF: Number Of SQL Parameters
3627**
3628** ^This routine can be used to find the number of [SQL parameters]
3629** in a [prepared statement].  SQL parameters are tokens of the
3630** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3631** placeholders for values that are [sqlite3_bind_blob | bound]
3632** to the parameters at a later time.
3633**
3634** ^(This routine actually returns the index of the largest (rightmost)
3635** parameter. For all forms except ?NNN, this will correspond to the
3636** number of unique parameters.  If parameters of the ?NNN form are used,
3637** there may be gaps in the list.)^
3638**
3639** See also: [sqlite3_bind_blob|sqlite3_bind()],
3640** [sqlite3_bind_parameter_name()], and
3641** [sqlite3_bind_parameter_index()].
3642*/
3643SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
3644
3645/*
3646** CAPI3REF: Name Of A Host Parameter
3647**
3648** ^The sqlite3_bind_parameter_name(P,N) interface returns
3649** the name of the N-th [SQL parameter] in the [prepared statement] P.
3650** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3651** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3652** respectively.
3653** In other words, the initial ":" or "$" or "@" or "?"
3654** is included as part of the name.)^
3655** ^Parameters of the form "?" without a following integer have no name
3656** and are referred to as "nameless" or "anonymous parameters".
3657**
3658** ^The first host parameter has an index of 1, not 0.
3659**
3660** ^If the value N is out of range or if the N-th parameter is
3661** nameless, then NULL is returned.  ^The returned string is
3662** always in UTF-8 encoding even if the named parameter was
3663** originally specified as UTF-16 in [sqlite3_prepare16()] or
3664** [sqlite3_prepare16_v2()].
3665**
3666** See also: [sqlite3_bind_blob|sqlite3_bind()],
3667** [sqlite3_bind_parameter_count()], and
3668** [sqlite3_bind_parameter_index()].
3669*/
3670SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3671
3672/*
3673** CAPI3REF: Index Of A Parameter With A Given Name
3674**
3675** ^Return the index of an SQL parameter given its name.  ^The
3676** index value returned is suitable for use as the second
3677** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3678** is returned if no matching parameter is found.  ^The parameter
3679** name must be given in UTF-8 even if the original statement
3680** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3681**
3682** See also: [sqlite3_bind_blob|sqlite3_bind()],
3683** [sqlite3_bind_parameter_count()], and
3684** [sqlite3_bind_parameter_index()].
3685*/
3686SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3687
3688/*
3689** CAPI3REF: Reset All Bindings On A Prepared Statement
3690**
3691** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3692** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3693** ^Use this routine to reset all host parameters to NULL.
3694*/
3695SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
3696
3697/*
3698** CAPI3REF: Number Of Columns In A Result Set
3699**
3700** ^Return the number of columns in the result set returned by the
3701** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3702** statement that does not return data (for example an [UPDATE]).
3703**
3704** See also: [sqlite3_data_count()]
3705*/
3706SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
3707
3708/*
3709** CAPI3REF: Column Names In A Result Set
3710**
3711** ^These routines return the name assigned to a particular column
3712** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3713** interface returns a pointer to a zero-terminated UTF-8 string
3714** and sqlite3_column_name16() returns a pointer to a zero-terminated
3715** UTF-16 string.  ^The first parameter is the [prepared statement]
3716** that implements the [SELECT] statement. ^The second parameter is the
3717** column number.  ^The leftmost column is number 0.
3718**
3719** ^The returned string pointer is valid until either the [prepared statement]
3720** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3721** reprepared by the first call to [sqlite3_step()] for a particular run
3722** or until the next call to
3723** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3724**
3725** ^If sqlite3_malloc() fails during the processing of either routine
3726** (for example during a conversion from UTF-8 to UTF-16) then a
3727** NULL pointer is returned.
3728**
3729** ^The name of a result column is the value of the "AS" clause for
3730** that column, if there is an AS clause.  If there is no AS clause
3731** then the name of the column is unspecified and may change from
3732** one release of SQLite to the next.
3733*/
3734SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
3735SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
3736
3737/*
3738** CAPI3REF: Source Of Data In A Query Result
3739**
3740** ^These routines provide a means to determine the database, table, and
3741** table column that is the origin of a particular result column in
3742** [SELECT] statement.
3743** ^The name of the database or table or column can be returned as
3744** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3745** the database name, the _table_ routines return the table name, and
3746** the origin_ routines return the column name.
3747** ^The returned string is valid until the [prepared statement] is destroyed
3748** using [sqlite3_finalize()] or until the statement is automatically
3749** reprepared by the first call to [sqlite3_step()] for a particular run
3750** or until the same information is requested
3751** again in a different encoding.
3752**
3753** ^The names returned are the original un-aliased names of the
3754** database, table, and column.
3755**
3756** ^The first argument to these interfaces is a [prepared statement].
3757** ^These functions return information about the Nth result column returned by
3758** the statement, where N is the second function argument.
3759** ^The left-most column is column 0 for these routines.
3760**
3761** ^If the Nth column returned by the statement is an expression or
3762** subquery and is not a column value, then all of these functions return
3763** NULL.  ^These routine might also return NULL if a memory allocation error
3764** occurs.  ^Otherwise, they return the name of the attached database, table,
3765** or column that query result column was extracted from.
3766**
3767** ^As with all other SQLite APIs, those whose names end with "16" return
3768** UTF-16 encoded strings and the other functions return UTF-8.
3769**
3770** ^These APIs are only available if the library was compiled with the
3771** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3772**
3773** If two or more threads call one or more of these routines against the same
3774** prepared statement and column at the same time then the results are
3775** undefined.
3776**
3777** If two or more threads call one or more
3778** [sqlite3_column_database_name | column metadata interfaces]
3779** for the same [prepared statement] and result column
3780** at the same time then the results are undefined.
3781*/
3782SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
3783SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
3784SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
3785SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
3786SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
3787SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
3788
3789/*
3790** CAPI3REF: Declared Datatype Of A Query Result
3791**
3792** ^(The first parameter is a [prepared statement].
3793** If this statement is a [SELECT] statement and the Nth column of the
3794** returned result set of that [SELECT] is a table column (not an
3795** expression or subquery) then the declared type of the table
3796** column is returned.)^  ^If the Nth column of the result set is an
3797** expression or subquery, then a NULL pointer is returned.
3798** ^The returned string is always UTF-8 encoded.
3799**
3800** ^(For example, given the database schema:
3801**
3802** CREATE TABLE t1(c1 VARIANT);
3803**
3804** and the following statement to be compiled:
3805**
3806** SELECT c1 + 1, c1 FROM t1;
3807**
3808** this routine would return the string "VARIANT" for the second result
3809** column (i==1), and a NULL pointer for the first result column (i==0).)^
3810**
3811** ^SQLite uses dynamic run-time typing.  ^So just because a column
3812** is declared to contain a particular type does not mean that the
3813** data stored in that column is of the declared type.  SQLite is
3814** strongly typed, but the typing is dynamic not static.  ^Type
3815** is associated with individual values, not with the containers
3816** used to hold those values.
3817*/
3818SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
3819SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
3820
3821/*
3822** CAPI3REF: Evaluate An SQL Statement
3823**
3824** After a [prepared statement] has been prepared using either
3825** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3826** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3827** must be called one or more times to evaluate the statement.
3828**
3829** The details of the behavior of the sqlite3_step() interface depend
3830** on whether the statement was prepared using the newer "v2" interface
3831** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3832** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3833** new "v2" interface is recommended for new applications but the legacy
3834** interface will continue to be supported.
3835**
3836** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
3837** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
3838** ^With the "v2" interface, any of the other [result codes] or
3839** [extended result codes] might be returned as well.
3840**
3841** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
3842** database locks it needs to do its job.  ^If the statement is a [COMMIT]
3843** or occurs outside of an explicit transaction, then you can retry the
3844** statement.  If the statement is not a [COMMIT] and occurs within an
3845** explicit transaction then you should rollback the transaction before
3846** continuing.
3847**
3848** ^[SQLITE_DONE] means that the statement has finished executing
3849** successfully.  sqlite3_step() should not be called again on this virtual
3850** machine without first calling [sqlite3_reset()] to reset the virtual
3851** machine back to its initial state.
3852**
3853** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
3854** is returned each time a new row of data is ready for processing by the
3855** caller. The values may be accessed using the [column access functions].
3856** sqlite3_step() is called again to retrieve the next row of data.
3857**
3858** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
3859** violation) has occurred.  sqlite3_step() should not be called again on
3860** the VM. More information may be found by calling [sqlite3_errmsg()].
3861** ^With the legacy interface, a more specific error code (for example,
3862** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
3863** can be obtained by calling [sqlite3_reset()] on the
3864** [prepared statement].  ^In the "v2" interface,
3865** the more specific error code is returned directly by sqlite3_step().
3866**
3867** [SQLITE_MISUSE] means that the this routine was called inappropriately.
3868** Perhaps it was called on a [prepared statement] that has
3869** already been [sqlite3_finalize | finalized] or on one that had
3870** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
3871** be the case that the same database connection is being used by two or
3872** more threads at the same moment in time.
3873**
3874** For all versions of SQLite up to and including 3.6.23.1, a call to
3875** [sqlite3_reset()] was required after sqlite3_step() returned anything
3876** other than [SQLITE_ROW] before any subsequent invocation of
3877** sqlite3_step().  Failure to reset the prepared statement using
3878** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3879** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
3880** calling [sqlite3_reset()] automatically in this circumstance rather
3881** than returning [SQLITE_MISUSE].  This is not considered a compatibility
3882** break because any application that ever receives an SQLITE_MISUSE error
3883** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
3884** can be used to restore the legacy behavior.
3885**
3886** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3887** API always returns a generic error code, [SQLITE_ERROR], following any
3888** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
3889** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3890** specific [error codes] that better describes the error.
3891** We admit that this is a goofy design.  The problem has been fixed
3892** with the "v2" interface.  If you prepare all of your SQL statements
3893** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
3894** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
3895** then the more specific [error codes] are returned directly
3896** by sqlite3_step().  The use of the "v2" interface is recommended.
3897*/
3898SQLITE_API int sqlite3_step(sqlite3_stmt*);
3899
3900/*
3901** CAPI3REF: Number of columns in a result set
3902**
3903** ^The sqlite3_data_count(P) interface returns the number of columns in the
3904** current row of the result set of [prepared statement] P.
3905** ^If prepared statement P does not have results ready to return
3906** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3907** interfaces) then sqlite3_data_count(P) returns 0.
3908** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3909**
3910** See also: [sqlite3_column_count()]
3911*/
3912SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3913
3914/*
3915** CAPI3REF: Fundamental Datatypes
3916** KEYWORDS: SQLITE_TEXT
3917**
3918** ^(Every value in SQLite has one of five fundamental datatypes:
3919**
3920** <ul>
3921** <li> 64-bit signed integer
3922** <li> 64-bit IEEE floating point number
3923** <li> string
3924** <li> BLOB
3925** <li> NULL
3926** </ul>)^
3927**
3928** These constants are codes for each of those types.
3929**
3930** Note that the SQLITE_TEXT constant was also used in SQLite version 2
3931** for a completely different meaning.  Software that links against both
3932** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
3933** SQLITE_TEXT.
3934*/
3935#define SQLITE_INTEGER  1
3936#define SQLITE_FLOAT    2
3937#define SQLITE_BLOB     4
3938#define SQLITE_NULL     5
3939#ifdef SQLITE_TEXT
3940# undef SQLITE_TEXT
3941#else
3942# define SQLITE_TEXT     3
3943#endif
3944#define SQLITE3_TEXT     3
3945
3946/*
3947** CAPI3REF: Result Values From A Query
3948** KEYWORDS: {column access functions}
3949**
3950** These routines form the "result set" interface.
3951**
3952** ^These routines return information about a single column of the current
3953** result row of a query.  ^In every case the first argument is a pointer
3954** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3955** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3956** and the second argument is the index of the column for which information
3957** should be returned. ^The leftmost column of the result set has the index 0.
3958** ^The number of columns in the result can be determined using
3959** [sqlite3_column_count()].
3960**
3961** If the SQL statement does not currently point to a valid row, or if the
3962** column index is out of range, the result is undefined.
3963** These routines may only be called when the most recent call to
3964** [sqlite3_step()] has returned [SQLITE_ROW] and neither
3965** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
3966** If any of these routines are called after [sqlite3_reset()] or
3967** [sqlite3_finalize()] or after [sqlite3_step()] has returned
3968** something other than [SQLITE_ROW], the results are undefined.
3969** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
3970** are called from a different thread while any of these routines
3971** are pending, then the results are undefined.
3972**
3973** ^The sqlite3_column_type() routine returns the
3974** [SQLITE_INTEGER | datatype code] for the initial data type
3975** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
3976** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
3977** returned by sqlite3_column_type() is only meaningful if no type
3978** conversions have occurred as described below.  After a type conversion,
3979** the value returned by sqlite3_column_type() is undefined.  Future
3980** versions of SQLite may change the behavior of sqlite3_column_type()
3981** following a type conversion.
3982**
3983** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
3984** routine returns the number of bytes in that BLOB or string.
3985** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
3986** the string to UTF-8 and then returns the number of bytes.
3987** ^If the result is a numeric value then sqlite3_column_bytes() uses
3988** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
3989** the number of bytes in that string.
3990** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
3991**
3992** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
3993** routine returns the number of bytes in that BLOB or string.
3994** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
3995** the string to UTF-16 and then returns the number of bytes.
3996** ^If the result is a numeric value then sqlite3_column_bytes16() uses
3997** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
3998** the number of bytes in that string.
3999** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
4000**
4001** ^The values returned by [sqlite3_column_bytes()] and
4002** [sqlite3_column_bytes16()] do not include the zero terminators at the end
4003** of the string.  ^For clarity: the values returned by
4004** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
4005** bytes in the string, not the number of characters.
4006**
4007** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4008** even empty strings, are always zero terminated.  ^The return
4009** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4010**
4011** ^The object returned by [sqlite3_column_value()] is an
4012** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
4013** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
4014** If the [unprotected sqlite3_value] object returned by
4015** [sqlite3_column_value()] is used in any other way, including calls
4016** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4017** or [sqlite3_value_bytes()], then the behavior is undefined.
4018**
4019** These routines attempt to convert the value where appropriate.  ^For
4020** example, if the internal representation is FLOAT and a text result
4021** is requested, [sqlite3_snprintf()] is used internally to perform the
4022** conversion automatically.  ^(The following table details the conversions
4023** that are applied:
4024**
4025** <blockquote>
4026** <table border="1">
4027** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
4028**
4029** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
4030** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
4031** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
4032** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
4033** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
4034** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
4035** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
4036** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
4037** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
4038** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
4039** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
4040** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
4041** <tr><td>  TEXT    <td>   BLOB    <td> No change
4042** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
4043** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
4044** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
4045** </table>
4046** </blockquote>)^
4047**
4048** The table above makes reference to standard C library functions atoi()
4049** and atof().  SQLite does not really use these functions.  It has its
4050** own equivalent internal routines.  The atoi() and atof() names are
4051** used in the table for brevity and because they are familiar to most
4052** C programmers.
4053**
4054** Note that when type conversions occur, pointers returned by prior
4055** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4056** sqlite3_column_text16() may be invalidated.
4057** Type conversions and pointer invalidations might occur
4058** in the following cases:
4059**
4060** <ul>
4061** <li> The initial content is a BLOB and sqlite3_column_text() or
4062**      sqlite3_column_text16() is called.  A zero-terminator might
4063**      need to be added to the string.</li>
4064** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
4065**      sqlite3_column_text16() is called.  The content must be converted
4066**      to UTF-16.</li>
4067** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
4068**      sqlite3_column_text() is called.  The content must be converted
4069**      to UTF-8.</li>
4070** </ul>
4071**
4072** ^Conversions between UTF-16be and UTF-16le are always done in place and do
4073** not invalidate a prior pointer, though of course the content of the buffer
4074** that the prior pointer references will have been modified.  Other kinds
4075** of conversion are done in place when it is possible, but sometimes they
4076** are not possible and in those cases prior pointers are invalidated.
4077**
4078** The safest and easiest to remember policy is to invoke these routines
4079** in one of the following ways:
4080**
4081** <ul>
4082**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4083**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
4084**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
4085** </ul>
4086**
4087** In other words, you should call sqlite3_column_text(),
4088** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
4089** into the desired format, then invoke sqlite3_column_bytes() or
4090** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
4091** to sqlite3_column_text() or sqlite3_column_blob() with calls to
4092** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
4093** with calls to sqlite3_column_bytes().
4094**
4095** ^The pointers returned are valid until a type conversion occurs as
4096** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4097** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
4098** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
4099** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4100** [sqlite3_free()].
4101**
4102** ^(If a memory allocation error occurs during the evaluation of any
4103** of these routines, a default value is returned.  The default value
4104** is either the integer 0, the floating point number 0.0, or a NULL
4105** pointer.  Subsequent calls to [sqlite3_errcode()] will return
4106** [SQLITE_NOMEM].)^
4107*/
4108SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
4109SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
4110SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
4111SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
4112SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
4113SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
4114SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
4115SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
4116SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
4117SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
4118
4119/*
4120** CAPI3REF: Destroy A Prepared Statement Object
4121**
4122** ^The sqlite3_finalize() function is called to delete a [prepared statement].
4123** ^If the most recent evaluation of the statement encountered no errors
4124** or if the statement is never been evaluated, then sqlite3_finalize() returns
4125** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
4126** sqlite3_finalize(S) returns the appropriate [error code] or
4127** [extended error code].
4128**
4129** ^The sqlite3_finalize(S) routine can be called at any point during
4130** the life cycle of [prepared statement] S:
4131** before statement S is ever evaluated, after
4132** one or more calls to [sqlite3_reset()], or after any call
4133** to [sqlite3_step()] regardless of whether or not the statement has
4134** completed execution.
4135**
4136** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
4137**
4138** The application must finalize every [prepared statement] in order to avoid
4139** resource leaks.  It is a grievous error for the application to try to use
4140** a prepared statement after it has been finalized.  Any use of a prepared
4141** statement after it has been finalized can result in undefined and
4142** undesirable behavior such as segfaults and heap corruption.
4143*/
4144SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
4145
4146/*
4147** CAPI3REF: Reset A Prepared Statement Object
4148**
4149** The sqlite3_reset() function is called to reset a [prepared statement]
4150** object back to its initial state, ready to be re-executed.
4151** ^Any SQL statement variables that had values bound to them using
4152** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
4153** Use [sqlite3_clear_bindings()] to reset the bindings.
4154**
4155** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
4156** back to the beginning of its program.
4157**
4158** ^If the most recent call to [sqlite3_step(S)] for the
4159** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
4160** or if [sqlite3_step(S)] has never before been called on S,
4161** then [sqlite3_reset(S)] returns [SQLITE_OK].
4162**
4163** ^If the most recent call to [sqlite3_step(S)] for the
4164** [prepared statement] S indicated an error, then
4165** [sqlite3_reset(S)] returns an appropriate [error code].
4166**
4167** ^The [sqlite3_reset(S)] interface does not change the values
4168** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
4169*/
4170SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
4171
4172/*
4173** CAPI3REF: Create Or Redefine SQL Functions
4174** KEYWORDS: {function creation routines}
4175** KEYWORDS: {application-defined SQL function}
4176** KEYWORDS: {application-defined SQL functions}
4177**
4178** ^These functions (collectively known as "function creation routines")
4179** are used to add SQL functions or aggregates or to redefine the behavior
4180** of existing SQL functions or aggregates.  The only differences between
4181** these routines are the text encoding expected for
4182** the second parameter (the name of the function being created)
4183** and the presence or absence of a destructor callback for
4184** the application data pointer.
4185**
4186** ^The first parameter is the [database connection] to which the SQL
4187** function is to be added.  ^If an application uses more than one database
4188** connection then application-defined SQL functions must be added
4189** to each database connection separately.
4190**
4191** ^The second parameter is the name of the SQL function to be created or
4192** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
4193** representation, exclusive of the zero-terminator.  ^Note that the name
4194** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
4195** ^Any attempt to create a function with a longer name
4196** will result in [SQLITE_MISUSE] being returned.
4197**
4198** ^The third parameter (nArg)
4199** is the number of arguments that the SQL function or
4200** aggregate takes. ^If this parameter is -1, then the SQL function or
4201** aggregate may take any number of arguments between 0 and the limit
4202** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4203** parameter is less than -1 or greater than 127 then the behavior is
4204** undefined.
4205**
4206** ^The fourth parameter, eTextRep, specifies what
4207** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4208** its parameters.  Every SQL function implementation must be able to work
4209** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
4210** more efficient with one encoding than another.  ^An application may
4211** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
4212** times with the same function but with different values of eTextRep.
4213** ^When multiple implementations of the same function are available, SQLite
4214** will pick the one that involves the least amount of data conversion.
4215** If there is only a single implementation which does not care what text
4216** encoding is used, then the fourth argument should be [SQLITE_ANY].
4217**
4218** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4219** function can gain access to this pointer using [sqlite3_user_data()].)^
4220**
4221** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4222** pointers to C-language functions that implement the SQL function or
4223** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4224** callback only; NULL pointers must be passed as the xStep and xFinal
4225** parameters. ^An aggregate SQL function requires an implementation of xStep
4226** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4227** SQL function or aggregate, pass NULL pointers for all three function
4228** callbacks.
4229**
4230** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4231** then it is destructor for the application data pointer.
4232** The destructor is invoked when the function is deleted, either by being
4233** overloaded or when the database connection closes.)^
4234** ^The destructor is also invoked if the call to
4235** sqlite3_create_function_v2() fails.
4236** ^When the destructor callback of the tenth parameter is invoked, it
4237** is passed a single argument which is a copy of the application data
4238** pointer which was the fifth parameter to sqlite3_create_function_v2().
4239**
4240** ^It is permitted to register multiple implementations of the same
4241** functions with the same name but with either differing numbers of
4242** arguments or differing preferred text encodings.  ^SQLite will use
4243** the implementation that most closely matches the way in which the
4244** SQL function is used.  ^A function implementation with a non-negative
4245** nArg parameter is a better match than a function implementation with
4246** a negative nArg.  ^A function where the preferred text encoding
4247** matches the database encoding is a better
4248** match than a function where the encoding is different.
4249** ^A function where the encoding difference is between UTF16le and UTF16be
4250** is a closer match than a function where the encoding difference is
4251** between UTF8 and UTF16.
4252**
4253** ^Built-in functions may be overloaded by new application-defined functions.
4254**
4255** ^An application-defined function is permitted to call other
4256** SQLite interfaces.  However, such calls must not
4257** close the database connection nor finalize or reset the prepared
4258** statement in which the function is running.
4259*/
4260SQLITE_API int sqlite3_create_function(
4261  sqlite3 *db,
4262  const char *zFunctionName,
4263  int nArg,
4264  int eTextRep,
4265  void *pApp,
4266  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4267  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4268  void (*xFinal)(sqlite3_context*)
4269);
4270SQLITE_API int sqlite3_create_function16(
4271  sqlite3 *db,
4272  const void *zFunctionName,
4273  int nArg,
4274  int eTextRep,
4275  void *pApp,
4276  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4277  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4278  void (*xFinal)(sqlite3_context*)
4279);
4280SQLITE_API int sqlite3_create_function_v2(
4281  sqlite3 *db,
4282  const char *zFunctionName,
4283  int nArg,
4284  int eTextRep,
4285  void *pApp,
4286  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4287  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4288  void (*xFinal)(sqlite3_context*),
4289  void(*xDestroy)(void*)
4290);
4291
4292/*
4293** CAPI3REF: Text Encodings
4294**
4295** These constant define integer codes that represent the various
4296** text encodings supported by SQLite.
4297*/
4298#define SQLITE_UTF8           1
4299#define SQLITE_UTF16LE        2
4300#define SQLITE_UTF16BE        3
4301#define SQLITE_UTF16          4    /* Use native byte order */
4302#define SQLITE_ANY            5    /* sqlite3_create_function only */
4303#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4304
4305/*
4306** CAPI3REF: Deprecated Functions
4307** DEPRECATED
4308**
4309** These functions are [deprecated].  In order to maintain
4310** backwards compatibility with older code, these functions continue
4311** to be supported.  However, new applications should avoid
4312** the use of these functions.  To help encourage people to avoid
4313** using these functions, we are not going to tell you what they do.
4314*/
4315#ifndef SQLITE_OMIT_DEPRECATED
4316SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4317SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4318SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4319SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
4320SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
4321SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
4322#endif
4323
4324/*
4325** CAPI3REF: Obtaining SQL Function Parameter Values
4326**
4327** The C-language implementation of SQL functions and aggregates uses
4328** this set of interface routines to access the parameter values on
4329** the function or aggregate.
4330**
4331** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4332** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4333** define callbacks that implement the SQL functions and aggregates.
4334** The 3rd parameter to these callbacks is an array of pointers to
4335** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4336** each parameter to the SQL function.  These routines are used to
4337** extract values from the [sqlite3_value] objects.
4338**
4339** These routines work only with [protected sqlite3_value] objects.
4340** Any attempt to use these routines on an [unprotected sqlite3_value]
4341** object results in undefined behavior.
4342**
4343** ^These routines work just like the corresponding [column access functions]
4344** except that  these routines take a single [protected sqlite3_value] object
4345** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4346**
4347** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4348** in the native byte-order of the host machine.  ^The
4349** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4350** extract UTF-16 strings as big-endian and little-endian respectively.
4351**
4352** ^(The sqlite3_value_numeric_type() interface attempts to apply
4353** numeric affinity to the value.  This means that an attempt is
4354** made to convert the value to an integer or floating point.  If
4355** such a conversion is possible without loss of information (in other
4356** words, if the value is a string that looks like a number)
4357** then the conversion is performed.  Otherwise no conversion occurs.
4358** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4359**
4360** Please pay particular attention to the fact that the pointer returned
4361** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4362** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4363** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4364** or [sqlite3_value_text16()].
4365**
4366** These routines must be called from the same thread as
4367** the SQL function that supplied the [sqlite3_value*] parameters.
4368*/
4369SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
4370SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
4371SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
4372SQLITE_API double sqlite3_value_double(sqlite3_value*);
4373SQLITE_API int sqlite3_value_int(sqlite3_value*);
4374SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
4375SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
4376SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
4377SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
4378SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
4379SQLITE_API int sqlite3_value_type(sqlite3_value*);
4380SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
4381
4382/*
4383** CAPI3REF: Obtain Aggregate Function Context
4384**
4385** Implementations of aggregate SQL functions use this
4386** routine to allocate memory for storing their state.
4387**
4388** ^The first time the sqlite3_aggregate_context(C,N) routine is called
4389** for a particular aggregate function, SQLite
4390** allocates N of memory, zeroes out that memory, and returns a pointer
4391** to the new memory. ^On second and subsequent calls to
4392** sqlite3_aggregate_context() for the same aggregate function instance,
4393** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4394** called once for each invocation of the xStep callback and then one
4395** last time when the xFinal callback is invoked.  ^(When no rows match
4396** an aggregate query, the xStep() callback of the aggregate function
4397** implementation is never called and xFinal() is called exactly once.
4398** In those cases, sqlite3_aggregate_context() might be called for the
4399** first time from within xFinal().)^
4400**
4401** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
4402** less than or equal to zero or if a memory allocate error occurs.
4403**
4404** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4405** determined by the N parameter on first successful call.  Changing the
4406** value of N in subsequent call to sqlite3_aggregate_context() within
4407** the same aggregate function instance will not resize the memory
4408** allocation.)^
4409**
4410** ^SQLite automatically frees the memory allocated by
4411** sqlite3_aggregate_context() when the aggregate query concludes.
4412**
4413** The first parameter must be a copy of the
4414** [sqlite3_context | SQL function context] that is the first parameter
4415** to the xStep or xFinal callback routine that implements the aggregate
4416** function.
4417**
4418** This routine must be called from the same thread in which
4419** the aggregate SQL function is running.
4420*/
4421SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4422
4423/*
4424** CAPI3REF: User Data For Functions
4425**
4426** ^The sqlite3_user_data() interface returns a copy of
4427** the pointer that was the pUserData parameter (the 5th parameter)
4428** of the [sqlite3_create_function()]
4429** and [sqlite3_create_function16()] routines that originally
4430** registered the application defined function.
4431**
4432** This routine must be called from the same thread in which
4433** the application-defined function is running.
4434*/
4435SQLITE_API void *sqlite3_user_data(sqlite3_context*);
4436
4437/*
4438** CAPI3REF: Database Connection For Functions
4439**
4440** ^The sqlite3_context_db_handle() interface returns a copy of
4441** the pointer to the [database connection] (the 1st parameter)
4442** of the [sqlite3_create_function()]
4443** and [sqlite3_create_function16()] routines that originally
4444** registered the application defined function.
4445*/
4446SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
4447
4448/*
4449** CAPI3REF: Function Auxiliary Data
4450**
4451** The following two functions may be used by scalar SQL functions to
4452** associate metadata with argument values. If the same value is passed to
4453** multiple invocations of the same SQL function during query execution, under
4454** some circumstances the associated metadata may be preserved. This may
4455** be used, for example, to add a regular-expression matching scalar
4456** function. The compiled version of the regular expression is stored as
4457** metadata associated with the SQL value passed as the regular expression
4458** pattern.  The compiled regular expression can be reused on multiple
4459** invocations of the same function so that the original pattern string
4460** does not need to be recompiled on each invocation.
4461**
4462** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4463** associated by the sqlite3_set_auxdata() function with the Nth argument
4464** value to the application-defined function. ^If no metadata has been ever
4465** been set for the Nth argument of the function, or if the corresponding
4466** function parameter has changed since the meta-data was set,
4467** then sqlite3_get_auxdata() returns a NULL pointer.
4468**
4469** ^The sqlite3_set_auxdata() interface saves the metadata
4470** pointed to by its 3rd parameter as the metadata for the N-th
4471** argument of the application-defined function.  Subsequent
4472** calls to sqlite3_get_auxdata() might return this data, if it has
4473** not been destroyed.
4474** ^If it is not NULL, SQLite will invoke the destructor
4475** function given by the 4th parameter to sqlite3_set_auxdata() on
4476** the metadata when the corresponding function parameter changes
4477** or when the SQL statement completes, whichever comes first.
4478**
4479** SQLite is free to call the destructor and drop metadata on any
4480** parameter of any function at any time.  ^The only guarantee is that
4481** the destructor will be called before the metadata is dropped.
4482**
4483** ^(In practice, metadata is preserved between function calls for
4484** expressions that are constant at compile time. This includes literal
4485** values and [parameters].)^
4486**
4487** These routines must be called from the same thread in which
4488** the SQL function is running.
4489*/
4490SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
4491SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4492
4493
4494/*
4495** CAPI3REF: Constants Defining Special Destructor Behavior
4496**
4497** These are special values for the destructor that is passed in as the
4498** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4499** argument is SQLITE_STATIC, it means that the content pointer is constant
4500** and will never change.  It does not need to be destroyed.  ^The
4501** SQLITE_TRANSIENT value means that the content will likely change in
4502** the near future and that SQLite should make its own private copy of
4503** the content before returning.
4504**
4505** The typedef is necessary to work around problems in certain
4506** C++ compilers.  See ticket #2191.
4507*/
4508typedef void (*sqlite3_destructor_type)(void*);
4509#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4510#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4511
4512/*
4513** CAPI3REF: Setting The Result Of An SQL Function
4514**
4515** These routines are used by the xFunc or xFinal callbacks that
4516** implement SQL functions and aggregates.  See
4517** [sqlite3_create_function()] and [sqlite3_create_function16()]
4518** for additional information.
4519**
4520** These functions work very much like the [parameter binding] family of
4521** functions used to bind values to host parameters in prepared statements.
4522** Refer to the [SQL parameter] documentation for additional information.
4523**
4524** ^The sqlite3_result_blob() interface sets the result from
4525** an application-defined function to be the BLOB whose content is pointed
4526** to by the second parameter and which is N bytes long where N is the
4527** third parameter.
4528**
4529** ^The sqlite3_result_zeroblob() interfaces set the result of
4530** the application-defined function to be a BLOB containing all zero
4531** bytes and N bytes in size, where N is the value of the 2nd parameter.
4532**
4533** ^The sqlite3_result_double() interface sets the result from
4534** an application-defined function to be a floating point value specified
4535** by its 2nd argument.
4536**
4537** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4538** cause the implemented SQL function to throw an exception.
4539** ^SQLite uses the string pointed to by the
4540** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4541** as the text of an error message.  ^SQLite interprets the error
4542** message string from sqlite3_result_error() as UTF-8. ^SQLite
4543** interprets the string from sqlite3_result_error16() as UTF-16 in native
4544** byte order.  ^If the third parameter to sqlite3_result_error()
4545** or sqlite3_result_error16() is negative then SQLite takes as the error
4546** message all text up through the first zero character.
4547** ^If the third parameter to sqlite3_result_error() or
4548** sqlite3_result_error16() is non-negative then SQLite takes that many
4549** bytes (not characters) from the 2nd parameter as the error message.
4550** ^The sqlite3_result_error() and sqlite3_result_error16()
4551** routines make a private copy of the error message text before
4552** they return.  Hence, the calling function can deallocate or
4553** modify the text after they return without harm.
4554** ^The sqlite3_result_error_code() function changes the error code
4555** returned by SQLite as a result of an error in a function.  ^By default,
4556** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4557** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4558**
4559** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
4560** indicating that a string or BLOB is too long to represent.
4561**
4562** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
4563** indicating that a memory allocation failed.
4564**
4565** ^The sqlite3_result_int() interface sets the return value
4566** of the application-defined function to be the 32-bit signed integer
4567** value given in the 2nd argument.
4568** ^The sqlite3_result_int64() interface sets the return value
4569** of the application-defined function to be the 64-bit signed integer
4570** value given in the 2nd argument.
4571**
4572** ^The sqlite3_result_null() interface sets the return value
4573** of the application-defined function to be NULL.
4574**
4575** ^The sqlite3_result_text(), sqlite3_result_text16(),
4576** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4577** set the return value of the application-defined function to be
4578** a text string which is represented as UTF-8, UTF-16 native byte order,
4579** UTF-16 little endian, or UTF-16 big endian, respectively.
4580** ^SQLite takes the text result from the application from
4581** the 2nd parameter of the sqlite3_result_text* interfaces.
4582** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4583** is negative, then SQLite takes result text from the 2nd parameter
4584** through the first zero character.
4585** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4586** is non-negative, then as many bytes (not characters) of the text
4587** pointed to by the 2nd parameter are taken as the application-defined
4588** function result.
4589** ^If the 4th parameter to the sqlite3_result_text* interfaces
4590** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4591** function as the destructor on the text or BLOB result when it has
4592** finished using that result.
4593** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4594** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4595** assumes that the text or BLOB result is in constant space and does not
4596** copy the content of the parameter nor call a destructor on the content
4597** when it has finished using that result.
4598** ^If the 4th parameter to the sqlite3_result_text* interfaces
4599** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4600** then SQLite makes a copy of the result into space obtained from
4601** from [sqlite3_malloc()] before it returns.
4602**
4603** ^The sqlite3_result_value() interface sets the result of
4604** the application-defined function to be a copy the
4605** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4606** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4607** so that the [sqlite3_value] specified in the parameter may change or
4608** be deallocated after sqlite3_result_value() returns without harm.
4609** ^A [protected sqlite3_value] object may always be used where an
4610** [unprotected sqlite3_value] object is required, so either
4611** kind of [sqlite3_value] object can be used with this interface.
4612**
4613** If these routines are called from within the different thread
4614** than the one containing the application-defined function that received
4615** the [sqlite3_context] pointer, the results are undefined.
4616*/
4617SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4618SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
4619SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
4620SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
4621SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
4622SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
4623SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
4624SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
4625SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4626SQLITE_API void sqlite3_result_null(sqlite3_context*);
4627SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4628SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4629SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4630SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4631SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4632SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
4633
4634/*
4635** CAPI3REF: Define New Collating Sequences
4636**
4637** ^These functions add, remove, or modify a [collation] associated
4638** with the [database connection] specified as the first argument.
4639**
4640** ^The name of the collation is a UTF-8 string
4641** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4642** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4643** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4644** considered to be the same name.
4645**
4646** ^(The third argument (eTextRep) must be one of the constants:
4647** <ul>
4648** <li> [SQLITE_UTF8],
4649** <li> [SQLITE_UTF16LE],
4650** <li> [SQLITE_UTF16BE],
4651** <li> [SQLITE_UTF16], or
4652** <li> [SQLITE_UTF16_ALIGNED].
4653** </ul>)^
4654** ^The eTextRep argument determines the encoding of strings passed
4655** to the collating function callback, xCallback.
4656** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4657** force strings to be UTF16 with native byte order.
4658** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4659** on an even byte address.
4660**
4661** ^The fourth argument, pArg, is an application data pointer that is passed
4662** through as the first argument to the collating function callback.
4663**
4664** ^The fifth argument, xCallback, is a pointer to the collating function.
4665** ^Multiple collating functions can be registered using the same name but
4666** with different eTextRep parameters and SQLite will use whichever
4667** function requires the least amount of data transformation.
4668** ^If the xCallback argument is NULL then the collating function is
4669** deleted.  ^When all collating functions having the same name are deleted,
4670** that collation is no longer usable.
4671**
4672** ^The collating function callback is invoked with a copy of the pArg
4673** application data pointer and with two strings in the encoding specified
4674** by the eTextRep argument.  The collating function must return an
4675** integer that is negative, zero, or positive
4676** if the first string is less than, equal to, or greater than the second,
4677** respectively.  A collating function must always return the same answer
4678** given the same inputs.  If two or more collating functions are registered
4679** to the same collation name (using different eTextRep values) then all
4680** must give an equivalent answer when invoked with equivalent strings.
4681** The collating function must obey the following properties for all
4682** strings A, B, and C:
4683**
4684** <ol>
4685** <li> If A==B then B==A.
4686** <li> If A==B and B==C then A==C.
4687** <li> If A&lt;B THEN B&gt;A.
4688** <li> If A&lt;B and B&lt;C then A&lt;C.
4689** </ol>
4690**
4691** If a collating function fails any of the above constraints and that
4692** collating function is  registered and used, then the behavior of SQLite
4693** is undefined.
4694**
4695** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4696** with the addition that the xDestroy callback is invoked on pArg when
4697** the collating function is deleted.
4698** ^Collating functions are deleted when they are overridden by later
4699** calls to the collation creation functions or when the
4700** [database connection] is closed using [sqlite3_close()].
4701**
4702** ^The xDestroy callback is <u>not</u> called if the
4703** sqlite3_create_collation_v2() function fails.  Applications that invoke
4704** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
4705** check the return code and dispose of the application data pointer
4706** themselves rather than expecting SQLite to deal with it for them.
4707** This is different from every other SQLite interface.  The inconsistency
4708** is unfortunate but cannot be changed without breaking backwards
4709** compatibility.
4710**
4711** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4712*/
4713SQLITE_API int sqlite3_create_collation(
4714  sqlite3*,
4715  const char *zName,
4716  int eTextRep,
4717  void *pArg,
4718  int(*xCompare)(void*,int,const void*,int,const void*)
4719);
4720SQLITE_API int sqlite3_create_collation_v2(
4721  sqlite3*,
4722  const char *zName,
4723  int eTextRep,
4724  void *pArg,
4725  int(*xCompare)(void*,int,const void*,int,const void*),
4726  void(*xDestroy)(void*)
4727);
4728SQLITE_API int sqlite3_create_collation16(
4729  sqlite3*,
4730  const void *zName,
4731  int eTextRep,
4732  void *pArg,
4733  int(*xCompare)(void*,int,const void*,int,const void*)
4734);
4735
4736/*
4737** CAPI3REF: Collation Needed Callbacks
4738**
4739** ^To avoid having to register all collation sequences before a database
4740** can be used, a single callback function may be registered with the
4741** [database connection] to be invoked whenever an undefined collation
4742** sequence is required.
4743**
4744** ^If the function is registered using the sqlite3_collation_needed() API,
4745** then it is passed the names of undefined collation sequences as strings
4746** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4747** the names are passed as UTF-16 in machine native byte order.
4748** ^A call to either function replaces the existing collation-needed callback.
4749**
4750** ^(When the callback is invoked, the first argument passed is a copy
4751** of the second argument to sqlite3_collation_needed() or
4752** sqlite3_collation_needed16().  The second argument is the database
4753** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4754** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4755** sequence function required.  The fourth parameter is the name of the
4756** required collation sequence.)^
4757**
4758** The callback function should register the desired collation using
4759** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4760** [sqlite3_create_collation_v2()].
4761*/
4762SQLITE_API int sqlite3_collation_needed(
4763  sqlite3*,
4764  void*,
4765  void(*)(void*,sqlite3*,int eTextRep,const char*)
4766);
4767SQLITE_API int sqlite3_collation_needed16(
4768  sqlite3*,
4769  void*,
4770  void(*)(void*,sqlite3*,int eTextRep,const void*)
4771);
4772
4773#ifdef SQLITE_HAS_CODEC
4774/*
4775** Specify the key for an encrypted database.  This routine should be
4776** called right after sqlite3_open().
4777**
4778** The code to implement this API is not available in the public release
4779** of SQLite.
4780*/
4781SQLITE_API int sqlite3_key(
4782  sqlite3 *db,                   /* Database to be rekeyed */
4783  const void *pKey, int nKey     /* The key */
4784);
4785
4786/*
4787** Change the key on an open database.  If the current database is not
4788** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
4789** database is decrypted.
4790**
4791** The code to implement this API is not available in the public release
4792** of SQLite.
4793*/
4794SQLITE_API int sqlite3_rekey(
4795  sqlite3 *db,                   /* Database to be rekeyed */
4796  const void *pKey, int nKey     /* The new key */
4797);
4798
4799/*
4800** Specify the activation key for a SEE database.  Unless
4801** activated, none of the SEE routines will work.
4802*/
4803SQLITE_API void sqlite3_activate_see(
4804  const char *zPassPhrase        /* Activation phrase */
4805);
4806#endif
4807
4808#ifdef SQLITE_ENABLE_CEROD
4809/*
4810** Specify the activation key for a CEROD database.  Unless
4811** activated, none of the CEROD routines will work.
4812*/
4813SQLITE_API void sqlite3_activate_cerod(
4814  const char *zPassPhrase        /* Activation phrase */
4815);
4816#endif
4817
4818/*
4819** CAPI3REF: Suspend Execution For A Short Time
4820**
4821** The sqlite3_sleep() function causes the current thread to suspend execution
4822** for at least a number of milliseconds specified in its parameter.
4823**
4824** If the operating system does not support sleep requests with
4825** millisecond time resolution, then the time will be rounded up to
4826** the nearest second. The number of milliseconds of sleep actually
4827** requested from the operating system is returned.
4828**
4829** ^SQLite implements this interface by calling the xSleep()
4830** method of the default [sqlite3_vfs] object.  If the xSleep() method
4831** of the default VFS is not implemented correctly, or not implemented at
4832** all, then the behavior of sqlite3_sleep() may deviate from the description
4833** in the previous paragraphs.
4834*/
4835SQLITE_API int sqlite3_sleep(int);
4836
4837/*
4838** CAPI3REF: Name Of The Folder Holding Temporary Files
4839**
4840** ^(If this global variable is made to point to a string which is
4841** the name of a folder (a.k.a. directory), then all temporary files
4842** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4843** will be placed in that directory.)^  ^If this variable
4844** is a NULL pointer, then SQLite performs a search for an appropriate
4845** temporary file directory.
4846**
4847** It is not safe to read or modify this variable in more than one
4848** thread at a time.  It is not safe to read or modify this variable
4849** if a [database connection] is being used at the same time in a separate
4850** thread.
4851** It is intended that this variable be set once
4852** as part of process initialization and before any SQLite interface
4853** routines have been called and that this variable remain unchanged
4854** thereafter.
4855**
4856** ^The [temp_store_directory pragma] may modify this variable and cause
4857** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4858** the [temp_store_directory pragma] always assumes that any string
4859** that this variable points to is held in memory obtained from
4860** [sqlite3_malloc] and the pragma may attempt to free that memory
4861** using [sqlite3_free].
4862** Hence, if this variable is modified directly, either it should be
4863** made NULL or made to point to memory obtained from [sqlite3_malloc]
4864** or else the use of the [temp_store_directory pragma] should be avoided.
4865*/
4866SQLITE_API char *sqlite3_temp_directory;
4867
4868/*
4869** CAPI3REF: Test For Auto-Commit Mode
4870** KEYWORDS: {autocommit mode}
4871**
4872** ^The sqlite3_get_autocommit() interface returns non-zero or
4873** zero if the given database connection is or is not in autocommit mode,
4874** respectively.  ^Autocommit mode is on by default.
4875** ^Autocommit mode is disabled by a [BEGIN] statement.
4876** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
4877**
4878** If certain kinds of errors occur on a statement within a multi-statement
4879** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
4880** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
4881** transaction might be rolled back automatically.  The only way to
4882** find out whether SQLite automatically rolled back the transaction after
4883** an error is to use this function.
4884**
4885** If another thread changes the autocommit status of the database
4886** connection while this routine is running, then the return value
4887** is undefined.
4888*/
4889SQLITE_API int sqlite3_get_autocommit(sqlite3*);
4890
4891/*
4892** CAPI3REF: Find The Database Handle Of A Prepared Statement
4893**
4894** ^The sqlite3_db_handle interface returns the [database connection] handle
4895** to which a [prepared statement] belongs.  ^The [database connection]
4896** returned by sqlite3_db_handle is the same [database connection]
4897** that was the first argument
4898** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4899** create the statement in the first place.
4900*/
4901SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4902
4903/*
4904** CAPI3REF: Find the next prepared statement
4905**
4906** ^This interface returns a pointer to the next [prepared statement] after
4907** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
4908** then this interface returns a pointer to the first prepared statement
4909** associated with the database connection pDb.  ^If no prepared statement
4910** satisfies the conditions of this routine, it returns NULL.
4911**
4912** The [database connection] pointer D in a call to
4913** [sqlite3_next_stmt(D,S)] must refer to an open database
4914** connection and in particular must not be a NULL pointer.
4915*/
4916SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
4917
4918/*
4919** CAPI3REF: Commit And Rollback Notification Callbacks
4920**
4921** ^The sqlite3_commit_hook() interface registers a callback
4922** function to be invoked whenever a transaction is [COMMIT | committed].
4923** ^Any callback set by a previous call to sqlite3_commit_hook()
4924** for the same database connection is overridden.
4925** ^The sqlite3_rollback_hook() interface registers a callback
4926** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
4927** ^Any callback set by a previous call to sqlite3_rollback_hook()
4928** for the same database connection is overridden.
4929** ^The pArg argument is passed through to the callback.
4930** ^If the callback on a commit hook function returns non-zero,
4931** then the commit is converted into a rollback.
4932**
4933** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4934** return the P argument from the previous call of the same function
4935** on the same [database connection] D, or NULL for
4936** the first call for each function on D.
4937**
4938** The callback implementation must not do anything that will modify
4939** the database connection that invoked the callback.  Any actions
4940** to modify the database connection must be deferred until after the
4941** completion of the [sqlite3_step()] call that triggered the commit
4942** or rollback hook in the first place.
4943** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4944** database connections for the meaning of "modify" in this paragraph.
4945**
4946** ^Registering a NULL function disables the callback.
4947**
4948** ^When the commit hook callback routine returns zero, the [COMMIT]
4949** operation is allowed to continue normally.  ^If the commit hook
4950** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
4951** ^The rollback hook is invoked on a rollback that results from a commit
4952** hook returning non-zero, just as it would be with any other rollback.
4953**
4954** ^For the purposes of this API, a transaction is said to have been
4955** rolled back if an explicit "ROLLBACK" statement is executed, or
4956** an error or constraint causes an implicit rollback to occur.
4957** ^The rollback callback is not invoked if a transaction is
4958** automatically rolled back because the database connection is closed.
4959**
4960** See also the [sqlite3_update_hook()] interface.
4961*/
4962SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4963SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
4964
4965/*
4966** CAPI3REF: Data Change Notification Callbacks
4967**
4968** ^The sqlite3_update_hook() interface registers a callback function
4969** with the [database connection] identified by the first argument
4970** to be invoked whenever a row is updated, inserted or deleted.
4971** ^Any callback set by a previous call to this function
4972** for the same database connection is overridden.
4973**
4974** ^The second argument is a pointer to the function to invoke when a
4975** row is updated, inserted or deleted.
4976** ^The first argument to the callback is a copy of the third argument
4977** to sqlite3_update_hook().
4978** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
4979** or [SQLITE_UPDATE], depending on the operation that caused the callback
4980** to be invoked.
4981** ^The third and fourth arguments to the callback contain pointers to the
4982** database and table name containing the affected row.
4983** ^The final callback parameter is the [rowid] of the row.
4984** ^In the case of an update, this is the [rowid] after the update takes place.
4985**
4986** ^(The update hook is not invoked when internal system tables are
4987** modified (i.e. sqlite_master and sqlite_sequence).)^
4988**
4989** ^In the current implementation, the update hook
4990** is not invoked when duplication rows are deleted because of an
4991** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
4992** invoked when rows are deleted using the [truncate optimization].
4993** The exceptions defined in this paragraph might change in a future
4994** release of SQLite.
4995**
4996** The update hook implementation must not do anything that will modify
4997** the database connection that invoked the update hook.  Any actions
4998** to modify the database connection must be deferred until after the
4999** completion of the [sqlite3_step()] call that triggered the update hook.
5000** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
5001** database connections for the meaning of "modify" in this paragraph.
5002**
5003** ^The sqlite3_update_hook(D,C,P) function
5004** returns the P argument from the previous call
5005** on the same [database connection] D, or NULL for
5006** the first call on D.
5007**
5008** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
5009** interfaces.
5010*/
5011SQLITE_API void *sqlite3_update_hook(
5012  sqlite3*,
5013  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
5014  void*
5015);
5016
5017/*
5018** CAPI3REF: Enable Or Disable Shared Pager Cache
5019** KEYWORDS: {shared cache}
5020**
5021** ^(This routine enables or disables the sharing of the database cache
5022** and schema data structures between [database connection | connections]
5023** to the same database. Sharing is enabled if the argument is true
5024** and disabled if the argument is false.)^
5025**
5026** ^Cache sharing is enabled and disabled for an entire process.
5027** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
5028** sharing was enabled or disabled for each thread separately.
5029**
5030** ^(The cache sharing mode set by this interface effects all subsequent
5031** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
5032** Existing database connections continue use the sharing mode
5033** that was in effect at the time they were opened.)^
5034**
5035** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
5036** successfully.  An [error code] is returned otherwise.)^
5037**
5038** ^Shared cache is disabled by default. But this might change in
5039** future releases of SQLite.  Applications that care about shared
5040** cache setting should set it explicitly.
5041**
5042** See Also:  [SQLite Shared-Cache Mode]
5043*/
5044SQLITE_API int sqlite3_enable_shared_cache(int);
5045
5046/*
5047** CAPI3REF: Attempt To Free Heap Memory
5048**
5049** ^The sqlite3_release_memory() interface attempts to free N bytes
5050** of heap memory by deallocating non-essential memory allocations
5051** held by the database library.   Memory used to cache database
5052** pages to improve performance is an example of non-essential memory.
5053** ^sqlite3_release_memory() returns the number of bytes actually freed,
5054** which might be more or less than the amount requested.
5055** ^The sqlite3_release_memory() routine is a no-op returning zero
5056** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5057*/
5058SQLITE_API int sqlite3_release_memory(int);
5059
5060/*
5061** CAPI3REF: Impose A Limit On Heap Size
5062**
5063** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5064** soft limit on the amount of heap memory that may be allocated by SQLite.
5065** ^SQLite strives to keep heap memory utilization below the soft heap
5066** limit by reducing the number of pages held in the page cache
5067** as heap memory usages approaches the limit.
5068** ^The soft heap limit is "soft" because even though SQLite strives to stay
5069** below the limit, it will exceed the limit rather than generate
5070** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
5071** is advisory only.
5072**
5073** ^The return value from sqlite3_soft_heap_limit64() is the size of
5074** the soft heap limit prior to the call.  ^If the argument N is negative
5075** then no change is made to the soft heap limit.  Hence, the current
5076** size of the soft heap limit can be determined by invoking
5077** sqlite3_soft_heap_limit64() with a negative argument.
5078**
5079** ^If the argument N is zero then the soft heap limit is disabled.
5080**
5081** ^(The soft heap limit is not enforced in the current implementation
5082** if one or more of following conditions are true:
5083**
5084** <ul>
5085** <li> The soft heap limit is set to zero.
5086** <li> Memory accounting is disabled using a combination of the
5087**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5088**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5089** <li> An alternative page cache implementation is specified using
5090**      [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
5091** <li> The page cache allocates from its own memory pool supplied
5092**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5093**      from the heap.
5094** </ul>)^
5095**
5096** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
5097** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
5098** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
5099** the soft heap limit is enforced on every memory allocation.  Without
5100** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
5101** when memory is allocated by the page cache.  Testing suggests that because
5102** the page cache is the predominate memory user in SQLite, most
5103** applications will achieve adequate soft heap limit enforcement without
5104** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5105**
5106** The circumstances under which SQLite will enforce the soft heap limit may
5107** changes in future releases of SQLite.
5108*/
5109SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
5110
5111/*
5112** CAPI3REF: Deprecated Soft Heap Limit Interface
5113** DEPRECATED
5114**
5115** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
5116** interface.  This routine is provided for historical compatibility
5117** only.  All new applications should use the
5118** [sqlite3_soft_heap_limit64()] interface rather than this one.
5119*/
5120SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
5121
5122
5123/*
5124** CAPI3REF: Extract Metadata About A Column Of A Table
5125**
5126** ^This routine returns metadata about a specific column of a specific
5127** database table accessible using the [database connection] handle
5128** passed as the first function argument.
5129**
5130** ^The column is identified by the second, third and fourth parameters to
5131** this function. ^The second parameter is either the name of the database
5132** (i.e. "main", "temp", or an attached database) containing the specified
5133** table or NULL. ^If it is NULL, then all attached databases are searched
5134** for the table using the same algorithm used by the database engine to
5135** resolve unqualified table references.
5136**
5137** ^The third and fourth parameters to this function are the table and column
5138** name of the desired column, respectively. Neither of these parameters
5139** may be NULL.
5140**
5141** ^Metadata is returned by writing to the memory locations passed as the 5th
5142** and subsequent parameters to this function. ^Any of these arguments may be
5143** NULL, in which case the corresponding element of metadata is omitted.
5144**
5145** ^(<blockquote>
5146** <table border="1">
5147** <tr><th> Parameter <th> Output<br>Type <th>  Description
5148**
5149** <tr><td> 5th <td> const char* <td> Data type
5150** <tr><td> 6th <td> const char* <td> Name of default collation sequence
5151** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
5152** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
5153** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
5154** </table>
5155** </blockquote>)^
5156**
5157** ^The memory pointed to by the character pointers returned for the
5158** declaration type and collation sequence is valid only until the next
5159** call to any SQLite API function.
5160**
5161** ^If the specified table is actually a view, an [error code] is returned.
5162**
5163** ^If the specified column is "rowid", "oid" or "_rowid_" and an
5164** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
5165** parameters are set for the explicitly declared column. ^(If there is no
5166** explicitly declared [INTEGER PRIMARY KEY] column, then the output
5167** parameters are set as follows:
5168**
5169** <pre>
5170**     data type: "INTEGER"
5171**     collation sequence: "BINARY"
5172**     not null: 0
5173**     primary key: 1
5174**     auto increment: 0
5175** </pre>)^
5176**
5177** ^(This function may load one or more schemas from database files. If an
5178** error occurs during this process, or if the requested table or column
5179** cannot be found, an [error code] is returned and an error message left
5180** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
5181**
5182** ^This API is only available if the library was compiled with the
5183** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
5184*/
5185SQLITE_API int sqlite3_table_column_metadata(
5186  sqlite3 *db,                /* Connection handle */
5187  const char *zDbName,        /* Database name or NULL */
5188  const char *zTableName,     /* Table name */
5189  const char *zColumnName,    /* Column name */
5190  char const **pzDataType,    /* OUTPUT: Declared data type */
5191  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
5192  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
5193  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5194  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5195);
5196
5197/*
5198** CAPI3REF: Load An Extension
5199**
5200** ^This interface loads an SQLite extension library from the named file.
5201**
5202** ^The sqlite3_load_extension() interface attempts to load an
5203** SQLite extension library contained in the file zFile.
5204**
5205** ^The entry point is zProc.
5206** ^zProc may be 0, in which case the name of the entry point
5207** defaults to "sqlite3_extension_init".
5208** ^The sqlite3_load_extension() interface returns
5209** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5210** ^If an error occurs and pzErrMsg is not 0, then the
5211** [sqlite3_load_extension()] interface shall attempt to
5212** fill *pzErrMsg with error message text stored in memory
5213** obtained from [sqlite3_malloc()]. The calling function
5214** should free this memory by calling [sqlite3_free()].
5215**
5216** ^Extension loading must be enabled using
5217** [sqlite3_enable_load_extension()] prior to calling this API,
5218** otherwise an error will be returned.
5219**
5220** See also the [load_extension() SQL function].
5221*/
5222SQLITE_API int sqlite3_load_extension(
5223  sqlite3 *db,          /* Load the extension into this database connection */
5224  const char *zFile,    /* Name of the shared library containing extension */
5225  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5226  char **pzErrMsg       /* Put error message here if not 0 */
5227);
5228
5229/*
5230** CAPI3REF: Enable Or Disable Extension Loading
5231**
5232** ^So as not to open security holes in older applications that are
5233** unprepared to deal with extension loading, and as a means of disabling
5234** extension loading while evaluating user-entered SQL, the following API
5235** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5236**
5237** ^Extension loading is off by default. See ticket #1863.
5238** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5239** to turn extension loading on and call it with onoff==0 to turn
5240** it back off again.
5241*/
5242SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5243
5244/*
5245** CAPI3REF: Automatically Load Statically Linked Extensions
5246**
5247** ^This interface causes the xEntryPoint() function to be invoked for
5248** each new [database connection] that is created.  The idea here is that
5249** xEntryPoint() is the entry point for a statically linked SQLite extension
5250** that is to be automatically loaded into all new database connections.
5251**
5252** ^(Even though the function prototype shows that xEntryPoint() takes
5253** no arguments and returns void, SQLite invokes xEntryPoint() with three
5254** arguments and expects and integer result as if the signature of the
5255** entry point where as follows:
5256**
5257** <blockquote><pre>
5258** &nbsp;  int xEntryPoint(
5259** &nbsp;    sqlite3 *db,
5260** &nbsp;    const char **pzErrMsg,
5261** &nbsp;    const struct sqlite3_api_routines *pThunk
5262** &nbsp;  );
5263** </pre></blockquote>)^
5264**
5265** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5266** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5267** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5268** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5269** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5270** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5271** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5272**
5273** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5274** on the list of automatic extensions is a harmless no-op. ^No entry point
5275** will be called more than once for each database connection that is opened.
5276**
5277** See also: [sqlite3_reset_auto_extension()].
5278*/
5279SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
5280
5281/*
5282** CAPI3REF: Reset Automatic Extension Loading
5283**
5284** ^This interface disables all automatic extensions previously
5285** registered using [sqlite3_auto_extension()].
5286*/
5287SQLITE_API void sqlite3_reset_auto_extension(void);
5288
5289/*
5290** The interface to the virtual-table mechanism is currently considered
5291** to be experimental.  The interface might change in incompatible ways.
5292** If this is a problem for you, do not use the interface at this time.
5293**
5294** When the virtual-table mechanism stabilizes, we will declare the
5295** interface fixed, support it indefinitely, and remove this comment.
5296*/
5297
5298/*
5299** Structures used by the virtual table interface
5300*/
5301typedef struct sqlite3_vtab sqlite3_vtab;
5302typedef struct sqlite3_index_info sqlite3_index_info;
5303typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5304typedef struct sqlite3_module sqlite3_module;
5305
5306/*
5307** CAPI3REF: Virtual Table Object
5308** KEYWORDS: sqlite3_module {virtual table module}
5309**
5310** This structure, sometimes called a "virtual table module",
5311** defines the implementation of a [virtual tables].
5312** This structure consists mostly of methods for the module.
5313**
5314** ^A virtual table module is created by filling in a persistent
5315** instance of this structure and passing a pointer to that instance
5316** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5317** ^The registration remains valid until it is replaced by a different
5318** module or until the [database connection] closes.  The content
5319** of this structure must not change while it is registered with
5320** any database connection.
5321*/
5322struct sqlite3_module {
5323  int iVersion;
5324  int (*xCreate)(sqlite3*, void *pAux,
5325               int argc, const char *const*argv,
5326               sqlite3_vtab **ppVTab, char**);
5327  int (*xConnect)(sqlite3*, void *pAux,
5328               int argc, const char *const*argv,
5329               sqlite3_vtab **ppVTab, char**);
5330  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5331  int (*xDisconnect)(sqlite3_vtab *pVTab);
5332  int (*xDestroy)(sqlite3_vtab *pVTab);
5333  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5334  int (*xClose)(sqlite3_vtab_cursor*);
5335  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5336                int argc, sqlite3_value **argv);
5337  int (*xNext)(sqlite3_vtab_cursor*);
5338  int (*xEof)(sqlite3_vtab_cursor*);
5339  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5340  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5341  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5342  int (*xBegin)(sqlite3_vtab *pVTab);
5343  int (*xSync)(sqlite3_vtab *pVTab);
5344  int (*xCommit)(sqlite3_vtab *pVTab);
5345  int (*xRollback)(sqlite3_vtab *pVTab);
5346  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5347                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5348                       void **ppArg);
5349  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5350  /* The methods above are in version 1 of the sqlite_module object. Those
5351  ** below are for version 2 and greater. */
5352  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
5353  int (*xRelease)(sqlite3_vtab *pVTab, int);
5354  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
5355};
5356
5357/*
5358** CAPI3REF: Virtual Table Indexing Information
5359** KEYWORDS: sqlite3_index_info
5360**
5361** The sqlite3_index_info structure and its substructures is used as part
5362** of the [virtual table] interface to
5363** pass information into and receive the reply from the [xBestIndex]
5364** method of a [virtual table module].  The fields under **Inputs** are the
5365** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5366** results into the **Outputs** fields.
5367**
5368** ^(The aConstraint[] array records WHERE clause constraints of the form:
5369**
5370** <blockquote>column OP expr</blockquote>
5371**
5372** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5373** stored in aConstraint[].op using one of the
5374** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5375** ^(The index of the column is stored in
5376** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5377** expr on the right-hand side can be evaluated (and thus the constraint
5378** is usable) and false if it cannot.)^
5379**
5380** ^The optimizer automatically inverts terms of the form "expr OP column"
5381** and makes other simplifications to the WHERE clause in an attempt to
5382** get as many WHERE clause terms into the form shown above as possible.
5383** ^The aConstraint[] array only reports WHERE clause terms that are
5384** relevant to the particular virtual table being queried.
5385**
5386** ^Information about the ORDER BY clause is stored in aOrderBy[].
5387** ^Each term of aOrderBy records a column of the ORDER BY clause.
5388**
5389** The [xBestIndex] method must fill aConstraintUsage[] with information
5390** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5391** the right-hand side of the corresponding aConstraint[] is evaluated
5392** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5393** is true, then the constraint is assumed to be fully handled by the
5394** virtual table and is not checked again by SQLite.)^
5395**
5396** ^The idxNum and idxPtr values are recorded and passed into the
5397** [xFilter] method.
5398** ^[sqlite3_free()] is used to free idxPtr if and only if
5399** needToFreeIdxPtr is true.
5400**
5401** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5402** the correct order to satisfy the ORDER BY clause so that no separate
5403** sorting step is required.
5404**
5405** ^The estimatedCost value is an estimate of the cost of doing the
5406** particular lookup.  A full scan of a table with N entries should have
5407** a cost of N.  A binary search of a table of N entries should have a
5408** cost of approximately log(N).
5409*/
5410struct sqlite3_index_info {
5411  /* Inputs */
5412  int nConstraint;           /* Number of entries in aConstraint */
5413  struct sqlite3_index_constraint {
5414     int iColumn;              /* Column on left-hand side of constraint */
5415     unsigned char op;         /* Constraint operator */
5416     unsigned char usable;     /* True if this constraint is usable */
5417     int iTermOffset;          /* Used internally - xBestIndex should ignore */
5418  } *aConstraint;            /* Table of WHERE clause constraints */
5419  int nOrderBy;              /* Number of terms in the ORDER BY clause */
5420  struct sqlite3_index_orderby {
5421     int iColumn;              /* Column number */
5422     unsigned char desc;       /* True for DESC.  False for ASC. */
5423  } *aOrderBy;               /* The ORDER BY clause */
5424  /* Outputs */
5425  struct sqlite3_index_constraint_usage {
5426    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5427    unsigned char omit;      /* Do not code a test for this constraint */
5428  } *aConstraintUsage;
5429  int idxNum;                /* Number used to identify the index */
5430  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5431  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5432  int orderByConsumed;       /* True if output is already ordered */
5433  double estimatedCost;      /* Estimated cost of using this index */
5434};
5435
5436/*
5437** CAPI3REF: Virtual Table Constraint Operator Codes
5438**
5439** These macros defined the allowed values for the
5440** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5441** an operator that is part of a constraint term in the wHERE clause of
5442** a query that uses a [virtual table].
5443*/
5444#define SQLITE_INDEX_CONSTRAINT_EQ    2
5445#define SQLITE_INDEX_CONSTRAINT_GT    4
5446#define SQLITE_INDEX_CONSTRAINT_LE    8
5447#define SQLITE_INDEX_CONSTRAINT_LT    16
5448#define SQLITE_INDEX_CONSTRAINT_GE    32
5449#define SQLITE_INDEX_CONSTRAINT_MATCH 64
5450
5451/*
5452** CAPI3REF: Register A Virtual Table Implementation
5453**
5454** ^These routines are used to register a new [virtual table module] name.
5455** ^Module names must be registered before
5456** creating a new [virtual table] using the module and before using a
5457** preexisting [virtual table] for the module.
5458**
5459** ^The module name is registered on the [database connection] specified
5460** by the first parameter.  ^The name of the module is given by the
5461** second parameter.  ^The third parameter is a pointer to
5462** the implementation of the [virtual table module].   ^The fourth
5463** parameter is an arbitrary client data pointer that is passed through
5464** into the [xCreate] and [xConnect] methods of the virtual table module
5465** when a new virtual table is be being created or reinitialized.
5466**
5467** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5468** is a pointer to a destructor for the pClientData.  ^SQLite will
5469** invoke the destructor function (if it is not NULL) when SQLite
5470** no longer needs the pClientData pointer.  ^The destructor will also
5471** be invoked if the call to sqlite3_create_module_v2() fails.
5472** ^The sqlite3_create_module()
5473** interface is equivalent to sqlite3_create_module_v2() with a NULL
5474** destructor.
5475*/
5476SQLITE_API int sqlite3_create_module(
5477  sqlite3 *db,               /* SQLite connection to register module with */
5478  const char *zName,         /* Name of the module */
5479  const sqlite3_module *p,   /* Methods for the module */
5480  void *pClientData          /* Client data for xCreate/xConnect */
5481);
5482SQLITE_API int sqlite3_create_module_v2(
5483  sqlite3 *db,               /* SQLite connection to register module with */
5484  const char *zName,         /* Name of the module */
5485  const sqlite3_module *p,   /* Methods for the module */
5486  void *pClientData,         /* Client data for xCreate/xConnect */
5487  void(*xDestroy)(void*)     /* Module destructor function */
5488);
5489
5490/*
5491** CAPI3REF: Virtual Table Instance Object
5492** KEYWORDS: sqlite3_vtab
5493**
5494** Every [virtual table module] implementation uses a subclass
5495** of this object to describe a particular instance
5496** of the [virtual table].  Each subclass will
5497** be tailored to the specific needs of the module implementation.
5498** The purpose of this superclass is to define certain fields that are
5499** common to all module implementations.
5500**
5501** ^Virtual tables methods can set an error message by assigning a
5502** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5503** take care that any prior string is freed by a call to [sqlite3_free()]
5504** prior to assigning a new string to zErrMsg.  ^After the error message
5505** is delivered up to the client application, the string will be automatically
5506** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5507*/
5508struct sqlite3_vtab {
5509  const sqlite3_module *pModule;  /* The module for this virtual table */
5510  int nRef;                       /* NO LONGER USED */
5511  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5512  /* Virtual table implementations will typically add additional fields */
5513};
5514
5515/*
5516** CAPI3REF: Virtual Table Cursor Object
5517** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5518**
5519** Every [virtual table module] implementation uses a subclass of the
5520** following structure to describe cursors that point into the
5521** [virtual table] and are used
5522** to loop through the virtual table.  Cursors are created using the
5523** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5524** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5525** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5526** of the module.  Each module implementation will define
5527** the content of a cursor structure to suit its own needs.
5528**
5529** This superclass exists in order to define fields of the cursor that
5530** are common to all implementations.
5531*/
5532struct sqlite3_vtab_cursor {
5533  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5534  /* Virtual table implementations will typically add additional fields */
5535};
5536
5537/*
5538** CAPI3REF: Declare The Schema Of A Virtual Table
5539**
5540** ^The [xCreate] and [xConnect] methods of a
5541** [virtual table module] call this interface
5542** to declare the format (the names and datatypes of the columns) of
5543** the virtual tables they implement.
5544*/
5545SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5546
5547/*
5548** CAPI3REF: Overload A Function For A Virtual Table
5549**
5550** ^(Virtual tables can provide alternative implementations of functions
5551** using the [xFindFunction] method of the [virtual table module].
5552** But global versions of those functions
5553** must exist in order to be overloaded.)^
5554**
5555** ^(This API makes sure a global version of a function with a particular
5556** name and number of parameters exists.  If no such function exists
5557** before this API is called, a new function is created.)^  ^The implementation
5558** of the new function always causes an exception to be thrown.  So
5559** the new function is not good for anything by itself.  Its only
5560** purpose is to be a placeholder function that can be overloaded
5561** by a [virtual table].
5562*/
5563SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5564
5565/*
5566** The interface to the virtual-table mechanism defined above (back up
5567** to a comment remarkably similar to this one) is currently considered
5568** to be experimental.  The interface might change in incompatible ways.
5569** If this is a problem for you, do not use the interface at this time.
5570**
5571** When the virtual-table mechanism stabilizes, we will declare the
5572** interface fixed, support it indefinitely, and remove this comment.
5573*/
5574
5575/*
5576** CAPI3REF: A Handle To An Open BLOB
5577** KEYWORDS: {BLOB handle} {BLOB handles}
5578**
5579** An instance of this object represents an open BLOB on which
5580** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5581** ^Objects of this type are created by [sqlite3_blob_open()]
5582** and destroyed by [sqlite3_blob_close()].
5583** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
5584** can be used to read or write small subsections of the BLOB.
5585** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
5586*/
5587typedef struct sqlite3_blob sqlite3_blob;
5588
5589/*
5590** CAPI3REF: Open A BLOB For Incremental I/O
5591**
5592** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
5593** in row iRow, column zColumn, table zTable in database zDb;
5594** in other words, the same BLOB that would be selected by:
5595**
5596** <pre>
5597**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5598** </pre>)^
5599**
5600** ^If the flags parameter is non-zero, then the BLOB is opened for read
5601** and write access. ^If it is zero, the BLOB is opened for read access.
5602** ^It is not possible to open a column that is part of an index or primary
5603** key for writing. ^If [foreign key constraints] are enabled, it is
5604** not possible to open a column that is part of a [child key] for writing.
5605**
5606** ^Note that the database name is not the filename that contains
5607** the database but rather the symbolic name of the database that
5608** appears after the AS keyword when the database is connected using [ATTACH].
5609** ^For the main database file, the database name is "main".
5610** ^For TEMP tables, the database name is "temp".
5611**
5612** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5613** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5614** to be a null pointer.)^
5615** ^This function sets the [database connection] error code and message
5616** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5617** functions. ^Note that the *ppBlob variable is always initialized in a
5618** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5619** regardless of the success or failure of this routine.
5620**
5621** ^(If the row that a BLOB handle points to is modified by an
5622** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5623** then the BLOB handle is marked as "expired".
5624** This is true if any column of the row is changed, even a column
5625** other than the one the BLOB handle is open on.)^
5626** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
5627** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
5628** ^(Changes written into a BLOB prior to the BLOB expiring are not
5629** rolled back by the expiration of the BLOB.  Such changes will eventually
5630** commit if the transaction continues to completion.)^
5631**
5632** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5633** the opened blob.  ^The size of a blob may not be changed by this
5634** interface.  Use the [UPDATE] SQL command to change the size of a
5635** blob.
5636**
5637** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5638** and the built-in [zeroblob] SQL function can be used, if desired,
5639** to create an empty, zero-filled blob in which to read or write using
5640** this interface.
5641**
5642** To avoid a resource leak, every open [BLOB handle] should eventually
5643** be released by a call to [sqlite3_blob_close()].
5644*/
5645SQLITE_API int sqlite3_blob_open(
5646  sqlite3*,
5647  const char *zDb,
5648  const char *zTable,
5649  const char *zColumn,
5650  sqlite3_int64 iRow,
5651  int flags,
5652  sqlite3_blob **ppBlob
5653);
5654
5655/*
5656** CAPI3REF: Move a BLOB Handle to a New Row
5657**
5658** ^This function is used to move an existing blob handle so that it points
5659** to a different row of the same database table. ^The new row is identified
5660** by the rowid value passed as the second argument. Only the row can be
5661** changed. ^The database, table and column on which the blob handle is open
5662** remain the same. Moving an existing blob handle to a new row can be
5663** faster than closing the existing handle and opening a new one.
5664**
5665** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
5666** it must exist and there must be either a blob or text value stored in
5667** the nominated column.)^ ^If the new row is not present in the table, or if
5668** it does not contain a blob or text value, or if another error occurs, an
5669** SQLite error code is returned and the blob handle is considered aborted.
5670** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
5671** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
5672** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5673** always returns zero.
5674**
5675** ^This function sets the database handle error code and message.
5676*/
5677SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5678
5679/*
5680** CAPI3REF: Close A BLOB Handle
5681**
5682** ^Closes an open [BLOB handle].
5683**
5684** ^Closing a BLOB shall cause the current transaction to commit
5685** if there are no other BLOBs, no pending prepared statements, and the
5686** database connection is in [autocommit mode].
5687** ^If any writes were made to the BLOB, they might be held in cache
5688** until the close operation if they will fit.
5689**
5690** ^(Closing the BLOB often forces the changes
5691** out to disk and so if any I/O errors occur, they will likely occur
5692** at the time when the BLOB is closed.  Any errors that occur during
5693** closing are reported as a non-zero return value.)^
5694**
5695** ^(The BLOB is closed unconditionally.  Even if this routine returns
5696** an error code, the BLOB is still closed.)^
5697**
5698** ^Calling this routine with a null pointer (such as would be returned
5699** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5700*/
5701SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5702
5703/*
5704** CAPI3REF: Return The Size Of An Open BLOB
5705**
5706** ^Returns the size in bytes of the BLOB accessible via the
5707** successfully opened [BLOB handle] in its only argument.  ^The
5708** incremental blob I/O routines can only read or overwriting existing
5709** blob content; they cannot change the size of a blob.
5710**
5711** This routine only works on a [BLOB handle] which has been created
5712** by a prior successful call to [sqlite3_blob_open()] and which has not
5713** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5714** to this routine results in undefined and probably undesirable behavior.
5715*/
5716SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
5717
5718/*
5719** CAPI3REF: Read Data From A BLOB Incrementally
5720**
5721** ^(This function is used to read data from an open [BLOB handle] into a
5722** caller-supplied buffer. N bytes of data are copied into buffer Z
5723** from the open BLOB, starting at offset iOffset.)^
5724**
5725** ^If offset iOffset is less than N bytes from the end of the BLOB,
5726** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
5727** less than zero, [SQLITE_ERROR] is returned and no data is read.
5728** ^The size of the blob (and hence the maximum value of N+iOffset)
5729** can be determined using the [sqlite3_blob_bytes()] interface.
5730**
5731** ^An attempt to read from an expired [BLOB handle] fails with an
5732** error code of [SQLITE_ABORT].
5733**
5734** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
5735** Otherwise, an [error code] or an [extended error code] is returned.)^
5736**
5737** This routine only works on a [BLOB handle] which has been created
5738** by a prior successful call to [sqlite3_blob_open()] and which has not
5739** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5740** to this routine results in undefined and probably undesirable behavior.
5741**
5742** See also: [sqlite3_blob_write()].
5743*/
5744SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5745
5746/*
5747** CAPI3REF: Write Data Into A BLOB Incrementally
5748**
5749** ^This function is used to write data into an open [BLOB handle] from a
5750** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5751** into the open BLOB, starting at offset iOffset.
5752**
5753** ^If the [BLOB handle] passed as the first argument was not opened for
5754** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5755** this function returns [SQLITE_READONLY].
5756**
5757** ^This function may only modify the contents of the BLOB; it is
5758** not possible to increase the size of a BLOB using this API.
5759** ^If offset iOffset is less than N bytes from the end of the BLOB,
5760** [SQLITE_ERROR] is returned and no data is written.  ^If N is
5761** less than zero [SQLITE_ERROR] is returned and no data is written.
5762** The size of the BLOB (and hence the maximum value of N+iOffset)
5763** can be determined using the [sqlite3_blob_bytes()] interface.
5764**
5765** ^An attempt to write to an expired [BLOB handle] fails with an
5766** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
5767** before the [BLOB handle] expired are not rolled back by the
5768** expiration of the handle, though of course those changes might
5769** have been overwritten by the statement that expired the BLOB handle
5770** or by other independent statements.
5771**
5772** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5773** Otherwise, an  [error code] or an [extended error code] is returned.)^
5774**
5775** This routine only works on a [BLOB handle] which has been created
5776** by a prior successful call to [sqlite3_blob_open()] and which has not
5777** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5778** to this routine results in undefined and probably undesirable behavior.
5779**
5780** See also: [sqlite3_blob_read()].
5781*/
5782SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
5783
5784/*
5785** CAPI3REF: Virtual File System Objects
5786**
5787** A virtual filesystem (VFS) is an [sqlite3_vfs] object
5788** that SQLite uses to interact
5789** with the underlying operating system.  Most SQLite builds come with a
5790** single default VFS that is appropriate for the host computer.
5791** New VFSes can be registered and existing VFSes can be unregistered.
5792** The following interfaces are provided.
5793**
5794** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
5795** ^Names are case sensitive.
5796** ^Names are zero-terminated UTF-8 strings.
5797** ^If there is no match, a NULL pointer is returned.
5798** ^If zVfsName is NULL then the default VFS is returned.
5799**
5800** ^New VFSes are registered with sqlite3_vfs_register().
5801** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
5802** ^The same VFS can be registered multiple times without injury.
5803** ^To make an existing VFS into the default VFS, register it again
5804** with the makeDflt flag set.  If two different VFSes with the
5805** same name are registered, the behavior is undefined.  If a
5806** VFS is registered with a name that is NULL or an empty string,
5807** then the behavior is undefined.
5808**
5809** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
5810** ^(If the default VFS is unregistered, another VFS is chosen as
5811** the default.  The choice for the new VFS is arbitrary.)^
5812*/
5813SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
5814SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
5815SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
5816
5817/*
5818** CAPI3REF: Mutexes
5819**
5820** The SQLite core uses these routines for thread
5821** synchronization. Though they are intended for internal
5822** use by SQLite, code that links against SQLite is
5823** permitted to use any of these routines.
5824**
5825** The SQLite source code contains multiple implementations
5826** of these mutex routines.  An appropriate implementation
5827** is selected automatically at compile-time.  ^(The following
5828** implementations are available in the SQLite core:
5829**
5830** <ul>
5831** <li>   SQLITE_MUTEX_OS2
5832** <li>   SQLITE_MUTEX_PTHREAD
5833** <li>   SQLITE_MUTEX_W32
5834** <li>   SQLITE_MUTEX_NOOP
5835** </ul>)^
5836**
5837** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5838** that does no real locking and is appropriate for use in
5839** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
5840** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5841** are appropriate for use on OS/2, Unix, and Windows.
5842**
5843** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5844** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5845** implementation is included with the library. In this case the
5846** application must supply a custom mutex implementation using the
5847** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
5848** before calling sqlite3_initialize() or any other public sqlite3_
5849** function that calls sqlite3_initialize().)^
5850**
5851** ^The sqlite3_mutex_alloc() routine allocates a new
5852** mutex and returns a pointer to it. ^If it returns NULL
5853** that means that a mutex could not be allocated.  ^SQLite
5854** will unwind its stack and return an error.  ^(The argument
5855** to sqlite3_mutex_alloc() is one of these integer constants:
5856**
5857** <ul>
5858** <li>  SQLITE_MUTEX_FAST
5859** <li>  SQLITE_MUTEX_RECURSIVE
5860** <li>  SQLITE_MUTEX_STATIC_MASTER
5861** <li>  SQLITE_MUTEX_STATIC_MEM
5862** <li>  SQLITE_MUTEX_STATIC_MEM2
5863** <li>  SQLITE_MUTEX_STATIC_PRNG
5864** <li>  SQLITE_MUTEX_STATIC_LRU
5865** <li>  SQLITE_MUTEX_STATIC_LRU2
5866** </ul>)^
5867**
5868** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5869** cause sqlite3_mutex_alloc() to create
5870** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5871** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5872** The mutex implementation does not need to make a distinction
5873** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5874** not want to.  ^SQLite will only request a recursive mutex in
5875** cases where it really needs one.  ^If a faster non-recursive mutex
5876** implementation is available on the host platform, the mutex subsystem
5877** might return such a mutex in response to SQLITE_MUTEX_FAST.
5878**
5879** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
5880** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
5881** a pointer to a static preexisting mutex.  ^Six static mutexes are
5882** used by the current version of SQLite.  Future versions of SQLite
5883** may add additional static mutexes.  Static mutexes are for internal
5884** use by SQLite only.  Applications that use SQLite mutexes should
5885** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5886** SQLITE_MUTEX_RECURSIVE.
5887**
5888** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5889** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5890** returns a different mutex on every call.  ^But for the static
5891** mutex types, the same mutex is returned on every call that has
5892** the same type number.
5893**
5894** ^The sqlite3_mutex_free() routine deallocates a previously
5895** allocated dynamic mutex.  ^SQLite is careful to deallocate every
5896** dynamic mutex that it allocates.  The dynamic mutexes must not be in
5897** use when they are deallocated.  Attempting to deallocate a static
5898** mutex results in undefined behavior.  ^SQLite never deallocates
5899** a static mutex.
5900**
5901** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5902** to enter a mutex.  ^If another thread is already within the mutex,
5903** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5904** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
5905** upon successful entry.  ^(Mutexes created using
5906** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
5907** In such cases the,
5908** mutex must be exited an equal number of times before another thread
5909** can enter.)^  ^(If the same thread tries to enter any other
5910** kind of mutex more than once, the behavior is undefined.
5911** SQLite will never exhibit
5912** such behavior in its own use of mutexes.)^
5913**
5914** ^(Some systems (for example, Windows 95) do not support the operation
5915** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
5916** will always return SQLITE_BUSY.  The SQLite core only ever uses
5917** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
5918**
5919** ^The sqlite3_mutex_leave() routine exits a mutex that was
5920** previously entered by the same thread.   ^(The behavior
5921** is undefined if the mutex is not currently entered by the
5922** calling thread or is not currently allocated.  SQLite will
5923** never do either.)^
5924**
5925** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
5926** sqlite3_mutex_leave() is a NULL pointer, then all three routines
5927** behave as no-ops.
5928**
5929** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
5930*/
5931SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
5932SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
5933SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
5934SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
5935SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
5936
5937/*
5938** CAPI3REF: Mutex Methods Object
5939**
5940** An instance of this structure defines the low-level routines
5941** used to allocate and use mutexes.
5942**
5943** Usually, the default mutex implementations provided by SQLite are
5944** sufficient, however the user has the option of substituting a custom
5945** implementation for specialized deployments or systems for which SQLite
5946** does not provide a suitable implementation. In this case, the user
5947** creates and populates an instance of this structure to pass
5948** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
5949** Additionally, an instance of this structure can be used as an
5950** output variable when querying the system for the current mutex
5951** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
5952**
5953** ^The xMutexInit method defined by this structure is invoked as
5954** part of system initialization by the sqlite3_initialize() function.
5955** ^The xMutexInit routine is called by SQLite exactly once for each
5956** effective call to [sqlite3_initialize()].
5957**
5958** ^The xMutexEnd method defined by this structure is invoked as
5959** part of system shutdown by the sqlite3_shutdown() function. The
5960** implementation of this method is expected to release all outstanding
5961** resources obtained by the mutex methods implementation, especially
5962** those obtained by the xMutexInit method.  ^The xMutexEnd()
5963** interface is invoked exactly once for each call to [sqlite3_shutdown()].
5964**
5965** ^(The remaining seven methods defined by this structure (xMutexAlloc,
5966** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
5967** xMutexNotheld) implement the following interfaces (respectively):
5968**
5969** <ul>
5970**   <li>  [sqlite3_mutex_alloc()] </li>
5971**   <li>  [sqlite3_mutex_free()] </li>
5972**   <li>  [sqlite3_mutex_enter()] </li>
5973**   <li>  [sqlite3_mutex_try()] </li>
5974**   <li>  [sqlite3_mutex_leave()] </li>
5975**   <li>  [sqlite3_mutex_held()] </li>
5976**   <li>  [sqlite3_mutex_notheld()] </li>
5977** </ul>)^
5978**
5979** The only difference is that the public sqlite3_XXX functions enumerated
5980** above silently ignore any invocations that pass a NULL pointer instead
5981** of a valid mutex handle. The implementations of the methods defined
5982** by this structure are not required to handle this case, the results
5983** of passing a NULL pointer instead of a valid mutex handle are undefined
5984** (i.e. it is acceptable to provide an implementation that segfaults if
5985** it is passed a NULL pointer).
5986**
5987** The xMutexInit() method must be threadsafe.  ^It must be harmless to
5988** invoke xMutexInit() multiple times within the same process and without
5989** intervening calls to xMutexEnd().  Second and subsequent calls to
5990** xMutexInit() must be no-ops.
5991**
5992** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5993** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
5994** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
5995** memory allocation for a fast or recursive mutex.
5996**
5997** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
5998** called, but only if the prior call to xMutexInit returned SQLITE_OK.
5999** If xMutexInit fails in any way, it is expected to clean up after itself
6000** prior to returning.
6001*/
6002typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
6003struct sqlite3_mutex_methods {
6004  int (*xMutexInit)(void);
6005  int (*xMutexEnd)(void);
6006  sqlite3_mutex *(*xMutexAlloc)(int);
6007  void (*xMutexFree)(sqlite3_mutex *);
6008  void (*xMutexEnter)(sqlite3_mutex *);
6009  int (*xMutexTry)(sqlite3_mutex *);
6010  void (*xMutexLeave)(sqlite3_mutex *);
6011  int (*xMutexHeld)(sqlite3_mutex *);
6012  int (*xMutexNotheld)(sqlite3_mutex *);
6013};
6014
6015/*
6016** CAPI3REF: Mutex Verification Routines
6017**
6018** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
6019** are intended for use inside assert() statements.  ^The SQLite core
6020** never uses these routines except inside an assert() and applications
6021** are advised to follow the lead of the core.  ^The SQLite core only
6022** provides implementations for these routines when it is compiled
6023** with the SQLITE_DEBUG flag.  ^External mutex implementations
6024** are only required to provide these routines if SQLITE_DEBUG is
6025** defined and if NDEBUG is not defined.
6026**
6027** ^These routines should return true if the mutex in their argument
6028** is held or not held, respectively, by the calling thread.
6029**
6030** ^The implementation is not required to provided versions of these
6031** routines that actually work. If the implementation does not provide working
6032** versions of these routines, it should at least provide stubs that always
6033** return true so that one does not get spurious assertion failures.
6034**
6035** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
6036** the routine should return 1.   This seems counter-intuitive since
6037** clearly the mutex cannot be held if it does not exist.  But
6038** the reason the mutex does not exist is because the build is not
6039** using mutexes.  And we do not want the assert() containing the
6040** call to sqlite3_mutex_held() to fail, so a non-zero return is
6041** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
6042** interface should also return 1 when given a NULL pointer.
6043*/
6044#ifndef NDEBUG
6045SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
6046SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
6047#endif
6048
6049/*
6050** CAPI3REF: Mutex Types
6051**
6052** The [sqlite3_mutex_alloc()] interface takes a single argument
6053** which is one of these integer constants.
6054**
6055** The set of static mutexes may change from one SQLite release to the
6056** next.  Applications that override the built-in mutex logic must be
6057** prepared to accommodate additional static mutexes.
6058*/
6059#define SQLITE_MUTEX_FAST             0
6060#define SQLITE_MUTEX_RECURSIVE        1
6061#define SQLITE_MUTEX_STATIC_MASTER    2
6062#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
6063#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
6064#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
6065#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
6066#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
6067#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
6068#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
6069
6070/*
6071** CAPI3REF: Retrieve the mutex for a database connection
6072**
6073** ^This interface returns a pointer the [sqlite3_mutex] object that
6074** serializes access to the [database connection] given in the argument
6075** when the [threading mode] is Serialized.
6076** ^If the [threading mode] is Single-thread or Multi-thread then this
6077** routine returns a NULL pointer.
6078*/
6079SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
6080
6081/*
6082** CAPI3REF: Low-Level Control Of Database Files
6083**
6084** ^The [sqlite3_file_control()] interface makes a direct call to the
6085** xFileControl method for the [sqlite3_io_methods] object associated
6086** with a particular database identified by the second argument. ^The
6087** name of the database is "main" for the main database or "temp" for the
6088** TEMP database, or the name that appears after the AS keyword for
6089** databases that are added using the [ATTACH] SQL command.
6090** ^A NULL pointer can be used in place of "main" to refer to the
6091** main database file.
6092** ^The third and fourth parameters to this routine
6093** are passed directly through to the second and third parameters of
6094** the xFileControl method.  ^The return value of the xFileControl
6095** method becomes the return value of this routine.
6096**
6097** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
6098** a pointer to the underlying [sqlite3_file] object to be written into
6099** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
6100** case is a short-circuit path which does not actually invoke the
6101** underlying sqlite3_io_methods.xFileControl method.
6102**
6103** ^If the second parameter (zDbName) does not match the name of any
6104** open database file, then SQLITE_ERROR is returned.  ^This error
6105** code is not remembered and will not be recalled by [sqlite3_errcode()]
6106** or [sqlite3_errmsg()].  The underlying xFileControl method might
6107** also return SQLITE_ERROR.  There is no way to distinguish between
6108** an incorrect zDbName and an SQLITE_ERROR return from the underlying
6109** xFileControl method.
6110**
6111** See also: [SQLITE_FCNTL_LOCKSTATE]
6112*/
6113SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
6114
6115/*
6116** CAPI3REF: Testing Interface
6117**
6118** ^The sqlite3_test_control() interface is used to read out internal
6119** state of SQLite and to inject faults into SQLite for testing
6120** purposes.  ^The first parameter is an operation code that determines
6121** the number, meaning, and operation of all subsequent parameters.
6122**
6123** This interface is not for use by applications.  It exists solely
6124** for verifying the correct operation of the SQLite library.  Depending
6125** on how the SQLite library is compiled, this interface might not exist.
6126**
6127** The details of the operation codes, their meanings, the parameters
6128** they take, and what they do are all subject to change without notice.
6129** Unlike most of the SQLite API, this function is not guaranteed to
6130** operate consistently from one release to the next.
6131*/
6132SQLITE_API int sqlite3_test_control(int op, ...);
6133
6134/*
6135** CAPI3REF: Testing Interface Operation Codes
6136**
6137** These constants are the valid operation code parameters used
6138** as the first argument to [sqlite3_test_control()].
6139**
6140** These parameters and their meanings are subject to change
6141** without notice.  These values are for testing purposes only.
6142** Applications should not use any of these parameters or the
6143** [sqlite3_test_control()] interface.
6144*/
6145#define SQLITE_TESTCTRL_FIRST                    5
6146#define SQLITE_TESTCTRL_PRNG_SAVE                5
6147#define SQLITE_TESTCTRL_PRNG_RESTORE             6
6148#define SQLITE_TESTCTRL_PRNG_RESET               7
6149#define SQLITE_TESTCTRL_BITVEC_TEST              8
6150#define SQLITE_TESTCTRL_FAULT_INSTALL            9
6151#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
6152#define SQLITE_TESTCTRL_PENDING_BYTE            11
6153#define SQLITE_TESTCTRL_ASSERT                  12
6154#define SQLITE_TESTCTRL_ALWAYS                  13
6155#define SQLITE_TESTCTRL_RESERVE                 14
6156#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
6157#define SQLITE_TESTCTRL_ISKEYWORD               16
6158#define SQLITE_TESTCTRL_PGHDRSZ                 17
6159#define SQLITE_TESTCTRL_SCRATCHMALLOC           18
6160#define SQLITE_TESTCTRL_LOCALTIME_FAULT         19
6161#define SQLITE_TESTCTRL_LAST                    19
6162
6163/*
6164** CAPI3REF: SQLite Runtime Status
6165**
6166** ^This interface is used to retrieve runtime status information
6167** about the performance of SQLite, and optionally to reset various
6168** highwater marks.  ^The first argument is an integer code for
6169** the specific parameter to measure.  ^(Recognized integer codes
6170** are of the form [status parameters | SQLITE_STATUS_...].)^
6171** ^The current value of the parameter is returned into *pCurrent.
6172** ^The highest recorded value is returned in *pHighwater.  ^If the
6173** resetFlag is true, then the highest record value is reset after
6174** *pHighwater is written.  ^(Some parameters do not record the highest
6175** value.  For those parameters
6176** nothing is written into *pHighwater and the resetFlag is ignored.)^
6177** ^(Other parameters record only the highwater mark and not the current
6178** value.  For these latter parameters nothing is written into *pCurrent.)^
6179**
6180** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6181** non-zero [error code] on failure.
6182**
6183** This routine is threadsafe but is not atomic.  This routine can be
6184** called while other threads are running the same or different SQLite
6185** interfaces.  However the values returned in *pCurrent and
6186** *pHighwater reflect the status of SQLite at different points in time
6187** and it is possible that another thread might change the parameter
6188** in between the times when *pCurrent and *pHighwater are written.
6189**
6190** See also: [sqlite3_db_status()]
6191*/
6192SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6193
6194
6195/*
6196** CAPI3REF: Status Parameters
6197** KEYWORDS: {status parameters}
6198**
6199** These integer constants designate various run-time status parameters
6200** that can be returned by [sqlite3_status()].
6201**
6202** <dl>
6203** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6204** <dd>This parameter is the current amount of memory checked out
6205** using [sqlite3_malloc()], either directly or indirectly.  The
6206** figure includes calls made to [sqlite3_malloc()] by the application
6207** and internal memory usage by the SQLite library.  Scratch memory
6208** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6209** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6210** this parameter.  The amount returned is the sum of the allocation
6211** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6212**
6213** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6214** <dd>This parameter records the largest memory allocation request
6215** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6216** internal equivalents).  Only the value returned in the
6217** *pHighwater parameter to [sqlite3_status()] is of interest.
6218** The value written into the *pCurrent parameter is undefined.</dd>)^
6219**
6220** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6221** <dd>This parameter records the number of separate memory allocations
6222** currently checked out.</dd>)^
6223**
6224** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6225** <dd>This parameter returns the number of pages used out of the
6226** [pagecache memory allocator] that was configured using
6227** [SQLITE_CONFIG_PAGECACHE].  The
6228** value returned is in pages, not in bytes.</dd>)^
6229**
6230** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
6231** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6232** <dd>This parameter returns the number of bytes of page cache
6233** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6234** buffer and where forced to overflow to [sqlite3_malloc()].  The
6235** returned value includes allocations that overflowed because they
6236** where too large (they were larger than the "sz" parameter to
6237** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6238** no space was left in the page cache.</dd>)^
6239**
6240** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6241** <dd>This parameter records the largest memory allocation request
6242** handed to [pagecache memory allocator].  Only the value returned in the
6243** *pHighwater parameter to [sqlite3_status()] is of interest.
6244** The value written into the *pCurrent parameter is undefined.</dd>)^
6245**
6246** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6247** <dd>This parameter returns the number of allocations used out of the
6248** [scratch memory allocator] configured using
6249** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6250** in bytes.  Since a single thread may only have one scratch allocation
6251** outstanding at time, this parameter also reports the number of threads
6252** using scratch memory at the same time.</dd>)^
6253**
6254** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6255** <dd>This parameter returns the number of bytes of scratch memory
6256** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6257** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6258** returned include overflows because the requested allocation was too
6259** larger (that is, because the requested allocation was larger than the
6260** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6261** slots were available.
6262** </dd>)^
6263**
6264** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6265** <dd>This parameter records the largest memory allocation request
6266** handed to [scratch memory allocator].  Only the value returned in the
6267** *pHighwater parameter to [sqlite3_status()] is of interest.
6268** The value written into the *pCurrent parameter is undefined.</dd>)^
6269**
6270** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6271** <dd>This parameter records the deepest parser stack.  It is only
6272** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6273** </dl>
6274**
6275** New status parameters may be added from time to time.
6276*/
6277#define SQLITE_STATUS_MEMORY_USED          0
6278#define SQLITE_STATUS_PAGECACHE_USED       1
6279#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6280#define SQLITE_STATUS_SCRATCH_USED         3
6281#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6282#define SQLITE_STATUS_MALLOC_SIZE          5
6283#define SQLITE_STATUS_PARSER_STACK         6
6284#define SQLITE_STATUS_PAGECACHE_SIZE       7
6285#define SQLITE_STATUS_SCRATCH_SIZE         8
6286#define SQLITE_STATUS_MALLOC_COUNT         9
6287
6288/*
6289** CAPI3REF: Database Connection Status
6290**
6291** ^This interface is used to retrieve runtime status information
6292** about a single [database connection].  ^The first argument is the
6293** database connection object to be interrogated.  ^The second argument
6294** is an integer constant, taken from the set of
6295** [SQLITE_DBSTATUS options], that
6296** determines the parameter to interrogate.  The set of
6297** [SQLITE_DBSTATUS options] is likely
6298** to grow in future releases of SQLite.
6299**
6300** ^The current value of the requested parameter is written into *pCur
6301** and the highest instantaneous value is written into *pHiwtr.  ^If
6302** the resetFlg is true, then the highest instantaneous value is
6303** reset back down to the current value.
6304**
6305** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6306** non-zero [error code] on failure.
6307**
6308** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6309*/
6310SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6311
6312/*
6313** CAPI3REF: Status Parameters for database connections
6314** KEYWORDS: {SQLITE_DBSTATUS options}
6315**
6316** These constants are the available integer "verbs" that can be passed as
6317** the second argument to the [sqlite3_db_status()] interface.
6318**
6319** New verbs may be added in future releases of SQLite. Existing verbs
6320** might be discontinued. Applications should check the return code from
6321** [sqlite3_db_status()] to make sure that the call worked.
6322** The [sqlite3_db_status()] interface will return a non-zero error code
6323** if a discontinued or unsupported verb is invoked.
6324**
6325** <dl>
6326** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6327** <dd>This parameter returns the number of lookaside memory slots currently
6328** checked out.</dd>)^
6329**
6330** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6331** <dd>This parameter returns the number malloc attempts that were
6332** satisfied using lookaside memory. Only the high-water value is meaningful;
6333** the current value is always zero.)^
6334**
6335** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
6336** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6337** <dd>This parameter returns the number malloc attempts that might have
6338** been satisfied using lookaside memory but failed due to the amount of
6339** memory requested being larger than the lookaside slot size.
6340** Only the high-water value is meaningful;
6341** the current value is always zero.)^
6342**
6343** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
6344** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6345** <dd>This parameter returns the number malloc attempts that might have
6346** been satisfied using lookaside memory but failed due to all lookaside
6347** memory already being in use.
6348** Only the high-water value is meaningful;
6349** the current value is always zero.)^
6350**
6351** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6352** <dd>This parameter returns the approximate number of of bytes of heap
6353** memory used by all pager caches associated with the database connection.)^
6354** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6355**
6356** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6357** <dd>This parameter returns the approximate number of of bytes of heap
6358** memory used to store the schema for all databases associated
6359** with the connection - main, temp, and any [ATTACH]-ed databases.)^
6360** ^The full amount of memory used by the schemas is reported, even if the
6361** schema memory is shared with other database connections due to
6362** [shared cache mode] being enabled.
6363** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6364**
6365** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6366** <dd>This parameter returns the approximate number of of bytes of heap
6367** and lookaside memory used by all prepared statements associated with
6368** the database connection.)^
6369** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6370** </dd>
6371** </dl>
6372*/
6373#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6374#define SQLITE_DBSTATUS_CACHE_USED           1
6375#define SQLITE_DBSTATUS_SCHEMA_USED          2
6376#define SQLITE_DBSTATUS_STMT_USED            3
6377#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6378#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6379#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6380#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
6381
6382
6383/*
6384** CAPI3REF: Prepared Statement Status
6385**
6386** ^(Each prepared statement maintains various
6387** [SQLITE_STMTSTATUS counters] that measure the number
6388** of times it has performed specific operations.)^  These counters can
6389** be used to monitor the performance characteristics of the prepared
6390** statements.  For example, if the number of table steps greatly exceeds
6391** the number of table searches or result rows, that would tend to indicate
6392** that the prepared statement is using a full table scan rather than
6393** an index.
6394**
6395** ^(This interface is used to retrieve and reset counter values from
6396** a [prepared statement].  The first argument is the prepared statement
6397** object to be interrogated.  The second argument
6398** is an integer code for a specific [SQLITE_STMTSTATUS counter]
6399** to be interrogated.)^
6400** ^The current value of the requested counter is returned.
6401** ^If the resetFlg is true, then the counter is reset to zero after this
6402** interface call returns.
6403**
6404** See also: [sqlite3_status()] and [sqlite3_db_status()].
6405*/
6406SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6407
6408/*
6409** CAPI3REF: Status Parameters for prepared statements
6410** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
6411**
6412** These preprocessor macros define integer codes that name counter
6413** values associated with the [sqlite3_stmt_status()] interface.
6414** The meanings of the various counters are as follows:
6415**
6416** <dl>
6417** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6418** <dd>^This is the number of times that SQLite has stepped forward in
6419** a table as part of a full table scan.  Large numbers for this counter
6420** may indicate opportunities for performance improvement through
6421** careful use of indices.</dd>
6422**
6423** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
6424** <dd>^This is the number of sort operations that have occurred.
6425** A non-zero value in this counter may indicate an opportunity to
6426** improvement performance through careful use of indices.</dd>
6427**
6428** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6429** <dd>^This is the number of rows inserted into transient indices that
6430** were created automatically in order to help joins run faster.
6431** A non-zero value in this counter may indicate an opportunity to
6432** improvement performance by adding permanent indices that do not
6433** need to be reinitialized each time the statement is run.</dd>
6434**
6435** </dl>
6436*/
6437#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6438#define SQLITE_STMTSTATUS_SORT              2
6439#define SQLITE_STMTSTATUS_AUTOINDEX         3
6440
6441/*
6442** CAPI3REF: Custom Page Cache Object
6443**
6444** The sqlite3_pcache type is opaque.  It is implemented by
6445** the pluggable module.  The SQLite core has no knowledge of
6446** its size or internal structure and never deals with the
6447** sqlite3_pcache object except by holding and passing pointers
6448** to the object.
6449**
6450** See [sqlite3_pcache_methods] for additional information.
6451*/
6452typedef struct sqlite3_pcache sqlite3_pcache;
6453
6454/*
6455** CAPI3REF: Application Defined Page Cache.
6456** KEYWORDS: {page cache}
6457**
6458** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6459** register an alternative page cache implementation by passing in an
6460** instance of the sqlite3_pcache_methods structure.)^
6461** In many applications, most of the heap memory allocated by
6462** SQLite is used for the page cache.
6463** By implementing a
6464** custom page cache using this API, an application can better control
6465** the amount of memory consumed by SQLite, the way in which
6466** that memory is allocated and released, and the policies used to
6467** determine exactly which parts of a database file are cached and for
6468** how long.
6469**
6470** The alternative page cache mechanism is an
6471** extreme measure that is only needed by the most demanding applications.
6472** The built-in page cache is recommended for most uses.
6473**
6474** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6475** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6476** the application may discard the parameter after the call to
6477** [sqlite3_config()] returns.)^
6478**
6479** [[the xInit() page cache method]]
6480** ^(The xInit() method is called once for each effective
6481** call to [sqlite3_initialize()])^
6482** (usually only once during the lifetime of the process). ^(The xInit()
6483** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6484** The intent of the xInit() method is to set up global data structures
6485** required by the custom page cache implementation.
6486** ^(If the xInit() method is NULL, then the
6487** built-in default page cache is used instead of the application defined
6488** page cache.)^
6489**
6490** [[the xShutdown() page cache method]]
6491** ^The xShutdown() method is called by [sqlite3_shutdown()].
6492** It can be used to clean up
6493** any outstanding resources before process shutdown, if required.
6494** ^The xShutdown() method may be NULL.
6495**
6496** ^SQLite automatically serializes calls to the xInit method,
6497** so the xInit method need not be threadsafe.  ^The
6498** xShutdown method is only called from [sqlite3_shutdown()] so it does
6499** not need to be threadsafe either.  All other methods must be threadsafe
6500** in multithreaded applications.
6501**
6502** ^SQLite will never invoke xInit() more than once without an intervening
6503** call to xShutdown().
6504**
6505** [[the xCreate() page cache methods]]
6506** ^SQLite invokes the xCreate() method to construct a new cache instance.
6507** SQLite will typically create one cache instance for each open database file,
6508** though this is not guaranteed. ^The
6509** first parameter, szPage, is the size in bytes of the pages that must
6510** be allocated by the cache.  ^szPage will not be a power of two.  ^szPage
6511** will the page size of the database file that is to be cached plus an
6512** increment (here called "R") of less than 250.  SQLite will use the
6513** extra R bytes on each page to store metadata about the underlying
6514** database page on disk.  The value of R depends
6515** on the SQLite version, the target platform, and how SQLite was compiled.
6516** ^(R is constant for a particular build of SQLite. Except, there are two
6517** distinct values of R when SQLite is compiled with the proprietary
6518** ZIPVFS extension.)^  ^The second argument to
6519** xCreate(), bPurgeable, is true if the cache being created will
6520** be used to cache database pages of a file stored on disk, or
6521** false if it is used for an in-memory database. The cache implementation
6522** does not have to do anything special based with the value of bPurgeable;
6523** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
6524** never invoke xUnpin() except to deliberately delete a page.
6525** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
6526** false will always have the "discard" flag set to true.
6527** ^Hence, a cache created with bPurgeable false will
6528** never contain any unpinned pages.
6529**
6530** [[the xCachesize() page cache method]]
6531** ^(The xCachesize() method may be called at any time by SQLite to set the
6532** suggested maximum cache-size (number of pages stored by) the cache
6533** instance passed as the first argument. This is the value configured using
6534** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
6535** parameter, the implementation is not required to do anything with this
6536** value; it is advisory only.
6537**
6538** [[the xPagecount() page cache methods]]
6539** The xPagecount() method must return the number of pages currently
6540** stored in the cache, both pinned and unpinned.
6541**
6542** [[the xFetch() page cache methods]]
6543** The xFetch() method locates a page in the cache and returns a pointer to
6544** the page, or a NULL pointer.
6545** A "page", in this context, means a buffer of szPage bytes aligned at an
6546** 8-byte boundary. The page to be fetched is determined by the key. ^The
6547** minimum key value is 1.  After it has been retrieved using xFetch, the page
6548** is considered to be "pinned".
6549**
6550** If the requested page is already in the page cache, then the page cache
6551** implementation must return a pointer to the page buffer with its content
6552** intact.  If the requested page is not already in the cache, then the
6553** cache implementation should use the value of the createFlag
6554** parameter to help it determined what action to take:
6555**
6556** <table border=1 width=85% align=center>
6557** <tr><th> createFlag <th> Behaviour when page is not already in cache
6558** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
6559** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
6560**                 Otherwise return NULL.
6561** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
6562**                 NULL if allocating a new page is effectively impossible.
6563** </table>
6564**
6565** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
6566** will only use a createFlag of 2 after a prior call with a createFlag of 1
6567** failed.)^  In between the to xFetch() calls, SQLite may
6568** attempt to unpin one or more cache pages by spilling the content of
6569** pinned pages to disk and synching the operating system disk cache.
6570**
6571** [[the xUnpin() page cache method]]
6572** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
6573** as its second argument.  If the third parameter, discard, is non-zero,
6574** then the page must be evicted from the cache.
6575** ^If the discard parameter is
6576** zero, then the page may be discarded or retained at the discretion of
6577** page cache implementation. ^The page cache implementation
6578** may choose to evict unpinned pages at any time.
6579**
6580** The cache must not perform any reference counting. A single
6581** call to xUnpin() unpins the page regardless of the number of prior calls
6582** to xFetch().
6583**
6584** [[the xRekey() page cache methods]]
6585** The xRekey() method is used to change the key value associated with the
6586** page passed as the second argument. If the cache
6587** previously contains an entry associated with newKey, it must be
6588** discarded. ^Any prior cache entry associated with newKey is guaranteed not
6589** to be pinned.
6590**
6591** When SQLite calls the xTruncate() method, the cache must discard all
6592** existing cache entries with page numbers (keys) greater than or equal
6593** to the value of the iLimit parameter passed to xTruncate(). If any
6594** of these pages are pinned, they are implicitly unpinned, meaning that
6595** they can be safely discarded.
6596**
6597** [[the xDestroy() page cache method]]
6598** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6599** All resources associated with the specified cache should be freed. ^After
6600** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6601** handle invalid, and will not use it with any other sqlite3_pcache_methods
6602** functions.
6603*/
6604typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6605struct sqlite3_pcache_methods {
6606  void *pArg;
6607  int (*xInit)(void*);
6608  void (*xShutdown)(void*);
6609  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6610  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6611  int (*xPagecount)(sqlite3_pcache*);
6612  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6613  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6614  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6615  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6616  void (*xDestroy)(sqlite3_pcache*);
6617};
6618
6619/*
6620** CAPI3REF: Online Backup Object
6621**
6622** The sqlite3_backup object records state information about an ongoing
6623** online backup operation.  ^The sqlite3_backup object is created by
6624** a call to [sqlite3_backup_init()] and is destroyed by a call to
6625** [sqlite3_backup_finish()].
6626**
6627** See Also: [Using the SQLite Online Backup API]
6628*/
6629typedef struct sqlite3_backup sqlite3_backup;
6630
6631/*
6632** CAPI3REF: Online Backup API.
6633**
6634** The backup API copies the content of one database into another.
6635** It is useful either for creating backups of databases or
6636** for copying in-memory databases to or from persistent files.
6637**
6638** See Also: [Using the SQLite Online Backup API]
6639**
6640** ^SQLite holds a write transaction open on the destination database file
6641** for the duration of the backup operation.
6642** ^The source database is read-locked only while it is being read;
6643** it is not locked continuously for the entire backup operation.
6644** ^Thus, the backup may be performed on a live source database without
6645** preventing other database connections from
6646** reading or writing to the source database while the backup is underway.
6647**
6648** ^(To perform a backup operation:
6649**   <ol>
6650**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
6651**         backup,
6652**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
6653**         the data between the two databases, and finally
6654**     <li><b>sqlite3_backup_finish()</b> is called to release all resources
6655**         associated with the backup operation.
6656**   </ol>)^
6657** There should be exactly one call to sqlite3_backup_finish() for each
6658** successful call to sqlite3_backup_init().
6659**
6660** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
6661**
6662** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
6663** [database connection] associated with the destination database
6664** and the database name, respectively.
6665** ^The database name is "main" for the main database, "temp" for the
6666** temporary database, or the name specified after the AS keyword in
6667** an [ATTACH] statement for an attached database.
6668** ^The S and M arguments passed to
6669** sqlite3_backup_init(D,N,S,M) identify the [database connection]
6670** and database name of the source database, respectively.
6671** ^The source and destination [database connections] (parameters S and D)
6672** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
6673** an error.
6674**
6675** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
6676** returned and an error code and error message are stored in the
6677** destination [database connection] D.
6678** ^The error code and message for the failed call to sqlite3_backup_init()
6679** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
6680** [sqlite3_errmsg16()] functions.
6681** ^A successful call to sqlite3_backup_init() returns a pointer to an
6682** [sqlite3_backup] object.
6683** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
6684** sqlite3_backup_finish() functions to perform the specified backup
6685** operation.
6686**
6687** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
6688**
6689** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
6690** the source and destination databases specified by [sqlite3_backup] object B.
6691** ^If N is negative, all remaining source pages are copied.
6692** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
6693** are still more pages to be copied, then the function returns [SQLITE_OK].
6694** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
6695** from source to destination, then it returns [SQLITE_DONE].
6696** ^If an error occurs while running sqlite3_backup_step(B,N),
6697** then an [error code] is returned. ^As well as [SQLITE_OK] and
6698** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6699** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6700** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6701**
6702** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6703** <ol>
6704** <li> the destination database was opened read-only, or
6705** <li> the destination database is using write-ahead-log journaling
6706** and the destination and source page sizes differ, or
6707** <li> the destination database is an in-memory database and the
6708** destination and source page sizes differ.
6709** </ol>)^
6710**
6711** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6712** the [sqlite3_busy_handler | busy-handler function]
6713** is invoked (if one is specified). ^If the
6714** busy-handler returns non-zero before the lock is available, then
6715** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
6716** sqlite3_backup_step() can be retried later. ^If the source
6717** [database connection]
6718** is being used to write to the source database when sqlite3_backup_step()
6719** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
6720** case the call to sqlite3_backup_step() can be retried later on. ^(If
6721** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
6722** [SQLITE_READONLY] is returned, then
6723** there is no point in retrying the call to sqlite3_backup_step(). These
6724** errors are considered fatal.)^  The application must accept
6725** that the backup operation has failed and pass the backup operation handle
6726** to the sqlite3_backup_finish() to release associated resources.
6727**
6728** ^The first call to sqlite3_backup_step() obtains an exclusive lock
6729** on the destination file. ^The exclusive lock is not released until either
6730** sqlite3_backup_finish() is called or the backup operation is complete
6731** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
6732** sqlite3_backup_step() obtains a [shared lock] on the source database that
6733** lasts for the duration of the sqlite3_backup_step() call.
6734** ^Because the source database is not locked between calls to
6735** sqlite3_backup_step(), the source database may be modified mid-way
6736** through the backup process.  ^If the source database is modified by an
6737** external process or via a database connection other than the one being
6738** used by the backup operation, then the backup will be automatically
6739** restarted by the next call to sqlite3_backup_step(). ^If the source
6740** database is modified by the using the same database connection as is used
6741** by the backup operation, then the backup database is automatically
6742** updated at the same time.
6743**
6744** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
6745**
6746** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
6747** application wishes to abandon the backup operation, the application
6748** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
6749** ^The sqlite3_backup_finish() interfaces releases all
6750** resources associated with the [sqlite3_backup] object.
6751** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
6752** active write-transaction on the destination database is rolled back.
6753** The [sqlite3_backup] object is invalid
6754** and may not be used following a call to sqlite3_backup_finish().
6755**
6756** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
6757** sqlite3_backup_step() errors occurred, regardless or whether or not
6758** sqlite3_backup_step() completed.
6759** ^If an out-of-memory condition or IO error occurred during any prior
6760** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
6761** sqlite3_backup_finish() returns the corresponding [error code].
6762**
6763** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
6764** is not a permanent error and does not affect the return value of
6765** sqlite3_backup_finish().
6766**
6767** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
6768** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
6769**
6770** ^Each call to sqlite3_backup_step() sets two values inside
6771** the [sqlite3_backup] object: the number of pages still to be backed
6772** up and the total number of pages in the source database file.
6773** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6774** retrieve these two values, respectively.
6775**
6776** ^The values returned by these functions are only updated by
6777** sqlite3_backup_step(). ^If the source database is modified during a backup
6778** operation, then the values are not updated to account for any extra
6779** pages that need to be updated or the size of the source database file
6780** changing.
6781**
6782** <b>Concurrent Usage of Database Handles</b>
6783**
6784** ^The source [database connection] may be used by the application for other
6785** purposes while a backup operation is underway or being initialized.
6786** ^If SQLite is compiled and configured to support threadsafe database
6787** connections, then the source database connection may be used concurrently
6788** from within other threads.
6789**
6790** However, the application must guarantee that the destination
6791** [database connection] is not passed to any other API (by any thread) after
6792** sqlite3_backup_init() is called and before the corresponding call to
6793** sqlite3_backup_finish().  SQLite does not currently check to see
6794** if the application incorrectly accesses the destination [database connection]
6795** and so no error code is reported, but the operations may malfunction
6796** nevertheless.  Use of the destination database connection while a
6797** backup is in progress might also also cause a mutex deadlock.
6798**
6799** If running in [shared cache mode], the application must
6800** guarantee that the shared cache used by the destination database
6801** is not accessed while the backup is running. In practice this means
6802** that the application must guarantee that the disk file being
6803** backed up to is not accessed by any connection within the process,
6804** not just the specific connection that was passed to sqlite3_backup_init().
6805**
6806** The [sqlite3_backup] object itself is partially threadsafe. Multiple
6807** threads may safely make multiple concurrent calls to sqlite3_backup_step().
6808** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
6809** APIs are not strictly speaking threadsafe. If they are invoked at the
6810** same time as another thread is invoking sqlite3_backup_step() it is
6811** possible that they return invalid values.
6812*/
6813SQLITE_API sqlite3_backup *sqlite3_backup_init(
6814  sqlite3 *pDest,                        /* Destination database handle */
6815  const char *zDestName,                 /* Destination database name */
6816  sqlite3 *pSource,                      /* Source database handle */
6817  const char *zSourceName                /* Source database name */
6818);
6819SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
6820SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
6821SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
6822SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
6823
6824/*
6825** CAPI3REF: Unlock Notification
6826**
6827** ^When running in shared-cache mode, a database operation may fail with
6828** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
6829** individual tables within the shared-cache cannot be obtained. See
6830** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
6831** ^This API may be used to register a callback that SQLite will invoke
6832** when the connection currently holding the required lock relinquishes it.
6833** ^This API is only available if the library was compiled with the
6834** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
6835**
6836** See Also: [Using the SQLite Unlock Notification Feature].
6837**
6838** ^Shared-cache locks are released when a database connection concludes
6839** its current transaction, either by committing it or rolling it back.
6840**
6841** ^When a connection (known as the blocked connection) fails to obtain a
6842** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
6843** identity of the database connection (the blocking connection) that
6844** has locked the required resource is stored internally. ^After an
6845** application receives an SQLITE_LOCKED error, it may call the
6846** sqlite3_unlock_notify() method with the blocked connection handle as
6847** the first argument to register for a callback that will be invoked
6848** when the blocking connections current transaction is concluded. ^The
6849** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
6850** call that concludes the blocking connections transaction.
6851**
6852** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
6853** there is a chance that the blocking connection will have already
6854** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
6855** If this happens, then the specified callback is invoked immediately,
6856** from within the call to sqlite3_unlock_notify().)^
6857**
6858** ^If the blocked connection is attempting to obtain a write-lock on a
6859** shared-cache table, and more than one other connection currently holds
6860** a read-lock on the same table, then SQLite arbitrarily selects one of
6861** the other connections to use as the blocking connection.
6862**
6863** ^(There may be at most one unlock-notify callback registered by a
6864** blocked connection. If sqlite3_unlock_notify() is called when the
6865** blocked connection already has a registered unlock-notify callback,
6866** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6867** called with a NULL pointer as its second argument, then any existing
6868** unlock-notify callback is canceled. ^The blocked connections
6869** unlock-notify callback may also be canceled by closing the blocked
6870** connection using [sqlite3_close()].
6871**
6872** The unlock-notify callback is not reentrant. If an application invokes
6873** any sqlite3_xxx API functions from within an unlock-notify callback, a
6874** crash or deadlock may be the result.
6875**
6876** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
6877** returns SQLITE_OK.
6878**
6879** <b>Callback Invocation Details</b>
6880**
6881** When an unlock-notify callback is registered, the application provides a
6882** single void* pointer that is passed to the callback when it is invoked.
6883** However, the signature of the callback function allows SQLite to pass
6884** it an array of void* context pointers. The first argument passed to
6885** an unlock-notify callback is a pointer to an array of void* pointers,
6886** and the second is the number of entries in the array.
6887**
6888** When a blocking connections transaction is concluded, there may be
6889** more than one blocked connection that has registered for an unlock-notify
6890** callback. ^If two or more such blocked connections have specified the
6891** same callback function, then instead of invoking the callback function
6892** multiple times, it is invoked once with the set of void* context pointers
6893** specified by the blocked connections bundled together into an array.
6894** This gives the application an opportunity to prioritize any actions
6895** related to the set of unblocked database connections.
6896**
6897** <b>Deadlock Detection</b>
6898**
6899** Assuming that after registering for an unlock-notify callback a
6900** database waits for the callback to be issued before taking any further
6901** action (a reasonable assumption), then using this API may cause the
6902** application to deadlock. For example, if connection X is waiting for
6903** connection Y's transaction to be concluded, and similarly connection
6904** Y is waiting on connection X's transaction, then neither connection
6905** will proceed and the system may remain deadlocked indefinitely.
6906**
6907** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
6908** detection. ^If a given call to sqlite3_unlock_notify() would put the
6909** system in a deadlocked state, then SQLITE_LOCKED is returned and no
6910** unlock-notify callback is registered. The system is said to be in
6911** a deadlocked state if connection A has registered for an unlock-notify
6912** callback on the conclusion of connection B's transaction, and connection
6913** B has itself registered for an unlock-notify callback when connection
6914** A's transaction is concluded. ^Indirect deadlock is also detected, so
6915** the system is also considered to be deadlocked if connection B has
6916** registered for an unlock-notify callback on the conclusion of connection
6917** C's transaction, where connection C is waiting on connection A. ^Any
6918** number of levels of indirection are allowed.
6919**
6920** <b>The "DROP TABLE" Exception</b>
6921**
6922** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
6923** always appropriate to call sqlite3_unlock_notify(). There is however,
6924** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
6925** SQLite checks if there are any currently executing SELECT statements
6926** that belong to the same connection. If there are, SQLITE_LOCKED is
6927** returned. In this case there is no "blocking connection", so invoking
6928** sqlite3_unlock_notify() results in the unlock-notify callback being
6929** invoked immediately. If the application then re-attempts the "DROP TABLE"
6930** or "DROP INDEX" query, an infinite loop might be the result.
6931**
6932** One way around this problem is to check the extended error code returned
6933** by an sqlite3_step() call. ^(If there is a blocking connection, then the
6934** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
6935** the special "DROP TABLE/INDEX" case, the extended error code is just
6936** SQLITE_LOCKED.)^
6937*/
6938SQLITE_API int sqlite3_unlock_notify(
6939  sqlite3 *pBlocked,                          /* Waiting connection */
6940  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
6941  void *pNotifyArg                            /* Argument to pass to xNotify */
6942);
6943
6944
6945/*
6946** CAPI3REF: String Comparison
6947**
6948** ^The [sqlite3_strnicmp()] API allows applications and extensions to
6949** compare the contents of two buffers containing UTF-8 strings in a
6950** case-independent fashion, using the same definition of case independence
6951** that SQLite uses internally when comparing identifiers.
6952*/
6953SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6954
6955/*
6956** CAPI3REF: Error Logging Interface
6957**
6958** ^The [sqlite3_log()] interface writes a message into the error log
6959** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
6960** ^If logging is enabled, the zFormat string and subsequent arguments are
6961** used with [sqlite3_snprintf()] to generate the final output string.
6962**
6963** The sqlite3_log() interface is intended for use by extensions such as
6964** virtual tables, collating functions, and SQL functions.  While there is
6965** nothing to prevent an application from calling sqlite3_log(), doing so
6966** is considered bad form.
6967**
6968** The zFormat string must not be NULL.
6969**
6970** To avoid deadlocks and other threading problems, the sqlite3_log() routine
6971** will not use dynamically allocated memory.  The log message is stored in
6972** a fixed-length buffer on the stack.  If the log message is longer than
6973** a few hundred characters, it will be truncated to the length of the
6974** buffer.
6975*/
6976SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
6977
6978/*
6979** CAPI3REF: Write-Ahead Log Commit Hook
6980**
6981** ^The [sqlite3_wal_hook()] function is used to register a callback that
6982** will be invoked each time a database connection commits data to a
6983** [write-ahead log] (i.e. whenever a transaction is committed in
6984** [journal_mode | journal_mode=WAL mode]).
6985**
6986** ^The callback is invoked by SQLite after the commit has taken place and
6987** the associated write-lock on the database released, so the implementation
6988** may read, write or [checkpoint] the database as required.
6989**
6990** ^The first parameter passed to the callback function when it is invoked
6991** is a copy of the third parameter passed to sqlite3_wal_hook() when
6992** registering the callback. ^The second is a copy of the database handle.
6993** ^The third parameter is the name of the database that was written to -
6994** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
6995** is the number of pages currently in the write-ahead log file,
6996** including those that were just committed.
6997**
6998** The callback function should normally return [SQLITE_OK].  ^If an error
6999** code is returned, that error will propagate back up through the
7000** SQLite code base to cause the statement that provoked the callback
7001** to report an error, though the commit will have still occurred. If the
7002** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
7003** that does not correspond to any valid SQLite error code, the results
7004** are undefined.
7005**
7006** A single database handle may have at most a single write-ahead log callback
7007** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
7008** previously registered write-ahead log callback. ^Note that the
7009** [sqlite3_wal_autocheckpoint()] interface and the
7010** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
7011** those overwrite any prior [sqlite3_wal_hook()] settings.
7012*/
7013SQLITE_API void *sqlite3_wal_hook(
7014  sqlite3*,
7015  int(*)(void *,sqlite3*,const char*,int),
7016  void*
7017);
7018
7019/*
7020** CAPI3REF: Configure an auto-checkpoint
7021**
7022** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
7023** [sqlite3_wal_hook()] that causes any database on [database connection] D
7024** to automatically [checkpoint]
7025** after committing a transaction if there are N or
7026** more frames in the [write-ahead log] file.  ^Passing zero or
7027** a negative value as the nFrame parameter disables automatic
7028** checkpoints entirely.
7029**
7030** ^The callback registered by this function replaces any existing callback
7031** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
7032** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7033** configured by this function.
7034**
7035** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7036** from SQL.
7037**
7038** ^Every new [database connection] defaults to having the auto-checkpoint
7039** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7040** pages.  The use of this interface
7041** is only necessary if the default setting is found to be suboptimal
7042** for a particular application.
7043*/
7044SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
7045
7046/*
7047** CAPI3REF: Checkpoint a database
7048**
7049** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7050** on [database connection] D to be [checkpointed].  ^If X is NULL or an
7051** empty string, then a checkpoint is run on all databases of
7052** connection D.  ^If the database connection D is not in
7053** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7054**
7055** ^The [wal_checkpoint pragma] can be used to invoke this interface
7056** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
7057** [wal_autocheckpoint pragma] can be used to cause this interface to be
7058** run whenever the WAL reaches a certain size threshold.
7059**
7060** See also: [sqlite3_wal_checkpoint_v2()]
7061*/
7062SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
7063
7064/*
7065** CAPI3REF: Checkpoint a database
7066**
7067** Run a checkpoint operation on WAL database zDb attached to database
7068** handle db. The specific operation is determined by the value of the
7069** eMode parameter:
7070**
7071** <dl>
7072** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7073**   Checkpoint as many frames as possible without waiting for any database
7074**   readers or writers to finish. Sync the db file if all frames in the log
7075**   are checkpointed. This mode is the same as calling
7076**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7077**
7078** <dt>SQLITE_CHECKPOINT_FULL<dd>
7079**   This mode blocks (calls the busy-handler callback) until there is no
7080**   database writer and all readers are reading from the most recent database
7081**   snapshot. It then checkpoints all frames in the log file and syncs the
7082**   database file. This call blocks database writers while it is running,
7083**   but not database readers.
7084**
7085** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7086**   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7087**   checkpointing the log file it blocks (calls the busy-handler callback)
7088**   until all readers are reading from the database file only. This ensures
7089**   that the next client to write to the database file restarts the log file
7090**   from the beginning. This call blocks database writers while it is running,
7091**   but not database readers.
7092** </dl>
7093**
7094** If pnLog is not NULL, then *pnLog is set to the total number of frames in
7095** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
7096** the total number of checkpointed frames (including any that were already
7097** checkpointed when this function is called). *pnLog and *pnCkpt may be
7098** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
7099** If no values are available because of an error, they are both set to -1
7100** before returning to communicate this to the caller.
7101**
7102** All calls obtain an exclusive "checkpoint" lock on the database file. If
7103** any other process is running a checkpoint operation at the same time, the
7104** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
7105** busy-handler configured, it will not be invoked in this case.
7106**
7107** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
7108** "writer" lock on the database file. If the writer lock cannot be obtained
7109** immediately, and a busy-handler is configured, it is invoked and the writer
7110** lock retried until either the busy-handler returns 0 or the lock is
7111** successfully obtained. The busy-handler is also invoked while waiting for
7112** database readers as described above. If the busy-handler returns 0 before
7113** the writer lock is obtained or while waiting for database readers, the
7114** checkpoint operation proceeds from that point in the same way as
7115** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
7116** without blocking any further. SQLITE_BUSY is returned in this case.
7117**
7118** If parameter zDb is NULL or points to a zero length string, then the
7119** specified operation is attempted on all WAL databases. In this case the
7120** values written to output parameters *pnLog and *pnCkpt are undefined. If
7121** an SQLITE_BUSY error is encountered when processing one or more of the
7122** attached WAL databases, the operation is still attempted on any remaining
7123** attached databases and SQLITE_BUSY is returned to the caller. If any other
7124** error occurs while processing an attached database, processing is abandoned
7125** and the error code returned to the caller immediately. If no error
7126** (SQLITE_BUSY or otherwise) is encountered while processing the attached
7127** databases, SQLITE_OK is returned.
7128**
7129** If database zDb is the name of an attached database that is not in WAL
7130** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
7131** zDb is not NULL (or a zero length string) and is not the name of any
7132** attached database, SQLITE_ERROR is returned to the caller.
7133*/
7134SQLITE_API int sqlite3_wal_checkpoint_v2(
7135  sqlite3 *db,                    /* Database handle */
7136  const char *zDb,                /* Name of attached database (or NULL) */
7137  int eMode,                      /* SQLITE_CHECKPOINT_* value */
7138  int *pnLog,                     /* OUT: Size of WAL log in frames */
7139  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
7140);
7141
7142/*
7143** CAPI3REF: Checkpoint operation parameters
7144**
7145** These constants can be used as the 3rd parameter to
7146** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
7147** documentation for additional information about the meaning and use of
7148** each of these values.
7149*/
7150#define SQLITE_CHECKPOINT_PASSIVE 0
7151#define SQLITE_CHECKPOINT_FULL    1
7152#define SQLITE_CHECKPOINT_RESTART 2
7153
7154/*
7155** CAPI3REF: Virtual Table Interface Configuration
7156**
7157** This function may be called by either the [xConnect] or [xCreate] method
7158** of a [virtual table] implementation to configure
7159** various facets of the virtual table interface.
7160**
7161** If this interface is invoked outside the context of an xConnect or
7162** xCreate virtual table method then the behavior is undefined.
7163**
7164** At present, there is only one option that may be configured using
7165** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
7166** may be added in the future.
7167*/
7168SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
7169
7170/*
7171** CAPI3REF: Virtual Table Configuration Options
7172**
7173** These macros define the various options to the
7174** [sqlite3_vtab_config()] interface that [virtual table] implementations
7175** can use to customize and optimize their behavior.
7176**
7177** <dl>
7178** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
7179** <dd>Calls of the form
7180** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
7181** where X is an integer.  If X is zero, then the [virtual table] whose
7182** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
7183** support constraints.  In this configuration (which is the default) if
7184** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
7185** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
7186** specified as part of the users SQL statement, regardless of the actual
7187** ON CONFLICT mode specified.
7188**
7189** If X is non-zero, then the virtual table implementation guarantees
7190** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
7191** any modifications to internal or persistent data structures have been made.
7192** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
7193** is able to roll back a statement or database transaction, and abandon
7194** or continue processing the current SQL statement as appropriate.
7195** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
7196** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
7197** had been ABORT.
7198**
7199** Virtual table implementations that are required to handle OR REPLACE
7200** must do so within the [xUpdate] method. If a call to the
7201** [sqlite3_vtab_on_conflict()] function indicates that the current ON
7202** CONFLICT policy is REPLACE, the virtual table implementation should
7203** silently replace the appropriate rows within the xUpdate callback and
7204** return SQLITE_OK. Or, if this is not possible, it may return
7205** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
7206** constraint handling.
7207** </dl>
7208*/
7209#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
7210
7211/*
7212** CAPI3REF: Determine The Virtual Table Conflict Policy
7213**
7214** This function may only be called from within a call to the [xUpdate] method
7215** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
7216** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
7217** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
7218** of the SQL statement that triggered the call to the [xUpdate] method of the
7219** [virtual table].
7220*/
7221SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7222
7223/*
7224** CAPI3REF: Conflict resolution modes
7225**
7226** These constants are returned by [sqlite3_vtab_on_conflict()] to
7227** inform a [virtual table] implementation what the [ON CONFLICT] mode
7228** is for the SQL statement being evaluated.
7229**
7230** Note that the [SQLITE_IGNORE] constant is also used as a potential
7231** return value from the [sqlite3_set_authorizer()] callback and that
7232** [SQLITE_ABORT] is also a [result code].
7233*/
7234#define SQLITE_ROLLBACK 1
7235/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7236#define SQLITE_FAIL     3
7237/* #define SQLITE_ABORT 4  // Also an error code */
7238#define SQLITE_REPLACE  5
7239
7240
7241
7242/*
7243** Undo the hack that converts floating point types to integer for
7244** builds on processors without floating point support.
7245*/
7246#ifdef SQLITE_OMIT_FLOATING_POINT
7247# undef double
7248#endif
7249
7250#if 0
7251}  /* End of the 'extern "C"' block */
7252#endif
7253#endif
7254
7255/*
7256** 2010 August 30
7257**
7258** The author disclaims copyright to this source code.  In place of
7259** a legal notice, here is a blessing:
7260**
7261**    May you do good and not evil.
7262**    May you find forgiveness for yourself and forgive others.
7263**    May you share freely, never taking more than you give.
7264**
7265*************************************************************************
7266*/
7267
7268#ifndef _SQLITE3RTREE_H_
7269#define _SQLITE3RTREE_H_
7270
7271
7272#if 0
7273extern "C" {
7274#endif
7275
7276typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7277
7278/*
7279** Register a geometry callback named zGeom that can be used as part of an
7280** R-Tree geometry query as follows:
7281**
7282**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7283*/
7284SQLITE_API int sqlite3_rtree_geometry_callback(
7285  sqlite3 *db,
7286  const char *zGeom,
7287  int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
7288  void *pContext
7289);
7290
7291
7292/*
7293** A pointer to a structure of the following type is passed as the first
7294** argument to callbacks registered using rtree_geometry_callback().
7295*/
7296struct sqlite3_rtree_geometry {
7297  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
7298  int nParam;                     /* Size of array aParam[] */
7299  double *aParam;                 /* Parameters passed to SQL geom function */
7300  void *pUser;                    /* Callback implementation user data */
7301  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
7302};
7303
7304
7305#if 0
7306}  /* end of the 'extern "C"' block */
7307#endif
7308
7309#endif  /* ifndef _SQLITE3RTREE_H_ */
7310
7311
7312/************** End of sqlite3.h *********************************************/
7313/************** Continuing where we left off in sqliteInt.h ******************/
7314/************** Include hash.h in the middle of sqliteInt.h ******************/
7315/************** Begin file hash.h ********************************************/
7316/*
7317** 2001 September 22
7318**
7319** The author disclaims copyright to this source code.  In place of
7320** a legal notice, here is a blessing:
7321**
7322**    May you do good and not evil.
7323**    May you find forgiveness for yourself and forgive others.
7324**    May you share freely, never taking more than you give.
7325**
7326*************************************************************************
7327** This is the header file for the generic hash-table implemenation
7328** used in SQLite.
7329*/
7330#ifndef _SQLITE_HASH_H_
7331#define _SQLITE_HASH_H_
7332
7333/* Forward declarations of structures. */
7334typedef struct Hash Hash;
7335typedef struct HashElem HashElem;
7336
7337/* A complete hash table is an instance of the following structure.
7338** The internals of this structure are intended to be opaque -- client
7339** code should not attempt to access or modify the fields of this structure
7340** directly.  Change this structure only by using the routines below.
7341** However, some of the "procedures" and "functions" for modifying and
7342** accessing this structure are really macros, so we can't really make
7343** this structure opaque.
7344**
7345** All elements of the hash table are on a single doubly-linked list.
7346** Hash.first points to the head of this list.
7347**
7348** There are Hash.htsize buckets.  Each bucket points to a spot in
7349** the global doubly-linked list.  The contents of the bucket are the
7350** element pointed to plus the next _ht.count-1 elements in the list.
7351**
7352** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
7353** by a linear search of the global list.  For small tables, the
7354** Hash.ht table is never allocated because if there are few elements
7355** in the table, it is faster to do a linear search than to manage
7356** the hash table.
7357*/
7358struct Hash {
7359  unsigned int htsize;      /* Number of buckets in the hash table */
7360  unsigned int count;       /* Number of entries in this table */
7361  HashElem *first;          /* The first element of the array */
7362  struct _ht {              /* the hash table */
7363    int count;                 /* Number of entries with this hash */
7364    HashElem *chain;           /* Pointer to first entry with this hash */
7365  } *ht;
7366};
7367
7368/* Each element in the hash table is an instance of the following
7369** structure.  All elements are stored on a single doubly-linked list.
7370**
7371** Again, this structure is intended to be opaque, but it can't really
7372** be opaque because it is used by macros.
7373*/
7374struct HashElem {
7375  HashElem *next, *prev;       /* Next and previous elements in the table */
7376  void *data;                  /* Data associated with this element */
7377  const char *pKey; int nKey;  /* Key associated with this element */
7378};
7379
7380/*
7381** Access routines.  To delete, insert a NULL pointer.
7382*/
7383SQLITE_PRIVATE void sqlite3HashInit(Hash*);
7384SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
7385SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
7386SQLITE_PRIVATE void sqlite3HashClear(Hash*);
7387
7388/*
7389** Macros for looping over all elements of a hash table.  The idiom is
7390** like this:
7391**
7392**   Hash h;
7393**   HashElem *p;
7394**   ...
7395**   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
7396**     SomeStructure *pData = sqliteHashData(p);
7397**     // do something with pData
7398**   }
7399*/
7400#define sqliteHashFirst(H)  ((H)->first)
7401#define sqliteHashNext(E)   ((E)->next)
7402#define sqliteHashData(E)   ((E)->data)
7403/* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
7404/* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
7405
7406/*
7407** Number of entries in a hash table
7408*/
7409/* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
7410
7411#endif /* _SQLITE_HASH_H_ */
7412
7413/************** End of hash.h ************************************************/
7414/************** Continuing where we left off in sqliteInt.h ******************/
7415/************** Include parse.h in the middle of sqliteInt.h *****************/
7416/************** Begin file parse.h *******************************************/
7417#define TK_SEMI                            1
7418#define TK_EXPLAIN                         2
7419#define TK_QUERY                           3
7420#define TK_PLAN                            4
7421#define TK_BEGIN                           5
7422#define TK_TRANSACTION                     6
7423#define TK_DEFERRED                        7
7424#define TK_IMMEDIATE                       8
7425#define TK_EXCLUSIVE                       9
7426#define TK_COMMIT                         10
7427#define TK_END                            11
7428#define TK_ROLLBACK                       12
7429#define TK_SAVEPOINT                      13
7430#define TK_RELEASE                        14
7431#define TK_TO                             15
7432#define TK_TABLE                          16
7433#define TK_CREATE                         17
7434#define TK_IF                             18
7435#define TK_NOT                            19
7436#define TK_EXISTS                         20
7437#define TK_TEMP                           21
7438#define TK_LP                             22
7439#define TK_RP                             23
7440#define TK_AS                             24
7441#define TK_COMMA                          25
7442#define TK_ID                             26
7443#define TK_INDEXED                        27
7444#define TK_ABORT                          28
7445#define TK_ACTION                         29
7446#define TK_AFTER                          30
7447#define TK_ANALYZE                        31
7448#define TK_ASC                            32
7449#define TK_ATTACH                         33
7450#define TK_BEFORE                         34
7451#define TK_BY                             35
7452#define TK_CASCADE                        36
7453#define TK_CAST                           37
7454#define TK_COLUMNKW                       38
7455#define TK_CONFLICT                       39
7456#define TK_DATABASE                       40
7457#define TK_DESC                           41
7458#define TK_DETACH                         42
7459#define TK_EACH                           43
7460#define TK_FAIL                           44
7461#define TK_FOR                            45
7462#define TK_IGNORE                         46
7463#define TK_INITIALLY                      47
7464#define TK_INSTEAD                        48
7465#define TK_LIKE_KW                        49
7466#define TK_MATCH                          50
7467#define TK_NO                             51
7468#define TK_KEY                            52
7469#define TK_OF                             53
7470#define TK_OFFSET                         54
7471#define TK_PRAGMA                         55
7472#define TK_RAISE                          56
7473#define TK_REPLACE                        57
7474#define TK_RESTRICT                       58
7475#define TK_ROW                            59
7476#define TK_TRIGGER                        60
7477#define TK_VACUUM                         61
7478#define TK_VIEW                           62
7479#define TK_VIRTUAL                        63
7480#define TK_REINDEX                        64
7481#define TK_RENAME                         65
7482#define TK_CTIME_KW                       66
7483#define TK_ANY                            67
7484#define TK_OR                             68
7485#define TK_AND                            69
7486#define TK_IS                             70
7487#define TK_BETWEEN                        71
7488#define TK_IN                             72
7489#define TK_ISNULL                         73
7490#define TK_NOTNULL                        74
7491#define TK_NE                             75
7492#define TK_EQ                             76
7493#define TK_GT                             77
7494#define TK_LE                             78
7495#define TK_LT                             79
7496#define TK_GE                             80
7497#define TK_ESCAPE                         81
7498#define TK_BITAND                         82
7499#define TK_BITOR                          83
7500#define TK_LSHIFT                         84
7501#define TK_RSHIFT                         85
7502#define TK_PLUS                           86
7503#define TK_MINUS                          87
7504#define TK_STAR                           88
7505#define TK_SLASH                          89
7506#define TK_REM                            90
7507#define TK_CONCAT                         91
7508#define TK_COLLATE                        92
7509#define TK_BITNOT                         93
7510#define TK_STRING                         94
7511#define TK_JOIN_KW                        95
7512#define TK_CONSTRAINT                     96
7513#define TK_DEFAULT                        97
7514#define TK_NULL                           98
7515#define TK_PRIMARY                        99
7516#define TK_UNIQUE                         100
7517#define TK_CHECK                          101
7518#define TK_REFERENCES                     102
7519#define TK_AUTOINCR                       103
7520#define TK_ON                             104
7521#define TK_INSERT                         105
7522#define TK_DELETE                         106
7523#define TK_UPDATE                         107
7524#define TK_SET                            108
7525#define TK_DEFERRABLE                     109
7526#define TK_FOREIGN                        110
7527#define TK_DROP                           111
7528#define TK_UNION                          112
7529#define TK_ALL                            113
7530#define TK_EXCEPT                         114
7531#define TK_INTERSECT                      115
7532#define TK_SELECT                         116
7533#define TK_DISTINCT                       117
7534#define TK_DOT                            118
7535#define TK_FROM                           119
7536#define TK_JOIN                           120
7537#define TK_USING                          121
7538#define TK_ORDER                          122
7539#define TK_GROUP                          123
7540#define TK_HAVING                         124
7541#define TK_LIMIT                          125
7542#define TK_WHERE                          126
7543#define TK_INTO                           127
7544#define TK_VALUES                         128
7545#define TK_INTEGER                        129
7546#define TK_FLOAT                          130
7547#define TK_BLOB                           131
7548#define TK_REGISTER                       132
7549#define TK_VARIABLE                       133
7550#define TK_CASE                           134
7551#define TK_WHEN                           135
7552#define TK_THEN                           136
7553#define TK_ELSE                           137
7554#define TK_INDEX                          138
7555#define TK_ALTER                          139
7556#define TK_ADD                            140
7557#define TK_TO_TEXT                        141
7558#define TK_TO_BLOB                        142
7559#define TK_TO_NUMERIC                     143
7560#define TK_TO_INT                         144
7561#define TK_TO_REAL                        145
7562#define TK_ISNOT                          146
7563#define TK_END_OF_FILE                    147
7564#define TK_ILLEGAL                        148
7565#define TK_SPACE                          149
7566#define TK_UNCLOSED_STRING                150
7567#define TK_FUNCTION                       151
7568#define TK_COLUMN                         152
7569#define TK_AGG_FUNCTION                   153
7570#define TK_AGG_COLUMN                     154
7571#define TK_CONST_FUNC                     155
7572#define TK_UMINUS                         156
7573#define TK_UPLUS                          157
7574
7575/************** End of parse.h ***********************************************/
7576/************** Continuing where we left off in sqliteInt.h ******************/
7577#include <stdio.h>
7578#include <stdlib.h>
7579#include <string.h>
7580#include <assert.h>
7581#include <stddef.h>
7582
7583/*
7584** If compiling for a processor that lacks floating point support,
7585** substitute integer for floating-point
7586*/
7587#ifdef SQLITE_OMIT_FLOATING_POINT
7588# define double sqlite_int64
7589# define float sqlite_int64
7590# define LONGDOUBLE_TYPE sqlite_int64
7591# ifndef SQLITE_BIG_DBL
7592#   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
7593# endif
7594# define SQLITE_OMIT_DATETIME_FUNCS 1
7595# define SQLITE_OMIT_TRACE 1
7596# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
7597# undef SQLITE_HAVE_ISNAN
7598#endif
7599#ifndef SQLITE_BIG_DBL
7600# define SQLITE_BIG_DBL (1e99)
7601#endif
7602
7603/*
7604** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
7605** afterward. Having this macro allows us to cause the C compiler
7606** to omit code used by TEMP tables without messy #ifndef statements.
7607*/
7608#ifdef SQLITE_OMIT_TEMPDB
7609#define OMIT_TEMPDB 1
7610#else
7611#define OMIT_TEMPDB 0
7612#endif
7613
7614/*
7615** The "file format" number is an integer that is incremented whenever
7616** the VDBE-level file format changes.  The following macros define the
7617** the default file format for new databases and the maximum file format
7618** that the library can read.
7619*/
7620#define SQLITE_MAX_FILE_FORMAT 4
7621#ifndef SQLITE_DEFAULT_FILE_FORMAT
7622# define SQLITE_DEFAULT_FILE_FORMAT 1
7623#endif
7624
7625/*
7626** Determine whether triggers are recursive by default.  This can be
7627** changed at run-time using a pragma.
7628*/
7629#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
7630# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
7631#endif
7632
7633/*
7634** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
7635** on the command-line
7636*/
7637#ifndef SQLITE_TEMP_STORE
7638# define SQLITE_TEMP_STORE 1
7639#endif
7640
7641/*
7642** GCC does not define the offsetof() macro so we'll have to do it
7643** ourselves.
7644*/
7645#ifndef offsetof
7646#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
7647#endif
7648
7649/*
7650** Check to see if this machine uses EBCDIC.  (Yes, believe it or
7651** not, there are still machines out there that use EBCDIC.)
7652*/
7653#if 'A' == '\301'
7654# define SQLITE_EBCDIC 1
7655#else
7656# define SQLITE_ASCII 1
7657#endif
7658
7659/*
7660** Integers of known sizes.  These typedefs might change for architectures
7661** where the sizes very.  Preprocessor macros are available so that the
7662** types can be conveniently redefined at compile-type.  Like this:
7663**
7664**         cc '-DUINTPTR_TYPE=long long int' ...
7665*/
7666#ifndef UINT32_TYPE
7667# ifdef HAVE_UINT32_T
7668#  define UINT32_TYPE uint32_t
7669# else
7670#  define UINT32_TYPE unsigned int
7671# endif
7672#endif
7673#ifndef UINT16_TYPE
7674# ifdef HAVE_UINT16_T
7675#  define UINT16_TYPE uint16_t
7676# else
7677#  define UINT16_TYPE unsigned short int
7678# endif
7679#endif
7680#ifndef INT16_TYPE
7681# ifdef HAVE_INT16_T
7682#  define INT16_TYPE int16_t
7683# else
7684#  define INT16_TYPE short int
7685# endif
7686#endif
7687#ifndef UINT8_TYPE
7688# ifdef HAVE_UINT8_T
7689#  define UINT8_TYPE uint8_t
7690# else
7691#  define UINT8_TYPE unsigned char
7692# endif
7693#endif
7694#ifndef INT8_TYPE
7695# ifdef HAVE_INT8_T
7696#  define INT8_TYPE int8_t
7697# else
7698#  define INT8_TYPE signed char
7699# endif
7700#endif
7701#ifndef LONGDOUBLE_TYPE
7702# define LONGDOUBLE_TYPE long double
7703#endif
7704typedef sqlite_int64 i64;          /* 8-byte signed integer */
7705typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
7706typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
7707typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
7708typedef INT16_TYPE i16;            /* 2-byte signed integer */
7709typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
7710typedef INT8_TYPE i8;              /* 1-byte signed integer */
7711
7712/*
7713** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
7714** that can be stored in a u32 without loss of data.  The value
7715** is 0x00000000ffffffff.  But because of quirks of some compilers, we
7716** have to specify the value in the less intuitive manner shown:
7717*/
7718#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
7719
7720/*
7721** Macros to determine whether the machine is big or little endian,
7722** evaluated at runtime.
7723*/
7724#ifdef SQLITE_AMALGAMATION
7725SQLITE_PRIVATE const int sqlite3one = 1;
7726#else
7727SQLITE_PRIVATE const int sqlite3one;
7728#endif
7729#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
7730                             || defined(__x86_64) || defined(__x86_64__)
7731# define SQLITE_BIGENDIAN    0
7732# define SQLITE_LITTLEENDIAN 1
7733# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
7734#else
7735# define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
7736# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
7737# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
7738#endif
7739
7740/*
7741** Constants for the largest and smallest possible 64-bit signed integers.
7742** These macros are designed to work correctly on both 32-bit and 64-bit
7743** compilers.
7744*/
7745#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
7746#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
7747
7748/*
7749** Round up a number to the next larger multiple of 8.  This is used
7750** to force 8-byte alignment on 64-bit architectures.
7751*/
7752#define ROUND8(x)     (((x)+7)&~7)
7753
7754/*
7755** Round down to the nearest multiple of 8
7756*/
7757#define ROUNDDOWN8(x) ((x)&~7)
7758
7759/*
7760** Assert that the pointer X is aligned to an 8-byte boundary.  This
7761** macro is used only within assert() to verify that the code gets
7762** all alignment restrictions correct.
7763**
7764** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
7765** underlying malloc() implemention might return us 4-byte aligned
7766** pointers.  In that case, only verify 4-byte alignment.
7767*/
7768#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
7769# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
7770#else
7771# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
7772#endif
7773
7774
7775/*
7776** An instance of the following structure is used to store the busy-handler
7777** callback for a given sqlite handle.
7778**
7779** The sqlite.busyHandler member of the sqlite struct contains the busy
7780** callback for the database handle. Each pager opened via the sqlite
7781** handle is passed a pointer to sqlite.busyHandler. The busy-handler
7782** callback is currently invoked only from within pager.c.
7783*/
7784typedef struct BusyHandler BusyHandler;
7785struct BusyHandler {
7786  int (*xFunc)(void *,int);  /* The busy callback */
7787  void *pArg;                /* First arg to busy callback */
7788  int nBusy;                 /* Incremented with each busy call */
7789};
7790
7791/*
7792** Name of the master database table.  The master database table
7793** is a special table that holds the names and attributes of all
7794** user tables and indices.
7795*/
7796#define MASTER_NAME       "sqlite_master"
7797#define TEMP_MASTER_NAME  "sqlite_temp_master"
7798
7799/*
7800** The root-page of the master database table.
7801*/
7802#define MASTER_ROOT       1
7803
7804/*
7805** The name of the schema table.
7806*/
7807#define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
7808
7809/*
7810** A convenience macro that returns the number of elements in
7811** an array.
7812*/
7813#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
7814
7815/*
7816** The following value as a destructor means to use sqlite3DbFree().
7817** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
7818*/
7819#define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
7820
7821/*
7822** When SQLITE_OMIT_WSD is defined, it means that the target platform does
7823** not support Writable Static Data (WSD) such as global and static variables.
7824** All variables must either be on the stack or dynamically allocated from
7825** the heap.  When WSD is unsupported, the variable declarations scattered
7826** throughout the SQLite code must become constants instead.  The SQLITE_WSD
7827** macro is used for this purpose.  And instead of referencing the variable
7828** directly, we use its constant as a key to lookup the run-time allocated
7829** buffer that holds real variable.  The constant is also the initializer
7830** for the run-time allocated buffer.
7831**
7832** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
7833** macros become no-ops and have zero performance impact.
7834*/
7835#ifdef SQLITE_OMIT_WSD
7836  #define SQLITE_WSD const
7837  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
7838  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
7839SQLITE_API   int sqlite3_wsd_init(int N, int J);
7840SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
7841#else
7842  #define SQLITE_WSD
7843  #define GLOBAL(t,v) v
7844  #define sqlite3GlobalConfig sqlite3Config
7845#endif
7846
7847/*
7848** The following macros are used to suppress compiler warnings and to
7849** make it clear to human readers when a function parameter is deliberately
7850** left unused within the body of a function. This usually happens when
7851** a function is called via a function pointer. For example the
7852** implementation of an SQL aggregate step callback may not use the
7853** parameter indicating the number of arguments passed to the aggregate,
7854** if it knows that this is enforced elsewhere.
7855**
7856** When a function parameter is not used at all within the body of a function,
7857** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
7858** However, these macros may also be used to suppress warnings related to
7859** parameters that may or may not be used depending on compilation options.
7860** For example those parameters only used in assert() statements. In these
7861** cases the parameters are named as per the usual conventions.
7862*/
7863#define UNUSED_PARAMETER(x) (void)(x)
7864#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
7865
7866/*
7867** Forward references to structures
7868*/
7869typedef struct AggInfo AggInfo;
7870typedef struct AuthContext AuthContext;
7871typedef struct AutoincInfo AutoincInfo;
7872typedef struct Bitvec Bitvec;
7873typedef struct CollSeq CollSeq;
7874typedef struct Column Column;
7875typedef struct Db Db;
7876typedef struct Schema Schema;
7877typedef struct Expr Expr;
7878typedef struct ExprList ExprList;
7879typedef struct ExprSpan ExprSpan;
7880typedef struct FKey FKey;
7881typedef struct FuncDestructor FuncDestructor;
7882typedef struct FuncDef FuncDef;
7883typedef struct FuncDefHash FuncDefHash;
7884typedef struct IdList IdList;
7885typedef struct Index Index;
7886typedef struct IndexSample IndexSample;
7887typedef struct KeyClass KeyClass;
7888typedef struct KeyInfo KeyInfo;
7889typedef struct Lookaside Lookaside;
7890typedef struct LookasideSlot LookasideSlot;
7891typedef struct Module Module;
7892typedef struct NameContext NameContext;
7893typedef struct Parse Parse;
7894typedef struct RowSet RowSet;
7895typedef struct Savepoint Savepoint;
7896typedef struct Select Select;
7897typedef struct SrcList SrcList;
7898typedef struct StrAccum StrAccum;
7899typedef struct Table Table;
7900typedef struct TableLock TableLock;
7901typedef struct Token Token;
7902typedef struct Trigger Trigger;
7903typedef struct TriggerPrg TriggerPrg;
7904typedef struct TriggerStep TriggerStep;
7905typedef struct UnpackedRecord UnpackedRecord;
7906typedef struct VTable VTable;
7907typedef struct VtabCtx VtabCtx;
7908typedef struct Walker Walker;
7909typedef struct WherePlan WherePlan;
7910typedef struct WhereInfo WhereInfo;
7911typedef struct WhereLevel WhereLevel;
7912
7913/*
7914** Defer sourcing vdbe.h and btree.h until after the "u8" and
7915** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
7916** pointer types (i.e. FuncDef) defined above.
7917*/
7918/************** Include btree.h in the middle of sqliteInt.h *****************/
7919/************** Begin file btree.h *******************************************/
7920/*
7921** 2001 September 15
7922**
7923** The author disclaims copyright to this source code.  In place of
7924** a legal notice, here is a blessing:
7925**
7926**    May you do good and not evil.
7927**    May you find forgiveness for yourself and forgive others.
7928**    May you share freely, never taking more than you give.
7929**
7930*************************************************************************
7931** This header file defines the interface that the sqlite B-Tree file
7932** subsystem.  See comments in the source code for a detailed description
7933** of what each interface routine does.
7934*/
7935#ifndef _BTREE_H_
7936#define _BTREE_H_
7937
7938/* TODO: This definition is just included so other modules compile. It
7939** needs to be revisited.
7940*/
7941#define SQLITE_N_BTREE_META 10
7942
7943/*
7944** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
7945** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
7946*/
7947#ifndef SQLITE_DEFAULT_AUTOVACUUM
7948  #define SQLITE_DEFAULT_AUTOVACUUM 0
7949#endif
7950
7951#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
7952#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
7953#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
7954
7955/*
7956** Forward declarations of structure
7957*/
7958typedef struct Btree Btree;
7959typedef struct BtCursor BtCursor;
7960typedef struct BtShared BtShared;
7961
7962
7963SQLITE_PRIVATE int sqlite3BtreeOpen(
7964  sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
7965  const char *zFilename,   /* Name of database file to open */
7966  sqlite3 *db,             /* Associated database connection */
7967  Btree **ppBtree,         /* Return open Btree* here */
7968  int flags,               /* Flags */
7969  int vfsFlags             /* Flags passed through to VFS open */
7970);
7971
7972/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
7973** following values.
7974**
7975** NOTE:  These values must match the corresponding PAGER_ values in
7976** pager.h.
7977*/
7978#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
7979#define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
7980#define BTREE_MEMORY        4  /* This is an in-memory DB */
7981#define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
7982#define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
7983
7984SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
7985SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
7986SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
7987SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
7988SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
7989SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
7990SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
7991SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
7992SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
7993SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
7994SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
7995SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
7996SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
7997SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
7998SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
7999SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
8000SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
8001SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
8002SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
8003SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
8004SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
8005SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
8006SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
8007SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
8008SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
8009SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
8010
8011SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
8012SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
8013SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
8014
8015SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
8016
8017/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
8018** of the flags shown below.
8019**
8020** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
8021** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
8022** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
8023** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
8024** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
8025** indices.)
8026*/
8027#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
8028#define BTREE_BLOBKEY    2    /* Table has keys only - no data */
8029
8030SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
8031SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
8032SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
8033
8034SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
8035SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
8036
8037/*
8038** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
8039** should be one of the following values. The integer values are assigned
8040** to constants so that the offset of the corresponding field in an
8041** SQLite database header may be found using the following formula:
8042**
8043**   offset = 36 + (idx * 4)
8044**
8045** For example, the free-page-count field is located at byte offset 36 of
8046** the database file header. The incr-vacuum-flag field is located at
8047** byte offset 64 (== 36+4*7).
8048*/
8049#define BTREE_FREE_PAGE_COUNT     0
8050#define BTREE_SCHEMA_VERSION      1
8051#define BTREE_FILE_FORMAT         2
8052#define BTREE_DEFAULT_CACHE_SIZE  3
8053#define BTREE_LARGEST_ROOT_PAGE   4
8054#define BTREE_TEXT_ENCODING       5
8055#define BTREE_USER_VERSION        6
8056#define BTREE_INCR_VACUUM         7
8057
8058SQLITE_PRIVATE int sqlite3BtreeCursor(
8059  Btree*,                              /* BTree containing table to open */
8060  int iTable,                          /* Index of root page */
8061  int wrFlag,                          /* 1 for writing.  0 for read-only */
8062  struct KeyInfo*,                     /* First argument to compare function */
8063  BtCursor *pCursor                    /* Space to write cursor structure */
8064);
8065SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
8066SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
8067
8068SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
8069SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
8070  BtCursor*,
8071  UnpackedRecord *pUnKey,
8072  i64 intKey,
8073  int bias,
8074  int *pRes
8075);
8076SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
8077SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
8078SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
8079                                  const void *pData, int nData,
8080                                  int nZero, int bias, int seekResult);
8081SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
8082SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
8083SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
8084SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
8085SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
8086SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
8087SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
8088SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
8089SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
8090SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
8091SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
8092SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
8093SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
8094
8095SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
8096SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
8097
8098SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
8099SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
8100SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
8101
8102SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
8103
8104#ifndef NDEBUG
8105SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
8106#endif
8107
8108#ifndef SQLITE_OMIT_BTREECOUNT
8109SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
8110#endif
8111
8112#ifdef SQLITE_TEST
8113SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
8114SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
8115#endif
8116
8117#ifndef SQLITE_OMIT_WAL
8118SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
8119#endif
8120
8121/*
8122** If we are not using shared cache, then there is no need to
8123** use mutexes to access the BtShared structures.  So make the
8124** Enter and Leave procedures no-ops.
8125*/
8126#ifndef SQLITE_OMIT_SHARED_CACHE
8127SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
8128SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
8129#else
8130# define sqlite3BtreeEnter(X)
8131# define sqlite3BtreeEnterAll(X)
8132#endif
8133
8134#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
8135SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
8136SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
8137SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
8138SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
8139SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
8140#ifndef NDEBUG
8141  /* These routines are used inside assert() statements only. */
8142SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
8143SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
8144SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
8145#endif
8146#else
8147
8148# define sqlite3BtreeSharable(X) 0
8149# define sqlite3BtreeLeave(X)
8150# define sqlite3BtreeEnterCursor(X)
8151# define sqlite3BtreeLeaveCursor(X)
8152# define sqlite3BtreeLeaveAll(X)
8153
8154# define sqlite3BtreeHoldsMutex(X) 1
8155# define sqlite3BtreeHoldsAllMutexes(X) 1
8156# define sqlite3SchemaMutexHeld(X,Y,Z) 1
8157#endif
8158
8159
8160#endif /* _BTREE_H_ */
8161
8162/************** End of btree.h ***********************************************/
8163/************** Continuing where we left off in sqliteInt.h ******************/
8164/************** Include vdbe.h in the middle of sqliteInt.h ******************/
8165/************** Begin file vdbe.h ********************************************/
8166/*
8167** 2001 September 15
8168**
8169** The author disclaims copyright to this source code.  In place of
8170** a legal notice, here is a blessing:
8171**
8172**    May you do good and not evil.
8173**    May you find forgiveness for yourself and forgive others.
8174**    May you share freely, never taking more than you give.
8175**
8176*************************************************************************
8177** Header file for the Virtual DataBase Engine (VDBE)
8178**
8179** This header defines the interface to the virtual database engine
8180** or VDBE.  The VDBE implements an abstract machine that runs a
8181** simple program to access and modify the underlying database.
8182*/
8183#ifndef _SQLITE_VDBE_H_
8184#define _SQLITE_VDBE_H_
8185/* #include <stdio.h> */
8186
8187/*
8188** A single VDBE is an opaque structure named "Vdbe".  Only routines
8189** in the source file sqliteVdbe.c are allowed to see the insides
8190** of this structure.
8191*/
8192typedef struct Vdbe Vdbe;
8193
8194/*
8195** The names of the following types declared in vdbeInt.h are required
8196** for the VdbeOp definition.
8197*/
8198typedef struct VdbeFunc VdbeFunc;
8199typedef struct Mem Mem;
8200typedef struct SubProgram SubProgram;
8201
8202/*
8203** A single instruction of the virtual machine has an opcode
8204** and as many as three operands.  The instruction is recorded
8205** as an instance of the following structure:
8206*/
8207struct VdbeOp {
8208  u8 opcode;          /* What operation to perform */
8209  signed char p4type; /* One of the P4_xxx constants for p4 */
8210  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
8211  u8 p5;              /* Fifth parameter is an unsigned character */
8212  int p1;             /* First operand */
8213  int p2;             /* Second parameter (often the jump destination) */
8214  int p3;             /* The third parameter */
8215  union {             /* fourth parameter */
8216    int i;                 /* Integer value if p4type==P4_INT32 */
8217    void *p;               /* Generic pointer */
8218    char *z;               /* Pointer to data for string (char array) types */
8219    i64 *pI64;             /* Used when p4type is P4_INT64 */
8220    double *pReal;         /* Used when p4type is P4_REAL */
8221    FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
8222    VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
8223    CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
8224    Mem *pMem;             /* Used when p4type is P4_MEM */
8225    VTable *pVtab;         /* Used when p4type is P4_VTAB */
8226    KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
8227    int *ai;               /* Used when p4type is P4_INTARRAY */
8228    SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
8229    int (*xAdvance)(BtCursor *, int *);
8230  } p4;
8231#ifdef SQLITE_DEBUG
8232  char *zComment;          /* Comment to improve readability */
8233#endif
8234#ifdef VDBE_PROFILE
8235  int cnt;                 /* Number of times this instruction was executed */
8236  u64 cycles;              /* Total time spent executing this instruction */
8237#endif
8238};
8239typedef struct VdbeOp VdbeOp;
8240
8241
8242/*
8243** A sub-routine used to implement a trigger program.
8244*/
8245struct SubProgram {
8246  VdbeOp *aOp;                  /* Array of opcodes for sub-program */
8247  int nOp;                      /* Elements in aOp[] */
8248  int nMem;                     /* Number of memory cells required */
8249  int nCsr;                     /* Number of cursors required */
8250  void *token;                  /* id that may be used to recursive triggers */
8251  SubProgram *pNext;            /* Next sub-program already visited */
8252};
8253
8254/*
8255** A smaller version of VdbeOp used for the VdbeAddOpList() function because
8256** it takes up less space.
8257*/
8258struct VdbeOpList {
8259  u8 opcode;          /* What operation to perform */
8260  signed char p1;     /* First operand */
8261  signed char p2;     /* Second parameter (often the jump destination) */
8262  signed char p3;     /* Third parameter */
8263};
8264typedef struct VdbeOpList VdbeOpList;
8265
8266/*
8267** Allowed values of VdbeOp.p4type
8268*/
8269#define P4_NOTUSED    0   /* The P4 parameter is not used */
8270#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
8271#define P4_STATIC   (-2)  /* Pointer to a static string */
8272#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
8273#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
8274#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
8275#define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
8276#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
8277#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
8278#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
8279#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
8280#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
8281#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
8282#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
8283#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8284#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
8285#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
8286
8287/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8288** is made.  That copy is freed when the Vdbe is finalized.  But if the
8289** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
8290** gets freed when the Vdbe is finalized so it still should be obtained
8291** from a single sqliteMalloc().  But no copy is made and the calling
8292** function should *not* try to free the KeyInfo.
8293*/
8294#define P4_KEYINFO_HANDOFF (-16)
8295#define P4_KEYINFO_STATIC  (-17)
8296
8297/*
8298** The Vdbe.aColName array contains 5n Mem structures, where n is the
8299** number of columns of data returned by the statement.
8300*/
8301#define COLNAME_NAME     0
8302#define COLNAME_DECLTYPE 1
8303#define COLNAME_DATABASE 2
8304#define COLNAME_TABLE    3
8305#define COLNAME_COLUMN   4
8306#ifdef SQLITE_ENABLE_COLUMN_METADATA
8307# define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
8308#else
8309# ifdef SQLITE_OMIT_DECLTYPE
8310#   define COLNAME_N      1      /* Store only the name */
8311# else
8312#   define COLNAME_N      2      /* Store the name and decltype */
8313# endif
8314#endif
8315
8316/*
8317** The following macro converts a relative address in the p2 field
8318** of a VdbeOp structure into a negative number so that
8319** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
8320** the macro again restores the address.
8321*/
8322#define ADDR(X)  (-1-(X))
8323
8324/*
8325** The makefile scans the vdbe.c source file and creates the "opcodes.h"
8326** header file that defines a number for each opcode used by the VDBE.
8327*/
8328/************** Include opcodes.h in the middle of vdbe.h ********************/
8329/************** Begin file opcodes.h *****************************************/
8330/* Automatically generated.  Do not edit */
8331/* See the mkopcodeh.awk script for details */
8332#define OP_Goto                                 1
8333#define OP_Gosub                                2
8334#define OP_Return                               3
8335#define OP_Yield                                4
8336#define OP_HaltIfNull                           5
8337#define OP_Halt                                 6
8338#define OP_Integer                              7
8339#define OP_Int64                                8
8340#define OP_Real                               130   /* same as TK_FLOAT    */
8341#define OP_String8                             94   /* same as TK_STRING   */
8342#define OP_String                               9
8343#define OP_Null                                10
8344#define OP_Blob                                11
8345#define OP_Variable                            12
8346#define OP_Move                                13
8347#define OP_Copy                                14
8348#define OP_SCopy                               15
8349#define OP_ResultRow                           16
8350#define OP_Concat                              91   /* same as TK_CONCAT   */
8351#define OP_Add                                 86   /* same as TK_PLUS     */
8352#define OP_Subtract                            87   /* same as TK_MINUS    */
8353#define OP_Multiply                            88   /* same as TK_STAR     */
8354#define OP_Divide                              89   /* same as TK_SLASH    */
8355#define OP_Remainder                           90   /* same as TK_REM      */
8356#define OP_CollSeq                             17
8357#define OP_Function                            18
8358#define OP_BitAnd                              82   /* same as TK_BITAND   */
8359#define OP_BitOr                               83   /* same as TK_BITOR    */
8360#define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
8361#define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
8362#define OP_AddImm                              20
8363#define OP_MustBeInt                           21
8364#define OP_RealAffinity                        22
8365#define OP_ToText                             141   /* same as TK_TO_TEXT  */
8366#define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
8367#define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
8368#define OP_ToInt                              144   /* same as TK_TO_INT   */
8369#define OP_ToReal                             145   /* same as TK_TO_REAL  */
8370#define OP_Eq                                  76   /* same as TK_EQ       */
8371#define OP_Ne                                  75   /* same as TK_NE       */
8372#define OP_Lt                                  79   /* same as TK_LT       */
8373#define OP_Le                                  78   /* same as TK_LE       */
8374#define OP_Gt                                  77   /* same as TK_GT       */
8375#define OP_Ge                                  80   /* same as TK_GE       */
8376#define OP_Permutation                         23
8377#define OP_Compare                             24
8378#define OP_Jump                                25
8379#define OP_And                                 69   /* same as TK_AND      */
8380#define OP_Or                                  68   /* same as TK_OR       */
8381#define OP_Not                                 19   /* same as TK_NOT      */
8382#define OP_BitNot                              93   /* same as TK_BITNOT   */
8383#define OP_Once                                26
8384#define OP_If                                  27
8385#define OP_IfNot                               28
8386#define OP_IsNull                              73   /* same as TK_ISNULL   */
8387#define OP_NotNull                             74   /* same as TK_NOTNULL  */
8388#define OP_Column                              29
8389#define OP_Affinity                            30
8390#define OP_MakeRecord                          31
8391#define OP_Count                               32
8392#define OP_Savepoint                           33
8393#define OP_AutoCommit                          34
8394#define OP_Transaction                         35
8395#define OP_ReadCookie                          36
8396#define OP_SetCookie                           37
8397#define OP_VerifyCookie                        38
8398#define OP_OpenRead                            39
8399#define OP_OpenWrite                           40
8400#define OP_OpenAutoindex                       41
8401#define OP_OpenEphemeral                       42
8402#define OP_SorterOpen                          43
8403#define OP_OpenPseudo                          44
8404#define OP_Close                               45
8405#define OP_SeekLt                              46
8406#define OP_SeekLe                              47
8407#define OP_SeekGe                              48
8408#define OP_SeekGt                              49
8409#define OP_Seek                                50
8410#define OP_NotFound                            51
8411#define OP_Found                               52
8412#define OP_IsUnique                            53
8413#define OP_NotExists                           54
8414#define OP_Sequence                            55
8415#define OP_NewRowid                            56
8416#define OP_Insert                              57
8417#define OP_InsertInt                           58
8418#define OP_Delete                              59
8419#define OP_ResetCount                          60
8420#define OP_SorterCompare                       61
8421#define OP_SorterData                          62
8422#define OP_RowKey                              63
8423#define OP_RowData                             64
8424#define OP_Rowid                               65
8425#define OP_NullRow                             66
8426#define OP_Last                                67
8427#define OP_SorterSort                          70
8428#define OP_Sort                                71
8429#define OP_Rewind                              72
8430#define OP_SorterNext                          81
8431#define OP_Prev                                92
8432#define OP_Next                                95
8433#define OP_SorterInsert                        96
8434#define OP_IdxInsert                           97
8435#define OP_IdxDelete                           98
8436#define OP_IdxRowid                            99
8437#define OP_IdxLT                              100
8438#define OP_IdxGE                              101
8439#define OP_Destroy                            102
8440#define OP_Clear                              103
8441#define OP_CreateIndex                        104
8442#define OP_CreateTable                        105
8443#define OP_ParseSchema                        106
8444#define OP_LoadAnalysis                       107
8445#define OP_DropTable                          108
8446#define OP_DropIndex                          109
8447#define OP_DropTrigger                        110
8448#define OP_IntegrityCk                        111
8449#define OP_RowSetAdd                          112
8450#define OP_RowSetRead                         113
8451#define OP_RowSetTest                         114
8452#define OP_Program                            115
8453#define OP_Param                              116
8454#define OP_FkCounter                          117
8455#define OP_FkIfZero                           118
8456#define OP_MemMax                             119
8457#define OP_IfPos                              120
8458#define OP_IfNeg                              121
8459#define OP_IfZero                             122
8460#define OP_AggStep                            123
8461#define OP_AggFinal                           124
8462#define OP_Checkpoint                         125
8463#define OP_JournalMode                        126
8464#define OP_Vacuum                             127
8465#define OP_IncrVacuum                         128
8466#define OP_Expire                             129
8467#define OP_TableLock                          131
8468#define OP_VBegin                             132
8469#define OP_VCreate                            133
8470#define OP_VDestroy                           134
8471#define OP_VOpen                              135
8472#define OP_VFilter                            136
8473#define OP_VColumn                            137
8474#define OP_VNext                              138
8475#define OP_VRename                            139
8476#define OP_VUpdate                            140
8477#define OP_Pagecount                          146
8478#define OP_MaxPgcnt                           147
8479#define OP_Trace                              148
8480#define OP_Noop                               149
8481#define OP_Explain                            150
8482
8483
8484/* Properties such as "out2" or "jump" that are specified in
8485** comments following the "case" for each opcode in the vdbe.c
8486** are encoded into bitvectors as follows:
8487*/
8488#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
8489#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
8490#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
8491#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
8492#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
8493#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
8494#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
8495#define OPFLG_INITIALIZER {\
8496/*   0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8497/*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8498/*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8499/*  24 */ 0x00, 0x01, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
8500/*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
8501/*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
8502/*  48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
8503/*  56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8504/*  64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
8505/*  72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8506/*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8507/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
8508/*  96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
8509/* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8510/* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
8511/* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
8512/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8513/* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
8514/* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
8515
8516/************** End of opcodes.h *********************************************/
8517/************** Continuing where we left off in vdbe.h ***********************/
8518
8519/*
8520** Prototypes for the VDBE interface.  See comments on the implementation
8521** for a description of what each of these routines does.
8522*/
8523SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
8524SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
8525SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
8526SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
8527SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8528SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8529SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8530SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8531SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8532SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
8533SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
8534SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
8535SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8536SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8537SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
8538SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8539SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
8540SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
8541SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
8542SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
8543SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
8544SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
8545SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
8546SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
8547SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
8548SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
8549#ifdef SQLITE_DEBUG
8550SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
8551SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
8552#endif
8553SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
8554SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
8555SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
8556SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
8557SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
8558SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
8559SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
8560SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
8561SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
8562SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
8563SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
8564SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
8565#ifndef SQLITE_OMIT_TRACE
8566SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
8567#endif
8568
8569SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
8570SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
8571SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
8572
8573#ifndef SQLITE_OMIT_TRIGGER
8574SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
8575#endif
8576
8577
8578#ifndef NDEBUG
8579SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
8580# define VdbeComment(X)  sqlite3VdbeComment X
8581SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
8582# define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
8583#else
8584# define VdbeComment(X)
8585# define VdbeNoopComment(X)
8586#endif
8587
8588#endif
8589
8590/************** End of vdbe.h ************************************************/
8591/************** Continuing where we left off in sqliteInt.h ******************/
8592/************** Include pager.h in the middle of sqliteInt.h *****************/
8593/************** Begin file pager.h *******************************************/
8594/*
8595** 2001 September 15
8596**
8597** The author disclaims copyright to this source code.  In place of
8598** a legal notice, here is a blessing:
8599**
8600**    May you do good and not evil.
8601**    May you find forgiveness for yourself and forgive others.
8602**    May you share freely, never taking more than you give.
8603**
8604*************************************************************************
8605** This header file defines the interface that the sqlite page cache
8606** subsystem.  The page cache subsystem reads and writes a file a page
8607** at a time and provides a journal for rollback.
8608*/
8609
8610#ifndef _PAGER_H_
8611#define _PAGER_H_
8612
8613/*
8614** Default maximum size for persistent journal files. A negative
8615** value means no limit. This value may be overridden using the
8616** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
8617*/
8618#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
8619  #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
8620#endif
8621
8622/*
8623** The type used to represent a page number.  The first page in a file
8624** is called page 1.  0 is used to represent "not a page".
8625*/
8626typedef u32 Pgno;
8627
8628/*
8629** Each open file is managed by a separate instance of the "Pager" structure.
8630*/
8631typedef struct Pager Pager;
8632
8633/*
8634** Handle type for pages.
8635*/
8636typedef struct PgHdr DbPage;
8637
8638/*
8639** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
8640** reserved for working around a windows/posix incompatibility). It is
8641** used in the journal to signify that the remainder of the journal file
8642** is devoted to storing a master journal name - there are no more pages to
8643** roll back. See comments for function writeMasterJournal() in pager.c
8644** for details.
8645*/
8646#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
8647
8648/*
8649** Allowed values for the flags parameter to sqlite3PagerOpen().
8650**
8651** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8652*/
8653#define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
8654#define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
8655#define PAGER_MEMORY        0x0004    /* In-memory database */
8656
8657/*
8658** Valid values for the second argument to sqlite3PagerLockingMode().
8659*/
8660#define PAGER_LOCKINGMODE_QUERY      -1
8661#define PAGER_LOCKINGMODE_NORMAL      0
8662#define PAGER_LOCKINGMODE_EXCLUSIVE   1
8663
8664/*
8665** Numeric constants that encode the journalmode.
8666*/
8667#define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
8668#define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
8669#define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
8670#define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
8671#define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
8672#define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
8673#define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
8674
8675/*
8676** The remainder of this file contains the declarations of the functions
8677** that make up the Pager sub-system API. See source code comments for
8678** a detailed description of each routine.
8679*/
8680
8681/* Open and close a Pager connection. */
8682SQLITE_PRIVATE int sqlite3PagerOpen(
8683  sqlite3_vfs*,
8684  Pager **ppPager,
8685  const char*,
8686  int,
8687  int,
8688  int,
8689  void(*)(DbPage*)
8690);
8691SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
8692SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
8693
8694/* Functions used to configure a Pager object. */
8695SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8696SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
8697SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8698SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
8699SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
8700SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
8701SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
8702SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
8703SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
8704SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
8705SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
8706
8707/* Functions used to obtain and release page references. */
8708SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
8709#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
8710SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
8711SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
8712SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
8713
8714/* Operations on page references. */
8715SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
8716SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
8717SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
8718SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
8719SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
8720SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
8721
8722/* Functions used to manage pager transactions and savepoints. */
8723SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
8724SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
8725SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
8726SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
8727SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
8728SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
8729SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
8730SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8731SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8732SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8733
8734SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
8735SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8736SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8737SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8738SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8739
8740/* Functions used to query pager state and configuration. */
8741SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
8742SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
8743SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
8744SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
8745SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
8746SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8747SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8748SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8749SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8750SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8751
8752/* Functions used to truncate the database file. */
8753SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8754
8755#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
8756SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
8757#endif
8758
8759/* Functions to support testing and debugging. */
8760#if !defined(NDEBUG) || defined(SQLITE_TEST)
8761SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
8762SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
8763#endif
8764#ifdef SQLITE_TEST
8765SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
8766SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
8767  void disable_simulated_io_errors(void);
8768  void enable_simulated_io_errors(void);
8769#else
8770# define disable_simulated_io_errors()
8771# define enable_simulated_io_errors()
8772#endif
8773
8774#endif /* _PAGER_H_ */
8775
8776/************** End of pager.h ***********************************************/
8777/************** Continuing where we left off in sqliteInt.h ******************/
8778/************** Include pcache.h in the middle of sqliteInt.h ****************/
8779/************** Begin file pcache.h ******************************************/
8780/*
8781** 2008 August 05
8782**
8783** The author disclaims copyright to this source code.  In place of
8784** a legal notice, here is a blessing:
8785**
8786**    May you do good and not evil.
8787**    May you find forgiveness for yourself and forgive others.
8788**    May you share freely, never taking more than you give.
8789**
8790*************************************************************************
8791** This header file defines the interface that the sqlite page cache
8792** subsystem.
8793*/
8794
8795#ifndef _PCACHE_H_
8796
8797typedef struct PgHdr PgHdr;
8798typedef struct PCache PCache;
8799
8800/*
8801** Every page in the cache is controlled by an instance of the following
8802** structure.
8803*/
8804struct PgHdr {
8805  void *pData;                   /* Content of this page */
8806  void *pExtra;                  /* Extra content */
8807  PgHdr *pDirty;                 /* Transient list of dirty pages */
8808  Pgno pgno;                     /* Page number for this page */
8809  Pager *pPager;                 /* The pager this page is part of */
8810#ifdef SQLITE_CHECK_PAGES
8811  u32 pageHash;                  /* Hash of page content */
8812#endif
8813  u16 flags;                     /* PGHDR flags defined below */
8814
8815  /**********************************************************************
8816  ** Elements above are public.  All that follows is private to pcache.c
8817  ** and should not be accessed by other modules.
8818  */
8819  i16 nRef;                      /* Number of users of this page */
8820  PCache *pCache;                /* Cache that owns this page */
8821
8822  PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
8823  PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
8824};
8825
8826/* Bit values for PgHdr.flags */
8827#define PGHDR_DIRTY             0x002  /* Page has changed */
8828#define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
8829                                       ** writing this page to the database */
8830#define PGHDR_NEED_READ         0x008  /* Content is unread */
8831#define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
8832#define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
8833
8834/* Initialize and shutdown the page cache subsystem */
8835SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
8836SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
8837
8838/* Page cache buffer management:
8839** These routines implement SQLITE_CONFIG_PAGECACHE.
8840*/
8841SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
8842
8843/* Create a new pager cache.
8844** Under memory stress, invoke xStress to try to make pages clean.
8845** Only clean and unpinned pages can be reclaimed.
8846*/
8847SQLITE_PRIVATE void sqlite3PcacheOpen(
8848  int szPage,                    /* Size of every page */
8849  int szExtra,                   /* Extra space associated with each page */
8850  int bPurgeable,                /* True if pages are on backing store */
8851  int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
8852  void *pStress,                 /* Argument to xStress */
8853  PCache *pToInit                /* Preallocated space for the PCache */
8854);
8855
8856/* Modify the page-size after the cache has been created. */
8857SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
8858
8859/* Return the size in bytes of a PCache object.  Used to preallocate
8860** storage space.
8861*/
8862SQLITE_PRIVATE int sqlite3PcacheSize(void);
8863
8864/* One release per successful fetch.  Page is pinned until released.
8865** Reference counted.
8866*/
8867SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
8868SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
8869
8870SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
8871SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
8872SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
8873SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
8874
8875/* Change a page number.  Used by incr-vacuum. */
8876SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
8877
8878/* Remove all pages with pgno>x.  Reset the cache if x==0 */
8879SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
8880
8881/* Get a list of all dirty pages in the cache, sorted by page number */
8882SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
8883
8884/* Reset and close the cache object */
8885SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8886
8887/* Clear flags from pages of the page cache */
8888SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
8889
8890/* Discard the contents of the cache */
8891SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
8892
8893/* Return the total number of outstanding page references */
8894SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
8895
8896/* Increment the reference count of an existing page */
8897SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
8898
8899SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
8900
8901/* Return the total number of pages stored in the cache */
8902SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8903
8904#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
8905/* Iterate through all dirty pages currently stored in the cache. This
8906** interface is only available if SQLITE_CHECK_PAGES is defined when the
8907** library is built.
8908*/
8909SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
8910#endif
8911
8912/* Set and get the suggested cache-size for the specified pager-cache.
8913**
8914** If no global maximum is configured, then the system attempts to limit
8915** the total number of pages cached by purgeable pager-caches to the sum
8916** of the suggested cache-sizes.
8917*/
8918SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8919#ifdef SQLITE_TEST
8920SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8921#endif
8922
8923#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8924/* Try to return memory used by the pcache module to the main memory heap */
8925SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8926#endif
8927
8928#ifdef SQLITE_TEST
8929SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
8930#endif
8931
8932SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
8933
8934#endif /* _PCACHE_H_ */
8935
8936/************** End of pcache.h **********************************************/
8937/************** Continuing where we left off in sqliteInt.h ******************/
8938
8939/************** Include os.h in the middle of sqliteInt.h ********************/
8940/************** Begin file os.h **********************************************/
8941/*
8942** 2001 September 16
8943**
8944** The author disclaims copyright to this source code.  In place of
8945** a legal notice, here is a blessing:
8946**
8947**    May you do good and not evil.
8948**    May you find forgiveness for yourself and forgive others.
8949**    May you share freely, never taking more than you give.
8950**
8951******************************************************************************
8952**
8953** This header file (together with is companion C source-code file
8954** "os.c") attempt to abstract the underlying operating system so that
8955** the SQLite library will work on both POSIX and windows systems.
8956**
8957** This header file is #include-ed by sqliteInt.h and thus ends up
8958** being included by every source file.
8959*/
8960#ifndef _SQLITE_OS_H_
8961#define _SQLITE_OS_H_
8962
8963/*
8964** Figure out if we are dealing with Unix, Windows, or some other
8965** operating system.  After the following block of preprocess macros,
8966** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
8967** will defined to either 1 or 0.  One of the four will be 1.  The other
8968** three will be 0.
8969*/
8970#if defined(SQLITE_OS_OTHER)
8971# if SQLITE_OS_OTHER==1
8972#   undef SQLITE_OS_UNIX
8973#   define SQLITE_OS_UNIX 0
8974#   undef SQLITE_OS_WIN
8975#   define SQLITE_OS_WIN 0
8976#   undef SQLITE_OS_OS2
8977#   define SQLITE_OS_OS2 0
8978# else
8979#   undef SQLITE_OS_OTHER
8980# endif
8981#endif
8982#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
8983# define SQLITE_OS_OTHER 0
8984# ifndef SQLITE_OS_WIN
8985#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
8986#     define SQLITE_OS_WIN 1
8987#     define SQLITE_OS_UNIX 0
8988#     define SQLITE_OS_OS2 0
8989#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
8990#     define SQLITE_OS_WIN 0
8991#     define SQLITE_OS_UNIX 0
8992#     define SQLITE_OS_OS2 1
8993#   else
8994#     define SQLITE_OS_WIN 0
8995#     define SQLITE_OS_UNIX 1
8996#     define SQLITE_OS_OS2 0
8997#  endif
8998# else
8999#  define SQLITE_OS_UNIX 0
9000#  define SQLITE_OS_OS2 0
9001# endif
9002#else
9003# ifndef SQLITE_OS_WIN
9004#  define SQLITE_OS_WIN 0
9005# endif
9006#endif
9007
9008/*
9009** Determine if we are dealing with WindowsCE - which has a much
9010** reduced API.
9011*/
9012#if defined(_WIN32_WCE)
9013# define SQLITE_OS_WINCE 1
9014#else
9015# define SQLITE_OS_WINCE 0
9016#endif
9017
9018
9019/*
9020** Define the maximum size of a temporary filename
9021*/
9022#if SQLITE_OS_WIN
9023# include <windows.h>
9024# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
9025#elif SQLITE_OS_OS2
9026# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
9027#  include <os2safe.h> /* has to be included before os2.h for linking to work */
9028# endif
9029# define INCL_DOSDATETIME
9030# define INCL_DOSFILEMGR
9031# define INCL_DOSERRORS
9032# define INCL_DOSMISC
9033# define INCL_DOSPROCESS
9034# define INCL_DOSMODULEMGR
9035# define INCL_DOSSEMAPHORES
9036# include <os2.h>
9037# include <uconv.h>
9038# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
9039#else
9040# define SQLITE_TEMPNAME_SIZE 200
9041#endif
9042
9043/* If the SET_FULLSYNC macro is not defined above, then make it
9044** a no-op
9045*/
9046#ifndef SET_FULLSYNC
9047# define SET_FULLSYNC(x,y)
9048#endif
9049
9050/*
9051** The default size of a disk sector
9052*/
9053#ifndef SQLITE_DEFAULT_SECTOR_SIZE
9054# define SQLITE_DEFAULT_SECTOR_SIZE 512
9055#endif
9056
9057/*
9058** Temporary files are named starting with this prefix followed by 16 random
9059** alphanumeric characters, and no file extension. They are stored in the
9060** OS's standard temporary file directory, and are deleted prior to exit.
9061** If sqlite is being embedded in another program, you may wish to change the
9062** prefix to reflect your program's name, so that if your program exits
9063** prematurely, old temporary files can be easily identified. This can be done
9064** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
9065**
9066** 2006-10-31:  The default prefix used to be "sqlite_".  But then
9067** Mcafee started using SQLite in their anti-virus product and it
9068** started putting files with the "sqlite" name in the c:/temp folder.
9069** This annoyed many windows users.  Those users would then do a
9070** Google search for "sqlite", find the telephone numbers of the
9071** developers and call to wake them up at night and complain.
9072** For this reason, the default name prefix is changed to be "sqlite"
9073** spelled backwards.  So the temp files are still identified, but
9074** anybody smart enough to figure out the code is also likely smart
9075** enough to know that calling the developer will not help get rid
9076** of the file.
9077*/
9078#ifndef SQLITE_TEMP_FILE_PREFIX
9079# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
9080#endif
9081
9082/*
9083** The following values may be passed as the second argument to
9084** sqlite3OsLock(). The various locks exhibit the following semantics:
9085**
9086** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
9087** RESERVED:  A single process may hold a RESERVED lock on a file at
9088**            any time. Other processes may hold and obtain new SHARED locks.
9089** PENDING:   A single process may hold a PENDING lock on a file at
9090**            any one time. Existing SHARED locks may persist, but no new
9091**            SHARED locks may be obtained by other processes.
9092** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
9093**
9094** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
9095** process that requests an EXCLUSIVE lock may actually obtain a PENDING
9096** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
9097** sqlite3OsLock().
9098*/
9099#define NO_LOCK         0
9100#define SHARED_LOCK     1
9101#define RESERVED_LOCK   2
9102#define PENDING_LOCK    3
9103#define EXCLUSIVE_LOCK  4
9104
9105/*
9106** File Locking Notes:  (Mostly about windows but also some info for Unix)
9107**
9108** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
9109** those functions are not available.  So we use only LockFile() and
9110** UnlockFile().
9111**
9112** LockFile() prevents not just writing but also reading by other processes.
9113** A SHARED_LOCK is obtained by locking a single randomly-chosen
9114** byte out of a specific range of bytes. The lock byte is obtained at
9115** random so two separate readers can probably access the file at the
9116** same time, unless they are unlucky and choose the same lock byte.
9117** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
9118** There can only be one writer.  A RESERVED_LOCK is obtained by locking
9119** a single byte of the file that is designated as the reserved lock byte.
9120** A PENDING_LOCK is obtained by locking a designated byte different from
9121** the RESERVED_LOCK byte.
9122**
9123** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
9124** which means we can use reader/writer locks.  When reader/writer locks
9125** are used, the lock is placed on the same range of bytes that is used
9126** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
9127** will support two or more Win95 readers or two or more WinNT readers.
9128** But a single Win95 reader will lock out all WinNT readers and a single
9129** WinNT reader will lock out all other Win95 readers.
9130**
9131** The following #defines specify the range of bytes used for locking.
9132** SHARED_SIZE is the number of bytes available in the pool from which
9133** a random byte is selected for a shared lock.  The pool of bytes for
9134** shared locks begins at SHARED_FIRST.
9135**
9136** The same locking strategy and
9137** byte ranges are used for Unix.  This leaves open the possiblity of having
9138** clients on win95, winNT, and unix all talking to the same shared file
9139** and all locking correctly.  To do so would require that samba (or whatever
9140** tool is being used for file sharing) implements locks correctly between
9141** windows and unix.  I'm guessing that isn't likely to happen, but by
9142** using the same locking range we are at least open to the possibility.
9143**
9144** Locking in windows is manditory.  For this reason, we cannot store
9145** actual data in the bytes used for locking.  The pager never allocates
9146** the pages involved in locking therefore.  SHARED_SIZE is selected so
9147** that all locks will fit on a single page even at the minimum page size.
9148** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
9149** is set high so that we don't have to allocate an unused page except
9150** for very large databases.  But one should test the page skipping logic
9151** by setting PENDING_BYTE low and running the entire regression suite.
9152**
9153** Changing the value of PENDING_BYTE results in a subtly incompatible
9154** file format.  Depending on how it is changed, you might not notice
9155** the incompatibility right away, even running a full regression test.
9156** The default location of PENDING_BYTE is the first byte past the
9157** 1GB boundary.
9158**
9159*/
9160#ifdef SQLITE_OMIT_WSD
9161# define PENDING_BYTE     (0x40000000)
9162#else
9163# define PENDING_BYTE      sqlite3PendingByte
9164#endif
9165#define RESERVED_BYTE     (PENDING_BYTE+1)
9166#define SHARED_FIRST      (PENDING_BYTE+2)
9167#define SHARED_SIZE       510
9168
9169/*
9170** Wrapper around OS specific sqlite3_os_init() function.
9171*/
9172SQLITE_PRIVATE int sqlite3OsInit(void);
9173
9174/*
9175** Functions for accessing sqlite3_file methods
9176*/
9177SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
9178SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
9179SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
9180SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
9181SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
9182SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
9183SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
9184SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
9185SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
9186SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
9187#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
9188SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
9189SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9190SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9191SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9192SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9193SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9194
9195/*
9196** Functions for accessing sqlite3_vfs methods
9197*/
9198SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
9199SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
9200SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
9201SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
9202#ifndef SQLITE_OMIT_LOAD_EXTENSION
9203SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
9204SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
9205SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
9206SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
9207#endif /* SQLITE_OMIT_LOAD_EXTENSION */
9208SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
9209SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
9210SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
9211
9212/*
9213** Convenience functions for opening and closing files using
9214** sqlite3_malloc() to obtain space for the file-handle structure.
9215*/
9216SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
9217SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
9218
9219#endif /* _SQLITE_OS_H_ */
9220
9221/************** End of os.h **************************************************/
9222/************** Continuing where we left off in sqliteInt.h ******************/
9223/************** Include mutex.h in the middle of sqliteInt.h *****************/
9224/************** Begin file mutex.h *******************************************/
9225/*
9226** 2007 August 28
9227**
9228** The author disclaims copyright to this source code.  In place of
9229** a legal notice, here is a blessing:
9230**
9231**    May you do good and not evil.
9232**    May you find forgiveness for yourself and forgive others.
9233**    May you share freely, never taking more than you give.
9234**
9235*************************************************************************
9236**
9237** This file contains the common header for all mutex implementations.
9238** The sqliteInt.h header #includes this file so that it is available
9239** to all source files.  We break it out in an effort to keep the code
9240** better organized.
9241**
9242** NOTE:  source files should *not* #include this header file directly.
9243** Source files should #include the sqliteInt.h file and let that file
9244** include this one indirectly.
9245*/
9246
9247
9248/*
9249** Figure out what version of the code to use.  The choices are
9250**
9251**   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
9252**                             mutexes implemention cannot be overridden
9253**                             at start-time.
9254**
9255**   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
9256**                             mutual exclusion is provided.  But this
9257**                             implementation can be overridden at
9258**                             start-time.
9259**
9260**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
9261**
9262**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
9263**
9264**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
9265*/
9266#if !SQLITE_THREADSAFE
9267# define SQLITE_MUTEX_OMIT
9268#endif
9269#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
9270#  if SQLITE_OS_UNIX
9271#    define SQLITE_MUTEX_PTHREADS
9272#  elif SQLITE_OS_WIN
9273#    define SQLITE_MUTEX_W32
9274#  elif SQLITE_OS_OS2
9275#    define SQLITE_MUTEX_OS2
9276#  else
9277#    define SQLITE_MUTEX_NOOP
9278#  endif
9279#endif
9280
9281#ifdef SQLITE_MUTEX_OMIT
9282/*
9283** If this is a no-op implementation, implement everything as macros.
9284*/
9285#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
9286#define sqlite3_mutex_free(X)
9287#define sqlite3_mutex_enter(X)
9288#define sqlite3_mutex_try(X)      SQLITE_OK
9289#define sqlite3_mutex_leave(X)
9290#define sqlite3_mutex_held(X)     ((void)(X),1)
9291#define sqlite3_mutex_notheld(X)  ((void)(X),1)
9292#define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
9293#define sqlite3MutexInit()        SQLITE_OK
9294#define sqlite3MutexEnd()
9295#endif /* defined(SQLITE_MUTEX_OMIT) */
9296
9297/************** End of mutex.h ***********************************************/
9298/************** Continuing where we left off in sqliteInt.h ******************/
9299
9300
9301/*
9302** Each database file to be accessed by the system is an instance
9303** of the following structure.  There are normally two of these structures
9304** in the sqlite.aDb[] array.  aDb[0] is the main database file and
9305** aDb[1] is the database file used to hold temporary tables.  Additional
9306** databases may be attached.
9307*/
9308struct Db {
9309  char *zName;         /* Name of this database */
9310  Btree *pBt;          /* The B*Tree structure for this database file */
9311  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
9312  u8 safety_level;     /* How aggressive at syncing data to disk */
9313  Schema *pSchema;     /* Pointer to database schema (possibly shared) */
9314};
9315
9316/*
9317** An instance of the following structure stores a database schema.
9318**
9319** Most Schema objects are associated with a Btree.  The exception is
9320** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
9321** In shared cache mode, a single Schema object can be shared by multiple
9322** Btrees that refer to the same underlying BtShared object.
9323**
9324** Schema objects are automatically deallocated when the last Btree that
9325** references them is destroyed.   The TEMP Schema is manually freed by
9326** sqlite3_close().
9327*
9328** A thread must be holding a mutex on the corresponding Btree in order
9329** to access Schema content.  This implies that the thread must also be
9330** holding a mutex on the sqlite3 connection pointer that owns the Btree.
9331** For a TEMP Schema, only the connection mutex is required.
9332*/
9333struct Schema {
9334  int schema_cookie;   /* Database schema version number for this file */
9335  int iGeneration;     /* Generation counter.  Incremented with each change */
9336  Hash tblHash;        /* All tables indexed by name */
9337  Hash idxHash;        /* All (named) indices indexed by name */
9338  Hash trigHash;       /* All triggers indexed by name */
9339  Hash fkeyHash;       /* All foreign keys by referenced table name */
9340  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
9341  u8 file_format;      /* Schema format version for this file */
9342  u8 enc;              /* Text encoding used by this database */
9343  u16 flags;           /* Flags associated with this schema */
9344  int cache_size;      /* Number of pages to use in the cache */
9345};
9346
9347/*
9348** These macros can be used to test, set, or clear bits in the
9349** Db.pSchema->flags field.
9350*/
9351#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
9352#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
9353#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
9354#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
9355
9356/*
9357** Allowed values for the DB.pSchema->flags field.
9358**
9359** The DB_SchemaLoaded flag is set after the database schema has been
9360** read into internal hash tables.
9361**
9362** DB_UnresetViews means that one or more views have column names that
9363** have been filled out.  If the schema changes, these column names might
9364** changes and so the view will need to be reset.
9365*/
9366#define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
9367#define DB_UnresetViews    0x0002  /* Some views have defined column names */
9368#define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
9369
9370/*
9371** The number of different kinds of things that can be limited
9372** using the sqlite3_limit() interface.
9373*/
9374#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
9375
9376/*
9377** Lookaside malloc is a set of fixed-size buffers that can be used
9378** to satisfy small transient memory allocation requests for objects
9379** associated with a particular database connection.  The use of
9380** lookaside malloc provides a significant performance enhancement
9381** (approx 10%) by avoiding numerous malloc/free requests while parsing
9382** SQL statements.
9383**
9384** The Lookaside structure holds configuration information about the
9385** lookaside malloc subsystem.  Each available memory allocation in
9386** the lookaside subsystem is stored on a linked list of LookasideSlot
9387** objects.
9388**
9389** Lookaside allocations are only allowed for objects that are associated
9390** with a particular database connection.  Hence, schema information cannot
9391** be stored in lookaside because in shared cache mode the schema information
9392** is shared by multiple database connections.  Therefore, while parsing
9393** schema information, the Lookaside.bEnabled flag is cleared so that
9394** lookaside allocations are not used to construct the schema objects.
9395*/
9396struct Lookaside {
9397  u16 sz;                 /* Size of each buffer in bytes */
9398  u8 bEnabled;            /* False to disable new lookaside allocations */
9399  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
9400  int nOut;               /* Number of buffers currently checked out */
9401  int mxOut;              /* Highwater mark for nOut */
9402  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
9403  LookasideSlot *pFree;   /* List of available buffers */
9404  void *pStart;           /* First byte of available memory space */
9405  void *pEnd;             /* First byte past end of available space */
9406};
9407struct LookasideSlot {
9408  LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
9409};
9410
9411/*
9412** A hash table for function definitions.
9413**
9414** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
9415** Collisions are on the FuncDef.pHash chain.
9416*/
9417struct FuncDefHash {
9418  FuncDef *a[23];       /* Hash table for functions */
9419};
9420
9421/*
9422** Each database connection is an instance of the following structure.
9423**
9424** The sqlite.lastRowid records the last insert rowid generated by an
9425** insert statement.  Inserts on views do not affect its value.  Each
9426** trigger has its own context, so that lastRowid can be updated inside
9427** triggers as usual.  The previous value will be restored once the trigger
9428** exits.  Upon entering a before or instead of trigger, lastRowid is no
9429** longer (since after version 2.8.12) reset to -1.
9430**
9431** The sqlite.nChange does not count changes within triggers and keeps no
9432** context.  It is reset at start of sqlite3_exec.
9433** The sqlite.lsChange represents the number of changes made by the last
9434** insert, update, or delete statement.  It remains constant throughout the
9435** length of a statement and is then updated by OP_SetCounts.  It keeps a
9436** context stack just like lastRowid so that the count of changes
9437** within a trigger is not seen outside the trigger.  Changes to views do not
9438** affect the value of lsChange.
9439** The sqlite.csChange keeps track of the number of current changes (since
9440** the last statement) and is used to update sqlite_lsChange.
9441**
9442** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
9443** store the most recent error code and, if applicable, string. The
9444** internal function sqlite3Error() is used to set these variables
9445** consistently.
9446*/
9447struct sqlite3 {
9448  sqlite3_vfs *pVfs;            /* OS Interface */
9449  int nDb;                      /* Number of backends currently in use */
9450  Db *aDb;                      /* All backends */
9451  int flags;                    /* Miscellaneous flags. See below */
9452  unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
9453  int errCode;                  /* Most recent error code (SQLITE_*) */
9454  int errMask;                  /* & result codes with this before returning */
9455  u8 autoCommit;                /* The auto-commit flag. */
9456  u8 temp_store;                /* 1: file 2: memory 0: default */
9457  u8 mallocFailed;              /* True if we have seen a malloc failure */
9458  u8 dfltLockMode;              /* Default locking-mode for attached dbs */
9459  signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
9460  u8 suppressErr;               /* Do not issue error messages if true */
9461  u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
9462  int nextPagesize;             /* Pagesize after VACUUM if >0 */
9463  int nTable;                   /* Number of tables in the database */
9464  CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
9465  i64 lastRowid;                /* ROWID of most recent insert (see above) */
9466  u32 magic;                    /* Magic number for detect library misuse */
9467  int nChange;                  /* Value returned by sqlite3_changes() */
9468  int nTotalChange;             /* Value returned by sqlite3_total_changes() */
9469  sqlite3_mutex *mutex;         /* Connection mutex */
9470  int aLimit[SQLITE_N_LIMIT];   /* Limits */
9471  struct sqlite3InitInfo {      /* Information used during initialization */
9472    int iDb;                    /* When back is being initialized */
9473    int newTnum;                /* Rootpage of table being initialized */
9474    u8 busy;                    /* TRUE if currently initializing */
9475    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
9476  } init;
9477  int nExtension;               /* Number of loaded extensions */
9478  void **aExtension;            /* Array of shared library handles */
9479  struct Vdbe *pVdbe;           /* List of active virtual machines */
9480  int activeVdbeCnt;            /* Number of VDBEs currently executing */
9481  int writeVdbeCnt;             /* Number of active VDBEs that are writing */
9482  int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
9483  void (*xTrace)(void*,const char*);        /* Trace function */
9484  void *pTraceArg;                          /* Argument to the trace function */
9485  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
9486  void *pProfileArg;                        /* Argument to profile function */
9487  void *pCommitArg;                 /* Argument to xCommitCallback() */
9488  int (*xCommitCallback)(void*);    /* Invoked at every commit. */
9489  void *pRollbackArg;               /* Argument to xRollbackCallback() */
9490  void (*xRollbackCallback)(void*); /* Invoked at every commit. */
9491  void *pUpdateArg;
9492  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
9493#ifndef SQLITE_OMIT_WAL
9494  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
9495  void *pWalArg;
9496#endif
9497  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
9498  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
9499  void *pCollNeededArg;
9500  sqlite3_value *pErr;          /* Most recent error message */
9501  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
9502  char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
9503  union {
9504    volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
9505    double notUsed1;            /* Spacer */
9506  } u1;
9507  Lookaside lookaside;          /* Lookaside malloc configuration */
9508#ifndef SQLITE_OMIT_AUTHORIZATION
9509  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
9510                                /* Access authorization function */
9511  void *pAuthArg;               /* 1st argument to the access auth function */
9512#endif
9513#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9514  int (*xProgress)(void *);     /* The progress callback */
9515  void *pProgressArg;           /* Argument to the progress callback */
9516  int nProgressOps;             /* Number of opcodes for progress callback */
9517#endif
9518#ifndef SQLITE_OMIT_VIRTUALTABLE
9519  Hash aModule;                 /* populated by sqlite3_create_module() */
9520  VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
9521  VTable **aVTrans;             /* Virtual tables with open transactions */
9522  int nVTrans;                  /* Allocated size of aVTrans */
9523  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
9524#endif
9525  FuncDefHash aFunc;            /* Hash table of connection functions */
9526  Hash aCollSeq;                /* All collating sequences */
9527  BusyHandler busyHandler;      /* Busy callback */
9528  int busyTimeout;              /* Busy handler timeout, in msec */
9529  Db aDbStatic[2];              /* Static space for the 2 default backends */
9530  Savepoint *pSavepoint;        /* List of active savepoints */
9531  int nSavepoint;               /* Number of non-transaction savepoints */
9532  int nStatement;               /* Number of nested statement-transactions  */
9533  u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
9534  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
9535  int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
9536
9537#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
9538  /* The following variables are all protected by the STATIC_MASTER
9539  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
9540  **
9541  ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
9542  ** unlock so that it can proceed.
9543  **
9544  ** When X.pBlockingConnection==Y, that means that something that X tried
9545  ** tried to do recently failed with an SQLITE_LOCKED error due to locks
9546  ** held by Y.
9547  */
9548  sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
9549  sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
9550  void *pUnlockArg;                     /* Argument to xUnlockNotify */
9551  void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
9552  sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
9553#endif
9554};
9555
9556/*
9557** A macro to discover the encoding of a database.
9558*/
9559#define ENC(db) ((db)->aDb[0].pSchema->enc)
9560
9561/*
9562** Possible values for the sqlite3.flags.
9563*/
9564#define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
9565#define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
9566#define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
9567#define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
9568#define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
9569                                          /*   DELETE, or UPDATE and return */
9570                                          /*   the count using a callback. */
9571#define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
9572                                          /*   result set is empty */
9573#define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
9574#define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
9575#define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
9576#define SQLITE_NoReadlock     0x00020000  /* Readlocks are omitted when
9577                                          ** accessing read-only databases */
9578#define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
9579#define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
9580#define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
9581#define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
9582#define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
9583#define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
9584#define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
9585#define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
9586#define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
9587#define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
9588#define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
9589#define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
9590#define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
9591
9592/*
9593** Bits of the sqlite3.flags field that are used by the
9594** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
9595** These must be the low-order bits of the flags field.
9596*/
9597#define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
9598#define SQLITE_ColumnCache    0x02        /* Disable the column cache */
9599#define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
9600#define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
9601#define SQLITE_IndexCover     0x10        /* Disable index covering table */
9602#define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
9603#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
9604#define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
9605#define SQLITE_DistinctOpt    0x80        /* DISTINCT using indexes */
9606#define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
9607
9608/*
9609** Possible values for the sqlite.magic field.
9610** The numbers are obtained at random and have no special meaning, other
9611** than being distinct from one another.
9612*/
9613#define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
9614#define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
9615#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
9616#define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
9617#define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
9618
9619/*
9620** Each SQL function is defined by an instance of the following
9621** structure.  A pointer to this structure is stored in the sqlite.aFunc
9622** hash table.  When multiple functions have the same name, the hash table
9623** points to a linked list of these structures.
9624*/
9625struct FuncDef {
9626  i16 nArg;            /* Number of arguments.  -1 means unlimited */
9627  u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
9628  u8 flags;            /* Some combination of SQLITE_FUNC_* */
9629  void *pUserData;     /* User data parameter */
9630  FuncDef *pNext;      /* Next function with same name */
9631  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
9632  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
9633  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
9634  char *zName;         /* SQL name of the function. */
9635  FuncDef *pHash;      /* Next with a different name but the same hash */
9636  FuncDestructor *pDestructor;   /* Reference counted destructor function */
9637};
9638
9639/*
9640** This structure encapsulates a user-function destructor callback (as
9641** configured using create_function_v2()) and a reference counter. When
9642** create_function_v2() is called to create a function with a destructor,
9643** a single object of this type is allocated. FuncDestructor.nRef is set to
9644** the number of FuncDef objects created (either 1 or 3, depending on whether
9645** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
9646** member of each of the new FuncDef objects is set to point to the allocated
9647** FuncDestructor.
9648**
9649** Thereafter, when one of the FuncDef objects is deleted, the reference
9650** count on this object is decremented. When it reaches 0, the destructor
9651** is invoked and the FuncDestructor structure freed.
9652*/
9653struct FuncDestructor {
9654  int nRef;
9655  void (*xDestroy)(void *);
9656  void *pUserData;
9657};
9658
9659/*
9660** Possible values for FuncDef.flags
9661*/
9662#define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
9663#define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
9664#define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
9665#define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
9666#define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
9667#define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
9668#define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
9669
9670/*
9671** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
9672** used to create the initializers for the FuncDef structures.
9673**
9674**   FUNCTION(zName, nArg, iArg, bNC, xFunc)
9675**     Used to create a scalar function definition of a function zName
9676**     implemented by C function xFunc that accepts nArg arguments. The
9677**     value passed as iArg is cast to a (void*) and made available
9678**     as the user-data (sqlite3_user_data()) for the function. If
9679**     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
9680**
9681**   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
9682**     Used to create an aggregate function definition implemented by
9683**     the C functions xStep and xFinal. The first four parameters
9684**     are interpreted in the same way as the first 4 parameters to
9685**     FUNCTION().
9686**
9687**   LIKEFUNC(zName, nArg, pArg, flags)
9688**     Used to create a scalar function definition of a function zName
9689**     that accepts nArg arguments and is implemented by a call to C
9690**     function likeFunc. Argument pArg is cast to a (void *) and made
9691**     available as the function user-data (sqlite3_user_data()). The
9692**     FuncDef.flags variable is set to the value passed as the flags
9693**     parameter.
9694*/
9695#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9696  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9697   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
9698#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9699  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
9700   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
9701#define LIKEFUNC(zName, nArg, arg, flags) \
9702  {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
9703#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9704  {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
9705   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
9706
9707/*
9708** All current savepoints are stored in a linked list starting at
9709** sqlite3.pSavepoint. The first element in the list is the most recently
9710** opened savepoint. Savepoints are added to the list by the vdbe
9711** OP_Savepoint instruction.
9712*/
9713struct Savepoint {
9714  char *zName;                        /* Savepoint name (nul-terminated) */
9715  i64 nDeferredCons;                  /* Number of deferred fk violations */
9716  Savepoint *pNext;                   /* Parent savepoint (if any) */
9717};
9718
9719/*
9720** The following are used as the second parameter to sqlite3Savepoint(),
9721** and as the P1 argument to the OP_Savepoint instruction.
9722*/
9723#define SAVEPOINT_BEGIN      0
9724#define SAVEPOINT_RELEASE    1
9725#define SAVEPOINT_ROLLBACK   2
9726
9727
9728/*
9729** Each SQLite module (virtual table definition) is defined by an
9730** instance of the following structure, stored in the sqlite3.aModule
9731** hash table.
9732*/
9733struct Module {
9734  const sqlite3_module *pModule;       /* Callback pointers */
9735  const char *zName;                   /* Name passed to create_module() */
9736  void *pAux;                          /* pAux passed to create_module() */
9737  void (*xDestroy)(void *);            /* Module destructor function */
9738};
9739
9740/*
9741** information about each column of an SQL table is held in an instance
9742** of this structure.
9743*/
9744struct Column {
9745  char *zName;     /* Name of this column */
9746  Expr *pDflt;     /* Default value of this column */
9747  char *zDflt;     /* Original text of the default value */
9748  char *zType;     /* Data type for this column */
9749  char *zColl;     /* Collating sequence.  If NULL, use the default */
9750  u8 notNull;      /* True if there is a NOT NULL constraint */
9751  u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
9752  char affinity;   /* One of the SQLITE_AFF_... values */
9753#ifndef SQLITE_OMIT_VIRTUALTABLE
9754  u8 isHidden;     /* True if this column is 'hidden' */
9755#endif
9756};
9757
9758/*
9759** A "Collating Sequence" is defined by an instance of the following
9760** structure. Conceptually, a collating sequence consists of a name and
9761** a comparison routine that defines the order of that sequence.
9762**
9763** There may two separate implementations of the collation function, one
9764** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
9765** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
9766** native byte order. When a collation sequence is invoked, SQLite selects
9767** the version that will require the least expensive encoding
9768** translations, if any.
9769**
9770** The CollSeq.pUser member variable is an extra parameter that passed in
9771** as the first argument to the UTF-8 comparison function, xCmp.
9772** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
9773** xCmp16.
9774**
9775** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
9776** collating sequence is undefined.  Indices built on an undefined
9777** collating sequence may not be read or written.
9778*/
9779struct CollSeq {
9780  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
9781  u8 enc;               /* Text encoding handled by xCmp() */
9782  u8 type;              /* One of the SQLITE_COLL_... values below */
9783  void *pUser;          /* First argument to xCmp() */
9784  int (*xCmp)(void*,int, const void*, int, const void*);
9785  void (*xDel)(void*);  /* Destructor for pUser */
9786};
9787
9788/*
9789** Allowed values of CollSeq.type:
9790*/
9791#define SQLITE_COLL_BINARY  1  /* The default memcmp() collating sequence */
9792#define SQLITE_COLL_NOCASE  2  /* The built-in NOCASE collating sequence */
9793#define SQLITE_COLL_REVERSE 3  /* The built-in REVERSE collating sequence */
9794#define SQLITE_COLL_USER    0  /* Any other user-defined collating sequence */
9795
9796/*
9797** A sort order can be either ASC or DESC.
9798*/
9799#define SQLITE_SO_ASC       0  /* Sort in ascending order */
9800#define SQLITE_SO_DESC      1  /* Sort in ascending order */
9801
9802/*
9803** Column affinity types.
9804**
9805** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
9806** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
9807** the speed a little by numbering the values consecutively.
9808**
9809** But rather than start with 0 or 1, we begin with 'a'.  That way,
9810** when multiple affinity types are concatenated into a string and
9811** used as the P4 operand, they will be more readable.
9812**
9813** Note also that the numeric types are grouped together so that testing
9814** for a numeric type is a single comparison.
9815*/
9816#define SQLITE_AFF_TEXT     'a'
9817#define SQLITE_AFF_NONE     'b'
9818#define SQLITE_AFF_NUMERIC  'c'
9819#define SQLITE_AFF_INTEGER  'd'
9820#define SQLITE_AFF_REAL     'e'
9821
9822#define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
9823
9824/*
9825** The SQLITE_AFF_MASK values masks off the significant bits of an
9826** affinity value.
9827*/
9828#define SQLITE_AFF_MASK     0x67
9829
9830/*
9831** Additional bit values that can be ORed with an affinity without
9832** changing the affinity.
9833*/
9834#define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
9835#define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
9836#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
9837
9838/*
9839** An object of this type is created for each virtual table present in
9840** the database schema.
9841**
9842** If the database schema is shared, then there is one instance of this
9843** structure for each database connection (sqlite3*) that uses the shared
9844** schema. This is because each database connection requires its own unique
9845** instance of the sqlite3_vtab* handle used to access the virtual table
9846** implementation. sqlite3_vtab* handles can not be shared between
9847** database connections, even when the rest of the in-memory database
9848** schema is shared, as the implementation often stores the database
9849** connection handle passed to it via the xConnect() or xCreate() method
9850** during initialization internally. This database connection handle may
9851** then be used by the virtual table implementation to access real tables
9852** within the database. So that they appear as part of the callers
9853** transaction, these accesses need to be made via the same database
9854** connection as that used to execute SQL operations on the virtual table.
9855**
9856** All VTable objects that correspond to a single table in a shared
9857** database schema are initially stored in a linked-list pointed to by
9858** the Table.pVTable member variable of the corresponding Table object.
9859** When an sqlite3_prepare() operation is required to access the virtual
9860** table, it searches the list for the VTable that corresponds to the
9861** database connection doing the preparing so as to use the correct
9862** sqlite3_vtab* handle in the compiled query.
9863**
9864** When an in-memory Table object is deleted (for example when the
9865** schema is being reloaded for some reason), the VTable objects are not
9866** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
9867** immediately. Instead, they are moved from the Table.pVTable list to
9868** another linked list headed by the sqlite3.pDisconnect member of the
9869** corresponding sqlite3 structure. They are then deleted/xDisconnected
9870** next time a statement is prepared using said sqlite3*. This is done
9871** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
9872** Refer to comments above function sqlite3VtabUnlockList() for an
9873** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
9874** list without holding the corresponding sqlite3.mutex mutex.
9875**
9876** The memory for objects of this type is always allocated by
9877** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
9878** the first argument.
9879*/
9880struct VTable {
9881  sqlite3 *db;              /* Database connection associated with this table */
9882  Module *pMod;             /* Pointer to module implementation */
9883  sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
9884  int nRef;                 /* Number of pointers to this structure */
9885  u8 bConstraint;           /* True if constraints are supported */
9886  int iSavepoint;           /* Depth of the SAVEPOINT stack */
9887  VTable *pNext;            /* Next in linked list (see above) */
9888};
9889
9890/*
9891** Each SQL table is represented in memory by an instance of the
9892** following structure.
9893**
9894** Table.zName is the name of the table.  The case of the original
9895** CREATE TABLE statement is stored, but case is not significant for
9896** comparisons.
9897**
9898** Table.nCol is the number of columns in this table.  Table.aCol is a
9899** pointer to an array of Column structures, one for each column.
9900**
9901** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
9902** the column that is that key.   Otherwise Table.iPKey is negative.  Note
9903** that the datatype of the PRIMARY KEY must be INTEGER for this field to
9904** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
9905** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
9906** is generated for each row of the table.  TF_HasPrimaryKey is set if
9907** the table has any PRIMARY KEY, INTEGER or otherwise.
9908**
9909** Table.tnum is the page number for the root BTree page of the table in the
9910** database file.  If Table.iDb is the index of the database table backend
9911** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
9912** holds temporary tables and indices.  If TF_Ephemeral is set
9913** then the table is stored in a file that is automatically deleted
9914** when the VDBE cursor to the table is closed.  In this case Table.tnum
9915** refers VDBE cursor number that holds the table open, not to the root
9916** page number.  Transient tables are used to hold the results of a
9917** sub-query that appears instead of a real table name in the FROM clause
9918** of a SELECT statement.
9919*/
9920struct Table {
9921  char *zName;         /* Name of the table or view */
9922  int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
9923  int nCol;            /* Number of columns in this table */
9924  Column *aCol;        /* Information about each column */
9925  Index *pIndex;       /* List of SQL indexes on this table. */
9926  int tnum;            /* Root BTree node for this table (see note above) */
9927  unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
9928  Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
9929  u16 nRef;            /* Number of pointers to this Table */
9930  u8 tabFlags;         /* Mask of TF_* values */
9931  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
9932  FKey *pFKey;         /* Linked list of all foreign keys in this table */
9933  char *zColAff;       /* String defining the affinity of each column */
9934#ifndef SQLITE_OMIT_CHECK
9935  Expr *pCheck;        /* The AND of all CHECK constraints */
9936#endif
9937#ifndef SQLITE_OMIT_ALTERTABLE
9938  int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
9939#endif
9940#ifndef SQLITE_OMIT_VIRTUALTABLE
9941  VTable *pVTable;     /* List of VTable objects. */
9942  int nModuleArg;      /* Number of arguments to the module */
9943  char **azModuleArg;  /* Text of all module args. [0] is module name */
9944#endif
9945  Trigger *pTrigger;   /* List of triggers stored in pSchema */
9946  Schema *pSchema;     /* Schema that contains this table */
9947  Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
9948};
9949
9950/*
9951** Allowed values for Tabe.tabFlags.
9952*/
9953#define TF_Readonly        0x01    /* Read-only system table */
9954#define TF_Ephemeral       0x02    /* An ephemeral table */
9955#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
9956#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
9957#define TF_Virtual         0x10    /* Is a virtual table */
9958#define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
9959
9960
9961
9962/*
9963** Test to see whether or not a table is a virtual table.  This is
9964** done as a macro so that it will be optimized out when virtual
9965** table support is omitted from the build.
9966*/
9967#ifndef SQLITE_OMIT_VIRTUALTABLE
9968#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
9969#  define IsHiddenColumn(X) ((X)->isHidden)
9970#else
9971#  define IsVirtual(X)      0
9972#  define IsHiddenColumn(X) 0
9973#endif
9974
9975/*
9976** Each foreign key constraint is an instance of the following structure.
9977**
9978** A foreign key is associated with two tables.  The "from" table is
9979** the table that contains the REFERENCES clause that creates the foreign
9980** key.  The "to" table is the table that is named in the REFERENCES clause.
9981** Consider this example:
9982**
9983**     CREATE TABLE ex1(
9984**       a INTEGER PRIMARY KEY,
9985**       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
9986**     );
9987**
9988** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
9989**
9990** Each REFERENCES clause generates an instance of the following structure
9991** which is attached to the from-table.  The to-table need not exist when
9992** the from-table is created.  The existence of the to-table is not checked.
9993*/
9994struct FKey {
9995  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
9996  FKey *pNextFrom;  /* Next foreign key in pFrom */
9997  char *zTo;        /* Name of table that the key points to (aka: Parent) */
9998  FKey *pNextTo;    /* Next foreign key on table named zTo */
9999  FKey *pPrevTo;    /* Previous foreign key on table named zTo */
10000  int nCol;         /* Number of columns in this key */
10001  /* EV: R-30323-21917 */
10002  u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
10003  u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
10004  Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
10005  struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
10006    int iFrom;         /* Index of column in pFrom */
10007    char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
10008  } aCol[1];        /* One entry for each of nCol column s */
10009};
10010
10011/*
10012** SQLite supports many different ways to resolve a constraint
10013** error.  ROLLBACK processing means that a constraint violation
10014** causes the operation in process to fail and for the current transaction
10015** to be rolled back.  ABORT processing means the operation in process
10016** fails and any prior changes from that one operation are backed out,
10017** but the transaction is not rolled back.  FAIL processing means that
10018** the operation in progress stops and returns an error code.  But prior
10019** changes due to the same operation are not backed out and no rollback
10020** occurs.  IGNORE means that the particular row that caused the constraint
10021** error is not inserted or updated.  Processing continues and no error
10022** is returned.  REPLACE means that preexisting database rows that caused
10023** a UNIQUE constraint violation are removed so that the new insert or
10024** update can proceed.  Processing continues and no error is reported.
10025**
10026** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
10027** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
10028** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
10029** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
10030** referenced table row is propagated into the row that holds the
10031** foreign key.
10032**
10033** The following symbolic values are used to record which type
10034** of action to take.
10035*/
10036#define OE_None     0   /* There is no constraint to check */
10037#define OE_Rollback 1   /* Fail the operation and rollback the transaction */
10038#define OE_Abort    2   /* Back out changes but do no rollback transaction */
10039#define OE_Fail     3   /* Stop the operation but leave all prior changes */
10040#define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
10041#define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
10042
10043#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10044#define OE_SetNull  7   /* Set the foreign key value to NULL */
10045#define OE_SetDflt  8   /* Set the foreign key value to its default */
10046#define OE_Cascade  9   /* Cascade the changes */
10047
10048#define OE_Default  99  /* Do whatever the default action is */
10049
10050
10051/*
10052** An instance of the following structure is passed as the first
10053** argument to sqlite3VdbeKeyCompare and is used to control the
10054** comparison of the two index keys.
10055*/
10056struct KeyInfo {
10057  sqlite3 *db;        /* The database connection */
10058  u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
10059  u16 nField;         /* Number of entries in aColl[] */
10060  u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
10061  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
10062};
10063
10064/*
10065** An instance of the following structure holds information about a
10066** single index record that has already been parsed out into individual
10067** values.
10068**
10069** A record is an object that contains one or more fields of data.
10070** Records are used to store the content of a table row and to store
10071** the key of an index.  A blob encoding of a record is created by
10072** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10073** OP_Column opcode.
10074**
10075** This structure holds a record that has already been disassembled
10076** into its constituent fields.
10077*/
10078struct UnpackedRecord {
10079  KeyInfo *pKeyInfo;  /* Collation and sort-order information */
10080  u16 nField;         /* Number of entries in apMem[] */
10081  u16 flags;          /* Boolean settings.  UNPACKED_... below */
10082  i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
10083  Mem *aMem;          /* Values */
10084};
10085
10086/*
10087** Allowed values of UnpackedRecord.flags
10088*/
10089#define UNPACKED_NEED_FREE     0x0001  /* Memory is from sqlite3Malloc() */
10090#define UNPACKED_NEED_DESTROY  0x0002  /* apMem[]s should all be destroyed */
10091#define UNPACKED_IGNORE_ROWID  0x0004  /* Ignore trailing rowid on key1 */
10092#define UNPACKED_INCRKEY       0x0008  /* Make this key an epsilon larger */
10093#define UNPACKED_PREFIX_MATCH  0x0010  /* A prefix match is considered OK */
10094#define UNPACKED_PREFIX_SEARCH 0x0020  /* A prefix match is considered OK */
10095
10096/*
10097** Each SQL index is represented in memory by an
10098** instance of the following structure.
10099**
10100** The columns of the table that are to be indexed are described
10101** by the aiColumn[] field of this structure.  For example, suppose
10102** we have the following table and index:
10103**
10104**     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
10105**     CREATE INDEX Ex2 ON Ex1(c3,c1);
10106**
10107** In the Table structure describing Ex1, nCol==3 because there are
10108** three columns in the table.  In the Index structure describing
10109** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
10110** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
10111** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
10112** The second column to be indexed (c1) has an index of 0 in
10113** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
10114**
10115** The Index.onError field determines whether or not the indexed columns
10116** must be unique and what to do if they are not.  When Index.onError=OE_None,
10117** it means this is not a unique index.  Otherwise it is a unique index
10118** and the value of Index.onError indicate the which conflict resolution
10119** algorithm to employ whenever an attempt is made to insert a non-unique
10120** element.
10121*/
10122struct Index {
10123  char *zName;     /* Name of this index */
10124  int nColumn;     /* Number of columns in the table used by this index */
10125  int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
10126  unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10127  Table *pTable;   /* The SQL table being indexed */
10128  int tnum;        /* Page containing root of this index in database file */
10129  u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10130  u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
10131  u8 bUnordered;   /* Use this index for == or IN queries only */
10132  char *zColAff;   /* String defining the affinity of each column */
10133  Index *pNext;    /* The next index associated with the same table */
10134  Schema *pSchema; /* Schema containing this index */
10135  u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
10136  char **azColl;   /* Array of collation sequence names for index */
10137  IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
10138};
10139
10140/*
10141** Each sample stored in the sqlite_stat2 table is represented in memory
10142** using a structure of this type.
10143*/
10144struct IndexSample {
10145  union {
10146    char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10147    double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
10148  } u;
10149  u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10150  u8 nByte;         /* Size in byte of text or blob. */
10151};
10152
10153/*
10154** Each token coming out of the lexer is an instance of
10155** this structure.  Tokens are also used as part of an expression.
10156**
10157** Note if Token.z==0 then Token.dyn and Token.n are undefined and
10158** may contain random values.  Do not make any assumptions about Token.dyn
10159** and Token.n when Token.z==0.
10160*/
10161struct Token {
10162  const char *z;     /* Text of the token.  Not NULL-terminated! */
10163  unsigned int n;    /* Number of characters in this token */
10164};
10165
10166/*
10167** An instance of this structure contains information needed to generate
10168** code for a SELECT that contains aggregate functions.
10169**
10170** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
10171** pointer to this structure.  The Expr.iColumn field is the index in
10172** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
10173** code for that node.
10174**
10175** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
10176** original Select structure that describes the SELECT statement.  These
10177** fields do not need to be freed when deallocating the AggInfo structure.
10178*/
10179struct AggInfo {
10180  u8 directMode;          /* Direct rendering mode means take data directly
10181                          ** from source tables rather than from accumulators */
10182  u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
10183                          ** than the source table */
10184  int sortingIdx;         /* Cursor number of the sorting index */
10185  int sortingIdxPTab;     /* Cursor number of pseudo-table */
10186  ExprList *pGroupBy;     /* The group by clause */
10187  int nSortingColumn;     /* Number of columns in the sorting index */
10188  struct AggInfo_col {    /* For each column used in source tables */
10189    Table *pTab;             /* Source table */
10190    int iTable;              /* Cursor number of the source table */
10191    int iColumn;             /* Column number within the source table */
10192    int iSorterColumn;       /* Column number in the sorting index */
10193    int iMem;                /* Memory location that acts as accumulator */
10194    Expr *pExpr;             /* The original expression */
10195  } *aCol;
10196  int nColumn;            /* Number of used entries in aCol[] */
10197  int nColumnAlloc;       /* Number of slots allocated for aCol[] */
10198  int nAccumulator;       /* Number of columns that show through to the output.
10199                          ** Additional columns are used only as parameters to
10200                          ** aggregate functions */
10201  struct AggInfo_func {   /* For each aggregate function */
10202    Expr *pExpr;             /* Expression encoding the function */
10203    FuncDef *pFunc;          /* The aggregate function implementation */
10204    int iMem;                /* Memory location that acts as accumulator */
10205    int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
10206  } *aFunc;
10207  int nFunc;              /* Number of entries in aFunc[] */
10208  int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
10209};
10210
10211/*
10212** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
10213** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
10214** than 32767 we have to make it 32-bit.  16-bit is preferred because
10215** it uses less memory in the Expr object, which is a big memory user
10216** in systems with lots of prepared statements.  And few applications
10217** need more than about 10 or 20 variables.  But some extreme users want
10218** to have prepared statements with over 32767 variables, and for them
10219** the option is available (at compile-time).
10220*/
10221#if SQLITE_MAX_VARIABLE_NUMBER<=32767
10222typedef i16 ynVar;
10223#else
10224typedef int ynVar;
10225#endif
10226
10227/*
10228** Each node of an expression in the parse tree is an instance
10229** of this structure.
10230**
10231** Expr.op is the opcode. The integer parser token codes are reused
10232** as opcodes here. For example, the parser defines TK_GE to be an integer
10233** code representing the ">=" operator. This same integer code is reused
10234** to represent the greater-than-or-equal-to operator in the expression
10235** tree.
10236**
10237** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
10238** or TK_STRING), then Expr.token contains the text of the SQL literal. If
10239** the expression is a variable (TK_VARIABLE), then Expr.token contains the
10240** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
10241** then Expr.token contains the name of the function.
10242**
10243** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
10244** binary operator. Either or both may be NULL.
10245**
10246** Expr.x.pList is a list of arguments if the expression is an SQL function,
10247** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
10248** Expr.x.pSelect is used if the expression is a sub-select or an expression of
10249** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
10250** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
10251** valid.
10252**
10253** An expression of the form ID or ID.ID refers to a column in a table.
10254** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
10255** the integer cursor number of a VDBE cursor pointing to that table and
10256** Expr.iColumn is the column number for the specific column.  If the
10257** expression is used as a result in an aggregate SELECT, then the
10258** value is also stored in the Expr.iAgg column in the aggregate so that
10259** it can be accessed after all aggregates are computed.
10260**
10261** If the expression is an unbound variable marker (a question mark
10262** character '?' in the original SQL) then the Expr.iTable holds the index
10263** number for that variable.
10264**
10265** If the expression is a subquery then Expr.iColumn holds an integer
10266** register number containing the result of the subquery.  If the
10267** subquery gives a constant result, then iTable is -1.  If the subquery
10268** gives a different answer at different times during statement processing
10269** then iTable is the address of a subroutine that computes the subquery.
10270**
10271** If the Expr is of type OP_Column, and the table it is selecting from
10272** is a disk table or the "old.*" pseudo-table, then pTab points to the
10273** corresponding table definition.
10274**
10275** ALLOCATION NOTES:
10276**
10277** Expr objects can use a lot of memory space in database schema.  To
10278** help reduce memory requirements, sometimes an Expr object will be
10279** truncated.  And to reduce the number of memory allocations, sometimes
10280** two or more Expr objects will be stored in a single memory allocation,
10281** together with Expr.zToken strings.
10282**
10283** If the EP_Reduced and EP_TokenOnly flags are set when
10284** an Expr object is truncated.  When EP_Reduced is set, then all
10285** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
10286** are contained within the same memory allocation.  Note, however, that
10287** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
10288** allocated, regardless of whether or not EP_Reduced is set.
10289*/
10290struct Expr {
10291  u8 op;                 /* Operation performed by this node */
10292  char affinity;         /* The affinity of the column or 0 if not a column */
10293  u16 flags;             /* Various flags.  EP_* See below */
10294  union {
10295    char *zToken;          /* Token value. Zero terminated and dequoted */
10296    int iValue;            /* Non-negative integer value if EP_IntValue */
10297  } u;
10298
10299  /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
10300  ** space is allocated for the fields below this point. An attempt to
10301  ** access them will result in a segfault or malfunction.
10302  *********************************************************************/
10303
10304  Expr *pLeft;           /* Left subnode */
10305  Expr *pRight;          /* Right subnode */
10306  union {
10307    ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
10308    Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
10309  } x;
10310  CollSeq *pColl;        /* The collation type of the column or 0 */
10311
10312  /* If the EP_Reduced flag is set in the Expr.flags mask, then no
10313  ** space is allocated for the fields below this point. An attempt to
10314  ** access them will result in a segfault or malfunction.
10315  *********************************************************************/
10316
10317  int iTable;            /* TK_COLUMN: cursor number of table holding column
10318                         ** TK_REGISTER: register number
10319                         ** TK_TRIGGER: 1 -> new, 0 -> old */
10320  ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
10321                         ** TK_VARIABLE: variable number (always >= 1). */
10322  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
10323  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
10324  u8 flags2;             /* Second set of flags.  EP2_... */
10325  u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
10326  AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
10327  Table *pTab;           /* Table for TK_COLUMN expressions. */
10328#if SQLITE_MAX_EXPR_DEPTH>0
10329  int nHeight;           /* Height of the tree headed by this node */
10330#endif
10331};
10332
10333/*
10334** The following are the meanings of bits in the Expr.flags field.
10335*/
10336#define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
10337#define EP_Agg        0x0002  /* Contains one or more aggregate functions */
10338#define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
10339#define EP_Error      0x0008  /* Expression contains one or more errors */
10340#define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
10341#define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
10342#define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
10343#define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
10344#define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
10345#define EP_FixedDest  0x0200  /* Result needed in a specific register */
10346#define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
10347#define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
10348
10349#define EP_Reduced    0x1000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10350#define EP_TokenOnly  0x2000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10351#define EP_Static     0x4000  /* Held in memory not obtained from malloc() */
10352
10353/*
10354** The following are the meanings of bits in the Expr.flags2 field.
10355*/
10356#define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
10357#define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
10358
10359/*
10360** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
10361** flag on an expression structure.  This flag is used for VV&A only.  The
10362** routine is implemented as a macro that only works when in debugging mode,
10363** so as not to burden production code.
10364*/
10365#ifdef SQLITE_DEBUG
10366# define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
10367#else
10368# define ExprSetIrreducible(X)
10369#endif
10370
10371/*
10372** These macros can be used to test, set, or clear bits in the
10373** Expr.flags field.
10374*/
10375#define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
10376#define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
10377#define ExprSetProperty(E,P)     (E)->flags|=(P)
10378#define ExprClearProperty(E,P)   (E)->flags&=~(P)
10379
10380/*
10381** Macros to determine the number of bytes required by a normal Expr
10382** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
10383** and an Expr struct with the EP_TokenOnly flag set.
10384*/
10385#define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
10386#define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
10387#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
10388
10389/*
10390** Flags passed to the sqlite3ExprDup() function. See the header comment
10391** above sqlite3ExprDup() for details.
10392*/
10393#define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
10394
10395/*
10396** A list of expressions.  Each expression may optionally have a
10397** name.  An expr/name combination can be used in several ways, such
10398** as the list of "expr AS ID" fields following a "SELECT" or in the
10399** list of "ID = expr" items in an UPDATE.  A list of expressions can
10400** also be used as the argument to a function, in which case the a.zName
10401** field is not used.
10402*/
10403struct ExprList {
10404  int nExpr;             /* Number of expressions on the list */
10405  int nAlloc;            /* Number of entries allocated below */
10406  int iECursor;          /* VDBE Cursor associated with this ExprList */
10407  struct ExprList_item {
10408    Expr *pExpr;           /* The list of expressions */
10409    char *zName;           /* Token associated with this expression */
10410    char *zSpan;           /* Original text of the expression */
10411    u8 sortOrder;          /* 1 for DESC or 0 for ASC */
10412    u8 done;               /* A flag to indicate when processing is finished */
10413    u16 iCol;              /* For ORDER BY, column number in result set */
10414    u16 iAlias;            /* Index into Parse.aAlias[] for zName */
10415  } *a;                  /* One entry for each expression */
10416};
10417
10418/*
10419** An instance of this structure is used by the parser to record both
10420** the parse tree for an expression and the span of input text for an
10421** expression.
10422*/
10423struct ExprSpan {
10424  Expr *pExpr;          /* The expression parse tree */
10425  const char *zStart;   /* First character of input text */
10426  const char *zEnd;     /* One character past the end of input text */
10427};
10428
10429/*
10430** An instance of this structure can hold a simple list of identifiers,
10431** such as the list "a,b,c" in the following statements:
10432**
10433**      INSERT INTO t(a,b,c) VALUES ...;
10434**      CREATE INDEX idx ON t(a,b,c);
10435**      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
10436**
10437** The IdList.a.idx field is used when the IdList represents the list of
10438** column names after a table name in an INSERT statement.  In the statement
10439**
10440**     INSERT INTO t(a,b,c) ...
10441**
10442** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
10443*/
10444struct IdList {
10445  struct IdList_item {
10446    char *zName;      /* Name of the identifier */
10447    int idx;          /* Index in some Table.aCol[] of a column named zName */
10448  } *a;
10449  int nId;         /* Number of identifiers on the list */
10450  int nAlloc;      /* Number of entries allocated for a[] below */
10451};
10452
10453/*
10454** The bitmask datatype defined below is used for various optimizations.
10455**
10456** Changing this from a 64-bit to a 32-bit type limits the number of
10457** tables in a join to 32 instead of 64.  But it also reduces the size
10458** of the library by 738 bytes on ix86.
10459*/
10460typedef u64 Bitmask;
10461
10462/*
10463** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
10464*/
10465#define BMS  ((int)(sizeof(Bitmask)*8))
10466
10467/*
10468** The following structure describes the FROM clause of a SELECT statement.
10469** Each table or subquery in the FROM clause is a separate element of
10470** the SrcList.a[] array.
10471**
10472** With the addition of multiple database support, the following structure
10473** can also be used to describe a particular table such as the table that
10474** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
10475** such a table must be a simple name: ID.  But in SQLite, the table can
10476** now be identified by a database name, a dot, then the table name: ID.ID.
10477**
10478** The jointype starts out showing the join type between the current table
10479** and the next table on the list.  The parser builds the list this way.
10480** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
10481** jointype expresses the join between the table and the previous table.
10482**
10483** In the colUsed field, the high-order bit (bit 63) is set if the table
10484** contains more than 63 columns and the 64-th or later column is used.
10485*/
10486struct SrcList {
10487  i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
10488  i16 nAlloc;      /* Number of entries allocated in a[] below */
10489  struct SrcList_item {
10490    char *zDatabase;  /* Name of database holding this table */
10491    char *zName;      /* Name of the table */
10492    char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
10493    Table *pTab;      /* An SQL table corresponding to zName */
10494    Select *pSelect;  /* A SELECT statement used in place of a table name */
10495    int addrFillSub;  /* Address of subroutine to manifest a subquery */
10496    int regReturn;    /* Register holding return address of addrFillSub */
10497    u8 jointype;      /* Type of join between this able and the previous */
10498    u8 notIndexed;    /* True if there is a NOT INDEXED clause */
10499    u8 isCorrelated;  /* True if sub-query is correlated */
10500#ifndef SQLITE_OMIT_EXPLAIN
10501    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
10502#endif
10503    int iCursor;      /* The VDBE cursor number used to access this table */
10504    Expr *pOn;        /* The ON clause of a join */
10505    IdList *pUsing;   /* The USING clause of a join */
10506    Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
10507    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
10508    Index *pIndex;    /* Index structure corresponding to zIndex, if any */
10509  } a[1];             /* One entry for each identifier on the list */
10510};
10511
10512/*
10513** Permitted values of the SrcList.a.jointype field
10514*/
10515#define JT_INNER     0x0001    /* Any kind of inner or cross join */
10516#define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
10517#define JT_NATURAL   0x0004    /* True for a "natural" join */
10518#define JT_LEFT      0x0008    /* Left outer join */
10519#define JT_RIGHT     0x0010    /* Right outer join */
10520#define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
10521#define JT_ERROR     0x0040    /* unknown or unsupported join type */
10522
10523
10524/*
10525** A WherePlan object holds information that describes a lookup
10526** strategy.
10527**
10528** This object is intended to be opaque outside of the where.c module.
10529** It is included here only so that that compiler will know how big it
10530** is.  None of the fields in this object should be used outside of
10531** the where.c module.
10532**
10533** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
10534** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
10535** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
10536** case that more than one of these conditions is true.
10537*/
10538struct WherePlan {
10539  u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
10540  u32 nEq;                       /* Number of == constraints */
10541  double nRow;                   /* Estimated number of rows (for EQP) */
10542  union {
10543    Index *pIdx;                   /* Index when WHERE_INDEXED is true */
10544    struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
10545    sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
10546  } u;
10547};
10548
10549/*
10550** For each nested loop in a WHERE clause implementation, the WhereInfo
10551** structure contains a single instance of this structure.  This structure
10552** is intended to be private the the where.c module and should not be
10553** access or modified by other modules.
10554**
10555** The pIdxInfo field is used to help pick the best index on a
10556** virtual table.  The pIdxInfo pointer contains indexing
10557** information for the i-th table in the FROM clause before reordering.
10558** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
10559** All other information in the i-th WhereLevel object for the i-th table
10560** after FROM clause ordering.
10561*/
10562struct WhereLevel {
10563  WherePlan plan;       /* query plan for this element of the FROM clause */
10564  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
10565  int iTabCur;          /* The VDBE cursor used to access the table */
10566  int iIdxCur;          /* The VDBE cursor used to access pIdx */
10567  int addrBrk;          /* Jump here to break out of the loop */
10568  int addrNxt;          /* Jump here to start the next IN combination */
10569  int addrCont;         /* Jump here to continue with the next loop cycle */
10570  int addrFirst;        /* First instruction of interior of the loop */
10571  u8 iFrom;             /* Which entry in the FROM clause */
10572  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
10573  int p1, p2;           /* Operands of the opcode used to ends the loop */
10574  union {               /* Information that depends on plan.wsFlags */
10575    struct {
10576      int nIn;              /* Number of entries in aInLoop[] */
10577      struct InLoop {
10578        int iCur;              /* The VDBE cursor used by this IN operator */
10579        int addrInTop;         /* Top of the IN loop */
10580      } *aInLoop;           /* Information about each nested IN operator */
10581    } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
10582  } u;
10583
10584  /* The following field is really not part of the current level.  But
10585  ** we need a place to cache virtual table index information for each
10586  ** virtual table in the FROM clause and the WhereLevel structure is
10587  ** a convenient place since there is one WhereLevel for each FROM clause
10588  ** element.
10589  */
10590  sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
10591};
10592
10593/*
10594** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
10595** and the WhereInfo.wctrlFlags member.
10596*/
10597#define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
10598#define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
10599#define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
10600#define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
10601#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
10602#define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
10603#define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
10604#define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
10605#define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
10606
10607/*
10608** The WHERE clause processing routine has two halves.  The
10609** first part does the start of the WHERE loop and the second
10610** half does the tail of the WHERE loop.  An instance of
10611** this structure is returned by the first half and passed
10612** into the second half to give some continuity.
10613*/
10614struct WhereInfo {
10615  Parse *pParse;       /* Parsing and code generating context */
10616  u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
10617  u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
10618  u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
10619  u8 eDistinct;
10620  SrcList *pTabList;             /* List of tables in the join */
10621  int iTop;                      /* The very beginning of the WHERE loop */
10622  int iContinue;                 /* Jump here to continue with next record */
10623  int iBreak;                    /* Jump here to break out of the loop */
10624  int nLevel;                    /* Number of nested loop */
10625  struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
10626  double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
10627  double nRowOut;                /* Estimated number of output rows */
10628  WhereLevel a[1];               /* Information about each nest loop in WHERE */
10629};
10630
10631#define WHERE_DISTINCT_UNIQUE 1
10632#define WHERE_DISTINCT_ORDERED 2
10633
10634/*
10635** A NameContext defines a context in which to resolve table and column
10636** names.  The context consists of a list of tables (the pSrcList) field and
10637** a list of named expression (pEList).  The named expression list may
10638** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
10639** to the table being operated on by INSERT, UPDATE, or DELETE.  The
10640** pEList corresponds to the result set of a SELECT and is NULL for
10641** other statements.
10642**
10643** NameContexts can be nested.  When resolving names, the inner-most
10644** context is searched first.  If no match is found, the next outer
10645** context is checked.  If there is still no match, the next context
10646** is checked.  This process continues until either a match is found
10647** or all contexts are check.  When a match is found, the nRef member of
10648** the context containing the match is incremented.
10649**
10650** Each subquery gets a new NameContext.  The pNext field points to the
10651** NameContext in the parent query.  Thus the process of scanning the
10652** NameContext list corresponds to searching through successively outer
10653** subqueries looking for a match.
10654*/
10655struct NameContext {
10656  Parse *pParse;       /* The parser */
10657  SrcList *pSrcList;   /* One or more tables used to resolve names */
10658  ExprList *pEList;    /* Optional list of named expressions */
10659  int nRef;            /* Number of names resolved by this context */
10660  int nErr;            /* Number of errors encountered while resolving names */
10661  u8 allowAgg;         /* Aggregate functions allowed here */
10662  u8 hasAgg;           /* True if aggregates are seen */
10663  u8 isCheck;          /* True if resolving names in a CHECK constraint */
10664  int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
10665  AggInfo *pAggInfo;   /* Information about aggregates at this level */
10666  NameContext *pNext;  /* Next outer name context.  NULL for outermost */
10667};
10668
10669/*
10670** An instance of the following structure contains all information
10671** needed to generate code for a single SELECT statement.
10672**
10673** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
10674** If there is a LIMIT clause, the parser sets nLimit to the value of the
10675** limit and nOffset to the value of the offset (or 0 if there is not
10676** offset).  But later on, nLimit and nOffset become the memory locations
10677** in the VDBE that record the limit and offset counters.
10678**
10679** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
10680** These addresses must be stored so that we can go back and fill in
10681** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
10682** the number of columns in P2 can be computed at the same time
10683** as the OP_OpenEphm instruction is coded because not
10684** enough information about the compound query is known at that point.
10685** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
10686** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
10687** sequences for the ORDER BY clause.
10688*/
10689struct Select {
10690  ExprList *pEList;      /* The fields of the result */
10691  u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
10692  char affinity;         /* MakeRecord with this affinity for SRT_Set */
10693  u16 selFlags;          /* Various SF_* values */
10694  SrcList *pSrc;         /* The FROM clause */
10695  Expr *pWhere;          /* The WHERE clause */
10696  ExprList *pGroupBy;    /* The GROUP BY clause */
10697  Expr *pHaving;         /* The HAVING clause */
10698  ExprList *pOrderBy;    /* The ORDER BY clause */
10699  Select *pPrior;        /* Prior select in a compound select statement */
10700  Select *pNext;         /* Next select to the left in a compound */
10701  Select *pRightmost;    /* Right-most select in a compound select statement */
10702  Expr *pLimit;          /* LIMIT expression. NULL means not used. */
10703  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
10704  int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
10705  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
10706  double nSelectRow;     /* Estimated number of result rows */
10707};
10708
10709/*
10710** Allowed values for Select.selFlags.  The "SF" prefix stands for
10711** "Select Flag".
10712*/
10713#define SF_Distinct        0x0001  /* Output should be DISTINCT */
10714#define SF_Resolved        0x0002  /* Identifiers have been resolved */
10715#define SF_Aggregate       0x0004  /* Contains aggregate functions */
10716#define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
10717#define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
10718#define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
10719#define SF_UseSorter       0x0040  /* Sort using a sorter */
10720
10721
10722/*
10723** The results of a select can be distributed in several ways.  The
10724** "SRT" prefix means "SELECT Result Type".
10725*/
10726#define SRT_Union        1  /* Store result as keys in an index */
10727#define SRT_Except       2  /* Remove result from a UNION index */
10728#define SRT_Exists       3  /* Store 1 if the result is not empty */
10729#define SRT_Discard      4  /* Do not save the results anywhere */
10730
10731/* The ORDER BY clause is ignored for all of the above */
10732#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
10733
10734#define SRT_Output       5  /* Output each row of result */
10735#define SRT_Mem          6  /* Store result in a memory cell */
10736#define SRT_Set          7  /* Store results as keys in an index */
10737#define SRT_Table        8  /* Store result as data with an automatic rowid */
10738#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
10739#define SRT_Coroutine   10  /* Generate a single row of result */
10740
10741/*
10742** A structure used to customize the behavior of sqlite3Select(). See
10743** comments above sqlite3Select() for details.
10744*/
10745typedef struct SelectDest SelectDest;
10746struct SelectDest {
10747  u8 eDest;         /* How to dispose of the results */
10748  u8 affinity;      /* Affinity used when eDest==SRT_Set */
10749  int iParm;        /* A parameter used by the eDest disposal method */
10750  int iMem;         /* Base register where results are written */
10751  int nMem;         /* Number of registers allocated */
10752};
10753
10754/*
10755** During code generation of statements that do inserts into AUTOINCREMENT
10756** tables, the following information is attached to the Table.u.autoInc.p
10757** pointer of each autoincrement table to record some side information that
10758** the code generator needs.  We have to keep per-table autoincrement
10759** information in case inserts are down within triggers.  Triggers do not
10760** normally coordinate their activities, but we do need to coordinate the
10761** loading and saving of autoincrement information.
10762*/
10763struct AutoincInfo {
10764  AutoincInfo *pNext;   /* Next info block in a list of them all */
10765  Table *pTab;          /* Table this info block refers to */
10766  int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
10767  int regCtr;           /* Memory register holding the rowid counter */
10768};
10769
10770/*
10771** Size of the column cache
10772*/
10773#ifndef SQLITE_N_COLCACHE
10774# define SQLITE_N_COLCACHE 10
10775#endif
10776
10777/*
10778** At least one instance of the following structure is created for each
10779** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
10780** statement. All such objects are stored in the linked list headed at
10781** Parse.pTriggerPrg and deleted once statement compilation has been
10782** completed.
10783**
10784** A Vdbe sub-program that implements the body and WHEN clause of trigger
10785** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
10786** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
10787** The Parse.pTriggerPrg list never contains two entries with the same
10788** values for both pTrigger and orconf.
10789**
10790** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
10791** accessed (or set to 0 for triggers fired as a result of INSERT
10792** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
10793** a mask of new.* columns used by the program.
10794*/
10795struct TriggerPrg {
10796  Trigger *pTrigger;      /* Trigger this program was coded from */
10797  int orconf;             /* Default ON CONFLICT policy */
10798  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
10799  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
10800  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
10801};
10802
10803/*
10804** The yDbMask datatype for the bitmask of all attached databases.
10805*/
10806#if SQLITE_MAX_ATTACHED>30
10807  typedef sqlite3_uint64 yDbMask;
10808#else
10809  typedef unsigned int yDbMask;
10810#endif
10811
10812/*
10813** An SQL parser context.  A copy of this structure is passed through
10814** the parser and down into all the parser action routine in order to
10815** carry around information that is global to the entire parse.
10816**
10817** The structure is divided into two parts.  When the parser and code
10818** generate call themselves recursively, the first part of the structure
10819** is constant but the second part is reset at the beginning and end of
10820** each recursion.
10821**
10822** The nTableLock and aTableLock variables are only used if the shared-cache
10823** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
10824** used to store the set of table-locks required by the statement being
10825** compiled. Function sqlite3TableLock() is used to add entries to the
10826** list.
10827*/
10828struct Parse {
10829  sqlite3 *db;         /* The main database structure */
10830  int rc;              /* Return code from execution */
10831  char *zErrMsg;       /* An error message */
10832  Vdbe *pVdbe;         /* An engine for executing database bytecode */
10833  u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
10834  u8 nameClash;        /* A permanent table name clashes with temp table name */
10835  u8 checkSchema;      /* Causes schema cookie check after an error */
10836  u8 nested;           /* Number of nested calls to the parser/code generator */
10837  u8 parseError;       /* True after a parsing error.  Ticket #1794 */
10838  u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
10839  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
10840  int aTempReg[8];     /* Holding area for temporary registers */
10841  int nRangeReg;       /* Size of the temporary register block */
10842  int iRangeReg;       /* First register in temporary register block */
10843  int nErr;            /* Number of errors seen */
10844  int nTab;            /* Number of previously allocated VDBE cursors */
10845  int nMem;            /* Number of memory cells used so far */
10846  int nSet;            /* Number of sets used so far */
10847  int ckBase;          /* Base register of data during check constraints */
10848  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
10849  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
10850  u8 nColCache;        /* Number of entries in the column cache */
10851  u8 iColCache;        /* Next entry of the cache to replace */
10852  struct yColCache {
10853    int iTable;           /* Table cursor number */
10854    int iColumn;          /* Table column number */
10855    u8 tempReg;           /* iReg is a temp register that needs to be freed */
10856    int iLevel;           /* Nesting level */
10857    int iReg;             /* Reg with value of this column. 0 means none. */
10858    int lru;              /* Least recently used entry has the smallest value */
10859  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
10860  yDbMask writeMask;   /* Start a write transaction on these databases */
10861  yDbMask cookieMask;  /* Bitmask of schema verified databases */
10862  u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
10863  u8 mayAbort;         /* True if statement may throw an ABORT exception */
10864  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
10865  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
10866#ifndef SQLITE_OMIT_SHARED_CACHE
10867  int nTableLock;        /* Number of locks in aTableLock */
10868  TableLock *aTableLock; /* Required table locks for shared-cache mode */
10869#endif
10870  int regRowid;        /* Register holding rowid of CREATE TABLE entry */
10871  int regRoot;         /* Register holding root page number for new objects */
10872  AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
10873  int nMaxArg;         /* Max args passed to user function by sub-program */
10874
10875  /* Information used while coding trigger programs. */
10876  Parse *pToplevel;    /* Parse structure for main program (or NULL) */
10877  Table *pTriggerTab;  /* Table triggers are being coded for */
10878  u32 oldmask;         /* Mask of old.* columns referenced */
10879  u32 newmask;         /* Mask of new.* columns referenced */
10880  u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
10881  u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
10882  u8 disableTriggers;  /* True to disable triggers */
10883  double nQueryLoop;   /* Estimated number of iterations of a query */
10884
10885  /* Above is constant between recursions.  Below is reset before and after
10886  ** each recursion */
10887
10888  int nVar;            /* Number of '?' variables seen in the SQL so far */
10889  int nzVar;           /* Number of available slots in azVar[] */
10890  char **azVar;        /* Pointers to names of parameters */
10891  Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
10892  int nAlias;          /* Number of aliased result set columns */
10893  int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
10894  int *aAlias;         /* Register used to hold aliased result */
10895  u8 explain;          /* True if the EXPLAIN flag is found on the query */
10896  Token sNameToken;    /* Token with unqualified schema object name */
10897  Token sLastToken;    /* The last token parsed */
10898  const char *zTail;   /* All SQL text past the last semicolon parsed */
10899  Table *pNewTable;    /* A table being constructed by CREATE TABLE */
10900  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
10901  const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
10902#ifndef SQLITE_OMIT_VIRTUALTABLE
10903  Token sArg;                /* Complete text of a module argument */
10904  u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
10905  int nVtabLock;             /* Number of virtual tables to lock */
10906  Table **apVtabLock;        /* Pointer to virtual tables needing locking */
10907#endif
10908  int nHeight;            /* Expression tree height of current sub-select */
10909  Table *pZombieTab;      /* List of Table objects to delete after code gen */
10910  TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
10911
10912#ifndef SQLITE_OMIT_EXPLAIN
10913  int iSelectId;
10914  int iNextSelectId;
10915#endif
10916};
10917
10918#ifdef SQLITE_OMIT_VIRTUALTABLE
10919  #define IN_DECLARE_VTAB 0
10920#else
10921  #define IN_DECLARE_VTAB (pParse->declareVtab)
10922#endif
10923
10924/*
10925** An instance of the following structure can be declared on a stack and used
10926** to save the Parse.zAuthContext value so that it can be restored later.
10927*/
10928struct AuthContext {
10929  const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
10930  Parse *pParse;              /* The Parse structure */
10931};
10932
10933/*
10934** Bitfield flags for P5 value in OP_Insert and OP_Delete
10935*/
10936#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
10937#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
10938#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
10939#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
10940#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
10941#define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
10942
10943/*
10944 * Each trigger present in the database schema is stored as an instance of
10945 * struct Trigger.
10946 *
10947 * Pointers to instances of struct Trigger are stored in two ways.
10948 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
10949 *    database). This allows Trigger structures to be retrieved by name.
10950 * 2. All triggers associated with a single table form a linked list, using the
10951 *    pNext member of struct Trigger. A pointer to the first element of the
10952 *    linked list is stored as the "pTrigger" member of the associated
10953 *    struct Table.
10954 *
10955 * The "step_list" member points to the first element of a linked list
10956 * containing the SQL statements specified as the trigger program.
10957 */
10958struct Trigger {
10959  char *zName;            /* The name of the trigger                        */
10960  char *table;            /* The table or view to which the trigger applies */
10961  u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
10962  u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
10963  Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
10964  IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
10965                             the <column-list> is stored here */
10966  Schema *pSchema;        /* Schema containing the trigger */
10967  Schema *pTabSchema;     /* Schema containing the table */
10968  TriggerStep *step_list; /* Link list of trigger program steps             */
10969  Trigger *pNext;         /* Next trigger associated with the table */
10970};
10971
10972/*
10973** A trigger is either a BEFORE or an AFTER trigger.  The following constants
10974** determine which.
10975**
10976** If there are multiple triggers, you might of some BEFORE and some AFTER.
10977** In that cases, the constants below can be ORed together.
10978*/
10979#define TRIGGER_BEFORE  1
10980#define TRIGGER_AFTER   2
10981
10982/*
10983 * An instance of struct TriggerStep is used to store a single SQL statement
10984 * that is a part of a trigger-program.
10985 *
10986 * Instances of struct TriggerStep are stored in a singly linked list (linked
10987 * using the "pNext" member) referenced by the "step_list" member of the
10988 * associated struct Trigger instance. The first element of the linked list is
10989 * the first step of the trigger-program.
10990 *
10991 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
10992 * "SELECT" statement. The meanings of the other members is determined by the
10993 * value of "op" as follows:
10994 *
10995 * (op == TK_INSERT)
10996 * orconf    -> stores the ON CONFLICT algorithm
10997 * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
10998 *              this stores a pointer to the SELECT statement. Otherwise NULL.
10999 * target    -> A token holding the quoted name of the table to insert into.
11000 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
11001 *              this stores values to be inserted. Otherwise NULL.
11002 * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
11003 *              statement, then this stores the column-names to be
11004 *              inserted into.
11005 *
11006 * (op == TK_DELETE)
11007 * target    -> A token holding the quoted name of the table to delete from.
11008 * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
11009 *              Otherwise NULL.
11010 *
11011 * (op == TK_UPDATE)
11012 * target    -> A token holding the quoted name of the table to update rows of.
11013 * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
11014 *              Otherwise NULL.
11015 * pExprList -> A list of the columns to update and the expressions to update
11016 *              them to. See sqlite3Update() documentation of "pChanges"
11017 *              argument.
11018 *
11019 */
11020struct TriggerStep {
11021  u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
11022  u8 orconf;           /* OE_Rollback etc. */
11023  Trigger *pTrig;      /* The trigger that this step is a part of */
11024  Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
11025  Token target;        /* Target table for DELETE, UPDATE, INSERT */
11026  Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
11027  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
11028  IdList *pIdList;     /* Column names for INSERT */
11029  TriggerStep *pNext;  /* Next in the link-list */
11030  TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
11031};
11032
11033/*
11034** The following structure contains information used by the sqliteFix...
11035** routines as they walk the parse tree to make database references
11036** explicit.
11037*/
11038typedef struct DbFixer DbFixer;
11039struct DbFixer {
11040  Parse *pParse;      /* The parsing context.  Error messages written here */
11041  const char *zDb;    /* Make sure all objects are contained in this database */
11042  const char *zType;  /* Type of the container - used for error messages */
11043  const Token *pName; /* Name of the container - used for error messages */
11044};
11045
11046/*
11047** An objected used to accumulate the text of a string where we
11048** do not necessarily know how big the string will be in the end.
11049*/
11050struct StrAccum {
11051  sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
11052  char *zBase;         /* A base allocation.  Not from malloc. */
11053  char *zText;         /* The string collected so far */
11054  int  nChar;          /* Length of the string so far */
11055  int  nAlloc;         /* Amount of space allocated in zText */
11056  int  mxAlloc;        /* Maximum allowed string length */
11057  u8   mallocFailed;   /* Becomes true if any memory allocation fails */
11058  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
11059  u8   tooBig;         /* Becomes true if string size exceeds limits */
11060};
11061
11062/*
11063** A pointer to this structure is used to communicate information
11064** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
11065*/
11066typedef struct {
11067  sqlite3 *db;        /* The database being initialized */
11068  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
11069  char **pzErrMsg;    /* Error message stored here */
11070  int rc;             /* Result code stored here */
11071} InitData;
11072
11073/*
11074** Structure containing global configuration data for the SQLite library.
11075**
11076** This structure also contains some state information.
11077*/
11078struct Sqlite3Config {
11079  int bMemstat;                     /* True to enable memory status */
11080  int bCoreMutex;                   /* True to enable core mutexing */
11081  int bFullMutex;                   /* True to enable full mutexing */
11082  int bOpenUri;                     /* True to interpret filenames as URIs */
11083  int mxStrlen;                     /* Maximum string length */
11084  int szLookaside;                  /* Default lookaside buffer size */
11085  int nLookaside;                   /* Default lookaside buffer count */
11086  sqlite3_mem_methods m;            /* Low-level memory allocation interface */
11087  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
11088  sqlite3_pcache_methods pcache;    /* Low-level page-cache interface */
11089  void *pHeap;                      /* Heap storage space */
11090  int nHeap;                        /* Size of pHeap[] */
11091  int mnReq, mxReq;                 /* Min and max heap requests sizes */
11092  void *pScratch;                   /* Scratch memory */
11093  int szScratch;                    /* Size of each scratch buffer */
11094  int nScratch;                     /* Number of scratch buffers */
11095  void *pPage;                      /* Page cache memory */
11096  int szPage;                       /* Size of each page in pPage[] */
11097  int nPage;                        /* Number of pages in pPage[] */
11098  int mxParserStack;                /* maximum depth of the parser stack */
11099  int sharedCacheEnabled;           /* true if shared-cache mode enabled */
11100  /* The above might be initialized to non-zero.  The following need to always
11101  ** initially be zero, however. */
11102  int isInit;                       /* True after initialization has finished */
11103  int inProgress;                   /* True while initialization in progress */
11104  int isMutexInit;                  /* True after mutexes are initialized */
11105  int isMallocInit;                 /* True after malloc is initialized */
11106  int isPCacheInit;                 /* True after malloc is initialized */
11107  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
11108  int nRefInitMutex;                /* Number of users of pInitMutex */
11109  void (*xLog)(void*,int,const char*); /* Function for logging */
11110  void *pLogArg;                       /* First argument to xLog() */
11111  int bLocaltimeFault;              /* True to fail localtime() calls */
11112};
11113
11114/*
11115** Context pointer passed down through the tree-walk.
11116*/
11117struct Walker {
11118  int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
11119  int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
11120  Parse *pParse;                            /* Parser context.  */
11121  union {                                   /* Extra data for callback */
11122    NameContext *pNC;                          /* Naming context */
11123    int i;                                     /* Integer value */
11124  } u;
11125};
11126
11127/* Forward declarations */
11128SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
11129SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
11130SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
11131SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
11132SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
11133
11134/*
11135** Return code from the parse-tree walking primitives and their
11136** callbacks.
11137*/
11138#define WRC_Continue    0   /* Continue down into children */
11139#define WRC_Prune       1   /* Omit children but continue walking siblings */
11140#define WRC_Abort       2   /* Abandon the tree walk */
11141
11142/*
11143** Assuming zIn points to the first byte of a UTF-8 character,
11144** advance zIn to point to the first byte of the next UTF-8 character.
11145*/
11146#define SQLITE_SKIP_UTF8(zIn) {                        \
11147  if( (*(zIn++))>=0xc0 ){                              \
11148    while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
11149  }                                                    \
11150}
11151
11152/*
11153** The SQLITE_*_BKPT macros are substitutes for the error codes with
11154** the same name but without the _BKPT suffix.  These macros invoke
11155** routines that report the line-number on which the error originated
11156** using sqlite3_log().  The routines also provide a convenient place
11157** to set a debugger breakpoint.
11158*/
11159SQLITE_PRIVATE int sqlite3CorruptError(int);
11160SQLITE_PRIVATE int sqlite3MisuseError(int);
11161SQLITE_PRIVATE int sqlite3CantopenError(int);
11162#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
11163#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
11164#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
11165
11166
11167/*
11168** FTS4 is really an extension for FTS3.  It is enabled using the
11169** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
11170** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
11171*/
11172#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
11173# define SQLITE_ENABLE_FTS3
11174#endif
11175
11176/*
11177** The ctype.h header is needed for non-ASCII systems.  It is also
11178** needed by FTS3 when FTS3 is included in the amalgamation.
11179*/
11180#if !defined(SQLITE_ASCII) || \
11181    (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
11182# include <ctype.h>
11183#endif
11184
11185/*
11186** The following macros mimic the standard library functions toupper(),
11187** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
11188** sqlite versions only work for ASCII characters, regardless of locale.
11189*/
11190#ifdef SQLITE_ASCII
11191# define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
11192# define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
11193# define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
11194# define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
11195# define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
11196# define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
11197# define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
11198#else
11199# define sqlite3Toupper(x)   toupper((unsigned char)(x))
11200# define sqlite3Isspace(x)   isspace((unsigned char)(x))
11201# define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
11202# define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
11203# define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
11204# define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
11205# define sqlite3Tolower(x)   tolower((unsigned char)(x))
11206#endif
11207
11208/*
11209** Internal function prototypes
11210*/
11211SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
11212SQLITE_PRIVATE int sqlite3Strlen30(const char*);
11213#define sqlite3StrNICmp sqlite3_strnicmp
11214
11215SQLITE_PRIVATE int sqlite3MallocInit(void);
11216SQLITE_PRIVATE void sqlite3MallocEnd(void);
11217SQLITE_PRIVATE void *sqlite3Malloc(int);
11218SQLITE_PRIVATE void *sqlite3MallocZero(int);
11219SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
11220SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
11221SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
11222SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
11223SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
11224SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
11225SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
11226SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
11227SQLITE_PRIVATE int sqlite3MallocSize(void*);
11228SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
11229SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
11230SQLITE_PRIVATE void sqlite3ScratchFree(void*);
11231SQLITE_PRIVATE void *sqlite3PageMalloc(int);
11232SQLITE_PRIVATE void sqlite3PageFree(void*);
11233SQLITE_PRIVATE void sqlite3MemSetDefault(void);
11234SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
11235SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
11236
11237/*
11238** On systems with ample stack space and that support alloca(), make
11239** use of alloca() to obtain space for large automatic objects.  By default,
11240** obtain space from malloc().
11241**
11242** The alloca() routine never returns NULL.  This will cause code paths
11243** that deal with sqlite3StackAlloc() failures to be unreachable.
11244*/
11245#ifdef SQLITE_USE_ALLOCA
11246# define sqlite3StackAllocRaw(D,N)   alloca(N)
11247# define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
11248# define sqlite3StackFree(D,P)
11249#else
11250# define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
11251# define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
11252# define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
11253#endif
11254
11255#ifdef SQLITE_ENABLE_MEMSYS3
11256SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
11257#endif
11258#ifdef SQLITE_ENABLE_MEMSYS5
11259SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
11260#endif
11261
11262
11263#ifndef SQLITE_MUTEX_OMIT
11264SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
11265SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
11266SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
11267SQLITE_PRIVATE   int sqlite3MutexInit(void);
11268SQLITE_PRIVATE   int sqlite3MutexEnd(void);
11269#endif
11270
11271SQLITE_PRIVATE int sqlite3StatusValue(int);
11272SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
11273SQLITE_PRIVATE void sqlite3StatusSet(int, int);
11274
11275#ifndef SQLITE_OMIT_FLOATING_POINT
11276SQLITE_PRIVATE   int sqlite3IsNaN(double);
11277#else
11278# define sqlite3IsNaN(X)  0
11279#endif
11280
11281SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
11282#ifndef SQLITE_OMIT_TRACE
11283SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
11284#endif
11285SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
11286SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
11287SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
11288#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
11289SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
11290#endif
11291#if defined(SQLITE_TEST)
11292SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
11293#endif
11294SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
11295SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
11296SQLITE_PRIVATE int sqlite3Dequote(char*);
11297SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
11298SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
11299SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
11300SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
11301SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
11302SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
11303SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
11304SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
11305SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
11306SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
11307SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
11308SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
11309SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
11310SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
11311SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
11312SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
11313SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
11314SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
11315SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
11316SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
11317SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
11318SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
11319SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
11320SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
11321SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
11322SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
11323SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
11324SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
11325SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
11326SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
11327SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
11328SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
11329SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
11330SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
11331SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
11332SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
11333SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
11334                    sqlite3_vfs**,char**,char **);
11335
11336SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
11337SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
11338SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
11339SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
11340SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
11341SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
11342SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
11343
11344SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
11345SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
11346SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
11347SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
11348SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
11349
11350SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
11351
11352#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
11353SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
11354#else
11355# define sqlite3ViewGetColumnNames(A,B) 0
11356#endif
11357
11358SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11359SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11360#ifndef SQLITE_OMIT_AUTOINCREMENT
11361SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
11362SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
11363#else
11364# define sqlite3AutoincrementBegin(X)
11365# define sqlite3AutoincrementEnd(X)
11366#endif
11367SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
11368SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
11369SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
11370SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
11371SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
11372SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
11373SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
11374                                      Token*, Select*, Expr*, IdList*);
11375SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
11376SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
11377SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
11378SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
11379SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
11380SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
11381SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
11382                        Token*, int, int);
11383SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
11384SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
11385SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
11386                         Expr*,ExprList*,int,Expr*,Expr*);
11387SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
11388SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
11389SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
11390SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
11391#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11392SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
11393#endif
11394SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
11395SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11396SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
11397SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
11398SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
11399SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
11400SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
11401SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
11402SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
11403SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
11404SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
11405SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
11406SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
11407SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
11408SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
11409SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
11410SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
11411SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
11412SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
11413SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
11414SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
11415SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
11416SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
11417SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
11418SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
11419SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
11420SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
11421SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
11422SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
11423SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
11424SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
11425SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
11426SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
11427SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
11428SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
11429SQLITE_PRIVATE void sqlite3PrngSaveState(void);
11430SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
11431SQLITE_PRIVATE void sqlite3PrngResetState(void);
11432SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
11433SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
11434SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
11435SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
11436SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
11437SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
11438SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
11439SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
11440SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
11441SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
11442SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
11443SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
11444SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
11445SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
11446SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
11447SQLITE_PRIVATE int sqlite3IsRowid(const char*);
11448SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
11449SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
11450SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
11451SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
11452                                     int*,int,int,int,int,int*);
11453SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
11454SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
11455SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
11456SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
11457SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
11458SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
11459SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
11460SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
11461SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
11462SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
11463SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
11464SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
11465SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
11466SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
11467SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
11468SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
11469SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
11470SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
11471SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
11472
11473#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
11474SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
11475#endif
11476
11477#ifndef SQLITE_OMIT_TRIGGER
11478SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
11479                           Expr*,int, int);
11480SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
11481SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
11482SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
11483SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
11484SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
11485SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
11486                            int, int, int);
11487SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
11488  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
11489SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
11490SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
11491SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
11492                                        ExprList*,Select*,u8);
11493SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
11494SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
11495SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
11496SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
11497SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
11498# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
11499#else
11500# define sqlite3TriggersExist(B,C,D,E,F) 0
11501# define sqlite3DeleteTrigger(A,B)
11502# define sqlite3DropTriggerPtr(A,B)
11503# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
11504# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
11505# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
11506# define sqlite3TriggerList(X, Y) 0
11507# define sqlite3ParseToplevel(p) p
11508# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
11509#endif
11510
11511SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
11512SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
11513SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
11514#ifndef SQLITE_OMIT_AUTHORIZATION
11515SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
11516SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
11517SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
11518SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
11519SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
11520#else
11521# define sqlite3AuthRead(a,b,c,d)
11522# define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
11523# define sqlite3AuthContextPush(a,b,c)
11524# define sqlite3AuthContextPop(a)  ((void)(a))
11525#endif
11526SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
11527SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
11528SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
11529SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
11530SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
11531SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
11532SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
11533SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
11534SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
11535SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
11536SQLITE_PRIVATE int sqlite3Atoi(const char*);
11537SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
11538SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
11539SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8*, const u8**);
11540
11541/*
11542** Routines to read and write variable-length integers.  These used to
11543** be defined locally, but now we use the varint routines in the util.c
11544** file.  Code should use the MACRO forms below, as the Varint32 versions
11545** are coded to assume the single byte case is already handled (which
11546** the MACRO form does).
11547*/
11548SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
11549SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
11550SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
11551SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
11552SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
11553
11554/*
11555** The header of a record consists of a sequence variable-length integers.
11556** These integers are almost always small and are encoded as a single byte.
11557** The following macros take advantage this fact to provide a fast encode
11558** and decode of the integers in a record header.  It is faster for the common
11559** case where the integer is a single byte.  It is a little slower when the
11560** integer is two or more bytes.  But overall it is faster.
11561**
11562** The following expressions are equivalent:
11563**
11564**     x = sqlite3GetVarint32( A, &B );
11565**     x = sqlite3PutVarint32( A, B );
11566**
11567**     x = getVarint32( A, B );
11568**     x = putVarint32( A, B );
11569**
11570*/
11571#define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
11572#define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
11573#define getVarint    sqlite3GetVarint
11574#define putVarint    sqlite3PutVarint
11575
11576
11577SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
11578SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
11579SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
11580SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
11581SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
11582SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
11583SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
11584SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
11585SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
11586SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
11587SQLITE_PRIVATE const char *sqlite3ErrStr(int);
11588SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
11589SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
11590SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
11591SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
11592SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
11593SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
11594SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
11595SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
11596SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
11597SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
11598SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
11599SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
11600SQLITE_PRIVATE int sqlite3AbsInt32(int);
11601#ifdef SQLITE_ENABLE_8_3_NAMES
11602SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
11603#else
11604# define sqlite3FileSuffix3(X,Y)
11605#endif
11606SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z);
11607
11608SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
11609SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
11610SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11611                        void(*)(void*));
11612SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11613SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11614SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11615#ifdef SQLITE_ENABLE_STAT2
11616SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11617#endif
11618SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11619SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11620#ifndef SQLITE_AMALGAMATION
11621SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
11622SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
11623SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
11624SQLITE_PRIVATE const Token sqlite3IntTokens[];
11625SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
11626SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
11627#ifndef SQLITE_OMIT_WSD
11628SQLITE_PRIVATE int sqlite3PendingByte;
11629#endif
11630#endif
11631SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
11632SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
11633SQLITE_PRIVATE void sqlite3AlterFunctions(void);
11634SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
11635SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
11636SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
11637SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
11638SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
11639SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
11640SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
11641SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
11642SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
11643SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
11644SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
11645SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
11646SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
11647SQLITE_PRIVATE char sqlite3AffinityType(const char*);
11648SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
11649SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
11650SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
11651SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
11652SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
11653SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
11654SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
11655SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
11656SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
11657SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
11658SQLITE_PRIVATE void sqlite3SchemaClear(void *);
11659SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
11660SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
11661SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
11662SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
11663  void (*)(sqlite3_context*,int,sqlite3_value **),
11664  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
11665  FuncDestructor *pDestructor
11666);
11667SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
11668SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
11669
11670SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
11671SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
11672SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
11673SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
11674SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
11675SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
11676
11677SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
11678SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
11679
11680/*
11681** The interface to the LEMON-generated parser
11682*/
11683SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
11684SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
11685SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
11686#ifdef YYTRACKMAXSTACKDEPTH
11687SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
11688#endif
11689
11690SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
11691#ifndef SQLITE_OMIT_LOAD_EXTENSION
11692SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
11693#else
11694# define sqlite3CloseExtensions(X)
11695#endif
11696
11697#ifndef SQLITE_OMIT_SHARED_CACHE
11698SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
11699#else
11700  #define sqlite3TableLock(v,w,x,y,z)
11701#endif
11702
11703#ifdef SQLITE_TEST
11704SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
11705#endif
11706
11707#ifdef SQLITE_OMIT_VIRTUALTABLE
11708#  define sqlite3VtabClear(Y)
11709#  define sqlite3VtabSync(X,Y) SQLITE_OK
11710#  define sqlite3VtabRollback(X)
11711#  define sqlite3VtabCommit(X)
11712#  define sqlite3VtabInSync(db) 0
11713#  define sqlite3VtabLock(X)
11714#  define sqlite3VtabUnlock(X)
11715#  define sqlite3VtabUnlockList(X)
11716#  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
11717#else
11718SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
11719SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
11720SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
11721SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
11722SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
11723SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
11724SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
11725SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
11726#  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11727#endif
11728SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
11729SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
11730SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
11731SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
11732SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
11733SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
11734SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
11735SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
11736SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
11737SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
11738SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
11739SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
11740SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
11741SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
11742SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11743SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11744SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11745SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11746SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11747SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11748SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11749
11750/* Declarations for functions in fkey.c. All of these are replaced by
11751** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
11752** key functionality is available. If OMIT_TRIGGER is defined but
11753** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
11754** this case foreign keys are parsed, but no other functionality is
11755** provided (enforcement of FK constraints requires the triggers sub-system).
11756*/
11757#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
11758SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
11759SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
11760SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
11761SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
11762SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
11763SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
11764#else
11765  #define sqlite3FkActions(a,b,c,d)
11766  #define sqlite3FkCheck(a,b,c,d)
11767  #define sqlite3FkDropTable(a,b,c)
11768  #define sqlite3FkOldmask(a,b)      0
11769  #define sqlite3FkRequired(a,b,c,d) 0
11770#endif
11771#ifndef SQLITE_OMIT_FOREIGN_KEY
11772SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
11773#else
11774  #define sqlite3FkDelete(a,b)
11775#endif
11776
11777
11778/*
11779** Available fault injectors.  Should be numbered beginning with 0.
11780*/
11781#define SQLITE_FAULTINJECTOR_MALLOC     0
11782#define SQLITE_FAULTINJECTOR_COUNT      1
11783
11784/*
11785** The interface to the code in fault.c used for identifying "benign"
11786** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
11787** is not defined.
11788*/
11789#ifndef SQLITE_OMIT_BUILTIN_TEST
11790SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
11791SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
11792#else
11793  #define sqlite3BeginBenignMalloc()
11794  #define sqlite3EndBenignMalloc()
11795#endif
11796
11797#define IN_INDEX_ROWID           1
11798#define IN_INDEX_EPH             2
11799#define IN_INDEX_INDEX           3
11800SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
11801
11802#ifdef SQLITE_ENABLE_ATOMIC_WRITE
11803SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
11804SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
11805SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
11806#else
11807  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
11808#endif
11809
11810SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
11811SQLITE_PRIVATE int sqlite3MemJournalSize(void);
11812SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
11813
11814#if SQLITE_MAX_EXPR_DEPTH>0
11815SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
11816SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
11817SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
11818#else
11819  #define sqlite3ExprSetHeight(x,y)
11820  #define sqlite3SelectExprHeight(x) 0
11821  #define sqlite3ExprCheckHeight(x,y)
11822#endif
11823
11824SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
11825SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
11826
11827#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
11828SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
11829SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
11830SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
11831#else
11832  #define sqlite3ConnectionBlocked(x,y)
11833  #define sqlite3ConnectionUnlocked(x)
11834  #define sqlite3ConnectionClosed(x)
11835#endif
11836
11837#ifdef SQLITE_DEBUG
11838SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
11839#endif
11840
11841/*
11842** If the SQLITE_ENABLE IOTRACE exists then the global variable
11843** sqlite3IoTrace is a pointer to a printf-like routine used to
11844** print I/O tracing messages.
11845*/
11846#ifdef SQLITE_ENABLE_IOTRACE
11847# define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
11848SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
11849SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
11850#else
11851# define IOTRACE(A)
11852# define sqlite3VdbeIOTraceSql(X)
11853#endif
11854
11855/*
11856** These routines are available for the mem2.c debugging memory allocator
11857** only.  They are used to verify that different "types" of memory
11858** allocations are properly tracked by the system.
11859**
11860** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
11861** the MEMTYPE_* macros defined below.  The type must be a bitmask with
11862** a single bit set.
11863**
11864** sqlite3MemdebugHasType() returns true if any of the bits in its second
11865** argument match the type set by the previous sqlite3MemdebugSetType().
11866** sqlite3MemdebugHasType() is intended for use inside assert() statements.
11867**
11868** sqlite3MemdebugNoType() returns true if none of the bits in its second
11869** argument match the type set by the previous sqlite3MemdebugSetType().
11870**
11871** Perhaps the most important point is the difference between MEMTYPE_HEAP
11872** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
11873** it might have been allocated by lookaside, except the allocation was
11874** too large or lookaside was already full.  It is important to verify
11875** that allocations that might have been satisfied by lookaside are not
11876** passed back to non-lookaside free() routines.  Asserts such as the
11877** example above are placed on the non-lookaside free() routines to verify
11878** this constraint.
11879**
11880** All of this is no-op for a production build.  It only comes into
11881** play when the SQLITE_MEMDEBUG compile-time option is used.
11882*/
11883#ifdef SQLITE_MEMDEBUG
11884SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
11885SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
11886SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
11887#else
11888# define sqlite3MemdebugSetType(X,Y)  /* no-op */
11889# define sqlite3MemdebugHasType(X,Y)  1
11890# define sqlite3MemdebugNoType(X,Y)   1
11891#endif
11892#define MEMTYPE_HEAP       0x01  /* General heap allocations */
11893#define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
11894#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
11895#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
11896#define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
11897
11898#endif /* _SQLITEINT_H_ */
11899
11900/************** End of sqliteInt.h *******************************************/
11901/************** Begin file global.c ******************************************/
11902/*
11903** 2008 June 13
11904**
11905** The author disclaims copyright to this source code.  In place of
11906** a legal notice, here is a blessing:
11907**
11908**    May you do good and not evil.
11909**    May you find forgiveness for yourself and forgive others.
11910**    May you share freely, never taking more than you give.
11911**
11912*************************************************************************
11913**
11914** This file contains definitions of global variables and contants.
11915*/
11916
11917/* An array to map all upper-case characters into their corresponding
11918** lower-case character.
11919**
11920** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
11921** handle case conversions for the UTF character set since the tables
11922** involved are nearly as big or bigger than SQLite itself.
11923*/
11924SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
11925#ifdef SQLITE_ASCII
11926      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
11927     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
11928     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
11929     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
11930    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
11931    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
11932    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
11933    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
11934    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
11935    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
11936    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
11937    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
11938    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
11939    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
11940    252,253,254,255
11941#endif
11942#ifdef SQLITE_EBCDIC
11943      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
11944     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
11945     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
11946     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
11947     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
11948     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
11949     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
11950    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
11951    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
11952    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
11953    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
11954    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
11955    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
11956    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
11957    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
11958    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
11959#endif
11960};
11961
11962/*
11963** The following 256 byte lookup table is used to support SQLites built-in
11964** equivalents to the following standard library functions:
11965**
11966**   isspace()                        0x01
11967**   isalpha()                        0x02
11968**   isdigit()                        0x04
11969**   isalnum()                        0x06
11970**   isxdigit()                       0x08
11971**   toupper()                        0x20
11972**   SQLite identifier character      0x40
11973**
11974** Bit 0x20 is set if the mapped character requires translation to upper
11975** case. i.e. if the character is a lower-case ASCII character.
11976** If x is a lower-case ASCII character, then its upper-case equivalent
11977** is (x - 0x20). Therefore toupper() can be implemented as:
11978**
11979**   (x & ~(map[x]&0x20))
11980**
11981** Standard function tolower() is implemented using the sqlite3UpperToLower[]
11982** array. tolower() is used more often than toupper() by SQLite.
11983**
11984** Bit 0x40 is set if the character non-alphanumeric and can be used in an
11985** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
11986** non-ASCII UTF character. Hence the test for whether or not a character is
11987** part of an identifier is 0x46.
11988**
11989** SQLite's versions are identical to the standard versions assuming a
11990** locale of "C". They are implemented as macros in sqliteInt.h.
11991*/
11992#ifdef SQLITE_ASCII
11993SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
11994  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
11995  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
11996  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
11997  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
11998  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
11999  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
12000  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
12001  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
12002
12003  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
12004  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
12005  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
12006  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
12007  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
12008  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
12009  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
12010  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
12011
12012  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
12013  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
12014  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
12015  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
12016  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
12017  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
12018  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
12019  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
12020
12021  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
12022  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
12023  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
12024  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
12025  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
12026  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
12027  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
12028  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
12029};
12030#endif
12031
12032#ifndef SQLITE_USE_URI
12033# define  SQLITE_USE_URI 0
12034#endif
12035
12036/*
12037** The following singleton contains the global configuration for
12038** the SQLite library.
12039*/
12040SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
12041   SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
12042   1,                         /* bCoreMutex */
12043   SQLITE_THREADSAFE==1,      /* bFullMutex */
12044   SQLITE_USE_URI,            /* bOpenUri */
12045   0x7ffffffe,                /* mxStrlen */
12046   128,                       /* szLookaside */
12047   500,                       /* nLookaside */
12048   {0,0,0,0,0,0,0,0},         /* m */
12049   {0,0,0,0,0,0,0,0,0},       /* mutex */
12050   {0,0,0,0,0,0,0,0,0,0,0},   /* pcache */
12051   (void*)0,                  /* pHeap */
12052   0,                         /* nHeap */
12053   0, 0,                      /* mnHeap, mxHeap */
12054   (void*)0,                  /* pScratch */
12055   0,                         /* szScratch */
12056   0,                         /* nScratch */
12057   (void*)0,                  /* pPage */
12058   0,                         /* szPage */
12059   0,                         /* nPage */
12060   0,                         /* mxParserStack */
12061   0,                         /* sharedCacheEnabled */
12062   /* All the rest should always be initialized to zero */
12063   0,                         /* isInit */
12064   0,                         /* inProgress */
12065   0,                         /* isMutexInit */
12066   0,                         /* isMallocInit */
12067   0,                         /* isPCacheInit */
12068   0,                         /* pInitMutex */
12069   0,                         /* nRefInitMutex */
12070   0,                         /* xLog */
12071   0,                         /* pLogArg */
12072   0,                         /* bLocaltimeFault */
12073};
12074
12075
12076/*
12077** Hash table for global functions - functions common to all
12078** database connections.  After initialization, this table is
12079** read-only.
12080*/
12081SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
12082
12083/*
12084** Constant tokens for values 0 and 1.
12085*/
12086SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
12087   { "0", 1 },
12088   { "1", 1 }
12089};
12090
12091
12092/*
12093** The value of the "pending" byte must be 0x40000000 (1 byte past the
12094** 1-gibabyte boundary) in a compatible database.  SQLite never uses
12095** the database page that contains the pending byte.  It never attempts
12096** to read or write that page.  The pending byte page is set assign
12097** for use by the VFS layers as space for managing file locks.
12098**
12099** During testing, it is often desirable to move the pending byte to
12100** a different position in the file.  This allows code that has to
12101** deal with the pending byte to run on files that are much smaller
12102** than 1 GiB.  The sqlite3_test_control() interface can be used to
12103** move the pending byte.
12104**
12105** IMPORTANT:  Changing the pending byte to any value other than
12106** 0x40000000 results in an incompatible database file format!
12107** Changing the pending byte during operating results in undefined
12108** and dileterious behavior.
12109*/
12110#ifndef SQLITE_OMIT_WSD
12111SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
12112#endif
12113
12114/*
12115** Properties of opcodes.  The OPFLG_INITIALIZER macro is
12116** created by mkopcodeh.awk during compilation.  Data is obtained
12117** from the comments following the "case OP_xxxx:" statements in
12118** the vdbe.c file.
12119*/
12120SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
12121
12122/************** End of global.c **********************************************/
12123/************** Begin file ctime.c *******************************************/
12124/*
12125** 2010 February 23
12126**
12127** The author disclaims copyright to this source code.  In place of
12128** a legal notice, here is a blessing:
12129**
12130**    May you do good and not evil.
12131**    May you find forgiveness for yourself and forgive others.
12132**    May you share freely, never taking more than you give.
12133**
12134*************************************************************************
12135**
12136** This file implements routines used to report what compile-time options
12137** SQLite was built with.
12138*/
12139
12140#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
12141
12142
12143/*
12144** An array of names of all compile-time options.  This array should
12145** be sorted A-Z.
12146**
12147** This array looks large, but in a typical installation actually uses
12148** only a handful of compile-time options, so most times this array is usually
12149** rather short and uses little memory space.
12150*/
12151static const char * const azCompileOpt[] = {
12152
12153/* These macros are provided to "stringify" the value of the define
12154** for those options in which the value is meaningful. */
12155#define CTIMEOPT_VAL_(opt) #opt
12156#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
12157
12158#ifdef SQLITE_32BIT_ROWID
12159  "32BIT_ROWID",
12160#endif
12161#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
12162  "4_BYTE_ALIGNED_MALLOC",
12163#endif
12164#ifdef SQLITE_CASE_SENSITIVE_LIKE
12165  "CASE_SENSITIVE_LIKE",
12166#endif
12167#ifdef SQLITE_CHECK_PAGES
12168  "CHECK_PAGES",
12169#endif
12170#ifdef SQLITE_COVERAGE_TEST
12171  "COVERAGE_TEST",
12172#endif
12173#ifdef SQLITE_DEBUG
12174  "DEBUG",
12175#endif
12176#ifdef SQLITE_DEFAULT_LOCKING_MODE
12177  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
12178#endif
12179#ifdef SQLITE_DISABLE_DIRSYNC
12180  "DISABLE_DIRSYNC",
12181#endif
12182#ifdef SQLITE_DISABLE_LFS
12183  "DISABLE_LFS",
12184#endif
12185#ifdef SQLITE_ENABLE_ATOMIC_WRITE
12186  "ENABLE_ATOMIC_WRITE",
12187#endif
12188#ifdef SQLITE_ENABLE_CEROD
12189  "ENABLE_CEROD",
12190#endif
12191#ifdef SQLITE_ENABLE_COLUMN_METADATA
12192  "ENABLE_COLUMN_METADATA",
12193#endif
12194#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
12195  "ENABLE_EXPENSIVE_ASSERT",
12196#endif
12197#ifdef SQLITE_ENABLE_FTS1
12198  "ENABLE_FTS1",
12199#endif
12200#ifdef SQLITE_ENABLE_FTS2
12201  "ENABLE_FTS2",
12202#endif
12203#ifdef SQLITE_ENABLE_FTS3
12204  "ENABLE_FTS3",
12205#endif
12206#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
12207  "ENABLE_FTS3_PARENTHESIS",
12208#endif
12209#ifdef SQLITE_ENABLE_FTS4
12210  "ENABLE_FTS4",
12211#endif
12212#ifdef SQLITE_ENABLE_ICU
12213  "ENABLE_ICU",
12214#endif
12215#ifdef SQLITE_ENABLE_IOTRACE
12216  "ENABLE_IOTRACE",
12217#endif
12218#ifdef SQLITE_ENABLE_LOAD_EXTENSION
12219  "ENABLE_LOAD_EXTENSION",
12220#endif
12221#ifdef SQLITE_ENABLE_LOCKING_STYLE
12222  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
12223#endif
12224#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
12225  "ENABLE_MEMORY_MANAGEMENT",
12226#endif
12227#ifdef SQLITE_ENABLE_MEMSYS3
12228  "ENABLE_MEMSYS3",
12229#endif
12230#ifdef SQLITE_ENABLE_MEMSYS5
12231  "ENABLE_MEMSYS5",
12232#endif
12233#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
12234  "ENABLE_OVERSIZE_CELL_CHECK",
12235#endif
12236#ifdef SQLITE_ENABLE_RTREE
12237  "ENABLE_RTREE",
12238#endif
12239#ifdef SQLITE_ENABLE_STAT2
12240  "ENABLE_STAT2",
12241#endif
12242#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12243  "ENABLE_UNLOCK_NOTIFY",
12244#endif
12245#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12246  "ENABLE_UPDATE_DELETE_LIMIT",
12247#endif
12248#ifdef SQLITE_HAS_CODEC
12249  "HAS_CODEC",
12250#endif
12251#ifdef SQLITE_HAVE_ISNAN
12252  "HAVE_ISNAN",
12253#endif
12254#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
12255  "HOMEGROWN_RECURSIVE_MUTEX",
12256#endif
12257#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
12258  "IGNORE_AFP_LOCK_ERRORS",
12259#endif
12260#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
12261  "IGNORE_FLOCK_LOCK_ERRORS",
12262#endif
12263#ifdef SQLITE_INT64_TYPE
12264  "INT64_TYPE",
12265#endif
12266#ifdef SQLITE_LOCK_TRACE
12267  "LOCK_TRACE",
12268#endif
12269#ifdef SQLITE_MAX_SCHEMA_RETRY
12270  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
12271#endif
12272#ifdef SQLITE_MEMDEBUG
12273  "MEMDEBUG",
12274#endif
12275#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
12276  "MIXED_ENDIAN_64BIT_FLOAT",
12277#endif
12278#ifdef SQLITE_NO_SYNC
12279  "NO_SYNC",
12280#endif
12281#ifdef SQLITE_OMIT_ALTERTABLE
12282  "OMIT_ALTERTABLE",
12283#endif
12284#ifdef SQLITE_OMIT_ANALYZE
12285  "OMIT_ANALYZE",
12286#endif
12287#ifdef SQLITE_OMIT_ATTACH
12288  "OMIT_ATTACH",
12289#endif
12290#ifdef SQLITE_OMIT_AUTHORIZATION
12291  "OMIT_AUTHORIZATION",
12292#endif
12293#ifdef SQLITE_OMIT_AUTOINCREMENT
12294  "OMIT_AUTOINCREMENT",
12295#endif
12296#ifdef SQLITE_OMIT_AUTOINIT
12297  "OMIT_AUTOINIT",
12298#endif
12299#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
12300  "OMIT_AUTOMATIC_INDEX",
12301#endif
12302#ifdef SQLITE_OMIT_AUTORESET
12303  "OMIT_AUTORESET",
12304#endif
12305#ifdef SQLITE_OMIT_AUTOVACUUM
12306  "OMIT_AUTOVACUUM",
12307#endif
12308#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
12309  "OMIT_BETWEEN_OPTIMIZATION",
12310#endif
12311#ifdef SQLITE_OMIT_BLOB_LITERAL
12312  "OMIT_BLOB_LITERAL",
12313#endif
12314#ifdef SQLITE_OMIT_BTREECOUNT
12315  "OMIT_BTREECOUNT",
12316#endif
12317#ifdef SQLITE_OMIT_BUILTIN_TEST
12318  "OMIT_BUILTIN_TEST",
12319#endif
12320#ifdef SQLITE_OMIT_CAST
12321  "OMIT_CAST",
12322#endif
12323#ifdef SQLITE_OMIT_CHECK
12324  "OMIT_CHECK",
12325#endif
12326/* // redundant
12327** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
12328**   "OMIT_COMPILEOPTION_DIAGS",
12329** #endif
12330*/
12331#ifdef SQLITE_OMIT_COMPLETE
12332  "OMIT_COMPLETE",
12333#endif
12334#ifdef SQLITE_OMIT_COMPOUND_SELECT
12335  "OMIT_COMPOUND_SELECT",
12336#endif
12337#ifdef SQLITE_OMIT_DATETIME_FUNCS
12338  "OMIT_DATETIME_FUNCS",
12339#endif
12340#ifdef SQLITE_OMIT_DECLTYPE
12341  "OMIT_DECLTYPE",
12342#endif
12343#ifdef SQLITE_OMIT_DEPRECATED
12344  "OMIT_DEPRECATED",
12345#endif
12346#ifdef SQLITE_OMIT_DISKIO
12347  "OMIT_DISKIO",
12348#endif
12349#ifdef SQLITE_OMIT_EXPLAIN
12350  "OMIT_EXPLAIN",
12351#endif
12352#ifdef SQLITE_OMIT_FLAG_PRAGMAS
12353  "OMIT_FLAG_PRAGMAS",
12354#endif
12355#ifdef SQLITE_OMIT_FLOATING_POINT
12356  "OMIT_FLOATING_POINT",
12357#endif
12358#ifdef SQLITE_OMIT_FOREIGN_KEY
12359  "OMIT_FOREIGN_KEY",
12360#endif
12361#ifdef SQLITE_OMIT_GET_TABLE
12362  "OMIT_GET_TABLE",
12363#endif
12364#ifdef SQLITE_OMIT_INCRBLOB
12365  "OMIT_INCRBLOB",
12366#endif
12367#ifdef SQLITE_OMIT_INTEGRITY_CHECK
12368  "OMIT_INTEGRITY_CHECK",
12369#endif
12370#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
12371  "OMIT_LIKE_OPTIMIZATION",
12372#endif
12373#ifdef SQLITE_OMIT_LOAD_EXTENSION
12374  "OMIT_LOAD_EXTENSION",
12375#endif
12376#ifdef SQLITE_OMIT_LOCALTIME
12377  "OMIT_LOCALTIME",
12378#endif
12379#ifdef SQLITE_OMIT_LOOKASIDE
12380  "OMIT_LOOKASIDE",
12381#endif
12382#ifdef SQLITE_OMIT_MEMORYDB
12383  "OMIT_MEMORYDB",
12384#endif
12385#ifdef SQLITE_OMIT_MERGE_SORT
12386  "OMIT_MERGE_SORT",
12387#endif
12388#ifdef SQLITE_OMIT_OR_OPTIMIZATION
12389  "OMIT_OR_OPTIMIZATION",
12390#endif
12391#ifdef SQLITE_OMIT_PAGER_PRAGMAS
12392  "OMIT_PAGER_PRAGMAS",
12393#endif
12394#ifdef SQLITE_OMIT_PRAGMA
12395  "OMIT_PRAGMA",
12396#endif
12397#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
12398  "OMIT_PROGRESS_CALLBACK",
12399#endif
12400#ifdef SQLITE_OMIT_QUICKBALANCE
12401  "OMIT_QUICKBALANCE",
12402#endif
12403#ifdef SQLITE_OMIT_REINDEX
12404  "OMIT_REINDEX",
12405#endif
12406#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
12407  "OMIT_SCHEMA_PRAGMAS",
12408#endif
12409#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
12410  "OMIT_SCHEMA_VERSION_PRAGMAS",
12411#endif
12412#ifdef SQLITE_OMIT_SHARED_CACHE
12413  "OMIT_SHARED_CACHE",
12414#endif
12415#ifdef SQLITE_OMIT_SUBQUERY
12416  "OMIT_SUBQUERY",
12417#endif
12418#ifdef SQLITE_OMIT_TCL_VARIABLE
12419  "OMIT_TCL_VARIABLE",
12420#endif
12421#ifdef SQLITE_OMIT_TEMPDB
12422  "OMIT_TEMPDB",
12423#endif
12424#ifdef SQLITE_OMIT_TRACE
12425  "OMIT_TRACE",
12426#endif
12427#ifdef SQLITE_OMIT_TRIGGER
12428  "OMIT_TRIGGER",
12429#endif
12430#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
12431  "OMIT_TRUNCATE_OPTIMIZATION",
12432#endif
12433#ifdef SQLITE_OMIT_UTF16
12434  "OMIT_UTF16",
12435#endif
12436#ifdef SQLITE_OMIT_VACUUM
12437  "OMIT_VACUUM",
12438#endif
12439#ifdef SQLITE_OMIT_VIEW
12440  "OMIT_VIEW",
12441#endif
12442#ifdef SQLITE_OMIT_VIRTUALTABLE
12443  "OMIT_VIRTUALTABLE",
12444#endif
12445#ifdef SQLITE_OMIT_WAL
12446  "OMIT_WAL",
12447#endif
12448#ifdef SQLITE_OMIT_WSD
12449  "OMIT_WSD",
12450#endif
12451#ifdef SQLITE_OMIT_XFER_OPT
12452  "OMIT_XFER_OPT",
12453#endif
12454#ifdef SQLITE_PAGECACHE_BLOCKALLOC
12455  "PAGECACHE_BLOCKALLOC",
12456#endif
12457#ifdef SQLITE_PERFORMANCE_TRACE
12458  "PERFORMANCE_TRACE",
12459#endif
12460#ifdef SQLITE_PROXY_DEBUG
12461  "PROXY_DEBUG",
12462#endif
12463#ifdef SQLITE_SECURE_DELETE
12464  "SECURE_DELETE",
12465#endif
12466#ifdef SQLITE_SMALL_STACK
12467  "SMALL_STACK",
12468#endif
12469#ifdef SQLITE_SOUNDEX
12470  "SOUNDEX",
12471#endif
12472#ifdef SQLITE_TCL
12473  "TCL",
12474#endif
12475#ifdef SQLITE_TEMP_STORE
12476  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
12477#endif
12478#ifdef SQLITE_TEST
12479  "TEST",
12480#endif
12481#ifdef SQLITE_THREADSAFE
12482  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
12483#endif
12484#ifdef SQLITE_USE_ALLOCA
12485  "USE_ALLOCA",
12486#endif
12487#ifdef SQLITE_ZERO_MALLOC
12488  "ZERO_MALLOC"
12489#endif
12490};
12491
12492/*
12493** Given the name of a compile-time option, return true if that option
12494** was used and false if not.
12495**
12496** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
12497** is not required for a match.
12498*/
12499SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
12500  int i, n;
12501  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
12502  n = sqlite3Strlen30(zOptName);
12503
12504  /* Since ArraySize(azCompileOpt) is normally in single digits, a
12505  ** linear search is adequate.  No need for a binary search. */
12506  for(i=0; i<ArraySize(azCompileOpt); i++){
12507    if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
12508       && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
12509  }
12510  return 0;
12511}
12512
12513/*
12514** Return the N-th compile-time option string.  If N is out of range,
12515** return a NULL pointer.
12516*/
12517SQLITE_API const char *sqlite3_compileoption_get(int N){
12518  if( N>=0 && N<ArraySize(azCompileOpt) ){
12519    return azCompileOpt[N];
12520  }
12521  return 0;
12522}
12523
12524#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
12525
12526/************** End of ctime.c ***********************************************/
12527/************** Begin file status.c ******************************************/
12528/*
12529** 2008 June 18
12530**
12531** The author disclaims copyright to this source code.  In place of
12532** a legal notice, here is a blessing:
12533**
12534**    May you do good and not evil.
12535**    May you find forgiveness for yourself and forgive others.
12536**    May you share freely, never taking more than you give.
12537**
12538*************************************************************************
12539**
12540** This module implements the sqlite3_status() interface and related
12541** functionality.
12542*/
12543/************** Include vdbeInt.h in the middle of status.c ******************/
12544/************** Begin file vdbeInt.h *****************************************/
12545/*
12546** 2003 September 6
12547**
12548** The author disclaims copyright to this source code.  In place of
12549** a legal notice, here is a blessing:
12550**
12551**    May you do good and not evil.
12552**    May you find forgiveness for yourself and forgive others.
12553**    May you share freely, never taking more than you give.
12554**
12555*************************************************************************
12556** This is the header file for information that is private to the
12557** VDBE.  This information used to all be at the top of the single
12558** source code file "vdbe.c".  When that file became too big (over
12559** 6000 lines long) it was split up into several smaller files and
12560** this header information was factored out.
12561*/
12562#ifndef _VDBEINT_H_
12563#define _VDBEINT_H_
12564
12565/*
12566** SQL is translated into a sequence of instructions to be
12567** executed by a virtual machine.  Each instruction is an instance
12568** of the following structure.
12569*/
12570typedef struct VdbeOp Op;
12571
12572/*
12573** Boolean values
12574*/
12575typedef unsigned char Bool;
12576
12577/* Opaque type used by code in vdbesort.c */
12578typedef struct VdbeSorter VdbeSorter;
12579
12580/*
12581** A cursor is a pointer into a single BTree within a database file.
12582** The cursor can seek to a BTree entry with a particular key, or
12583** loop over all entries of the Btree.  You can also insert new BTree
12584** entries or retrieve the key or data from the entry that the cursor
12585** is currently pointing to.
12586**
12587** Every cursor that the virtual machine has open is represented by an
12588** instance of the following structure.
12589*/
12590struct VdbeCursor {
12591  BtCursor *pCursor;    /* The cursor structure of the backend */
12592  Btree *pBt;           /* Separate file holding temporary table */
12593  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
12594  int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
12595  int pseudoTableReg;   /* Register holding pseudotable content. */
12596  int nField;           /* Number of fields in the header */
12597  Bool zeroed;          /* True if zeroed out and ready for reuse */
12598  Bool rowidIsValid;    /* True if lastRowid is valid */
12599  Bool atFirst;         /* True if pointing to first entry */
12600  Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
12601  Bool nullRow;         /* True if pointing to a row with no data */
12602  Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
12603  Bool isTable;         /* True if a table requiring integer keys */
12604  Bool isIndex;         /* True if an index containing keys only - no data */
12605  Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
12606  Bool isSorter;        /* True if a new-style sorter */
12607  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
12608  const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
12609  i64 seqCount;         /* Sequence counter */
12610  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
12611  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
12612  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
12613
12614  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12615  ** OP_IsUnique opcode on this cursor. */
12616  int seekResult;
12617
12618  /* Cached information about the header for the data record that the
12619  ** cursor is currently pointing to.  Only valid if cacheStatus matches
12620  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
12621  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
12622  ** the cache is out of date.
12623  **
12624  ** aRow might point to (ephemeral) data for the current row, or it might
12625  ** be NULL.
12626  */
12627  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
12628  int payloadSize;      /* Total number of bytes in the record */
12629  u32 *aType;           /* Type values for all entries in the record */
12630  u32 *aOffset;         /* Cached offsets to the start of each columns data */
12631  u8 *aRow;             /* Data for the current row, if all on one page */
12632};
12633typedef struct VdbeCursor VdbeCursor;
12634
12635/*
12636** When a sub-program is executed (OP_Program), a structure of this type
12637** is allocated to store the current value of the program counter, as
12638** well as the current memory cell array and various other frame specific
12639** values stored in the Vdbe struct. When the sub-program is finished,
12640** these values are copied back to the Vdbe from the VdbeFrame structure,
12641** restoring the state of the VM to as it was before the sub-program
12642** began executing.
12643**
12644** The memory for a VdbeFrame object is allocated and managed by a memory
12645** cell in the parent (calling) frame. When the memory cell is deleted or
12646** overwritten, the VdbeFrame object is not freed immediately. Instead, it
12647** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
12648** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
12649** this instead of deleting the VdbeFrame immediately is to avoid recursive
12650** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
12651** child frame are released.
12652**
12653** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
12654** set to NULL if the currently executing frame is the main program.
12655*/
12656typedef struct VdbeFrame VdbeFrame;
12657struct VdbeFrame {
12658  Vdbe *v;                /* VM this frame belongs to */
12659  int pc;                 /* Program Counter in parent (calling) frame */
12660  Op *aOp;                /* Program instructions for parent frame */
12661  int nOp;                /* Size of aOp array */
12662  Mem *aMem;              /* Array of memory cells for parent frame */
12663  int nMem;               /* Number of entries in aMem */
12664  VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
12665  u16 nCursor;            /* Number of entries in apCsr */
12666  void *token;            /* Copy of SubProgram.token */
12667  int nChildMem;          /* Number of memory cells for child frame */
12668  int nChildCsr;          /* Number of cursors for child frame */
12669  i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
12670  int nChange;            /* Statement changes (Vdbe.nChanges)     */
12671  VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
12672};
12673
12674#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
12675
12676/*
12677** A value for VdbeCursor.cacheValid that means the cache is always invalid.
12678*/
12679#define CACHE_STALE 0
12680
12681/*
12682** Internally, the vdbe manipulates nearly all SQL values as Mem
12683** structures. Each Mem struct may cache multiple representations (string,
12684** integer etc.) of the same value.
12685*/
12686struct Mem {
12687  sqlite3 *db;        /* The associated database connection */
12688  char *z;            /* String or BLOB value */
12689  double r;           /* Real value */
12690  union {
12691    i64 i;              /* Integer value used when MEM_Int is set in flags */
12692    int nZero;          /* Used when bit MEM_Zero is set in flags */
12693    FuncDef *pDef;      /* Used only when flags==MEM_Agg */
12694    RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
12695    VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
12696  } u;
12697  int n;              /* Number of characters in string value, excluding '\0' */
12698  u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
12699  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
12700  u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
12701#ifdef SQLITE_DEBUG
12702  Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
12703  void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
12704#endif
12705  void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
12706  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
12707};
12708
12709/* One or more of the following flags are set to indicate the validOK
12710** representations of the value stored in the Mem struct.
12711**
12712** If the MEM_Null flag is set, then the value is an SQL NULL value.
12713** No other flags may be set in this case.
12714**
12715** If the MEM_Str flag is set then Mem.z points at a string representation.
12716** Usually this is encoded in the same unicode encoding as the main
12717** database (see below for exceptions). If the MEM_Term flag is also
12718** set, then the string is nul terminated. The MEM_Int and MEM_Real
12719** flags may coexist with the MEM_Str flag.
12720*/
12721#define MEM_Null      0x0001   /* Value is NULL */
12722#define MEM_Str       0x0002   /* Value is a string */
12723#define MEM_Int       0x0004   /* Value is an integer */
12724#define MEM_Real      0x0008   /* Value is a real number */
12725#define MEM_Blob      0x0010   /* Value is a BLOB */
12726#define MEM_RowSet    0x0020   /* Value is a RowSet object */
12727#define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
12728#define MEM_Invalid   0x0080   /* Value is undefined */
12729#define MEM_TypeMask  0x00ff   /* Mask of type bits */
12730
12731/* Whenever Mem contains a valid string or blob representation, one of
12732** the following flags must be set to determine the memory management
12733** policy for Mem.z.  The MEM_Term flag tells us whether or not the
12734** string is \000 or \u0000 terminated
12735*/
12736#define MEM_Term      0x0200   /* String rep is nul terminated */
12737#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
12738#define MEM_Static    0x0800   /* Mem.z points to a static string */
12739#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
12740#define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
12741#define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
12742#ifdef SQLITE_OMIT_INCRBLOB
12743  #undef MEM_Zero
12744  #define MEM_Zero 0x0000
12745#endif
12746
12747/*
12748** Clear any existing type flags from a Mem and replace them with f
12749*/
12750#define MemSetTypeFlag(p, f) \
12751   ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
12752
12753/*
12754** Return true if a memory cell is not marked as invalid.  This macro
12755** is for use inside assert() statements only.
12756*/
12757#ifdef SQLITE_DEBUG
12758#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
12759#endif
12760
12761
12762/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
12763** additional information about auxiliary information bound to arguments
12764** of the function.  This is used to implement the sqlite3_get_auxdata()
12765** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
12766** that can be associated with a constant argument to a function.  This
12767** allows functions such as "regexp" to compile their constant regular
12768** expression argument once and reused the compiled code for multiple
12769** invocations.
12770*/
12771struct VdbeFunc {
12772  FuncDef *pFunc;               /* The definition of the function */
12773  int nAux;                     /* Number of entries allocated for apAux[] */
12774  struct AuxData {
12775    void *pAux;                   /* Aux data for the i-th argument */
12776    void (*xDelete)(void *);      /* Destructor for the aux data */
12777  } apAux[1];                   /* One slot for each function argument */
12778};
12779
12780/*
12781** The "context" argument for a installable function.  A pointer to an
12782** instance of this structure is the first argument to the routines used
12783** implement the SQL functions.
12784**
12785** There is a typedef for this structure in sqlite.h.  So all routines,
12786** even the public interface to SQLite, can use a pointer to this structure.
12787** But this file is the only place where the internal details of this
12788** structure are known.
12789**
12790** This structure is defined inside of vdbeInt.h because it uses substructures
12791** (Mem) which are only defined there.
12792*/
12793struct sqlite3_context {
12794  FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
12795  VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
12796  Mem s;                /* The return value is stored here */
12797  Mem *pMem;            /* Memory cell used to store aggregate context */
12798  int isError;          /* Error code returned by the function. */
12799  CollSeq *pColl;       /* Collating sequence */
12800};
12801
12802/*
12803** An instance of the virtual machine.  This structure contains the complete
12804** state of the virtual machine.
12805**
12806** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
12807** is really a pointer to an instance of this structure.
12808**
12809** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
12810** any virtual table method invocations made by the vdbe program. It is
12811** set to 2 for xDestroy method calls and 1 for all other methods. This
12812** variable is used for two purposes: to allow xDestroy methods to execute
12813** "DROP TABLE" statements and to prevent some nasty side effects of
12814** malloc failure when SQLite is invoked recursively by a virtual table
12815** method function.
12816*/
12817struct Vdbe {
12818  sqlite3 *db;            /* The database connection that owns this statement */
12819  Op *aOp;                /* Space to hold the virtual machine's program */
12820  Mem *aMem;              /* The memory locations */
12821  Mem **apArg;            /* Arguments to currently executing user function */
12822  Mem *aColName;          /* Column names to return */
12823  Mem *pResultSet;        /* Pointer to an array of results */
12824  int nMem;               /* Number of memory locations currently allocated */
12825  int nOp;                /* Number of instructions in the program */
12826  int nOpAlloc;           /* Number of slots allocated for aOp[] */
12827  int nLabel;             /* Number of labels used */
12828  int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
12829  int *aLabel;            /* Space to hold the labels */
12830  u16 nResColumn;         /* Number of columns in one row of the result set */
12831  u16 nCursor;            /* Number of slots in apCsr[] */
12832  u32 magic;              /* Magic number for sanity checking */
12833  char *zErrMsg;          /* Error message written here */
12834  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
12835  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
12836  Mem *aVar;              /* Values for the OP_Variable opcode. */
12837  char **azVar;           /* Name of variables */
12838  ynVar nVar;             /* Number of entries in aVar[] */
12839  ynVar nzVar;            /* Number of entries in azVar[] */
12840  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
12841  int pc;                 /* The program counter */
12842  int rc;                 /* Value to return */
12843  u8 errorAction;         /* Recovery action to do in case of an error */
12844  u8 explain;             /* True if EXPLAIN present on SQL command */
12845  u8 changeCntOn;         /* True to update the change-counter */
12846  u8 expired;             /* True if the VM needs to be recompiled */
12847  u8 runOnlyOnce;         /* Automatically expire on reset */
12848  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
12849  u8 inVtabMethod;        /* See comments above */
12850  u8 usesStmtJournal;     /* True if uses a statement journal */
12851  u8 readOnly;            /* True for read-only statements */
12852  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
12853  int nChange;            /* Number of db changes made since last reset */
12854  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
12855  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
12856  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
12857  int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
12858#ifndef SQLITE_OMIT_TRACE
12859  i64 startTime;          /* Time when query started - used for profiling */
12860#endif
12861  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
12862  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
12863  char *zSql;             /* Text of the SQL statement that generated this */
12864  void *pFree;            /* Free this when deleting the vdbe */
12865#ifdef SQLITE_DEBUG
12866  FILE *trace;            /* Write an execution trace here, if not NULL */
12867#endif
12868  VdbeFrame *pFrame;      /* Parent frame */
12869  VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
12870  int nFrame;             /* Number of frames in pFrame list */
12871  u32 expmask;            /* Binding to these vars invalidates VM */
12872  SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
12873};
12874
12875/*
12876** The following are allowed values for Vdbe.magic
12877*/
12878#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
12879#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
12880#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
12881#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
12882
12883/*
12884** Function prototypes
12885*/
12886SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
12887void sqliteVdbePopStack(Vdbe*,int);
12888SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
12889#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
12890SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
12891#endif
12892SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
12893SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
12894SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
12895SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
12896SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
12897
12898int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
12899SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
12900SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
12901SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
12902SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
12903SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
12904SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
12905SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
12906SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
12907SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
12908SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
12909SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
12910SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
12911SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
12912SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
12913#ifdef SQLITE_OMIT_FLOATING_POINT
12914# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
12915#else
12916SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
12917#endif
12918SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
12919SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
12920SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
12921SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
12922SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
12923SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
12924SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
12925SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
12926SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
12927SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12928SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12929SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12930SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12931SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12932#define MemReleaseExt(X)  \
12933  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12934    sqlite3VdbeMemReleaseExternal(X);
12935SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12936SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12937SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12938SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12939SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12940SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12941SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12942
12943#ifdef SQLITE_OMIT_MERGE_SORT
12944# define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
12945# define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
12946# define sqlite3VdbeSorterClose(Y,Z)
12947# define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
12948# define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
12949# define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
12950# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
12951#else
12952SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
12953SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
12954SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
12955SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
12956SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
12957SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
12958SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
12959#endif
12960
12961#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12962SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
12963SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
12964#else
12965# define sqlite3VdbeEnter(X)
12966# define sqlite3VdbeLeave(X)
12967#endif
12968
12969#ifdef SQLITE_DEBUG
12970SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
12971#endif
12972
12973#ifndef SQLITE_OMIT_FOREIGN_KEY
12974SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
12975#else
12976# define sqlite3VdbeCheckFk(p,i) 0
12977#endif
12978
12979SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
12980#ifdef SQLITE_DEBUG
12981SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
12982SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
12983#endif
12984SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
12985
12986#ifndef SQLITE_OMIT_INCRBLOB
12987SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
12988#else
12989  #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
12990#endif
12991
12992#endif /* !defined(_VDBEINT_H_) */
12993
12994/************** End of vdbeInt.h *********************************************/
12995/************** Continuing where we left off in status.c *********************/
12996
12997/*
12998** Variables in which to record status information.
12999*/
13000typedef struct sqlite3StatType sqlite3StatType;
13001static SQLITE_WSD struct sqlite3StatType {
13002  int nowValue[10];         /* Current value */
13003  int mxValue[10];          /* Maximum value */
13004} sqlite3Stat = { {0,}, {0,} };
13005
13006
13007/* The "wsdStat" macro will resolve to the status information
13008** state vector.  If writable static data is unsupported on the target,
13009** we have to locate the state vector at run-time.  In the more common
13010** case where writable static data is supported, wsdStat can refer directly
13011** to the "sqlite3Stat" state vector declared above.
13012*/
13013#ifdef SQLITE_OMIT_WSD
13014# define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
13015# define wsdStat x[0]
13016#else
13017# define wsdStatInit
13018# define wsdStat sqlite3Stat
13019#endif
13020
13021/*
13022** Return the current value of a status parameter.
13023*/
13024SQLITE_PRIVATE int sqlite3StatusValue(int op){
13025  wsdStatInit;
13026  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
13027  return wsdStat.nowValue[op];
13028}
13029
13030/*
13031** Add N to the value of a status record.  It is assumed that the
13032** caller holds appropriate locks.
13033*/
13034SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
13035  wsdStatInit;
13036  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
13037  wsdStat.nowValue[op] += N;
13038  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
13039    wsdStat.mxValue[op] = wsdStat.nowValue[op];
13040  }
13041}
13042
13043/*
13044** Set the value of a status to X.
13045*/
13046SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
13047  wsdStatInit;
13048  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
13049  wsdStat.nowValue[op] = X;
13050  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
13051    wsdStat.mxValue[op] = wsdStat.nowValue[op];
13052  }
13053}
13054
13055/*
13056** Query status information.
13057**
13058** This implementation assumes that reading or writing an aligned
13059** 32-bit integer is an atomic operation.  If that assumption is not true,
13060** then this routine is not threadsafe.
13061*/
13062SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
13063  wsdStatInit;
13064  if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
13065    return SQLITE_MISUSE_BKPT;
13066  }
13067  *pCurrent = wsdStat.nowValue[op];
13068  *pHighwater = wsdStat.mxValue[op];
13069  if( resetFlag ){
13070    wsdStat.mxValue[op] = wsdStat.nowValue[op];
13071  }
13072  return SQLITE_OK;
13073}
13074
13075/*
13076** Query status information for a single database connection
13077*/
13078SQLITE_API int sqlite3_db_status(
13079  sqlite3 *db,          /* The database connection whose status is desired */
13080  int op,               /* Status verb */
13081  int *pCurrent,        /* Write current value here */
13082  int *pHighwater,      /* Write high-water mark here */
13083  int resetFlag         /* Reset high-water mark if true */
13084){
13085  int rc = SQLITE_OK;   /* Return code */
13086  sqlite3_mutex_enter(db->mutex);
13087  switch( op ){
13088    case SQLITE_DBSTATUS_LOOKASIDE_USED: {
13089      *pCurrent = db->lookaside.nOut;
13090      *pHighwater = db->lookaside.mxOut;
13091      if( resetFlag ){
13092        db->lookaside.mxOut = db->lookaside.nOut;
13093      }
13094      break;
13095    }
13096
13097    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
13098    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
13099    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
13100      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
13101      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
13102      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
13103      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
13104      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
13105      *pCurrent = 0;
13106      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
13107      if( resetFlag ){
13108        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
13109      }
13110      break;
13111    }
13112
13113    /*
13114    ** Return an approximation for the amount of memory currently used
13115    ** by all pagers associated with the given database connection.  The
13116    ** highwater mark is meaningless and is returned as zero.
13117    */
13118    case SQLITE_DBSTATUS_CACHE_USED: {
13119      int totalUsed = 0;
13120      int i;
13121      sqlite3BtreeEnterAll(db);
13122      for(i=0; i<db->nDb; i++){
13123        Btree *pBt = db->aDb[i].pBt;
13124        if( pBt ){
13125          Pager *pPager = sqlite3BtreePager(pBt);
13126          totalUsed += sqlite3PagerMemUsed(pPager);
13127        }
13128      }
13129      sqlite3BtreeLeaveAll(db);
13130      *pCurrent = totalUsed;
13131      *pHighwater = 0;
13132      break;
13133    }
13134
13135    /*
13136    ** *pCurrent gets an accurate estimate of the amount of memory used
13137    ** to store the schema for all databases (main, temp, and any ATTACHed
13138    ** databases.  *pHighwater is set to zero.
13139    */
13140    case SQLITE_DBSTATUS_SCHEMA_USED: {
13141      int i;                      /* Used to iterate through schemas */
13142      int nByte = 0;              /* Used to accumulate return value */
13143
13144      sqlite3BtreeEnterAll(db);
13145      db->pnBytesFreed = &nByte;
13146      for(i=0; i<db->nDb; i++){
13147        Schema *pSchema = db->aDb[i].pSchema;
13148        if( ALWAYS(pSchema!=0) ){
13149          HashElem *p;
13150
13151          nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
13152              pSchema->tblHash.count
13153            + pSchema->trigHash.count
13154            + pSchema->idxHash.count
13155            + pSchema->fkeyHash.count
13156          );
13157          nByte += sqlite3MallocSize(pSchema->tblHash.ht);
13158          nByte += sqlite3MallocSize(pSchema->trigHash.ht);
13159          nByte += sqlite3MallocSize(pSchema->idxHash.ht);
13160          nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
13161
13162          for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
13163            sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
13164          }
13165          for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
13166            sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
13167          }
13168        }
13169      }
13170      db->pnBytesFreed = 0;
13171      sqlite3BtreeLeaveAll(db);
13172
13173      *pHighwater = 0;
13174      *pCurrent = nByte;
13175      break;
13176    }
13177
13178    /*
13179    ** *pCurrent gets an accurate estimate of the amount of memory used
13180    ** to store all prepared statements.
13181    ** *pHighwater is set to zero.
13182    */
13183    case SQLITE_DBSTATUS_STMT_USED: {
13184      struct Vdbe *pVdbe;         /* Used to iterate through VMs */
13185      int nByte = 0;              /* Used to accumulate return value */
13186
13187      db->pnBytesFreed = &nByte;
13188      for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
13189        sqlite3VdbeDeleteObject(db, pVdbe);
13190      }
13191      db->pnBytesFreed = 0;
13192
13193      *pHighwater = 0;
13194      *pCurrent = nByte;
13195
13196      break;
13197    }
13198
13199    default: {
13200      rc = SQLITE_ERROR;
13201    }
13202  }
13203  sqlite3_mutex_leave(db->mutex);
13204  return rc;
13205}
13206
13207/************** End of status.c **********************************************/
13208/************** Begin file date.c ********************************************/
13209/*
13210** 2003 October 31
13211**
13212** The author disclaims copyright to this source code.  In place of
13213** a legal notice, here is a blessing:
13214**
13215**    May you do good and not evil.
13216**    May you find forgiveness for yourself and forgive others.
13217**    May you share freely, never taking more than you give.
13218**
13219*************************************************************************
13220** This file contains the C functions that implement date and time
13221** functions for SQLite.
13222**
13223** There is only one exported symbol in this file - the function
13224** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
13225** All other code has file scope.
13226**
13227** SQLite processes all times and dates as Julian Day numbers.  The
13228** dates and times are stored as the number of days since noon
13229** in Greenwich on November 24, 4714 B.C. according to the Gregorian
13230** calendar system.
13231**
13232** 1970-01-01 00:00:00 is JD 2440587.5
13233** 2000-01-01 00:00:00 is JD 2451544.5
13234**
13235** This implemention requires years to be expressed as a 4-digit number
13236** which means that only dates between 0000-01-01 and 9999-12-31 can
13237** be represented, even though julian day numbers allow a much wider
13238** range of dates.
13239**
13240** The Gregorian calendar system is used for all dates and times,
13241** even those that predate the Gregorian calendar.  Historians usually
13242** use the Julian calendar for dates prior to 1582-10-15 and for some
13243** dates afterwards, depending on locale.  Beware of this difference.
13244**
13245** The conversion algorithms are implemented based on descriptions
13246** in the following text:
13247**
13248**      Jean Meeus
13249**      Astronomical Algorithms, 2nd Edition, 1998
13250**      ISBM 0-943396-61-1
13251**      Willmann-Bell, Inc
13252**      Richmond, Virginia (USA)
13253*/
13254/* #include <stdlib.h> */
13255/* #include <assert.h> */
13256#include <time.h>
13257
13258#ifndef SQLITE_OMIT_DATETIME_FUNCS
13259
13260
13261/*
13262** A structure for holding a single date and time.
13263*/
13264typedef struct DateTime DateTime;
13265struct DateTime {
13266  sqlite3_int64 iJD; /* The julian day number times 86400000 */
13267  int Y, M, D;       /* Year, month, and day */
13268  int h, m;          /* Hour and minutes */
13269  int tz;            /* Timezone offset in minutes */
13270  double s;          /* Seconds */
13271  char validYMD;     /* True (1) if Y,M,D are valid */
13272  char validHMS;     /* True (1) if h,m,s are valid */
13273  char validJD;      /* True (1) if iJD is valid */
13274  char validTZ;      /* True (1) if tz is valid */
13275};
13276
13277
13278/*
13279** Convert zDate into one or more integers.  Additional arguments
13280** come in groups of 5 as follows:
13281**
13282**       N       number of digits in the integer
13283**       min     minimum allowed value of the integer
13284**       max     maximum allowed value of the integer
13285**       nextC   first character after the integer
13286**       pVal    where to write the integers value.
13287**
13288** Conversions continue until one with nextC==0 is encountered.
13289** The function returns the number of successful conversions.
13290*/
13291static int getDigits(const char *zDate, ...){
13292  va_list ap;
13293  int val;
13294  int N;
13295  int min;
13296  int max;
13297  int nextC;
13298  int *pVal;
13299  int cnt = 0;
13300  va_start(ap, zDate);
13301  do{
13302    N = va_arg(ap, int);
13303    min = va_arg(ap, int);
13304    max = va_arg(ap, int);
13305    nextC = va_arg(ap, int);
13306    pVal = va_arg(ap, int*);
13307    val = 0;
13308    while( N-- ){
13309      if( !sqlite3Isdigit(*zDate) ){
13310        goto end_getDigits;
13311      }
13312      val = val*10 + *zDate - '0';
13313      zDate++;
13314    }
13315    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
13316      goto end_getDigits;
13317    }
13318    *pVal = val;
13319    zDate++;
13320    cnt++;
13321  }while( nextC );
13322end_getDigits:
13323  va_end(ap);
13324  return cnt;
13325}
13326
13327/*
13328** Parse a timezone extension on the end of a date-time.
13329** The extension is of the form:
13330**
13331**        (+/-)HH:MM
13332**
13333** Or the "zulu" notation:
13334**
13335**        Z
13336**
13337** If the parse is successful, write the number of minutes
13338** of change in p->tz and return 0.  If a parser error occurs,
13339** return non-zero.
13340**
13341** A missing specifier is not considered an error.
13342*/
13343static int parseTimezone(const char *zDate, DateTime *p){
13344  int sgn = 0;
13345  int nHr, nMn;
13346  int c;
13347  while( sqlite3Isspace(*zDate) ){ zDate++; }
13348  p->tz = 0;
13349  c = *zDate;
13350  if( c=='-' ){
13351    sgn = -1;
13352  }else if( c=='+' ){
13353    sgn = +1;
13354  }else if( c=='Z' || c=='z' ){
13355    zDate++;
13356    goto zulu_time;
13357  }else{
13358    return c!=0;
13359  }
13360  zDate++;
13361  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
13362    return 1;
13363  }
13364  zDate += 5;
13365  p->tz = sgn*(nMn + nHr*60);
13366zulu_time:
13367  while( sqlite3Isspace(*zDate) ){ zDate++; }
13368  return *zDate!=0;
13369}
13370
13371/*
13372** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
13373** The HH, MM, and SS must each be exactly 2 digits.  The
13374** fractional seconds FFFF can be one or more digits.
13375**
13376** Return 1 if there is a parsing error and 0 on success.
13377*/
13378static int parseHhMmSs(const char *zDate, DateTime *p){
13379  int h, m, s;
13380  double ms = 0.0;
13381  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
13382    return 1;
13383  }
13384  zDate += 5;
13385  if( *zDate==':' ){
13386    zDate++;
13387    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
13388      return 1;
13389    }
13390    zDate += 2;
13391    if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
13392      double rScale = 1.0;
13393      zDate++;
13394      while( sqlite3Isdigit(*zDate) ){
13395        ms = ms*10.0 + *zDate - '0';
13396        rScale *= 10.0;
13397        zDate++;
13398      }
13399      ms /= rScale;
13400    }
13401  }else{
13402    s = 0;
13403  }
13404  p->validJD = 0;
13405  p->validHMS = 1;
13406  p->h = h;
13407  p->m = m;
13408  p->s = s + ms;
13409  if( parseTimezone(zDate, p) ) return 1;
13410  p->validTZ = (p->tz!=0)?1:0;
13411  return 0;
13412}
13413
13414/*
13415** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
13416** that the YYYY-MM-DD is according to the Gregorian calendar.
13417**
13418** Reference:  Meeus page 61
13419*/
13420static void computeJD(DateTime *p){
13421  int Y, M, D, A, B, X1, X2;
13422
13423  if( p->validJD ) return;
13424  if( p->validYMD ){
13425    Y = p->Y;
13426    M = p->M;
13427    D = p->D;
13428  }else{
13429    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
13430    M = 1;
13431    D = 1;
13432  }
13433  if( M<=2 ){
13434    Y--;
13435    M += 12;
13436  }
13437  A = Y/100;
13438  B = 2 - A + (A/4);
13439  X1 = 36525*(Y+4716)/100;
13440  X2 = 306001*(M+1)/10000;
13441  p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
13442  p->validJD = 1;
13443  if( p->validHMS ){
13444    p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
13445    if( p->validTZ ){
13446      p->iJD -= p->tz*60000;
13447      p->validYMD = 0;
13448      p->validHMS = 0;
13449      p->validTZ = 0;
13450    }
13451  }
13452}
13453
13454/*
13455** Parse dates of the form
13456**
13457**     YYYY-MM-DD HH:MM:SS.FFF
13458**     YYYY-MM-DD HH:MM:SS
13459**     YYYY-MM-DD HH:MM
13460**     YYYY-MM-DD
13461**
13462** Write the result into the DateTime structure and return 0
13463** on success and 1 if the input string is not a well-formed
13464** date.
13465*/
13466static int parseYyyyMmDd(const char *zDate, DateTime *p){
13467  int Y, M, D, neg;
13468
13469  if( zDate[0]=='-' ){
13470    zDate++;
13471    neg = 1;
13472  }else{
13473    neg = 0;
13474  }
13475  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
13476    return 1;
13477  }
13478  zDate += 10;
13479  while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
13480  if( parseHhMmSs(zDate, p)==0 ){
13481    /* We got the time */
13482  }else if( *zDate==0 ){
13483    p->validHMS = 0;
13484  }else{
13485    return 1;
13486  }
13487  p->validJD = 0;
13488  p->validYMD = 1;
13489  p->Y = neg ? -Y : Y;
13490  p->M = M;
13491  p->D = D;
13492  if( p->validTZ ){
13493    computeJD(p);
13494  }
13495  return 0;
13496}
13497
13498/*
13499** Set the time to the current time reported by the VFS
13500*/
13501static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13502  sqlite3 *db = sqlite3_context_db_handle(context);
13503  sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
13504  p->validJD = 1;
13505}
13506
13507/*
13508** Attempt to parse the given string into a Julian Day Number.  Return
13509** the number of errors.
13510**
13511** The following are acceptable forms for the input string:
13512**
13513**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
13514**      DDDD.DD
13515**      now
13516**
13517** In the first form, the +/-HH:MM is always optional.  The fractional
13518** seconds extension (the ".FFF") is optional.  The seconds portion
13519** (":SS.FFF") is option.  The year and date can be omitted as long
13520** as there is a time string.  The time string can be omitted as long
13521** as there is a year and date.
13522*/
13523static int parseDateOrTime(
13524  sqlite3_context *context,
13525  const char *zDate,
13526  DateTime *p
13527){
13528  double r;
13529  if( parseYyyyMmDd(zDate,p)==0 ){
13530    return 0;
13531  }else if( parseHhMmSs(zDate, p)==0 ){
13532    return 0;
13533  }else if( sqlite3StrICmp(zDate,"now")==0){
13534    setDateTimeToCurrent(context, p);
13535    return 0;
13536  }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
13537    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
13538    p->validJD = 1;
13539    return 0;
13540  }
13541  return 1;
13542}
13543
13544/*
13545** Compute the Year, Month, and Day from the julian day number.
13546*/
13547static void computeYMD(DateTime *p){
13548  int Z, A, B, C, D, E, X1;
13549  if( p->validYMD ) return;
13550  if( !p->validJD ){
13551    p->Y = 2000;
13552    p->M = 1;
13553    p->D = 1;
13554  }else{
13555    Z = (int)((p->iJD + 43200000)/86400000);
13556    A = (int)((Z - 1867216.25)/36524.25);
13557    A = Z + 1 + A - (A/4);
13558    B = A + 1524;
13559    C = (int)((B - 122.1)/365.25);
13560    D = (36525*C)/100;
13561    E = (int)((B-D)/30.6001);
13562    X1 = (int)(30.6001*E);
13563    p->D = B - D - X1;
13564    p->M = E<14 ? E-1 : E-13;
13565    p->Y = p->M>2 ? C - 4716 : C - 4715;
13566  }
13567  p->validYMD = 1;
13568}
13569
13570/*
13571** Compute the Hour, Minute, and Seconds from the julian day number.
13572*/
13573static void computeHMS(DateTime *p){
13574  int s;
13575  if( p->validHMS ) return;
13576  computeJD(p);
13577  s = (int)((p->iJD + 43200000) % 86400000);
13578  p->s = s/1000.0;
13579  s = (int)p->s;
13580  p->s -= s;
13581  p->h = s/3600;
13582  s -= p->h*3600;
13583  p->m = s/60;
13584  p->s += s - p->m*60;
13585  p->validHMS = 1;
13586}
13587
13588/*
13589** Compute both YMD and HMS
13590*/
13591static void computeYMD_HMS(DateTime *p){
13592  computeYMD(p);
13593  computeHMS(p);
13594}
13595
13596/*
13597** Clear the YMD and HMS and the TZ
13598*/
13599static void clearYMD_HMS_TZ(DateTime *p){
13600  p->validYMD = 0;
13601  p->validHMS = 0;
13602  p->validTZ = 0;
13603}
13604
13605/*
13606** On recent Windows platforms, the localtime_s() function is available
13607** as part of the "Secure CRT". It is essentially equivalent to
13608** localtime_r() available under most POSIX platforms, except that the
13609** order of the parameters is reversed.
13610**
13611** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
13612**
13613** If the user has not indicated to use localtime_r() or localtime_s()
13614** already, check for an MSVC build environment that provides
13615** localtime_s().
13616*/
13617#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
13618     defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
13619#define HAVE_LOCALTIME_S 1
13620#endif
13621
13622#ifndef SQLITE_OMIT_LOCALTIME
13623/*
13624** The following routine implements the rough equivalent of localtime_r()
13625** using whatever operating-system specific localtime facility that
13626** is available.  This routine returns 0 on success and
13627** non-zero on any kind of error.
13628**
13629** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
13630** routine will always fail.
13631*/
13632static int osLocaltime(time_t *t, struct tm *pTm){
13633  int rc;
13634#if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
13635      && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
13636  struct tm *pX;
13637#if SQLITE_THREADSAFE>0
13638  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
13639#endif
13640  sqlite3_mutex_enter(mutex);
13641  pX = localtime(t);
13642#ifndef SQLITE_OMIT_BUILTIN_TEST
13643  if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
13644#endif
13645  if( pX ) *pTm = *pX;
13646  sqlite3_mutex_leave(mutex);
13647  rc = pX==0;
13648#else
13649#ifndef SQLITE_OMIT_BUILTIN_TEST
13650  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
13651#endif
13652#if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
13653  rc = localtime_r(t, pTm)==0;
13654#else
13655  rc = localtime_s(pTm, t);
13656#endif /* HAVE_LOCALTIME_R */
13657#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
13658  return rc;
13659}
13660#endif /* SQLITE_OMIT_LOCALTIME */
13661
13662
13663#ifndef SQLITE_OMIT_LOCALTIME
13664/*
13665** Compute the difference (in milliseconds) between localtime and UTC
13666** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
13667** return this value and set *pRc to SQLITE_OK.
13668**
13669** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
13670** is undefined in this case.
13671*/
13672static sqlite3_int64 localtimeOffset(
13673  DateTime *p,                    /* Date at which to calculate offset */
13674  sqlite3_context *pCtx,          /* Write error here if one occurs */
13675  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
13676){
13677  DateTime x, y;
13678  time_t t;
13679  struct tm sLocal;
13680
13681  /* Initialize the contents of sLocal to avoid a compiler warning. */
13682  memset(&sLocal, 0, sizeof(sLocal));
13683
13684  x = *p;
13685  computeYMD_HMS(&x);
13686  if( x.Y<1971 || x.Y>=2038 ){
13687    x.Y = 2000;
13688    x.M = 1;
13689    x.D = 1;
13690    x.h = 0;
13691    x.m = 0;
13692    x.s = 0.0;
13693  } else {
13694    int s = (int)(x.s + 0.5);
13695    x.s = s;
13696  }
13697  x.tz = 0;
13698  x.validJD = 0;
13699  computeJD(&x);
13700  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
13701  if( osLocaltime(&t, &sLocal) ){
13702    sqlite3_result_error(pCtx, "local time unavailable", -1);
13703    *pRc = SQLITE_ERROR;
13704    return 0;
13705  }
13706  y.Y = sLocal.tm_year + 1900;
13707  y.M = sLocal.tm_mon + 1;
13708  y.D = sLocal.tm_mday;
13709  y.h = sLocal.tm_hour;
13710  y.m = sLocal.tm_min;
13711  y.s = sLocal.tm_sec;
13712  y.validYMD = 1;
13713  y.validHMS = 1;
13714  y.validJD = 0;
13715  y.validTZ = 0;
13716  computeJD(&y);
13717  *pRc = SQLITE_OK;
13718  return y.iJD - x.iJD;
13719}
13720#endif /* SQLITE_OMIT_LOCALTIME */
13721
13722/*
13723** Process a modifier to a date-time stamp.  The modifiers are
13724** as follows:
13725**
13726**     NNN days
13727**     NNN hours
13728**     NNN minutes
13729**     NNN.NNNN seconds
13730**     NNN months
13731**     NNN years
13732**     start of month
13733**     start of year
13734**     start of week
13735**     start of day
13736**     weekday N
13737**     unixepoch
13738**     localtime
13739**     utc
13740**
13741** Return 0 on success and 1 if there is any kind of error. If the error
13742** is in a system call (i.e. localtime()), then an error message is written
13743** to context pCtx. If the error is an unrecognized modifier, no error is
13744** written to pCtx.
13745*/
13746static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
13747  int rc = 1;
13748  int n;
13749  double r;
13750  char *z, zBuf[30];
13751  z = zBuf;
13752  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
13753    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
13754  }
13755  z[n] = 0;
13756  switch( z[0] ){
13757#ifndef SQLITE_OMIT_LOCALTIME
13758    case 'l': {
13759      /*    localtime
13760      **
13761      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
13762      ** show local time.
13763      */
13764      if( strcmp(z, "localtime")==0 ){
13765        computeJD(p);
13766        p->iJD += localtimeOffset(p, pCtx, &rc);
13767        clearYMD_HMS_TZ(p);
13768      }
13769      break;
13770    }
13771#endif
13772    case 'u': {
13773      /*
13774      **    unixepoch
13775      **
13776      ** Treat the current value of p->iJD as the number of
13777      ** seconds since 1970.  Convert to a real julian day number.
13778      */
13779      if( strcmp(z, "unixepoch")==0 && p->validJD ){
13780        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
13781        clearYMD_HMS_TZ(p);
13782        rc = 0;
13783      }
13784#ifndef SQLITE_OMIT_LOCALTIME
13785      else if( strcmp(z, "utc")==0 ){
13786        sqlite3_int64 c1;
13787        computeJD(p);
13788        c1 = localtimeOffset(p, pCtx, &rc);
13789        if( rc==SQLITE_OK ){
13790          p->iJD -= c1;
13791          clearYMD_HMS_TZ(p);
13792          p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
13793        }
13794      }
13795#endif
13796      break;
13797    }
13798    case 'w': {
13799      /*
13800      **    weekday N
13801      **
13802      ** Move the date to the same time on the next occurrence of
13803      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
13804      ** date is already on the appropriate weekday, this is a no-op.
13805      */
13806      if( strncmp(z, "weekday ", 8)==0
13807               && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
13808               && (n=(int)r)==r && n>=0 && r<7 ){
13809        sqlite3_int64 Z;
13810        computeYMD_HMS(p);
13811        p->validTZ = 0;
13812        p->validJD = 0;
13813        computeJD(p);
13814        Z = ((p->iJD + 129600000)/86400000) % 7;
13815        if( Z>n ) Z -= 7;
13816        p->iJD += (n - Z)*86400000;
13817        clearYMD_HMS_TZ(p);
13818        rc = 0;
13819      }
13820      break;
13821    }
13822    case 's': {
13823      /*
13824      **    start of TTTTT
13825      **
13826      ** Move the date backwards to the beginning of the current day,
13827      ** or month or year.
13828      */
13829      if( strncmp(z, "start of ", 9)!=0 ) break;
13830      z += 9;
13831      computeYMD(p);
13832      p->validHMS = 1;
13833      p->h = p->m = 0;
13834      p->s = 0.0;
13835      p->validTZ = 0;
13836      p->validJD = 0;
13837      if( strcmp(z,"month")==0 ){
13838        p->D = 1;
13839        rc = 0;
13840      }else if( strcmp(z,"year")==0 ){
13841        computeYMD(p);
13842        p->M = 1;
13843        p->D = 1;
13844        rc = 0;
13845      }else if( strcmp(z,"day")==0 ){
13846        rc = 0;
13847      }
13848      break;
13849    }
13850    case '+':
13851    case '-':
13852    case '0':
13853    case '1':
13854    case '2':
13855    case '3':
13856    case '4':
13857    case '5':
13858    case '6':
13859    case '7':
13860    case '8':
13861    case '9': {
13862      double rRounder;
13863      for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
13864      if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
13865        rc = 1;
13866        break;
13867      }
13868      if( z[n]==':' ){
13869        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
13870        ** specified number of hours, minutes, seconds, and fractional seconds
13871        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
13872        ** omitted.
13873        */
13874        const char *z2 = z;
13875        DateTime tx;
13876        sqlite3_int64 day;
13877        if( !sqlite3Isdigit(*z2) ) z2++;
13878        memset(&tx, 0, sizeof(tx));
13879        if( parseHhMmSs(z2, &tx) ) break;
13880        computeJD(&tx);
13881        tx.iJD -= 43200000;
13882        day = tx.iJD/86400000;
13883        tx.iJD -= day*86400000;
13884        if( z[0]=='-' ) tx.iJD = -tx.iJD;
13885        computeJD(p);
13886        clearYMD_HMS_TZ(p);
13887        p->iJD += tx.iJD;
13888        rc = 0;
13889        break;
13890      }
13891      z += n;
13892      while( sqlite3Isspace(*z) ) z++;
13893      n = sqlite3Strlen30(z);
13894      if( n>10 || n<3 ) break;
13895      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
13896      computeJD(p);
13897      rc = 0;
13898      rRounder = r<0 ? -0.5 : +0.5;
13899      if( n==3 && strcmp(z,"day")==0 ){
13900        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
13901      }else if( n==4 && strcmp(z,"hour")==0 ){
13902        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
13903      }else if( n==6 && strcmp(z,"minute")==0 ){
13904        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
13905      }else if( n==6 && strcmp(z,"second")==0 ){
13906        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
13907      }else if( n==5 && strcmp(z,"month")==0 ){
13908        int x, y;
13909        computeYMD_HMS(p);
13910        p->M += (int)r;
13911        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
13912        p->Y += x;
13913        p->M -= x*12;
13914        p->validJD = 0;
13915        computeJD(p);
13916        y = (int)r;
13917        if( y!=r ){
13918          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
13919        }
13920      }else if( n==4 && strcmp(z,"year")==0 ){
13921        int y = (int)r;
13922        computeYMD_HMS(p);
13923        p->Y += y;
13924        p->validJD = 0;
13925        computeJD(p);
13926        if( y!=r ){
13927          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
13928        }
13929      }else{
13930        rc = 1;
13931      }
13932      clearYMD_HMS_TZ(p);
13933      break;
13934    }
13935    default: {
13936      break;
13937    }
13938  }
13939  return rc;
13940}
13941
13942/*
13943** Process time function arguments.  argv[0] is a date-time stamp.
13944** argv[1] and following are modifiers.  Parse them all and write
13945** the resulting time into the DateTime structure p.  Return 0
13946** on success and 1 if there are any errors.
13947**
13948** If there are zero parameters (if even argv[0] is undefined)
13949** then assume a default value of "now" for argv[0].
13950*/
13951static int isDate(
13952  sqlite3_context *context,
13953  int argc,
13954  sqlite3_value **argv,
13955  DateTime *p
13956){
13957  int i;
13958  const unsigned char *z;
13959  int eType;
13960  memset(p, 0, sizeof(*p));
13961  if( argc==0 ){
13962    setDateTimeToCurrent(context, p);
13963  }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
13964                   || eType==SQLITE_INTEGER ){
13965    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
13966    p->validJD = 1;
13967  }else{
13968    z = sqlite3_value_text(argv[0]);
13969    if( !z || parseDateOrTime(context, (char*)z, p) ){
13970      return 1;
13971    }
13972  }
13973  for(i=1; i<argc; i++){
13974    z = sqlite3_value_text(argv[i]);
13975    if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
13976  }
13977  return 0;
13978}
13979
13980
13981/*
13982** The following routines implement the various date and time functions
13983** of SQLite.
13984*/
13985
13986/*
13987**    julianday( TIMESTRING, MOD, MOD, ...)
13988**
13989** Return the julian day number of the date specified in the arguments
13990*/
13991static void juliandayFunc(
13992  sqlite3_context *context,
13993  int argc,
13994  sqlite3_value **argv
13995){
13996  DateTime x;
13997  if( isDate(context, argc, argv, &x)==0 ){
13998    computeJD(&x);
13999    sqlite3_result_double(context, x.iJD/86400000.0);
14000  }
14001}
14002
14003/*
14004**    datetime( TIMESTRING, MOD, MOD, ...)
14005**
14006** Return YYYY-MM-DD HH:MM:SS
14007*/
14008static void datetimeFunc(
14009  sqlite3_context *context,
14010  int argc,
14011  sqlite3_value **argv
14012){
14013  DateTime x;
14014  if( isDate(context, argc, argv, &x)==0 ){
14015    char zBuf[100];
14016    computeYMD_HMS(&x);
14017    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
14018                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
14019    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14020  }
14021}
14022
14023/*
14024**    time( TIMESTRING, MOD, MOD, ...)
14025**
14026** Return HH:MM:SS
14027*/
14028static void timeFunc(
14029  sqlite3_context *context,
14030  int argc,
14031  sqlite3_value **argv
14032){
14033  DateTime x;
14034  if( isDate(context, argc, argv, &x)==0 ){
14035    char zBuf[100];
14036    computeHMS(&x);
14037    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
14038    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14039  }
14040}
14041
14042/*
14043**    date( TIMESTRING, MOD, MOD, ...)
14044**
14045** Return YYYY-MM-DD
14046*/
14047static void dateFunc(
14048  sqlite3_context *context,
14049  int argc,
14050  sqlite3_value **argv
14051){
14052  DateTime x;
14053  if( isDate(context, argc, argv, &x)==0 ){
14054    char zBuf[100];
14055    computeYMD(&x);
14056    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
14057    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14058  }
14059}
14060
14061/*
14062**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
14063**
14064** Return a string described by FORMAT.  Conversions as follows:
14065**
14066**   %d  day of month
14067**   %f  ** fractional seconds  SS.SSS
14068**   %H  hour 00-24
14069**   %j  day of year 000-366
14070**   %J  ** Julian day number
14071**   %m  month 01-12
14072**   %M  minute 00-59
14073**   %s  seconds since 1970-01-01
14074**   %S  seconds 00-59
14075**   %w  day of week 0-6  sunday==0
14076**   %W  week of year 00-53
14077**   %Y  year 0000-9999
14078**   %%  %
14079*/
14080static void strftimeFunc(
14081  sqlite3_context *context,
14082  int argc,
14083  sqlite3_value **argv
14084){
14085  DateTime x;
14086  u64 n;
14087  size_t i,j;
14088  char *z;
14089  sqlite3 *db;
14090  const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
14091  char zBuf[100];
14092  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
14093  db = sqlite3_context_db_handle(context);
14094  for(i=0, n=1; zFmt[i]; i++, n++){
14095    if( zFmt[i]=='%' ){
14096      switch( zFmt[i+1] ){
14097        case 'd':
14098        case 'H':
14099        case 'm':
14100        case 'M':
14101        case 'S':
14102        case 'W':
14103          n++;
14104          /* fall thru */
14105        case 'w':
14106        case '%':
14107          break;
14108        case 'f':
14109          n += 8;
14110          break;
14111        case 'j':
14112          n += 3;
14113          break;
14114        case 'Y':
14115          n += 8;
14116          break;
14117        case 's':
14118        case 'J':
14119          n += 50;
14120          break;
14121        default:
14122          return;  /* ERROR.  return a NULL */
14123      }
14124      i++;
14125    }
14126  }
14127  testcase( n==sizeof(zBuf)-1 );
14128  testcase( n==sizeof(zBuf) );
14129  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
14130  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
14131  if( n<sizeof(zBuf) ){
14132    z = zBuf;
14133  }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
14134    sqlite3_result_error_toobig(context);
14135    return;
14136  }else{
14137    z = sqlite3DbMallocRaw(db, (int)n);
14138    if( z==0 ){
14139      sqlite3_result_error_nomem(context);
14140      return;
14141    }
14142  }
14143  computeJD(&x);
14144  computeYMD_HMS(&x);
14145  for(i=j=0; zFmt[i]; i++){
14146    if( zFmt[i]!='%' ){
14147      z[j++] = zFmt[i];
14148    }else{
14149      i++;
14150      switch( zFmt[i] ){
14151        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
14152        case 'f': {
14153          double s = x.s;
14154          if( s>59.999 ) s = 59.999;
14155          sqlite3_snprintf(7, &z[j],"%06.3f", s);
14156          j += sqlite3Strlen30(&z[j]);
14157          break;
14158        }
14159        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
14160        case 'W': /* Fall thru */
14161        case 'j': {
14162          int nDay;             /* Number of days since 1st day of year */
14163          DateTime y = x;
14164          y.validJD = 0;
14165          y.M = 1;
14166          y.D = 1;
14167          computeJD(&y);
14168          nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
14169          if( zFmt[i]=='W' ){
14170            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
14171            wd = (int)(((x.iJD+43200000)/86400000)%7);
14172            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
14173            j += 2;
14174          }else{
14175            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
14176            j += 3;
14177          }
14178          break;
14179        }
14180        case 'J': {
14181          sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
14182          j+=sqlite3Strlen30(&z[j]);
14183          break;
14184        }
14185        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
14186        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
14187        case 's': {
14188          sqlite3_snprintf(30,&z[j],"%lld",
14189                           (i64)(x.iJD/1000 - 21086676*(i64)10000));
14190          j += sqlite3Strlen30(&z[j]);
14191          break;
14192        }
14193        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
14194        case 'w': {
14195          z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
14196          break;
14197        }
14198        case 'Y': {
14199          sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
14200          break;
14201        }
14202        default:   z[j++] = '%'; break;
14203      }
14204    }
14205  }
14206  z[j] = 0;
14207  sqlite3_result_text(context, z, -1,
14208                      z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
14209}
14210
14211/*
14212** current_time()
14213**
14214** This function returns the same value as time('now').
14215*/
14216static void ctimeFunc(
14217  sqlite3_context *context,
14218  int NotUsed,
14219  sqlite3_value **NotUsed2
14220){
14221  UNUSED_PARAMETER2(NotUsed, NotUsed2);
14222  timeFunc(context, 0, 0);
14223}
14224
14225/*
14226** current_date()
14227**
14228** This function returns the same value as date('now').
14229*/
14230static void cdateFunc(
14231  sqlite3_context *context,
14232  int NotUsed,
14233  sqlite3_value **NotUsed2
14234){
14235  UNUSED_PARAMETER2(NotUsed, NotUsed2);
14236  dateFunc(context, 0, 0);
14237}
14238
14239/*
14240** current_timestamp()
14241**
14242** This function returns the same value as datetime('now').
14243*/
14244static void ctimestampFunc(
14245  sqlite3_context *context,
14246  int NotUsed,
14247  sqlite3_value **NotUsed2
14248){
14249  UNUSED_PARAMETER2(NotUsed, NotUsed2);
14250  datetimeFunc(context, 0, 0);
14251}
14252#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
14253
14254#ifdef SQLITE_OMIT_DATETIME_FUNCS
14255/*
14256** If the library is compiled to omit the full-scale date and time
14257** handling (to get a smaller binary), the following minimal version
14258** of the functions current_time(), current_date() and current_timestamp()
14259** are included instead. This is to support column declarations that
14260** include "DEFAULT CURRENT_TIME" etc.
14261**
14262** This function uses the C-library functions time(), gmtime()
14263** and strftime(). The format string to pass to strftime() is supplied
14264** as the user-data for the function.
14265*/
14266static void currentTimeFunc(
14267  sqlite3_context *context,
14268  int argc,
14269  sqlite3_value **argv
14270){
14271  time_t t;
14272  char *zFormat = (char *)sqlite3_user_data(context);
14273  sqlite3 *db;
14274  sqlite3_int64 iT;
14275  char zBuf[20];
14276
14277  UNUSED_PARAMETER(argc);
14278  UNUSED_PARAMETER(argv);
14279
14280  db = sqlite3_context_db_handle(context);
14281  sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
14282  t = iT/1000 - 10000*(sqlite3_int64)21086676;
14283#ifdef HAVE_GMTIME_R
14284  {
14285    struct tm sNow;
14286    gmtime_r(&t, &sNow);
14287    strftime(zBuf, 20, zFormat, &sNow);
14288  }
14289#else
14290  {
14291    struct tm *pTm;
14292    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14293    pTm = gmtime(&t);
14294    strftime(zBuf, 20, zFormat, pTm);
14295    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14296  }
14297#endif
14298
14299  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14300}
14301#endif
14302
14303/*
14304** This function registered all of the above C functions as SQL
14305** functions.  This should be the only routine in this file with
14306** external linkage.
14307*/
14308SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
14309  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
14310#ifndef SQLITE_OMIT_DATETIME_FUNCS
14311    FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
14312    FUNCTION(date,             -1, 0, 0, dateFunc      ),
14313    FUNCTION(time,             -1, 0, 0, timeFunc      ),
14314    FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
14315    FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
14316    FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
14317    FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
14318    FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
14319#else
14320    STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
14321    STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
14322    STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
14323#endif
14324  };
14325  int i;
14326  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
14327  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
14328
14329  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
14330    sqlite3FuncDefInsert(pHash, &aFunc[i]);
14331  }
14332}
14333
14334/************** End of date.c ************************************************/
14335/************** Begin file os.c **********************************************/
14336/*
14337** 2005 November 29
14338**
14339** The author disclaims copyright to this source code.  In place of
14340** a legal notice, here is a blessing:
14341**
14342**    May you do good and not evil.
14343**    May you find forgiveness for yourself and forgive others.
14344**    May you share freely, never taking more than you give.
14345**
14346******************************************************************************
14347**
14348** This file contains OS interface code that is common to all
14349** architectures.
14350*/
14351#define _SQLITE_OS_C_ 1
14352#undef _SQLITE_OS_C_
14353
14354/*
14355** The default SQLite sqlite3_vfs implementations do not allocate
14356** memory (actually, os_unix.c allocates a small amount of memory
14357** from within OsOpen()), but some third-party implementations may.
14358** So we test the effects of a malloc() failing and the sqlite3OsXXX()
14359** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
14360**
14361** The following functions are instrumented for malloc() failure
14362** testing:
14363**
14364**     sqlite3OsOpen()
14365**     sqlite3OsRead()
14366**     sqlite3OsWrite()
14367**     sqlite3OsSync()
14368**     sqlite3OsLock()
14369**
14370*/
14371#if defined(SQLITE_TEST)
14372SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
14373  #define DO_OS_MALLOC_TEST(x)                                       \
14374  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
14375    void *pTstAlloc = sqlite3Malloc(10);                             \
14376    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
14377    sqlite3_free(pTstAlloc);                                         \
14378  }
14379#else
14380  #define DO_OS_MALLOC_TEST(x)
14381#endif
14382
14383/*
14384** The following routines are convenience wrappers around methods
14385** of the sqlite3_file object.  This is mostly just syntactic sugar. All
14386** of this would be completely automatic if SQLite were coded using
14387** C++ instead of plain old C.
14388*/
14389SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
14390  int rc = SQLITE_OK;
14391  if( pId->pMethods ){
14392    rc = pId->pMethods->xClose(pId);
14393    pId->pMethods = 0;
14394  }
14395  return rc;
14396}
14397SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
14398  DO_OS_MALLOC_TEST(id);
14399  return id->pMethods->xRead(id, pBuf, amt, offset);
14400}
14401SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
14402  DO_OS_MALLOC_TEST(id);
14403  return id->pMethods->xWrite(id, pBuf, amt, offset);
14404}
14405SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
14406  return id->pMethods->xTruncate(id, size);
14407}
14408SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
14409  DO_OS_MALLOC_TEST(id);
14410  return id->pMethods->xSync(id, flags);
14411}
14412SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
14413  DO_OS_MALLOC_TEST(id);
14414  return id->pMethods->xFileSize(id, pSize);
14415}
14416SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
14417  DO_OS_MALLOC_TEST(id);
14418  return id->pMethods->xLock(id, lockType);
14419}
14420SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
14421  return id->pMethods->xUnlock(id, lockType);
14422}
14423SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
14424  DO_OS_MALLOC_TEST(id);
14425  return id->pMethods->xCheckReservedLock(id, pResOut);
14426}
14427SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
14428  return id->pMethods->xFileControl(id, op, pArg);
14429}
14430SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
14431  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
14432  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
14433}
14434SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
14435  return id->pMethods->xDeviceCharacteristics(id);
14436}
14437SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
14438  return id->pMethods->xShmLock(id, offset, n, flags);
14439}
14440SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
14441  id->pMethods->xShmBarrier(id);
14442}
14443SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
14444  return id->pMethods->xShmUnmap(id, deleteFlag);
14445}
14446SQLITE_PRIVATE int sqlite3OsShmMap(
14447  sqlite3_file *id,               /* Database file handle */
14448  int iPage,
14449  int pgsz,
14450  int bExtend,                    /* True to extend file if necessary */
14451  void volatile **pp              /* OUT: Pointer to mapping */
14452){
14453  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
14454}
14455
14456/*
14457** The next group of routines are convenience wrappers around the
14458** VFS methods.
14459*/
14460SQLITE_PRIVATE int sqlite3OsOpen(
14461  sqlite3_vfs *pVfs,
14462  const char *zPath,
14463  sqlite3_file *pFile,
14464  int flags,
14465  int *pFlagsOut
14466){
14467  int rc;
14468  DO_OS_MALLOC_TEST(0);
14469  /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14470  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
14471  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14472  ** reaching the VFS. */
14473  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
14474  assert( rc==SQLITE_OK || pFile->pMethods==0 );
14475  return rc;
14476}
14477SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14478  return pVfs->xDelete(pVfs, zPath, dirSync);
14479}
14480SQLITE_PRIVATE int sqlite3OsAccess(
14481  sqlite3_vfs *pVfs,
14482  const char *zPath,
14483  int flags,
14484  int *pResOut
14485){
14486  DO_OS_MALLOC_TEST(0);
14487  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
14488}
14489SQLITE_PRIVATE int sqlite3OsFullPathname(
14490  sqlite3_vfs *pVfs,
14491  const char *zPath,
14492  int nPathOut,
14493  char *zPathOut
14494){
14495  zPathOut[0] = 0;
14496  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
14497}
14498#ifndef SQLITE_OMIT_LOAD_EXTENSION
14499SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
14500  return pVfs->xDlOpen(pVfs, zPath);
14501}
14502SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14503  pVfs->xDlError(pVfs, nByte, zBufOut);
14504}
14505SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
14506  return pVfs->xDlSym(pVfs, pHdle, zSym);
14507}
14508SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
14509  pVfs->xDlClose(pVfs, pHandle);
14510}
14511#endif /* SQLITE_OMIT_LOAD_EXTENSION */
14512SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
14513  return pVfs->xRandomness(pVfs, nByte, zBufOut);
14514}
14515SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
14516  return pVfs->xSleep(pVfs, nMicro);
14517}
14518SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
14519  int rc;
14520  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
14521  ** method to get the current date and time if that method is available
14522  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
14523  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
14524  ** unavailable.
14525  */
14526  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
14527    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
14528  }else{
14529    double r;
14530    rc = pVfs->xCurrentTime(pVfs, &r);
14531    *pTimeOut = (sqlite3_int64)(r*86400000.0);
14532  }
14533  return rc;
14534}
14535
14536SQLITE_PRIVATE int sqlite3OsOpenMalloc(
14537  sqlite3_vfs *pVfs,
14538  const char *zFile,
14539  sqlite3_file **ppFile,
14540  int flags,
14541  int *pOutFlags
14542){
14543  int rc = SQLITE_NOMEM;
14544  sqlite3_file *pFile;
14545  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
14546  if( pFile ){
14547    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
14548    if( rc!=SQLITE_OK ){
14549      sqlite3_free(pFile);
14550    }else{
14551      *ppFile = pFile;
14552    }
14553  }
14554  return rc;
14555}
14556SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
14557  int rc = SQLITE_OK;
14558  assert( pFile );
14559  rc = sqlite3OsClose(pFile);
14560  sqlite3_free(pFile);
14561  return rc;
14562}
14563
14564/*
14565** This function is a wrapper around the OS specific implementation of
14566** sqlite3_os_init(). The purpose of the wrapper is to provide the
14567** ability to simulate a malloc failure, so that the handling of an
14568** error in sqlite3_os_init() by the upper layers can be tested.
14569*/
14570SQLITE_PRIVATE int sqlite3OsInit(void){
14571  void *p = sqlite3_malloc(10);
14572  if( p==0 ) return SQLITE_NOMEM;
14573  sqlite3_free(p);
14574  return sqlite3_os_init();
14575}
14576
14577/*
14578** The list of all registered VFS implementations.
14579*/
14580static sqlite3_vfs * SQLITE_WSD vfsList = 0;
14581#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
14582
14583/*
14584** Locate a VFS by name.  If no name is given, simply return the
14585** first VFS on the list.
14586*/
14587SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
14588  sqlite3_vfs *pVfs = 0;
14589#if SQLITE_THREADSAFE
14590  sqlite3_mutex *mutex;
14591#endif
14592#ifndef SQLITE_OMIT_AUTOINIT
14593  int rc = sqlite3_initialize();
14594  if( rc ) return 0;
14595#endif
14596#if SQLITE_THREADSAFE
14597  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14598#endif
14599  sqlite3_mutex_enter(mutex);
14600  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
14601    if( zVfs==0 ) break;
14602    if( strcmp(zVfs, pVfs->zName)==0 ) break;
14603  }
14604  sqlite3_mutex_leave(mutex);
14605  return pVfs;
14606}
14607
14608/*
14609** Unlink a VFS from the linked list
14610*/
14611static void vfsUnlink(sqlite3_vfs *pVfs){
14612  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
14613  if( pVfs==0 ){
14614    /* No-op */
14615  }else if( vfsList==pVfs ){
14616    vfsList = pVfs->pNext;
14617  }else if( vfsList ){
14618    sqlite3_vfs *p = vfsList;
14619    while( p->pNext && p->pNext!=pVfs ){
14620      p = p->pNext;
14621    }
14622    if( p->pNext==pVfs ){
14623      p->pNext = pVfs->pNext;
14624    }
14625  }
14626}
14627
14628/*
14629** Register a VFS with the system.  It is harmless to register the same
14630** VFS multiple times.  The new VFS becomes the default if makeDflt is
14631** true.
14632*/
14633SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14634  sqlite3_mutex *mutex = 0;
14635#ifndef SQLITE_OMIT_AUTOINIT
14636  int rc = sqlite3_initialize();
14637  if( rc ) return rc;
14638#endif
14639  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14640  sqlite3_mutex_enter(mutex);
14641  vfsUnlink(pVfs);
14642  if( makeDflt || vfsList==0 ){
14643    pVfs->pNext = vfsList;
14644    vfsList = pVfs;
14645  }else{
14646    pVfs->pNext = vfsList->pNext;
14647    vfsList->pNext = pVfs;
14648  }
14649  assert(vfsList);
14650  sqlite3_mutex_leave(mutex);
14651  return SQLITE_OK;
14652}
14653
14654/*
14655** Unregister a VFS so that it is no longer accessible.
14656*/
14657SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
14658#if SQLITE_THREADSAFE
14659  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14660#endif
14661  sqlite3_mutex_enter(mutex);
14662  vfsUnlink(pVfs);
14663  sqlite3_mutex_leave(mutex);
14664  return SQLITE_OK;
14665}
14666
14667/************** End of os.c **************************************************/
14668/************** Begin file fault.c *******************************************/
14669/*
14670** 2008 Jan 22
14671**
14672** The author disclaims copyright to this source code.  In place of
14673** a legal notice, here is a blessing:
14674**
14675**    May you do good and not evil.
14676**    May you find forgiveness for yourself and forgive others.
14677**    May you share freely, never taking more than you give.
14678**
14679*************************************************************************
14680**
14681** This file contains code to support the concept of "benign"
14682** malloc failures (when the xMalloc() or xRealloc() method of the
14683** sqlite3_mem_methods structure fails to allocate a block of memory
14684** and returns 0).
14685**
14686** Most malloc failures are non-benign. After they occur, SQLite
14687** abandons the current operation and returns an error code (usually
14688** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
14689** fatal. For example, if a malloc fails while resizing a hash table, this
14690** is completely recoverable simply by not carrying out the resize. The
14691** hash table will continue to function normally.  So a malloc failure
14692** during a hash table resize is a benign fault.
14693*/
14694
14695
14696#ifndef SQLITE_OMIT_BUILTIN_TEST
14697
14698/*
14699** Global variables.
14700*/
14701typedef struct BenignMallocHooks BenignMallocHooks;
14702static SQLITE_WSD struct BenignMallocHooks {
14703  void (*xBenignBegin)(void);
14704  void (*xBenignEnd)(void);
14705} sqlite3Hooks = { 0, 0 };
14706
14707/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
14708** structure.  If writable static data is unsupported on the target,
14709** we have to locate the state vector at run-time.  In the more common
14710** case where writable static data is supported, wsdHooks can refer directly
14711** to the "sqlite3Hooks" state vector declared above.
14712*/
14713#ifdef SQLITE_OMIT_WSD
14714# define wsdHooksInit \
14715  BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
14716# define wsdHooks x[0]
14717#else
14718# define wsdHooksInit
14719# define wsdHooks sqlite3Hooks
14720#endif
14721
14722
14723/*
14724** Register hooks to call when sqlite3BeginBenignMalloc() and
14725** sqlite3EndBenignMalloc() are called, respectively.
14726*/
14727SQLITE_PRIVATE void sqlite3BenignMallocHooks(
14728  void (*xBenignBegin)(void),
14729  void (*xBenignEnd)(void)
14730){
14731  wsdHooksInit;
14732  wsdHooks.xBenignBegin = xBenignBegin;
14733  wsdHooks.xBenignEnd = xBenignEnd;
14734}
14735
14736/*
14737** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
14738** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
14739** indicates that subsequent malloc failures are non-benign.
14740*/
14741SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
14742  wsdHooksInit;
14743  if( wsdHooks.xBenignBegin ){
14744    wsdHooks.xBenignBegin();
14745  }
14746}
14747SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
14748  wsdHooksInit;
14749  if( wsdHooks.xBenignEnd ){
14750    wsdHooks.xBenignEnd();
14751  }
14752}
14753
14754#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
14755
14756/************** End of fault.c ***********************************************/
14757/************** Begin file mem0.c ********************************************/
14758/*
14759** 2008 October 28
14760**
14761** The author disclaims copyright to this source code.  In place of
14762** a legal notice, here is a blessing:
14763**
14764**    May you do good and not evil.
14765**    May you find forgiveness for yourself and forgive others.
14766**    May you share freely, never taking more than you give.
14767**
14768*************************************************************************
14769**
14770** This file contains a no-op memory allocation drivers for use when
14771** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
14772** here always fail.  SQLite will not operate with these drivers.  These
14773** are merely placeholders.  Real drivers must be substituted using
14774** sqlite3_config() before SQLite will operate.
14775*/
14776
14777/*
14778** This version of the memory allocator is the default.  It is
14779** used when no other memory allocator is specified using compile-time
14780** macros.
14781*/
14782#ifdef SQLITE_ZERO_MALLOC
14783
14784/*
14785** No-op versions of all memory allocation routines
14786*/
14787static void *sqlite3MemMalloc(int nByte){ return 0; }
14788static void sqlite3MemFree(void *pPrior){ return; }
14789static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
14790static int sqlite3MemSize(void *pPrior){ return 0; }
14791static int sqlite3MemRoundup(int n){ return n; }
14792static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
14793static void sqlite3MemShutdown(void *NotUsed){ return; }
14794
14795/*
14796** This routine is the only routine in this file with external linkage.
14797**
14798** Populate the low-level memory allocation function pointers in
14799** sqlite3GlobalConfig.m with pointers to the routines in this file.
14800*/
14801SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14802  static const sqlite3_mem_methods defaultMethods = {
14803     sqlite3MemMalloc,
14804     sqlite3MemFree,
14805     sqlite3MemRealloc,
14806     sqlite3MemSize,
14807     sqlite3MemRoundup,
14808     sqlite3MemInit,
14809     sqlite3MemShutdown,
14810     0
14811  };
14812  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14813}
14814
14815#endif /* SQLITE_ZERO_MALLOC */
14816
14817/************** End of mem0.c ************************************************/
14818/************** Begin file mem1.c ********************************************/
14819/*
14820** 2007 August 14
14821**
14822** The author disclaims copyright to this source code.  In place of
14823** a legal notice, here is a blessing:
14824**
14825**    May you do good and not evil.
14826**    May you find forgiveness for yourself and forgive others.
14827**    May you share freely, never taking more than you give.
14828**
14829*************************************************************************
14830**
14831** This file contains low-level memory allocation drivers for when
14832** SQLite will use the standard C-library malloc/realloc/free interface
14833** to obtain the memory it needs.
14834**
14835** This file contains implementations of the low-level memory allocation
14836** routines specified in the sqlite3_mem_methods object.
14837*/
14838
14839/*
14840** This version of the memory allocator is the default.  It is
14841** used when no other memory allocator is specified using compile-time
14842** macros.
14843*/
14844#ifdef SQLITE_SYSTEM_MALLOC
14845
14846/*
14847** Like malloc(), but remember the size of the allocation
14848** so that we can find it later using sqlite3MemSize().
14849**
14850** For this low-level routine, we are guaranteed that nByte>0 because
14851** cases of nByte<=0 will be intercepted and dealt with by higher level
14852** routines.
14853*/
14854static void *sqlite3MemMalloc(int nByte){
14855  sqlite3_int64 *p;
14856  assert( nByte>0 );
14857  nByte = ROUND8(nByte);
14858  p = malloc( nByte+8 );
14859  if( p ){
14860    p[0] = nByte;
14861    p++;
14862  }else{
14863    testcase( sqlite3GlobalConfig.xLog!=0 );
14864    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
14865  }
14866  return (void *)p;
14867}
14868
14869/*
14870** Like free() but works for allocations obtained from sqlite3MemMalloc()
14871** or sqlite3MemRealloc().
14872**
14873** For this low-level routine, we already know that pPrior!=0 since
14874** cases where pPrior==0 will have been intecepted and dealt with
14875** by higher-level routines.
14876*/
14877static void sqlite3MemFree(void *pPrior){
14878  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14879  assert( pPrior!=0 );
14880  p--;
14881  free(p);
14882}
14883
14884/*
14885** Report the allocated size of a prior return from xMalloc()
14886** or xRealloc().
14887*/
14888static int sqlite3MemSize(void *pPrior){
14889  sqlite3_int64 *p;
14890  if( pPrior==0 ) return 0;
14891  p = (sqlite3_int64*)pPrior;
14892  p--;
14893  return (int)p[0];
14894}
14895
14896/*
14897** Like realloc().  Resize an allocation previously obtained from
14898** sqlite3MemMalloc().
14899**
14900** For this low-level interface, we know that pPrior!=0.  Cases where
14901** pPrior==0 while have been intercepted by higher-level routine and
14902** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
14903** cases where nByte<=0 will have been intercepted by higher-level
14904** routines and redirected to xFree.
14905*/
14906static void *sqlite3MemRealloc(void *pPrior, int nByte){
14907  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14908  assert( pPrior!=0 && nByte>0 );
14909  assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
14910  p--;
14911  p = realloc(p, nByte+8 );
14912  if( p ){
14913    p[0] = nByte;
14914    p++;
14915  }else{
14916    testcase( sqlite3GlobalConfig.xLog!=0 );
14917    sqlite3_log(SQLITE_NOMEM,
14918      "failed memory resize %u to %u bytes",
14919      sqlite3MemSize(pPrior), nByte);
14920  }
14921  return (void*)p;
14922}
14923
14924/*
14925** Round up a request size to the next valid allocation size.
14926*/
14927static int sqlite3MemRoundup(int n){
14928  return ROUND8(n);
14929}
14930
14931/*
14932** Initialize this module.
14933*/
14934static int sqlite3MemInit(void *NotUsed){
14935  UNUSED_PARAMETER(NotUsed);
14936  return SQLITE_OK;
14937}
14938
14939/*
14940** Deinitialize this module.
14941*/
14942static void sqlite3MemShutdown(void *NotUsed){
14943  UNUSED_PARAMETER(NotUsed);
14944  return;
14945}
14946
14947/*
14948** This routine is the only routine in this file with external linkage.
14949**
14950** Populate the low-level memory allocation function pointers in
14951** sqlite3GlobalConfig.m with pointers to the routines in this file.
14952*/
14953SQLITE_PRIVATE void sqlite3MemSetDefault(void){
14954  static const sqlite3_mem_methods defaultMethods = {
14955     sqlite3MemMalloc,
14956     sqlite3MemFree,
14957     sqlite3MemRealloc,
14958     sqlite3MemSize,
14959     sqlite3MemRoundup,
14960     sqlite3MemInit,
14961     sqlite3MemShutdown,
14962     0
14963  };
14964  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
14965}
14966
14967#endif /* SQLITE_SYSTEM_MALLOC */
14968
14969/************** End of mem1.c ************************************************/
14970/************** Begin file mem2.c ********************************************/
14971/*
14972** 2007 August 15
14973**
14974** The author disclaims copyright to this source code.  In place of
14975** a legal notice, here is a blessing:
14976**
14977**    May you do good and not evil.
14978**    May you find forgiveness for yourself and forgive others.
14979**    May you share freely, never taking more than you give.
14980**
14981*************************************************************************
14982**
14983** This file contains low-level memory allocation drivers for when
14984** SQLite will use the standard C-library malloc/realloc/free interface
14985** to obtain the memory it needs while adding lots of additional debugging
14986** information to each allocation in order to help detect and fix memory
14987** leaks and memory usage errors.
14988**
14989** This file contains implementations of the low-level memory allocation
14990** routines specified in the sqlite3_mem_methods object.
14991*/
14992
14993/*
14994** This version of the memory allocator is used only if the
14995** SQLITE_MEMDEBUG macro is defined
14996*/
14997#ifdef SQLITE_MEMDEBUG
14998
14999/*
15000** The backtrace functionality is only available with GLIBC
15001*/
15002#ifdef __GLIBC__
15003  extern int backtrace(void**,int);
15004  extern void backtrace_symbols_fd(void*const*,int,int);
15005#else
15006# define backtrace(A,B) 1
15007# define backtrace_symbols_fd(A,B,C)
15008#endif
15009/* #include <stdio.h> */
15010
15011/*
15012** Each memory allocation looks like this:
15013**
15014**  ------------------------------------------------------------------------
15015**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
15016**  ------------------------------------------------------------------------
15017**
15018** The application code sees only a pointer to the allocation.  We have
15019** to back up from the allocation pointer to find the MemBlockHdr.  The
15020** MemBlockHdr tells us the size of the allocation and the number of
15021** backtrace pointers.  There is also a guard word at the end of the
15022** MemBlockHdr.
15023*/
15024struct MemBlockHdr {
15025  i64 iSize;                          /* Size of this allocation */
15026  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
15027  char nBacktrace;                    /* Number of backtraces on this alloc */
15028  char nBacktraceSlots;               /* Available backtrace slots */
15029  u8 nTitle;                          /* Bytes of title; includes '\0' */
15030  u8 eType;                           /* Allocation type code */
15031  int iForeGuard;                     /* Guard word for sanity */
15032};
15033
15034/*
15035** Guard words
15036*/
15037#define FOREGUARD 0x80F5E153
15038#define REARGUARD 0xE4676B53
15039
15040/*
15041** Number of malloc size increments to track.
15042*/
15043#define NCSIZE  1000
15044
15045/*
15046** All of the static variables used by this module are collected
15047** into a single structure named "mem".  This is to keep the
15048** static variables organized and to reduce namespace pollution
15049** when this module is combined with other in the amalgamation.
15050*/
15051static struct {
15052
15053  /*
15054  ** Mutex to control access to the memory allocation subsystem.
15055  */
15056  sqlite3_mutex *mutex;
15057
15058  /*
15059  ** Head and tail of a linked list of all outstanding allocations
15060  */
15061  struct MemBlockHdr *pFirst;
15062  struct MemBlockHdr *pLast;
15063
15064  /*
15065  ** The number of levels of backtrace to save in new allocations.
15066  */
15067  int nBacktrace;
15068  void (*xBacktrace)(int, int, void **);
15069
15070  /*
15071  ** Title text to insert in front of each block
15072  */
15073  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
15074  char zTitle[100];  /* The title text */
15075
15076  /*
15077  ** sqlite3MallocDisallow() increments the following counter.
15078  ** sqlite3MallocAllow() decrements it.
15079  */
15080  int disallow; /* Do not allow memory allocation */
15081
15082  /*
15083  ** Gather statistics on the sizes of memory allocations.
15084  ** nAlloc[i] is the number of allocation attempts of i*8
15085  ** bytes.  i==NCSIZE is the number of allocation attempts for
15086  ** sizes more than NCSIZE*8 bytes.
15087  */
15088  int nAlloc[NCSIZE];      /* Total number of allocations */
15089  int nCurrent[NCSIZE];    /* Current number of allocations */
15090  int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
15091
15092} mem;
15093
15094
15095/*
15096** Adjust memory usage statistics
15097*/
15098static void adjustStats(int iSize, int increment){
15099  int i = ROUND8(iSize)/8;
15100  if( i>NCSIZE-1 ){
15101    i = NCSIZE - 1;
15102  }
15103  if( increment>0 ){
15104    mem.nAlloc[i]++;
15105    mem.nCurrent[i]++;
15106    if( mem.nCurrent[i]>mem.mxCurrent[i] ){
15107      mem.mxCurrent[i] = mem.nCurrent[i];
15108    }
15109  }else{
15110    mem.nCurrent[i]--;
15111    assert( mem.nCurrent[i]>=0 );
15112  }
15113}
15114
15115/*
15116** Given an allocation, find the MemBlockHdr for that allocation.
15117**
15118** This routine checks the guards at either end of the allocation and
15119** if they are incorrect it asserts.
15120*/
15121static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
15122  struct MemBlockHdr *p;
15123  int *pInt;
15124  u8 *pU8;
15125  int nReserve;
15126
15127  p = (struct MemBlockHdr*)pAllocation;
15128  p--;
15129  assert( p->iForeGuard==(int)FOREGUARD );
15130  nReserve = ROUND8(p->iSize);
15131  pInt = (int*)pAllocation;
15132  pU8 = (u8*)pAllocation;
15133  assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
15134  /* This checks any of the "extra" bytes allocated due
15135  ** to rounding up to an 8 byte boundary to ensure
15136  ** they haven't been overwritten.
15137  */
15138  while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
15139  return p;
15140}
15141
15142/*
15143** Return the number of bytes currently allocated at address p.
15144*/
15145static int sqlite3MemSize(void *p){
15146  struct MemBlockHdr *pHdr;
15147  if( !p ){
15148    return 0;
15149  }
15150  pHdr = sqlite3MemsysGetHeader(p);
15151  return pHdr->iSize;
15152}
15153
15154/*
15155** Initialize the memory allocation subsystem.
15156*/
15157static int sqlite3MemInit(void *NotUsed){
15158  UNUSED_PARAMETER(NotUsed);
15159  assert( (sizeof(struct MemBlockHdr)&7) == 0 );
15160  if( !sqlite3GlobalConfig.bMemstat ){
15161    /* If memory status is enabled, then the malloc.c wrapper will already
15162    ** hold the STATIC_MEM mutex when the routines here are invoked. */
15163    mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
15164  }
15165  return SQLITE_OK;
15166}
15167
15168/*
15169** Deinitialize the memory allocation subsystem.
15170*/
15171static void sqlite3MemShutdown(void *NotUsed){
15172  UNUSED_PARAMETER(NotUsed);
15173  mem.mutex = 0;
15174}
15175
15176/*
15177** Round up a request size to the next valid allocation size.
15178*/
15179static int sqlite3MemRoundup(int n){
15180  return ROUND8(n);
15181}
15182
15183/*
15184** Fill a buffer with pseudo-random bytes.  This is used to preset
15185** the content of a new memory allocation to unpredictable values and
15186** to clear the content of a freed allocation to unpredictable values.
15187*/
15188static void randomFill(char *pBuf, int nByte){
15189  unsigned int x, y, r;
15190  x = SQLITE_PTR_TO_INT(pBuf);
15191  y = nByte | 1;
15192  while( nByte >= 4 ){
15193    x = (x>>1) ^ (-(x&1) & 0xd0000001);
15194    y = y*1103515245 + 12345;
15195    r = x ^ y;
15196    *(int*)pBuf = r;
15197    pBuf += 4;
15198    nByte -= 4;
15199  }
15200  while( nByte-- > 0 ){
15201    x = (x>>1) ^ (-(x&1) & 0xd0000001);
15202    y = y*1103515245 + 12345;
15203    r = x ^ y;
15204    *(pBuf++) = r & 0xff;
15205  }
15206}
15207
15208/*
15209** Allocate nByte bytes of memory.
15210*/
15211static void *sqlite3MemMalloc(int nByte){
15212  struct MemBlockHdr *pHdr;
15213  void **pBt;
15214  char *z;
15215  int *pInt;
15216  void *p = 0;
15217  int totalSize;
15218  int nReserve;
15219  sqlite3_mutex_enter(mem.mutex);
15220  assert( mem.disallow==0 );
15221  nReserve = ROUND8(nByte);
15222  totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
15223               mem.nBacktrace*sizeof(void*) + mem.nTitle;
15224  p = malloc(totalSize);
15225  if( p ){
15226    z = p;
15227    pBt = (void**)&z[mem.nTitle];
15228    pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
15229    pHdr->pNext = 0;
15230    pHdr->pPrev = mem.pLast;
15231    if( mem.pLast ){
15232      mem.pLast->pNext = pHdr;
15233    }else{
15234      mem.pFirst = pHdr;
15235    }
15236    mem.pLast = pHdr;
15237    pHdr->iForeGuard = FOREGUARD;
15238    pHdr->eType = MEMTYPE_HEAP;
15239    pHdr->nBacktraceSlots = mem.nBacktrace;
15240    pHdr->nTitle = mem.nTitle;
15241    if( mem.nBacktrace ){
15242      void *aAddr[40];
15243      pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
15244      memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
15245      assert(pBt[0]);
15246      if( mem.xBacktrace ){
15247        mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
15248      }
15249    }else{
15250      pHdr->nBacktrace = 0;
15251    }
15252    if( mem.nTitle ){
15253      memcpy(z, mem.zTitle, mem.nTitle);
15254    }
15255    pHdr->iSize = nByte;
15256    adjustStats(nByte, +1);
15257    pInt = (int*)&pHdr[1];
15258    pInt[nReserve/sizeof(int)] = REARGUARD;
15259    randomFill((char*)pInt, nByte);
15260    memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
15261    p = (void*)pInt;
15262  }
15263  sqlite3_mutex_leave(mem.mutex);
15264  return p;
15265}
15266
15267/*
15268** Free memory.
15269*/
15270static void sqlite3MemFree(void *pPrior){
15271  struct MemBlockHdr *pHdr;
15272  void **pBt;
15273  char *z;
15274  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
15275       || mem.mutex!=0 );
15276  pHdr = sqlite3MemsysGetHeader(pPrior);
15277  pBt = (void**)pHdr;
15278  pBt -= pHdr->nBacktraceSlots;
15279  sqlite3_mutex_enter(mem.mutex);
15280  if( pHdr->pPrev ){
15281    assert( pHdr->pPrev->pNext==pHdr );
15282    pHdr->pPrev->pNext = pHdr->pNext;
15283  }else{
15284    assert( mem.pFirst==pHdr );
15285    mem.pFirst = pHdr->pNext;
15286  }
15287  if( pHdr->pNext ){
15288    assert( pHdr->pNext->pPrev==pHdr );
15289    pHdr->pNext->pPrev = pHdr->pPrev;
15290  }else{
15291    assert( mem.pLast==pHdr );
15292    mem.pLast = pHdr->pPrev;
15293  }
15294  z = (char*)pBt;
15295  z -= pHdr->nTitle;
15296  adjustStats(pHdr->iSize, -1);
15297  randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
15298                pHdr->iSize + sizeof(int) + pHdr->nTitle);
15299  free(z);
15300  sqlite3_mutex_leave(mem.mutex);
15301}
15302
15303/*
15304** Change the size of an existing memory allocation.
15305**
15306** For this debugging implementation, we *always* make a copy of the
15307** allocation into a new place in memory.  In this way, if the
15308** higher level code is using pointer to the old allocation, it is
15309** much more likely to break and we are much more liking to find
15310** the error.
15311*/
15312static void *sqlite3MemRealloc(void *pPrior, int nByte){
15313  struct MemBlockHdr *pOldHdr;
15314  void *pNew;
15315  assert( mem.disallow==0 );
15316  assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
15317  pOldHdr = sqlite3MemsysGetHeader(pPrior);
15318  pNew = sqlite3MemMalloc(nByte);
15319  if( pNew ){
15320    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
15321    if( nByte>pOldHdr->iSize ){
15322      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
15323    }
15324    sqlite3MemFree(pPrior);
15325  }
15326  return pNew;
15327}
15328
15329/*
15330** Populate the low-level memory allocation function pointers in
15331** sqlite3GlobalConfig.m with pointers to the routines in this file.
15332*/
15333SQLITE_PRIVATE void sqlite3MemSetDefault(void){
15334  static const sqlite3_mem_methods defaultMethods = {
15335     sqlite3MemMalloc,
15336     sqlite3MemFree,
15337     sqlite3MemRealloc,
15338     sqlite3MemSize,
15339     sqlite3MemRoundup,
15340     sqlite3MemInit,
15341     sqlite3MemShutdown,
15342     0
15343  };
15344  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
15345}
15346
15347/*
15348** Set the "type" of an allocation.
15349*/
15350SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
15351  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15352    struct MemBlockHdr *pHdr;
15353    pHdr = sqlite3MemsysGetHeader(p);
15354    assert( pHdr->iForeGuard==FOREGUARD );
15355    pHdr->eType = eType;
15356  }
15357}
15358
15359/*
15360** Return TRUE if the mask of type in eType matches the type of the
15361** allocation p.  Also return true if p==NULL.
15362**
15363** This routine is designed for use within an assert() statement, to
15364** verify the type of an allocation.  For example:
15365**
15366**     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
15367*/
15368SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
15369  int rc = 1;
15370  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15371    struct MemBlockHdr *pHdr;
15372    pHdr = sqlite3MemsysGetHeader(p);
15373    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
15374    if( (pHdr->eType&eType)==0 ){
15375      rc = 0;
15376    }
15377  }
15378  return rc;
15379}
15380
15381/*
15382** Return TRUE if the mask of type in eType matches no bits of the type of the
15383** allocation p.  Also return true if p==NULL.
15384**
15385** This routine is designed for use within an assert() statement, to
15386** verify the type of an allocation.  For example:
15387**
15388**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
15389*/
15390SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
15391  int rc = 1;
15392  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
15393    struct MemBlockHdr *pHdr;
15394    pHdr = sqlite3MemsysGetHeader(p);
15395    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
15396    if( (pHdr->eType&eType)!=0 ){
15397      rc = 0;
15398    }
15399  }
15400  return rc;
15401}
15402
15403/*
15404** Set the number of backtrace levels kept for each allocation.
15405** A value of zero turns off backtracing.  The number is always rounded
15406** up to a multiple of 2.
15407*/
15408SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
15409  if( depth<0 ){ depth = 0; }
15410  if( depth>20 ){ depth = 20; }
15411  depth = (depth+1)&0xfe;
15412  mem.nBacktrace = depth;
15413}
15414
15415SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
15416  mem.xBacktrace = xBacktrace;
15417}
15418
15419/*
15420** Set the title string for subsequent allocations.
15421*/
15422SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
15423  unsigned int n = sqlite3Strlen30(zTitle) + 1;
15424  sqlite3_mutex_enter(mem.mutex);
15425  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
15426  memcpy(mem.zTitle, zTitle, n);
15427  mem.zTitle[n] = 0;
15428  mem.nTitle = ROUND8(n);
15429  sqlite3_mutex_leave(mem.mutex);
15430}
15431
15432SQLITE_PRIVATE void sqlite3MemdebugSync(){
15433  struct MemBlockHdr *pHdr;
15434  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15435    void **pBt = (void**)pHdr;
15436    pBt -= pHdr->nBacktraceSlots;
15437    mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
15438  }
15439}
15440
15441/*
15442** Open the file indicated and write a log of all unfreed memory
15443** allocations into that log.
15444*/
15445SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
15446  FILE *out;
15447  struct MemBlockHdr *pHdr;
15448  void **pBt;
15449  int i;
15450  out = fopen(zFilename, "w");
15451  if( out==0 ){
15452    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
15453                    zFilename);
15454    return;
15455  }
15456  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
15457    char *z = (char*)pHdr;
15458    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
15459    fprintf(out, "**** %lld bytes at %p from %s ****\n",
15460            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
15461    if( pHdr->nBacktrace ){
15462      fflush(out);
15463      pBt = (void**)pHdr;
15464      pBt -= pHdr->nBacktraceSlots;
15465      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
15466      fprintf(out, "\n");
15467    }
15468  }
15469  fprintf(out, "COUNTS:\n");
15470  for(i=0; i<NCSIZE-1; i++){
15471    if( mem.nAlloc[i] ){
15472      fprintf(out, "   %5d: %10d %10d %10d\n",
15473            i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
15474    }
15475  }
15476  if( mem.nAlloc[NCSIZE-1] ){
15477    fprintf(out, "   %5d: %10d %10d %10d\n",
15478             NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
15479             mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
15480  }
15481  fclose(out);
15482}
15483
15484/*
15485** Return the number of times sqlite3MemMalloc() has been called.
15486*/
15487SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
15488  int i;
15489  int nTotal = 0;
15490  for(i=0; i<NCSIZE; i++){
15491    nTotal += mem.nAlloc[i];
15492  }
15493  return nTotal;
15494}
15495
15496
15497#endif /* SQLITE_MEMDEBUG */
15498
15499/************** End of mem2.c ************************************************/
15500/************** Begin file mem3.c ********************************************/
15501/*
15502** 2007 October 14
15503**
15504** The author disclaims copyright to this source code.  In place of
15505** a legal notice, here is a blessing:
15506**
15507**    May you do good and not evil.
15508**    May you find forgiveness for yourself and forgive others.
15509**    May you share freely, never taking more than you give.
15510**
15511*************************************************************************
15512** This file contains the C functions that implement a memory
15513** allocation subsystem for use by SQLite.
15514**
15515** This version of the memory allocation subsystem omits all
15516** use of malloc(). The SQLite user supplies a block of memory
15517** before calling sqlite3_initialize() from which allocations
15518** are made and returned by the xMalloc() and xRealloc()
15519** implementations. Once sqlite3_initialize() has been called,
15520** the amount of memory available to SQLite is fixed and cannot
15521** be changed.
15522**
15523** This version of the memory allocation subsystem is included
15524** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
15525*/
15526
15527/*
15528** This version of the memory allocator is only built into the library
15529** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
15530** mean that the library will use a memory-pool by default, just that
15531** it is available. The mempool allocator is activated by calling
15532** sqlite3_config().
15533*/
15534#ifdef SQLITE_ENABLE_MEMSYS3
15535
15536/*
15537** Maximum size (in Mem3Blocks) of a "small" chunk.
15538*/
15539#define MX_SMALL 10
15540
15541
15542/*
15543** Number of freelist hash slots
15544*/
15545#define N_HASH  61
15546
15547/*
15548** A memory allocation (also called a "chunk") consists of two or
15549** more blocks where each block is 8 bytes.  The first 8 bytes are
15550** a header that is not returned to the user.
15551**
15552** A chunk is two or more blocks that is either checked out or
15553** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
15554** size of the allocation in blocks if the allocation is free.
15555** The u.hdr.size4x&1 bit is true if the chunk is checked out and
15556** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
15557** is true if the previous chunk is checked out and false if the
15558** previous chunk is free.  The u.hdr.prevSize field is the size of
15559** the previous chunk in blocks if the previous chunk is on the
15560** freelist. If the previous chunk is checked out, then
15561** u.hdr.prevSize can be part of the data for that chunk and should
15562** not be read or written.
15563**
15564** We often identify a chunk by its index in mem3.aPool[].  When
15565** this is done, the chunk index refers to the second block of
15566** the chunk.  In this way, the first chunk has an index of 1.
15567** A chunk index of 0 means "no such chunk" and is the equivalent
15568** of a NULL pointer.
15569**
15570** The second block of free chunks is of the form u.list.  The
15571** two fields form a double-linked list of chunks of related sizes.
15572** Pointers to the head of the list are stored in mem3.aiSmall[]
15573** for smaller chunks and mem3.aiHash[] for larger chunks.
15574**
15575** The second block of a chunk is user data if the chunk is checked
15576** out.  If a chunk is checked out, the user data may extend into
15577** the u.hdr.prevSize value of the following chunk.
15578*/
15579typedef struct Mem3Block Mem3Block;
15580struct Mem3Block {
15581  union {
15582    struct {
15583      u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
15584      u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
15585    } hdr;
15586    struct {
15587      u32 next;       /* Index in mem3.aPool[] of next free chunk */
15588      u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
15589    } list;
15590  } u;
15591};
15592
15593/*
15594** All of the static variables used by this module are collected
15595** into a single structure named "mem3".  This is to keep the
15596** static variables organized and to reduce namespace pollution
15597** when this module is combined with other in the amalgamation.
15598*/
15599static SQLITE_WSD struct Mem3Global {
15600  /*
15601  ** Memory available for allocation. nPool is the size of the array
15602  ** (in Mem3Blocks) pointed to by aPool less 2.
15603  */
15604  u32 nPool;
15605  Mem3Block *aPool;
15606
15607  /*
15608  ** True if we are evaluating an out-of-memory callback.
15609  */
15610  int alarmBusy;
15611
15612  /*
15613  ** Mutex to control access to the memory allocation subsystem.
15614  */
15615  sqlite3_mutex *mutex;
15616
15617  /*
15618  ** The minimum amount of free space that we have seen.
15619  */
15620  u32 mnMaster;
15621
15622  /*
15623  ** iMaster is the index of the master chunk.  Most new allocations
15624  ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
15625  ** of the current master.  iMaster is 0 if there is not master chunk.
15626  ** The master chunk is not in either the aiHash[] or aiSmall[].
15627  */
15628  u32 iMaster;
15629  u32 szMaster;
15630
15631  /*
15632  ** Array of lists of free blocks according to the block size
15633  ** for smaller chunks, or a hash on the block size for larger
15634  ** chunks.
15635  */
15636  u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
15637  u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
15638} mem3 = { 97535575 };
15639
15640#define mem3 GLOBAL(struct Mem3Global, mem3)
15641
15642/*
15643** Unlink the chunk at mem3.aPool[i] from list it is currently
15644** on.  *pRoot is the list that i is a member of.
15645*/
15646static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
15647  u32 next = mem3.aPool[i].u.list.next;
15648  u32 prev = mem3.aPool[i].u.list.prev;
15649  assert( sqlite3_mutex_held(mem3.mutex) );
15650  if( prev==0 ){
15651    *pRoot = next;
15652  }else{
15653    mem3.aPool[prev].u.list.next = next;
15654  }
15655  if( next ){
15656    mem3.aPool[next].u.list.prev = prev;
15657  }
15658  mem3.aPool[i].u.list.next = 0;
15659  mem3.aPool[i].u.list.prev = 0;
15660}
15661
15662/*
15663** Unlink the chunk at index i from
15664** whatever list is currently a member of.
15665*/
15666static void memsys3Unlink(u32 i){
15667  u32 size, hash;
15668  assert( sqlite3_mutex_held(mem3.mutex) );
15669  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15670  assert( i>=1 );
15671  size = mem3.aPool[i-1].u.hdr.size4x/4;
15672  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15673  assert( size>=2 );
15674  if( size <= MX_SMALL ){
15675    memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
15676  }else{
15677    hash = size % N_HASH;
15678    memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15679  }
15680}
15681
15682/*
15683** Link the chunk at mem3.aPool[i] so that is on the list rooted
15684** at *pRoot.
15685*/
15686static void memsys3LinkIntoList(u32 i, u32 *pRoot){
15687  assert( sqlite3_mutex_held(mem3.mutex) );
15688  mem3.aPool[i].u.list.next = *pRoot;
15689  mem3.aPool[i].u.list.prev = 0;
15690  if( *pRoot ){
15691    mem3.aPool[*pRoot].u.list.prev = i;
15692  }
15693  *pRoot = i;
15694}
15695
15696/*
15697** Link the chunk at index i into either the appropriate
15698** small chunk list, or into the large chunk hash table.
15699*/
15700static void memsys3Link(u32 i){
15701  u32 size, hash;
15702  assert( sqlite3_mutex_held(mem3.mutex) );
15703  assert( i>=1 );
15704  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
15705  size = mem3.aPool[i-1].u.hdr.size4x/4;
15706  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
15707  assert( size>=2 );
15708  if( size <= MX_SMALL ){
15709    memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
15710  }else{
15711    hash = size % N_HASH;
15712    memsys3LinkIntoList(i, &mem3.aiHash[hash]);
15713  }
15714}
15715
15716/*
15717** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
15718** will already be held (obtained by code in malloc.c) if
15719** sqlite3GlobalConfig.bMemStat is true.
15720*/
15721static void memsys3Enter(void){
15722  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
15723    mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
15724  }
15725  sqlite3_mutex_enter(mem3.mutex);
15726}
15727static void memsys3Leave(void){
15728  sqlite3_mutex_leave(mem3.mutex);
15729}
15730
15731/*
15732** Called when we are unable to satisfy an allocation of nBytes.
15733*/
15734static void memsys3OutOfMemory(int nByte){
15735  if( !mem3.alarmBusy ){
15736    mem3.alarmBusy = 1;
15737    assert( sqlite3_mutex_held(mem3.mutex) );
15738    sqlite3_mutex_leave(mem3.mutex);
15739    sqlite3_release_memory(nByte);
15740    sqlite3_mutex_enter(mem3.mutex);
15741    mem3.alarmBusy = 0;
15742  }
15743}
15744
15745
15746/*
15747** Chunk i is a free chunk that has been unlinked.  Adjust its
15748** size parameters for check-out and return a pointer to the
15749** user portion of the chunk.
15750*/
15751static void *memsys3Checkout(u32 i, u32 nBlock){
15752  u32 x;
15753  assert( sqlite3_mutex_held(mem3.mutex) );
15754  assert( i>=1 );
15755  assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
15756  assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
15757  x = mem3.aPool[i-1].u.hdr.size4x;
15758  mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
15759  mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
15760  mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
15761  return &mem3.aPool[i];
15762}
15763
15764/*
15765** Carve a piece off of the end of the mem3.iMaster free chunk.
15766** Return a pointer to the new allocation.  Or, if the master chunk
15767** is not large enough, return 0.
15768*/
15769static void *memsys3FromMaster(u32 nBlock){
15770  assert( sqlite3_mutex_held(mem3.mutex) );
15771  assert( mem3.szMaster>=nBlock );
15772  if( nBlock>=mem3.szMaster-1 ){
15773    /* Use the entire master */
15774    void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
15775    mem3.iMaster = 0;
15776    mem3.szMaster = 0;
15777    mem3.mnMaster = 0;
15778    return p;
15779  }else{
15780    /* Split the master block.  Return the tail. */
15781    u32 newi, x;
15782    newi = mem3.iMaster + mem3.szMaster - nBlock;
15783    assert( newi > mem3.iMaster+1 );
15784    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
15785    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
15786    mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
15787    mem3.szMaster -= nBlock;
15788    mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
15789    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15790    mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15791    if( mem3.szMaster < mem3.mnMaster ){
15792      mem3.mnMaster = mem3.szMaster;
15793    }
15794    return (void*)&mem3.aPool[newi];
15795  }
15796}
15797
15798/*
15799** *pRoot is the head of a list of free chunks of the same size
15800** or same size hash.  In other words, *pRoot is an entry in either
15801** mem3.aiSmall[] or mem3.aiHash[].
15802**
15803** This routine examines all entries on the given list and tries
15804** to coalesce each entries with adjacent free chunks.
15805**
15806** If it sees a chunk that is larger than mem3.iMaster, it replaces
15807** the current mem3.iMaster with the new larger chunk.  In order for
15808** this mem3.iMaster replacement to work, the master chunk must be
15809** linked into the hash tables.  That is not the normal state of
15810** affairs, of course.  The calling routine must link the master
15811** chunk before invoking this routine, then must unlink the (possibly
15812** changed) master chunk once this routine has finished.
15813*/
15814static void memsys3Merge(u32 *pRoot){
15815  u32 iNext, prev, size, i, x;
15816
15817  assert( sqlite3_mutex_held(mem3.mutex) );
15818  for(i=*pRoot; i>0; i=iNext){
15819    iNext = mem3.aPool[i].u.list.next;
15820    size = mem3.aPool[i-1].u.hdr.size4x;
15821    assert( (size&1)==0 );
15822    if( (size&2)==0 ){
15823      memsys3UnlinkFromList(i, pRoot);
15824      assert( i > mem3.aPool[i-1].u.hdr.prevSize );
15825      prev = i - mem3.aPool[i-1].u.hdr.prevSize;
15826      if( prev==iNext ){
15827        iNext = mem3.aPool[prev].u.list.next;
15828      }
15829      memsys3Unlink(prev);
15830      size = i + size/4 - prev;
15831      x = mem3.aPool[prev-1].u.hdr.size4x & 2;
15832      mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
15833      mem3.aPool[prev+size-1].u.hdr.prevSize = size;
15834      memsys3Link(prev);
15835      i = prev;
15836    }else{
15837      size /= 4;
15838    }
15839    if( size>mem3.szMaster ){
15840      mem3.iMaster = i;
15841      mem3.szMaster = size;
15842    }
15843  }
15844}
15845
15846/*
15847** Return a block of memory of at least nBytes in size.
15848** Return NULL if unable.
15849**
15850** This function assumes that the necessary mutexes, if any, are
15851** already held by the caller. Hence "Unsafe".
15852*/
15853static void *memsys3MallocUnsafe(int nByte){
15854  u32 i;
15855  u32 nBlock;
15856  u32 toFree;
15857
15858  assert( sqlite3_mutex_held(mem3.mutex) );
15859  assert( sizeof(Mem3Block)==8 );
15860  if( nByte<=12 ){
15861    nBlock = 2;
15862  }else{
15863    nBlock = (nByte + 11)/8;
15864  }
15865  assert( nBlock>=2 );
15866
15867  /* STEP 1:
15868  ** Look for an entry of the correct size in either the small
15869  ** chunk table or in the large chunk hash table.  This is
15870  ** successful most of the time (about 9 times out of 10).
15871  */
15872  if( nBlock <= MX_SMALL ){
15873    i = mem3.aiSmall[nBlock-2];
15874    if( i>0 ){
15875      memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
15876      return memsys3Checkout(i, nBlock);
15877    }
15878  }else{
15879    int hash = nBlock % N_HASH;
15880    for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
15881      if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
15882        memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
15883        return memsys3Checkout(i, nBlock);
15884      }
15885    }
15886  }
15887
15888  /* STEP 2:
15889  ** Try to satisfy the allocation by carving a piece off of the end
15890  ** of the master chunk.  This step usually works if step 1 fails.
15891  */
15892  if( mem3.szMaster>=nBlock ){
15893    return memsys3FromMaster(nBlock);
15894  }
15895
15896
15897  /* STEP 3:
15898  ** Loop through the entire memory pool.  Coalesce adjacent free
15899  ** chunks.  Recompute the master chunk as the largest free chunk.
15900  ** Then try again to satisfy the allocation by carving a piece off
15901  ** of the end of the master chunk.  This step happens very
15902  ** rarely (we hope!)
15903  */
15904  for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
15905    memsys3OutOfMemory(toFree);
15906    if( mem3.iMaster ){
15907      memsys3Link(mem3.iMaster);
15908      mem3.iMaster = 0;
15909      mem3.szMaster = 0;
15910    }
15911    for(i=0; i<N_HASH; i++){
15912      memsys3Merge(&mem3.aiHash[i]);
15913    }
15914    for(i=0; i<MX_SMALL-1; i++){
15915      memsys3Merge(&mem3.aiSmall[i]);
15916    }
15917    if( mem3.szMaster ){
15918      memsys3Unlink(mem3.iMaster);
15919      if( mem3.szMaster>=nBlock ){
15920        return memsys3FromMaster(nBlock);
15921      }
15922    }
15923  }
15924
15925  /* If none of the above worked, then we fail. */
15926  return 0;
15927}
15928
15929/*
15930** Free an outstanding memory allocation.
15931**
15932** This function assumes that the necessary mutexes, if any, are
15933** already held by the caller. Hence "Unsafe".
15934*/
15935static void memsys3FreeUnsafe(void *pOld){
15936  Mem3Block *p = (Mem3Block*)pOld;
15937  int i;
15938  u32 size, x;
15939  assert( sqlite3_mutex_held(mem3.mutex) );
15940  assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
15941  i = p - mem3.aPool;
15942  assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
15943  size = mem3.aPool[i-1].u.hdr.size4x/4;
15944  assert( i+size<=mem3.nPool+1 );
15945  mem3.aPool[i-1].u.hdr.size4x &= ~1;
15946  mem3.aPool[i+size-1].u.hdr.prevSize = size;
15947  mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
15948  memsys3Link(i);
15949
15950  /* Try to expand the master using the newly freed chunk */
15951  if( mem3.iMaster ){
15952    while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
15953      size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
15954      mem3.iMaster -= size;
15955      mem3.szMaster += size;
15956      memsys3Unlink(mem3.iMaster);
15957      x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15958      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15959      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15960    }
15961    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
15962    while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
15963      memsys3Unlink(mem3.iMaster+mem3.szMaster);
15964      mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
15965      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
15966      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
15967    }
15968  }
15969}
15970
15971/*
15972** Return the size of an outstanding allocation, in bytes.  The
15973** size returned omits the 8-byte header overhead.  This only
15974** works for chunks that are currently checked out.
15975*/
15976static int memsys3Size(void *p){
15977  Mem3Block *pBlock;
15978  if( p==0 ) return 0;
15979  pBlock = (Mem3Block*)p;
15980  assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
15981  return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
15982}
15983
15984/*
15985** Round up a request size to the next valid allocation size.
15986*/
15987static int memsys3Roundup(int n){
15988  if( n<=12 ){
15989    return 12;
15990  }else{
15991    return ((n+11)&~7) - 4;
15992  }
15993}
15994
15995/*
15996** Allocate nBytes of memory.
15997*/
15998static void *memsys3Malloc(int nBytes){
15999  sqlite3_int64 *p;
16000  assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
16001  memsys3Enter();
16002  p = memsys3MallocUnsafe(nBytes);
16003  memsys3Leave();
16004  return (void*)p;
16005}
16006
16007/*
16008** Free memory.
16009*/
16010static void memsys3Free(void *pPrior){
16011  assert( pPrior );
16012  memsys3Enter();
16013  memsys3FreeUnsafe(pPrior);
16014  memsys3Leave();
16015}
16016
16017/*
16018** Change the size of an existing memory allocation
16019*/
16020static void *memsys3Realloc(void *pPrior, int nBytes){
16021  int nOld;
16022  void *p;
16023  if( pPrior==0 ){
16024    return sqlite3_malloc(nBytes);
16025  }
16026  if( nBytes<=0 ){
16027    sqlite3_free(pPrior);
16028    return 0;
16029  }
16030  nOld = memsys3Size(pPrior);
16031  if( nBytes<=nOld && nBytes>=nOld-128 ){
16032    return pPrior;
16033  }
16034  memsys3Enter();
16035  p = memsys3MallocUnsafe(nBytes);
16036  if( p ){
16037    if( nOld<nBytes ){
16038      memcpy(p, pPrior, nOld);
16039    }else{
16040      memcpy(p, pPrior, nBytes);
16041    }
16042    memsys3FreeUnsafe(pPrior);
16043  }
16044  memsys3Leave();
16045  return p;
16046}
16047
16048/*
16049** Initialize this module.
16050*/
16051static int memsys3Init(void *NotUsed){
16052  UNUSED_PARAMETER(NotUsed);
16053  if( !sqlite3GlobalConfig.pHeap ){
16054    return SQLITE_ERROR;
16055  }
16056
16057  /* Store a pointer to the memory block in global structure mem3. */
16058  assert( sizeof(Mem3Block)==8 );
16059  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
16060  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
16061
16062  /* Initialize the master block. */
16063  mem3.szMaster = mem3.nPool;
16064  mem3.mnMaster = mem3.szMaster;
16065  mem3.iMaster = 1;
16066  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
16067  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
16068  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
16069
16070  return SQLITE_OK;
16071}
16072
16073/*
16074** Deinitialize this module.
16075*/
16076static void memsys3Shutdown(void *NotUsed){
16077  UNUSED_PARAMETER(NotUsed);
16078  mem3.mutex = 0;
16079  return;
16080}
16081
16082
16083
16084/*
16085** Open the file indicated and write a log of all unfreed memory
16086** allocations into that log.
16087*/
16088SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
16089#ifdef SQLITE_DEBUG
16090  FILE *out;
16091  u32 i, j;
16092  u32 size;
16093  if( zFilename==0 || zFilename[0]==0 ){
16094    out = stdout;
16095  }else{
16096    out = fopen(zFilename, "w");
16097    if( out==0 ){
16098      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16099                      zFilename);
16100      return;
16101    }
16102  }
16103  memsys3Enter();
16104  fprintf(out, "CHUNKS:\n");
16105  for(i=1; i<=mem3.nPool; i+=size/4){
16106    size = mem3.aPool[i-1].u.hdr.size4x;
16107    if( size/4<=1 ){
16108      fprintf(out, "%p size error\n", &mem3.aPool[i]);
16109      assert( 0 );
16110      break;
16111    }
16112    if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
16113      fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
16114      assert( 0 );
16115      break;
16116    }
16117    if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
16118      fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
16119      assert( 0 );
16120      break;
16121    }
16122    if( size&1 ){
16123      fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
16124    }else{
16125      fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
16126                  i==mem3.iMaster ? " **master**" : "");
16127    }
16128  }
16129  for(i=0; i<MX_SMALL-1; i++){
16130    if( mem3.aiSmall[i]==0 ) continue;
16131    fprintf(out, "small(%2d):", i);
16132    for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
16133      fprintf(out, " %p(%d)", &mem3.aPool[j],
16134              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
16135    }
16136    fprintf(out, "\n");
16137  }
16138  for(i=0; i<N_HASH; i++){
16139    if( mem3.aiHash[i]==0 ) continue;
16140    fprintf(out, "hash(%2d):", i);
16141    for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
16142      fprintf(out, " %p(%d)", &mem3.aPool[j],
16143              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
16144    }
16145    fprintf(out, "\n");
16146  }
16147  fprintf(out, "master=%d\n", mem3.iMaster);
16148  fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
16149  fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
16150  sqlite3_mutex_leave(mem3.mutex);
16151  if( out==stdout ){
16152    fflush(stdout);
16153  }else{
16154    fclose(out);
16155  }
16156#else
16157  UNUSED_PARAMETER(zFilename);
16158#endif
16159}
16160
16161/*
16162** This routine is the only routine in this file with external
16163** linkage.
16164**
16165** Populate the low-level memory allocation function pointers in
16166** sqlite3GlobalConfig.m with pointers to the routines in this file. The
16167** arguments specify the block of memory to manage.
16168**
16169** This routine is only called by sqlite3_config(), and therefore
16170** is not required to be threadsafe (it is not).
16171*/
16172SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
16173  static const sqlite3_mem_methods mempoolMethods = {
16174     memsys3Malloc,
16175     memsys3Free,
16176     memsys3Realloc,
16177     memsys3Size,
16178     memsys3Roundup,
16179     memsys3Init,
16180     memsys3Shutdown,
16181     0
16182  };
16183  return &mempoolMethods;
16184}
16185
16186#endif /* SQLITE_ENABLE_MEMSYS3 */
16187
16188/************** End of mem3.c ************************************************/
16189/************** Begin file mem5.c ********************************************/
16190/*
16191** 2007 October 14
16192**
16193** The author disclaims copyright to this source code.  In place of
16194** a legal notice, here is a blessing:
16195**
16196**    May you do good and not evil.
16197**    May you find forgiveness for yourself and forgive others.
16198**    May you share freely, never taking more than you give.
16199**
16200*************************************************************************
16201** This file contains the C functions that implement a memory
16202** allocation subsystem for use by SQLite.
16203**
16204** This version of the memory allocation subsystem omits all
16205** use of malloc(). The application gives SQLite a block of memory
16206** before calling sqlite3_initialize() from which allocations
16207** are made and returned by the xMalloc() and xRealloc()
16208** implementations. Once sqlite3_initialize() has been called,
16209** the amount of memory available to SQLite is fixed and cannot
16210** be changed.
16211**
16212** This version of the memory allocation subsystem is included
16213** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
16214**
16215** This memory allocator uses the following algorithm:
16216**
16217**   1.  All memory allocations sizes are rounded up to a power of 2.
16218**
16219**   2.  If two adjacent free blocks are the halves of a larger block,
16220**       then the two blocks are coalesed into the single larger block.
16221**
16222**   3.  New memory is allocated from the first available free block.
16223**
16224** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
16225** Concerning Dynamic Storage Allocation". Journal of the Association for
16226** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
16227**
16228** Let n be the size of the largest allocation divided by the minimum
16229** allocation size (after rounding all sizes up to a power of 2.)  Let M
16230** be the maximum amount of memory ever outstanding at one time.  Let
16231** N be the total amount of memory available for allocation.  Robson
16232** proved that this memory allocator will never breakdown due to
16233** fragmentation as long as the following constraint holds:
16234**
16235**      N >=  M*(1 + log2(n)/2) - n + 1
16236**
16237** The sqlite3_status() logic tracks the maximum values of n and M so
16238** that an application can, at any time, verify this constraint.
16239*/
16240
16241/*
16242** This version of the memory allocator is used only when
16243** SQLITE_ENABLE_MEMSYS5 is defined.
16244*/
16245#ifdef SQLITE_ENABLE_MEMSYS5
16246
16247/*
16248** A minimum allocation is an instance of the following structure.
16249** Larger allocations are an array of these structures where the
16250** size of the array is a power of 2.
16251**
16252** The size of this object must be a power of two.  That fact is
16253** verified in memsys5Init().
16254*/
16255typedef struct Mem5Link Mem5Link;
16256struct Mem5Link {
16257  int next;       /* Index of next free chunk */
16258  int prev;       /* Index of previous free chunk */
16259};
16260
16261/*
16262** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
16263** mem5.szAtom is always at least 8 and 32-bit integers are used,
16264** it is not actually possible to reach this limit.
16265*/
16266#define LOGMAX 30
16267
16268/*
16269** Masks used for mem5.aCtrl[] elements.
16270*/
16271#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
16272#define CTRL_FREE     0x20    /* True if not checked out */
16273
16274/*
16275** All of the static variables used by this module are collected
16276** into a single structure named "mem5".  This is to keep the
16277** static variables organized and to reduce namespace pollution
16278** when this module is combined with other in the amalgamation.
16279*/
16280static SQLITE_WSD struct Mem5Global {
16281  /*
16282  ** Memory available for allocation
16283  */
16284  int szAtom;      /* Smallest possible allocation in bytes */
16285  int nBlock;      /* Number of szAtom sized blocks in zPool */
16286  u8 *zPool;       /* Memory available to be allocated */
16287
16288  /*
16289  ** Mutex to control access to the memory allocation subsystem.
16290  */
16291  sqlite3_mutex *mutex;
16292
16293  /*
16294  ** Performance statistics
16295  */
16296  u64 nAlloc;         /* Total number of calls to malloc */
16297  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
16298  u64 totalExcess;    /* Total internal fragmentation */
16299  u32 currentOut;     /* Current checkout, including internal fragmentation */
16300  u32 currentCount;   /* Current number of distinct checkouts */
16301  u32 maxOut;         /* Maximum instantaneous currentOut */
16302  u32 maxCount;       /* Maximum instantaneous currentCount */
16303  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
16304
16305  /*
16306  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
16307  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
16308  ** and so forth.
16309  */
16310  int aiFreelist[LOGMAX+1];
16311
16312  /*
16313  ** Space for tracking which blocks are checked out and the size
16314  ** of each block.  One byte per block.
16315  */
16316  u8 *aCtrl;
16317
16318} mem5;
16319
16320/*
16321** Access the static variable through a macro for SQLITE_OMIT_WSD
16322*/
16323#define mem5 GLOBAL(struct Mem5Global, mem5)
16324
16325/*
16326** Assuming mem5.zPool is divided up into an array of Mem5Link
16327** structures, return a pointer to the idx-th such lik.
16328*/
16329#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
16330
16331/*
16332** Unlink the chunk at mem5.aPool[i] from list it is currently
16333** on.  It should be found on mem5.aiFreelist[iLogsize].
16334*/
16335static void memsys5Unlink(int i, int iLogsize){
16336  int next, prev;
16337  assert( i>=0 && i<mem5.nBlock );
16338  assert( iLogsize>=0 && iLogsize<=LOGMAX );
16339  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
16340
16341  next = MEM5LINK(i)->next;
16342  prev = MEM5LINK(i)->prev;
16343  if( prev<0 ){
16344    mem5.aiFreelist[iLogsize] = next;
16345  }else{
16346    MEM5LINK(prev)->next = next;
16347  }
16348  if( next>=0 ){
16349    MEM5LINK(next)->prev = prev;
16350  }
16351}
16352
16353/*
16354** Link the chunk at mem5.aPool[i] so that is on the iLogsize
16355** free list.
16356*/
16357static void memsys5Link(int i, int iLogsize){
16358  int x;
16359  assert( sqlite3_mutex_held(mem5.mutex) );
16360  assert( i>=0 && i<mem5.nBlock );
16361  assert( iLogsize>=0 && iLogsize<=LOGMAX );
16362  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
16363
16364  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
16365  MEM5LINK(i)->prev = -1;
16366  if( x>=0 ){
16367    assert( x<mem5.nBlock );
16368    MEM5LINK(x)->prev = i;
16369  }
16370  mem5.aiFreelist[iLogsize] = i;
16371}
16372
16373/*
16374** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
16375** will already be held (obtained by code in malloc.c) if
16376** sqlite3GlobalConfig.bMemStat is true.
16377*/
16378static void memsys5Enter(void){
16379  sqlite3_mutex_enter(mem5.mutex);
16380}
16381static void memsys5Leave(void){
16382  sqlite3_mutex_leave(mem5.mutex);
16383}
16384
16385/*
16386** Return the size of an outstanding allocation, in bytes.  The
16387** size returned omits the 8-byte header overhead.  This only
16388** works for chunks that are currently checked out.
16389*/
16390static int memsys5Size(void *p){
16391  int iSize = 0;
16392  if( p ){
16393    int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
16394    assert( i>=0 && i<mem5.nBlock );
16395    iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
16396  }
16397  return iSize;
16398}
16399
16400/*
16401** Find the first entry on the freelist iLogsize.  Unlink that
16402** entry and return its index.
16403*/
16404static int memsys5UnlinkFirst(int iLogsize){
16405  int i;
16406  int iFirst;
16407
16408  assert( iLogsize>=0 && iLogsize<=LOGMAX );
16409  i = iFirst = mem5.aiFreelist[iLogsize];
16410  assert( iFirst>=0 );
16411  while( i>0 ){
16412    if( i<iFirst ) iFirst = i;
16413    i = MEM5LINK(i)->next;
16414  }
16415  memsys5Unlink(iFirst, iLogsize);
16416  return iFirst;
16417}
16418
16419/*
16420** Return a block of memory of at least nBytes in size.
16421** Return NULL if unable.  Return NULL if nBytes==0.
16422**
16423** The caller guarantees that nByte positive.
16424**
16425** The caller has obtained a mutex prior to invoking this
16426** routine so there is never any chance that two or more
16427** threads can be in this routine at the same time.
16428*/
16429static void *memsys5MallocUnsafe(int nByte){
16430  int i;           /* Index of a mem5.aPool[] slot */
16431  int iBin;        /* Index into mem5.aiFreelist[] */
16432  int iFullSz;     /* Size of allocation rounded up to power of 2 */
16433  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
16434
16435  /* nByte must be a positive */
16436  assert( nByte>0 );
16437
16438  /* Keep track of the maximum allocation request.  Even unfulfilled
16439  ** requests are counted */
16440  if( (u32)nByte>mem5.maxRequest ){
16441    mem5.maxRequest = nByte;
16442  }
16443
16444  /* Abort if the requested allocation size is larger than the largest
16445  ** power of two that we can represent using 32-bit signed integers.
16446  */
16447  if( nByte > 0x40000000 ){
16448    return 0;
16449  }
16450
16451  /* Round nByte up to the next valid power of two */
16452  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
16453
16454  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
16455  ** block.  If not, then split a block of the next larger power of
16456  ** two in order to create a new free block of size iLogsize.
16457  */
16458  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
16459  if( iBin>LOGMAX ){
16460    testcase( sqlite3GlobalConfig.xLog!=0 );
16461    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
16462    return 0;
16463  }
16464  i = memsys5UnlinkFirst(iBin);
16465  while( iBin>iLogsize ){
16466    int newSize;
16467
16468    iBin--;
16469    newSize = 1 << iBin;
16470    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
16471    memsys5Link(i+newSize, iBin);
16472  }
16473  mem5.aCtrl[i] = iLogsize;
16474
16475  /* Update allocator performance statistics. */
16476  mem5.nAlloc++;
16477  mem5.totalAlloc += iFullSz;
16478  mem5.totalExcess += iFullSz - nByte;
16479  mem5.currentCount++;
16480  mem5.currentOut += iFullSz;
16481  if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
16482  if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
16483
16484  /* Return a pointer to the allocated memory. */
16485  return (void*)&mem5.zPool[i*mem5.szAtom];
16486}
16487
16488/*
16489** Free an outstanding memory allocation.
16490*/
16491static void memsys5FreeUnsafe(void *pOld){
16492  u32 size, iLogsize;
16493  int iBlock;
16494
16495  /* Set iBlock to the index of the block pointed to by pOld in
16496  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
16497  */
16498  iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
16499
16500  /* Check that the pointer pOld points to a valid, non-free block. */
16501  assert( iBlock>=0 && iBlock<mem5.nBlock );
16502  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
16503  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
16504
16505  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
16506  size = 1<<iLogsize;
16507  assert( iBlock+size-1<(u32)mem5.nBlock );
16508
16509  mem5.aCtrl[iBlock] |= CTRL_FREE;
16510  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
16511  assert( mem5.currentCount>0 );
16512  assert( mem5.currentOut>=(size*mem5.szAtom) );
16513  mem5.currentCount--;
16514  mem5.currentOut -= size*mem5.szAtom;
16515  assert( mem5.currentOut>0 || mem5.currentCount==0 );
16516  assert( mem5.currentCount>0 || mem5.currentOut==0 );
16517
16518  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16519  while( ALWAYS(iLogsize<LOGMAX) ){
16520    int iBuddy;
16521    if( (iBlock>>iLogsize) & 1 ){
16522      iBuddy = iBlock - size;
16523    }else{
16524      iBuddy = iBlock + size;
16525    }
16526    assert( iBuddy>=0 );
16527    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
16528    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
16529    memsys5Unlink(iBuddy, iLogsize);
16530    iLogsize++;
16531    if( iBuddy<iBlock ){
16532      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
16533      mem5.aCtrl[iBlock] = 0;
16534      iBlock = iBuddy;
16535    }else{
16536      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
16537      mem5.aCtrl[iBuddy] = 0;
16538    }
16539    size *= 2;
16540  }
16541  memsys5Link(iBlock, iLogsize);
16542}
16543
16544/*
16545** Allocate nBytes of memory
16546*/
16547static void *memsys5Malloc(int nBytes){
16548  sqlite3_int64 *p = 0;
16549  if( nBytes>0 ){
16550    memsys5Enter();
16551    p = memsys5MallocUnsafe(nBytes);
16552    memsys5Leave();
16553  }
16554  return (void*)p;
16555}
16556
16557/*
16558** Free memory.
16559**
16560** The outer layer memory allocator prevents this routine from
16561** being called with pPrior==0.
16562*/
16563static void memsys5Free(void *pPrior){
16564  assert( pPrior!=0 );
16565  memsys5Enter();
16566  memsys5FreeUnsafe(pPrior);
16567  memsys5Leave();
16568}
16569
16570/*
16571** Change the size of an existing memory allocation.
16572**
16573** The outer layer memory allocator prevents this routine from
16574** being called with pPrior==0.
16575**
16576** nBytes is always a value obtained from a prior call to
16577** memsys5Round().  Hence nBytes is always a non-negative power
16578** of two.  If nBytes==0 that means that an oversize allocation
16579** (an allocation larger than 0x40000000) was requested and this
16580** routine should return 0 without freeing pPrior.
16581*/
16582static void *memsys5Realloc(void *pPrior, int nBytes){
16583  int nOld;
16584  void *p;
16585  assert( pPrior!=0 );
16586  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
16587  assert( nBytes>=0 );
16588  if( nBytes==0 ){
16589    return 0;
16590  }
16591  nOld = memsys5Size(pPrior);
16592  if( nBytes<=nOld ){
16593    return pPrior;
16594  }
16595  memsys5Enter();
16596  p = memsys5MallocUnsafe(nBytes);
16597  if( p ){
16598    memcpy(p, pPrior, nOld);
16599    memsys5FreeUnsafe(pPrior);
16600  }
16601  memsys5Leave();
16602  return p;
16603}
16604
16605/*
16606** Round up a request size to the next valid allocation size.  If
16607** the allocation is too large to be handled by this allocation system,
16608** return 0.
16609**
16610** All allocations must be a power of two and must be expressed by a
16611** 32-bit signed integer.  Hence the largest allocation is 0x40000000
16612** or 1073741824 bytes.
16613*/
16614static int memsys5Roundup(int n){
16615  int iFullSz;
16616  if( n > 0x40000000 ) return 0;
16617  for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
16618  return iFullSz;
16619}
16620
16621/*
16622** Return the ceiling of the logarithm base 2 of iValue.
16623**
16624** Examples:   memsys5Log(1) -> 0
16625**             memsys5Log(2) -> 1
16626**             memsys5Log(4) -> 2
16627**             memsys5Log(5) -> 3
16628**             memsys5Log(8) -> 3
16629**             memsys5Log(9) -> 4
16630*/
16631static int memsys5Log(int iValue){
16632  int iLog;
16633  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
16634  return iLog;
16635}
16636
16637/*
16638** Initialize the memory allocator.
16639**
16640** This routine is not threadsafe.  The caller must be holding a mutex
16641** to prevent multiple threads from entering at the same time.
16642*/
16643static int memsys5Init(void *NotUsed){
16644  int ii;            /* Loop counter */
16645  int nByte;         /* Number of bytes of memory available to this allocator */
16646  u8 *zByte;         /* Memory usable by this allocator */
16647  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
16648  int iOffset;       /* An offset into mem5.aCtrl[] */
16649
16650  UNUSED_PARAMETER(NotUsed);
16651
16652  /* For the purposes of this routine, disable the mutex */
16653  mem5.mutex = 0;
16654
16655  /* The size of a Mem5Link object must be a power of two.  Verify that
16656  ** this is case.
16657  */
16658  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
16659
16660  nByte = sqlite3GlobalConfig.nHeap;
16661  zByte = (u8*)sqlite3GlobalConfig.pHeap;
16662  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
16663
16664  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
16665  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
16666  mem5.szAtom = (1<<nMinLog);
16667  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
16668    mem5.szAtom = mem5.szAtom << 1;
16669  }
16670
16671  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
16672  mem5.zPool = zByte;
16673  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
16674
16675  for(ii=0; ii<=LOGMAX; ii++){
16676    mem5.aiFreelist[ii] = -1;
16677  }
16678
16679  iOffset = 0;
16680  for(ii=LOGMAX; ii>=0; ii--){
16681    int nAlloc = (1<<ii);
16682    if( (iOffset+nAlloc)<=mem5.nBlock ){
16683      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
16684      memsys5Link(iOffset, ii);
16685      iOffset += nAlloc;
16686    }
16687    assert((iOffset+nAlloc)>mem5.nBlock);
16688  }
16689
16690  /* If a mutex is required for normal operation, allocate one */
16691  if( sqlite3GlobalConfig.bMemstat==0 ){
16692    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16693  }
16694
16695  return SQLITE_OK;
16696}
16697
16698/*
16699** Deinitialize this module.
16700*/
16701static void memsys5Shutdown(void *NotUsed){
16702  UNUSED_PARAMETER(NotUsed);
16703  mem5.mutex = 0;
16704  return;
16705}
16706
16707#ifdef SQLITE_TEST
16708/*
16709** Open the file indicated and write a log of all unfreed memory
16710** allocations into that log.
16711*/
16712SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
16713  FILE *out;
16714  int i, j, n;
16715  int nMinLog;
16716
16717  if( zFilename==0 || zFilename[0]==0 ){
16718    out = stdout;
16719  }else{
16720    out = fopen(zFilename, "w");
16721    if( out==0 ){
16722      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16723                      zFilename);
16724      return;
16725    }
16726  }
16727  memsys5Enter();
16728  nMinLog = memsys5Log(mem5.szAtom);
16729  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
16730    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
16731    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
16732  }
16733  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
16734  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
16735  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
16736  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
16737  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
16738  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
16739  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
16740  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
16741  memsys5Leave();
16742  if( out==stdout ){
16743    fflush(stdout);
16744  }else{
16745    fclose(out);
16746  }
16747}
16748#endif
16749
16750/*
16751** This routine is the only routine in this file with external
16752** linkage. It returns a pointer to a static sqlite3_mem_methods
16753** struct populated with the memsys5 methods.
16754*/
16755SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
16756  static const sqlite3_mem_methods memsys5Methods = {
16757     memsys5Malloc,
16758     memsys5Free,
16759     memsys5Realloc,
16760     memsys5Size,
16761     memsys5Roundup,
16762     memsys5Init,
16763     memsys5Shutdown,
16764     0
16765  };
16766  return &memsys5Methods;
16767}
16768
16769#endif /* SQLITE_ENABLE_MEMSYS5 */
16770
16771/************** End of mem5.c ************************************************/
16772/************** Begin file mutex.c *******************************************/
16773/*
16774** 2007 August 14
16775**
16776** The author disclaims copyright to this source code.  In place of
16777** a legal notice, here is a blessing:
16778**
16779**    May you do good and not evil.
16780**    May you find forgiveness for yourself and forgive others.
16781**    May you share freely, never taking more than you give.
16782**
16783*************************************************************************
16784** This file contains the C functions that implement mutexes.
16785**
16786** This file contains code that is common across all mutex implementations.
16787*/
16788
16789#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
16790/*
16791** For debugging purposes, record when the mutex subsystem is initialized
16792** and uninitialized so that we can assert() if there is an attempt to
16793** allocate a mutex while the system is uninitialized.
16794*/
16795static SQLITE_WSD int mutexIsInit = 0;
16796#endif /* SQLITE_DEBUG */
16797
16798
16799#ifndef SQLITE_MUTEX_OMIT
16800/*
16801** Initialize the mutex system.
16802*/
16803SQLITE_PRIVATE int sqlite3MutexInit(void){
16804  int rc = SQLITE_OK;
16805  if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
16806    /* If the xMutexAlloc method has not been set, then the user did not
16807    ** install a mutex implementation via sqlite3_config() prior to
16808    ** sqlite3_initialize() being called. This block copies pointers to
16809    ** the default implementation into the sqlite3GlobalConfig structure.
16810    */
16811    sqlite3_mutex_methods const *pFrom;
16812    sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
16813
16814    if( sqlite3GlobalConfig.bCoreMutex ){
16815      pFrom = sqlite3DefaultMutex();
16816    }else{
16817      pFrom = sqlite3NoopMutex();
16818    }
16819    memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
16820    memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
16821           sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
16822    pTo->xMutexAlloc = pFrom->xMutexAlloc;
16823  }
16824  rc = sqlite3GlobalConfig.mutex.xMutexInit();
16825
16826#ifdef SQLITE_DEBUG
16827  GLOBAL(int, mutexIsInit) = 1;
16828#endif
16829
16830  return rc;
16831}
16832
16833/*
16834** Shutdown the mutex system. This call frees resources allocated by
16835** sqlite3MutexInit().
16836*/
16837SQLITE_PRIVATE int sqlite3MutexEnd(void){
16838  int rc = SQLITE_OK;
16839  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
16840    rc = sqlite3GlobalConfig.mutex.xMutexEnd();
16841  }
16842
16843#ifdef SQLITE_DEBUG
16844  GLOBAL(int, mutexIsInit) = 0;
16845#endif
16846
16847  return rc;
16848}
16849
16850/*
16851** Retrieve a pointer to a static mutex or allocate a new dynamic one.
16852*/
16853SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
16854#ifndef SQLITE_OMIT_AUTOINIT
16855  if( sqlite3_initialize() ) return 0;
16856#endif
16857  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16858}
16859
16860SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
16861  if( !sqlite3GlobalConfig.bCoreMutex ){
16862    return 0;
16863  }
16864  assert( GLOBAL(int, mutexIsInit) );
16865  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
16866}
16867
16868/*
16869** Free a dynamic mutex.
16870*/
16871SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
16872  if( p ){
16873    sqlite3GlobalConfig.mutex.xMutexFree(p);
16874  }
16875}
16876
16877/*
16878** Obtain the mutex p. If some other thread already has the mutex, block
16879** until it can be obtained.
16880*/
16881SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
16882  if( p ){
16883    sqlite3GlobalConfig.mutex.xMutexEnter(p);
16884  }
16885}
16886
16887/*
16888** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
16889** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
16890*/
16891SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
16892  int rc = SQLITE_OK;
16893  if( p ){
16894    return sqlite3GlobalConfig.mutex.xMutexTry(p);
16895  }
16896  return rc;
16897}
16898
16899/*
16900** The sqlite3_mutex_leave() routine exits a mutex that was previously
16901** entered by the same thread.  The behavior is undefined if the mutex
16902** is not currently entered. If a NULL pointer is passed as an argument
16903** this function is a no-op.
16904*/
16905SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
16906  if( p ){
16907    sqlite3GlobalConfig.mutex.xMutexLeave(p);
16908  }
16909}
16910
16911#ifndef NDEBUG
16912/*
16913** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
16914** intended for use inside assert() statements.
16915*/
16916SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
16917  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
16918}
16919SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
16920  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
16921}
16922#endif
16923
16924#endif /* SQLITE_MUTEX_OMIT */
16925
16926/************** End of mutex.c ***********************************************/
16927/************** Begin file mutex_noop.c **************************************/
16928/*
16929** 2008 October 07
16930**
16931** The author disclaims copyright to this source code.  In place of
16932** a legal notice, here is a blessing:
16933**
16934**    May you do good and not evil.
16935**    May you find forgiveness for yourself and forgive others.
16936**    May you share freely, never taking more than you give.
16937**
16938*************************************************************************
16939** This file contains the C functions that implement mutexes.
16940**
16941** This implementation in this file does not provide any mutual
16942** exclusion and is thus suitable for use only in applications
16943** that use SQLite in a single thread.  The routines defined
16944** here are place-holders.  Applications can substitute working
16945** mutex routines at start-time using the
16946**
16947**     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
16948**
16949** interface.
16950**
16951** If compiled with SQLITE_DEBUG, then additional logic is inserted
16952** that does error checking on mutexes to make sure they are being
16953** called correctly.
16954*/
16955
16956#ifndef SQLITE_MUTEX_OMIT
16957
16958#ifndef SQLITE_DEBUG
16959/*
16960** Stub routines for all mutex methods.
16961**
16962** This routines provide no mutual exclusion or error checking.
16963*/
16964static int noopMutexInit(void){ return SQLITE_OK; }
16965static int noopMutexEnd(void){ return SQLITE_OK; }
16966static sqlite3_mutex *noopMutexAlloc(int id){
16967  UNUSED_PARAMETER(id);
16968  return (sqlite3_mutex*)8;
16969}
16970static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16971static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16972static int noopMutexTry(sqlite3_mutex *p){
16973  UNUSED_PARAMETER(p);
16974  return SQLITE_OK;
16975}
16976static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
16977
16978SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
16979  static const sqlite3_mutex_methods sMutex = {
16980    noopMutexInit,
16981    noopMutexEnd,
16982    noopMutexAlloc,
16983    noopMutexFree,
16984    noopMutexEnter,
16985    noopMutexTry,
16986    noopMutexLeave,
16987
16988    0,
16989    0,
16990  };
16991
16992  return &sMutex;
16993}
16994#endif /* !SQLITE_DEBUG */
16995
16996#ifdef SQLITE_DEBUG
16997/*
16998** In this implementation, error checking is provided for testing
16999** and debugging purposes.  The mutexes still do not provide any
17000** mutual exclusion.
17001*/
17002
17003/*
17004** The mutex object
17005*/
17006typedef struct sqlite3_debug_mutex {
17007  int id;     /* The mutex type */
17008  int cnt;    /* Number of entries without a matching leave */
17009} sqlite3_debug_mutex;
17010
17011/*
17012** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17013** intended for use inside assert() statements.
17014*/
17015static int debugMutexHeld(sqlite3_mutex *pX){
17016  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17017  return p==0 || p->cnt>0;
17018}
17019static int debugMutexNotheld(sqlite3_mutex *pX){
17020  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17021  return p==0 || p->cnt==0;
17022}
17023
17024/*
17025** Initialize and deinitialize the mutex subsystem.
17026*/
17027static int debugMutexInit(void){ return SQLITE_OK; }
17028static int debugMutexEnd(void){ return SQLITE_OK; }
17029
17030/*
17031** The sqlite3_mutex_alloc() routine allocates a new
17032** mutex and returns a pointer to it.  If it returns NULL
17033** that means that a mutex could not be allocated.
17034*/
17035static sqlite3_mutex *debugMutexAlloc(int id){
17036  static sqlite3_debug_mutex aStatic[6];
17037  sqlite3_debug_mutex *pNew = 0;
17038  switch( id ){
17039    case SQLITE_MUTEX_FAST:
17040    case SQLITE_MUTEX_RECURSIVE: {
17041      pNew = sqlite3Malloc(sizeof(*pNew));
17042      if( pNew ){
17043        pNew->id = id;
17044        pNew->cnt = 0;
17045      }
17046      break;
17047    }
17048    default: {
17049      assert( id-2 >= 0 );
17050      assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
17051      pNew = &aStatic[id-2];
17052      pNew->id = id;
17053      break;
17054    }
17055  }
17056  return (sqlite3_mutex*)pNew;
17057}
17058
17059/*
17060** This routine deallocates a previously allocated mutex.
17061*/
17062static void debugMutexFree(sqlite3_mutex *pX){
17063  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17064  assert( p->cnt==0 );
17065  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17066  sqlite3_free(p);
17067}
17068
17069/*
17070** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17071** to enter a mutex.  If another thread is already within the mutex,
17072** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17073** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17074** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17075** be entered multiple times by the same thread.  In such cases the,
17076** mutex must be exited an equal number of times before another thread
17077** can enter.  If the same thread tries to enter any other kind of mutex
17078** more than once, the behavior is undefined.
17079*/
17080static void debugMutexEnter(sqlite3_mutex *pX){
17081  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17082  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
17083  p->cnt++;
17084}
17085static int debugMutexTry(sqlite3_mutex *pX){
17086  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17087  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
17088  p->cnt++;
17089  return SQLITE_OK;
17090}
17091
17092/*
17093** The sqlite3_mutex_leave() routine exits a mutex that was
17094** previously entered by the same thread.  The behavior
17095** is undefined if the mutex is not currently entered or
17096** is not currently allocated.  SQLite will never do either.
17097*/
17098static void debugMutexLeave(sqlite3_mutex *pX){
17099  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
17100  assert( debugMutexHeld(pX) );
17101  p->cnt--;
17102  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
17103}
17104
17105SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
17106  static const sqlite3_mutex_methods sMutex = {
17107    debugMutexInit,
17108    debugMutexEnd,
17109    debugMutexAlloc,
17110    debugMutexFree,
17111    debugMutexEnter,
17112    debugMutexTry,
17113    debugMutexLeave,
17114
17115    debugMutexHeld,
17116    debugMutexNotheld
17117  };
17118
17119  return &sMutex;
17120}
17121#endif /* SQLITE_DEBUG */
17122
17123/*
17124** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
17125** is used regardless of the run-time threadsafety setting.
17126*/
17127#ifdef SQLITE_MUTEX_NOOP
17128SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17129  return sqlite3NoopMutex();
17130}
17131#endif /* SQLITE_MUTEX_NOOP */
17132#endif /* SQLITE_MUTEX_OMIT */
17133
17134/************** End of mutex_noop.c ******************************************/
17135/************** Begin file mutex_os2.c ***************************************/
17136/*
17137** 2007 August 28
17138**
17139** The author disclaims copyright to this source code.  In place of
17140** a legal notice, here is a blessing:
17141**
17142**    May you do good and not evil.
17143**    May you find forgiveness for yourself and forgive others.
17144**    May you share freely, never taking more than you give.
17145**
17146*************************************************************************
17147** This file contains the C functions that implement mutexes for OS/2
17148*/
17149
17150/*
17151** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
17152** See the mutex.h file for details.
17153*/
17154#ifdef SQLITE_MUTEX_OS2
17155
17156/********************** OS/2 Mutex Implementation **********************
17157**
17158** This implementation of mutexes is built using the OS/2 API.
17159*/
17160
17161/*
17162** The mutex object
17163** Each recursive mutex is an instance of the following structure.
17164*/
17165struct sqlite3_mutex {
17166  HMTX mutex;       /* Mutex controlling the lock */
17167  int  id;          /* Mutex type */
17168#ifdef SQLITE_DEBUG
17169 int   trace;       /* True to trace changes */
17170#endif
17171};
17172
17173#ifdef SQLITE_DEBUG
17174#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
17175#else
17176#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
17177#endif
17178
17179/*
17180** Initialize and deinitialize the mutex subsystem.
17181*/
17182static int os2MutexInit(void){ return SQLITE_OK; }
17183static int os2MutexEnd(void){ return SQLITE_OK; }
17184
17185/*
17186** The sqlite3_mutex_alloc() routine allocates a new
17187** mutex and returns a pointer to it.  If it returns NULL
17188** that means that a mutex could not be allocated.
17189** SQLite will unwind its stack and return an error.  The argument
17190** to sqlite3_mutex_alloc() is one of these integer constants:
17191**
17192** <ul>
17193** <li>  SQLITE_MUTEX_FAST
17194** <li>  SQLITE_MUTEX_RECURSIVE
17195** <li>  SQLITE_MUTEX_STATIC_MASTER
17196** <li>  SQLITE_MUTEX_STATIC_MEM
17197** <li>  SQLITE_MUTEX_STATIC_MEM2
17198** <li>  SQLITE_MUTEX_STATIC_PRNG
17199** <li>  SQLITE_MUTEX_STATIC_LRU
17200** <li>  SQLITE_MUTEX_STATIC_LRU2
17201** </ul>
17202**
17203** The first two constants cause sqlite3_mutex_alloc() to create
17204** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17205** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17206** The mutex implementation does not need to make a distinction
17207** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17208** not want to.  But SQLite will only request a recursive mutex in
17209** cases where it really needs one.  If a faster non-recursive mutex
17210** implementation is available on the host platform, the mutex subsystem
17211** might return such a mutex in response to SQLITE_MUTEX_FAST.
17212**
17213** The other allowed parameters to sqlite3_mutex_alloc() each return
17214** a pointer to a static preexisting mutex.  Six static mutexes are
17215** used by the current version of SQLite.  Future versions of SQLite
17216** may add additional static mutexes.  Static mutexes are for internal
17217** use by SQLite only.  Applications that use SQLite mutexes should
17218** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17219** SQLITE_MUTEX_RECURSIVE.
17220**
17221** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17222** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17223** returns a different mutex on every call.  But for the static
17224** mutex types, the same mutex is returned on every call that has
17225** the same type number.
17226*/
17227static sqlite3_mutex *os2MutexAlloc(int iType){
17228  sqlite3_mutex *p = NULL;
17229  switch( iType ){
17230    case SQLITE_MUTEX_FAST:
17231    case SQLITE_MUTEX_RECURSIVE: {
17232      p = sqlite3MallocZero( sizeof(*p) );
17233      if( p ){
17234        p->id = iType;
17235        if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
17236          sqlite3_free( p );
17237          p = NULL;
17238        }
17239      }
17240      break;
17241    }
17242    default: {
17243      static volatile int isInit = 0;
17244      static sqlite3_mutex staticMutexes[6] = {
17245        SQLITE3_MUTEX_INITIALIZER,
17246        SQLITE3_MUTEX_INITIALIZER,
17247        SQLITE3_MUTEX_INITIALIZER,
17248        SQLITE3_MUTEX_INITIALIZER,
17249        SQLITE3_MUTEX_INITIALIZER,
17250        SQLITE3_MUTEX_INITIALIZER,
17251      };
17252      if ( !isInit ){
17253        APIRET rc;
17254        PTIB ptib;
17255        PPIB ppib;
17256        HMTX mutex;
17257        char name[32];
17258        DosGetInfoBlocks( &ptib, &ppib );
17259        sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
17260                          ppib->pib_ulpid );
17261        while( !isInit ){
17262          mutex = 0;
17263          rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
17264          if( rc == NO_ERROR ){
17265            unsigned int i;
17266            if( !isInit ){
17267              for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
17268                DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
17269              }
17270              isInit = 1;
17271            }
17272            DosCloseMutexSem( mutex );
17273          }else if( rc == ERROR_DUPLICATE_NAME ){
17274            DosSleep( 1 );
17275          }else{
17276            return p;
17277          }
17278        }
17279      }
17280      assert( iType-2 >= 0 );
17281      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
17282      p = &staticMutexes[iType-2];
17283      p->id = iType;
17284      break;
17285    }
17286  }
17287  return p;
17288}
17289
17290
17291/*
17292** This routine deallocates a previously allocated mutex.
17293** SQLite is careful to deallocate every mutex that it allocates.
17294*/
17295static void os2MutexFree(sqlite3_mutex *p){
17296#ifdef SQLITE_DEBUG
17297  TID tid;
17298  PID pid;
17299  ULONG ulCount;
17300  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17301  assert( ulCount==0 );
17302  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17303#endif
17304  DosCloseMutexSem( p->mutex );
17305  sqlite3_free( p );
17306}
17307
17308#ifdef SQLITE_DEBUG
17309/*
17310** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17311** intended for use inside assert() statements.
17312*/
17313static int os2MutexHeld(sqlite3_mutex *p){
17314  TID tid;
17315  PID pid;
17316  ULONG ulCount;
17317  PTIB ptib;
17318  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17319  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
17320    return 0;
17321  DosGetInfoBlocks(&ptib, NULL);
17322  return tid==ptib->tib_ptib2->tib2_ultid;
17323}
17324static int os2MutexNotheld(sqlite3_mutex *p){
17325  TID tid;
17326  PID pid;
17327  ULONG ulCount;
17328  PTIB ptib;
17329  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17330  if( ulCount==0 )
17331    return 1;
17332  DosGetInfoBlocks(&ptib, NULL);
17333  return tid!=ptib->tib_ptib2->tib2_ultid;
17334}
17335static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
17336  TID   tid;
17337  PID   pid;
17338  ULONG ulCount;
17339  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17340  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
17341}
17342#endif
17343
17344/*
17345** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17346** to enter a mutex.  If another thread is already within the mutex,
17347** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17348** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17349** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17350** be entered multiple times by the same thread.  In such cases the,
17351** mutex must be exited an equal number of times before another thread
17352** can enter.  If the same thread tries to enter any other kind of mutex
17353** more than once, the behavior is undefined.
17354*/
17355static void os2MutexEnter(sqlite3_mutex *p){
17356  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17357  DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
17358#ifdef SQLITE_DEBUG
17359  if( p->trace ) os2MutexTrace(p, "enter");
17360#endif
17361}
17362static int os2MutexTry(sqlite3_mutex *p){
17363  int rc = SQLITE_BUSY;
17364  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17365  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
17366    rc = SQLITE_OK;
17367#ifdef SQLITE_DEBUG
17368    if( p->trace ) os2MutexTrace(p, "try");
17369#endif
17370  }
17371  return rc;
17372}
17373
17374/*
17375** The sqlite3_mutex_leave() routine exits a mutex that was
17376** previously entered by the same thread.  The behavior
17377** is undefined if the mutex is not currently entered or
17378** is not currently allocated.  SQLite will never do either.
17379*/
17380static void os2MutexLeave(sqlite3_mutex *p){
17381  assert( os2MutexHeld(p) );
17382  DosReleaseMutexSem(p->mutex);
17383#ifdef SQLITE_DEBUG
17384  if( p->trace ) os2MutexTrace(p, "leave");
17385#endif
17386}
17387
17388SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17389  static const sqlite3_mutex_methods sMutex = {
17390    os2MutexInit,
17391    os2MutexEnd,
17392    os2MutexAlloc,
17393    os2MutexFree,
17394    os2MutexEnter,
17395    os2MutexTry,
17396    os2MutexLeave,
17397#ifdef SQLITE_DEBUG
17398    os2MutexHeld,
17399    os2MutexNotheld
17400#else
17401    0,
17402    0
17403#endif
17404  };
17405
17406  return &sMutex;
17407}
17408#endif /* SQLITE_MUTEX_OS2 */
17409
17410/************** End of mutex_os2.c *******************************************/
17411/************** Begin file mutex_unix.c **************************************/
17412/*
17413** 2007 August 28
17414**
17415** The author disclaims copyright to this source code.  In place of
17416** a legal notice, here is a blessing:
17417**
17418**    May you do good and not evil.
17419**    May you find forgiveness for yourself and forgive others.
17420**    May you share freely, never taking more than you give.
17421**
17422*************************************************************************
17423** This file contains the C functions that implement mutexes for pthreads
17424*/
17425
17426/*
17427** The code in this file is only used if we are compiling threadsafe
17428** under unix with pthreads.
17429**
17430** Note that this implementation requires a version of pthreads that
17431** supports recursive mutexes.
17432*/
17433#ifdef SQLITE_MUTEX_PTHREADS
17434
17435#include <pthread.h>
17436
17437/*
17438** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
17439** are necessary under two condidtions:  (1) Debug builds and (2) using
17440** home-grown mutexes.  Encapsulate these conditions into a single #define.
17441*/
17442#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
17443# define SQLITE_MUTEX_NREF 1
17444#else
17445# define SQLITE_MUTEX_NREF 0
17446#endif
17447
17448/*
17449** Each recursive mutex is an instance of the following structure.
17450*/
17451struct sqlite3_mutex {
17452  pthread_mutex_t mutex;     /* Mutex controlling the lock */
17453#if SQLITE_MUTEX_NREF
17454  int id;                    /* Mutex type */
17455  volatile int nRef;         /* Number of entrances */
17456  volatile pthread_t owner;  /* Thread that is within this mutex */
17457  int trace;                 /* True to trace changes */
17458#endif
17459};
17460#if SQLITE_MUTEX_NREF
17461#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
17462#else
17463#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
17464#endif
17465
17466/*
17467** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17468** intended for use only inside assert() statements.  On some platforms,
17469** there might be race conditions that can cause these routines to
17470** deliver incorrect results.  In particular, if pthread_equal() is
17471** not an atomic operation, then these routines might delivery
17472** incorrect results.  On most platforms, pthread_equal() is a
17473** comparison of two integers and is therefore atomic.  But we are
17474** told that HPUX is not such a platform.  If so, then these routines
17475** will not always work correctly on HPUX.
17476**
17477** On those platforms where pthread_equal() is not atomic, SQLite
17478** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
17479** make sure no assert() statements are evaluated and hence these
17480** routines are never called.
17481*/
17482#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
17483static int pthreadMutexHeld(sqlite3_mutex *p){
17484  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
17485}
17486static int pthreadMutexNotheld(sqlite3_mutex *p){
17487  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
17488}
17489#endif
17490
17491/*
17492** Initialize and deinitialize the mutex subsystem.
17493*/
17494static int pthreadMutexInit(void){ return SQLITE_OK; }
17495static int pthreadMutexEnd(void){ return SQLITE_OK; }
17496
17497/*
17498** The sqlite3_mutex_alloc() routine allocates a new
17499** mutex and returns a pointer to it.  If it returns NULL
17500** that means that a mutex could not be allocated.  SQLite
17501** will unwind its stack and return an error.  The argument
17502** to sqlite3_mutex_alloc() is one of these integer constants:
17503**
17504** <ul>
17505** <li>  SQLITE_MUTEX_FAST
17506** <li>  SQLITE_MUTEX_RECURSIVE
17507** <li>  SQLITE_MUTEX_STATIC_MASTER
17508** <li>  SQLITE_MUTEX_STATIC_MEM
17509** <li>  SQLITE_MUTEX_STATIC_MEM2
17510** <li>  SQLITE_MUTEX_STATIC_PRNG
17511** <li>  SQLITE_MUTEX_STATIC_LRU
17512** <li>  SQLITE_MUTEX_STATIC_PMEM
17513** </ul>
17514**
17515** The first two constants cause sqlite3_mutex_alloc() to create
17516** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17517** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17518** The mutex implementation does not need to make a distinction
17519** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17520** not want to.  But SQLite will only request a recursive mutex in
17521** cases where it really needs one.  If a faster non-recursive mutex
17522** implementation is available on the host platform, the mutex subsystem
17523** might return such a mutex in response to SQLITE_MUTEX_FAST.
17524**
17525** The other allowed parameters to sqlite3_mutex_alloc() each return
17526** a pointer to a static preexisting mutex.  Six static mutexes are
17527** used by the current version of SQLite.  Future versions of SQLite
17528** may add additional static mutexes.  Static mutexes are for internal
17529** use by SQLite only.  Applications that use SQLite mutexes should
17530** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17531** SQLITE_MUTEX_RECURSIVE.
17532**
17533** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17534** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17535** returns a different mutex on every call.  But for the static
17536** mutex types, the same mutex is returned on every call that has
17537** the same type number.
17538*/
17539static sqlite3_mutex *pthreadMutexAlloc(int iType){
17540  static sqlite3_mutex staticMutexes[] = {
17541    SQLITE3_MUTEX_INITIALIZER,
17542    SQLITE3_MUTEX_INITIALIZER,
17543    SQLITE3_MUTEX_INITIALIZER,
17544    SQLITE3_MUTEX_INITIALIZER,
17545    SQLITE3_MUTEX_INITIALIZER,
17546    SQLITE3_MUTEX_INITIALIZER
17547  };
17548  sqlite3_mutex *p;
17549  switch( iType ){
17550    case SQLITE_MUTEX_RECURSIVE: {
17551      p = sqlite3MallocZero( sizeof(*p) );
17552      if( p ){
17553#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17554        /* If recursive mutexes are not available, we will have to
17555        ** build our own.  See below. */
17556        pthread_mutex_init(&p->mutex, 0);
17557#else
17558        /* Use a recursive mutex if it is available */
17559        pthread_mutexattr_t recursiveAttr;
17560        pthread_mutexattr_init(&recursiveAttr);
17561        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
17562        pthread_mutex_init(&p->mutex, &recursiveAttr);
17563        pthread_mutexattr_destroy(&recursiveAttr);
17564#endif
17565#if SQLITE_MUTEX_NREF
17566        p->id = iType;
17567#endif
17568      }
17569      break;
17570    }
17571    case SQLITE_MUTEX_FAST: {
17572      p = sqlite3MallocZero( sizeof(*p) );
17573      if( p ){
17574#if SQLITE_MUTEX_NREF
17575        p->id = iType;
17576#endif
17577        pthread_mutex_init(&p->mutex, 0);
17578      }
17579      break;
17580    }
17581    default: {
17582      assert( iType-2 >= 0 );
17583      assert( iType-2 < ArraySize(staticMutexes) );
17584      p = &staticMutexes[iType-2];
17585#if SQLITE_MUTEX_NREF
17586      p->id = iType;
17587#endif
17588      break;
17589    }
17590  }
17591  return p;
17592}
17593
17594
17595/*
17596** This routine deallocates a previously
17597** allocated mutex.  SQLite is careful to deallocate every
17598** mutex that it allocates.
17599*/
17600static void pthreadMutexFree(sqlite3_mutex *p){
17601  assert( p->nRef==0 );
17602  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17603  pthread_mutex_destroy(&p->mutex);
17604  sqlite3_free(p);
17605}
17606
17607/*
17608** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17609** to enter a mutex.  If another thread is already within the mutex,
17610** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17611** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17612** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17613** be entered multiple times by the same thread.  In such cases the,
17614** mutex must be exited an equal number of times before another thread
17615** can enter.  If the same thread tries to enter any other kind of mutex
17616** more than once, the behavior is undefined.
17617*/
17618static void pthreadMutexEnter(sqlite3_mutex *p){
17619  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17620
17621#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17622  /* If recursive mutexes are not available, then we have to grow
17623  ** our own.  This implementation assumes that pthread_equal()
17624  ** is atomic - that it cannot be deceived into thinking self
17625  ** and p->owner are equal if p->owner changes between two values
17626  ** that are not equal to self while the comparison is taking place.
17627  ** This implementation also assumes a coherent cache - that
17628  ** separate processes cannot read different values from the same
17629  ** address at the same time.  If either of these two conditions
17630  ** are not met, then the mutexes will fail and problems will result.
17631  */
17632  {
17633    pthread_t self = pthread_self();
17634    if( p->nRef>0 && pthread_equal(p->owner, self) ){
17635      p->nRef++;
17636    }else{
17637      pthread_mutex_lock(&p->mutex);
17638      assert( p->nRef==0 );
17639      p->owner = self;
17640      p->nRef = 1;
17641    }
17642  }
17643#else
17644  /* Use the built-in recursive mutexes if they are available.
17645  */
17646  pthread_mutex_lock(&p->mutex);
17647#if SQLITE_MUTEX_NREF
17648  assert( p->nRef>0 || p->owner==0 );
17649  p->owner = pthread_self();
17650  p->nRef++;
17651#endif
17652#endif
17653
17654#ifdef SQLITE_DEBUG
17655  if( p->trace ){
17656    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17657  }
17658#endif
17659}
17660static int pthreadMutexTry(sqlite3_mutex *p){
17661  int rc;
17662  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
17663
17664#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17665  /* If recursive mutexes are not available, then we have to grow
17666  ** our own.  This implementation assumes that pthread_equal()
17667  ** is atomic - that it cannot be deceived into thinking self
17668  ** and p->owner are equal if p->owner changes between two values
17669  ** that are not equal to self while the comparison is taking place.
17670  ** This implementation also assumes a coherent cache - that
17671  ** separate processes cannot read different values from the same
17672  ** address at the same time.  If either of these two conditions
17673  ** are not met, then the mutexes will fail and problems will result.
17674  */
17675  {
17676    pthread_t self = pthread_self();
17677    if( p->nRef>0 && pthread_equal(p->owner, self) ){
17678      p->nRef++;
17679      rc = SQLITE_OK;
17680    }else if( pthread_mutex_trylock(&p->mutex)==0 ){
17681      assert( p->nRef==0 );
17682      p->owner = self;
17683      p->nRef = 1;
17684      rc = SQLITE_OK;
17685    }else{
17686      rc = SQLITE_BUSY;
17687    }
17688  }
17689#else
17690  /* Use the built-in recursive mutexes if they are available.
17691  */
17692  if( pthread_mutex_trylock(&p->mutex)==0 ){
17693#if SQLITE_MUTEX_NREF
17694    p->owner = pthread_self();
17695    p->nRef++;
17696#endif
17697    rc = SQLITE_OK;
17698  }else{
17699    rc = SQLITE_BUSY;
17700  }
17701#endif
17702
17703#ifdef SQLITE_DEBUG
17704  if( rc==SQLITE_OK && p->trace ){
17705    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17706  }
17707#endif
17708  return rc;
17709}
17710
17711/*
17712** The sqlite3_mutex_leave() routine exits a mutex that was
17713** previously entered by the same thread.  The behavior
17714** is undefined if the mutex is not currently entered or
17715** is not currently allocated.  SQLite will never do either.
17716*/
17717static void pthreadMutexLeave(sqlite3_mutex *p){
17718  assert( pthreadMutexHeld(p) );
17719#if SQLITE_MUTEX_NREF
17720  p->nRef--;
17721  if( p->nRef==0 ) p->owner = 0;
17722#endif
17723  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17724
17725#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
17726  if( p->nRef==0 ){
17727    pthread_mutex_unlock(&p->mutex);
17728  }
17729#else
17730  pthread_mutex_unlock(&p->mutex);
17731#endif
17732
17733#ifdef SQLITE_DEBUG
17734  if( p->trace ){
17735    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17736  }
17737#endif
17738}
17739
17740SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17741  static const sqlite3_mutex_methods sMutex = {
17742    pthreadMutexInit,
17743    pthreadMutexEnd,
17744    pthreadMutexAlloc,
17745    pthreadMutexFree,
17746    pthreadMutexEnter,
17747    pthreadMutexTry,
17748    pthreadMutexLeave,
17749#ifdef SQLITE_DEBUG
17750    pthreadMutexHeld,
17751    pthreadMutexNotheld
17752#else
17753    0,
17754    0
17755#endif
17756  };
17757
17758  return &sMutex;
17759}
17760
17761#endif /* SQLITE_MUTEX_PTHREAD */
17762
17763/************** End of mutex_unix.c ******************************************/
17764/************** Begin file mutex_w32.c ***************************************/
17765/*
17766** 2007 August 14
17767**
17768** The author disclaims copyright to this source code.  In place of
17769** a legal notice, here is a blessing:
17770**
17771**    May you do good and not evil.
17772**    May you find forgiveness for yourself and forgive others.
17773**    May you share freely, never taking more than you give.
17774**
17775*************************************************************************
17776** This file contains the C functions that implement mutexes for win32
17777*/
17778
17779/*
17780** The code in this file is only used if we are compiling multithreaded
17781** on a win32 system.
17782*/
17783#ifdef SQLITE_MUTEX_W32
17784
17785/*
17786** Each recursive mutex is an instance of the following structure.
17787*/
17788struct sqlite3_mutex {
17789  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
17790  int id;                    /* Mutex type */
17791#ifdef SQLITE_DEBUG
17792  volatile int nRef;         /* Number of enterances */
17793  volatile DWORD owner;      /* Thread holding this mutex */
17794  int trace;                 /* True to trace changes */
17795#endif
17796};
17797#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
17798#ifdef SQLITE_DEBUG
17799#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
17800#else
17801#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
17802#endif
17803
17804/*
17805** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
17806** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
17807**
17808** Here is an interesting observation:  Win95, Win98, and WinME lack
17809** the LockFileEx() API.  But we can still statically link against that
17810** API as long as we don't call it win running Win95/98/ME.  A call to
17811** this routine is used to determine if the host is Win95/98/ME or
17812** WinNT/2K/XP so that we will know whether or not we can safely call
17813** the LockFileEx() API.
17814**
17815** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
17816** which is only available if your application was compiled with
17817** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
17818** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
17819** this out as well.
17820*/
17821#if 0
17822#if SQLITE_OS_WINCE
17823# define mutexIsNT()  (1)
17824#else
17825  static int mutexIsNT(void){
17826    static int osType = 0;
17827    if( osType==0 ){
17828      OSVERSIONINFO sInfo;
17829      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
17830      GetVersionEx(&sInfo);
17831      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
17832    }
17833    return osType==2;
17834  }
17835#endif /* SQLITE_OS_WINCE */
17836#endif
17837
17838#ifdef SQLITE_DEBUG
17839/*
17840** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17841** intended for use only inside assert() statements.
17842*/
17843static int winMutexHeld(sqlite3_mutex *p){
17844  return p->nRef!=0 && p->owner==GetCurrentThreadId();
17845}
17846static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
17847  return p->nRef==0 || p->owner!=tid;
17848}
17849static int winMutexNotheld(sqlite3_mutex *p){
17850  DWORD tid = GetCurrentThreadId();
17851  return winMutexNotheld2(p, tid);
17852}
17853#endif
17854
17855
17856/*
17857** Initialize and deinitialize the mutex subsystem.
17858*/
17859static sqlite3_mutex winMutex_staticMutexes[6] = {
17860  SQLITE3_MUTEX_INITIALIZER,
17861  SQLITE3_MUTEX_INITIALIZER,
17862  SQLITE3_MUTEX_INITIALIZER,
17863  SQLITE3_MUTEX_INITIALIZER,
17864  SQLITE3_MUTEX_INITIALIZER,
17865  SQLITE3_MUTEX_INITIALIZER
17866};
17867static int winMutex_isInit = 0;
17868/* As winMutexInit() and winMutexEnd() are called as part
17869** of the sqlite3_initialize and sqlite3_shutdown()
17870** processing, the "interlocked" magic is probably not
17871** strictly necessary.
17872*/
17873static long winMutex_lock = 0;
17874
17875static int winMutexInit(void){
17876  /* The first to increment to 1 does actual initialization */
17877  if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
17878    int i;
17879    for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17880      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
17881    }
17882    winMutex_isInit = 1;
17883  }else{
17884    /* Someone else is in the process of initing the static mutexes */
17885    while( !winMutex_isInit ){
17886      Sleep(1);
17887    }
17888  }
17889  return SQLITE_OK;
17890}
17891
17892static int winMutexEnd(void){
17893  /* The first to decrement to 0 does actual shutdown
17894  ** (which should be the last to shutdown.) */
17895  if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
17896    if( winMutex_isInit==1 ){
17897      int i;
17898      for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
17899        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
17900      }
17901      winMutex_isInit = 0;
17902    }
17903  }
17904  return SQLITE_OK;
17905}
17906
17907/*
17908** The sqlite3_mutex_alloc() routine allocates a new
17909** mutex and returns a pointer to it.  If it returns NULL
17910** that means that a mutex could not be allocated.  SQLite
17911** will unwind its stack and return an error.  The argument
17912** to sqlite3_mutex_alloc() is one of these integer constants:
17913**
17914** <ul>
17915** <li>  SQLITE_MUTEX_FAST
17916** <li>  SQLITE_MUTEX_RECURSIVE
17917** <li>  SQLITE_MUTEX_STATIC_MASTER
17918** <li>  SQLITE_MUTEX_STATIC_MEM
17919** <li>  SQLITE_MUTEX_STATIC_MEM2
17920** <li>  SQLITE_MUTEX_STATIC_PRNG
17921** <li>  SQLITE_MUTEX_STATIC_LRU
17922** <li>  SQLITE_MUTEX_STATIC_PMEM
17923** </ul>
17924**
17925** The first two constants cause sqlite3_mutex_alloc() to create
17926** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17927** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17928** The mutex implementation does not need to make a distinction
17929** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17930** not want to.  But SQLite will only request a recursive mutex in
17931** cases where it really needs one.  If a faster non-recursive mutex
17932** implementation is available on the host platform, the mutex subsystem
17933** might return such a mutex in response to SQLITE_MUTEX_FAST.
17934**
17935** The other allowed parameters to sqlite3_mutex_alloc() each return
17936** a pointer to a static preexisting mutex.  Six static mutexes are
17937** used by the current version of SQLite.  Future versions of SQLite
17938** may add additional static mutexes.  Static mutexes are for internal
17939** use by SQLite only.  Applications that use SQLite mutexes should
17940** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17941** SQLITE_MUTEX_RECURSIVE.
17942**
17943** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17944** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17945** returns a different mutex on every call.  But for the static
17946** mutex types, the same mutex is returned on every call that has
17947** the same type number.
17948*/
17949static sqlite3_mutex *winMutexAlloc(int iType){
17950  sqlite3_mutex *p;
17951
17952  switch( iType ){
17953    case SQLITE_MUTEX_FAST:
17954    case SQLITE_MUTEX_RECURSIVE: {
17955      p = sqlite3MallocZero( sizeof(*p) );
17956      if( p ){
17957#ifdef SQLITE_DEBUG
17958        p->id = iType;
17959#endif
17960        InitializeCriticalSection(&p->mutex);
17961      }
17962      break;
17963    }
17964    default: {
17965      assert( winMutex_isInit==1 );
17966      assert( iType-2 >= 0 );
17967      assert( iType-2 < ArraySize(winMutex_staticMutexes) );
17968      p = &winMutex_staticMutexes[iType-2];
17969#ifdef SQLITE_DEBUG
17970      p->id = iType;
17971#endif
17972      break;
17973    }
17974  }
17975  return p;
17976}
17977
17978
17979/*
17980** This routine deallocates a previously
17981** allocated mutex.  SQLite is careful to deallocate every
17982** mutex that it allocates.
17983*/
17984static void winMutexFree(sqlite3_mutex *p){
17985  assert( p );
17986  assert( p->nRef==0 && p->owner==0 );
17987  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17988  DeleteCriticalSection(&p->mutex);
17989  sqlite3_free(p);
17990}
17991
17992/*
17993** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17994** to enter a mutex.  If another thread is already within the mutex,
17995** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17996** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
17997** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
17998** be entered multiple times by the same thread.  In such cases the,
17999** mutex must be exited an equal number of times before another thread
18000** can enter.  If the same thread tries to enter any other kind of mutex
18001** more than once, the behavior is undefined.
18002*/
18003static void winMutexEnter(sqlite3_mutex *p){
18004#ifdef SQLITE_DEBUG
18005  DWORD tid = GetCurrentThreadId();
18006  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
18007#endif
18008  EnterCriticalSection(&p->mutex);
18009#ifdef SQLITE_DEBUG
18010  assert( p->nRef>0 || p->owner==0 );
18011  p->owner = tid;
18012  p->nRef++;
18013  if( p->trace ){
18014    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18015  }
18016#endif
18017}
18018static int winMutexTry(sqlite3_mutex *p){
18019#ifndef NDEBUG
18020  DWORD tid = GetCurrentThreadId();
18021#endif
18022  int rc = SQLITE_BUSY;
18023  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
18024  /*
18025  ** The sqlite3_mutex_try() routine is very rarely used, and when it
18026  ** is used it is merely an optimization.  So it is OK for it to always
18027  ** fail.
18028  **
18029  ** The TryEnterCriticalSection() interface is only available on WinNT.
18030  ** And some windows compilers complain if you try to use it without
18031  ** first doing some #defines that prevent SQLite from building on Win98.
18032  ** For that reason, we will omit this optimization for now.  See
18033  ** ticket #2685.
18034  */
18035#if 0
18036  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
18037    p->owner = tid;
18038    p->nRef++;
18039    rc = SQLITE_OK;
18040  }
18041#else
18042  UNUSED_PARAMETER(p);
18043#endif
18044#ifdef SQLITE_DEBUG
18045  if( rc==SQLITE_OK && p->trace ){
18046    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18047  }
18048#endif
18049  return rc;
18050}
18051
18052/*
18053** The sqlite3_mutex_leave() routine exits a mutex that was
18054** previously entered by the same thread.  The behavior
18055** is undefined if the mutex is not currently entered or
18056** is not currently allocated.  SQLite will never do either.
18057*/
18058static void winMutexLeave(sqlite3_mutex *p){
18059#ifndef NDEBUG
18060  DWORD tid = GetCurrentThreadId();
18061  assert( p->nRef>0 );
18062  assert( p->owner==tid );
18063  p->nRef--;
18064  if( p->nRef==0 ) p->owner = 0;
18065  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
18066#endif
18067  LeaveCriticalSection(&p->mutex);
18068#ifdef SQLITE_DEBUG
18069  if( p->trace ){
18070    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18071  }
18072#endif
18073}
18074
18075SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
18076  static const sqlite3_mutex_methods sMutex = {
18077    winMutexInit,
18078    winMutexEnd,
18079    winMutexAlloc,
18080    winMutexFree,
18081    winMutexEnter,
18082    winMutexTry,
18083    winMutexLeave,
18084#ifdef SQLITE_DEBUG
18085    winMutexHeld,
18086    winMutexNotheld
18087#else
18088    0,
18089    0
18090#endif
18091  };
18092
18093  return &sMutex;
18094}
18095#endif /* SQLITE_MUTEX_W32 */
18096
18097/************** End of mutex_w32.c *******************************************/
18098/************** Begin file malloc.c ******************************************/
18099/*
18100** 2001 September 15
18101**
18102** The author disclaims copyright to this source code.  In place of
18103** a legal notice, here is a blessing:
18104**
18105**    May you do good and not evil.
18106**    May you find forgiveness for yourself and forgive others.
18107**    May you share freely, never taking more than you give.
18108**
18109*************************************************************************
18110**
18111** Memory allocation functions used throughout sqlite.
18112*/
18113/* #include <stdarg.h> */
18114
18115/*
18116** Attempt to release up to n bytes of non-essential memory currently
18117** held by SQLite. An example of non-essential memory is memory used to
18118** cache database pages that are not currently in use.
18119*/
18120SQLITE_API int sqlite3_release_memory(int n){
18121#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
18122  return sqlite3PcacheReleaseMemory(n);
18123#else
18124  /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
18125  ** is a no-op returning zero if SQLite is not compiled with
18126  ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
18127  UNUSED_PARAMETER(n);
18128  return 0;
18129#endif
18130}
18131
18132/*
18133** An instance of the following object records the location of
18134** each unused scratch buffer.
18135*/
18136typedef struct ScratchFreeslot {
18137  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
18138} ScratchFreeslot;
18139
18140/*
18141** State information local to the memory allocation subsystem.
18142*/
18143static SQLITE_WSD struct Mem0Global {
18144  sqlite3_mutex *mutex;         /* Mutex to serialize access */
18145
18146  /*
18147  ** The alarm callback and its arguments.  The mem0.mutex lock will
18148  ** be held while the callback is running.  Recursive calls into
18149  ** the memory subsystem are allowed, but no new callbacks will be
18150  ** issued.
18151  */
18152  sqlite3_int64 alarmThreshold;
18153  void (*alarmCallback)(void*, sqlite3_int64,int);
18154  void *alarmArg;
18155
18156  /*
18157  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
18158  ** (so that a range test can be used to determine if an allocation
18159  ** being freed came from pScratch) and a pointer to the list of
18160  ** unused scratch allocations.
18161  */
18162  void *pScratchEnd;
18163  ScratchFreeslot *pScratchFree;
18164  u32 nScratchFree;
18165
18166  /*
18167  ** True if heap is nearly "full" where "full" is defined by the
18168  ** sqlite3_soft_heap_limit() setting.
18169  */
18170  int nearlyFull;
18171} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
18172
18173#define mem0 GLOBAL(struct Mem0Global, mem0)
18174
18175/*
18176** This routine runs when the memory allocator sees that the
18177** total memory allocation is about to exceed the soft heap
18178** limit.
18179*/
18180static void softHeapLimitEnforcer(
18181  void *NotUsed,
18182  sqlite3_int64 NotUsed2,
18183  int allocSize
18184){
18185  UNUSED_PARAMETER2(NotUsed, NotUsed2);
18186  sqlite3_release_memory(allocSize);
18187}
18188
18189/*
18190** Change the alarm callback
18191*/
18192static int sqlite3MemoryAlarm(
18193  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
18194  void *pArg,
18195  sqlite3_int64 iThreshold
18196){
18197  int nUsed;
18198  sqlite3_mutex_enter(mem0.mutex);
18199  mem0.alarmCallback = xCallback;
18200  mem0.alarmArg = pArg;
18201  mem0.alarmThreshold = iThreshold;
18202  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18203  mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
18204  sqlite3_mutex_leave(mem0.mutex);
18205  return SQLITE_OK;
18206}
18207
18208#ifndef SQLITE_OMIT_DEPRECATED
18209/*
18210** Deprecated external interface.  Internal/core SQLite code
18211** should call sqlite3MemoryAlarm.
18212*/
18213SQLITE_API int sqlite3_memory_alarm(
18214  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
18215  void *pArg,
18216  sqlite3_int64 iThreshold
18217){
18218  return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
18219}
18220#endif
18221
18222/*
18223** Set the soft heap-size limit for the library. Passing a zero or
18224** negative value indicates no limit.
18225*/
18226SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
18227  sqlite3_int64 priorLimit;
18228  sqlite3_int64 excess;
18229#ifndef SQLITE_OMIT_AUTOINIT
18230  sqlite3_initialize();
18231#endif
18232  sqlite3_mutex_enter(mem0.mutex);
18233  priorLimit = mem0.alarmThreshold;
18234  sqlite3_mutex_leave(mem0.mutex);
18235  if( n<0 ) return priorLimit;
18236  if( n>0 ){
18237    sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
18238  }else{
18239    sqlite3MemoryAlarm(0, 0, 0);
18240  }
18241  excess = sqlite3_memory_used() - n;
18242  if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
18243  return priorLimit;
18244}
18245SQLITE_API void sqlite3_soft_heap_limit(int n){
18246  if( n<0 ) n = 0;
18247  sqlite3_soft_heap_limit64(n);
18248}
18249
18250/*
18251** Initialize the memory allocation subsystem.
18252*/
18253SQLITE_PRIVATE int sqlite3MallocInit(void){
18254  if( sqlite3GlobalConfig.m.xMalloc==0 ){
18255    sqlite3MemSetDefault();
18256  }
18257  memset(&mem0, 0, sizeof(mem0));
18258  if( sqlite3GlobalConfig.bCoreMutex ){
18259    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
18260  }
18261  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
18262      && sqlite3GlobalConfig.nScratch>0 ){
18263    int i, n, sz;
18264    ScratchFreeslot *pSlot;
18265    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
18266    sqlite3GlobalConfig.szScratch = sz;
18267    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
18268    n = sqlite3GlobalConfig.nScratch;
18269    mem0.pScratchFree = pSlot;
18270    mem0.nScratchFree = n;
18271    for(i=0; i<n-1; i++){
18272      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
18273      pSlot = pSlot->pNext;
18274    }
18275    pSlot->pNext = 0;
18276    mem0.pScratchEnd = (void*)&pSlot[1];
18277  }else{
18278    mem0.pScratchEnd = 0;
18279    sqlite3GlobalConfig.pScratch = 0;
18280    sqlite3GlobalConfig.szScratch = 0;
18281    sqlite3GlobalConfig.nScratch = 0;
18282  }
18283  if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
18284      || sqlite3GlobalConfig.nPage<1 ){
18285    sqlite3GlobalConfig.pPage = 0;
18286    sqlite3GlobalConfig.szPage = 0;
18287    sqlite3GlobalConfig.nPage = 0;
18288  }
18289  return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
18290}
18291
18292/*
18293** Return true if the heap is currently under memory pressure - in other
18294** words if the amount of heap used is close to the limit set by
18295** sqlite3_soft_heap_limit().
18296*/
18297SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
18298  return mem0.nearlyFull;
18299}
18300
18301/*
18302** Deinitialize the memory allocation subsystem.
18303*/
18304SQLITE_PRIVATE void sqlite3MallocEnd(void){
18305  if( sqlite3GlobalConfig.m.xShutdown ){
18306    sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
18307  }
18308  memset(&mem0, 0, sizeof(mem0));
18309}
18310
18311/*
18312** Return the amount of memory currently checked out.
18313*/
18314SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
18315  int n, mx;
18316  sqlite3_int64 res;
18317  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
18318  res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
18319  return res;
18320}
18321
18322/*
18323** Return the maximum amount of memory that has ever been
18324** checked out since either the beginning of this process
18325** or since the most recent reset.
18326*/
18327SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
18328  int n, mx;
18329  sqlite3_int64 res;
18330  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
18331  res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
18332  return res;
18333}
18334
18335/*
18336** Trigger the alarm
18337*/
18338static void sqlite3MallocAlarm(int nByte){
18339  void (*xCallback)(void*,sqlite3_int64,int);
18340  sqlite3_int64 nowUsed;
18341  void *pArg;
18342  if( mem0.alarmCallback==0 ) return;
18343  xCallback = mem0.alarmCallback;
18344  nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18345  pArg = mem0.alarmArg;
18346  mem0.alarmCallback = 0;
18347  sqlite3_mutex_leave(mem0.mutex);
18348  xCallback(pArg, nowUsed, nByte);
18349  sqlite3_mutex_enter(mem0.mutex);
18350  mem0.alarmCallback = xCallback;
18351  mem0.alarmArg = pArg;
18352}
18353
18354/*
18355** Do a memory allocation with statistics and alarms.  Assume the
18356** lock is already held.
18357*/
18358static int mallocWithAlarm(int n, void **pp){
18359  int nFull;
18360  void *p;
18361  assert( sqlite3_mutex_held(mem0.mutex) );
18362  nFull = sqlite3GlobalConfig.m.xRoundup(n);
18363  sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
18364  if( mem0.alarmCallback!=0 ){
18365    int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
18366    if( nUsed >= mem0.alarmThreshold - nFull ){
18367      mem0.nearlyFull = 1;
18368      sqlite3MallocAlarm(nFull);
18369    }else{
18370      mem0.nearlyFull = 0;
18371    }
18372  }
18373  p = sqlite3GlobalConfig.m.xMalloc(nFull);
18374#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
18375  if( p==0 && mem0.alarmCallback ){
18376    sqlite3MallocAlarm(nFull);
18377    p = sqlite3GlobalConfig.m.xMalloc(nFull);
18378  }
18379#endif
18380  if( p ){
18381    nFull = sqlite3MallocSize(p);
18382    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
18383    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
18384  }
18385  *pp = p;
18386  return nFull;
18387}
18388
18389/*
18390** Allocate memory.  This routine is like sqlite3_malloc() except that it
18391** assumes the memory subsystem has already been initialized.
18392*/
18393SQLITE_PRIVATE void *sqlite3Malloc(int n){
18394  void *p;
18395  if( n<=0               /* IMP: R-65312-04917 */
18396   || n>=0x7fffff00
18397  ){
18398    /* A memory allocation of a number of bytes which is near the maximum
18399    ** signed integer value might cause an integer overflow inside of the
18400    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
18401    ** 255 bytes of overhead.  SQLite itself will never use anything near
18402    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
18403    p = 0;
18404  }else if( sqlite3GlobalConfig.bMemstat ){
18405    sqlite3_mutex_enter(mem0.mutex);
18406    mallocWithAlarm(n, &p);
18407    sqlite3_mutex_leave(mem0.mutex);
18408  }else{
18409    p = sqlite3GlobalConfig.m.xMalloc(n);
18410  }
18411  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
18412  return p;
18413}
18414
18415/*
18416** This version of the memory allocation is for use by the application.
18417** First make sure the memory subsystem is initialized, then do the
18418** allocation.
18419*/
18420SQLITE_API void *sqlite3_malloc(int n){
18421#ifndef SQLITE_OMIT_AUTOINIT
18422  if( sqlite3_initialize() ) return 0;
18423#endif
18424  return sqlite3Malloc(n);
18425}
18426
18427/*
18428** Each thread may only have a single outstanding allocation from
18429** xScratchMalloc().  We verify this constraint in the single-threaded
18430** case by setting scratchAllocOut to 1 when an allocation
18431** is outstanding clearing it when the allocation is freed.
18432*/
18433#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18434static int scratchAllocOut = 0;
18435#endif
18436
18437
18438/*
18439** Allocate memory that is to be used and released right away.
18440** This routine is similar to alloca() in that it is not intended
18441** for situations where the memory might be held long-term.  This
18442** routine is intended to get memory to old large transient data
18443** structures that would not normally fit on the stack of an
18444** embedded processor.
18445*/
18446SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
18447  void *p;
18448  assert( n>0 );
18449
18450  sqlite3_mutex_enter(mem0.mutex);
18451  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
18452    p = mem0.pScratchFree;
18453    mem0.pScratchFree = mem0.pScratchFree->pNext;
18454    mem0.nScratchFree--;
18455    sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
18456    sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18457    sqlite3_mutex_leave(mem0.mutex);
18458  }else{
18459    if( sqlite3GlobalConfig.bMemstat ){
18460      sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
18461      n = mallocWithAlarm(n, &p);
18462      if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
18463      sqlite3_mutex_leave(mem0.mutex);
18464    }else{
18465      sqlite3_mutex_leave(mem0.mutex);
18466      p = sqlite3GlobalConfig.m.xMalloc(n);
18467    }
18468    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
18469  }
18470  assert( sqlite3_mutex_notheld(mem0.mutex) );
18471
18472
18473#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18474  /* Verify that no more than two scratch allocations per thread
18475  ** are outstanding at one time.  (This is only checked in the
18476  ** single-threaded case since checking in the multi-threaded case
18477  ** would be much more complicated.) */
18478  assert( scratchAllocOut<=1 );
18479  if( p ) scratchAllocOut++;
18480#endif
18481
18482  return p;
18483}
18484SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
18485  if( p ){
18486
18487#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
18488    /* Verify that no more than two scratch allocation per thread
18489    ** is outstanding at one time.  (This is only checked in the
18490    ** single-threaded case since checking in the multi-threaded case
18491    ** would be much more complicated.) */
18492    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
18493    scratchAllocOut--;
18494#endif
18495
18496    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
18497      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
18498      ScratchFreeslot *pSlot;
18499      pSlot = (ScratchFreeslot*)p;
18500      sqlite3_mutex_enter(mem0.mutex);
18501      pSlot->pNext = mem0.pScratchFree;
18502      mem0.pScratchFree = pSlot;
18503      mem0.nScratchFree++;
18504      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
18505      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
18506      sqlite3_mutex_leave(mem0.mutex);
18507    }else{
18508      /* Release memory back to the heap */
18509      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
18510      assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
18511      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18512      if( sqlite3GlobalConfig.bMemstat ){
18513        int iSize = sqlite3MallocSize(p);
18514        sqlite3_mutex_enter(mem0.mutex);
18515        sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
18516        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
18517        sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18518        sqlite3GlobalConfig.m.xFree(p);
18519        sqlite3_mutex_leave(mem0.mutex);
18520      }else{
18521        sqlite3GlobalConfig.m.xFree(p);
18522      }
18523    }
18524  }
18525}
18526
18527/*
18528** TRUE if p is a lookaside memory allocation from db
18529*/
18530#ifndef SQLITE_OMIT_LOOKASIDE
18531static int isLookaside(sqlite3 *db, void *p){
18532  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
18533}
18534#else
18535#define isLookaside(A,B) 0
18536#endif
18537
18538/*
18539** Return the size of a memory allocation previously obtained from
18540** sqlite3Malloc() or sqlite3_malloc().
18541*/
18542SQLITE_PRIVATE int sqlite3MallocSize(void *p){
18543  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18544  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18545  return sqlite3GlobalConfig.m.xSize(p);
18546}
18547SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
18548  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18549  if( db && isLookaside(db, p) ){
18550    return db->lookaside.sz;
18551  }else{
18552    assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18553    assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18554    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18555    return sqlite3GlobalConfig.m.xSize(p);
18556  }
18557}
18558
18559/*
18560** Free memory previously obtained from sqlite3Malloc().
18561*/
18562SQLITE_API void sqlite3_free(void *p){
18563  if( p==0 ) return;  /* IMP: R-49053-54554 */
18564  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
18565  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
18566  if( sqlite3GlobalConfig.bMemstat ){
18567    sqlite3_mutex_enter(mem0.mutex);
18568    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
18569    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
18570    sqlite3GlobalConfig.m.xFree(p);
18571    sqlite3_mutex_leave(mem0.mutex);
18572  }else{
18573    sqlite3GlobalConfig.m.xFree(p);
18574  }
18575}
18576
18577/*
18578** Free memory that might be associated with a particular database
18579** connection.
18580*/
18581SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
18582  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18583  if( db ){
18584    if( db->pnBytesFreed ){
18585      *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
18586      return;
18587    }
18588    if( isLookaside(db, p) ){
18589      LookasideSlot *pBuf = (LookasideSlot*)p;
18590      pBuf->pNext = db->lookaside.pFree;
18591      db->lookaside.pFree = pBuf;
18592      db->lookaside.nOut--;
18593      return;
18594    }
18595  }
18596  assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18597  assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18598  assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
18599  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18600  sqlite3_free(p);
18601}
18602
18603/*
18604** Change the size of an existing memory allocation
18605*/
18606SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
18607  int nOld, nNew, nDiff;
18608  void *pNew;
18609  if( pOld==0 ){
18610    return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
18611  }
18612  if( nBytes<=0 ){
18613    sqlite3_free(pOld); /* IMP: R-31593-10574 */
18614    return 0;
18615  }
18616  if( nBytes>=0x7fffff00 ){
18617    /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
18618    return 0;
18619  }
18620  nOld = sqlite3MallocSize(pOld);
18621  /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
18622  ** argument to xRealloc is always a value returned by a prior call to
18623  ** xRoundup. */
18624  nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
18625  if( nOld==nNew ){
18626    pNew = pOld;
18627  }else if( sqlite3GlobalConfig.bMemstat ){
18628    sqlite3_mutex_enter(mem0.mutex);
18629    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
18630    nDiff = nNew - nOld;
18631    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
18632          mem0.alarmThreshold-nDiff ){
18633      sqlite3MallocAlarm(nDiff);
18634    }
18635    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
18636    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
18637    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18638    if( pNew==0 && mem0.alarmCallback ){
18639      sqlite3MallocAlarm(nBytes);
18640      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18641    }
18642    if( pNew ){
18643      nNew = sqlite3MallocSize(pNew);
18644      sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
18645    }
18646    sqlite3_mutex_leave(mem0.mutex);
18647  }else{
18648    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
18649  }
18650  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
18651  return pNew;
18652}
18653
18654/*
18655** The public interface to sqlite3Realloc.  Make sure that the memory
18656** subsystem is initialized prior to invoking sqliteRealloc.
18657*/
18658SQLITE_API void *sqlite3_realloc(void *pOld, int n){
18659#ifndef SQLITE_OMIT_AUTOINIT
18660  if( sqlite3_initialize() ) return 0;
18661#endif
18662  return sqlite3Realloc(pOld, n);
18663}
18664
18665
18666/*
18667** Allocate and zero memory.
18668*/
18669SQLITE_PRIVATE void *sqlite3MallocZero(int n){
18670  void *p = sqlite3Malloc(n);
18671  if( p ){
18672    memset(p, 0, n);
18673  }
18674  return p;
18675}
18676
18677/*
18678** Allocate and zero memory.  If the allocation fails, make
18679** the mallocFailed flag in the connection pointer.
18680*/
18681SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
18682  void *p = sqlite3DbMallocRaw(db, n);
18683  if( p ){
18684    memset(p, 0, n);
18685  }
18686  return p;
18687}
18688
18689/*
18690** Allocate and zero memory.  If the allocation fails, make
18691** the mallocFailed flag in the connection pointer.
18692**
18693** If db!=0 and db->mallocFailed is true (indicating a prior malloc
18694** failure on the same database connection) then always return 0.
18695** Hence for a particular database connection, once malloc starts
18696** failing, it fails consistently until mallocFailed is reset.
18697** This is an important assumption.  There are many places in the
18698** code that do things like this:
18699**
18700**         int *a = (int*)sqlite3DbMallocRaw(db, 100);
18701**         int *b = (int*)sqlite3DbMallocRaw(db, 200);
18702**         if( b ) a[10] = 9;
18703**
18704** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
18705** that all prior mallocs (ex: "a") worked too.
18706*/
18707SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
18708  void *p;
18709  assert( db==0 || sqlite3_mutex_held(db->mutex) );
18710  assert( db==0 || db->pnBytesFreed==0 );
18711#ifndef SQLITE_OMIT_LOOKASIDE
18712  if( db ){
18713    LookasideSlot *pBuf;
18714    if( db->mallocFailed ){
18715      return 0;
18716    }
18717    if( db->lookaside.bEnabled ){
18718      if( n>db->lookaside.sz ){
18719        db->lookaside.anStat[1]++;
18720      }else if( (pBuf = db->lookaside.pFree)==0 ){
18721        db->lookaside.anStat[2]++;
18722      }else{
18723        db->lookaside.pFree = pBuf->pNext;
18724        db->lookaside.nOut++;
18725        db->lookaside.anStat[0]++;
18726        if( db->lookaside.nOut>db->lookaside.mxOut ){
18727          db->lookaside.mxOut = db->lookaside.nOut;
18728        }
18729        return (void*)pBuf;
18730      }
18731    }
18732  }
18733#else
18734  if( db && db->mallocFailed ){
18735    return 0;
18736  }
18737#endif
18738  p = sqlite3Malloc(n);
18739  if( !p && db ){
18740    db->mallocFailed = 1;
18741  }
18742  sqlite3MemdebugSetType(p, MEMTYPE_DB |
18743         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18744  return p;
18745}
18746
18747/*
18748** Resize the block of memory pointed to by p to n bytes. If the
18749** resize fails, set the mallocFailed flag in the connection object.
18750*/
18751SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
18752  void *pNew = 0;
18753  assert( db!=0 );
18754  assert( sqlite3_mutex_held(db->mutex) );
18755  if( db->mallocFailed==0 ){
18756    if( p==0 ){
18757      return sqlite3DbMallocRaw(db, n);
18758    }
18759    if( isLookaside(db, p) ){
18760      if( n<=db->lookaside.sz ){
18761        return p;
18762      }
18763      pNew = sqlite3DbMallocRaw(db, n);
18764      if( pNew ){
18765        memcpy(pNew, p, db->lookaside.sz);
18766        sqlite3DbFree(db, p);
18767      }
18768    }else{
18769      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
18770      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
18771      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
18772      pNew = sqlite3_realloc(p, n);
18773      if( !pNew ){
18774        sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
18775        db->mallocFailed = 1;
18776      }
18777      sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
18778            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
18779    }
18780  }
18781  return pNew;
18782}
18783
18784/*
18785** Attempt to reallocate p.  If the reallocation fails, then free p
18786** and set the mallocFailed flag in the database connection.
18787*/
18788SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
18789  void *pNew;
18790  pNew = sqlite3DbRealloc(db, p, n);
18791  if( !pNew ){
18792    sqlite3DbFree(db, p);
18793  }
18794  return pNew;
18795}
18796
18797/*
18798** Make a copy of a string in memory obtained from sqliteMalloc(). These
18799** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
18800** is because when memory debugging is turned on, these two functions are
18801** called via macros that record the current file and line number in the
18802** ThreadData structure.
18803*/
18804SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
18805  char *zNew;
18806  size_t n;
18807  if( z==0 ){
18808    return 0;
18809  }
18810  n = sqlite3Strlen30(z) + 1;
18811  assert( (n&0x7fffffff)==n );
18812  zNew = sqlite3DbMallocRaw(db, (int)n);
18813  if( zNew ){
18814    memcpy(zNew, z, n);
18815  }
18816  return zNew;
18817}
18818SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
18819  char *zNew;
18820  if( z==0 ){
18821    return 0;
18822  }
18823  assert( (n&0x7fffffff)==n );
18824  zNew = sqlite3DbMallocRaw(db, n+1);
18825  if( zNew ){
18826    memcpy(zNew, z, n);
18827    zNew[n] = 0;
18828  }
18829  return zNew;
18830}
18831
18832/*
18833** Create a string from the zFromat argument and the va_list that follows.
18834** Store the string in memory obtained from sqliteMalloc() and make *pz
18835** point to that string.
18836*/
18837SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
18838  va_list ap;
18839  char *z;
18840
18841  va_start(ap, zFormat);
18842  z = sqlite3VMPrintf(db, zFormat, ap);
18843  va_end(ap);
18844  sqlite3DbFree(db, *pz);
18845  *pz = z;
18846}
18847
18848
18849/*
18850** This function must be called before exiting any API function (i.e.
18851** returning control to the user) that has called sqlite3_malloc or
18852** sqlite3_realloc.
18853**
18854** The returned value is normally a copy of the second argument to this
18855** function. However, if a malloc() failure has occurred since the previous
18856** invocation SQLITE_NOMEM is returned instead.
18857**
18858** If the first argument, db, is not NULL and a malloc() error has occurred,
18859** then the connection error-code (the value returned by sqlite3_errcode())
18860** is set to SQLITE_NOMEM.
18861*/
18862SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
18863  /* If the db handle is not NULL, then we must hold the connection handle
18864  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
18865  ** is unsafe, as is the call to sqlite3Error().
18866  */
18867  assert( !db || sqlite3_mutex_held(db->mutex) );
18868  if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
18869    sqlite3Error(db, SQLITE_NOMEM, 0);
18870    db->mallocFailed = 0;
18871    rc = SQLITE_NOMEM;
18872  }
18873  return rc & (db ? db->errMask : 0xff);
18874}
18875
18876/************** End of malloc.c **********************************************/
18877/************** Begin file printf.c ******************************************/
18878/*
18879** The "printf" code that follows dates from the 1980's.  It is in
18880** the public domain.  The original comments are included here for
18881** completeness.  They are very out-of-date but might be useful as
18882** an historical reference.  Most of the "enhancements" have been backed
18883** out so that the functionality is now the same as standard printf().
18884**
18885**************************************************************************
18886**
18887** The following modules is an enhanced replacement for the "printf" subroutines
18888** found in the standard C library.  The following enhancements are
18889** supported:
18890**
18891**      +  Additional functions.  The standard set of "printf" functions
18892**         includes printf, fprintf, sprintf, vprintf, vfprintf, and
18893**         vsprintf.  This module adds the following:
18894**
18895**           *  snprintf -- Works like sprintf, but has an extra argument
18896**                          which is the size of the buffer written to.
18897**
18898**           *  mprintf --  Similar to sprintf.  Writes output to memory
18899**                          obtained from malloc.
18900**
18901**           *  xprintf --  Calls a function to dispose of output.
18902**
18903**           *  nprintf --  No output, but returns the number of characters
18904**                          that would have been output by printf.
18905**
18906**           *  A v- version (ex: vsnprintf) of every function is also
18907**              supplied.
18908**
18909**      +  A few extensions to the formatting notation are supported:
18910**
18911**           *  The "=" flag (similar to "-") causes the output to be
18912**              be centered in the appropriately sized field.
18913**
18914**           *  The %b field outputs an integer in binary notation.
18915**
18916**           *  The %c field now accepts a precision.  The character output
18917**              is repeated by the number of times the precision specifies.
18918**
18919**           *  The %' field works like %c, but takes as its character the
18920**              next character of the format string, instead of the next
18921**              argument.  For example,  printf("%.78'-")  prints 78 minus
18922**              signs, the same as  printf("%.78c",'-').
18923**
18924**      +  When compiled using GCC on a SPARC, this version of printf is
18925**         faster than the library printf for SUN OS 4.1.
18926**
18927**      +  All functions are fully reentrant.
18928**
18929*/
18930
18931/*
18932** Conversion types fall into various categories as defined by the
18933** following enumeration.
18934*/
18935#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
18936#define etFLOAT       2 /* Floating point.  %f */
18937#define etEXP         3 /* Exponentional notation. %e and %E */
18938#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
18939#define etSIZE        5 /* Return number of characters processed so far. %n */
18940#define etSTRING      6 /* Strings. %s */
18941#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
18942#define etPERCENT     8 /* Percent symbol. %% */
18943#define etCHARX       9 /* Characters. %c */
18944/* The rest are extensions, not normally found in printf() */
18945#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
18946#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
18947                          NULL pointers replaced by SQL NULL.  %Q */
18948#define etTOKEN      12 /* a pointer to a Token structure */
18949#define etSRCLIST    13 /* a pointer to a SrcList */
18950#define etPOINTER    14 /* The %p conversion */
18951#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
18952#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
18953
18954#define etINVALID     0 /* Any unrecognized conversion type */
18955
18956
18957/*
18958** An "etByte" is an 8-bit unsigned value.
18959*/
18960typedef unsigned char etByte;
18961
18962/*
18963** Each builtin conversion character (ex: the 'd' in "%d") is described
18964** by an instance of the following structure
18965*/
18966typedef struct et_info {   /* Information about each format field */
18967  char fmttype;            /* The format field code letter */
18968  etByte base;             /* The base for radix conversion */
18969  etByte flags;            /* One or more of FLAG_ constants below */
18970  etByte type;             /* Conversion paradigm */
18971  etByte charset;          /* Offset into aDigits[] of the digits string */
18972  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
18973} et_info;
18974
18975/*
18976** Allowed values for et_info.flags
18977*/
18978#define FLAG_SIGNED  1     /* True if the value to convert is signed */
18979#define FLAG_INTERN  2     /* True if for internal use only */
18980#define FLAG_STRING  4     /* Allow infinity precision */
18981
18982
18983/*
18984** The following table is searched linearly, so it is good to put the
18985** most frequently used conversion types first.
18986*/
18987static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
18988static const char aPrefix[] = "-x0\000X0";
18989static const et_info fmtinfo[] = {
18990  {  'd', 10, 1, etRADIX,      0,  0 },
18991  {  's',  0, 4, etSTRING,     0,  0 },
18992  {  'g',  0, 1, etGENERIC,    30, 0 },
18993  {  'z',  0, 4, etDYNSTRING,  0,  0 },
18994  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
18995  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
18996  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
18997  {  'c',  0, 0, etCHARX,      0,  0 },
18998  {  'o',  8, 0, etRADIX,      0,  2 },
18999  {  'u', 10, 0, etRADIX,      0,  0 },
19000  {  'x', 16, 0, etRADIX,      16, 1 },
19001  {  'X', 16, 0, etRADIX,      0,  4 },
19002#ifndef SQLITE_OMIT_FLOATING_POINT
19003  {  'f',  0, 1, etFLOAT,      0,  0 },
19004  {  'e',  0, 1, etEXP,        30, 0 },
19005  {  'E',  0, 1, etEXP,        14, 0 },
19006  {  'G',  0, 1, etGENERIC,    14, 0 },
19007#endif
19008  {  'i', 10, 1, etRADIX,      0,  0 },
19009  {  'n',  0, 0, etSIZE,       0,  0 },
19010  {  '%',  0, 0, etPERCENT,    0,  0 },
19011  {  'p', 16, 0, etPOINTER,    0,  1 },
19012
19013/* All the rest have the FLAG_INTERN bit set and are thus for internal
19014** use only */
19015  {  'T',  0, 2, etTOKEN,      0,  0 },
19016  {  'S',  0, 2, etSRCLIST,    0,  0 },
19017  {  'r', 10, 3, etORDINAL,    0,  0 },
19018};
19019
19020/*
19021** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
19022** conversions will work.
19023*/
19024#ifndef SQLITE_OMIT_FLOATING_POINT
19025/*
19026** "*val" is a double such that 0.1 <= *val < 10.0
19027** Return the ascii code for the leading digit of *val, then
19028** multiply "*val" by 10.0 to renormalize.
19029**
19030** Example:
19031**     input:     *val = 3.14159
19032**     output:    *val = 1.4159    function return = '3'
19033**
19034** The counter *cnt is incremented each time.  After counter exceeds
19035** 16 (the number of significant digits in a 64-bit float) '0' is
19036** always returned.
19037*/
19038static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
19039  int digit;
19040  LONGDOUBLE_TYPE d;
19041  if( (*cnt)++ >= 16 ) return '0';
19042  digit = (int)*val;
19043  d = digit;
19044  digit += '0';
19045  *val = (*val - d)*10.0;
19046  return (char)digit;
19047}
19048#endif /* SQLITE_OMIT_FLOATING_POINT */
19049
19050/*
19051** Append N space characters to the given string buffer.
19052*/
19053static void appendSpace(StrAccum *pAccum, int N){
19054  static const char zSpaces[] = "                             ";
19055  while( N>=(int)sizeof(zSpaces)-1 ){
19056    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
19057    N -= sizeof(zSpaces)-1;
19058  }
19059  if( N>0 ){
19060    sqlite3StrAccumAppend(pAccum, zSpaces, N);
19061  }
19062}
19063
19064/*
19065** On machines with a small stack size, you can redefine the
19066** SQLITE_PRINT_BUF_SIZE to be less than 350.
19067*/
19068#ifndef SQLITE_PRINT_BUF_SIZE
19069# if defined(SQLITE_SMALL_STACK)
19070#   define SQLITE_PRINT_BUF_SIZE 50
19071# else
19072#   define SQLITE_PRINT_BUF_SIZE 350
19073# endif
19074#endif
19075#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
19076
19077/*
19078** The root program.  All variations call this core.
19079**
19080** INPUTS:
19081**   func   This is a pointer to a function taking three arguments
19082**            1. A pointer to anything.  Same as the "arg" parameter.
19083**            2. A pointer to the list of characters to be output
19084**               (Note, this list is NOT null terminated.)
19085**            3. An integer number of characters to be output.
19086**               (Note: This number might be zero.)
19087**
19088**   arg    This is the pointer to anything which will be passed as the
19089**          first argument to "func".  Use it for whatever you like.
19090**
19091**   fmt    This is the format string, as in the usual print.
19092**
19093**   ap     This is a pointer to a list of arguments.  Same as in
19094**          vfprint.
19095**
19096** OUTPUTS:
19097**          The return value is the total number of characters sent to
19098**          the function "func".  Returns -1 on a error.
19099**
19100** Note that the order in which automatic variables are declared below
19101** seems to make a big difference in determining how fast this beast
19102** will run.
19103*/
19104SQLITE_PRIVATE void sqlite3VXPrintf(
19105  StrAccum *pAccum,                  /* Accumulate results here */
19106  int useExtended,                   /* Allow extended %-conversions */
19107  const char *fmt,                   /* Format string */
19108  va_list ap                         /* arguments */
19109){
19110  int c;                     /* Next character in the format string */
19111  char *bufpt;               /* Pointer to the conversion buffer */
19112  int precision;             /* Precision of the current field */
19113  int length;                /* Length of the field */
19114  int idx;                   /* A general purpose loop counter */
19115  int width;                 /* Width of the current field */
19116  etByte flag_leftjustify;   /* True if "-" flag is present */
19117  etByte flag_plussign;      /* True if "+" flag is present */
19118  etByte flag_blanksign;     /* True if " " flag is present */
19119  etByte flag_alternateform; /* True if "#" flag is present */
19120  etByte flag_altform2;      /* True if "!" flag is present */
19121  etByte flag_zeropad;       /* True if field width constant starts with zero */
19122  etByte flag_long;          /* True if "l" flag is present */
19123  etByte flag_longlong;      /* True if the "ll" flag is present */
19124  etByte done;               /* Loop termination flag */
19125  sqlite_uint64 longvalue;   /* Value for integer types */
19126  LONGDOUBLE_TYPE realvalue; /* Value for real types */
19127  const et_info *infop;      /* Pointer to the appropriate info structure */
19128  char buf[etBUFSIZE];       /* Conversion buffer */
19129  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
19130  etByte xtype = 0;          /* Conversion paradigm */
19131  char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
19132#ifndef SQLITE_OMIT_FLOATING_POINT
19133  int  exp, e2;              /* exponent of real numbers */
19134  double rounder;            /* Used for rounding floating point values */
19135  etByte flag_dp;            /* True if decimal point should be shown */
19136  etByte flag_rtz;           /* True if trailing zeros should be removed */
19137  etByte flag_exp;           /* True to force display of the exponent */
19138  int nsd;                   /* Number of significant digits returned */
19139#endif
19140
19141  length = 0;
19142  bufpt = 0;
19143  for(; (c=(*fmt))!=0; ++fmt){
19144    if( c!='%' ){
19145      int amt;
19146      bufpt = (char *)fmt;
19147      amt = 1;
19148      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
19149      sqlite3StrAccumAppend(pAccum, bufpt, amt);
19150      if( c==0 ) break;
19151    }
19152    if( (c=(*++fmt))==0 ){
19153      sqlite3StrAccumAppend(pAccum, "%", 1);
19154      break;
19155    }
19156    /* Find out what flags are present */
19157    flag_leftjustify = flag_plussign = flag_blanksign =
19158     flag_alternateform = flag_altform2 = flag_zeropad = 0;
19159    done = 0;
19160    do{
19161      switch( c ){
19162        case '-':   flag_leftjustify = 1;     break;
19163        case '+':   flag_plussign = 1;        break;
19164        case ' ':   flag_blanksign = 1;       break;
19165        case '#':   flag_alternateform = 1;   break;
19166        case '!':   flag_altform2 = 1;        break;
19167        case '0':   flag_zeropad = 1;         break;
19168        default:    done = 1;                 break;
19169      }
19170    }while( !done && (c=(*++fmt))!=0 );
19171    /* Get the field width */
19172    width = 0;
19173    if( c=='*' ){
19174      width = va_arg(ap,int);
19175      if( width<0 ){
19176        flag_leftjustify = 1;
19177        width = -width;
19178      }
19179      c = *++fmt;
19180    }else{
19181      while( c>='0' && c<='9' ){
19182        width = width*10 + c - '0';
19183        c = *++fmt;
19184      }
19185    }
19186    if( width > etBUFSIZE-10 ){
19187      width = etBUFSIZE-10;
19188    }
19189    /* Get the precision */
19190    if( c=='.' ){
19191      precision = 0;
19192      c = *++fmt;
19193      if( c=='*' ){
19194        precision = va_arg(ap,int);
19195        if( precision<0 ) precision = -precision;
19196        c = *++fmt;
19197      }else{
19198        while( c>='0' && c<='9' ){
19199          precision = precision*10 + c - '0';
19200          c = *++fmt;
19201        }
19202      }
19203    }else{
19204      precision = -1;
19205    }
19206    /* Get the conversion type modifier */
19207    if( c=='l' ){
19208      flag_long = 1;
19209      c = *++fmt;
19210      if( c=='l' ){
19211        flag_longlong = 1;
19212        c = *++fmt;
19213      }else{
19214        flag_longlong = 0;
19215      }
19216    }else{
19217      flag_long = flag_longlong = 0;
19218    }
19219    /* Fetch the info entry for the field */
19220    infop = &fmtinfo[0];
19221    xtype = etINVALID;
19222    for(idx=0; idx<ArraySize(fmtinfo); idx++){
19223      if( c==fmtinfo[idx].fmttype ){
19224        infop = &fmtinfo[idx];
19225        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
19226          xtype = infop->type;
19227        }else{
19228          return;
19229        }
19230        break;
19231      }
19232    }
19233    zExtra = 0;
19234
19235
19236    /* Limit the precision to prevent overflowing buf[] during conversion */
19237    if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
19238      precision = etBUFSIZE-40;
19239    }
19240
19241    /*
19242    ** At this point, variables are initialized as follows:
19243    **
19244    **   flag_alternateform          TRUE if a '#' is present.
19245    **   flag_altform2               TRUE if a '!' is present.
19246    **   flag_plussign               TRUE if a '+' is present.
19247    **   flag_leftjustify            TRUE if a '-' is present or if the
19248    **                               field width was negative.
19249    **   flag_zeropad                TRUE if the width began with 0.
19250    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
19251    **                               the conversion character.
19252    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
19253    **                               the conversion character.
19254    **   flag_blanksign              TRUE if a ' ' is present.
19255    **   width                       The specified field width.  This is
19256    **                               always non-negative.  Zero is the default.
19257    **   precision                   The specified precision.  The default
19258    **                               is -1.
19259    **   xtype                       The class of the conversion.
19260    **   infop                       Pointer to the appropriate info struct.
19261    */
19262    switch( xtype ){
19263      case etPOINTER:
19264        flag_longlong = sizeof(char*)==sizeof(i64);
19265        flag_long = sizeof(char*)==sizeof(long int);
19266        /* Fall through into the next case */
19267      case etORDINAL:
19268      case etRADIX:
19269        if( infop->flags & FLAG_SIGNED ){
19270          i64 v;
19271          if( flag_longlong ){
19272            v = va_arg(ap,i64);
19273          }else if( flag_long ){
19274            v = va_arg(ap,long int);
19275          }else{
19276            v = va_arg(ap,int);
19277          }
19278          if( v<0 ){
19279            if( v==SMALLEST_INT64 ){
19280              longvalue = ((u64)1)<<63;
19281            }else{
19282              longvalue = -v;
19283            }
19284            prefix = '-';
19285          }else{
19286            longvalue = v;
19287            if( flag_plussign )        prefix = '+';
19288            else if( flag_blanksign )  prefix = ' ';
19289            else                       prefix = 0;
19290          }
19291        }else{
19292          if( flag_longlong ){
19293            longvalue = va_arg(ap,u64);
19294          }else if( flag_long ){
19295            longvalue = va_arg(ap,unsigned long int);
19296          }else{
19297            longvalue = va_arg(ap,unsigned int);
19298          }
19299          prefix = 0;
19300        }
19301        if( longvalue==0 ) flag_alternateform = 0;
19302        if( flag_zeropad && precision<width-(prefix!=0) ){
19303          precision = width-(prefix!=0);
19304        }
19305        bufpt = &buf[etBUFSIZE-1];
19306        if( xtype==etORDINAL ){
19307          static const char zOrd[] = "thstndrd";
19308          int x = (int)(longvalue % 10);
19309          if( x>=4 || (longvalue/10)%10==1 ){
19310            x = 0;
19311          }
19312          buf[etBUFSIZE-3] = zOrd[x*2];
19313          buf[etBUFSIZE-2] = zOrd[x*2+1];
19314          bufpt -= 2;
19315        }
19316        {
19317          register const char *cset;      /* Use registers for speed */
19318          register int base;
19319          cset = &aDigits[infop->charset];
19320          base = infop->base;
19321          do{                                           /* Convert to ascii */
19322            *(--bufpt) = cset[longvalue%base];
19323            longvalue = longvalue/base;
19324          }while( longvalue>0 );
19325        }
19326        length = (int)(&buf[etBUFSIZE-1]-bufpt);
19327        for(idx=precision-length; idx>0; idx--){
19328          *(--bufpt) = '0';                             /* Zero pad */
19329        }
19330        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
19331        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
19332          const char *pre;
19333          char x;
19334          pre = &aPrefix[infop->prefix];
19335          for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
19336        }
19337        length = (int)(&buf[etBUFSIZE-1]-bufpt);
19338        break;
19339      case etFLOAT:
19340      case etEXP:
19341      case etGENERIC:
19342        realvalue = va_arg(ap,double);
19343#ifdef SQLITE_OMIT_FLOATING_POINT
19344        length = 0;
19345#else
19346        if( precision<0 ) precision = 6;         /* Set default precision */
19347        if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
19348        if( realvalue<0.0 ){
19349          realvalue = -realvalue;
19350          prefix = '-';
19351        }else{
19352          if( flag_plussign )          prefix = '+';
19353          else if( flag_blanksign )    prefix = ' ';
19354          else                         prefix = 0;
19355        }
19356        if( xtype==etGENERIC && precision>0 ) precision--;
19357#if 0
19358        /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
19359        for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
19360#else
19361        /* It makes more sense to use 0.5 */
19362        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
19363#endif
19364        if( xtype==etFLOAT ) realvalue += rounder;
19365        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
19366        exp = 0;
19367        if( sqlite3IsNaN((double)realvalue) ){
19368          bufpt = "NaN";
19369          length = 3;
19370          break;
19371        }
19372        if( realvalue>0.0 ){
19373          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
19374          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
19375          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
19376          while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
19377          while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
19378          if( exp>350 ){
19379            if( prefix=='-' ){
19380              bufpt = "-Inf";
19381            }else if( prefix=='+' ){
19382              bufpt = "+Inf";
19383            }else{
19384              bufpt = "Inf";
19385            }
19386            length = sqlite3Strlen30(bufpt);
19387            break;
19388          }
19389        }
19390        bufpt = buf;
19391        /*
19392        ** If the field type is etGENERIC, then convert to either etEXP
19393        ** or etFLOAT, as appropriate.
19394        */
19395        flag_exp = xtype==etEXP;
19396        if( xtype!=etFLOAT ){
19397          realvalue += rounder;
19398          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
19399        }
19400        if( xtype==etGENERIC ){
19401          flag_rtz = !flag_alternateform;
19402          if( exp<-4 || exp>precision ){
19403            xtype = etEXP;
19404          }else{
19405            precision = precision - exp;
19406            xtype = etFLOAT;
19407          }
19408        }else{
19409          flag_rtz = 0;
19410        }
19411        if( xtype==etEXP ){
19412          e2 = 0;
19413        }else{
19414          e2 = exp;
19415        }
19416        nsd = 0;
19417        flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19418        /* The sign in front of the number */
19419        if( prefix ){
19420          *(bufpt++) = prefix;
19421        }
19422        /* Digits prior to the decimal point */
19423        if( e2<0 ){
19424          *(bufpt++) = '0';
19425        }else{
19426          for(; e2>=0; e2--){
19427            *(bufpt++) = et_getdigit(&realvalue,&nsd);
19428          }
19429        }
19430        /* The decimal point */
19431        if( flag_dp ){
19432          *(bufpt++) = '.';
19433        }
19434        /* "0" digits after the decimal point but before the first
19435        ** significant digit of the number */
19436        for(e2++; e2<0; precision--, e2++){
19437          assert( precision>0 );
19438          *(bufpt++) = '0';
19439        }
19440        /* Significant digits after the decimal point */
19441        while( (precision--)>0 ){
19442          *(bufpt++) = et_getdigit(&realvalue,&nsd);
19443        }
19444        /* Remove trailing zeros and the "." if no digits follow the "." */
19445        if( flag_rtz && flag_dp ){
19446          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19447          assert( bufpt>buf );
19448          if( bufpt[-1]=='.' ){
19449            if( flag_altform2 ){
19450              *(bufpt++) = '0';
19451            }else{
19452              *(--bufpt) = 0;
19453            }
19454          }
19455        }
19456        /* Add the "eNNN" suffix */
19457        if( flag_exp || xtype==etEXP ){
19458          *(bufpt++) = aDigits[infop->charset];
19459          if( exp<0 ){
19460            *(bufpt++) = '-'; exp = -exp;
19461          }else{
19462            *(bufpt++) = '+';
19463          }
19464          if( exp>=100 ){
19465            *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
19466            exp %= 100;
19467          }
19468          *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
19469          *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
19470        }
19471        *bufpt = 0;
19472
19473        /* The converted number is in buf[] and zero terminated. Output it.
19474        ** Note that the number is in the usual order, not reversed as with
19475        ** integer conversions. */
19476        length = (int)(bufpt-buf);
19477        bufpt = buf;
19478
19479        /* Special case:  Add leading zeros if the flag_zeropad flag is
19480        ** set and we are not left justified */
19481        if( flag_zeropad && !flag_leftjustify && length < width){
19482          int i;
19483          int nPad = width - length;
19484          for(i=width; i>=nPad; i--){
19485            bufpt[i] = bufpt[i-nPad];
19486          }
19487          i = prefix!=0;
19488          while( nPad-- ) bufpt[i++] = '0';
19489          length = width;
19490        }
19491#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
19492        break;
19493      case etSIZE:
19494        *(va_arg(ap,int*)) = pAccum->nChar;
19495        length = width = 0;
19496        break;
19497      case etPERCENT:
19498        buf[0] = '%';
19499        bufpt = buf;
19500        length = 1;
19501        break;
19502      case etCHARX:
19503        c = va_arg(ap,int);
19504        buf[0] = (char)c;
19505        if( precision>=0 ){
19506          for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
19507          length = precision;
19508        }else{
19509          length =1;
19510        }
19511        bufpt = buf;
19512        break;
19513      case etSTRING:
19514      case etDYNSTRING:
19515        bufpt = va_arg(ap,char*);
19516        if( bufpt==0 ){
19517          bufpt = "";
19518        }else if( xtype==etDYNSTRING ){
19519          zExtra = bufpt;
19520        }
19521        if( precision>=0 ){
19522          for(length=0; length<precision && bufpt[length]; length++){}
19523        }else{
19524          length = sqlite3Strlen30(bufpt);
19525        }
19526        break;
19527      case etSQLESCAPE:
19528      case etSQLESCAPE2:
19529      case etSQLESCAPE3: {
19530        int i, j, k, n, isnull;
19531        int needQuote;
19532        char ch;
19533        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
19534        char *escarg = va_arg(ap,char*);
19535        isnull = escarg==0;
19536        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
19537        k = precision;
19538        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
19539          if( ch==q )  n++;
19540        }
19541        needQuote = !isnull && xtype==etSQLESCAPE2;
19542        n += i + 1 + needQuote*2;
19543        if( n>etBUFSIZE ){
19544          bufpt = zExtra = sqlite3Malloc( n );
19545          if( bufpt==0 ){
19546            pAccum->mallocFailed = 1;
19547            return;
19548          }
19549        }else{
19550          bufpt = buf;
19551        }
19552        j = 0;
19553        if( needQuote ) bufpt[j++] = q;
19554        k = i;
19555        for(i=0; i<k; i++){
19556          bufpt[j++] = ch = escarg[i];
19557          if( ch==q ) bufpt[j++] = ch;
19558        }
19559        if( needQuote ) bufpt[j++] = q;
19560        bufpt[j] = 0;
19561        length = j;
19562        /* The precision in %q and %Q means how many input characters to
19563        ** consume, not the length of the output...
19564        ** if( precision>=0 && precision<length ) length = precision; */
19565        break;
19566      }
19567      case etTOKEN: {
19568        Token *pToken = va_arg(ap, Token*);
19569        if( pToken ){
19570          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
19571        }
19572        length = width = 0;
19573        break;
19574      }
19575      case etSRCLIST: {
19576        SrcList *pSrc = va_arg(ap, SrcList*);
19577        int k = va_arg(ap, int);
19578        struct SrcList_item *pItem = &pSrc->a[k];
19579        assert( k>=0 && k<pSrc->nSrc );
19580        if( pItem->zDatabase ){
19581          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
19582          sqlite3StrAccumAppend(pAccum, ".", 1);
19583        }
19584        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
19585        length = width = 0;
19586        break;
19587      }
19588      default: {
19589        assert( xtype==etINVALID );
19590        return;
19591      }
19592    }/* End switch over the format type */
19593    /*
19594    ** The text of the conversion is pointed to by "bufpt" and is
19595    ** "length" characters long.  The field width is "width".  Do
19596    ** the output.
19597    */
19598    if( !flag_leftjustify ){
19599      register int nspace;
19600      nspace = width-length;
19601      if( nspace>0 ){
19602        appendSpace(pAccum, nspace);
19603      }
19604    }
19605    if( length>0 ){
19606      sqlite3StrAccumAppend(pAccum, bufpt, length);
19607    }
19608    if( flag_leftjustify ){
19609      register int nspace;
19610      nspace = width-length;
19611      if( nspace>0 ){
19612        appendSpace(pAccum, nspace);
19613      }
19614    }
19615    if( zExtra ){
19616      sqlite3_free(zExtra);
19617    }
19618  }/* End for loop over the format string */
19619} /* End of function */
19620
19621/*
19622** Append N bytes of text from z to the StrAccum object.
19623*/
19624SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
19625  assert( z!=0 || N==0 );
19626  if( p->tooBig | p->mallocFailed ){
19627    testcase(p->tooBig);
19628    testcase(p->mallocFailed);
19629    return;
19630  }
19631  if( N<0 ){
19632    N = sqlite3Strlen30(z);
19633  }
19634  if( N==0 || NEVER(z==0) ){
19635    return;
19636  }
19637  if( p->nChar+N >= p->nAlloc ){
19638    char *zNew;
19639    if( !p->useMalloc ){
19640      p->tooBig = 1;
19641      N = p->nAlloc - p->nChar - 1;
19642      if( N<=0 ){
19643        return;
19644      }
19645    }else{
19646      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
19647      i64 szNew = p->nChar;
19648      szNew += N + 1;
19649      if( szNew > p->mxAlloc ){
19650        sqlite3StrAccumReset(p);
19651        p->tooBig = 1;
19652        return;
19653      }else{
19654        p->nAlloc = (int)szNew;
19655      }
19656      if( p->useMalloc==1 ){
19657        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19658      }else{
19659        zNew = sqlite3_realloc(zOld, p->nAlloc);
19660      }
19661      if( zNew ){
19662        if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
19663        p->zText = zNew;
19664      }else{
19665        p->mallocFailed = 1;
19666        sqlite3StrAccumReset(p);
19667        return;
19668      }
19669    }
19670  }
19671  memcpy(&p->zText[p->nChar], z, N);
19672  p->nChar += N;
19673}
19674
19675/*
19676** Finish off a string by making sure it is zero-terminated.
19677** Return a pointer to the resulting string.  Return a NULL
19678** pointer if any kind of error was encountered.
19679*/
19680SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
19681  if( p->zText ){
19682    p->zText[p->nChar] = 0;
19683    if( p->useMalloc && p->zText==p->zBase ){
19684      if( p->useMalloc==1 ){
19685        p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
19686      }else{
19687        p->zText = sqlite3_malloc(p->nChar+1);
19688      }
19689      if( p->zText ){
19690        memcpy(p->zText, p->zBase, p->nChar+1);
19691      }else{
19692        p->mallocFailed = 1;
19693      }
19694    }
19695  }
19696  return p->zText;
19697}
19698
19699/*
19700** Reset an StrAccum string.  Reclaim all malloced memory.
19701*/
19702SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
19703  if( p->zText!=p->zBase ){
19704    if( p->useMalloc==1 ){
19705      sqlite3DbFree(p->db, p->zText);
19706    }else{
19707      sqlite3_free(p->zText);
19708    }
19709  }
19710  p->zText = 0;
19711}
19712
19713/*
19714** Initialize a string accumulator
19715*/
19716SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
19717  p->zText = p->zBase = zBase;
19718  p->db = 0;
19719  p->nChar = 0;
19720  p->nAlloc = n;
19721  p->mxAlloc = mx;
19722  p->useMalloc = 1;
19723  p->tooBig = 0;
19724  p->mallocFailed = 0;
19725}
19726
19727/*
19728** Print into memory obtained from sqliteMalloc().  Use the internal
19729** %-conversion extensions.
19730*/
19731SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
19732  char *z;
19733  char zBase[SQLITE_PRINT_BUF_SIZE];
19734  StrAccum acc;
19735  assert( db!=0 );
19736  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
19737                      db->aLimit[SQLITE_LIMIT_LENGTH]);
19738  acc.db = db;
19739  sqlite3VXPrintf(&acc, 1, zFormat, ap);
19740  z = sqlite3StrAccumFinish(&acc);
19741  if( acc.mallocFailed ){
19742    db->mallocFailed = 1;
19743  }
19744  return z;
19745}
19746
19747/*
19748** Print into memory obtained from sqliteMalloc().  Use the internal
19749** %-conversion extensions.
19750*/
19751SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
19752  va_list ap;
19753  char *z;
19754  va_start(ap, zFormat);
19755  z = sqlite3VMPrintf(db, zFormat, ap);
19756  va_end(ap);
19757  return z;
19758}
19759
19760/*
19761** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
19762** the string and before returnning.  This routine is intended to be used
19763** to modify an existing string.  For example:
19764**
19765**       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
19766**
19767*/
19768SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
19769  va_list ap;
19770  char *z;
19771  va_start(ap, zFormat);
19772  z = sqlite3VMPrintf(db, zFormat, ap);
19773  va_end(ap);
19774  sqlite3DbFree(db, zStr);
19775  return z;
19776}
19777
19778/*
19779** Print into memory obtained from sqlite3_malloc().  Omit the internal
19780** %-conversion extensions.
19781*/
19782SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
19783  char *z;
19784  char zBase[SQLITE_PRINT_BUF_SIZE];
19785  StrAccum acc;
19786#ifndef SQLITE_OMIT_AUTOINIT
19787  if( sqlite3_initialize() ) return 0;
19788#endif
19789  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
19790  acc.useMalloc = 2;
19791  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19792  z = sqlite3StrAccumFinish(&acc);
19793  return z;
19794}
19795
19796/*
19797** Print into memory obtained from sqlite3_malloc()().  Omit the internal
19798** %-conversion extensions.
19799*/
19800SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
19801  va_list ap;
19802  char *z;
19803#ifndef SQLITE_OMIT_AUTOINIT
19804  if( sqlite3_initialize() ) return 0;
19805#endif
19806  va_start(ap, zFormat);
19807  z = sqlite3_vmprintf(zFormat, ap);
19808  va_end(ap);
19809  return z;
19810}
19811
19812/*
19813** sqlite3_snprintf() works like snprintf() except that it ignores the
19814** current locale settings.  This is important for SQLite because we
19815** are not able to use a "," as the decimal point in place of "." as
19816** specified by some locales.
19817**
19818** Oops:  The first two arguments of sqlite3_snprintf() are backwards
19819** from the snprintf() standard.  Unfortunately, it is too late to change
19820** this without breaking compatibility, so we just have to live with the
19821** mistake.
19822**
19823** sqlite3_vsnprintf() is the varargs version.
19824*/
19825SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
19826  StrAccum acc;
19827  if( n<=0 ) return zBuf;
19828  sqlite3StrAccumInit(&acc, zBuf, n, 0);
19829  acc.useMalloc = 0;
19830  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19831  return sqlite3StrAccumFinish(&acc);
19832}
19833SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
19834  char *z;
19835  va_list ap;
19836  va_start(ap,zFormat);
19837  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
19838  va_end(ap);
19839  return z;
19840}
19841
19842/*
19843** This is the routine that actually formats the sqlite3_log() message.
19844** We house it in a separate routine from sqlite3_log() to avoid using
19845** stack space on small-stack systems when logging is disabled.
19846**
19847** sqlite3_log() must render into a static buffer.  It cannot dynamically
19848** allocate memory because it might be called while the memory allocator
19849** mutex is held.
19850*/
19851static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
19852  StrAccum acc;                          /* String accumulator */
19853  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
19854
19855  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
19856  acc.useMalloc = 0;
19857  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19858  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
19859                           sqlite3StrAccumFinish(&acc));
19860}
19861
19862/*
19863** Format and write a message to the log if logging is enabled.
19864*/
19865SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
19866  va_list ap;                             /* Vararg list */
19867  if( sqlite3GlobalConfig.xLog ){
19868    va_start(ap, zFormat);
19869    renderLogMsg(iErrCode, zFormat, ap);
19870    va_end(ap);
19871  }
19872}
19873
19874#if defined(SQLITE_DEBUG)
19875/*
19876** A version of printf() that understands %lld.  Used for debugging.
19877** The printf() built into some versions of windows does not understand %lld
19878** and segfaults if you give it a long long int.
19879*/
19880SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
19881  va_list ap;
19882  StrAccum acc;
19883  char zBuf[500];
19884  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
19885  acc.useMalloc = 0;
19886  va_start(ap,zFormat);
19887  sqlite3VXPrintf(&acc, 0, zFormat, ap);
19888  va_end(ap);
19889  sqlite3StrAccumFinish(&acc);
19890  fprintf(stdout,"%s", zBuf);
19891  fflush(stdout);
19892}
19893#endif
19894
19895#ifndef SQLITE_OMIT_TRACE
19896/*
19897** variable-argument wrapper around sqlite3VXPrintf().
19898*/
19899SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
19900  va_list ap;
19901  va_start(ap,zFormat);
19902  sqlite3VXPrintf(p, 1, zFormat, ap);
19903  va_end(ap);
19904}
19905#endif
19906
19907/************** End of printf.c **********************************************/
19908/************** Begin file random.c ******************************************/
19909/*
19910** 2001 September 15
19911**
19912** The author disclaims copyright to this source code.  In place of
19913** a legal notice, here is a blessing:
19914**
19915**    May you do good and not evil.
19916**    May you find forgiveness for yourself and forgive others.
19917**    May you share freely, never taking more than you give.
19918**
19919*************************************************************************
19920** This file contains code to implement a pseudo-random number
19921** generator (PRNG) for SQLite.
19922**
19923** Random numbers are used by some of the database backends in order
19924** to generate random integer keys for tables or random filenames.
19925*/
19926
19927
19928/* All threads share a single random number generator.
19929** This structure is the current state of the generator.
19930*/
19931static SQLITE_WSD struct sqlite3PrngType {
19932  unsigned char isInit;          /* True if initialized */
19933  unsigned char i, j;            /* State variables */
19934  unsigned char s[256];          /* State variables */
19935} sqlite3Prng;
19936
19937/*
19938** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
19939** must be held while executing this routine.
19940**
19941** Why not just use a library random generator like lrand48() for this?
19942** Because the OP_NewRowid opcode in the VDBE depends on having a very
19943** good source of random numbers.  The lrand48() library function may
19944** well be good enough.  But maybe not.  Or maybe lrand48() has some
19945** subtle problems on some systems that could cause problems.  It is hard
19946** to know.  To minimize the risk of problems due to bad lrand48()
19947** implementations, SQLite uses this random number generator based
19948** on RC4, which we know works very well.
19949**
19950** (Later):  Actually, OP_NewRowid does not depend on a good source of
19951** randomness any more.  But we will leave this code in all the same.
19952*/
19953static u8 randomByte(void){
19954  unsigned char t;
19955
19956
19957  /* The "wsdPrng" macro will resolve to the pseudo-random number generator
19958  ** state vector.  If writable static data is unsupported on the target,
19959  ** we have to locate the state vector at run-time.  In the more common
19960  ** case where writable static data is supported, wsdPrng can refer directly
19961  ** to the "sqlite3Prng" state vector declared above.
19962  */
19963#ifdef SQLITE_OMIT_WSD
19964  struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
19965# define wsdPrng p[0]
19966#else
19967# define wsdPrng sqlite3Prng
19968#endif
19969
19970
19971  /* Initialize the state of the random number generator once,
19972  ** the first time this routine is called.  The seed value does
19973  ** not need to contain a lot of randomness since we are not
19974  ** trying to do secure encryption or anything like that...
19975  **
19976  ** Nothing in this file or anywhere else in SQLite does any kind of
19977  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
19978  ** number generator) not as an encryption device.
19979  */
19980  if( !wsdPrng.isInit ){
19981    int i;
19982    char k[256];
19983    wsdPrng.j = 0;
19984    wsdPrng.i = 0;
19985    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
19986    for(i=0; i<256; i++){
19987      wsdPrng.s[i] = (u8)i;
19988    }
19989    for(i=0; i<256; i++){
19990      wsdPrng.j += wsdPrng.s[i] + k[i];
19991      t = wsdPrng.s[wsdPrng.j];
19992      wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
19993      wsdPrng.s[i] = t;
19994    }
19995    wsdPrng.isInit = 1;
19996  }
19997
19998  /* Generate and return single random byte
19999  */
20000  wsdPrng.i++;
20001  t = wsdPrng.s[wsdPrng.i];
20002  wsdPrng.j += t;
20003  wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
20004  wsdPrng.s[wsdPrng.j] = t;
20005  t += wsdPrng.s[wsdPrng.i];
20006  return wsdPrng.s[t];
20007}
20008
20009/*
20010** Return N random bytes.
20011*/
20012SQLITE_API void sqlite3_randomness(int N, void *pBuf){
20013  unsigned char *zBuf = pBuf;
20014#if SQLITE_THREADSAFE
20015  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
20016#endif
20017  sqlite3_mutex_enter(mutex);
20018  while( N-- ){
20019    *(zBuf++) = randomByte();
20020  }
20021  sqlite3_mutex_leave(mutex);
20022}
20023
20024#ifndef SQLITE_OMIT_BUILTIN_TEST
20025/*
20026** For testing purposes, we sometimes want to preserve the state of
20027** PRNG and restore the PRNG to its saved state at a later time, or
20028** to reset the PRNG to its initial state.  These routines accomplish
20029** those tasks.
20030**
20031** The sqlite3_test_control() interface calls these routines to
20032** control the PRNG.
20033*/
20034static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
20035SQLITE_PRIVATE void sqlite3PrngSaveState(void){
20036  memcpy(
20037    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
20038    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
20039    sizeof(sqlite3Prng)
20040  );
20041}
20042SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
20043  memcpy(
20044    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
20045    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
20046    sizeof(sqlite3Prng)
20047  );
20048}
20049SQLITE_PRIVATE void sqlite3PrngResetState(void){
20050  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
20051}
20052#endif /* SQLITE_OMIT_BUILTIN_TEST */
20053
20054/************** End of random.c **********************************************/
20055/************** Begin file utf.c *********************************************/
20056/*
20057** 2004 April 13
20058**
20059** The author disclaims copyright to this source code.  In place of
20060** a legal notice, here is a blessing:
20061**
20062**    May you do good and not evil.
20063**    May you find forgiveness for yourself and forgive others.
20064**    May you share freely, never taking more than you give.
20065**
20066*************************************************************************
20067** This file contains routines used to translate between UTF-8,
20068** UTF-16, UTF-16BE, and UTF-16LE.
20069**
20070** Notes on UTF-8:
20071**
20072**   Byte-0    Byte-1    Byte-2    Byte-3    Value
20073**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
20074**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
20075**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
20076**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
20077**
20078**
20079** Notes on UTF-16:  (with wwww+1==uuuuu)
20080**
20081**      Word-0               Word-1          Value
20082**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
20083**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
20084**
20085**
20086** BOM or Byte Order Mark:
20087**     0xff 0xfe   little-endian utf-16 follows
20088**     0xfe 0xff   big-endian utf-16 follows
20089**
20090*/
20091/* #include <assert.h> */
20092
20093#ifndef SQLITE_AMALGAMATION
20094/*
20095** The following constant value is used by the SQLITE_BIGENDIAN and
20096** SQLITE_LITTLEENDIAN macros.
20097*/
20098SQLITE_PRIVATE const int sqlite3one = 1;
20099#endif /* SQLITE_AMALGAMATION */
20100
20101/*
20102** This lookup table is used to help decode the first byte of
20103** a multi-byte UTF8 character.
20104*/
20105static const unsigned char sqlite3Utf8Trans1[] = {
20106  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20107  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
20108  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
20109  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
20110  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20111  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
20112  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
20113  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
20114};
20115
20116
20117#define WRITE_UTF8(zOut, c) {                          \
20118  if( c<0x00080 ){                                     \
20119    *zOut++ = (u8)(c&0xFF);                            \
20120  }                                                    \
20121  else if( c<0x00800 ){                                \
20122    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
20123    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20124  }                                                    \
20125  else if( c<0x10000 ){                                \
20126    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
20127    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
20128    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20129  }else{                                               \
20130    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
20131    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
20132    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
20133    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
20134  }                                                    \
20135}
20136
20137#define WRITE_UTF16LE(zOut, c) {                                    \
20138  if( c<=0xFFFF ){                                                  \
20139    *zOut++ = (u8)(c&0x00FF);                                       \
20140    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
20141  }else{                                                            \
20142    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
20143    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
20144    *zOut++ = (u8)(c&0x00FF);                                       \
20145    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
20146  }                                                                 \
20147}
20148
20149#define WRITE_UTF16BE(zOut, c) {                                    \
20150  if( c<=0xFFFF ){                                                  \
20151    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
20152    *zOut++ = (u8)(c&0x00FF);                                       \
20153  }else{                                                            \
20154    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
20155    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
20156    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
20157    *zOut++ = (u8)(c&0x00FF);                                       \
20158  }                                                                 \
20159}
20160
20161#define READ_UTF16LE(zIn, TERM, c){                                   \
20162  c = (*zIn++);                                                       \
20163  c += ((*zIn++)<<8);                                                 \
20164  if( c>=0xD800 && c<0xE000 && TERM ){                                \
20165    int c2 = (*zIn++);                                                \
20166    c2 += ((*zIn++)<<8);                                              \
20167    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
20168  }                                                                   \
20169}
20170
20171#define READ_UTF16BE(zIn, TERM, c){                                   \
20172  c = ((*zIn++)<<8);                                                  \
20173  c += (*zIn++);                                                      \
20174  if( c>=0xD800 && c<0xE000 && TERM ){                                \
20175    int c2 = ((*zIn++)<<8);                                           \
20176    c2 += (*zIn++);                                                   \
20177    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
20178  }                                                                   \
20179}
20180
20181/*
20182** Translate a single UTF-8 character.  Return the unicode value.
20183**
20184** During translation, assume that the byte that zTerm points
20185** is a 0x00.
20186**
20187** Write a pointer to the next unread byte back into *pzNext.
20188**
20189** Notes On Invalid UTF-8:
20190**
20191**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
20192**     be encoded as a multi-byte character.  Any multi-byte character that
20193**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
20194**
20195**  *  This routine never allows a UTF16 surrogate value to be encoded.
20196**     If a multi-byte character attempts to encode a value between
20197**     0xd800 and 0xe000 then it is rendered as 0xfffd.
20198**
20199**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
20200**     byte of a character are interpreted as single-byte characters
20201**     and rendered as themselves even though they are technically
20202**     invalid characters.
20203**
20204**  *  This routine accepts an infinite number of different UTF8 encodings
20205**     for unicode values 0x80 and greater.  It do not change over-length
20206**     encodings to 0xfffd as some systems recommend.
20207*/
20208#define READ_UTF8(zIn, zTerm, c)                           \
20209  c = *(zIn++);                                            \
20210  if( c>=0xc0 ){                                           \
20211    c = sqlite3Utf8Trans1[c-0xc0];                         \
20212    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
20213      c = (c<<6) + (0x3f & *(zIn++));                      \
20214    }                                                      \
20215    if( c<0x80                                             \
20216        || (c&0xFFFFF800)==0xD800                          \
20217        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
20218  }
20219SQLITE_PRIVATE u32 sqlite3Utf8Read(
20220  const unsigned char *zIn,       /* First byte of UTF-8 character */
20221  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
20222){
20223  unsigned int c;
20224
20225  /* Same as READ_UTF8() above but without the zTerm parameter.
20226  ** For this routine, we assume the UTF8 string is always zero-terminated.
20227  */
20228  c = *(zIn++);
20229  if( c>=0xc0 ){
20230    c = sqlite3Utf8Trans1[c-0xc0];
20231    while( (*zIn & 0xc0)==0x80 ){
20232      c = (c<<6) + (0x3f & *(zIn++));
20233    }
20234    if( c<0x80
20235        || (c&0xFFFFF800)==0xD800
20236        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
20237  }
20238  *pzNext = zIn;
20239  return c;
20240}
20241
20242
20243
20244
20245/*
20246** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
20247** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
20248*/
20249/* #define TRANSLATE_TRACE 1 */
20250
20251#ifndef SQLITE_OMIT_UTF16
20252/*
20253** This routine transforms the internal text encoding used by pMem to
20254** desiredEnc. It is an error if the string is already of the desired
20255** encoding, or if *pMem does not contain a string value.
20256*/
20257SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
20258  int len;                    /* Maximum length of output string in bytes */
20259  unsigned char *zOut;                  /* Output buffer */
20260  unsigned char *zIn;                   /* Input iterator */
20261  unsigned char *zTerm;                 /* End of input */
20262  unsigned char *z;                     /* Output iterator */
20263  unsigned int c;
20264
20265  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
20266  assert( pMem->flags&MEM_Str );
20267  assert( pMem->enc!=desiredEnc );
20268  assert( pMem->enc!=0 );
20269  assert( pMem->n>=0 );
20270
20271#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
20272  {
20273    char zBuf[100];
20274    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
20275    fprintf(stderr, "INPUT:  %s\n", zBuf);
20276  }
20277#endif
20278
20279  /* If the translation is between UTF-16 little and big endian, then
20280  ** all that is required is to swap the byte order. This case is handled
20281  ** differently from the others.
20282  */
20283  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
20284    u8 temp;
20285    int rc;
20286    rc = sqlite3VdbeMemMakeWriteable(pMem);
20287    if( rc!=SQLITE_OK ){
20288      assert( rc==SQLITE_NOMEM );
20289      return SQLITE_NOMEM;
20290    }
20291    zIn = (u8*)pMem->z;
20292    zTerm = &zIn[pMem->n&~1];
20293    while( zIn<zTerm ){
20294      temp = *zIn;
20295      *zIn = *(zIn+1);
20296      zIn++;
20297      *zIn++ = temp;
20298    }
20299    pMem->enc = desiredEnc;
20300    goto translate_out;
20301  }
20302
20303  /* Set len to the maximum number of bytes required in the output buffer. */
20304  if( desiredEnc==SQLITE_UTF8 ){
20305    /* When converting from UTF-16, the maximum growth results from
20306    ** translating a 2-byte character to a 4-byte UTF-8 character.
20307    ** A single byte is required for the output string
20308    ** nul-terminator.
20309    */
20310    pMem->n &= ~1;
20311    len = pMem->n * 2 + 1;
20312  }else{
20313    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
20314    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
20315    ** character. Two bytes are required in the output buffer for the
20316    ** nul-terminator.
20317    */
20318    len = pMem->n * 2 + 2;
20319  }
20320
20321  /* Set zIn to point at the start of the input buffer and zTerm to point 1
20322  ** byte past the end.
20323  **
20324  ** Variable zOut is set to point at the output buffer, space obtained
20325  ** from sqlite3_malloc().
20326  */
20327  zIn = (u8*)pMem->z;
20328  zTerm = &zIn[pMem->n];
20329  zOut = sqlite3DbMallocRaw(pMem->db, len);
20330  if( !zOut ){
20331    return SQLITE_NOMEM;
20332  }
20333  z = zOut;
20334
20335  if( pMem->enc==SQLITE_UTF8 ){
20336    if( desiredEnc==SQLITE_UTF16LE ){
20337      /* UTF-8 -> UTF-16 Little-endian */
20338      while( zIn<zTerm ){
20339        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
20340        READ_UTF8(zIn, zTerm, c);
20341        WRITE_UTF16LE(z, c);
20342      }
20343    }else{
20344      assert( desiredEnc==SQLITE_UTF16BE );
20345      /* UTF-8 -> UTF-16 Big-endian */
20346      while( zIn<zTerm ){
20347        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
20348        READ_UTF8(zIn, zTerm, c);
20349        WRITE_UTF16BE(z, c);
20350      }
20351    }
20352    pMem->n = (int)(z - zOut);
20353    *z++ = 0;
20354  }else{
20355    assert( desiredEnc==SQLITE_UTF8 );
20356    if( pMem->enc==SQLITE_UTF16LE ){
20357      /* UTF-16 Little-endian -> UTF-8 */
20358      while( zIn<zTerm ){
20359        READ_UTF16LE(zIn, zIn<zTerm, c);
20360        WRITE_UTF8(z, c);
20361      }
20362    }else{
20363      /* UTF-16 Big-endian -> UTF-8 */
20364      while( zIn<zTerm ){
20365        READ_UTF16BE(zIn, zIn<zTerm, c);
20366        WRITE_UTF8(z, c);
20367      }
20368    }
20369    pMem->n = (int)(z - zOut);
20370  }
20371  *z = 0;
20372  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
20373
20374  sqlite3VdbeMemRelease(pMem);
20375  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
20376  pMem->enc = desiredEnc;
20377  pMem->flags |= (MEM_Term|MEM_Dyn);
20378  pMem->z = (char*)zOut;
20379  pMem->zMalloc = pMem->z;
20380
20381translate_out:
20382#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
20383  {
20384    char zBuf[100];
20385    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
20386    fprintf(stderr, "OUTPUT: %s\n", zBuf);
20387  }
20388#endif
20389  return SQLITE_OK;
20390}
20391
20392/*
20393** This routine checks for a byte-order mark at the beginning of the
20394** UTF-16 string stored in *pMem. If one is present, it is removed and
20395** the encoding of the Mem adjusted. This routine does not do any
20396** byte-swapping, it just sets Mem.enc appropriately.
20397**
20398** The allocation (static, dynamic etc.) and encoding of the Mem may be
20399** changed by this function.
20400*/
20401SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
20402  int rc = SQLITE_OK;
20403  u8 bom = 0;
20404
20405  assert( pMem->n>=0 );
20406  if( pMem->n>1 ){
20407    u8 b1 = *(u8 *)pMem->z;
20408    u8 b2 = *(((u8 *)pMem->z) + 1);
20409    if( b1==0xFE && b2==0xFF ){
20410      bom = SQLITE_UTF16BE;
20411    }
20412    if( b1==0xFF && b2==0xFE ){
20413      bom = SQLITE_UTF16LE;
20414    }
20415  }
20416
20417  if( bom ){
20418    rc = sqlite3VdbeMemMakeWriteable(pMem);
20419    if( rc==SQLITE_OK ){
20420      pMem->n -= 2;
20421      memmove(pMem->z, &pMem->z[2], pMem->n);
20422      pMem->z[pMem->n] = '\0';
20423      pMem->z[pMem->n+1] = '\0';
20424      pMem->flags |= MEM_Term;
20425      pMem->enc = bom;
20426    }
20427  }
20428  return rc;
20429}
20430#endif /* SQLITE_OMIT_UTF16 */
20431
20432/*
20433** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
20434** return the number of unicode characters in pZ up to (but not including)
20435** the first 0x00 byte. If nByte is not less than zero, return the
20436** number of unicode characters in the first nByte of pZ (or up to
20437** the first 0x00, whichever comes first).
20438*/
20439SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
20440  int r = 0;
20441  const u8 *z = (const u8*)zIn;
20442  const u8 *zTerm;
20443  if( nByte>=0 ){
20444    zTerm = &z[nByte];
20445  }else{
20446    zTerm = (const u8*)(-1);
20447  }
20448  assert( z<=zTerm );
20449  while( *z!=0 && z<zTerm ){
20450    SQLITE_SKIP_UTF8(z);
20451    r++;
20452  }
20453  return r;
20454}
20455
20456/* This test function is not currently used by the automated test-suite.
20457** Hence it is only available in debug builds.
20458*/
20459#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
20460/*
20461** Translate UTF-8 to UTF-8.
20462**
20463** This has the effect of making sure that the string is well-formed
20464** UTF-8.  Miscoded characters are removed.
20465**
20466** The translation is done in-place and aborted if the output
20467** overruns the input.
20468*/
20469SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
20470  unsigned char *zOut = zIn;
20471  unsigned char *zStart = zIn;
20472  u32 c;
20473
20474  while( zIn[0] && zOut<=zIn ){
20475    c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
20476    if( c!=0xfffd ){
20477      WRITE_UTF8(zOut, c);
20478    }
20479  }
20480  *zOut = 0;
20481  return (int)(zOut - zStart);
20482}
20483#endif
20484
20485#ifndef SQLITE_OMIT_UTF16
20486/*
20487** Convert a UTF-16 string in the native encoding into a UTF-8 string.
20488** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
20489** be freed by the calling function.
20490**
20491** NULL is returned if there is an allocation error.
20492*/
20493SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
20494  Mem m;
20495  memset(&m, 0, sizeof(m));
20496  m.db = db;
20497  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
20498  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
20499  if( db->mallocFailed ){
20500    sqlite3VdbeMemRelease(&m);
20501    m.z = 0;
20502  }
20503  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
20504  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
20505  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
20506  assert( m.z || db->mallocFailed );
20507  return m.z;
20508}
20509
20510/*
20511** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
20512** enc. A pointer to the new string is returned, and the value of *pnOut
20513** is set to the length of the returned string in bytes. The call should
20514** arrange to call sqlite3DbFree() on the returned pointer when it is
20515** no longer required.
20516**
20517** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20518** flag set.
20519*/
20520#ifdef SQLITE_ENABLE_STAT2
20521SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20522  Mem m;
20523  memset(&m, 0, sizeof(m));
20524  m.db = db;
20525  sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
20526  if( sqlite3VdbeMemTranslate(&m, enc) ){
20527    assert( db->mallocFailed );
20528    return 0;
20529  }
20530  assert( m.z==m.zMalloc );
20531  *pnOut = m.n;
20532  return m.z;
20533}
20534#endif
20535
20536/*
20537** zIn is a UTF-16 encoded unicode string at least nChar characters long.
20538** Return the number of bytes in the first nChar unicode characters
20539** in pZ.  nChar must be non-negative.
20540*/
20541SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
20542  int c;
20543  unsigned char const *z = zIn;
20544  int n = 0;
20545
20546  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
20547    while( n<nChar ){
20548      READ_UTF16BE(z, 1, c);
20549      n++;
20550    }
20551  }else{
20552    while( n<nChar ){
20553      READ_UTF16LE(z, 1, c);
20554      n++;
20555    }
20556  }
20557  return (int)(z-(unsigned char const *)zIn);
20558}
20559
20560#if defined(SQLITE_TEST)
20561/*
20562** This routine is called from the TCL test function "translate_selftest".
20563** It checks that the primitives for serializing and deserializing
20564** characters in each encoding are inverses of each other.
20565*/
20566SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
20567  unsigned int i, t;
20568  unsigned char zBuf[20];
20569  unsigned char *z;
20570  int n;
20571  unsigned int c;
20572
20573  for(i=0; i<0x00110000; i++){
20574    z = zBuf;
20575    WRITE_UTF8(z, i);
20576    n = (int)(z-zBuf);
20577    assert( n>0 && n<=4 );
20578    z[0] = 0;
20579    z = zBuf;
20580    c = sqlite3Utf8Read(z, (const u8**)&z);
20581    t = i;
20582    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
20583    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
20584    assert( c==t );
20585    assert( (z-zBuf)==n );
20586  }
20587  for(i=0; i<0x00110000; i++){
20588    if( i>=0xD800 && i<0xE000 ) continue;
20589    z = zBuf;
20590    WRITE_UTF16LE(z, i);
20591    n = (int)(z-zBuf);
20592    assert( n>0 && n<=4 );
20593    z[0] = 0;
20594    z = zBuf;
20595    READ_UTF16LE(z, 1, c);
20596    assert( c==i );
20597    assert( (z-zBuf)==n );
20598  }
20599  for(i=0; i<0x00110000; i++){
20600    if( i>=0xD800 && i<0xE000 ) continue;
20601    z = zBuf;
20602    WRITE_UTF16BE(z, i);
20603    n = (int)(z-zBuf);
20604    assert( n>0 && n<=4 );
20605    z[0] = 0;
20606    z = zBuf;
20607    READ_UTF16BE(z, 1, c);
20608    assert( c==i );
20609    assert( (z-zBuf)==n );
20610  }
20611}
20612#endif /* SQLITE_TEST */
20613#endif /* SQLITE_OMIT_UTF16 */
20614
20615/************** End of utf.c *************************************************/
20616/************** Begin file util.c ********************************************/
20617/*
20618** 2001 September 15
20619**
20620** The author disclaims copyright to this source code.  In place of
20621** a legal notice, here is a blessing:
20622**
20623**    May you do good and not evil.
20624**    May you find forgiveness for yourself and forgive others.
20625**    May you share freely, never taking more than you give.
20626**
20627*************************************************************************
20628** Utility functions used throughout sqlite.
20629**
20630** This file contains functions for allocating memory, comparing
20631** strings, and stuff like that.
20632**
20633*/
20634/* #include <stdarg.h> */
20635#ifdef SQLITE_HAVE_ISNAN
20636# include <math.h>
20637#endif
20638
20639/*
20640** Routine needed to support the testcase() macro.
20641*/
20642#ifdef SQLITE_COVERAGE_TEST
20643SQLITE_PRIVATE void sqlite3Coverage(int x){
20644  static unsigned dummy = 0;
20645  dummy += (unsigned)x;
20646}
20647#endif
20648
20649#ifndef SQLITE_OMIT_FLOATING_POINT
20650/*
20651** Return true if the floating point value is Not a Number (NaN).
20652**
20653** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
20654** Otherwise, we have our own implementation that works on most systems.
20655*/
20656SQLITE_PRIVATE int sqlite3IsNaN(double x){
20657  int rc;   /* The value return */
20658#if !defined(SQLITE_HAVE_ISNAN)
20659  /*
20660  ** Systems that support the isnan() library function should probably
20661  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
20662  ** found that many systems do not have a working isnan() function so
20663  ** this implementation is provided as an alternative.
20664  **
20665  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
20666  ** On the other hand, the use of -ffast-math comes with the following
20667  ** warning:
20668  **
20669  **      This option [-ffast-math] should never be turned on by any
20670  **      -O option since it can result in incorrect output for programs
20671  **      which depend on an exact implementation of IEEE or ISO
20672  **      rules/specifications for math functions.
20673  **
20674  ** Under MSVC, this NaN test may fail if compiled with a floating-
20675  ** point precision mode other than /fp:precise.  From the MSDN
20676  ** documentation:
20677  **
20678  **      The compiler [with /fp:precise] will properly handle comparisons
20679  **      involving NaN. For example, x != x evaluates to true if x is NaN
20680  **      ...
20681  */
20682#ifdef __FAST_MATH__
20683# error SQLite will not work correctly with the -ffast-math option of GCC.
20684#endif
20685  volatile double y = x;
20686  volatile double z = y;
20687  rc = (y!=z);
20688#else  /* if defined(SQLITE_HAVE_ISNAN) */
20689  rc = isnan(x);
20690#endif /* SQLITE_HAVE_ISNAN */
20691  testcase( rc );
20692  return rc;
20693}
20694#endif /* SQLITE_OMIT_FLOATING_POINT */
20695
20696/*
20697** Compute a string length that is limited to what can be stored in
20698** lower 30 bits of a 32-bit signed integer.
20699**
20700** The value returned will never be negative.  Nor will it ever be greater
20701** than the actual length of the string.  For very long strings (greater
20702** than 1GiB) the value returned might be less than the true string length.
20703*/
20704SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
20705  const char *z2 = z;
20706  if( z==0 ) return 0;
20707  while( *z2 ){ z2++; }
20708  return 0x3fffffff & (int)(z2 - z);
20709}
20710
20711/*
20712** Set the most recent error code and error string for the sqlite
20713** handle "db". The error code is set to "err_code".
20714**
20715** If it is not NULL, string zFormat specifies the format of the
20716** error string in the style of the printf functions: The following
20717** format characters are allowed:
20718**
20719**      %s      Insert a string
20720**      %z      A string that should be freed after use
20721**      %d      Insert an integer
20722**      %T      Insert a token
20723**      %S      Insert the first element of a SrcList
20724**
20725** zFormat and any string tokens that follow it are assumed to be
20726** encoded in UTF-8.
20727**
20728** To clear the most recent error for sqlite handle "db", sqlite3Error
20729** should be called with err_code set to SQLITE_OK and zFormat set
20730** to NULL.
20731*/
20732SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
20733  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
20734    db->errCode = err_code;
20735    if( zFormat ){
20736      char *z;
20737      va_list ap;
20738      va_start(ap, zFormat);
20739      z = sqlite3VMPrintf(db, zFormat, ap);
20740      va_end(ap);
20741      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
20742    }else{
20743      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
20744    }
20745  }
20746}
20747
20748/*
20749** Add an error message to pParse->zErrMsg and increment pParse->nErr.
20750** The following formatting characters are allowed:
20751**
20752**      %s      Insert a string
20753**      %z      A string that should be freed after use
20754**      %d      Insert an integer
20755**      %T      Insert a token
20756**      %S      Insert the first element of a SrcList
20757**
20758** This function should be used to report any error that occurs whilst
20759** compiling an SQL statement (i.e. within sqlite3_prepare()). The
20760** last thing the sqlite3_prepare() function does is copy the error
20761** stored by this function into the database handle using sqlite3Error().
20762** Function sqlite3Error() should be used during statement execution
20763** (sqlite3_step() etc.).
20764*/
20765SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
20766  char *zMsg;
20767  va_list ap;
20768  sqlite3 *db = pParse->db;
20769  va_start(ap, zFormat);
20770  zMsg = sqlite3VMPrintf(db, zFormat, ap);
20771  va_end(ap);
20772  if( db->suppressErr ){
20773    sqlite3DbFree(db, zMsg);
20774  }else{
20775    pParse->nErr++;
20776    sqlite3DbFree(db, pParse->zErrMsg);
20777    pParse->zErrMsg = zMsg;
20778    pParse->rc = SQLITE_ERROR;
20779  }
20780}
20781
20782/*
20783** Convert an SQL-style quoted string into a normal string by removing
20784** the quote characters.  The conversion is done in-place.  If the
20785** input does not begin with a quote character, then this routine
20786** is a no-op.
20787**
20788** The input string must be zero-terminated.  A new zero-terminator
20789** is added to the dequoted string.
20790**
20791** The return value is -1 if no dequoting occurs or the length of the
20792** dequoted string, exclusive of the zero terminator, if dequoting does
20793** occur.
20794**
20795** 2002-Feb-14: This routine is extended to remove MS-Access style
20796** brackets from around identifers.  For example:  "[a-b-c]" becomes
20797** "a-b-c".
20798*/
20799SQLITE_PRIVATE int sqlite3Dequote(char *z){
20800  char quote;
20801  int i, j;
20802  if( z==0 ) return -1;
20803  quote = z[0];
20804  switch( quote ){
20805    case '\'':  break;
20806    case '"':   break;
20807    case '`':   break;                /* For MySQL compatibility */
20808    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
20809    default:    return -1;
20810  }
20811  for(i=1, j=0; ALWAYS(z[i]); i++){
20812    if( z[i]==quote ){
20813      if( z[i+1]==quote ){
20814        z[j++] = quote;
20815        i++;
20816      }else{
20817        break;
20818      }
20819    }else{
20820      z[j++] = z[i];
20821    }
20822  }
20823  z[j] = 0;
20824  return j;
20825}
20826
20827/* Convenient short-hand */
20828#define UpperToLower sqlite3UpperToLower
20829
20830/*
20831** Some systems have stricmp().  Others have strcasecmp().  Because
20832** there is no consistency, we will define our own.
20833**
20834** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
20835** applications and extensions to compare the contents of two buffers
20836** containing UTF-8 strings in a case-independent fashion, using the same
20837** definition of case independence that SQLite uses internally when
20838** comparing identifiers.
20839*/
20840SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
20841  register unsigned char *a, *b;
20842  a = (unsigned char *)zLeft;
20843  b = (unsigned char *)zRight;
20844  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20845  return UpperToLower[*a] - UpperToLower[*b];
20846}
20847SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
20848  register unsigned char *a, *b;
20849  a = (unsigned char *)zLeft;
20850  b = (unsigned char *)zRight;
20851  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
20852  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
20853}
20854
20855/*
20856** The string z[] is an text representation of a real number.
20857** Convert this string to a double and write it into *pResult.
20858**
20859** The string z[] is length bytes in length (bytes, not characters) and
20860** uses the encoding enc.  The string is not necessarily zero-terminated.
20861**
20862** Return TRUE if the result is a valid real number (or integer) and FALSE
20863** if the string is empty or contains extraneous text.  Valid numbers
20864** are in one of these formats:
20865**
20866**    [+-]digits[E[+-]digits]
20867**    [+-]digits.[digits][E[+-]digits]
20868**    [+-].digits[E[+-]digits]
20869**
20870** Leading and trailing whitespace is ignored for the purpose of determining
20871** validity.
20872**
20873** If some prefix of the input string is a valid number, this routine
20874** returns FALSE but it still converts the prefix and writes the result
20875** into *pResult.
20876*/
20877SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
20878#ifndef SQLITE_OMIT_FLOATING_POINT
20879  int incr = (enc==SQLITE_UTF8?1:2);
20880  const char *zEnd = z + length;
20881  /* sign * significand * (10 ^ (esign * exponent)) */
20882  int sign = 1;    /* sign of significand */
20883  i64 s = 0;       /* significand */
20884  int d = 0;       /* adjust exponent for shifting decimal point */
20885  int esign = 1;   /* sign of exponent */
20886  int e = 0;       /* exponent */
20887  int eValid = 1;  /* True exponent is either not used or is well-formed */
20888  double result;
20889  int nDigits = 0;
20890
20891  *pResult = 0.0;   /* Default return value, in case of an error */
20892
20893  if( enc==SQLITE_UTF16BE ) z++;
20894
20895  /* skip leading spaces */
20896  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20897  if( z>=zEnd ) return 0;
20898
20899  /* get sign of significand */
20900  if( *z=='-' ){
20901    sign = -1;
20902    z+=incr;
20903  }else if( *z=='+' ){
20904    z+=incr;
20905  }
20906
20907  /* skip leading zeroes */
20908  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
20909
20910  /* copy max significant digits to significand */
20911  while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20912    s = s*10 + (*z - '0');
20913    z+=incr, nDigits++;
20914  }
20915
20916  /* skip non-significant significand digits
20917  ** (increase exponent by d to shift decimal left) */
20918  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
20919  if( z>=zEnd ) goto do_atof_calc;
20920
20921  /* if decimal point is present */
20922  if( *z=='.' ){
20923    z+=incr;
20924    /* copy digits from after decimal to significand
20925    ** (decrease exponent by d to shift decimal right) */
20926    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
20927      s = s*10 + (*z - '0');
20928      z+=incr, nDigits++, d--;
20929    }
20930    /* skip non-significant digits */
20931    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
20932  }
20933  if( z>=zEnd ) goto do_atof_calc;
20934
20935  /* if exponent is present */
20936  if( *z=='e' || *z=='E' ){
20937    z+=incr;
20938    eValid = 0;
20939    if( z>=zEnd ) goto do_atof_calc;
20940    /* get sign of exponent */
20941    if( *z=='-' ){
20942      esign = -1;
20943      z+=incr;
20944    }else if( *z=='+' ){
20945      z+=incr;
20946    }
20947    /* copy digits to exponent */
20948    while( z<zEnd && sqlite3Isdigit(*z) ){
20949      e = e*10 + (*z - '0');
20950      z+=incr;
20951      eValid = 1;
20952    }
20953  }
20954
20955  /* skip trailing spaces */
20956  if( nDigits && eValid ){
20957    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
20958  }
20959
20960do_atof_calc:
20961  /* adjust exponent by d, and update sign */
20962  e = (e*esign) + d;
20963  if( e<0 ) {
20964    esign = -1;
20965    e *= -1;
20966  } else {
20967    esign = 1;
20968  }
20969
20970  /* if 0 significand */
20971  if( !s ) {
20972    /* In the IEEE 754 standard, zero is signed.
20973    ** Add the sign if we've seen at least one digit */
20974    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
20975  } else {
20976    /* attempt to reduce exponent */
20977    if( esign>0 ){
20978      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
20979    }else{
20980      while( !(s%10) && e>0 ) e--,s/=10;
20981    }
20982
20983    /* adjust the sign of significand */
20984    s = sign<0 ? -s : s;
20985
20986    /* if exponent, scale significand as appropriate
20987    ** and store in result. */
20988    if( e ){
20989      double scale = 1.0;
20990      /* attempt to handle extremely small/large numbers better */
20991      if( e>307 && e<342 ){
20992        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
20993        if( esign<0 ){
20994          result = s / scale;
20995          result /= 1.0e+308;
20996        }else{
20997          result = s * scale;
20998          result *= 1.0e+308;
20999        }
21000      }else{
21001        /* 1.0e+22 is the largest power of 10 than can be
21002        ** represented exactly. */
21003        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
21004        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
21005        if( esign<0 ){
21006          result = s / scale;
21007        }else{
21008          result = s * scale;
21009        }
21010      }
21011    } else {
21012      result = (double)s;
21013    }
21014  }
21015
21016  /* store the result */
21017  *pResult = result;
21018
21019  /* return true if number and no extra non-whitespace chracters after */
21020  return z>=zEnd && nDigits>0 && eValid;
21021#else
21022  return !sqlite3Atoi64(z, pResult, length, enc);
21023#endif /* SQLITE_OMIT_FLOATING_POINT */
21024}
21025
21026/*
21027** Compare the 19-character string zNum against the text representation
21028** value 2^63:  9223372036854775808.  Return negative, zero, or positive
21029** if zNum is less than, equal to, or greater than the string.
21030** Note that zNum must contain exactly 19 characters.
21031**
21032** Unlike memcmp() this routine is guaranteed to return the difference
21033** in the values of the last digit if the only difference is in the
21034** last digit.  So, for example,
21035**
21036**      compare2pow63("9223372036854775800", 1)
21037**
21038** will return -8.
21039*/
21040static int compare2pow63(const char *zNum, int incr){
21041  int c = 0;
21042  int i;
21043                    /* 012345678901234567 */
21044  const char *pow63 = "922337203685477580";
21045  for(i=0; c==0 && i<18; i++){
21046    c = (zNum[i*incr]-pow63[i])*10;
21047  }
21048  if( c==0 ){
21049    c = zNum[18*incr] - '8';
21050    testcase( c==(-1) );
21051    testcase( c==0 );
21052    testcase( c==(+1) );
21053  }
21054  return c;
21055}
21056
21057
21058/*
21059** Convert zNum to a 64-bit signed integer.
21060**
21061** If the zNum value is representable as a 64-bit twos-complement
21062** integer, then write that value into *pNum and return 0.
21063**
21064** If zNum is exactly 9223372036854665808, return 2.  This special
21065** case is broken out because while 9223372036854665808 cannot be a
21066** signed 64-bit integer, its negative -9223372036854665808 can be.
21067**
21068** If zNum is too big for a 64-bit integer and is not
21069** 9223372036854665808 then return 1.
21070**
21071** length is the number of bytes in the string (bytes, not characters).
21072** The string is not necessarily zero-terminated.  The encoding is
21073** given by enc.
21074*/
21075SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
21076  int incr = (enc==SQLITE_UTF8?1:2);
21077  u64 u = 0;
21078  int neg = 0; /* assume positive */
21079  int i;
21080  int c = 0;
21081  const char *zStart;
21082  const char *zEnd = zNum + length;
21083  if( enc==SQLITE_UTF16BE ) zNum++;
21084  while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
21085  if( zNum<zEnd ){
21086    if( *zNum=='-' ){
21087      neg = 1;
21088      zNum+=incr;
21089    }else if( *zNum=='+' ){
21090      zNum+=incr;
21091    }
21092  }
21093  zStart = zNum;
21094  while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
21095  for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
21096    u = u*10 + c - '0';
21097  }
21098  if( u>LARGEST_INT64 ){
21099    *pNum = SMALLEST_INT64;
21100  }else if( neg ){
21101    *pNum = -(i64)u;
21102  }else{
21103    *pNum = (i64)u;
21104  }
21105  testcase( i==18 );
21106  testcase( i==19 );
21107  testcase( i==20 );
21108  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
21109    /* zNum is empty or contains non-numeric text or is longer
21110    ** than 19 digits (thus guaranteeing that it is too large) */
21111    return 1;
21112  }else if( i<19*incr ){
21113    /* Less than 19 digits, so we know that it fits in 64 bits */
21114    assert( u<=LARGEST_INT64 );
21115    return 0;
21116  }else{
21117    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
21118    c = compare2pow63(zNum, incr);
21119    if( c<0 ){
21120      /* zNum is less than 9223372036854775808 so it fits */
21121      assert( u<=LARGEST_INT64 );
21122      return 0;
21123    }else if( c>0 ){
21124      /* zNum is greater than 9223372036854775808 so it overflows */
21125      return 1;
21126    }else{
21127      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
21128      ** special case 2 overflow if positive */
21129      assert( u-1==LARGEST_INT64 );
21130      assert( (*pNum)==SMALLEST_INT64 );
21131      return neg ? 0 : 2;
21132    }
21133  }
21134}
21135
21136/*
21137** If zNum represents an integer that will fit in 32-bits, then set
21138** *pValue to that integer and return true.  Otherwise return false.
21139**
21140** Any non-numeric characters that following zNum are ignored.
21141** This is different from sqlite3Atoi64() which requires the
21142** input number to be zero-terminated.
21143*/
21144SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
21145  sqlite_int64 v = 0;
21146  int i, c;
21147  int neg = 0;
21148  if( zNum[0]=='-' ){
21149    neg = 1;
21150    zNum++;
21151  }else if( zNum[0]=='+' ){
21152    zNum++;
21153  }
21154  while( zNum[0]=='0' ) zNum++;
21155  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
21156    v = v*10 + c;
21157  }
21158
21159  /* The longest decimal representation of a 32 bit integer is 10 digits:
21160  **
21161  **             1234567890
21162  **     2^31 -> 2147483648
21163  */
21164  testcase( i==10 );
21165  if( i>10 ){
21166    return 0;
21167  }
21168  testcase( v-neg==2147483647 );
21169  if( v-neg>2147483647 ){
21170    return 0;
21171  }
21172  if( neg ){
21173    v = -v;
21174  }
21175  *pValue = (int)v;
21176  return 1;
21177}
21178
21179/*
21180** Return a 32-bit integer value extracted from a string.  If the
21181** string is not an integer, just return 0.
21182*/
21183SQLITE_PRIVATE int sqlite3Atoi(const char *z){
21184  int x = 0;
21185  if( z ) sqlite3GetInt32(z, &x);
21186  return x;
21187}
21188
21189/*
21190** The variable-length integer encoding is as follows:
21191**
21192** KEY:
21193**         A = 0xxxxxxx    7 bits of data and one flag bit
21194**         B = 1xxxxxxx    7 bits of data and one flag bit
21195**         C = xxxxxxxx    8 bits of data
21196**
21197**  7 bits - A
21198** 14 bits - BA
21199** 21 bits - BBA
21200** 28 bits - BBBA
21201** 35 bits - BBBBA
21202** 42 bits - BBBBBA
21203** 49 bits - BBBBBBA
21204** 56 bits - BBBBBBBA
21205** 64 bits - BBBBBBBBC
21206*/
21207
21208/*
21209** Write a 64-bit variable-length integer to memory starting at p[0].
21210** The length of data write will be between 1 and 9 bytes.  The number
21211** of bytes written is returned.
21212**
21213** A variable-length integer consists of the lower 7 bits of each byte
21214** for all bytes that have the 8th bit set and one byte with the 8th
21215** bit clear.  Except, if we get to the 9th byte, it stores the full
21216** 8 bits and is the last byte.
21217*/
21218SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
21219  int i, j, n;
21220  u8 buf[10];
21221  if( v & (((u64)0xff000000)<<32) ){
21222    p[8] = (u8)v;
21223    v >>= 8;
21224    for(i=7; i>=0; i--){
21225      p[i] = (u8)((v & 0x7f) | 0x80);
21226      v >>= 7;
21227    }
21228    return 9;
21229  }
21230  n = 0;
21231  do{
21232    buf[n++] = (u8)((v & 0x7f) | 0x80);
21233    v >>= 7;
21234  }while( v!=0 );
21235  buf[0] &= 0x7f;
21236  assert( n<=9 );
21237  for(i=0, j=n-1; j>=0; j--, i++){
21238    p[i] = buf[j];
21239  }
21240  return n;
21241}
21242
21243/*
21244** This routine is a faster version of sqlite3PutVarint() that only
21245** works for 32-bit positive integers and which is optimized for
21246** the common case of small integers.  A MACRO version, putVarint32,
21247** is provided which inlines the single-byte case.  All code should use
21248** the MACRO version as this function assumes the single-byte case has
21249** already been handled.
21250*/
21251SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
21252#ifndef putVarint32
21253  if( (v & ~0x7f)==0 ){
21254    p[0] = v;
21255    return 1;
21256  }
21257#endif
21258  if( (v & ~0x3fff)==0 ){
21259    p[0] = (u8)((v>>7) | 0x80);
21260    p[1] = (u8)(v & 0x7f);
21261    return 2;
21262  }
21263  return sqlite3PutVarint(p, v);
21264}
21265
21266/*
21267** Bitmasks used by sqlite3GetVarint().  These precomputed constants
21268** are defined here rather than simply putting the constant expressions
21269** inline in order to work around bugs in the RVT compiler.
21270**
21271** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
21272**
21273** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
21274*/
21275#define SLOT_2_0     0x001fc07f
21276#define SLOT_4_2_0   0xf01fc07f
21277
21278
21279/*
21280** Read a 64-bit variable-length integer from memory starting at p[0].
21281** Return the number of bytes read.  The value is stored in *v.
21282*/
21283SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
21284  u32 a,b,s;
21285
21286  a = *p;
21287  /* a: p0 (unmasked) */
21288  if (!(a&0x80))
21289  {
21290    *v = a;
21291    return 1;
21292  }
21293
21294  p++;
21295  b = *p;
21296  /* b: p1 (unmasked) */
21297  if (!(b&0x80))
21298  {
21299    a &= 0x7f;
21300    a = a<<7;
21301    a |= b;
21302    *v = a;
21303    return 2;
21304  }
21305
21306  /* Verify that constants are precomputed correctly */
21307  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
21308  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
21309
21310  p++;
21311  a = a<<14;
21312  a |= *p;
21313  /* a: p0<<14 | p2 (unmasked) */
21314  if (!(a&0x80))
21315  {
21316    a &= SLOT_2_0;
21317    b &= 0x7f;
21318    b = b<<7;
21319    a |= b;
21320    *v = a;
21321    return 3;
21322  }
21323
21324  /* CSE1 from below */
21325  a &= SLOT_2_0;
21326  p++;
21327  b = b<<14;
21328  b |= *p;
21329  /* b: p1<<14 | p3 (unmasked) */
21330  if (!(b&0x80))
21331  {
21332    b &= SLOT_2_0;
21333    /* moved CSE1 up */
21334    /* a &= (0x7f<<14)|(0x7f); */
21335    a = a<<7;
21336    a |= b;
21337    *v = a;
21338    return 4;
21339  }
21340
21341  /* a: p0<<14 | p2 (masked) */
21342  /* b: p1<<14 | p3 (unmasked) */
21343  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21344  /* moved CSE1 up */
21345  /* a &= (0x7f<<14)|(0x7f); */
21346  b &= SLOT_2_0;
21347  s = a;
21348  /* s: p0<<14 | p2 (masked) */
21349
21350  p++;
21351  a = a<<14;
21352  a |= *p;
21353  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
21354  if (!(a&0x80))
21355  {
21356    /* we can skip these cause they were (effectively) done above in calc'ing s */
21357    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
21358    /* b &= (0x7f<<14)|(0x7f); */
21359    b = b<<7;
21360    a |= b;
21361    s = s>>18;
21362    *v = ((u64)s)<<32 | a;
21363    return 5;
21364  }
21365
21366  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21367  s = s<<7;
21368  s |= b;
21369  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
21370
21371  p++;
21372  b = b<<14;
21373  b |= *p;
21374  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
21375  if (!(b&0x80))
21376  {
21377    /* we can skip this cause it was (effectively) done above in calc'ing s */
21378    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
21379    a &= SLOT_2_0;
21380    a = a<<7;
21381    a |= b;
21382    s = s>>18;
21383    *v = ((u64)s)<<32 | a;
21384    return 6;
21385  }
21386
21387  p++;
21388  a = a<<14;
21389  a |= *p;
21390  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
21391  if (!(a&0x80))
21392  {
21393    a &= SLOT_4_2_0;
21394    b &= SLOT_2_0;
21395    b = b<<7;
21396    a |= b;
21397    s = s>>11;
21398    *v = ((u64)s)<<32 | a;
21399    return 7;
21400  }
21401
21402  /* CSE2 from below */
21403  a &= SLOT_2_0;
21404  p++;
21405  b = b<<14;
21406  b |= *p;
21407  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
21408  if (!(b&0x80))
21409  {
21410    b &= SLOT_4_2_0;
21411    /* moved CSE2 up */
21412    /* a &= (0x7f<<14)|(0x7f); */
21413    a = a<<7;
21414    a |= b;
21415    s = s>>4;
21416    *v = ((u64)s)<<32 | a;
21417    return 8;
21418  }
21419
21420  p++;
21421  a = a<<15;
21422  a |= *p;
21423  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
21424
21425  /* moved CSE2 up */
21426  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
21427  b &= SLOT_2_0;
21428  b = b<<8;
21429  a |= b;
21430
21431  s = s<<4;
21432  b = p[-4];
21433  b &= 0x7f;
21434  b = b>>3;
21435  s |= b;
21436
21437  *v = ((u64)s)<<32 | a;
21438
21439  return 9;
21440}
21441
21442/*
21443** Read a 32-bit variable-length integer from memory starting at p[0].
21444** Return the number of bytes read.  The value is stored in *v.
21445**
21446** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
21447** integer, then set *v to 0xffffffff.
21448**
21449** A MACRO version, getVarint32, is provided which inlines the
21450** single-byte case.  All code should use the MACRO version as
21451** this function assumes the single-byte case has already been handled.
21452*/
21453SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
21454  u32 a,b;
21455
21456  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
21457  ** by the getVarin32() macro */
21458  a = *p;
21459  /* a: p0 (unmasked) */
21460#ifndef getVarint32
21461  if (!(a&0x80))
21462  {
21463    /* Values between 0 and 127 */
21464    *v = a;
21465    return 1;
21466  }
21467#endif
21468
21469  /* The 2-byte case */
21470  p++;
21471  b = *p;
21472  /* b: p1 (unmasked) */
21473  if (!(b&0x80))
21474  {
21475    /* Values between 128 and 16383 */
21476    a &= 0x7f;
21477    a = a<<7;
21478    *v = a | b;
21479    return 2;
21480  }
21481
21482  /* The 3-byte case */
21483  p++;
21484  a = a<<14;
21485  a |= *p;
21486  /* a: p0<<14 | p2 (unmasked) */
21487  if (!(a&0x80))
21488  {
21489    /* Values between 16384 and 2097151 */
21490    a &= (0x7f<<14)|(0x7f);
21491    b &= 0x7f;
21492    b = b<<7;
21493    *v = a | b;
21494    return 3;
21495  }
21496
21497  /* A 32-bit varint is used to store size information in btrees.
21498  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
21499  ** A 3-byte varint is sufficient, for example, to record the size
21500  ** of a 1048569-byte BLOB or string.
21501  **
21502  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
21503  ** rare larger cases can be handled by the slower 64-bit varint
21504  ** routine.
21505  */
21506#if 1
21507  {
21508    u64 v64;
21509    u8 n;
21510
21511    p -= 2;
21512    n = sqlite3GetVarint(p, &v64);
21513    assert( n>3 && n<=9 );
21514    if( (v64 & SQLITE_MAX_U32)!=v64 ){
21515      *v = 0xffffffff;
21516    }else{
21517      *v = (u32)v64;
21518    }
21519    return n;
21520  }
21521
21522#else
21523  /* For following code (kept for historical record only) shows an
21524  ** unrolling for the 3- and 4-byte varint cases.  This code is
21525  ** slightly faster, but it is also larger and much harder to test.
21526  */
21527  p++;
21528  b = b<<14;
21529  b |= *p;
21530  /* b: p1<<14 | p3 (unmasked) */
21531  if (!(b&0x80))
21532  {
21533    /* Values between 2097152 and 268435455 */
21534    b &= (0x7f<<14)|(0x7f);
21535    a &= (0x7f<<14)|(0x7f);
21536    a = a<<7;
21537    *v = a | b;
21538    return 4;
21539  }
21540
21541  p++;
21542  a = a<<14;
21543  a |= *p;
21544  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
21545  if (!(a&0x80))
21546  {
21547    /* Values  between 268435456 and 34359738367 */
21548    a &= SLOT_4_2_0;
21549    b &= SLOT_4_2_0;
21550    b = b<<7;
21551    *v = a | b;
21552    return 5;
21553  }
21554
21555  /* We can only reach this point when reading a corrupt database
21556  ** file.  In that case we are not in any hurry.  Use the (relatively
21557  ** slow) general-purpose sqlite3GetVarint() routine to extract the
21558  ** value. */
21559  {
21560    u64 v64;
21561    u8 n;
21562
21563    p -= 4;
21564    n = sqlite3GetVarint(p, &v64);
21565    assert( n>5 && n<=9 );
21566    *v = (u32)v64;
21567    return n;
21568  }
21569#endif
21570}
21571
21572/*
21573** Return the number of bytes that will be needed to store the given
21574** 64-bit integer.
21575*/
21576SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
21577  int i = 0;
21578  do{
21579    i++;
21580    v >>= 7;
21581  }while( v!=0 && ALWAYS(i<9) );
21582  return i;
21583}
21584
21585
21586/*
21587** Read or write a four-byte big-endian integer value.
21588*/
21589SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
21590  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
21591}
21592SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
21593  p[0] = (u8)(v>>24);
21594  p[1] = (u8)(v>>16);
21595  p[2] = (u8)(v>>8);
21596  p[3] = (u8)v;
21597}
21598
21599
21600
21601/*
21602** Translate a single byte of Hex into an integer.
21603** This routine only works if h really is a valid hexadecimal
21604** character:  0..9a..fA..F
21605*/
21606SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
21607  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
21608#ifdef SQLITE_ASCII
21609  h += 9*(1&(h>>6));
21610#endif
21611#ifdef SQLITE_EBCDIC
21612  h += 9*(1&~(h>>4));
21613#endif
21614  return (u8)(h & 0xf);
21615}
21616
21617#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
21618/*
21619** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
21620** value.  Return a pointer to its binary value.  Space to hold the
21621** binary value has been obtained from malloc and must be freed by
21622** the calling routine.
21623*/
21624SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
21625  char *zBlob;
21626  int i;
21627
21628  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
21629  n--;
21630  if( zBlob ){
21631    for(i=0; i<n; i+=2){
21632      zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
21633    }
21634    zBlob[i/2] = 0;
21635  }
21636  return zBlob;
21637}
21638#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
21639
21640/*
21641** Log an error that is an API call on a connection pointer that should
21642** not have been used.  The "type" of connection pointer is given as the
21643** argument.  The zType is a word like "NULL" or "closed" or "invalid".
21644*/
21645static void logBadConnection(const char *zType){
21646  sqlite3_log(SQLITE_MISUSE,
21647     "API call with %s database connection pointer",
21648     zType
21649  );
21650}
21651
21652/*
21653** Check to make sure we have a valid db pointer.  This test is not
21654** foolproof but it does provide some measure of protection against
21655** misuse of the interface such as passing in db pointers that are
21656** NULL or which have been previously closed.  If this routine returns
21657** 1 it means that the db pointer is valid and 0 if it should not be
21658** dereferenced for any reason.  The calling function should invoke
21659** SQLITE_MISUSE immediately.
21660**
21661** sqlite3SafetyCheckOk() requires that the db pointer be valid for
21662** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
21663** open properly and is not fit for general use but which can be
21664** used as an argument to sqlite3_errmsg() or sqlite3_close().
21665*/
21666SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
21667  u32 magic;
21668  if( db==0 ){
21669    logBadConnection("NULL");
21670    return 0;
21671  }
21672  magic = db->magic;
21673  if( magic!=SQLITE_MAGIC_OPEN ){
21674    if( sqlite3SafetyCheckSickOrOk(db) ){
21675      testcase( sqlite3GlobalConfig.xLog!=0 );
21676      logBadConnection("unopened");
21677    }
21678    return 0;
21679  }else{
21680    return 1;
21681  }
21682}
21683SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
21684  u32 magic;
21685  magic = db->magic;
21686  if( magic!=SQLITE_MAGIC_SICK &&
21687      magic!=SQLITE_MAGIC_OPEN &&
21688      magic!=SQLITE_MAGIC_BUSY ){
21689    testcase( sqlite3GlobalConfig.xLog!=0 );
21690    logBadConnection("invalid");
21691    return 0;
21692  }else{
21693    return 1;
21694  }
21695}
21696
21697/*
21698** Attempt to add, substract, or multiply the 64-bit signed value iB against
21699** the other 64-bit signed integer at *pA and store the result in *pA.
21700** Return 0 on success.  Or if the operation would have resulted in an
21701** overflow, leave *pA unchanged and return 1.
21702*/
21703SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
21704  i64 iA = *pA;
21705  testcase( iA==0 ); testcase( iA==1 );
21706  testcase( iB==-1 ); testcase( iB==0 );
21707  if( iB>=0 ){
21708    testcase( iA>0 && LARGEST_INT64 - iA == iB );
21709    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
21710    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
21711    *pA += iB;
21712  }else{
21713    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
21714    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
21715    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
21716    *pA += iB;
21717  }
21718  return 0;
21719}
21720SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
21721  testcase( iB==SMALLEST_INT64+1 );
21722  if( iB==SMALLEST_INT64 ){
21723    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
21724    if( (*pA)>=0 ) return 1;
21725    *pA -= iB;
21726    return 0;
21727  }else{
21728    return sqlite3AddInt64(pA, -iB);
21729  }
21730}
21731#define TWOPOWER32 (((i64)1)<<32)
21732#define TWOPOWER31 (((i64)1)<<31)
21733SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
21734  i64 iA = *pA;
21735  i64 iA1, iA0, iB1, iB0, r;
21736
21737  iA1 = iA/TWOPOWER32;
21738  iA0 = iA % TWOPOWER32;
21739  iB1 = iB/TWOPOWER32;
21740  iB0 = iB % TWOPOWER32;
21741  if( iA1*iB1 != 0 ) return 1;
21742  assert( iA1*iB0==0 || iA0*iB1==0 );
21743  r = iA1*iB0 + iA0*iB1;
21744  testcase( r==(-TWOPOWER31)-1 );
21745  testcase( r==(-TWOPOWER31) );
21746  testcase( r==TWOPOWER31 );
21747  testcase( r==TWOPOWER31-1 );
21748  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
21749  r *= TWOPOWER32;
21750  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
21751  *pA = r;
21752  return 0;
21753}
21754
21755/*
21756** Compute the absolute value of a 32-bit signed integer, of possible.  Or
21757** if the integer has a value of -2147483648, return +2147483647
21758*/
21759SQLITE_PRIVATE int sqlite3AbsInt32(int x){
21760  if( x>=0 ) return x;
21761  if( x==(int)0x80000000 ) return 0x7fffffff;
21762  return -x;
21763}
21764
21765#ifdef SQLITE_ENABLE_8_3_NAMES
21766/*
21767** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
21768** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
21769** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
21770** three characters, then shorten the suffix on z[] to be the last three
21771** characters of the original suffix.
21772**
21773** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
21774** do the suffix shortening regardless of URI parameter.
21775**
21776** Examples:
21777**
21778**     test.db-journal    =>   test.nal
21779**     test.db-wal        =>   test.wal
21780**     test.db-shm        =>   test.shm
21781*/
21782SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
21783#if SQLITE_ENABLE_8_3_NAMES<2
21784  const char *zOk;
21785  zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21786  if( zOk && sqlite3GetBoolean(zOk) )
21787#endif
21788  {
21789    int i, sz;
21790    sz = sqlite3Strlen30(z);
21791    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21792    if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
21793  }
21794}
21795#endif
21796
21797/************** End of util.c ************************************************/
21798/************** Begin file hash.c ********************************************/
21799/*
21800** 2001 September 22
21801**
21802** The author disclaims copyright to this source code.  In place of
21803** a legal notice, here is a blessing:
21804**
21805**    May you do good and not evil.
21806**    May you find forgiveness for yourself and forgive others.
21807**    May you share freely, never taking more than you give.
21808**
21809*************************************************************************
21810** This is the implementation of generic hash-tables
21811** used in SQLite.
21812*/
21813/* #include <assert.h> */
21814
21815/* Turn bulk memory into a hash table object by initializing the
21816** fields of the Hash structure.
21817**
21818** "pNew" is a pointer to the hash table that is to be initialized.
21819*/
21820SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
21821  assert( pNew!=0 );
21822  pNew->first = 0;
21823  pNew->count = 0;
21824  pNew->htsize = 0;
21825  pNew->ht = 0;
21826}
21827
21828/* Remove all entries from a hash table.  Reclaim all memory.
21829** Call this routine to delete a hash table or to reset a hash table
21830** to the empty state.
21831*/
21832SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
21833  HashElem *elem;         /* For looping over all elements of the table */
21834
21835  assert( pH!=0 );
21836  elem = pH->first;
21837  pH->first = 0;
21838  sqlite3_free(pH->ht);
21839  pH->ht = 0;
21840  pH->htsize = 0;
21841  while( elem ){
21842    HashElem *next_elem = elem->next;
21843    sqlite3_free(elem);
21844    elem = next_elem;
21845  }
21846  pH->count = 0;
21847}
21848
21849/*
21850** The hashing function.
21851*/
21852static unsigned int strHash(const char *z, int nKey){
21853  int h = 0;
21854  assert( nKey>=0 );
21855  while( nKey > 0  ){
21856    h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
21857    nKey--;
21858  }
21859  return h;
21860}
21861
21862
21863/* Link pNew element into the hash table pH.  If pEntry!=0 then also
21864** insert pNew into the pEntry hash bucket.
21865*/
21866static void insertElement(
21867  Hash *pH,              /* The complete hash table */
21868  struct _ht *pEntry,    /* The entry into which pNew is inserted */
21869  HashElem *pNew         /* The element to be inserted */
21870){
21871  HashElem *pHead;       /* First element already in pEntry */
21872  if( pEntry ){
21873    pHead = pEntry->count ? pEntry->chain : 0;
21874    pEntry->count++;
21875    pEntry->chain = pNew;
21876  }else{
21877    pHead = 0;
21878  }
21879  if( pHead ){
21880    pNew->next = pHead;
21881    pNew->prev = pHead->prev;
21882    if( pHead->prev ){ pHead->prev->next = pNew; }
21883    else             { pH->first = pNew; }
21884    pHead->prev = pNew;
21885  }else{
21886    pNew->next = pH->first;
21887    if( pH->first ){ pH->first->prev = pNew; }
21888    pNew->prev = 0;
21889    pH->first = pNew;
21890  }
21891}
21892
21893
21894/* Resize the hash table so that it cantains "new_size" buckets.
21895**
21896** The hash table might fail to resize if sqlite3_malloc() fails or
21897** if the new size is the same as the prior size.
21898** Return TRUE if the resize occurs and false if not.
21899*/
21900static int rehash(Hash *pH, unsigned int new_size){
21901  struct _ht *new_ht;            /* The new hash table */
21902  HashElem *elem, *next_elem;    /* For looping over existing elements */
21903
21904#if SQLITE_MALLOC_SOFT_LIMIT>0
21905  if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
21906    new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
21907  }
21908  if( new_size==pH->htsize ) return 0;
21909#endif
21910
21911  /* The inability to allocates space for a larger hash table is
21912  ** a performance hit but it is not a fatal error.  So mark the
21913  ** allocation as a benign.
21914  */
21915  sqlite3BeginBenignMalloc();
21916  new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
21917  sqlite3EndBenignMalloc();
21918
21919  if( new_ht==0 ) return 0;
21920  sqlite3_free(pH->ht);
21921  pH->ht = new_ht;
21922  pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
21923  memset(new_ht, 0, new_size*sizeof(struct _ht));
21924  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
21925    unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
21926    next_elem = elem->next;
21927    insertElement(pH, &new_ht[h], elem);
21928  }
21929  return 1;
21930}
21931
21932/* This function (for internal use only) locates an element in an
21933** hash table that matches the given key.  The hash for this key has
21934** already been computed and is passed as the 4th parameter.
21935*/
21936static HashElem *findElementGivenHash(
21937  const Hash *pH,     /* The pH to be searched */
21938  const char *pKey,   /* The key we are searching for */
21939  int nKey,           /* Bytes in key (not counting zero terminator) */
21940  unsigned int h      /* The hash for this key. */
21941){
21942  HashElem *elem;                /* Used to loop thru the element list */
21943  int count;                     /* Number of elements left to test */
21944
21945  if( pH->ht ){
21946    struct _ht *pEntry = &pH->ht[h];
21947    elem = pEntry->chain;
21948    count = pEntry->count;
21949  }else{
21950    elem = pH->first;
21951    count = pH->count;
21952  }
21953  while( count-- && ALWAYS(elem) ){
21954    if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
21955      return elem;
21956    }
21957    elem = elem->next;
21958  }
21959  return 0;
21960}
21961
21962/* Remove a single entry from the hash table given a pointer to that
21963** element and a hash on the element's key.
21964*/
21965static void removeElementGivenHash(
21966  Hash *pH,         /* The pH containing "elem" */
21967  HashElem* elem,   /* The element to be removed from the pH */
21968  unsigned int h    /* Hash value for the element */
21969){
21970  struct _ht *pEntry;
21971  if( elem->prev ){
21972    elem->prev->next = elem->next;
21973  }else{
21974    pH->first = elem->next;
21975  }
21976  if( elem->next ){
21977    elem->next->prev = elem->prev;
21978  }
21979  if( pH->ht ){
21980    pEntry = &pH->ht[h];
21981    if( pEntry->chain==elem ){
21982      pEntry->chain = elem->next;
21983    }
21984    pEntry->count--;
21985    assert( pEntry->count>=0 );
21986  }
21987  sqlite3_free( elem );
21988  pH->count--;
21989  if( pH->count<=0 ){
21990    assert( pH->first==0 );
21991    assert( pH->count==0 );
21992    sqlite3HashClear(pH);
21993  }
21994}
21995
21996/* Attempt to locate an element of the hash table pH with a key
21997** that matches pKey,nKey.  Return the data for this element if it is
21998** found, or NULL if there is no match.
21999*/
22000SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
22001  HashElem *elem;    /* The element that matches key */
22002  unsigned int h;    /* A hash on key */
22003
22004  assert( pH!=0 );
22005  assert( pKey!=0 );
22006  assert( nKey>=0 );
22007  if( pH->ht ){
22008    h = strHash(pKey, nKey) % pH->htsize;
22009  }else{
22010    h = 0;
22011  }
22012  elem = findElementGivenHash(pH, pKey, nKey, h);
22013  return elem ? elem->data : 0;
22014}
22015
22016/* Insert an element into the hash table pH.  The key is pKey,nKey
22017** and the data is "data".
22018**
22019** If no element exists with a matching key, then a new
22020** element is created and NULL is returned.
22021**
22022** If another element already exists with the same key, then the
22023** new data replaces the old data and the old data is returned.
22024** The key is not copied in this instance.  If a malloc fails, then
22025** the new data is returned and the hash table is unchanged.
22026**
22027** If the "data" parameter to this function is NULL, then the
22028** element corresponding to "key" is removed from the hash table.
22029*/
22030SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
22031  unsigned int h;       /* the hash of the key modulo hash table size */
22032  HashElem *elem;       /* Used to loop thru the element list */
22033  HashElem *new_elem;   /* New element added to the pH */
22034
22035  assert( pH!=0 );
22036  assert( pKey!=0 );
22037  assert( nKey>=0 );
22038  if( pH->htsize ){
22039    h = strHash(pKey, nKey) % pH->htsize;
22040  }else{
22041    h = 0;
22042  }
22043  elem = findElementGivenHash(pH,pKey,nKey,h);
22044  if( elem ){
22045    void *old_data = elem->data;
22046    if( data==0 ){
22047      removeElementGivenHash(pH,elem,h);
22048    }else{
22049      elem->data = data;
22050      elem->pKey = pKey;
22051      assert(nKey==elem->nKey);
22052    }
22053    return old_data;
22054  }
22055  if( data==0 ) return 0;
22056  new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
22057  if( new_elem==0 ) return data;
22058  new_elem->pKey = pKey;
22059  new_elem->nKey = nKey;
22060  new_elem->data = data;
22061  pH->count++;
22062  if( pH->count>=10 && pH->count > 2*pH->htsize ){
22063    if( rehash(pH, pH->count*2) ){
22064      assert( pH->htsize>0 );
22065      h = strHash(pKey, nKey) % pH->htsize;
22066    }
22067  }
22068  if( pH->ht ){
22069    insertElement(pH, &pH->ht[h], new_elem);
22070  }else{
22071    insertElement(pH, 0, new_elem);
22072  }
22073  return 0;
22074}
22075
22076/************** End of hash.c ************************************************/
22077/************** Begin file opcodes.c *****************************************/
22078/* Automatically generated.  Do not edit */
22079/* See the mkopcodec.awk script for details. */
22080#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
22081SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
22082 static const char *const azName[] = { "?",
22083     /*   1 */ "Goto",
22084     /*   2 */ "Gosub",
22085     /*   3 */ "Return",
22086     /*   4 */ "Yield",
22087     /*   5 */ "HaltIfNull",
22088     /*   6 */ "Halt",
22089     /*   7 */ "Integer",
22090     /*   8 */ "Int64",
22091     /*   9 */ "String",
22092     /*  10 */ "Null",
22093     /*  11 */ "Blob",
22094     /*  12 */ "Variable",
22095     /*  13 */ "Move",
22096     /*  14 */ "Copy",
22097     /*  15 */ "SCopy",
22098     /*  16 */ "ResultRow",
22099     /*  17 */ "CollSeq",
22100     /*  18 */ "Function",
22101     /*  19 */ "Not",
22102     /*  20 */ "AddImm",
22103     /*  21 */ "MustBeInt",
22104     /*  22 */ "RealAffinity",
22105     /*  23 */ "Permutation",
22106     /*  24 */ "Compare",
22107     /*  25 */ "Jump",
22108     /*  26 */ "Once",
22109     /*  27 */ "If",
22110     /*  28 */ "IfNot",
22111     /*  29 */ "Column",
22112     /*  30 */ "Affinity",
22113     /*  31 */ "MakeRecord",
22114     /*  32 */ "Count",
22115     /*  33 */ "Savepoint",
22116     /*  34 */ "AutoCommit",
22117     /*  35 */ "Transaction",
22118     /*  36 */ "ReadCookie",
22119     /*  37 */ "SetCookie",
22120     /*  38 */ "VerifyCookie",
22121     /*  39 */ "OpenRead",
22122     /*  40 */ "OpenWrite",
22123     /*  41 */ "OpenAutoindex",
22124     /*  42 */ "OpenEphemeral",
22125     /*  43 */ "SorterOpen",
22126     /*  44 */ "OpenPseudo",
22127     /*  45 */ "Close",
22128     /*  46 */ "SeekLt",
22129     /*  47 */ "SeekLe",
22130     /*  48 */ "SeekGe",
22131     /*  49 */ "SeekGt",
22132     /*  50 */ "Seek",
22133     /*  51 */ "NotFound",
22134     /*  52 */ "Found",
22135     /*  53 */ "IsUnique",
22136     /*  54 */ "NotExists",
22137     /*  55 */ "Sequence",
22138     /*  56 */ "NewRowid",
22139     /*  57 */ "Insert",
22140     /*  58 */ "InsertInt",
22141     /*  59 */ "Delete",
22142     /*  60 */ "ResetCount",
22143     /*  61 */ "SorterCompare",
22144     /*  62 */ "SorterData",
22145     /*  63 */ "RowKey",
22146     /*  64 */ "RowData",
22147     /*  65 */ "Rowid",
22148     /*  66 */ "NullRow",
22149     /*  67 */ "Last",
22150     /*  68 */ "Or",
22151     /*  69 */ "And",
22152     /*  70 */ "SorterSort",
22153     /*  71 */ "Sort",
22154     /*  72 */ "Rewind",
22155     /*  73 */ "IsNull",
22156     /*  74 */ "NotNull",
22157     /*  75 */ "Ne",
22158     /*  76 */ "Eq",
22159     /*  77 */ "Gt",
22160     /*  78 */ "Le",
22161     /*  79 */ "Lt",
22162     /*  80 */ "Ge",
22163     /*  81 */ "SorterNext",
22164     /*  82 */ "BitAnd",
22165     /*  83 */ "BitOr",
22166     /*  84 */ "ShiftLeft",
22167     /*  85 */ "ShiftRight",
22168     /*  86 */ "Add",
22169     /*  87 */ "Subtract",
22170     /*  88 */ "Multiply",
22171     /*  89 */ "Divide",
22172     /*  90 */ "Remainder",
22173     /*  91 */ "Concat",
22174     /*  92 */ "Prev",
22175     /*  93 */ "BitNot",
22176     /*  94 */ "String8",
22177     /*  95 */ "Next",
22178     /*  96 */ "SorterInsert",
22179     /*  97 */ "IdxInsert",
22180     /*  98 */ "IdxDelete",
22181     /*  99 */ "IdxRowid",
22182     /* 100 */ "IdxLT",
22183     /* 101 */ "IdxGE",
22184     /* 102 */ "Destroy",
22185     /* 103 */ "Clear",
22186     /* 104 */ "CreateIndex",
22187     /* 105 */ "CreateTable",
22188     /* 106 */ "ParseSchema",
22189     /* 107 */ "LoadAnalysis",
22190     /* 108 */ "DropTable",
22191     /* 109 */ "DropIndex",
22192     /* 110 */ "DropTrigger",
22193     /* 111 */ "IntegrityCk",
22194     /* 112 */ "RowSetAdd",
22195     /* 113 */ "RowSetRead",
22196     /* 114 */ "RowSetTest",
22197     /* 115 */ "Program",
22198     /* 116 */ "Param",
22199     /* 117 */ "FkCounter",
22200     /* 118 */ "FkIfZero",
22201     /* 119 */ "MemMax",
22202     /* 120 */ "IfPos",
22203     /* 121 */ "IfNeg",
22204     /* 122 */ "IfZero",
22205     /* 123 */ "AggStep",
22206     /* 124 */ "AggFinal",
22207     /* 125 */ "Checkpoint",
22208     /* 126 */ "JournalMode",
22209     /* 127 */ "Vacuum",
22210     /* 128 */ "IncrVacuum",
22211     /* 129 */ "Expire",
22212     /* 130 */ "Real",
22213     /* 131 */ "TableLock",
22214     /* 132 */ "VBegin",
22215     /* 133 */ "VCreate",
22216     /* 134 */ "VDestroy",
22217     /* 135 */ "VOpen",
22218     /* 136 */ "VFilter",
22219     /* 137 */ "VColumn",
22220     /* 138 */ "VNext",
22221     /* 139 */ "VRename",
22222     /* 140 */ "VUpdate",
22223     /* 141 */ "ToText",
22224     /* 142 */ "ToBlob",
22225     /* 143 */ "ToNumeric",
22226     /* 144 */ "ToInt",
22227     /* 145 */ "ToReal",
22228     /* 146 */ "Pagecount",
22229     /* 147 */ "MaxPgcnt",
22230     /* 148 */ "Trace",
22231     /* 149 */ "Noop",
22232     /* 150 */ "Explain",
22233  };
22234  return azName[i];
22235}
22236#endif
22237
22238/************** End of opcodes.c *********************************************/
22239/************** Begin file os_os2.c ******************************************/
22240/*
22241** 2006 Feb 14
22242**
22243** The author disclaims copyright to this source code.  In place of
22244** a legal notice, here is a blessing:
22245**
22246**    May you do good and not evil.
22247**    May you find forgiveness for yourself and forgive others.
22248**    May you share freely, never taking more than you give.
22249**
22250******************************************************************************
22251**
22252** This file contains code that is specific to OS/2.
22253*/
22254
22255
22256#if SQLITE_OS_OS2
22257
22258/*
22259** A Note About Memory Allocation:
22260**
22261** This driver uses malloc()/free() directly rather than going through
22262** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
22263** are designed for use on embedded systems where memory is scarce and
22264** malloc failures happen frequently.  OS/2 does not typically run on
22265** embedded systems, and when it does the developers normally have bigger
22266** problems to worry about than running out of memory.  So there is not
22267** a compelling need to use the wrappers.
22268**
22269** But there is a good reason to not use the wrappers.  If we use the
22270** wrappers then we will get simulated malloc() failures within this
22271** driver.  And that causes all kinds of problems for our tests.  We
22272** could enhance SQLite to deal with simulated malloc failures within
22273** the OS driver, but the code to deal with those failure would not
22274** be exercised on Linux (which does not need to malloc() in the driver)
22275** and so we would have difficulty writing coverage tests for that
22276** code.  Better to leave the code out, we think.
22277**
22278** The point of this discussion is as follows:  When creating a new
22279** OS layer for an embedded system, if you use this file as an example,
22280** avoid the use of malloc()/free().  Those routines work ok on OS/2
22281** desktops but not so well in embedded systems.
22282*/
22283
22284/*
22285** Macros used to determine whether or not to use threads.
22286*/
22287#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
22288# define SQLITE_OS2_THREADS 1
22289#endif
22290
22291/*
22292** Include code that is common to all os_*.c files
22293*/
22294/************** Include os_common.h in the middle of os_os2.c ****************/
22295/************** Begin file os_common.h ***************************************/
22296/*
22297** 2004 May 22
22298**
22299** The author disclaims copyright to this source code.  In place of
22300** a legal notice, here is a blessing:
22301**
22302**    May you do good and not evil.
22303**    May you find forgiveness for yourself and forgive others.
22304**    May you share freely, never taking more than you give.
22305**
22306******************************************************************************
22307**
22308** This file contains macros and a little bit of code that is common to
22309** all of the platform-specific files (os_*.c) and is #included into those
22310** files.
22311**
22312** This file should be #included by the os_*.c files only.  It is not a
22313** general purpose header file.
22314*/
22315#ifndef _OS_COMMON_H_
22316#define _OS_COMMON_H_
22317
22318/*
22319** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22320** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22321** switch.  The following code should catch this problem at compile-time.
22322*/
22323#ifdef MEMORY_DEBUG
22324# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
22325#endif
22326
22327#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
22328# ifndef SQLITE_DEBUG_OS_TRACE
22329#   define SQLITE_DEBUG_OS_TRACE 0
22330# endif
22331  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22332# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
22333#else
22334# define OSTRACE(X)
22335#endif
22336
22337/*
22338** Macros for performance tracing.  Normally turned off.  Only works
22339** on i486 hardware.
22340*/
22341#ifdef SQLITE_PERFORMANCE_TRACE
22342
22343/*
22344** hwtime.h contains inline assembler code for implementing
22345** high-performance timing routines.
22346*/
22347/************** Include hwtime.h in the middle of os_common.h ****************/
22348/************** Begin file hwtime.h ******************************************/
22349/*
22350** 2008 May 27
22351**
22352** The author disclaims copyright to this source code.  In place of
22353** a legal notice, here is a blessing:
22354**
22355**    May you do good and not evil.
22356**    May you find forgiveness for yourself and forgive others.
22357**    May you share freely, never taking more than you give.
22358**
22359******************************************************************************
22360**
22361** This file contains inline asm code for retrieving "high-performance"
22362** counters for x86 class CPUs.
22363*/
22364#ifndef _HWTIME_H_
22365#define _HWTIME_H_
22366
22367/*
22368** The following routine only works on pentium-class (or newer) processors.
22369** It uses the RDTSC opcode to read the cycle count value out of the
22370** processor and returns that value.  This can be used for high-res
22371** profiling.
22372*/
22373#if (defined(__GNUC__) || defined(_MSC_VER)) && \
22374      (defined(i386) || defined(__i386__) || defined(_M_IX86))
22375
22376  #if defined(__GNUC__)
22377
22378  __inline__ sqlite_uint64 sqlite3Hwtime(void){
22379     unsigned int lo, hi;
22380     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
22381     return (sqlite_uint64)hi << 32 | lo;
22382  }
22383
22384  #elif defined(_MSC_VER)
22385
22386  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
22387     __asm {
22388        rdtsc
22389        ret       ; return value at EDX:EAX
22390     }
22391  }
22392
22393  #endif
22394
22395#elif (defined(__GNUC__) && defined(__x86_64__))
22396
22397  __inline__ sqlite_uint64 sqlite3Hwtime(void){
22398      unsigned long val;
22399      __asm__ __volatile__ ("rdtsc" : "=A" (val));
22400      return val;
22401  }
22402
22403#elif (defined(__GNUC__) && defined(__ppc__))
22404
22405  __inline__ sqlite_uint64 sqlite3Hwtime(void){
22406      unsigned long long retval;
22407      unsigned long junk;
22408      __asm__ __volatile__ ("\n\
22409          1:      mftbu   %1\n\
22410                  mftb    %L0\n\
22411                  mftbu   %0\n\
22412                  cmpw    %0,%1\n\
22413                  bne     1b"
22414                  : "=r" (retval), "=r" (junk));
22415      return retval;
22416  }
22417
22418#else
22419
22420  #error Need implementation of sqlite3Hwtime() for your platform.
22421
22422  /*
22423  ** To compile without implementing sqlite3Hwtime() for your platform,
22424  ** you can remove the above #error and use the following
22425  ** stub function.  You will lose timing support for many
22426  ** of the debugging and testing utilities, but it should at
22427  ** least compile and run.
22428  */
22429SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
22430
22431#endif
22432
22433#endif /* !defined(_HWTIME_H_) */
22434
22435/************** End of hwtime.h **********************************************/
22436/************** Continuing where we left off in os_common.h ******************/
22437
22438static sqlite_uint64 g_start;
22439static sqlite_uint64 g_elapsed;
22440#define TIMER_START       g_start=sqlite3Hwtime()
22441#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
22442#define TIMER_ELAPSED     g_elapsed
22443#else
22444#define TIMER_START
22445#define TIMER_END
22446#define TIMER_ELAPSED     ((sqlite_uint64)0)
22447#endif
22448
22449/*
22450** If we compile with the SQLITE_TEST macro set, then the following block
22451** of code will give us the ability to simulate a disk I/O error.  This
22452** is used for testing the I/O recovery logic.
22453*/
22454#ifdef SQLITE_TEST
22455SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
22456SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
22457SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
22458SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
22459SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
22460SQLITE_API int sqlite3_diskfull_pending = 0;
22461SQLITE_API int sqlite3_diskfull = 0;
22462#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
22463#define SimulateIOError(CODE)  \
22464  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
22465       || sqlite3_io_error_pending-- == 1 )  \
22466              { local_ioerr(); CODE; }
22467static void local_ioerr(){
22468  IOTRACE(("IOERR\n"));
22469  sqlite3_io_error_hit++;
22470  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
22471}
22472#define SimulateDiskfullError(CODE) \
22473   if( sqlite3_diskfull_pending ){ \
22474     if( sqlite3_diskfull_pending == 1 ){ \
22475       local_ioerr(); \
22476       sqlite3_diskfull = 1; \
22477       sqlite3_io_error_hit = 1; \
22478       CODE; \
22479     }else{ \
22480       sqlite3_diskfull_pending--; \
22481     } \
22482   }
22483#else
22484#define SimulateIOErrorBenign(X)
22485#define SimulateIOError(A)
22486#define SimulateDiskfullError(A)
22487#endif
22488
22489/*
22490** When testing, keep a count of the number of open files.
22491*/
22492#ifdef SQLITE_TEST
22493SQLITE_API int sqlite3_open_file_count = 0;
22494#define OpenCounter(X)  sqlite3_open_file_count+=(X)
22495#else
22496#define OpenCounter(X)
22497#endif
22498
22499#endif /* !defined(_OS_COMMON_H_) */
22500
22501/************** End of os_common.h *******************************************/
22502/************** Continuing where we left off in os_os2.c *********************/
22503
22504/* Forward references */
22505typedef struct os2File os2File;         /* The file structure */
22506typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
22507typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
22508
22509/*
22510** The os2File structure is subclass of sqlite3_file specific for the OS/2
22511** protability layer.
22512*/
22513struct os2File {
22514  const sqlite3_io_methods *pMethod;  /* Always the first entry */
22515  HFILE h;                  /* Handle for accessing the file */
22516  int flags;                /* Flags provided to os2Open() */
22517  int locktype;             /* Type of lock currently held on this file */
22518  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
22519  char *zFullPathCp;        /* Full path name of this file */
22520  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
22521};
22522
22523#define LOCK_TIMEOUT 10L /* the default locking timeout */
22524
22525/*
22526** Missing from some versions of the OS/2 toolkit -
22527** used to allocate from high memory if possible
22528*/
22529#ifndef OBJ_ANY
22530# define OBJ_ANY 0x00000400
22531#endif
22532
22533/*****************************************************************************
22534** The next group of routines implement the I/O methods specified
22535** by the sqlite3_io_methods object.
22536******************************************************************************/
22537
22538/*
22539** Close a file.
22540*/
22541static int os2Close( sqlite3_file *id ){
22542  APIRET rc;
22543  os2File *pFile = (os2File*)id;
22544
22545  assert( id!=0 );
22546  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
22547
22548  rc = DosClose( pFile->h );
22549
22550  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
22551    DosForceDelete( (PSZ)pFile->zFullPathCp );
22552
22553  free( pFile->zFullPathCp );
22554  pFile->zFullPathCp = NULL;
22555  pFile->locktype = NO_LOCK;
22556  pFile->h = (HFILE)-1;
22557  pFile->flags = 0;
22558
22559  OpenCounter( -1 );
22560  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22561}
22562
22563/*
22564** Read data from a file into a buffer.  Return SQLITE_OK if all
22565** bytes were read successfully and SQLITE_IOERR if anything goes
22566** wrong.
22567*/
22568static int os2Read(
22569  sqlite3_file *id,               /* File to read from */
22570  void *pBuf,                     /* Write content into this buffer */
22571  int amt,                        /* Number of bytes to read */
22572  sqlite3_int64 offset            /* Begin reading at this offset */
22573){
22574  ULONG fileLocation = 0L;
22575  ULONG got;
22576  os2File *pFile = (os2File*)id;
22577  assert( id!=0 );
22578  SimulateIOError( return SQLITE_IOERR_READ );
22579  OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
22580  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22581    return SQLITE_IOERR;
22582  }
22583  if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
22584    return SQLITE_IOERR_READ;
22585  }
22586  if( got == (ULONG)amt )
22587    return SQLITE_OK;
22588  else {
22589    /* Unread portions of the input buffer must be zero-filled */
22590    memset(&((char*)pBuf)[got], 0, amt-got);
22591    return SQLITE_IOERR_SHORT_READ;
22592  }
22593}
22594
22595/*
22596** Write data from a buffer into a file.  Return SQLITE_OK on success
22597** or some other error code on failure.
22598*/
22599static int os2Write(
22600  sqlite3_file *id,               /* File to write into */
22601  const void *pBuf,               /* The bytes to be written */
22602  int amt,                        /* Number of bytes to write */
22603  sqlite3_int64 offset            /* Offset into the file to begin writing at */
22604){
22605  ULONG fileLocation = 0L;
22606  APIRET rc = NO_ERROR;
22607  ULONG wrote;
22608  os2File *pFile = (os2File*)id;
22609  assert( id!=0 );
22610  SimulateIOError( return SQLITE_IOERR_WRITE );
22611  SimulateDiskfullError( return SQLITE_FULL );
22612  OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
22613  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
22614    return SQLITE_IOERR;
22615  }
22616  assert( amt>0 );
22617  while( amt > 0 &&
22618         ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
22619         wrote > 0
22620  ){
22621    amt -= wrote;
22622    pBuf = &((char*)pBuf)[wrote];
22623  }
22624
22625  return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
22626}
22627
22628/*
22629** Truncate an open file to a specified size
22630*/
22631static int os2Truncate( sqlite3_file *id, i64 nByte ){
22632  APIRET rc;
22633  os2File *pFile = (os2File*)id;
22634  assert( id!=0 );
22635  OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
22636  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
22637
22638  /* If the user has configured a chunk-size for this file, truncate the
22639  ** file so that it consists of an integer number of chunks (i.e. the
22640  ** actual file size after the operation may be larger than the requested
22641  ** size).
22642  */
22643  if( pFile->szChunk ){
22644    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
22645  }
22646
22647  rc = DosSetFileSize( pFile->h, nByte );
22648  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
22649}
22650
22651#ifdef SQLITE_TEST
22652/*
22653** Count the number of fullsyncs and normal syncs.  This is used to test
22654** that syncs and fullsyncs are occuring at the right times.
22655*/
22656SQLITE_API int sqlite3_sync_count = 0;
22657SQLITE_API int sqlite3_fullsync_count = 0;
22658#endif
22659
22660/*
22661** Make sure all writes to a particular file are committed to disk.
22662*/
22663static int os2Sync( sqlite3_file *id, int flags ){
22664  os2File *pFile = (os2File*)id;
22665  OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
22666#ifdef SQLITE_TEST
22667  if( flags & SQLITE_SYNC_FULL){
22668    sqlite3_fullsync_count++;
22669  }
22670  sqlite3_sync_count++;
22671#endif
22672  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
22673  ** no-op
22674  */
22675#ifdef SQLITE_NO_SYNC
22676  UNUSED_PARAMETER(pFile);
22677  return SQLITE_OK;
22678#else
22679  return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
22680#endif
22681}
22682
22683/*
22684** Determine the current size of a file in bytes
22685*/
22686static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
22687  APIRET rc = NO_ERROR;
22688  FILESTATUS3 fsts3FileInfo;
22689  memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
22690  assert( id!=0 );
22691  SimulateIOError( return SQLITE_IOERR_FSTAT );
22692  rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
22693  if( rc == NO_ERROR ){
22694    *pSize = fsts3FileInfo.cbFile;
22695    return SQLITE_OK;
22696  }else{
22697    return SQLITE_IOERR_FSTAT;
22698  }
22699}
22700
22701/*
22702** Acquire a reader lock.
22703*/
22704static int getReadLock( os2File *pFile ){
22705  FILELOCK  LockArea,
22706            UnlockArea;
22707  APIRET res;
22708  memset(&LockArea, 0, sizeof(LockArea));
22709  memset(&UnlockArea, 0, sizeof(UnlockArea));
22710  LockArea.lOffset = SHARED_FIRST;
22711  LockArea.lRange = SHARED_SIZE;
22712  UnlockArea.lOffset = 0L;
22713  UnlockArea.lRange = 0L;
22714  res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22715  OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
22716  return res;
22717}
22718
22719/*
22720** Undo a readlock
22721*/
22722static int unlockReadLock( os2File *id ){
22723  FILELOCK  LockArea,
22724            UnlockArea;
22725  APIRET res;
22726  memset(&LockArea, 0, sizeof(LockArea));
22727  memset(&UnlockArea, 0, sizeof(UnlockArea));
22728  LockArea.lOffset = 0L;
22729  LockArea.lRange = 0L;
22730  UnlockArea.lOffset = SHARED_FIRST;
22731  UnlockArea.lRange = SHARED_SIZE;
22732  res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
22733  OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
22734  return res;
22735}
22736
22737/*
22738** Lock the file with the lock specified by parameter locktype - one
22739** of the following:
22740**
22741**     (1) SHARED_LOCK
22742**     (2) RESERVED_LOCK
22743**     (3) PENDING_LOCK
22744**     (4) EXCLUSIVE_LOCK
22745**
22746** Sometimes when requesting one lock state, additional lock states
22747** are inserted in between.  The locking might fail on one of the later
22748** transitions leaving the lock state different from what it started but
22749** still short of its goal.  The following chart shows the allowed
22750** transitions and the inserted intermediate states:
22751**
22752**    UNLOCKED -> SHARED
22753**    SHARED -> RESERVED
22754**    SHARED -> (PENDING) -> EXCLUSIVE
22755**    RESERVED -> (PENDING) -> EXCLUSIVE
22756**    PENDING -> EXCLUSIVE
22757**
22758** This routine will only increase a lock.  The os2Unlock() routine
22759** erases all locks at once and returns us immediately to locking level 0.
22760** It is not possible to lower the locking level one step at a time.  You
22761** must go straight to locking level 0.
22762*/
22763static int os2Lock( sqlite3_file *id, int locktype ){
22764  int rc = SQLITE_OK;       /* Return code from subroutines */
22765  APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
22766  int newLocktype;       /* Set pFile->locktype to this value before exiting */
22767  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
22768  FILELOCK  LockArea,
22769            UnlockArea;
22770  os2File *pFile = (os2File*)id;
22771  memset(&LockArea, 0, sizeof(LockArea));
22772  memset(&UnlockArea, 0, sizeof(UnlockArea));
22773  assert( pFile!=0 );
22774  OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
22775
22776  /* If there is already a lock of this type or more restrictive on the
22777  ** os2File, do nothing. Don't use the end_lock: exit path, as
22778  ** sqlite3_mutex_enter() hasn't been called yet.
22779  */
22780  if( pFile->locktype>=locktype ){
22781    OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
22782    return SQLITE_OK;
22783  }
22784
22785  /* Make sure the locking sequence is correct
22786  */
22787  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
22788  assert( locktype!=PENDING_LOCK );
22789  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
22790
22791  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
22792  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
22793  ** the PENDING_LOCK byte is temporary.
22794  */
22795  newLocktype = pFile->locktype;
22796  if( pFile->locktype==NO_LOCK
22797      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
22798  ){
22799    LockArea.lOffset = PENDING_BYTE;
22800    LockArea.lRange = 1L;
22801    UnlockArea.lOffset = 0L;
22802    UnlockArea.lRange = 0L;
22803
22804    /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
22805    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
22806    if( res == NO_ERROR ){
22807      gotPendingLock = 1;
22808      OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
22809    }
22810  }
22811
22812  /* Acquire a shared lock
22813  */
22814  if( locktype==SHARED_LOCK && res == NO_ERROR ){
22815    assert( pFile->locktype==NO_LOCK );
22816    res = getReadLock(pFile);
22817    if( res == NO_ERROR ){
22818      newLocktype = SHARED_LOCK;
22819    }
22820    OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
22821  }
22822
22823  /* Acquire a RESERVED lock
22824  */
22825  if( locktype==RESERVED_LOCK && res == NO_ERROR ){
22826    assert( pFile->locktype==SHARED_LOCK );
22827    LockArea.lOffset = RESERVED_BYTE;
22828    LockArea.lRange = 1L;
22829    UnlockArea.lOffset = 0L;
22830    UnlockArea.lRange = 0L;
22831    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22832    if( res == NO_ERROR ){
22833      newLocktype = RESERVED_LOCK;
22834    }
22835    OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
22836  }
22837
22838  /* Acquire a PENDING lock
22839  */
22840  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22841    newLocktype = PENDING_LOCK;
22842    gotPendingLock = 0;
22843    OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
22844               pFile->h ));
22845  }
22846
22847  /* Acquire an EXCLUSIVE lock
22848  */
22849  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
22850    assert( pFile->locktype>=SHARED_LOCK );
22851    res = unlockReadLock(pFile);
22852    OSTRACE(( "unreadlock = %d\n", res ));
22853    LockArea.lOffset = SHARED_FIRST;
22854    LockArea.lRange = SHARED_SIZE;
22855    UnlockArea.lOffset = 0L;
22856    UnlockArea.lRange = 0L;
22857    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22858    if( res == NO_ERROR ){
22859      newLocktype = EXCLUSIVE_LOCK;
22860    }else{
22861      OSTRACE(( "OS/2 error-code = %d\n", res ));
22862      getReadLock(pFile);
22863    }
22864    OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
22865  }
22866
22867  /* If we are holding a PENDING lock that ought to be released, then
22868  ** release it now.
22869  */
22870  if( gotPendingLock && locktype==SHARED_LOCK ){
22871    int r;
22872    LockArea.lOffset = 0L;
22873    LockArea.lRange = 0L;
22874    UnlockArea.lOffset = PENDING_BYTE;
22875    UnlockArea.lRange = 1L;
22876    r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22877    OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
22878  }
22879
22880  /* Update the state of the lock has held in the file descriptor then
22881  ** return the appropriate result code.
22882  */
22883  if( res == NO_ERROR ){
22884    rc = SQLITE_OK;
22885  }else{
22886    OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
22887              locktype, newLocktype ));
22888    rc = SQLITE_BUSY;
22889  }
22890  pFile->locktype = newLocktype;
22891  OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
22892  return rc;
22893}
22894
22895/*
22896** This routine checks if there is a RESERVED lock held on the specified
22897** file by this or any other process. If such a lock is held, return
22898** non-zero, otherwise zero.
22899*/
22900static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
22901  int r = 0;
22902  os2File *pFile = (os2File*)id;
22903  assert( pFile!=0 );
22904  if( pFile->locktype>=RESERVED_LOCK ){
22905    r = 1;
22906    OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
22907  }else{
22908    FILELOCK  LockArea,
22909              UnlockArea;
22910    APIRET rc = NO_ERROR;
22911    memset(&LockArea, 0, sizeof(LockArea));
22912    memset(&UnlockArea, 0, sizeof(UnlockArea));
22913    LockArea.lOffset = RESERVED_BYTE;
22914    LockArea.lRange = 1L;
22915    UnlockArea.lOffset = 0L;
22916    UnlockArea.lRange = 0L;
22917    rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22918    OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
22919    if( rc == NO_ERROR ){
22920      APIRET rcu = NO_ERROR; /* return code for unlocking */
22921      LockArea.lOffset = 0L;
22922      LockArea.lRange = 0L;
22923      UnlockArea.lOffset = RESERVED_BYTE;
22924      UnlockArea.lRange = 1L;
22925      rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22926      OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
22927    }
22928    r = !(rc == NO_ERROR);
22929    OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
22930  }
22931  *pOut = r;
22932  return SQLITE_OK;
22933}
22934
22935/*
22936** Lower the locking level on file descriptor id to locktype.  locktype
22937** must be either NO_LOCK or SHARED_LOCK.
22938**
22939** If the locking level of the file descriptor is already at or below
22940** the requested locking level, this routine is a no-op.
22941**
22942** It is not possible for this routine to fail if the second argument
22943** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
22944** might return SQLITE_IOERR;
22945*/
22946static int os2Unlock( sqlite3_file *id, int locktype ){
22947  int type;
22948  os2File *pFile = (os2File*)id;
22949  APIRET rc = SQLITE_OK;
22950  APIRET res = NO_ERROR;
22951  FILELOCK  LockArea,
22952            UnlockArea;
22953  memset(&LockArea, 0, sizeof(LockArea));
22954  memset(&UnlockArea, 0, sizeof(UnlockArea));
22955  assert( pFile!=0 );
22956  assert( locktype<=SHARED_LOCK );
22957  OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
22958  type = pFile->locktype;
22959  if( type>=EXCLUSIVE_LOCK ){
22960    LockArea.lOffset = 0L;
22961    LockArea.lRange = 0L;
22962    UnlockArea.lOffset = SHARED_FIRST;
22963    UnlockArea.lRange = SHARED_SIZE;
22964    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22965    OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
22966    if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
22967      /* This should never happen.  We should always be able to
22968      ** reacquire the read lock */
22969      OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
22970      rc = SQLITE_IOERR_UNLOCK;
22971    }
22972  }
22973  if( type>=RESERVED_LOCK ){
22974    LockArea.lOffset = 0L;
22975    LockArea.lRange = 0L;
22976    UnlockArea.lOffset = RESERVED_BYTE;
22977    UnlockArea.lRange = 1L;
22978    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22979    OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
22980  }
22981  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
22982    res = unlockReadLock(pFile);
22983    OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
22984              pFile->h, type, locktype, res ));
22985  }
22986  if( type>=PENDING_LOCK ){
22987    LockArea.lOffset = 0L;
22988    LockArea.lRange = 0L;
22989    UnlockArea.lOffset = PENDING_BYTE;
22990    UnlockArea.lRange = 1L;
22991    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
22992    OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
22993  }
22994  pFile->locktype = locktype;
22995  OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
22996  return rc;
22997}
22998
22999/*
23000** Control and query of the open file handle.
23001*/
23002static int os2FileControl(sqlite3_file *id, int op, void *pArg){
23003  switch( op ){
23004    case SQLITE_FCNTL_LOCKSTATE: {
23005      *(int*)pArg = ((os2File*)id)->locktype;
23006      OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
23007                ((os2File*)id)->h, ((os2File*)id)->locktype ));
23008      return SQLITE_OK;
23009    }
23010    case SQLITE_FCNTL_CHUNK_SIZE: {
23011      ((os2File*)id)->szChunk = *(int*)pArg;
23012      return SQLITE_OK;
23013    }
23014    case SQLITE_FCNTL_SIZE_HINT: {
23015      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
23016      SimulateIOErrorBenign(1);
23017      os2Truncate(id, sz);
23018      SimulateIOErrorBenign(0);
23019      return SQLITE_OK;
23020    }
23021    case SQLITE_FCNTL_SYNC_OMITTED: {
23022      return SQLITE_OK;
23023    }
23024  }
23025  return SQLITE_NOTFOUND;
23026}
23027
23028/*
23029** Return the sector size in bytes of the underlying block device for
23030** the specified file. This is almost always 512 bytes, but may be
23031** larger for some devices.
23032**
23033** SQLite code assumes this function cannot fail. It also assumes that
23034** if two files are created in the same file-system directory (i.e.
23035** a database and its journal file) that the sector size will be the
23036** same for both.
23037*/
23038static int os2SectorSize(sqlite3_file *id){
23039  UNUSED_PARAMETER(id);
23040  return SQLITE_DEFAULT_SECTOR_SIZE;
23041}
23042
23043/*
23044** Return a vector of device characteristics.
23045*/
23046static int os2DeviceCharacteristics(sqlite3_file *id){
23047  UNUSED_PARAMETER(id);
23048  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
23049}
23050
23051
23052/*
23053** Character set conversion objects used by conversion routines.
23054*/
23055static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
23056static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
23057
23058/*
23059** Helper function to initialize the conversion objects from and to UTF-8.
23060*/
23061static void initUconvObjects( void ){
23062  if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
23063    ucUtf8 = NULL;
23064  if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
23065    uclCp = NULL;
23066}
23067
23068/*
23069** Helper function to free the conversion objects from and to UTF-8.
23070*/
23071static void freeUconvObjects( void ){
23072  if ( ucUtf8 )
23073    UniFreeUconvObject( ucUtf8 );
23074  if ( uclCp )
23075    UniFreeUconvObject( uclCp );
23076  ucUtf8 = NULL;
23077  uclCp = NULL;
23078}
23079
23080/*
23081** Helper function to convert UTF-8 filenames to local OS/2 codepage.
23082** The two-step process: first convert the incoming UTF-8 string
23083** into UCS-2 and then from UCS-2 to the current codepage.
23084** The returned char pointer has to be freed.
23085*/
23086static char *convertUtf8PathToCp( const char *in ){
23087  UniChar tempPath[CCHMAXPATH];
23088  char *out = (char *)calloc( CCHMAXPATH, 1 );
23089
23090  if( !out )
23091    return NULL;
23092
23093  if( !ucUtf8 || !uclCp )
23094    initUconvObjects();
23095
23096  /* determine string for the conversion of UTF-8 which is CP1208 */
23097  if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23098    return out; /* if conversion fails, return the empty string */
23099
23100  /* conversion for current codepage which can be used for paths */
23101  UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
23102
23103  return out;
23104}
23105
23106/*
23107** Helper function to convert filenames from local codepage to UTF-8.
23108** The two-step process: first convert the incoming codepage-specific
23109** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
23110** The returned char pointer has to be freed.
23111**
23112** This function is non-static to be able to use this in shell.c and
23113** similar applications that take command line arguments.
23114*/
23115char *convertCpPathToUtf8( const char *in ){
23116  UniChar tempPath[CCHMAXPATH];
23117  char *out = (char *)calloc( CCHMAXPATH, 1 );
23118
23119  if( !out )
23120    return NULL;
23121
23122  if( !ucUtf8 || !uclCp )
23123    initUconvObjects();
23124
23125  /* conversion for current codepage which can be used for paths */
23126  if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23127    return out; /* if conversion fails, return the empty string */
23128
23129  /* determine string for the conversion of UTF-8 which is CP1208 */
23130  UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
23131
23132  return out;
23133}
23134
23135
23136#ifndef SQLITE_OMIT_WAL
23137
23138/*
23139** Use main database file for interprocess locking. If un-defined
23140** a separate file is created for this purpose. The file will be
23141** used only to set file locks. There will be no data written to it.
23142*/
23143#define SQLITE_OS2_NO_WAL_LOCK_FILE
23144
23145#if 0
23146static void _ERR_TRACE( const char *fmt, ... ) {
23147  va_list  ap;
23148  va_start(ap, fmt);
23149  vfprintf(stderr, fmt, ap);
23150  fflush(stderr);
23151}
23152#define ERR_TRACE(rc, msg)        \
23153        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
23154#else
23155#define ERR_TRACE(rc, msg)
23156#endif
23157
23158/*
23159** Helper functions to obtain and relinquish the global mutex. The
23160** global mutex is used to protect os2ShmNodeList.
23161**
23162** Function os2ShmMutexHeld() is used to assert() that the global mutex
23163** is held when required. This function is only used as part of assert()
23164** statements. e.g.
23165**
23166**   os2ShmEnterMutex()
23167**     assert( os2ShmMutexHeld() );
23168**   os2ShmLeaveMutex()
23169*/
23170static void os2ShmEnterMutex(void){
23171  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23172}
23173static void os2ShmLeaveMutex(void){
23174  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23175}
23176#ifdef SQLITE_DEBUG
23177static int os2ShmMutexHeld(void) {
23178  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23179}
23180int GetCurrentProcessId(void) {
23181  PPIB pib;
23182  DosGetInfoBlocks(NULL, &pib);
23183  return (int)pib->pib_ulpid;
23184}
23185#endif
23186
23187/*
23188** Object used to represent a the shared memory area for a single log file.
23189** When multiple threads all reference the same log-summary, each thread has
23190** its own os2File object, but they all point to a single instance of this
23191** object.  In other words, each log-summary is opened only once per process.
23192**
23193** os2ShmMutexHeld() must be true when creating or destroying
23194** this object or while reading or writing the following fields:
23195**
23196**      nRef
23197**      pNext
23198**
23199** The following fields are read-only after the object is created:
23200**
23201**      szRegion
23202**      hLockFile
23203**      shmBaseName
23204**
23205** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
23206** os2ShmMutexHeld() is true when reading or writing any other field
23207** in this structure.
23208**
23209*/
23210struct os2ShmNode {
23211  sqlite3_mutex *mutex;      /* Mutex to access this object */
23212  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
23213
23214  int szRegion;              /* Size of shared-memory regions */
23215
23216  int nRegion;               /* Size of array apRegion */
23217  void **apRegion;           /* Array of pointers to shared-memory regions */
23218
23219  int nRef;                  /* Number of os2ShmLink objects pointing to this */
23220  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
23221
23222  HFILE hLockFile;           /* File used for inter-process memory locking */
23223  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
23224};
23225
23226
23227/*
23228** Structure used internally by this VFS to record the state of an
23229** open shared memory connection.
23230**
23231** The following fields are initialized when this object is created and
23232** are read-only thereafter:
23233**
23234**    os2Shm.pShmNode
23235**    os2Shm.id
23236**
23237** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
23238** while accessing any read/write fields.
23239*/
23240struct os2ShmLink {
23241  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
23242  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
23243  u32 sharedMask;            /* Mask of shared locks held */
23244  u32 exclMask;              /* Mask of exclusive locks held */
23245#ifdef SQLITE_DEBUG
23246  u8 id;                     /* Id of this connection with its os2ShmNode */
23247#endif
23248};
23249
23250
23251/*
23252** A global list of all os2ShmNode objects.
23253**
23254** The os2ShmMutexHeld() must be true while reading or writing this list.
23255*/
23256static os2ShmNode *os2ShmNodeList = NULL;
23257
23258/*
23259** Constants used for locking
23260*/
23261#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
23262#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
23263#else
23264#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
23265#endif
23266
23267#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
23268
23269/*
23270** Apply advisory locks for all n bytes beginning at ofst.
23271*/
23272#define _SHM_UNLCK  1   /* no lock */
23273#define _SHM_RDLCK  2   /* shared lock, no wait */
23274#define _SHM_WRLCK  3   /* exlusive lock, no wait */
23275#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
23276static int os2ShmSystemLock(
23277  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
23278  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
23279  int ofst,             /* Offset to first byte to be locked/unlocked */
23280  int nByte             /* Number of bytes to lock or unlock */
23281){
23282  APIRET rc;
23283  FILELOCK area;
23284  ULONG mode, timeout;
23285
23286  /* Access to the os2ShmNode object is serialized by the caller */
23287  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
23288
23289  mode = 1;     /* shared lock */
23290  timeout = 0;  /* no wait */
23291  area.lOffset = ofst;
23292  area.lRange = nByte;
23293
23294  switch( lockType ) {
23295    case _SHM_WRLCK_WAIT:
23296      timeout = (ULONG)-1;      /* wait forever */
23297    case _SHM_WRLCK:
23298      mode = 0;                 /* exclusive lock */
23299    case _SHM_RDLCK:
23300      rc = DosSetFileLocks(pNode->hLockFile,
23301                           NULL, &area, timeout, mode);
23302      break;
23303    /* case _SHM_UNLCK: */
23304    default:
23305      rc = DosSetFileLocks(pNode->hLockFile,
23306                           &area, NULL, 0, 0);
23307      break;
23308  }
23309
23310  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
23311           pNode->hLockFile,
23312           rc==SQLITE_OK ? "ok" : "failed",
23313           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
23314           rc));
23315
23316  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
23317
23318  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
23319}
23320
23321/*
23322** Find an os2ShmNode in global list or allocate a new one, if not found.
23323**
23324** This is not a VFS shared-memory method; it is a utility function called
23325** by VFS shared-memory methods.
23326*/
23327static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
23328  os2ShmLink *pLink;
23329  os2ShmNode *pNode;
23330  int cbShmName, rc = SQLITE_OK;
23331  char shmName[CCHMAXPATH + 30];
23332#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23333  ULONG action;
23334#endif
23335
23336  /* We need some additional space at the end to append the region number */
23337  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
23338  if( cbShmName >= CCHMAXPATH-8 )
23339    return SQLITE_IOERR_SHMOPEN;
23340
23341  /* Replace colon in file name to form a valid shared memory name */
23342  shmName[10+1] = '!';
23343
23344  /* Allocate link object (we free it later in case of failure) */
23345  pLink = sqlite3_malloc( sizeof(*pLink) );
23346  if( !pLink )
23347    return SQLITE_NOMEM;
23348
23349  /* Access node list */
23350  os2ShmEnterMutex();
23351
23352  /* Find node by it's shared memory base name */
23353  for( pNode = os2ShmNodeList;
23354       pNode && stricmp(shmName, pNode->shmBaseName) != 0;
23355       pNode = pNode->pNext )   ;
23356
23357  /* Not found: allocate a new node */
23358  if( !pNode ) {
23359    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
23360    if( pNode ) {
23361      memset(pNode, 0, sizeof(*pNode) );
23362      pNode->szRegion = szRegion;
23363      pNode->hLockFile = (HFILE)-1;
23364      strcpy(pNode->shmBaseName, shmName);
23365
23366#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23367      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
23368#else
23369      sprintf(shmName, "%s-lck", fd->zFullPathCp);
23370      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
23371                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
23372                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
23373                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
23374                  NULL) != 0 ) {
23375#endif
23376        sqlite3_free(pNode);
23377        rc = SQLITE_IOERR;
23378      } else {
23379        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
23380        if( !pNode->mutex ) {
23381          sqlite3_free(pNode);
23382          rc = SQLITE_NOMEM;
23383        }
23384      }
23385    } else {
23386      rc = SQLITE_NOMEM;
23387    }
23388
23389    if( rc == SQLITE_OK ) {
23390      pNode->pNext = os2ShmNodeList;
23391      os2ShmNodeList = pNode;
23392    } else {
23393      pNode = NULL;
23394    }
23395  } else if( pNode->szRegion != szRegion ) {
23396    rc = SQLITE_IOERR_SHMSIZE;
23397    pNode = NULL;
23398  }
23399
23400  if( pNode ) {
23401    sqlite3_mutex_enter(pNode->mutex);
23402
23403    memset(pLink, 0, sizeof(*pLink));
23404
23405    pLink->pShmNode = pNode;
23406    pLink->pNext = pNode->pFirst;
23407    pNode->pFirst = pLink;
23408    pNode->nRef++;
23409
23410    fd->pShmLink = pLink;
23411
23412    sqlite3_mutex_leave(pNode->mutex);
23413
23414  } else {
23415    /* Error occured. Free our link object. */
23416    sqlite3_free(pLink);
23417  }
23418
23419  os2ShmLeaveMutex();
23420
23421  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))
23422
23423  return rc;
23424}
23425
23426/*
23427** Purge the os2ShmNodeList list of all entries with nRef==0.
23428**
23429** This is not a VFS shared-memory method; it is a utility function called
23430** by VFS shared-memory methods.
23431*/
23432static void os2PurgeShmNodes( int deleteFlag ) {
23433  os2ShmNode *pNode;
23434  os2ShmNode **ppNode;
23435
23436  os2ShmEnterMutex();
23437
23438  ppNode = &os2ShmNodeList;
23439
23440  while( *ppNode ) {
23441    pNode = *ppNode;
23442
23443    if( pNode->nRef == 0 ) {
23444      *ppNode = pNode->pNext;
23445
23446      if( pNode->apRegion ) {
23447        /* Prevent other processes from resizing the shared memory */
23448        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23449
23450        while( pNode->nRegion-- ) {
23451#ifdef SQLITE_DEBUG
23452          int rc =
23453#endif
23454          DosFreeMem(pNode->apRegion[pNode->nRegion]);
23455
23456          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
23457                  (int)GetCurrentProcessId(), pNode->nRegion,
23458                  rc == 0 ? "ok" : "failed"));
23459        }
23460
23461        /* Allow other processes to resize the shared memory */
23462        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23463
23464        sqlite3_free(pNode->apRegion);
23465      }
23466
23467      DosClose(pNode->hLockFile);
23468
23469#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23470      if( deleteFlag ) {
23471         char fileName[CCHMAXPATH];
23472         /* Skip "\\SHAREMEM\\" */
23473         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
23474         /* restore colon */
23475         fileName[1] = ':';
23476
23477         DosForceDelete(fileName);
23478      }
23479#endif
23480
23481      sqlite3_mutex_free(pNode->mutex);
23482
23483      sqlite3_free(pNode);
23484
23485    } else {
23486      ppNode = &pNode->pNext;
23487    }
23488  }
23489
23490  os2ShmLeaveMutex();
23491}
23492
23493/*
23494** This function is called to obtain a pointer to region iRegion of the
23495** shared-memory associated with the database file id. Shared-memory regions
23496** are numbered starting from zero. Each shared-memory region is szRegion
23497** bytes in size.
23498**
23499** If an error occurs, an error code is returned and *pp is set to NULL.
23500**
23501** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
23502** region has not been allocated (by any client, including one running in a
23503** separate process), then *pp is set to NULL and SQLITE_OK returned. If
23504** bExtend is non-zero and the requested shared-memory region has not yet
23505** been allocated, it is allocated by this function.
23506**
23507** If the shared-memory region has already been allocated or is allocated by
23508** this call as described above, then it is mapped into this processes
23509** address space (if it is not already), *pp is set to point to the mapped
23510** memory and SQLITE_OK returned.
23511*/
23512static int os2ShmMap(
23513  sqlite3_file *id,               /* Handle open on database file */
23514  int iRegion,                    /* Region to retrieve */
23515  int szRegion,                   /* Size of regions */
23516  int bExtend,                    /* True to extend block if necessary */
23517  void volatile **pp              /* OUT: Mapped memory */
23518){
23519  PVOID pvTemp;
23520  void **apRegion;
23521  os2ShmNode *pNode;
23522  int n, rc = SQLITE_OK;
23523  char shmName[CCHMAXPATH];
23524  os2File *pFile = (os2File*)id;
23525
23526  *pp = NULL;
23527
23528  if( !pFile->pShmLink )
23529    rc = os2OpenSharedMemory( pFile, szRegion );
23530
23531  if( rc == SQLITE_OK ) {
23532    pNode = pFile->pShmLink->pShmNode ;
23533
23534    sqlite3_mutex_enter(pNode->mutex);
23535
23536    assert( szRegion==pNode->szRegion );
23537
23538    /* Unmapped region ? */
23539    if( iRegion >= pNode->nRegion ) {
23540      /* Prevent other processes from resizing the shared memory */
23541      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23542
23543      apRegion = sqlite3_realloc(
23544        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
23545
23546      if( apRegion ) {
23547        pNode->apRegion = apRegion;
23548
23549        while( pNode->nRegion <= iRegion ) {
23550          sprintf(shmName, "%s-%u",
23551                  pNode->shmBaseName, pNode->nRegion);
23552
23553          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
23554                PAG_READ | PAG_WRITE) != NO_ERROR ) {
23555            if( !bExtend )
23556              break;
23557
23558            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23559                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
23560                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
23561                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
23562              rc = SQLITE_NOMEM;
23563              break;
23564            }
23565          }
23566
23567          apRegion[pNode->nRegion++] = pvTemp;
23568        }
23569
23570        /* zero out remaining entries */
23571        for( n = pNode->nRegion; n <= iRegion; n++ )
23572          pNode->apRegion[n] = NULL;
23573
23574        /* Return this region (maybe zero) */
23575        *pp = pNode->apRegion[iRegion];
23576      } else {
23577        rc = SQLITE_NOMEM;
23578      }
23579
23580      /* Allow other processes to resize the shared memory */
23581      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
23582
23583    } else {
23584      /* Region has been mapped previously */
23585      *pp = pNode->apRegion[iRegion];
23586    }
23587
23588    sqlite3_mutex_leave(pNode->mutex);
23589  }
23590
23591  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
23592                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
23593
23594  return rc;
23595}
23596
23597/*
23598** Close a connection to shared-memory.  Delete the underlying
23599** storage if deleteFlag is true.
23600**
23601** If there is no shared memory associated with the connection then this
23602** routine is a harmless no-op.
23603*/
23604static int os2ShmUnmap(
23605  sqlite3_file *id,               /* The underlying database file */
23606  int deleteFlag                  /* Delete shared-memory if true */
23607){
23608  os2File *pFile = (os2File*)id;
23609  os2ShmLink *pLink = pFile->pShmLink;
23610
23611  if( pLink ) {
23612    int nRef = -1;
23613    os2ShmLink **ppLink;
23614    os2ShmNode *pNode = pLink->pShmNode;
23615
23616    sqlite3_mutex_enter(pNode->mutex);
23617
23618    for( ppLink = &pNode->pFirst;
23619         *ppLink && *ppLink != pLink;
23620         ppLink = &(*ppLink)->pNext )   ;
23621
23622    assert(*ppLink);
23623
23624    if( *ppLink ) {
23625      *ppLink = pLink->pNext;
23626      nRef = --pNode->nRef;
23627    } else {
23628      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
23629                    pNode->shmBaseName))
23630    }
23631
23632    pFile->pShmLink = NULL;
23633    sqlite3_free(pLink);
23634
23635    sqlite3_mutex_leave(pNode->mutex);
23636
23637    if( nRef == 0 )
23638      os2PurgeShmNodes( deleteFlag );
23639  }
23640
23641  return SQLITE_OK;
23642}
23643
23644/*
23645** Change the lock state for a shared-memory segment.
23646**
23647** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
23648** different here than in posix.  In xShmLock(), one can go from unlocked
23649** to shared and back or from unlocked to exclusive and back.  But one may
23650** not go from shared to exclusive or from exclusive to shared.
23651*/
23652static int os2ShmLock(
23653  sqlite3_file *id,          /* Database file holding the shared memory */
23654  int ofst,                  /* First lock to acquire or release */
23655  int n,                     /* Number of locks to acquire or release */
23656  int flags                  /* What to do with the lock */
23657){
23658  u32 mask;                             /* Mask of locks to take or release */
23659  int rc = SQLITE_OK;                   /* Result code */
23660  os2File *pFile = (os2File*)id;
23661  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
23662  os2ShmLink *pX;                       /* For looping over all siblings */
23663  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
23664
23665  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
23666  assert( n>=1 );
23667  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
23668       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
23669       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
23670       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
23671  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
23672
23673  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
23674  assert( n>1 || mask==(1<<ofst) );
23675
23676
23677  sqlite3_mutex_enter(pShmNode->mutex);
23678
23679  if( flags & SQLITE_SHM_UNLOCK ){
23680    u32 allMask = 0; /* Mask of locks held by siblings */
23681
23682    /* See if any siblings hold this same lock */
23683    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23684      if( pX==p ) continue;
23685      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
23686      allMask |= pX->sharedMask;
23687    }
23688
23689    /* Unlock the system-level locks */
23690    if( (mask & allMask)==0 ){
23691      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
23692    }else{
23693      rc = SQLITE_OK;
23694    }
23695
23696    /* Undo the local locks */
23697    if( rc==SQLITE_OK ){
23698      p->exclMask &= ~mask;
23699      p->sharedMask &= ~mask;
23700    }
23701  }else if( flags & SQLITE_SHM_SHARED ){
23702    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
23703
23704    /* Find out which shared locks are already held by sibling connections.
23705    ** If any sibling already holds an exclusive lock, go ahead and return
23706    ** SQLITE_BUSY.
23707    */
23708    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23709      if( (pX->exclMask & mask)!=0 ){
23710        rc = SQLITE_BUSY;
23711        break;
23712      }
23713      allShared |= pX->sharedMask;
23714    }
23715
23716    /* Get shared locks at the system level, if necessary */
23717    if( rc==SQLITE_OK ){
23718      if( (allShared & mask)==0 ){
23719        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
23720      }else{
23721        rc = SQLITE_OK;
23722      }
23723    }
23724
23725    /* Get the local shared locks */
23726    if( rc==SQLITE_OK ){
23727      p->sharedMask |= mask;
23728    }
23729  }else{
23730    /* Make sure no sibling connections hold locks that will block this
23731    ** lock.  If any do, return SQLITE_BUSY right away.
23732    */
23733    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
23734      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
23735        rc = SQLITE_BUSY;
23736        break;
23737      }
23738    }
23739
23740    /* Get the exclusive locks at the system level.  Then if successful
23741    ** also mark the local connection as being locked.
23742    */
23743    if( rc==SQLITE_OK ){
23744      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
23745      if( rc==SQLITE_OK ){
23746        assert( (p->sharedMask & mask)==0 );
23747        p->exclMask |= mask;
23748      }
23749    }
23750  }
23751
23752  sqlite3_mutex_leave(pShmNode->mutex);
23753
23754  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
23755           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
23756           rc ? "failed" : "ok"));
23757
23758  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
23759                 ofst, n, flags, rc))
23760
23761  return rc;
23762}
23763
23764/*
23765** Implement a memory barrier or memory fence on shared memory.
23766**
23767** All loads and stores begun before the barrier must complete before
23768** any load or store begun after the barrier.
23769*/
23770static void os2ShmBarrier(
23771  sqlite3_file *id                /* Database file holding the shared memory */
23772){
23773  UNUSED_PARAMETER(id);
23774  os2ShmEnterMutex();
23775  os2ShmLeaveMutex();
23776}
23777
23778#else
23779# define os2ShmMap     0
23780# define os2ShmLock    0
23781# define os2ShmBarrier 0
23782# define os2ShmUnmap   0
23783#endif /* #ifndef SQLITE_OMIT_WAL */
23784
23785
23786/*
23787** This vector defines all the methods that can operate on an
23788** sqlite3_file for os2.
23789*/
23790static const sqlite3_io_methods os2IoMethod = {
23791  2,                              /* iVersion */
23792  os2Close,                       /* xClose */
23793  os2Read,                        /* xRead */
23794  os2Write,                       /* xWrite */
23795  os2Truncate,                    /* xTruncate */
23796  os2Sync,                        /* xSync */
23797  os2FileSize,                    /* xFileSize */
23798  os2Lock,                        /* xLock */
23799  os2Unlock,                      /* xUnlock */
23800  os2CheckReservedLock,           /* xCheckReservedLock */
23801  os2FileControl,                 /* xFileControl */
23802  os2SectorSize,                  /* xSectorSize */
23803  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
23804  os2ShmMap,                      /* xShmMap */
23805  os2ShmLock,                     /* xShmLock */
23806  os2ShmBarrier,                  /* xShmBarrier */
23807  os2ShmUnmap                     /* xShmUnmap */
23808};
23809
23810
23811/***************************************************************************
23812** Here ends the I/O methods that form the sqlite3_io_methods object.
23813**
23814** The next block of code implements the VFS methods.
23815****************************************************************************/
23816
23817/*
23818** Create a temporary file name in zBuf.  zBuf must be big enough to
23819** hold at pVfs->mxPathname characters.
23820*/
23821static int getTempname(int nBuf, char *zBuf ){
23822  static const char zChars[] =
23823    "abcdefghijklmnopqrstuvwxyz"
23824    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23825    "0123456789";
23826  int i, j;
23827  PSZ zTempPathCp;
23828  char zTempPath[CCHMAXPATH];
23829  ULONG ulDriveNum, ulDriveMap;
23830
23831  /* It's odd to simulate an io-error here, but really this is just
23832  ** using the io-error infrastructure to test that SQLite handles this
23833  ** function failing.
23834  */
23835  SimulateIOError( return SQLITE_IOERR );
23836
23837  if( sqlite3_temp_directory ) {
23838    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
23839  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
23840             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
23841             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
23842    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
23843    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
23844    free( zTempPathUTF );
23845  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
23846    zTempPath[0] = (char)('A' + ulDriveNum - 1);
23847    zTempPath[1] = ':';
23848    zTempPath[2] = '\0';
23849  } else {
23850    zTempPath[0] = '\0';
23851  }
23852
23853  /* Strip off a trailing slashes or backslashes, otherwise we would get *
23854   * multiple (back)slashes which causes DosOpen() to fail.              *
23855   * Trailing spaces are not allowed, either.                            */
23856  j = sqlite3Strlen30(zTempPath);
23857  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
23858                    zTempPath[j-1] == ' ' ) ){
23859    j--;
23860  }
23861  zTempPath[j] = '\0';
23862
23863  /* We use 20 bytes to randomize the name */
23864  sqlite3_snprintf(nBuf-22, zBuf,
23865                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
23866  j = sqlite3Strlen30(zBuf);
23867  sqlite3_randomness( 20, &zBuf[j] );
23868  for( i = 0; i < 20; i++, j++ ){
23869    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
23870  }
23871  zBuf[j] = 0;
23872
23873  OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
23874  return SQLITE_OK;
23875}
23876
23877
23878/*
23879** Turn a relative pathname into a full pathname.  Write the full
23880** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
23881** bytes in size.
23882*/
23883static int os2FullPathname(
23884  sqlite3_vfs *pVfs,          /* Pointer to vfs object */
23885  const char *zRelative,      /* Possibly relative input path */
23886  int nFull,                  /* Size of output buffer in bytes */
23887  char *zFull                 /* Output buffer */
23888){
23889  char *zRelativeCp = convertUtf8PathToCp( zRelative );
23890  char zFullCp[CCHMAXPATH] = "\0";
23891  char *zFullUTF;
23892  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
23893                                zFullCp, CCHMAXPATH );
23894  free( zRelativeCp );
23895  zFullUTF = convertCpPathToUtf8( zFullCp );
23896  sqlite3_snprintf( nFull, zFull, zFullUTF );
23897  free( zFullUTF );
23898  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23899}
23900
23901
23902/*
23903** Open a file.
23904*/
23905static int os2Open(
23906  sqlite3_vfs *pVfs,            /* Not used */
23907  const char *zName,            /* Name of the file (UTF-8) */
23908  sqlite3_file *id,             /* Write the SQLite file handle here */
23909  int flags,                    /* Open mode flags */
23910  int *pOutFlags                /* Status return flags */
23911){
23912  HFILE h;
23913  ULONG ulOpenFlags = 0;
23914  ULONG ulOpenMode = 0;
23915  ULONG ulAction = 0;
23916  ULONG rc;
23917  os2File *pFile = (os2File*)id;
23918  const char *zUtf8Name = zName;
23919  char *zNameCp;
23920  char  zTmpname[CCHMAXPATH];
23921
23922  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
23923  int isCreate     = (flags & SQLITE_OPEN_CREATE);
23924  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
23925#ifndef NDEBUG
23926  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
23927  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
23928  int eType        = (flags & 0xFFFFFF00);
23929  int isOpenJournal = (isCreate && (
23930        eType==SQLITE_OPEN_MASTER_JOURNAL
23931     || eType==SQLITE_OPEN_MAIN_JOURNAL
23932     || eType==SQLITE_OPEN_WAL
23933  ));
23934#endif
23935
23936  UNUSED_PARAMETER(pVfs);
23937  assert( id!=0 );
23938
23939  /* Check the following statements are true:
23940  **
23941  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
23942  **   (b) if CREATE is set, then READWRITE must also be set, and
23943  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
23944  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
23945  */
23946  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
23947  assert(isCreate==0 || isReadWrite);
23948  assert(isExclusive==0 || isCreate);
23949  assert(isDelete==0 || isCreate);
23950
23951  /* The main DB, main journal, WAL file and master journal are never
23952  ** automatically deleted. Nor are they ever temporary files.  */
23953  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
23954  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
23955  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
23956  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
23957
23958  /* Assert that the upper layer has set one of the "file-type" flags. */
23959  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
23960       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
23961       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
23962       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
23963  );
23964
23965  memset( pFile, 0, sizeof(*pFile) );
23966  pFile->h = (HFILE)-1;
23967
23968  /* If the second argument to this function is NULL, generate a
23969  ** temporary file name to use
23970  */
23971  if( !zUtf8Name ){
23972    assert(isDelete && !isOpenJournal);
23973    rc = getTempname(CCHMAXPATH, zTmpname);
23974    if( rc!=SQLITE_OK ){
23975      return rc;
23976    }
23977    zUtf8Name = zTmpname;
23978  }
23979
23980  if( isReadWrite ){
23981    ulOpenMode |= OPEN_ACCESS_READWRITE;
23982  }else{
23983    ulOpenMode |= OPEN_ACCESS_READONLY;
23984  }
23985
23986  /* Open in random access mode for possibly better speed.  Allow full
23987  ** sharing because file locks will provide exclusive access when needed.
23988  ** The handle should not be inherited by child processes and we don't
23989  ** want popups from the critical error handler.
23990  */
23991  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
23992                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
23993
23994  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
23995  ** created. SQLite doesn't use it to indicate "exclusive access"
23996  ** as it is usually understood.
23997  */
23998  if( isExclusive ){
23999    /* Creates a new file, only if it does not already exist. */
24000    /* If the file exists, it fails. */
24001    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
24002  }else if( isCreate ){
24003    /* Open existing file, or create if it doesn't exist */
24004    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24005  }else{
24006    /* Opens a file, only if it exists. */
24007    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24008  }
24009
24010  zNameCp = convertUtf8PathToCp( zUtf8Name );
24011  rc = DosOpen( (PSZ)zNameCp,
24012                &h,
24013                &ulAction,
24014                0L,
24015                FILE_NORMAL,
24016                ulOpenFlags,
24017                ulOpenMode,
24018                (PEAOP2)NULL );
24019  free( zNameCp );
24020
24021  if( rc != NO_ERROR ){
24022    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
24023              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
24024
24025    if( isReadWrite ){
24026      return os2Open( pVfs, zName, id,
24027                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
24028                      pOutFlags );
24029    }else{
24030      return SQLITE_CANTOPEN;
24031    }
24032  }
24033
24034  if( pOutFlags ){
24035    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
24036  }
24037
24038  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
24039  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
24040  pFile->pMethod = &os2IoMethod;
24041  pFile->flags = flags;
24042  pFile->h = h;
24043
24044  OpenCounter(+1);
24045  OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
24046  return SQLITE_OK;
24047}
24048
24049/*
24050** Delete the named file.
24051*/
24052static int os2Delete(
24053  sqlite3_vfs *pVfs,                     /* Not used on os2 */
24054  const char *zFilename,                 /* Name of file to delete */
24055  int syncDir                            /* Not used on os2 */
24056){
24057  APIRET rc;
24058  char *zFilenameCp;
24059  SimulateIOError( return SQLITE_IOERR_DELETE );
24060  zFilenameCp = convertUtf8PathToCp( zFilename );
24061  rc = DosDelete( (PSZ)zFilenameCp );
24062  free( zFilenameCp );
24063  OSTRACE(( "DELETE \"%s\"\n", zFilename ));
24064  return (rc == NO_ERROR ||
24065          rc == ERROR_FILE_NOT_FOUND ||
24066          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
24067}
24068
24069/*
24070** Check the existance and status of a file.
24071*/
24072static int os2Access(
24073  sqlite3_vfs *pVfs,        /* Not used on os2 */
24074  const char *zFilename,    /* Name of file to check */
24075  int flags,                /* Type of test to make on this file */
24076  int *pOut                 /* Write results here */
24077){
24078  APIRET rc;
24079  FILESTATUS3 fsts3ConfigInfo;
24080  char *zFilenameCp;
24081
24082  UNUSED_PARAMETER(pVfs);
24083  SimulateIOError( return SQLITE_IOERR_ACCESS; );
24084
24085  zFilenameCp = convertUtf8PathToCp( zFilename );
24086  rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
24087                         &fsts3ConfigInfo, sizeof(FILESTATUS3) );
24088  free( zFilenameCp );
24089  OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
24090            fsts3ConfigInfo.attrFile, flags, rc ));
24091
24092  switch( flags ){
24093    case SQLITE_ACCESS_EXISTS:
24094      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
24095      ** as if it does not exist.
24096      */
24097      if( fsts3ConfigInfo.cbFile == 0 )
24098        rc = ERROR_FILE_NOT_FOUND;
24099      break;
24100    case SQLITE_ACCESS_READ:
24101      break;
24102    case SQLITE_ACCESS_READWRITE:
24103      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
24104        rc = ERROR_ACCESS_DENIED;
24105      break;
24106    default:
24107      rc = ERROR_FILE_NOT_FOUND;
24108      assert( !"Invalid flags argument" );
24109  }
24110
24111  *pOut = (rc == NO_ERROR);
24112  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
24113
24114  return SQLITE_OK;
24115}
24116
24117
24118#ifndef SQLITE_OMIT_LOAD_EXTENSION
24119/*
24120** Interfaces for opening a shared library, finding entry points
24121** within the shared library, and closing the shared library.
24122*/
24123/*
24124** Interfaces for opening a shared library, finding entry points
24125** within the shared library, and closing the shared library.
24126*/
24127static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24128  HMODULE hmod;
24129  APIRET rc;
24130  char *zFilenameCp = convertUtf8PathToCp(zFilename);
24131  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
24132  free(zFilenameCp);
24133  return rc != NO_ERROR ? 0 : (void*)hmod;
24134}
24135/*
24136** A no-op since the error code is returned on the DosLoadModule call.
24137** os2Dlopen returns zero if DosLoadModule is not successful.
24138*/
24139static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24140/* no-op */
24141}
24142static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
24143  PFN pfn;
24144  APIRET rc;
24145  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
24146  if( rc != NO_ERROR ){
24147    /* if the symbol itself was not found, search again for the same
24148     * symbol with an extra underscore, that might be needed depending
24149     * on the calling convention */
24150    char _zSymbol[256] = "_";
24151    strncat(_zSymbol, zSymbol, 254);
24152    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
24153  }
24154  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
24155}
24156static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
24157  DosFreeModule((HMODULE)pHandle);
24158}
24159#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24160  #define os2DlOpen 0
24161  #define os2DlError 0
24162  #define os2DlSym 0
24163  #define os2DlClose 0
24164#endif
24165
24166
24167/*
24168** Write up to nBuf bytes of randomness into zBuf.
24169*/
24170static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
24171  int n = 0;
24172#if defined(SQLITE_TEST)
24173  n = nBuf;
24174  memset(zBuf, 0, nBuf);
24175#else
24176  int i;
24177  PPIB ppib;
24178  PTIB ptib;
24179  DATETIME dt;
24180  static unsigned c = 0;
24181  /* Ordered by variation probability */
24182  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
24183                            QSV_MAXPRMEM, QSV_MAXSHMEM,
24184                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
24185
24186  /* 8 bytes; timezone and weekday don't increase the randomness much */
24187  if( (int)sizeof(dt)-3 <= nBuf - n ){
24188    c += 0x0100;
24189    DosGetDateTime(&dt);
24190    dt.year = (USHORT)((dt.year - 1900) | c);
24191    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
24192    n += sizeof(dt)-3;
24193  }
24194
24195  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
24196  if( (int)sizeof(ULONG) <= nBuf - n ){
24197    DosGetInfoBlocks(&ptib, &ppib);
24198    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
24199                                 ptib->tib_ptib2->tib2_ultid);
24200    n += sizeof(ULONG);
24201  }
24202
24203  /* Up to 6 * 4 bytes; variables depend on the system state */
24204  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
24205    DosQuerySysInfo(svIdx[i], svIdx[i],
24206                    (PULONG)&zBuf[n], sizeof(ULONG));
24207    n += sizeof(ULONG);
24208  }
24209#endif
24210
24211  return n;
24212}
24213
24214/*
24215** Sleep for a little while.  Return the amount of time slept.
24216** The argument is the number of microseconds we want to sleep.
24217** The return value is the number of microseconds of sleep actually
24218** requested from the underlying operating system, a number which
24219** might be greater than or equal to the argument, but not less
24220** than the argument.
24221*/
24222static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
24223  DosSleep( (microsec/1000) );
24224  return microsec;
24225}
24226
24227/*
24228** The following variable, if set to a non-zero value, becomes the result
24229** returned from sqlite3OsCurrentTime().  This is used for testing.
24230*/
24231#ifdef SQLITE_TEST
24232SQLITE_API int sqlite3_current_time = 0;
24233#endif
24234
24235/*
24236** Find the current time (in Universal Coordinated Time).  Write into *piNow
24237** the current time and date as a Julian Day number times 86_400_000.  In
24238** other words, write into *piNow the number of milliseconds since the Julian
24239** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24240** proleptic Gregorian calendar.
24241**
24242** On success, return 0.  Return 1 if the time and date cannot be found.
24243*/
24244static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24245#ifdef SQLITE_TEST
24246  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24247#endif
24248  int year, month, datepart, timepart;
24249
24250  DATETIME dt;
24251  DosGetDateTime( &dt );
24252
24253  year = dt.year;
24254  month = dt.month;
24255
24256  /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
24257  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
24258  ** Calculate the Julian days
24259  */
24260  datepart = (int)dt.day - 32076 +
24261    1461*(year + 4800 + (month - 14)/12)/4 +
24262    367*(month - 2 - (month - 14)/12*12)/12 -
24263    3*((year + 4900 + (month - 14)/12)/100)/4;
24264
24265  /* Time in milliseconds, hours to noon added */
24266  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
24267    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
24268
24269  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
24270
24271#ifdef SQLITE_TEST
24272  if( sqlite3_current_time ){
24273    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24274  }
24275#endif
24276
24277  UNUSED_PARAMETER(pVfs);
24278  return 0;
24279}
24280
24281/*
24282** Find the current time (in Universal Coordinated Time).  Write the
24283** current time and date as a Julian Day number into *prNow and
24284** return 0.  Return 1 if the time and date cannot be found.
24285*/
24286static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
24287  int rc;
24288  sqlite3_int64 i;
24289  rc = os2CurrentTimeInt64(pVfs, &i);
24290  if( !rc ){
24291    *prNow = i/86400000.0;
24292  }
24293  return rc;
24294}
24295
24296/*
24297** The idea is that this function works like a combination of
24298** GetLastError() and FormatMessage() on windows (or errno and
24299** strerror_r() on unix). After an error is returned by an OS
24300** function, SQLite calls this function with zBuf pointing to
24301** a buffer of nBuf bytes. The OS layer should populate the
24302** buffer with a nul-terminated UTF-8 encoded error message
24303** describing the last IO error to have occurred within the calling
24304** thread.
24305**
24306** If the error message is too large for the supplied buffer,
24307** it should be truncated. The return value of xGetLastError
24308** is zero if the error message fits in the buffer, or non-zero
24309** otherwise (if the message was truncated). If non-zero is returned,
24310** then it is not necessary to include the nul-terminator character
24311** in the output buffer.
24312**
24313** Not supplying an error message will have no adverse effect
24314** on SQLite. It is fine to have an implementation that never
24315** returns an error message:
24316**
24317**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24318**     assert(zBuf[0]=='\0');
24319**     return 0;
24320**   }
24321**
24322** However if an error message is supplied, it will be incorporated
24323** by sqlite into the error message available to the user using
24324** sqlite3_errmsg(), possibly making IO errors easier to debug.
24325*/
24326static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24327  assert(zBuf[0]=='\0');
24328  return 0;
24329}
24330
24331/*
24332** Initialize and deinitialize the operating system interface.
24333*/
24334SQLITE_API int sqlite3_os_init(void){
24335  static sqlite3_vfs os2Vfs = {
24336    3,                 /* iVersion */
24337    sizeof(os2File),   /* szOsFile */
24338    CCHMAXPATH,        /* mxPathname */
24339    0,                 /* pNext */
24340    "os2",             /* zName */
24341    0,                 /* pAppData */
24342
24343    os2Open,           /* xOpen */
24344    os2Delete,         /* xDelete */
24345    os2Access,         /* xAccess */
24346    os2FullPathname,   /* xFullPathname */
24347    os2DlOpen,         /* xDlOpen */
24348    os2DlError,        /* xDlError */
24349    os2DlSym,          /* xDlSym */
24350    os2DlClose,        /* xDlClose */
24351    os2Randomness,     /* xRandomness */
24352    os2Sleep,          /* xSleep */
24353    os2CurrentTime,    /* xCurrentTime */
24354    os2GetLastError,   /* xGetLastError */
24355    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
24356    0,                 /* xSetSystemCall */
24357    0,                 /* xGetSystemCall */
24358    0                  /* xNextSystemCall */
24359  };
24360  sqlite3_vfs_register(&os2Vfs, 1);
24361  initUconvObjects();
24362/*  sqlite3OSTrace = 1; */
24363  return SQLITE_OK;
24364}
24365SQLITE_API int sqlite3_os_end(void){
24366  freeUconvObjects();
24367  return SQLITE_OK;
24368}
24369
24370#endif /* SQLITE_OS_OS2 */
24371
24372/************** End of os_os2.c **********************************************/
24373/************** Begin file os_unix.c *****************************************/
24374/*
24375** 2004 May 22
24376**
24377** The author disclaims copyright to this source code.  In place of
24378** a legal notice, here is a blessing:
24379**
24380**    May you do good and not evil.
24381**    May you find forgiveness for yourself and forgive others.
24382**    May you share freely, never taking more than you give.
24383**
24384******************************************************************************
24385**
24386** This file contains the VFS implementation for unix-like operating systems
24387** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
24388**
24389** There are actually several different VFS implementations in this file.
24390** The differences are in the way that file locking is done.  The default
24391** implementation uses Posix Advisory Locks.  Alternative implementations
24392** use flock(), dot-files, various proprietary locking schemas, or simply
24393** skip locking all together.
24394**
24395** This source file is organized into divisions where the logic for various
24396** subfunctions is contained within the appropriate division.  PLEASE
24397** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
24398** in the correct division and should be clearly labeled.
24399**
24400** The layout of divisions is as follows:
24401**
24402**   *  General-purpose declarations and utility functions.
24403**   *  Unique file ID logic used by VxWorks.
24404**   *  Various locking primitive implementations (all except proxy locking):
24405**      + for Posix Advisory Locks
24406**      + for no-op locks
24407**      + for dot-file locks
24408**      + for flock() locking
24409**      + for named semaphore locks (VxWorks only)
24410**      + for AFP filesystem locks (MacOSX only)
24411**   *  sqlite3_file methods not associated with locking.
24412**   *  Definitions of sqlite3_io_methods objects for all locking
24413**      methods plus "finder" functions for each locking method.
24414**   *  sqlite3_vfs method implementations.
24415**   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
24416**   *  Definitions of sqlite3_vfs objects for all locking methods
24417**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
24418*/
24419#if SQLITE_OS_UNIX              /* This file is used on unix only */
24420
24421/*
24422** There are various methods for file locking used for concurrency
24423** control:
24424**
24425**   1. POSIX locking (the default),
24426**   2. No locking,
24427**   3. Dot-file locking,
24428**   4. flock() locking,
24429**   5. AFP locking (OSX only),
24430**   6. Named POSIX semaphores (VXWorks only),
24431**   7. proxy locking. (OSX only)
24432**
24433** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
24434** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
24435** selection of the appropriate locking style based on the filesystem
24436** where the database is located.
24437*/
24438#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
24439#  if defined(__APPLE__)
24440#    define SQLITE_ENABLE_LOCKING_STYLE 1
24441#  else
24442#    define SQLITE_ENABLE_LOCKING_STYLE 0
24443#  endif
24444#endif
24445
24446/*
24447** Define the OS_VXWORKS pre-processor macro to 1 if building on
24448** vxworks, or 0 otherwise.
24449*/
24450#ifndef OS_VXWORKS
24451#  if defined(__RTP__) || defined(_WRS_KERNEL)
24452#    define OS_VXWORKS 1
24453#  else
24454#    define OS_VXWORKS 0
24455#  endif
24456#endif
24457
24458/*
24459** These #defines should enable >2GB file support on Posix if the
24460** underlying operating system supports it.  If the OS lacks
24461** large file support, these should be no-ops.
24462**
24463** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
24464** on the compiler command line.  This is necessary if you are compiling
24465** on a recent machine (ex: RedHat 7.2) but you want your code to work
24466** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
24467** without this option, LFS is enable.  But LFS does not exist in the kernel
24468** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
24469** portability you should omit LFS.
24470**
24471** The previous paragraph was written in 2005.  (This paragraph is written
24472** on 2008-11-28.) These days, all Linux kernels support large files, so
24473** you should probably leave LFS enabled.  But some embedded platforms might
24474** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
24475*/
24476#ifndef SQLITE_DISABLE_LFS
24477# define _LARGE_FILE       1
24478# ifndef _FILE_OFFSET_BITS
24479#   define _FILE_OFFSET_BITS 64
24480# endif
24481# define _LARGEFILE_SOURCE 1
24482#endif
24483
24484/*
24485** standard include files.
24486*/
24487#include <sys/types.h>
24488#include <sys/stat.h>
24489#include <fcntl.h>
24490#include <unistd.h>
24491/* #include <time.h> */
24492#include <sys/time.h>
24493#include <errno.h>
24494#ifndef SQLITE_OMIT_WAL
24495#include <sys/mman.h>
24496#endif
24497
24498#if SQLITE_ENABLE_LOCKING_STYLE
24499# include <sys/ioctl.h>
24500# if OS_VXWORKS
24501#  include <semaphore.h>
24502#  include <limits.h>
24503# else
24504#  include <sys/file.h>
24505#  include <sys/param.h>
24506# endif
24507#endif /* SQLITE_ENABLE_LOCKING_STYLE */
24508
24509#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
24510# include <sys/mount.h>
24511#endif
24512
24513#ifdef HAVE_UTIME
24514# include <utime.h>
24515#endif
24516
24517/*
24518** Allowed values of unixFile.fsFlags
24519*/
24520#define SQLITE_FSFLAGS_IS_MSDOS     0x1
24521
24522/*
24523** If we are to be thread-safe, include the pthreads header and define
24524** the SQLITE_UNIX_THREADS macro.
24525*/
24526#if SQLITE_THREADSAFE
24527/* # include <pthread.h> */
24528# define SQLITE_UNIX_THREADS 1
24529#endif
24530
24531/*
24532** Default permissions when creating a new file
24533*/
24534#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
24535# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
24536#endif
24537
24538/*
24539 ** Default permissions when creating auto proxy dir
24540 */
24541#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
24542# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
24543#endif
24544
24545/*
24546** Maximum supported path-length.
24547*/
24548#define MAX_PATHNAME 512
24549
24550/*
24551** Only set the lastErrno if the error code is a real error and not
24552** a normal expected return code of SQLITE_BUSY or SQLITE_OK
24553*/
24554#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
24555
24556/* Forward references */
24557typedef struct unixShm unixShm;               /* Connection shared memory */
24558typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
24559typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
24560typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
24561
24562/*
24563** Sometimes, after a file handle is closed by SQLite, the file descriptor
24564** cannot be closed immediately. In these cases, instances of the following
24565** structure are used to store the file descriptor while waiting for an
24566** opportunity to either close or reuse it.
24567*/
24568struct UnixUnusedFd {
24569  int fd;                   /* File descriptor to close */
24570  int flags;                /* Flags this file descriptor was opened with */
24571  UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
24572};
24573
24574/*
24575** The unixFile structure is subclass of sqlite3_file specific to the unix
24576** VFS implementations.
24577*/
24578typedef struct unixFile unixFile;
24579struct unixFile {
24580  sqlite3_io_methods const *pMethod;  /* Always the first entry */
24581  unixInodeInfo *pInode;              /* Info about locks on this inode */
24582  int h;                              /* The file descriptor */
24583  unsigned char eFileLock;            /* The type of lock held on this fd */
24584  unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
24585  int lastErrno;                      /* The unix errno from last I/O error */
24586  void *lockingContext;               /* Locking style specific state */
24587  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
24588  const char *zPath;                  /* Name of the file */
24589  unixShm *pShm;                      /* Shared memory segment information */
24590  int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
24591#if SQLITE_ENABLE_LOCKING_STYLE
24592  int openFlags;                      /* The flags specified at open() */
24593#endif
24594#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
24595  unsigned fsFlags;                   /* cached details from statfs() */
24596#endif
24597#if OS_VXWORKS
24598  int isDelete;                       /* Delete on close if true */
24599  struct vxworksFileId *pId;          /* Unique file ID */
24600#endif
24601#ifndef NDEBUG
24602  /* The next group of variables are used to track whether or not the
24603  ** transaction counter in bytes 24-27 of database files are updated
24604  ** whenever any part of the database changes.  An assertion fault will
24605  ** occur if a file is updated without also updating the transaction
24606  ** counter.  This test is made to avoid new problems similar to the
24607  ** one described by ticket #3584.
24608  */
24609  unsigned char transCntrChng;   /* True if the transaction counter changed */
24610  unsigned char dbUpdate;        /* True if any part of database file changed */
24611  unsigned char inNormalWrite;   /* True if in a normal write operation */
24612#endif
24613#ifdef SQLITE_TEST
24614  /* In test mode, increase the size of this structure a bit so that
24615  ** it is larger than the struct CrashFile defined in test6.c.
24616  */
24617  char aPadding[32];
24618#endif
24619};
24620
24621/*
24622** Allowed values for the unixFile.ctrlFlags bitmask:
24623*/
24624#define UNIXFILE_EXCL        0x01     /* Connections from one process only */
24625#define UNIXFILE_RDONLY      0x02     /* Connection is read only */
24626#define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
24627#ifndef SQLITE_DISABLE_DIRSYNC
24628# define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
24629#else
24630# define UNIXFILE_DIRSYNC    0x00
24631#endif
24632
24633/*
24634** Include code that is common to all os_*.c files
24635*/
24636/************** Include os_common.h in the middle of os_unix.c ***************/
24637/************** Begin file os_common.h ***************************************/
24638/*
24639** 2004 May 22
24640**
24641** The author disclaims copyright to this source code.  In place of
24642** a legal notice, here is a blessing:
24643**
24644**    May you do good and not evil.
24645**    May you find forgiveness for yourself and forgive others.
24646**    May you share freely, never taking more than you give.
24647**
24648******************************************************************************
24649**
24650** This file contains macros and a little bit of code that is common to
24651** all of the platform-specific files (os_*.c) and is #included into those
24652** files.
24653**
24654** This file should be #included by the os_*.c files only.  It is not a
24655** general purpose header file.
24656*/
24657#ifndef _OS_COMMON_H_
24658#define _OS_COMMON_H_
24659
24660/*
24661** At least two bugs have slipped in because we changed the MEMORY_DEBUG
24662** macro to SQLITE_DEBUG and some older makefiles have not yet made the
24663** switch.  The following code should catch this problem at compile-time.
24664*/
24665#ifdef MEMORY_DEBUG
24666# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
24667#endif
24668
24669#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
24670# ifndef SQLITE_DEBUG_OS_TRACE
24671#   define SQLITE_DEBUG_OS_TRACE 0
24672# endif
24673  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
24674# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
24675#else
24676# define OSTRACE(X)
24677#endif
24678
24679/*
24680** Macros for performance tracing.  Normally turned off.  Only works
24681** on i486 hardware.
24682*/
24683#ifdef SQLITE_PERFORMANCE_TRACE
24684
24685/*
24686** hwtime.h contains inline assembler code for implementing
24687** high-performance timing routines.
24688*/
24689/************** Include hwtime.h in the middle of os_common.h ****************/
24690/************** Begin file hwtime.h ******************************************/
24691/*
24692** 2008 May 27
24693**
24694** The author disclaims copyright to this source code.  In place of
24695** a legal notice, here is a blessing:
24696**
24697**    May you do good and not evil.
24698**    May you find forgiveness for yourself and forgive others.
24699**    May you share freely, never taking more than you give.
24700**
24701******************************************************************************
24702**
24703** This file contains inline asm code for retrieving "high-performance"
24704** counters for x86 class CPUs.
24705*/
24706#ifndef _HWTIME_H_
24707#define _HWTIME_H_
24708
24709/*
24710** The following routine only works on pentium-class (or newer) processors.
24711** It uses the RDTSC opcode to read the cycle count value out of the
24712** processor and returns that value.  This can be used for high-res
24713** profiling.
24714*/
24715#if (defined(__GNUC__) || defined(_MSC_VER)) && \
24716      (defined(i386) || defined(__i386__) || defined(_M_IX86))
24717
24718  #if defined(__GNUC__)
24719
24720  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24721     unsigned int lo, hi;
24722     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
24723     return (sqlite_uint64)hi << 32 | lo;
24724  }
24725
24726  #elif defined(_MSC_VER)
24727
24728  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
24729     __asm {
24730        rdtsc
24731        ret       ; return value at EDX:EAX
24732     }
24733  }
24734
24735  #endif
24736
24737#elif (defined(__GNUC__) && defined(__x86_64__))
24738
24739  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24740      unsigned long val;
24741      __asm__ __volatile__ ("rdtsc" : "=A" (val));
24742      return val;
24743  }
24744
24745#elif (defined(__GNUC__) && defined(__ppc__))
24746
24747  __inline__ sqlite_uint64 sqlite3Hwtime(void){
24748      unsigned long long retval;
24749      unsigned long junk;
24750      __asm__ __volatile__ ("\n\
24751          1:      mftbu   %1\n\
24752                  mftb    %L0\n\
24753                  mftbu   %0\n\
24754                  cmpw    %0,%1\n\
24755                  bne     1b"
24756                  : "=r" (retval), "=r" (junk));
24757      return retval;
24758  }
24759
24760#else
24761
24762  #error Need implementation of sqlite3Hwtime() for your platform.
24763
24764  /*
24765  ** To compile without implementing sqlite3Hwtime() for your platform,
24766  ** you can remove the above #error and use the following
24767  ** stub function.  You will lose timing support for many
24768  ** of the debugging and testing utilities, but it should at
24769  ** least compile and run.
24770  */
24771SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
24772
24773#endif
24774
24775#endif /* !defined(_HWTIME_H_) */
24776
24777/************** End of hwtime.h **********************************************/
24778/************** Continuing where we left off in os_common.h ******************/
24779
24780static sqlite_uint64 g_start;
24781static sqlite_uint64 g_elapsed;
24782#define TIMER_START       g_start=sqlite3Hwtime()
24783#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
24784#define TIMER_ELAPSED     g_elapsed
24785#else
24786#define TIMER_START
24787#define TIMER_END
24788#define TIMER_ELAPSED     ((sqlite_uint64)0)
24789#endif
24790
24791/*
24792** If we compile with the SQLITE_TEST macro set, then the following block
24793** of code will give us the ability to simulate a disk I/O error.  This
24794** is used for testing the I/O recovery logic.
24795*/
24796#ifdef SQLITE_TEST
24797SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
24798SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
24799SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
24800SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
24801SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
24802SQLITE_API int sqlite3_diskfull_pending = 0;
24803SQLITE_API int sqlite3_diskfull = 0;
24804#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
24805#define SimulateIOError(CODE)  \
24806  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
24807       || sqlite3_io_error_pending-- == 1 )  \
24808              { local_ioerr(); CODE; }
24809static void local_ioerr(){
24810  IOTRACE(("IOERR\n"));
24811  sqlite3_io_error_hit++;
24812  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
24813}
24814#define SimulateDiskfullError(CODE) \
24815   if( sqlite3_diskfull_pending ){ \
24816     if( sqlite3_diskfull_pending == 1 ){ \
24817       local_ioerr(); \
24818       sqlite3_diskfull = 1; \
24819       sqlite3_io_error_hit = 1; \
24820       CODE; \
24821     }else{ \
24822       sqlite3_diskfull_pending--; \
24823     } \
24824   }
24825#else
24826#define SimulateIOErrorBenign(X)
24827#define SimulateIOError(A)
24828#define SimulateDiskfullError(A)
24829#endif
24830
24831/*
24832** When testing, keep a count of the number of open files.
24833*/
24834#ifdef SQLITE_TEST
24835SQLITE_API int sqlite3_open_file_count = 0;
24836#define OpenCounter(X)  sqlite3_open_file_count+=(X)
24837#else
24838#define OpenCounter(X)
24839#endif
24840
24841#endif /* !defined(_OS_COMMON_H_) */
24842
24843/************** End of os_common.h *******************************************/
24844/************** Continuing where we left off in os_unix.c ********************/
24845
24846/*
24847** Define various macros that are missing from some systems.
24848*/
24849#ifndef O_LARGEFILE
24850# define O_LARGEFILE 0
24851#endif
24852#ifdef SQLITE_DISABLE_LFS
24853# undef O_LARGEFILE
24854# define O_LARGEFILE 0
24855#endif
24856#ifndef O_NOFOLLOW
24857# define O_NOFOLLOW 0
24858#endif
24859#ifndef O_BINARY
24860# define O_BINARY 0
24861#endif
24862
24863/*
24864** The threadid macro resolves to the thread-id or to 0.  Used for
24865** testing and debugging only.
24866*/
24867#if SQLITE_THREADSAFE
24868#define threadid pthread_self()
24869#else
24870#define threadid 0
24871#endif
24872
24873/*
24874** Different Unix systems declare open() in different ways.  Same use
24875** open(const char*,int,mode_t).  Others use open(const char*,int,...).
24876** The difference is important when using a pointer to the function.
24877**
24878** The safest way to deal with the problem is to always use this wrapper
24879** which always has the same well-defined interface.
24880*/
24881static int posixOpen(const char *zFile, int flags, int mode){
24882  return open(zFile, flags, mode);
24883}
24884
24885/* Forward reference */
24886static int openDirectory(const char*, int*);
24887
24888/*
24889** Many system calls are accessed through pointer-to-functions so that
24890** they may be overridden at runtime to facilitate fault injection during
24891** testing and sandboxing.  The following array holds the names and pointers
24892** to all overrideable system calls.
24893*/
24894static struct unix_syscall {
24895  const char *zName;            /* Name of the sytem call */
24896  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
24897  sqlite3_syscall_ptr pDefault; /* Default value */
24898} aSyscall[] = {
24899  { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
24900#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
24901
24902  { "close",        (sqlite3_syscall_ptr)close,      0  },
24903#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
24904
24905  { "access",       (sqlite3_syscall_ptr)access,     0  },
24906#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
24907
24908  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
24909#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
24910
24911  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
24912#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
24913
24914/*
24915** The DJGPP compiler environment looks mostly like Unix, but it
24916** lacks the fcntl() system call.  So redefine fcntl() to be something
24917** that always succeeds.  This means that locking does not occur under
24918** DJGPP.  But it is DOS - what did you expect?
24919*/
24920#ifdef __DJGPP__
24921  { "fstat",        0,                 0  },
24922#define osFstat(a,b,c)    0
24923#else
24924  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
24925#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
24926#endif
24927
24928  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
24929#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
24930
24931  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
24932#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
24933
24934  { "read",         (sqlite3_syscall_ptr)read,       0  },
24935#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24936
24937#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24938  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
24939#else
24940  { "pread",        (sqlite3_syscall_ptr)0,          0  },
24941#endif
24942#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
24943
24944#if defined(USE_PREAD64)
24945  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
24946#else
24947  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
24948#endif
24949#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24950
24951  { "write",        (sqlite3_syscall_ptr)write,      0  },
24952#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24953
24954#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24955  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
24956#else
24957  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
24958#endif
24959#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
24960                    aSyscall[12].pCurrent)
24961
24962#if defined(USE_PREAD64)
24963  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
24964#else
24965  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
24966#endif
24967#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
24968                    aSyscall[13].pCurrent)
24969
24970#if SQLITE_ENABLE_LOCKING_STYLE
24971  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
24972#else
24973  { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
24974#endif
24975#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
24976
24977#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
24978  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
24979#else
24980  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
24981#endif
24982#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24983
24984  { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
24985#define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
24986
24987  { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
24988#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
24989
24990}; /* End of the overrideable system calls */
24991
24992/*
24993** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24994** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
24995** system call pointer, or SQLITE_NOTFOUND if there is no configurable
24996** system call named zName.
24997*/
24998static int unixSetSystemCall(
24999  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
25000  const char *zName,            /* Name of system call to override */
25001  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
25002){
25003  unsigned int i;
25004  int rc = SQLITE_NOTFOUND;
25005
25006  UNUSED_PARAMETER(pNotUsed);
25007  if( zName==0 ){
25008    /* If no zName is given, restore all system calls to their default
25009    ** settings and return NULL
25010    */
25011    rc = SQLITE_OK;
25012    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
25013      if( aSyscall[i].pDefault ){
25014        aSyscall[i].pCurrent = aSyscall[i].pDefault;
25015      }
25016    }
25017  }else{
25018    /* If zName is specified, operate on only the one system call
25019    ** specified.
25020    */
25021    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
25022      if( strcmp(zName, aSyscall[i].zName)==0 ){
25023        if( aSyscall[i].pDefault==0 ){
25024          aSyscall[i].pDefault = aSyscall[i].pCurrent;
25025        }
25026        rc = SQLITE_OK;
25027        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
25028        aSyscall[i].pCurrent = pNewFunc;
25029        break;
25030      }
25031    }
25032  }
25033  return rc;
25034}
25035
25036/*
25037** Return the value of a system call.  Return NULL if zName is not a
25038** recognized system call name.  NULL is also returned if the system call
25039** is currently undefined.
25040*/
25041static sqlite3_syscall_ptr unixGetSystemCall(
25042  sqlite3_vfs *pNotUsed,
25043  const char *zName
25044){
25045  unsigned int i;
25046
25047  UNUSED_PARAMETER(pNotUsed);
25048  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
25049    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
25050  }
25051  return 0;
25052}
25053
25054/*
25055** Return the name of the first system call after zName.  If zName==NULL
25056** then return the name of the first system call.  Return NULL if zName
25057** is the last system call or if zName is not the name of a valid
25058** system call.
25059*/
25060static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
25061  int i = -1;
25062
25063  UNUSED_PARAMETER(p);
25064  if( zName ){
25065    for(i=0; i<ArraySize(aSyscall)-1; i++){
25066      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
25067    }
25068  }
25069  for(i++; i<ArraySize(aSyscall); i++){
25070    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
25071  }
25072  return 0;
25073}
25074
25075/*
25076** Retry open() calls that fail due to EINTR
25077*/
25078static int robust_open(const char *z, int f, int m){
25079  int rc;
25080  do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
25081  return rc;
25082}
25083
25084/*
25085** Helper functions to obtain and relinquish the global mutex. The
25086** global mutex is used to protect the unixInodeInfo and
25087** vxworksFileId objects used by this file, all of which may be
25088** shared by multiple threads.
25089**
25090** Function unixMutexHeld() is used to assert() that the global mutex
25091** is held when required. This function is only used as part of assert()
25092** statements. e.g.
25093**
25094**   unixEnterMutex()
25095**     assert( unixMutexHeld() );
25096**   unixEnterLeave()
25097*/
25098static void unixEnterMutex(void){
25099  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
25100}
25101static void unixLeaveMutex(void){
25102  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
25103}
25104#ifdef SQLITE_DEBUG
25105static int unixMutexHeld(void) {
25106  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
25107}
25108#endif
25109
25110
25111#ifdef SQLITE_DEBUG
25112/*
25113** Helper function for printing out trace information from debugging
25114** binaries. This returns the string represetation of the supplied
25115** integer lock-type.
25116*/
25117static const char *azFileLock(int eFileLock){
25118  switch( eFileLock ){
25119    case NO_LOCK: return "NONE";
25120    case SHARED_LOCK: return "SHARED";
25121    case RESERVED_LOCK: return "RESERVED";
25122    case PENDING_LOCK: return "PENDING";
25123    case EXCLUSIVE_LOCK: return "EXCLUSIVE";
25124  }
25125  return "ERROR";
25126}
25127#endif
25128
25129#ifdef SQLITE_LOCK_TRACE
25130/*
25131** Print out information about all locking operations.
25132**
25133** This routine is used for troubleshooting locks on multithreaded
25134** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
25135** command-line option on the compiler.  This code is normally
25136** turned off.
25137*/
25138static int lockTrace(int fd, int op, struct flock *p){
25139  char *zOpName, *zType;
25140  int s;
25141  int savedErrno;
25142  if( op==F_GETLK ){
25143    zOpName = "GETLK";
25144  }else if( op==F_SETLK ){
25145    zOpName = "SETLK";
25146  }else{
25147    s = osFcntl(fd, op, p);
25148    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
25149    return s;
25150  }
25151  if( p->l_type==F_RDLCK ){
25152    zType = "RDLCK";
25153  }else if( p->l_type==F_WRLCK ){
25154    zType = "WRLCK";
25155  }else if( p->l_type==F_UNLCK ){
25156    zType = "UNLCK";
25157  }else{
25158    assert( 0 );
25159  }
25160  assert( p->l_whence==SEEK_SET );
25161  s = osFcntl(fd, op, p);
25162  savedErrno = errno;
25163  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
25164     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
25165     (int)p->l_pid, s);
25166  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
25167    struct flock l2;
25168    l2 = *p;
25169    osFcntl(fd, F_GETLK, &l2);
25170    if( l2.l_type==F_RDLCK ){
25171      zType = "RDLCK";
25172    }else if( l2.l_type==F_WRLCK ){
25173      zType = "WRLCK";
25174    }else if( l2.l_type==F_UNLCK ){
25175      zType = "UNLCK";
25176    }else{
25177      assert( 0 );
25178    }
25179    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
25180       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
25181  }
25182  errno = savedErrno;
25183  return s;
25184}
25185#undef osFcntl
25186#define osFcntl lockTrace
25187#endif /* SQLITE_LOCK_TRACE */
25188
25189/*
25190** Retry ftruncate() calls that fail due to EINTR
25191*/
25192static int robust_ftruncate(int h, sqlite3_int64 sz){
25193  int rc;
25194  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
25195  return rc;
25196}
25197
25198/*
25199** This routine translates a standard POSIX errno code into something
25200** useful to the clients of the sqlite3 functions.  Specifically, it is
25201** intended to translate a variety of "try again" errors into SQLITE_BUSY
25202** and a variety of "please close the file descriptor NOW" errors into
25203** SQLITE_IOERR
25204**
25205** Errors during initialization of locks, or file system support for locks,
25206** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
25207*/
25208static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
25209  switch (posixError) {
25210#if 0
25211  /* At one point this code was not commented out. In theory, this branch
25212  ** should never be hit, as this function should only be called after
25213  ** a locking-related function (i.e. fcntl()) has returned non-zero with
25214  ** the value of errno as the first argument. Since a system call has failed,
25215  ** errno should be non-zero.
25216  **
25217  ** Despite this, if errno really is zero, we still don't want to return
25218  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
25219  ** propagated back to the caller. Commenting this branch out means errno==0
25220  ** will be handled by the "default:" case below.
25221  */
25222  case 0:
25223    return SQLITE_OK;
25224#endif
25225
25226  case EAGAIN:
25227  case ETIMEDOUT:
25228  case EBUSY:
25229  case EINTR:
25230  case ENOLCK:
25231    /* random NFS retry error, unless during file system support
25232     * introspection, in which it actually means what it says */
25233    return SQLITE_BUSY;
25234
25235  case EACCES:
25236    /* EACCES is like EAGAIN during locking operations, but not any other time*/
25237    if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
25238	(sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
25239	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
25240	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
25241      return SQLITE_BUSY;
25242    }
25243    /* else fall through */
25244  case EPERM:
25245    return SQLITE_PERM;
25246
25247  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
25248  ** this module never makes such a call. And the code in SQLite itself
25249  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
25250  ** this case is also commented out. If the system does set errno to EDEADLK,
25251  ** the default SQLITE_IOERR_XXX code will be returned. */
25252#if 0
25253  case EDEADLK:
25254    return SQLITE_IOERR_BLOCKED;
25255#endif
25256
25257#if EOPNOTSUPP!=ENOTSUP
25258  case EOPNOTSUPP:
25259    /* something went terribly awry, unless during file system support
25260     * introspection, in which it actually means what it says */
25261#endif
25262#ifdef ENOTSUP
25263  case ENOTSUP:
25264    /* invalid fd, unless during file system support introspection, in which
25265     * it actually means what it says */
25266#endif
25267  case EIO:
25268  case EBADF:
25269  case EINVAL:
25270  case ENOTCONN:
25271  case ENODEV:
25272  case ENXIO:
25273  case ENOENT:
25274#ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
25275  case ESTALE:
25276#endif
25277  case ENOSYS:
25278    /* these should force the client to close the file and reconnect */
25279
25280  default:
25281    return sqliteIOErr;
25282  }
25283}
25284
25285
25286
25287/******************************************************************************
25288****************** Begin Unique File ID Utility Used By VxWorks ***************
25289**
25290** On most versions of unix, we can get a unique ID for a file by concatenating
25291** the device number and the inode number.  But this does not work on VxWorks.
25292** On VxWorks, a unique file id must be based on the canonical filename.
25293**
25294** A pointer to an instance of the following structure can be used as a
25295** unique file ID in VxWorks.  Each instance of this structure contains
25296** a copy of the canonical filename.  There is also a reference count.
25297** The structure is reclaimed when the number of pointers to it drops to
25298** zero.
25299**
25300** There are never very many files open at one time and lookups are not
25301** a performance-critical path, so it is sufficient to put these
25302** structures on a linked list.
25303*/
25304struct vxworksFileId {
25305  struct vxworksFileId *pNext;  /* Next in a list of them all */
25306  int nRef;                     /* Number of references to this one */
25307  int nName;                    /* Length of the zCanonicalName[] string */
25308  char *zCanonicalName;         /* Canonical filename */
25309};
25310
25311#if OS_VXWORKS
25312/*
25313** All unique filenames are held on a linked list headed by this
25314** variable:
25315*/
25316static struct vxworksFileId *vxworksFileList = 0;
25317
25318/*
25319** Simplify a filename into its canonical form
25320** by making the following changes:
25321**
25322**  * removing any trailing and duplicate /
25323**  * convert /./ into just /
25324**  * convert /A/../ where A is any simple name into just /
25325**
25326** Changes are made in-place.  Return the new name length.
25327**
25328** The original filename is in z[0..n-1].  Return the number of
25329** characters in the simplified name.
25330*/
25331static int vxworksSimplifyName(char *z, int n){
25332  int i, j;
25333  while( n>1 && z[n-1]=='/' ){ n--; }
25334  for(i=j=0; i<n; i++){
25335    if( z[i]=='/' ){
25336      if( z[i+1]=='/' ) continue;
25337      if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
25338        i += 1;
25339        continue;
25340      }
25341      if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
25342        while( j>0 && z[j-1]!='/' ){ j--; }
25343        if( j>0 ){ j--; }
25344        i += 2;
25345        continue;
25346      }
25347    }
25348    z[j++] = z[i];
25349  }
25350  z[j] = 0;
25351  return j;
25352}
25353
25354/*
25355** Find a unique file ID for the given absolute pathname.  Return
25356** a pointer to the vxworksFileId object.  This pointer is the unique
25357** file ID.
25358**
25359** The nRef field of the vxworksFileId object is incremented before
25360** the object is returned.  A new vxworksFileId object is created
25361** and added to the global list if necessary.
25362**
25363** If a memory allocation error occurs, return NULL.
25364*/
25365static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
25366  struct vxworksFileId *pNew;         /* search key and new file ID */
25367  struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
25368  int n;                              /* Length of zAbsoluteName string */
25369
25370  assert( zAbsoluteName[0]=='/' );
25371  n = (int)strlen(zAbsoluteName);
25372  pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
25373  if( pNew==0 ) return 0;
25374  pNew->zCanonicalName = (char*)&pNew[1];
25375  memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
25376  n = vxworksSimplifyName(pNew->zCanonicalName, n);
25377
25378  /* Search for an existing entry that matching the canonical name.
25379  ** If found, increment the reference count and return a pointer to
25380  ** the existing file ID.
25381  */
25382  unixEnterMutex();
25383  for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
25384    if( pCandidate->nName==n
25385     && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
25386    ){
25387       sqlite3_free(pNew);
25388       pCandidate->nRef++;
25389       unixLeaveMutex();
25390       return pCandidate;
25391    }
25392  }
25393
25394  /* No match was found.  We will make a new file ID */
25395  pNew->nRef = 1;
25396  pNew->nName = n;
25397  pNew->pNext = vxworksFileList;
25398  vxworksFileList = pNew;
25399  unixLeaveMutex();
25400  return pNew;
25401}
25402
25403/*
25404** Decrement the reference count on a vxworksFileId object.  Free
25405** the object when the reference count reaches zero.
25406*/
25407static void vxworksReleaseFileId(struct vxworksFileId *pId){
25408  unixEnterMutex();
25409  assert( pId->nRef>0 );
25410  pId->nRef--;
25411  if( pId->nRef==0 ){
25412    struct vxworksFileId **pp;
25413    for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
25414    assert( *pp==pId );
25415    *pp = pId->pNext;
25416    sqlite3_free(pId);
25417  }
25418  unixLeaveMutex();
25419}
25420#endif /* OS_VXWORKS */
25421/*************** End of Unique File ID Utility Used By VxWorks ****************
25422******************************************************************************/
25423
25424
25425/******************************************************************************
25426*************************** Posix Advisory Locking ****************************
25427**
25428** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
25429** section 6.5.2.2 lines 483 through 490 specify that when a process
25430** sets or clears a lock, that operation overrides any prior locks set
25431** by the same process.  It does not explicitly say so, but this implies
25432** that it overrides locks set by the same process using a different
25433** file descriptor.  Consider this test case:
25434**
25435**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
25436**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
25437**
25438** Suppose ./file1 and ./file2 are really the same file (because
25439** one is a hard or symbolic link to the other) then if you set
25440** an exclusive lock on fd1, then try to get an exclusive lock
25441** on fd2, it works.  I would have expected the second lock to
25442** fail since there was already a lock on the file due to fd1.
25443** But not so.  Since both locks came from the same process, the
25444** second overrides the first, even though they were on different
25445** file descriptors opened on different file names.
25446**
25447** This means that we cannot use POSIX locks to synchronize file access
25448** among competing threads of the same process.  POSIX locks will work fine
25449** to synchronize access for threads in separate processes, but not
25450** threads within the same process.
25451**
25452** To work around the problem, SQLite has to manage file locks internally
25453** on its own.  Whenever a new database is opened, we have to find the
25454** specific inode of the database file (the inode is determined by the
25455** st_dev and st_ino fields of the stat structure that fstat() fills in)
25456** and check for locks already existing on that inode.  When locks are
25457** created or removed, we have to look at our own internal record of the
25458** locks to see if another thread has previously set a lock on that same
25459** inode.
25460**
25461** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
25462** For VxWorks, we have to use the alternative unique ID system based on
25463** canonical filename and implemented in the previous division.)
25464**
25465** The sqlite3_file structure for POSIX is no longer just an integer file
25466** descriptor.  It is now a structure that holds the integer file
25467** descriptor and a pointer to a structure that describes the internal
25468** locks on the corresponding inode.  There is one locking structure
25469** per inode, so if the same inode is opened twice, both unixFile structures
25470** point to the same locking structure.  The locking structure keeps
25471** a reference count (so we will know when to delete it) and a "cnt"
25472** field that tells us its internal lock status.  cnt==0 means the
25473** file is unlocked.  cnt==-1 means the file has an exclusive lock.
25474** cnt>0 means there are cnt shared locks on the file.
25475**
25476** Any attempt to lock or unlock a file first checks the locking
25477** structure.  The fcntl() system call is only invoked to set a
25478** POSIX lock if the internal lock structure transitions between
25479** a locked and an unlocked state.
25480**
25481** But wait:  there are yet more problems with POSIX advisory locks.
25482**
25483** If you close a file descriptor that points to a file that has locks,
25484** all locks on that file that are owned by the current process are
25485** released.  To work around this problem, each unixInodeInfo object
25486** maintains a count of the number of pending locks on tha inode.
25487** When an attempt is made to close an unixFile, if there are
25488** other unixFile open on the same inode that are holding locks, the call
25489** to close() the file descriptor is deferred until all of the locks clear.
25490** The unixInodeInfo structure keeps a list of file descriptors that need to
25491** be closed and that list is walked (and cleared) when the last lock
25492** clears.
25493**
25494** Yet another problem:  LinuxThreads do not play well with posix locks.
25495**
25496** Many older versions of linux use the LinuxThreads library which is
25497** not posix compliant.  Under LinuxThreads, a lock created by thread
25498** A cannot be modified or overridden by a different thread B.
25499** Only thread A can modify the lock.  Locking behavior is correct
25500** if the appliation uses the newer Native Posix Thread Library (NPTL)
25501** on linux - with NPTL a lock created by thread A can override locks
25502** in thread B.  But there is no way to know at compile-time which
25503** threading library is being used.  So there is no way to know at
25504** compile-time whether or not thread A can override locks on thread B.
25505** One has to do a run-time check to discover the behavior of the
25506** current process.
25507**
25508** SQLite used to support LinuxThreads.  But support for LinuxThreads
25509** was dropped beginning with version 3.7.0.  SQLite will still work with
25510** LinuxThreads provided that (1) there is no more than one connection
25511** per database file in the same process and (2) database connections
25512** do not move across threads.
25513*/
25514
25515/*
25516** An instance of the following structure serves as the key used
25517** to locate a particular unixInodeInfo object.
25518*/
25519struct unixFileId {
25520  dev_t dev;                  /* Device number */
25521#if OS_VXWORKS
25522  struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
25523#else
25524  ino_t ino;                  /* Inode number */
25525#endif
25526};
25527
25528/*
25529** An instance of the following structure is allocated for each open
25530** inode.  Or, on LinuxThreads, there is one of these structures for
25531** each inode opened by each thread.
25532**
25533** A single inode can have multiple file descriptors, so each unixFile
25534** structure contains a pointer to an instance of this object and this
25535** object keeps a count of the number of unixFile pointing to it.
25536*/
25537struct unixInodeInfo {
25538  struct unixFileId fileId;       /* The lookup key */
25539  int nShared;                    /* Number of SHARED locks held */
25540  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
25541  unsigned char bProcessLock;     /* An exclusive process lock is held */
25542  int nRef;                       /* Number of pointers to this structure */
25543  unixShmNode *pShmNode;          /* Shared memory associated with this inode */
25544  int nLock;                      /* Number of outstanding file locks */
25545  UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
25546  unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
25547  unixInodeInfo *pPrev;           /*    .... doubly linked */
25548#if SQLITE_ENABLE_LOCKING_STYLE
25549  unsigned long long sharedByte;  /* for AFP simulated shared lock */
25550#endif
25551#if OS_VXWORKS
25552  sem_t *pSem;                    /* Named POSIX semaphore */
25553  char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
25554#endif
25555};
25556
25557/*
25558** A lists of all unixInodeInfo objects.
25559*/
25560static unixInodeInfo *inodeList = 0;
25561
25562/*
25563**
25564** This function - unixLogError_x(), is only ever called via the macro
25565** unixLogError().
25566**
25567** It is invoked after an error occurs in an OS function and errno has been
25568** set. It logs a message using sqlite3_log() containing the current value of
25569** errno and, if possible, the human-readable equivalent from strerror() or
25570** strerror_r().
25571**
25572** The first argument passed to the macro should be the error code that
25573** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
25574** The two subsequent arguments should be the name of the OS function that
25575** failed (e.g. "unlink", "open") and the the associated file-system path,
25576** if any.
25577*/
25578#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
25579static int unixLogErrorAtLine(
25580  int errcode,                    /* SQLite error code */
25581  const char *zFunc,              /* Name of OS function that failed */
25582  const char *zPath,              /* File path associated with error */
25583  int iLine                       /* Source line number where error occurred */
25584){
25585  char *zErr;                     /* Message from strerror() or equivalent */
25586  int iErrno = errno;             /* Saved syscall error number */
25587
25588  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
25589  ** the strerror() function to obtain the human-readable error message
25590  ** equivalent to errno. Otherwise, use strerror_r().
25591  */
25592#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
25593  char aErr[80];
25594  memset(aErr, 0, sizeof(aErr));
25595  zErr = aErr;
25596
25597  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
25598  ** assume that the system provides the the GNU version of strerror_r() that
25599  ** returns a pointer to a buffer containing the error message. That pointer
25600  ** may point to aErr[], or it may point to some static storage somewhere.
25601  ** Otherwise, assume that the system provides the POSIX version of
25602  ** strerror_r(), which always writes an error message into aErr[].
25603  **
25604  ** If the code incorrectly assumes that it is the POSIX version that is
25605  ** available, the error message will often be an empty string. Not a
25606  ** huge problem. Incorrectly concluding that the GNU version is available
25607  ** could lead to a segfault though.
25608  */
25609#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
25610  zErr =
25611# endif
25612  strerror_r(iErrno, aErr, sizeof(aErr)-1);
25613
25614#elif SQLITE_THREADSAFE
25615  /* This is a threadsafe build, but strerror_r() is not available. */
25616  zErr = "";
25617#else
25618  /* Non-threadsafe build, use strerror(). */
25619  zErr = strerror(iErrno);
25620#endif
25621
25622  assert( errcode!=SQLITE_OK );
25623  if( zPath==0 ) zPath = "";
25624  sqlite3_log(errcode,
25625      "os_unix.c:%d: (%d) %s(%s) - %s",
25626      iLine, iErrno, zFunc, zPath, zErr
25627  );
25628
25629  return errcode;
25630}
25631
25632/*
25633** Close a file descriptor.
25634**
25635** We assume that close() almost always works, since it is only in a
25636** very sick application or on a very sick platform that it might fail.
25637** If it does fail, simply leak the file descriptor, but do log the
25638** error.
25639**
25640** Note that it is not safe to retry close() after EINTR since the
25641** file descriptor might have already been reused by another thread.
25642** So we don't even try to recover from an EINTR.  Just log the error
25643** and move on.
25644*/
25645static void robust_close(unixFile *pFile, int h, int lineno){
25646  if( osClose(h) ){
25647    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
25648                       pFile ? pFile->zPath : 0, lineno);
25649  }
25650}
25651
25652/*
25653** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
25654*/
25655static void closePendingFds(unixFile *pFile){
25656  unixInodeInfo *pInode = pFile->pInode;
25657  UnixUnusedFd *p;
25658  UnixUnusedFd *pNext;
25659  for(p=pInode->pUnused; p; p=pNext){
25660    pNext = p->pNext;
25661    robust_close(pFile, p->fd, __LINE__);
25662    sqlite3_free(p);
25663  }
25664  pInode->pUnused = 0;
25665}
25666
25667/*
25668** Release a unixInodeInfo structure previously allocated by findInodeInfo().
25669**
25670** The mutex entered using the unixEnterMutex() function must be held
25671** when this function is called.
25672*/
25673static void releaseInodeInfo(unixFile *pFile){
25674  unixInodeInfo *pInode = pFile->pInode;
25675  assert( unixMutexHeld() );
25676  if( ALWAYS(pInode) ){
25677    pInode->nRef--;
25678    if( pInode->nRef==0 ){
25679      assert( pInode->pShmNode==0 );
25680      closePendingFds(pFile);
25681      if( pInode->pPrev ){
25682        assert( pInode->pPrev->pNext==pInode );
25683        pInode->pPrev->pNext = pInode->pNext;
25684      }else{
25685        assert( inodeList==pInode );
25686        inodeList = pInode->pNext;
25687      }
25688      if( pInode->pNext ){
25689        assert( pInode->pNext->pPrev==pInode );
25690        pInode->pNext->pPrev = pInode->pPrev;
25691      }
25692      sqlite3_free(pInode);
25693    }
25694  }
25695}
25696
25697/*
25698** Given a file descriptor, locate the unixInodeInfo object that
25699** describes that file descriptor.  Create a new one if necessary.  The
25700** return value might be uninitialized if an error occurs.
25701**
25702** The mutex entered using the unixEnterMutex() function must be held
25703** when this function is called.
25704**
25705** Return an appropriate error code.
25706*/
25707static int findInodeInfo(
25708  unixFile *pFile,               /* Unix file with file desc used in the key */
25709  unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
25710){
25711  int rc;                        /* System call return code */
25712  int fd;                        /* The file descriptor for pFile */
25713  struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
25714  struct stat statbuf;           /* Low-level file information */
25715  unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
25716
25717  assert( unixMutexHeld() );
25718
25719  /* Get low-level information about the file that we can used to
25720  ** create a unique name for the file.
25721  */
25722  fd = pFile->h;
25723  rc = osFstat(fd, &statbuf);
25724  if( rc!=0 ){
25725    pFile->lastErrno = errno;
25726#ifdef EOVERFLOW
25727    if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
25728#endif
25729    return SQLITE_IOERR;
25730  }
25731
25732#ifdef __APPLE__
25733  /* On OS X on an msdos filesystem, the inode number is reported
25734  ** incorrectly for zero-size files.  See ticket #3260.  To work
25735  ** around this problem (we consider it a bug in OS X, not SQLite)
25736  ** we always increase the file size to 1 by writing a single byte
25737  ** prior to accessing the inode number.  The one byte written is
25738  ** an ASCII 'S' character which also happens to be the first byte
25739  ** in the header of every SQLite database.  In this way, if there
25740  ** is a race condition such that another thread has already populated
25741  ** the first page of the database, no damage is done.
25742  */
25743  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
25744    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
25745    if( rc!=1 ){
25746      pFile->lastErrno = errno;
25747      return SQLITE_IOERR;
25748    }
25749    rc = osFstat(fd, &statbuf);
25750    if( rc!=0 ){
25751      pFile->lastErrno = errno;
25752      return SQLITE_IOERR;
25753    }
25754  }
25755#endif
25756
25757  memset(&fileId, 0, sizeof(fileId));
25758  fileId.dev = statbuf.st_dev;
25759#if OS_VXWORKS
25760  fileId.pId = pFile->pId;
25761#else
25762  fileId.ino = statbuf.st_ino;
25763#endif
25764  pInode = inodeList;
25765  while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
25766    pInode = pInode->pNext;
25767  }
25768  if( pInode==0 ){
25769    pInode = sqlite3_malloc( sizeof(*pInode) );
25770    if( pInode==0 ){
25771      return SQLITE_NOMEM;
25772    }
25773    memset(pInode, 0, sizeof(*pInode));
25774    memcpy(&pInode->fileId, &fileId, sizeof(fileId));
25775    pInode->nRef = 1;
25776    pInode->pNext = inodeList;
25777    pInode->pPrev = 0;
25778    if( inodeList ) inodeList->pPrev = pInode;
25779    inodeList = pInode;
25780  }else{
25781    pInode->nRef++;
25782  }
25783  *ppInode = pInode;
25784  return SQLITE_OK;
25785}
25786
25787
25788/*
25789** This routine checks if there is a RESERVED lock held on the specified
25790** file by this or any other process. If such a lock is held, set *pResOut
25791** to a non-zero value otherwise *pResOut is set to zero.  The return value
25792** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25793*/
25794static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
25795  int rc = SQLITE_OK;
25796  int reserved = 0;
25797  unixFile *pFile = (unixFile*)id;
25798
25799  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25800
25801  assert( pFile );
25802  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
25803
25804  /* Check if a thread in this process holds such a lock */
25805  if( pFile->pInode->eFileLock>SHARED_LOCK ){
25806    reserved = 1;
25807  }
25808
25809  /* Otherwise see if some other process holds it.
25810  */
25811#ifndef __DJGPP__
25812  if( !reserved && !pFile->pInode->bProcessLock ){
25813    struct flock lock;
25814    lock.l_whence = SEEK_SET;
25815    lock.l_start = RESERVED_BYTE;
25816    lock.l_len = 1;
25817    lock.l_type = F_WRLCK;
25818    if( osFcntl(pFile->h, F_GETLK, &lock) ){
25819      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
25820      pFile->lastErrno = errno;
25821    } else if( lock.l_type!=F_UNLCK ){
25822      reserved = 1;
25823    }
25824  }
25825#endif
25826
25827  unixLeaveMutex();
25828  OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
25829
25830  *pResOut = reserved;
25831  return rc;
25832}
25833
25834/*
25835** Attempt to set a system-lock on the file pFile.  The lock is
25836** described by pLock.
25837**
25838** If the pFile was opened read/write from unix-excl, then the only lock
25839** ever obtained is an exclusive lock, and it is obtained exactly once
25840** the first time any lock is attempted.  All subsequent system locking
25841** operations become no-ops.  Locking operations still happen internally,
25842** in order to coordinate access between separate database connections
25843** within this process, but all of that is handled in memory and the
25844** operating system does not participate.
25845**
25846** This function is a pass-through to fcntl(F_SETLK) if pFile is using
25847** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
25848** and is read-only.
25849**
25850** Zero is returned if the call completes successfully, or -1 if a call
25851** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
25852*/
25853static int unixFileLock(unixFile *pFile, struct flock *pLock){
25854  int rc;
25855  unixInodeInfo *pInode = pFile->pInode;
25856  assert( unixMutexHeld() );
25857  assert( pInode!=0 );
25858  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
25859   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
25860  ){
25861    if( pInode->bProcessLock==0 ){
25862      struct flock lock;
25863      assert( pInode->nLock==0 );
25864      lock.l_whence = SEEK_SET;
25865      lock.l_start = SHARED_FIRST;
25866      lock.l_len = SHARED_SIZE;
25867      lock.l_type = F_WRLCK;
25868      rc = osFcntl(pFile->h, F_SETLK, &lock);
25869      if( rc<0 ) return rc;
25870      pInode->bProcessLock = 1;
25871      pInode->nLock++;
25872    }else{
25873      rc = 0;
25874    }
25875  }else{
25876    rc = osFcntl(pFile->h, F_SETLK, pLock);
25877  }
25878  return rc;
25879}
25880
25881/*
25882** Lock the file with the lock specified by parameter eFileLock - one
25883** of the following:
25884**
25885**     (1) SHARED_LOCK
25886**     (2) RESERVED_LOCK
25887**     (3) PENDING_LOCK
25888**     (4) EXCLUSIVE_LOCK
25889**
25890** Sometimes when requesting one lock state, additional lock states
25891** are inserted in between.  The locking might fail on one of the later
25892** transitions leaving the lock state different from what it started but
25893** still short of its goal.  The following chart shows the allowed
25894** transitions and the inserted intermediate states:
25895**
25896**    UNLOCKED -> SHARED
25897**    SHARED -> RESERVED
25898**    SHARED -> (PENDING) -> EXCLUSIVE
25899**    RESERVED -> (PENDING) -> EXCLUSIVE
25900**    PENDING -> EXCLUSIVE
25901**
25902** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25903** routine to lower a locking level.
25904*/
25905static int unixLock(sqlite3_file *id, int eFileLock){
25906  /* The following describes the implementation of the various locks and
25907  ** lock transitions in terms of the POSIX advisory shared and exclusive
25908  ** lock primitives (called read-locks and write-locks below, to avoid
25909  ** confusion with SQLite lock names). The algorithms are complicated
25910  ** slightly in order to be compatible with windows systems simultaneously
25911  ** accessing the same database file, in case that is ever required.
25912  **
25913  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
25914  ** byte', each single bytes at well known offsets, and the 'shared byte
25915  ** range', a range of 510 bytes at a well known offset.
25916  **
25917  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
25918  ** byte'.  If this is successful, a random byte from the 'shared byte
25919  ** range' is read-locked and the lock on the 'pending byte' released.
25920  **
25921  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
25922  ** A RESERVED lock is implemented by grabbing a write-lock on the
25923  ** 'reserved byte'.
25924  **
25925  ** A process may only obtain a PENDING lock after it has obtained a
25926  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
25927  ** on the 'pending byte'. This ensures that no new SHARED locks can be
25928  ** obtained, but existing SHARED locks are allowed to persist. A process
25929  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
25930  ** This property is used by the algorithm for rolling back a journal file
25931  ** after a crash.
25932  **
25933  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
25934  ** implemented by obtaining a write-lock on the entire 'shared byte
25935  ** range'. Since all other locks require a read-lock on one of the bytes
25936  ** within this range, this ensures that no other locks are held on the
25937  ** database.
25938  **
25939  ** The reason a single byte cannot be used instead of the 'shared byte
25940  ** range' is that some versions of windows do not support read-locks. By
25941  ** locking a random byte from a range, concurrent SHARED locks may exist
25942  ** even if the locking primitive used is always a write-lock.
25943  */
25944  int rc = SQLITE_OK;
25945  unixFile *pFile = (unixFile*)id;
25946  unixInodeInfo *pInode = pFile->pInode;
25947  struct flock lock;
25948  int tErrno = 0;
25949
25950  assert( pFile );
25951  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25952      azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25953      azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
25954
25955  /* If there is already a lock of this type or more restrictive on the
25956  ** unixFile, do nothing. Don't use the end_lock: exit path, as
25957  ** unixEnterMutex() hasn't been called yet.
25958  */
25959  if( pFile->eFileLock>=eFileLock ){
25960    OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
25961            azFileLock(eFileLock)));
25962    return SQLITE_OK;
25963  }
25964
25965  /* Make sure the locking sequence is correct.
25966  **  (1) We never move from unlocked to anything higher than shared lock.
25967  **  (2) SQLite never explicitly requests a pendig lock.
25968  **  (3) A shared lock is always held when a reserve lock is requested.
25969  */
25970  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
25971  assert( eFileLock!=PENDING_LOCK );
25972  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
25973
25974  /* This mutex is needed because pFile->pInode is shared across threads
25975  */
25976  unixEnterMutex();
25977  pInode = pFile->pInode;
25978
25979  /* If some thread using this PID has a lock via a different unixFile*
25980  ** handle that precludes the requested lock, return BUSY.
25981  */
25982  if( (pFile->eFileLock!=pInode->eFileLock &&
25983          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
25984  ){
25985    rc = SQLITE_BUSY;
25986    goto end_lock;
25987  }
25988
25989  /* If a SHARED lock is requested, and some thread using this PID already
25990  ** has a SHARED or RESERVED lock, then increment reference counts and
25991  ** return SQLITE_OK.
25992  */
25993  if( eFileLock==SHARED_LOCK &&
25994      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
25995    assert( eFileLock==SHARED_LOCK );
25996    assert( pFile->eFileLock==0 );
25997    assert( pInode->nShared>0 );
25998    pFile->eFileLock = SHARED_LOCK;
25999    pInode->nShared++;
26000    pInode->nLock++;
26001    goto end_lock;
26002  }
26003
26004
26005  /* A PENDING lock is needed before acquiring a SHARED lock and before
26006  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
26007  ** be released.
26008  */
26009  lock.l_len = 1L;
26010  lock.l_whence = SEEK_SET;
26011  if( eFileLock==SHARED_LOCK
26012      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
26013  ){
26014    lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
26015    lock.l_start = PENDING_BYTE;
26016    if( unixFileLock(pFile, &lock) ){
26017      tErrno = errno;
26018      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26019      if( rc!=SQLITE_BUSY ){
26020        pFile->lastErrno = tErrno;
26021      }
26022      goto end_lock;
26023    }
26024  }
26025
26026
26027  /* If control gets to this point, then actually go ahead and make
26028  ** operating system calls for the specified lock.
26029  */
26030  if( eFileLock==SHARED_LOCK ){
26031    assert( pInode->nShared==0 );
26032    assert( pInode->eFileLock==0 );
26033    assert( rc==SQLITE_OK );
26034
26035    /* Now get the read-lock */
26036    lock.l_start = SHARED_FIRST;
26037    lock.l_len = SHARED_SIZE;
26038    if( unixFileLock(pFile, &lock) ){
26039      tErrno = errno;
26040      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26041    }
26042
26043    /* Drop the temporary PENDING lock */
26044    lock.l_start = PENDING_BYTE;
26045    lock.l_len = 1L;
26046    lock.l_type = F_UNLCK;
26047    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
26048      /* This could happen with a network mount */
26049      tErrno = errno;
26050      rc = SQLITE_IOERR_UNLOCK;
26051    }
26052
26053    if( rc ){
26054      if( rc!=SQLITE_BUSY ){
26055        pFile->lastErrno = tErrno;
26056      }
26057      goto end_lock;
26058    }else{
26059      pFile->eFileLock = SHARED_LOCK;
26060      pInode->nLock++;
26061      pInode->nShared = 1;
26062    }
26063  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
26064    /* We are trying for an exclusive lock but another thread in this
26065    ** same process is still holding a shared lock. */
26066    rc = SQLITE_BUSY;
26067  }else{
26068    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
26069    ** assumed that there is a SHARED or greater lock on the file
26070    ** already.
26071    */
26072    assert( 0!=pFile->eFileLock );
26073    lock.l_type = F_WRLCK;
26074
26075    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
26076    if( eFileLock==RESERVED_LOCK ){
26077      lock.l_start = RESERVED_BYTE;
26078      lock.l_len = 1L;
26079    }else{
26080      lock.l_start = SHARED_FIRST;
26081      lock.l_len = SHARED_SIZE;
26082    }
26083
26084    if( unixFileLock(pFile, &lock) ){
26085      tErrno = errno;
26086      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26087      if( rc!=SQLITE_BUSY ){
26088        pFile->lastErrno = tErrno;
26089      }
26090    }
26091  }
26092
26093
26094#ifndef NDEBUG
26095  /* Set up the transaction-counter change checking flags when
26096  ** transitioning from a SHARED to a RESERVED lock.  The change
26097  ** from SHARED to RESERVED marks the beginning of a normal
26098  ** write operation (not a hot journal rollback).
26099  */
26100  if( rc==SQLITE_OK
26101   && pFile->eFileLock<=SHARED_LOCK
26102   && eFileLock==RESERVED_LOCK
26103  ){
26104    pFile->transCntrChng = 0;
26105    pFile->dbUpdate = 0;
26106    pFile->inNormalWrite = 1;
26107  }
26108#endif
26109
26110
26111  if( rc==SQLITE_OK ){
26112    pFile->eFileLock = eFileLock;
26113    pInode->eFileLock = eFileLock;
26114  }else if( eFileLock==EXCLUSIVE_LOCK ){
26115    pFile->eFileLock = PENDING_LOCK;
26116    pInode->eFileLock = PENDING_LOCK;
26117  }
26118
26119end_lock:
26120  unixLeaveMutex();
26121  OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
26122      rc==SQLITE_OK ? "ok" : "failed"));
26123  return rc;
26124}
26125
26126/*
26127** Add the file descriptor used by file handle pFile to the corresponding
26128** pUnused list.
26129*/
26130static void setPendingFd(unixFile *pFile){
26131  unixInodeInfo *pInode = pFile->pInode;
26132  UnixUnusedFd *p = pFile->pUnused;
26133  p->pNext = pInode->pUnused;
26134  pInode->pUnused = p;
26135  pFile->h = -1;
26136  pFile->pUnused = 0;
26137}
26138
26139/*
26140** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26141** must be either NO_LOCK or SHARED_LOCK.
26142**
26143** If the locking level of the file descriptor is already at or below
26144** the requested locking level, this routine is a no-op.
26145**
26146** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
26147** the byte range is divided into 2 parts and the first part is unlocked then
26148** set to a read lock, then the other part is simply unlocked.  This works
26149** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
26150** remove the write lock on a region when a read lock is set.
26151*/
26152static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
26153  unixFile *pFile = (unixFile*)id;
26154  unixInodeInfo *pInode;
26155  struct flock lock;
26156  int rc = SQLITE_OK;
26157  int h;
26158
26159  assert( pFile );
26160  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
26161      pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26162      getpid()));
26163
26164  assert( eFileLock<=SHARED_LOCK );
26165  if( pFile->eFileLock<=eFileLock ){
26166    return SQLITE_OK;
26167  }
26168  unixEnterMutex();
26169  h = pFile->h;
26170  pInode = pFile->pInode;
26171  assert( pInode->nShared!=0 );
26172  if( pFile->eFileLock>SHARED_LOCK ){
26173    assert( pInode->eFileLock==pFile->eFileLock );
26174    SimulateIOErrorBenign(1);
26175    SimulateIOError( h=(-1) )
26176    SimulateIOErrorBenign(0);
26177
26178#ifndef NDEBUG
26179    /* When reducing a lock such that other processes can start
26180    ** reading the database file again, make sure that the
26181    ** transaction counter was updated if any part of the database
26182    ** file changed.  If the transaction counter is not updated,
26183    ** other connections to the same file might not realize that
26184    ** the file has changed and hence might not know to flush their
26185    ** cache.  The use of a stale cache can lead to database corruption.
26186    */
26187#if 0
26188    assert( pFile->inNormalWrite==0
26189         || pFile->dbUpdate==0
26190         || pFile->transCntrChng==1 );
26191#endif
26192    pFile->inNormalWrite = 0;
26193#endif
26194
26195    /* downgrading to a shared lock on NFS involves clearing the write lock
26196    ** before establishing the readlock - to avoid a race condition we downgrade
26197    ** the lock in 2 blocks, so that part of the range will be covered by a
26198    ** write lock until the rest is covered by a read lock:
26199    **  1:   [WWWWW]
26200    **  2:   [....W]
26201    **  3:   [RRRRW]
26202    **  4:   [RRRR.]
26203    */
26204    if( eFileLock==SHARED_LOCK ){
26205
26206#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
26207      (void)handleNFSUnlock;
26208      assert( handleNFSUnlock==0 );
26209#endif
26210#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26211      if( handleNFSUnlock ){
26212        int tErrno;               /* Error code from system call errors */
26213        off_t divSize = SHARED_SIZE - 1;
26214
26215        lock.l_type = F_UNLCK;
26216        lock.l_whence = SEEK_SET;
26217        lock.l_start = SHARED_FIRST;
26218        lock.l_len = divSize;
26219        if( unixFileLock(pFile, &lock)==(-1) ){
26220          tErrno = errno;
26221          rc = SQLITE_IOERR_UNLOCK;
26222          if( IS_LOCK_ERROR(rc) ){
26223            pFile->lastErrno = tErrno;
26224          }
26225          goto end_unlock;
26226        }
26227        lock.l_type = F_RDLCK;
26228        lock.l_whence = SEEK_SET;
26229        lock.l_start = SHARED_FIRST;
26230        lock.l_len = divSize;
26231        if( unixFileLock(pFile, &lock)==(-1) ){
26232          tErrno = errno;
26233          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
26234          if( IS_LOCK_ERROR(rc) ){
26235            pFile->lastErrno = tErrno;
26236          }
26237          goto end_unlock;
26238        }
26239        lock.l_type = F_UNLCK;
26240        lock.l_whence = SEEK_SET;
26241        lock.l_start = SHARED_FIRST+divSize;
26242        lock.l_len = SHARED_SIZE-divSize;
26243        if( unixFileLock(pFile, &lock)==(-1) ){
26244          tErrno = errno;
26245          rc = SQLITE_IOERR_UNLOCK;
26246          if( IS_LOCK_ERROR(rc) ){
26247            pFile->lastErrno = tErrno;
26248          }
26249          goto end_unlock;
26250        }
26251      }else
26252#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26253      {
26254        lock.l_type = F_RDLCK;
26255        lock.l_whence = SEEK_SET;
26256        lock.l_start = SHARED_FIRST;
26257        lock.l_len = SHARED_SIZE;
26258        if( unixFileLock(pFile, &lock) ){
26259          /* In theory, the call to unixFileLock() cannot fail because another
26260          ** process is holding an incompatible lock. If it does, this
26261          ** indicates that the other process is not following the locking
26262          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
26263          ** SQLITE_BUSY would confuse the upper layer (in practice it causes
26264          ** an assert to fail). */
26265          rc = SQLITE_IOERR_RDLOCK;
26266          pFile->lastErrno = errno;
26267          goto end_unlock;
26268        }
26269      }
26270    }
26271    lock.l_type = F_UNLCK;
26272    lock.l_whence = SEEK_SET;
26273    lock.l_start = PENDING_BYTE;
26274    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
26275    if( unixFileLock(pFile, &lock)==0 ){
26276      pInode->eFileLock = SHARED_LOCK;
26277    }else{
26278      rc = SQLITE_IOERR_UNLOCK;
26279      pFile->lastErrno = errno;
26280      goto end_unlock;
26281    }
26282  }
26283  if( eFileLock==NO_LOCK ){
26284    /* Decrement the shared lock counter.  Release the lock using an
26285    ** OS call only when all threads in this same process have released
26286    ** the lock.
26287    */
26288    pInode->nShared--;
26289    if( pInode->nShared==0 ){
26290      lock.l_type = F_UNLCK;
26291      lock.l_whence = SEEK_SET;
26292      lock.l_start = lock.l_len = 0L;
26293      SimulateIOErrorBenign(1);
26294      SimulateIOError( h=(-1) )
26295      SimulateIOErrorBenign(0);
26296      if( unixFileLock(pFile, &lock)==0 ){
26297        pInode->eFileLock = NO_LOCK;
26298      }else{
26299        rc = SQLITE_IOERR_UNLOCK;
26300	pFile->lastErrno = errno;
26301        pInode->eFileLock = NO_LOCK;
26302        pFile->eFileLock = NO_LOCK;
26303      }
26304    }
26305
26306    /* Decrement the count of locks against this same file.  When the
26307    ** count reaches zero, close any other file descriptors whose close
26308    ** was deferred because of outstanding locks.
26309    */
26310    pInode->nLock--;
26311    assert( pInode->nLock>=0 );
26312    if( pInode->nLock==0 ){
26313      closePendingFds(pFile);
26314    }
26315  }
26316
26317end_unlock:
26318  unixLeaveMutex();
26319  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
26320  return rc;
26321}
26322
26323/*
26324** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26325** must be either NO_LOCK or SHARED_LOCK.
26326**
26327** If the locking level of the file descriptor is already at or below
26328** the requested locking level, this routine is a no-op.
26329*/
26330static int unixUnlock(sqlite3_file *id, int eFileLock){
26331  return posixUnlock(id, eFileLock, 0);
26332}
26333
26334/*
26335** This function performs the parts of the "close file" operation
26336** common to all locking schemes. It closes the directory and file
26337** handles, if they are valid, and sets all fields of the unixFile
26338** structure to 0.
26339**
26340** It is *not* necessary to hold the mutex when this routine is called,
26341** even on VxWorks.  A mutex will be acquired on VxWorks by the
26342** vxworksReleaseFileId() routine.
26343*/
26344static int closeUnixFile(sqlite3_file *id){
26345  unixFile *pFile = (unixFile*)id;
26346  if( pFile->h>=0 ){
26347    robust_close(pFile, pFile->h, __LINE__);
26348    pFile->h = -1;
26349  }
26350#if OS_VXWORKS
26351  if( pFile->pId ){
26352    if( pFile->isDelete ){
26353      osUnlink(pFile->pId->zCanonicalName);
26354    }
26355    vxworksReleaseFileId(pFile->pId);
26356    pFile->pId = 0;
26357  }
26358#endif
26359  OSTRACE(("CLOSE   %-3d\n", pFile->h));
26360  OpenCounter(-1);
26361  sqlite3_free(pFile->pUnused);
26362  memset(pFile, 0, sizeof(unixFile));
26363  return SQLITE_OK;
26364}
26365
26366/*
26367** Close a file.
26368*/
26369static int unixClose(sqlite3_file *id){
26370  int rc = SQLITE_OK;
26371  unixFile *pFile = (unixFile *)id;
26372  unixUnlock(id, NO_LOCK);
26373  unixEnterMutex();
26374
26375  /* unixFile.pInode is always valid here. Otherwise, a different close
26376  ** routine (e.g. nolockClose()) would be called instead.
26377  */
26378  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
26379  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
26380    /* If there are outstanding locks, do not actually close the file just
26381    ** yet because that would clear those locks.  Instead, add the file
26382    ** descriptor to pInode->pUnused list.  It will be automatically closed
26383    ** when the last lock is cleared.
26384    */
26385    setPendingFd(pFile);
26386  }
26387  releaseInodeInfo(pFile);
26388  rc = closeUnixFile(id);
26389  unixLeaveMutex();
26390  return rc;
26391}
26392
26393/************** End of the posix advisory lock implementation *****************
26394******************************************************************************/
26395
26396/******************************************************************************
26397****************************** No-op Locking **********************************
26398**
26399** Of the various locking implementations available, this is by far the
26400** simplest:  locking is ignored.  No attempt is made to lock the database
26401** file for reading or writing.
26402**
26403** This locking mode is appropriate for use on read-only databases
26404** (ex: databases that are burned into CD-ROM, for example.)  It can
26405** also be used if the application employs some external mechanism to
26406** prevent simultaneous access of the same database by two or more
26407** database connections.  But there is a serious risk of database
26408** corruption if this locking mode is used in situations where multiple
26409** database connections are accessing the same database file at the same
26410** time and one or more of those connections are writing.
26411*/
26412
26413static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
26414  UNUSED_PARAMETER(NotUsed);
26415  *pResOut = 0;
26416  return SQLITE_OK;
26417}
26418static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
26419  UNUSED_PARAMETER2(NotUsed, NotUsed2);
26420  return SQLITE_OK;
26421}
26422static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
26423  UNUSED_PARAMETER2(NotUsed, NotUsed2);
26424  return SQLITE_OK;
26425}
26426
26427/*
26428** Close the file.
26429*/
26430static int nolockClose(sqlite3_file *id) {
26431  return closeUnixFile(id);
26432}
26433
26434/******************* End of the no-op lock implementation *********************
26435******************************************************************************/
26436
26437/******************************************************************************
26438************************* Begin dot-file Locking ******************************
26439**
26440** The dotfile locking implementation uses the existance of separate lock
26441** files in order to control access to the database.  This works on just
26442** about every filesystem imaginable.  But there are serious downsides:
26443**
26444**    (1)  There is zero concurrency.  A single reader blocks all other
26445**         connections from reading or writing the database.
26446**
26447**    (2)  An application crash or power loss can leave stale lock files
26448**         sitting around that need to be cleared manually.
26449**
26450** Nevertheless, a dotlock is an appropriate locking mode for use if no
26451** other locking strategy is available.
26452**
26453** Dotfile locking works by creating a file in the same directory as the
26454** database and with the same name but with a ".lock" extension added.
26455** The existance of a lock file implies an EXCLUSIVE lock.  All other lock
26456** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
26457*/
26458
26459/*
26460** The file suffix added to the data base filename in order to create the
26461** lock file.
26462*/
26463#define DOTLOCK_SUFFIX ".lock"
26464
26465/*
26466** This routine checks if there is a RESERVED lock held on the specified
26467** file by this or any other process. If such a lock is held, set *pResOut
26468** to a non-zero value otherwise *pResOut is set to zero.  The return value
26469** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26470**
26471** In dotfile locking, either a lock exists or it does not.  So in this
26472** variation of CheckReservedLock(), *pResOut is set to true if any lock
26473** is held on the file and false if the file is unlocked.
26474*/
26475static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
26476  int rc = SQLITE_OK;
26477  int reserved = 0;
26478  unixFile *pFile = (unixFile*)id;
26479
26480  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26481
26482  assert( pFile );
26483
26484  /* Check if a thread in this process holds such a lock */
26485  if( pFile->eFileLock>SHARED_LOCK ){
26486    /* Either this connection or some other connection in the same process
26487    ** holds a lock on the file.  No need to check further. */
26488    reserved = 1;
26489  }else{
26490    /* The lock is held if and only if the lockfile exists */
26491    const char *zLockFile = (const char*)pFile->lockingContext;
26492    reserved = osAccess(zLockFile, 0)==0;
26493  }
26494  OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
26495  *pResOut = reserved;
26496  return rc;
26497}
26498
26499/*
26500** Lock the file with the lock specified by parameter eFileLock - one
26501** of the following:
26502**
26503**     (1) SHARED_LOCK
26504**     (2) RESERVED_LOCK
26505**     (3) PENDING_LOCK
26506**     (4) EXCLUSIVE_LOCK
26507**
26508** Sometimes when requesting one lock state, additional lock states
26509** are inserted in between.  The locking might fail on one of the later
26510** transitions leaving the lock state different from what it started but
26511** still short of its goal.  The following chart shows the allowed
26512** transitions and the inserted intermediate states:
26513**
26514**    UNLOCKED -> SHARED
26515**    SHARED -> RESERVED
26516**    SHARED -> (PENDING) -> EXCLUSIVE
26517**    RESERVED -> (PENDING) -> EXCLUSIVE
26518**    PENDING -> EXCLUSIVE
26519**
26520** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26521** routine to lower a locking level.
26522**
26523** With dotfile locking, we really only support state (4): EXCLUSIVE.
26524** But we track the other locking levels internally.
26525*/
26526static int dotlockLock(sqlite3_file *id, int eFileLock) {
26527  unixFile *pFile = (unixFile*)id;
26528  int fd;
26529  char *zLockFile = (char *)pFile->lockingContext;
26530  int rc = SQLITE_OK;
26531
26532
26533  /* If we have any lock, then the lock file already exists.  All we have
26534  ** to do is adjust our internal record of the lock level.
26535  */
26536  if( pFile->eFileLock > NO_LOCK ){
26537    pFile->eFileLock = eFileLock;
26538    /* Always update the timestamp on the old file */
26539#ifdef HAVE_UTIME
26540    utime(zLockFile, NULL);
26541#else
26542    utimes(zLockFile, NULL);
26543#endif
26544    return SQLITE_OK;
26545  }
26546
26547  /* grab an exclusive lock */
26548  fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
26549  if( fd<0 ){
26550    /* failed to open/create the file, someone else may have stolen the lock */
26551    int tErrno = errno;
26552    if( EEXIST == tErrno ){
26553      rc = SQLITE_BUSY;
26554    } else {
26555      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26556      if( IS_LOCK_ERROR(rc) ){
26557        pFile->lastErrno = tErrno;
26558      }
26559    }
26560    return rc;
26561  }
26562  robust_close(pFile, fd, __LINE__);
26563
26564  /* got it, set the type and return ok */
26565  pFile->eFileLock = eFileLock;
26566  return rc;
26567}
26568
26569/*
26570** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26571** must be either NO_LOCK or SHARED_LOCK.
26572**
26573** If the locking level of the file descriptor is already at or below
26574** the requested locking level, this routine is a no-op.
26575**
26576** When the locking level reaches NO_LOCK, delete the lock file.
26577*/
26578static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
26579  unixFile *pFile = (unixFile*)id;
26580  char *zLockFile = (char *)pFile->lockingContext;
26581
26582  assert( pFile );
26583  OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
26584	   pFile->eFileLock, getpid()));
26585  assert( eFileLock<=SHARED_LOCK );
26586
26587  /* no-op if possible */
26588  if( pFile->eFileLock==eFileLock ){
26589    return SQLITE_OK;
26590  }
26591
26592  /* To downgrade to shared, simply update our internal notion of the
26593  ** lock state.  No need to mess with the file on disk.
26594  */
26595  if( eFileLock==SHARED_LOCK ){
26596    pFile->eFileLock = SHARED_LOCK;
26597    return SQLITE_OK;
26598  }
26599
26600  /* To fully unlock the database, delete the lock file */
26601  assert( eFileLock==NO_LOCK );
26602  if( osUnlink(zLockFile) ){
26603    int rc = 0;
26604    int tErrno = errno;
26605    if( ENOENT != tErrno ){
26606      rc = SQLITE_IOERR_UNLOCK;
26607    }
26608    if( IS_LOCK_ERROR(rc) ){
26609      pFile->lastErrno = tErrno;
26610    }
26611    return rc;
26612  }
26613  pFile->eFileLock = NO_LOCK;
26614  return SQLITE_OK;
26615}
26616
26617/*
26618** Close a file.  Make sure the lock has been released before closing.
26619*/
26620static int dotlockClose(sqlite3_file *id) {
26621  int rc;
26622  if( id ){
26623    unixFile *pFile = (unixFile*)id;
26624    dotlockUnlock(id, NO_LOCK);
26625    sqlite3_free(pFile->lockingContext);
26626  }
26627  rc = closeUnixFile(id);
26628  return rc;
26629}
26630/****************** End of the dot-file lock implementation *******************
26631******************************************************************************/
26632
26633/******************************************************************************
26634************************** Begin flock Locking ********************************
26635**
26636** Use the flock() system call to do file locking.
26637**
26638** flock() locking is like dot-file locking in that the various
26639** fine-grain locking levels supported by SQLite are collapsed into
26640** a single exclusive lock.  In other words, SHARED, RESERVED, and
26641** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
26642** still works when you do this, but concurrency is reduced since
26643** only a single process can be reading the database at a time.
26644**
26645** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
26646** compiling for VXWORKS.
26647*/
26648#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
26649
26650/*
26651** Retry flock() calls that fail with EINTR
26652*/
26653#ifdef EINTR
26654static int robust_flock(int fd, int op){
26655  int rc;
26656  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
26657  return rc;
26658}
26659#else
26660# define robust_flock(a,b) flock(a,b)
26661#endif
26662
26663
26664/*
26665** This routine checks if there is a RESERVED lock held on the specified
26666** file by this or any other process. If such a lock is held, set *pResOut
26667** to a non-zero value otherwise *pResOut is set to zero.  The return value
26668** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26669*/
26670static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
26671  int rc = SQLITE_OK;
26672  int reserved = 0;
26673  unixFile *pFile = (unixFile*)id;
26674
26675  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26676
26677  assert( pFile );
26678
26679  /* Check if a thread in this process holds such a lock */
26680  if( pFile->eFileLock>SHARED_LOCK ){
26681    reserved = 1;
26682  }
26683
26684  /* Otherwise see if some other process holds it. */
26685  if( !reserved ){
26686    /* attempt to get the lock */
26687    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
26688    if( !lrc ){
26689      /* got the lock, unlock it */
26690      lrc = robust_flock(pFile->h, LOCK_UN);
26691      if ( lrc ) {
26692        int tErrno = errno;
26693        /* unlock failed with an error */
26694        lrc = SQLITE_IOERR_UNLOCK;
26695        if( IS_LOCK_ERROR(lrc) ){
26696          pFile->lastErrno = tErrno;
26697          rc = lrc;
26698        }
26699      }
26700    } else {
26701      int tErrno = errno;
26702      reserved = 1;
26703      /* someone else might have it reserved */
26704      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26705      if( IS_LOCK_ERROR(lrc) ){
26706        pFile->lastErrno = tErrno;
26707        rc = lrc;
26708      }
26709    }
26710  }
26711  OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
26712
26713#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26714  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26715    rc = SQLITE_OK;
26716    reserved=1;
26717  }
26718#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26719  *pResOut = reserved;
26720  return rc;
26721}
26722
26723/*
26724** Lock the file with the lock specified by parameter eFileLock - one
26725** of the following:
26726**
26727**     (1) SHARED_LOCK
26728**     (2) RESERVED_LOCK
26729**     (3) PENDING_LOCK
26730**     (4) EXCLUSIVE_LOCK
26731**
26732** Sometimes when requesting one lock state, additional lock states
26733** are inserted in between.  The locking might fail on one of the later
26734** transitions leaving the lock state different from what it started but
26735** still short of its goal.  The following chart shows the allowed
26736** transitions and the inserted intermediate states:
26737**
26738**    UNLOCKED -> SHARED
26739**    SHARED -> RESERVED
26740**    SHARED -> (PENDING) -> EXCLUSIVE
26741**    RESERVED -> (PENDING) -> EXCLUSIVE
26742**    PENDING -> EXCLUSIVE
26743**
26744** flock() only really support EXCLUSIVE locks.  We track intermediate
26745** lock states in the sqlite3_file structure, but all locks SHARED or
26746** above are really EXCLUSIVE locks and exclude all other processes from
26747** access the file.
26748**
26749** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26750** routine to lower a locking level.
26751*/
26752static int flockLock(sqlite3_file *id, int eFileLock) {
26753  int rc = SQLITE_OK;
26754  unixFile *pFile = (unixFile*)id;
26755
26756  assert( pFile );
26757
26758  /* if we already have a lock, it is exclusive.
26759  ** Just adjust level and punt on outta here. */
26760  if (pFile->eFileLock > NO_LOCK) {
26761    pFile->eFileLock = eFileLock;
26762    return SQLITE_OK;
26763  }
26764
26765  /* grab an exclusive lock */
26766
26767  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
26768    int tErrno = errno;
26769    /* didn't get, must be busy */
26770    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26771    if( IS_LOCK_ERROR(rc) ){
26772      pFile->lastErrno = tErrno;
26773    }
26774  } else {
26775    /* got it, set the type and return ok */
26776    pFile->eFileLock = eFileLock;
26777  }
26778  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
26779           rc==SQLITE_OK ? "ok" : "failed"));
26780#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26781  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26782    rc = SQLITE_BUSY;
26783  }
26784#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26785  return rc;
26786}
26787
26788
26789/*
26790** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26791** must be either NO_LOCK or SHARED_LOCK.
26792**
26793** If the locking level of the file descriptor is already at or below
26794** the requested locking level, this routine is a no-op.
26795*/
26796static int flockUnlock(sqlite3_file *id, int eFileLock) {
26797  unixFile *pFile = (unixFile*)id;
26798
26799  assert( pFile );
26800  OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
26801           pFile->eFileLock, getpid()));
26802  assert( eFileLock<=SHARED_LOCK );
26803
26804  /* no-op if possible */
26805  if( pFile->eFileLock==eFileLock ){
26806    return SQLITE_OK;
26807  }
26808
26809  /* shared can just be set because we always have an exclusive */
26810  if (eFileLock==SHARED_LOCK) {
26811    pFile->eFileLock = eFileLock;
26812    return SQLITE_OK;
26813  }
26814
26815  /* no, really, unlock. */
26816  if( robust_flock(pFile->h, LOCK_UN) ){
26817#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26818    return SQLITE_OK;
26819#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26820    return SQLITE_IOERR_UNLOCK;
26821  }else{
26822    pFile->eFileLock = NO_LOCK;
26823    return SQLITE_OK;
26824  }
26825}
26826
26827/*
26828** Close a file.
26829*/
26830static int flockClose(sqlite3_file *id) {
26831  if( id ){
26832    flockUnlock(id, NO_LOCK);
26833  }
26834  return closeUnixFile(id);
26835}
26836
26837#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
26838
26839/******************* End of the flock lock implementation *********************
26840******************************************************************************/
26841
26842/******************************************************************************
26843************************ Begin Named Semaphore Locking ************************
26844**
26845** Named semaphore locking is only supported on VxWorks.
26846**
26847** Semaphore locking is like dot-lock and flock in that it really only
26848** supports EXCLUSIVE locking.  Only a single process can read or write
26849** the database file at a time.  This reduces potential concurrency, but
26850** makes the lock implementation much easier.
26851*/
26852#if OS_VXWORKS
26853
26854/*
26855** This routine checks if there is a RESERVED lock held on the specified
26856** file by this or any other process. If such a lock is held, set *pResOut
26857** to a non-zero value otherwise *pResOut is set to zero.  The return value
26858** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26859*/
26860static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
26861  int rc = SQLITE_OK;
26862  int reserved = 0;
26863  unixFile *pFile = (unixFile*)id;
26864
26865  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26866
26867  assert( pFile );
26868
26869  /* Check if a thread in this process holds such a lock */
26870  if( pFile->eFileLock>SHARED_LOCK ){
26871    reserved = 1;
26872  }
26873
26874  /* Otherwise see if some other process holds it. */
26875  if( !reserved ){
26876    sem_t *pSem = pFile->pInode->pSem;
26877    struct stat statBuf;
26878
26879    if( sem_trywait(pSem)==-1 ){
26880      int tErrno = errno;
26881      if( EAGAIN != tErrno ){
26882        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
26883        pFile->lastErrno = tErrno;
26884      } else {
26885        /* someone else has the lock when we are in NO_LOCK */
26886        reserved = (pFile->eFileLock < SHARED_LOCK);
26887      }
26888    }else{
26889      /* we could have it if we want it */
26890      sem_post(pSem);
26891    }
26892  }
26893  OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
26894
26895  *pResOut = reserved;
26896  return rc;
26897}
26898
26899/*
26900** Lock the file with the lock specified by parameter eFileLock - one
26901** of the following:
26902**
26903**     (1) SHARED_LOCK
26904**     (2) RESERVED_LOCK
26905**     (3) PENDING_LOCK
26906**     (4) EXCLUSIVE_LOCK
26907**
26908** Sometimes when requesting one lock state, additional lock states
26909** are inserted in between.  The locking might fail on one of the later
26910** transitions leaving the lock state different from what it started but
26911** still short of its goal.  The following chart shows the allowed
26912** transitions and the inserted intermediate states:
26913**
26914**    UNLOCKED -> SHARED
26915**    SHARED -> RESERVED
26916**    SHARED -> (PENDING) -> EXCLUSIVE
26917**    RESERVED -> (PENDING) -> EXCLUSIVE
26918**    PENDING -> EXCLUSIVE
26919**
26920** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
26921** lock states in the sqlite3_file structure, but all locks SHARED or
26922** above are really EXCLUSIVE locks and exclude all other processes from
26923** access the file.
26924**
26925** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26926** routine to lower a locking level.
26927*/
26928static int semLock(sqlite3_file *id, int eFileLock) {
26929  unixFile *pFile = (unixFile*)id;
26930  int fd;
26931  sem_t *pSem = pFile->pInode->pSem;
26932  int rc = SQLITE_OK;
26933
26934  /* if we already have a lock, it is exclusive.
26935  ** Just adjust level and punt on outta here. */
26936  if (pFile->eFileLock > NO_LOCK) {
26937    pFile->eFileLock = eFileLock;
26938    rc = SQLITE_OK;
26939    goto sem_end_lock;
26940  }
26941
26942  /* lock semaphore now but bail out when already locked. */
26943  if( sem_trywait(pSem)==-1 ){
26944    rc = SQLITE_BUSY;
26945    goto sem_end_lock;
26946  }
26947
26948  /* got it, set the type and return ok */
26949  pFile->eFileLock = eFileLock;
26950
26951 sem_end_lock:
26952  return rc;
26953}
26954
26955/*
26956** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26957** must be either NO_LOCK or SHARED_LOCK.
26958**
26959** If the locking level of the file descriptor is already at or below
26960** the requested locking level, this routine is a no-op.
26961*/
26962static int semUnlock(sqlite3_file *id, int eFileLock) {
26963  unixFile *pFile = (unixFile*)id;
26964  sem_t *pSem = pFile->pInode->pSem;
26965
26966  assert( pFile );
26967  assert( pSem );
26968  OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
26969	   pFile->eFileLock, getpid()));
26970  assert( eFileLock<=SHARED_LOCK );
26971
26972  /* no-op if possible */
26973  if( pFile->eFileLock==eFileLock ){
26974    return SQLITE_OK;
26975  }
26976
26977  /* shared can just be set because we always have an exclusive */
26978  if (eFileLock==SHARED_LOCK) {
26979    pFile->eFileLock = eFileLock;
26980    return SQLITE_OK;
26981  }
26982
26983  /* no, really unlock. */
26984  if ( sem_post(pSem)==-1 ) {
26985    int rc, tErrno = errno;
26986    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
26987    if( IS_LOCK_ERROR(rc) ){
26988      pFile->lastErrno = tErrno;
26989    }
26990    return rc;
26991  }
26992  pFile->eFileLock = NO_LOCK;
26993  return SQLITE_OK;
26994}
26995
26996/*
26997 ** Close a file.
26998 */
26999static int semClose(sqlite3_file *id) {
27000  if( id ){
27001    unixFile *pFile = (unixFile*)id;
27002    semUnlock(id, NO_LOCK);
27003    assert( pFile );
27004    unixEnterMutex();
27005    releaseInodeInfo(pFile);
27006    unixLeaveMutex();
27007    closeUnixFile(id);
27008  }
27009  return SQLITE_OK;
27010}
27011
27012#endif /* OS_VXWORKS */
27013/*
27014** Named semaphore locking is only available on VxWorks.
27015**
27016*************** End of the named semaphore lock implementation ****************
27017******************************************************************************/
27018
27019
27020/******************************************************************************
27021*************************** Begin AFP Locking *********************************
27022**
27023** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
27024** on Apple Macintosh computers - both OS9 and OSX.
27025**
27026** Third-party implementations of AFP are available.  But this code here
27027** only works on OSX.
27028*/
27029
27030#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
27031/*
27032** The afpLockingContext structure contains all afp lock specific state
27033*/
27034typedef struct afpLockingContext afpLockingContext;
27035struct afpLockingContext {
27036  int reserved;
27037  const char *dbPath;             /* Name of the open file */
27038};
27039
27040struct ByteRangeLockPB2
27041{
27042  unsigned long long offset;        /* offset to first byte to lock */
27043  unsigned long long length;        /* nbr of bytes to lock */
27044  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
27045  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
27046  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
27047  int fd;                           /* file desc to assoc this lock with */
27048};
27049
27050#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
27051
27052/*
27053** This is a utility for setting or clearing a bit-range lock on an
27054** AFP filesystem.
27055**
27056** Return SQLITE_OK on success, SQLITE_BUSY on failure.
27057*/
27058static int afpSetLock(
27059  const char *path,              /* Name of the file to be locked or unlocked */
27060  unixFile *pFile,               /* Open file descriptor on path */
27061  unsigned long long offset,     /* First byte to be locked */
27062  unsigned long long length,     /* Number of bytes to lock */
27063  int setLockFlag                /* True to set lock.  False to clear lock */
27064){
27065  struct ByteRangeLockPB2 pb;
27066  int err;
27067
27068  pb.unLockFlag = setLockFlag ? 0 : 1;
27069  pb.startEndFlag = 0;
27070  pb.offset = offset;
27071  pb.length = length;
27072  pb.fd = pFile->h;
27073
27074  OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
27075    (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
27076    offset, length));
27077  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
27078  if ( err==-1 ) {
27079    int rc;
27080    int tErrno = errno;
27081    OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
27082             path, tErrno, strerror(tErrno)));
27083#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
27084    rc = SQLITE_BUSY;
27085#else
27086    rc = sqliteErrorFromPosixError(tErrno,
27087                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
27088#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
27089    if( IS_LOCK_ERROR(rc) ){
27090      pFile->lastErrno = tErrno;
27091    }
27092    return rc;
27093  } else {
27094    return SQLITE_OK;
27095  }
27096}
27097
27098/*
27099** This routine checks if there is a RESERVED lock held on the specified
27100** file by this or any other process. If such a lock is held, set *pResOut
27101** to a non-zero value otherwise *pResOut is set to zero.  The return value
27102** is set to SQLITE_OK unless an I/O error occurs during lock checking.
27103*/
27104static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
27105  int rc = SQLITE_OK;
27106  int reserved = 0;
27107  unixFile *pFile = (unixFile*)id;
27108  afpLockingContext *context;
27109
27110  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27111
27112  assert( pFile );
27113  context = (afpLockingContext *) pFile->lockingContext;
27114  if( context->reserved ){
27115    *pResOut = 1;
27116    return SQLITE_OK;
27117  }
27118  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
27119
27120  /* Check if a thread in this process holds such a lock */
27121  if( pFile->pInode->eFileLock>SHARED_LOCK ){
27122    reserved = 1;
27123  }
27124
27125  /* Otherwise see if some other process holds it.
27126   */
27127  if( !reserved ){
27128    /* lock the RESERVED byte */
27129    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
27130    if( SQLITE_OK==lrc ){
27131      /* if we succeeded in taking the reserved lock, unlock it to restore
27132      ** the original state */
27133      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
27134    } else {
27135      /* if we failed to get the lock then someone else must have it */
27136      reserved = 1;
27137    }
27138    if( IS_LOCK_ERROR(lrc) ){
27139      rc=lrc;
27140    }
27141  }
27142
27143  unixLeaveMutex();
27144  OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
27145
27146  *pResOut = reserved;
27147  return rc;
27148}
27149
27150/*
27151** Lock the file with the lock specified by parameter eFileLock - one
27152** of the following:
27153**
27154**     (1) SHARED_LOCK
27155**     (2) RESERVED_LOCK
27156**     (3) PENDING_LOCK
27157**     (4) EXCLUSIVE_LOCK
27158**
27159** Sometimes when requesting one lock state, additional lock states
27160** are inserted in between.  The locking might fail on one of the later
27161** transitions leaving the lock state different from what it started but
27162** still short of its goal.  The following chart shows the allowed
27163** transitions and the inserted intermediate states:
27164**
27165**    UNLOCKED -> SHARED
27166**    SHARED -> RESERVED
27167**    SHARED -> (PENDING) -> EXCLUSIVE
27168**    RESERVED -> (PENDING) -> EXCLUSIVE
27169**    PENDING -> EXCLUSIVE
27170**
27171** This routine will only increase a lock.  Use the sqlite3OsUnlock()
27172** routine to lower a locking level.
27173*/
27174static int afpLock(sqlite3_file *id, int eFileLock){
27175  int rc = SQLITE_OK;
27176  unixFile *pFile = (unixFile*)id;
27177  unixInodeInfo *pInode = pFile->pInode;
27178  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27179
27180  assert( pFile );
27181  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
27182           azFileLock(eFileLock), azFileLock(pFile->eFileLock),
27183           azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
27184
27185  /* If there is already a lock of this type or more restrictive on the
27186  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
27187  ** unixEnterMutex() hasn't been called yet.
27188  */
27189  if( pFile->eFileLock>=eFileLock ){
27190    OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
27191           azFileLock(eFileLock)));
27192    return SQLITE_OK;
27193  }
27194
27195  /* Make sure the locking sequence is correct
27196  **  (1) We never move from unlocked to anything higher than shared lock.
27197  **  (2) SQLite never explicitly requests a pendig lock.
27198  **  (3) A shared lock is always held when a reserve lock is requested.
27199  */
27200  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
27201  assert( eFileLock!=PENDING_LOCK );
27202  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
27203
27204  /* This mutex is needed because pFile->pInode is shared across threads
27205  */
27206  unixEnterMutex();
27207  pInode = pFile->pInode;
27208
27209  /* If some thread using this PID has a lock via a different unixFile*
27210  ** handle that precludes the requested lock, return BUSY.
27211  */
27212  if( (pFile->eFileLock!=pInode->eFileLock &&
27213       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
27214     ){
27215    rc = SQLITE_BUSY;
27216    goto afp_end_lock;
27217  }
27218
27219  /* If a SHARED lock is requested, and some thread using this PID already
27220  ** has a SHARED or RESERVED lock, then increment reference counts and
27221  ** return SQLITE_OK.
27222  */
27223  if( eFileLock==SHARED_LOCK &&
27224     (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
27225    assert( eFileLock==SHARED_LOCK );
27226    assert( pFile->eFileLock==0 );
27227    assert( pInode->nShared>0 );
27228    pFile->eFileLock = SHARED_LOCK;
27229    pInode->nShared++;
27230    pInode->nLock++;
27231    goto afp_end_lock;
27232  }
27233
27234  /* A PENDING lock is needed before acquiring a SHARED lock and before
27235  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
27236  ** be released.
27237  */
27238  if( eFileLock==SHARED_LOCK
27239      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
27240  ){
27241    int failed;
27242    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
27243    if (failed) {
27244      rc = failed;
27245      goto afp_end_lock;
27246    }
27247  }
27248
27249  /* If control gets to this point, then actually go ahead and make
27250  ** operating system calls for the specified lock.
27251  */
27252  if( eFileLock==SHARED_LOCK ){
27253    int lrc1, lrc2, lrc1Errno = 0;
27254    long lk, mask;
27255
27256    assert( pInode->nShared==0 );
27257    assert( pInode->eFileLock==0 );
27258
27259    mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
27260    /* Now get the read-lock SHARED_LOCK */
27261    /* note that the quality of the randomness doesn't matter that much */
27262    lk = random();
27263    pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
27264    lrc1 = afpSetLock(context->dbPath, pFile,
27265          SHARED_FIRST+pInode->sharedByte, 1, 1);
27266    if( IS_LOCK_ERROR(lrc1) ){
27267      lrc1Errno = pFile->lastErrno;
27268    }
27269    /* Drop the temporary PENDING lock */
27270    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
27271
27272    if( IS_LOCK_ERROR(lrc1) ) {
27273      pFile->lastErrno = lrc1Errno;
27274      rc = lrc1;
27275      goto afp_end_lock;
27276    } else if( IS_LOCK_ERROR(lrc2) ){
27277      rc = lrc2;
27278      goto afp_end_lock;
27279    } else if( lrc1 != SQLITE_OK ) {
27280      rc = lrc1;
27281    } else {
27282      pFile->eFileLock = SHARED_LOCK;
27283      pInode->nLock++;
27284      pInode->nShared = 1;
27285    }
27286  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
27287    /* We are trying for an exclusive lock but another thread in this
27288     ** same process is still holding a shared lock. */
27289    rc = SQLITE_BUSY;
27290  }else{
27291    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
27292    ** assumed that there is a SHARED or greater lock on the file
27293    ** already.
27294    */
27295    int failed = 0;
27296    assert( 0!=pFile->eFileLock );
27297    if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
27298        /* Acquire a RESERVED lock */
27299        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
27300      if( !failed ){
27301        context->reserved = 1;
27302      }
27303    }
27304    if (!failed && eFileLock == EXCLUSIVE_LOCK) {
27305      /* Acquire an EXCLUSIVE lock */
27306
27307      /* Remove the shared lock before trying the range.  we'll need to
27308      ** reestablish the shared lock if we can't get the  afpUnlock
27309      */
27310      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
27311                         pInode->sharedByte, 1, 0)) ){
27312        int failed2 = SQLITE_OK;
27313        /* now attemmpt to get the exclusive lock range */
27314        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
27315                               SHARED_SIZE, 1);
27316        if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
27317                       SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
27318          /* Can't reestablish the shared lock.  Sqlite can't deal, this is
27319          ** a critical I/O error
27320          */
27321          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
27322               SQLITE_IOERR_LOCK;
27323          goto afp_end_lock;
27324        }
27325      }else{
27326        rc = failed;
27327      }
27328    }
27329    if( failed ){
27330      rc = failed;
27331    }
27332  }
27333
27334  if( rc==SQLITE_OK ){
27335    pFile->eFileLock = eFileLock;
27336    pInode->eFileLock = eFileLock;
27337  }else if( eFileLock==EXCLUSIVE_LOCK ){
27338    pFile->eFileLock = PENDING_LOCK;
27339    pInode->eFileLock = PENDING_LOCK;
27340  }
27341
27342afp_end_lock:
27343  unixLeaveMutex();
27344  OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
27345         rc==SQLITE_OK ? "ok" : "failed"));
27346  return rc;
27347}
27348
27349/*
27350** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27351** must be either NO_LOCK or SHARED_LOCK.
27352**
27353** If the locking level of the file descriptor is already at or below
27354** the requested locking level, this routine is a no-op.
27355*/
27356static int afpUnlock(sqlite3_file *id, int eFileLock) {
27357  int rc = SQLITE_OK;
27358  unixFile *pFile = (unixFile*)id;
27359  unixInodeInfo *pInode;
27360  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27361  int skipShared = 0;
27362#ifdef SQLITE_TEST
27363  int h = pFile->h;
27364#endif
27365
27366  assert( pFile );
27367  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
27368           pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
27369           getpid()));
27370
27371  assert( eFileLock<=SHARED_LOCK );
27372  if( pFile->eFileLock<=eFileLock ){
27373    return SQLITE_OK;
27374  }
27375  unixEnterMutex();
27376  pInode = pFile->pInode;
27377  assert( pInode->nShared!=0 );
27378  if( pFile->eFileLock>SHARED_LOCK ){
27379    assert( pInode->eFileLock==pFile->eFileLock );
27380    SimulateIOErrorBenign(1);
27381    SimulateIOError( h=(-1) )
27382    SimulateIOErrorBenign(0);
27383
27384#ifndef NDEBUG
27385    /* When reducing a lock such that other processes can start
27386    ** reading the database file again, make sure that the
27387    ** transaction counter was updated if any part of the database
27388    ** file changed.  If the transaction counter is not updated,
27389    ** other connections to the same file might not realize that
27390    ** the file has changed and hence might not know to flush their
27391    ** cache.  The use of a stale cache can lead to database corruption.
27392    */
27393    assert( pFile->inNormalWrite==0
27394           || pFile->dbUpdate==0
27395           || pFile->transCntrChng==1 );
27396    pFile->inNormalWrite = 0;
27397#endif
27398
27399    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
27400      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
27401      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
27402        /* only re-establish the shared lock if necessary */
27403        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
27404        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
27405      } else {
27406        skipShared = 1;
27407      }
27408    }
27409    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
27410      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
27411    }
27412    if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
27413      rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
27414      if( !rc ){
27415        context->reserved = 0;
27416      }
27417    }
27418    if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
27419      pInode->eFileLock = SHARED_LOCK;
27420    }
27421  }
27422  if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
27423
27424    /* Decrement the shared lock counter.  Release the lock using an
27425    ** OS call only when all threads in this same process have released
27426    ** the lock.
27427    */
27428    unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
27429    pInode->nShared--;
27430    if( pInode->nShared==0 ){
27431      SimulateIOErrorBenign(1);
27432      SimulateIOError( h=(-1) )
27433      SimulateIOErrorBenign(0);
27434      if( !skipShared ){
27435        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
27436      }
27437      if( !rc ){
27438        pInode->eFileLock = NO_LOCK;
27439        pFile->eFileLock = NO_LOCK;
27440      }
27441    }
27442    if( rc==SQLITE_OK ){
27443      pInode->nLock--;
27444      assert( pInode->nLock>=0 );
27445      if( pInode->nLock==0 ){
27446        closePendingFds(pFile);
27447      }
27448    }
27449  }
27450
27451  unixLeaveMutex();
27452  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
27453  return rc;
27454}
27455
27456/*
27457** Close a file & cleanup AFP specific locking context
27458*/
27459static int afpClose(sqlite3_file *id) {
27460  int rc = SQLITE_OK;
27461  if( id ){
27462    unixFile *pFile = (unixFile*)id;
27463    afpUnlock(id, NO_LOCK);
27464    unixEnterMutex();
27465    if( pFile->pInode && pFile->pInode->nLock ){
27466      /* If there are outstanding locks, do not actually close the file just
27467      ** yet because that would clear those locks.  Instead, add the file
27468      ** descriptor to pInode->aPending.  It will be automatically closed when
27469      ** the last lock is cleared.
27470      */
27471      setPendingFd(pFile);
27472    }
27473    releaseInodeInfo(pFile);
27474    sqlite3_free(pFile->lockingContext);
27475    rc = closeUnixFile(id);
27476    unixLeaveMutex();
27477  }
27478  return rc;
27479}
27480
27481#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27482/*
27483** The code above is the AFP lock implementation.  The code is specific
27484** to MacOSX and does not work on other unix platforms.  No alternative
27485** is available.  If you don't compile for a mac, then the "unix-afp"
27486** VFS is not available.
27487**
27488********************* End of the AFP lock implementation **********************
27489******************************************************************************/
27490
27491/******************************************************************************
27492*************************** Begin NFS Locking ********************************/
27493
27494#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
27495/*
27496 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27497 ** must be either NO_LOCK or SHARED_LOCK.
27498 **
27499 ** If the locking level of the file descriptor is already at or below
27500 ** the requested locking level, this routine is a no-op.
27501 */
27502static int nfsUnlock(sqlite3_file *id, int eFileLock){
27503  return posixUnlock(id, eFileLock, 1);
27504}
27505
27506#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27507/*
27508** The code above is the NFS lock implementation.  The code is specific
27509** to MacOSX and does not work on other unix platforms.  No alternative
27510** is available.
27511**
27512********************* End of the NFS lock implementation **********************
27513******************************************************************************/
27514
27515/******************************************************************************
27516**************** Non-locking sqlite3_file methods *****************************
27517**
27518** The next division contains implementations for all methods of the
27519** sqlite3_file object other than the locking methods.  The locking
27520** methods were defined in divisions above (one locking method per
27521** division).  Those methods that are common to all locking modes
27522** are gather together into this division.
27523*/
27524
27525/*
27526** Seek to the offset passed as the second argument, then read cnt
27527** bytes into pBuf. Return the number of bytes actually read.
27528**
27529** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
27530** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
27531** one system to another.  Since SQLite does not define USE_PREAD
27532** any any form by default, we will not attempt to define _XOPEN_SOURCE.
27533** See tickets #2741 and #2681.
27534**
27535** To avoid stomping the errno value on a failed read the lastErrno value
27536** is set before returning.
27537*/
27538static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
27539  int got;
27540#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27541  i64 newOffset;
27542#endif
27543  TIMER_START;
27544#if defined(USE_PREAD)
27545  do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27546  SimulateIOError( got = -1 );
27547#elif defined(USE_PREAD64)
27548  do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
27549  SimulateIOError( got = -1 );
27550#else
27551  newOffset = lseek(id->h, offset, SEEK_SET);
27552  SimulateIOError( newOffset-- );
27553  if( newOffset!=offset ){
27554    if( newOffset == -1 ){
27555      ((unixFile*)id)->lastErrno = errno;
27556    }else{
27557      ((unixFile*)id)->lastErrno = 0;
27558    }
27559    return -1;
27560  }
27561  do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27562#endif
27563  TIMER_END;
27564  if( got<0 ){
27565    ((unixFile*)id)->lastErrno = errno;
27566  }
27567  OSTRACE(("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27568  return got;
27569}
27570
27571/*
27572** Read data from a file into a buffer.  Return SQLITE_OK if all
27573** bytes were read successfully and SQLITE_IOERR if anything goes
27574** wrong.
27575*/
27576static int unixRead(
27577  sqlite3_file *id,
27578  void *pBuf,
27579  int amt,
27580  sqlite3_int64 offset
27581){
27582  unixFile *pFile = (unixFile *)id;
27583  int got;
27584  assert( id );
27585
27586  /* If this is a database file (not a journal, master-journal or temp
27587  ** file), the bytes in the locking range should never be read or written. */
27588#if 0
27589  assert( pFile->pUnused==0
27590       || offset>=PENDING_BYTE+512
27591       || offset+amt<=PENDING_BYTE
27592  );
27593#endif
27594
27595  got = seekAndRead(pFile, offset, pBuf, amt);
27596  if( got==amt ){
27597    return SQLITE_OK;
27598  }else if( got<0 ){
27599    /* lastErrno set by seekAndRead */
27600    return SQLITE_IOERR_READ;
27601  }else{
27602    pFile->lastErrno = 0; /* not a system error */
27603    /* Unread parts of the buffer must be zero-filled */
27604    memset(&((char*)pBuf)[got], 0, amt-got);
27605    return SQLITE_IOERR_SHORT_READ;
27606  }
27607}
27608
27609/*
27610** Seek to the offset in id->offset then read cnt bytes into pBuf.
27611** Return the number of bytes actually read.  Update the offset.
27612**
27613** To avoid stomping the errno value on a failed write the lastErrno value
27614** is set before returning.
27615*/
27616static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
27617  int got;
27618#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27619  i64 newOffset;
27620#endif
27621  TIMER_START;
27622#if defined(USE_PREAD)
27623  do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27624#elif defined(USE_PREAD64)
27625  do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27626#else
27627  do{
27628    newOffset = lseek(id->h, offset, SEEK_SET);
27629    SimulateIOError( newOffset-- );
27630    if( newOffset!=offset ){
27631      if( newOffset == -1 ){
27632        ((unixFile*)id)->lastErrno = errno;
27633      }else{
27634        ((unixFile*)id)->lastErrno = 0;
27635      }
27636      return -1;
27637    }
27638    got = osWrite(id->h, pBuf, cnt);
27639  }while( got<0 && errno==EINTR );
27640#endif
27641  TIMER_END;
27642  if( got<0 ){
27643    ((unixFile*)id)->lastErrno = errno;
27644  }
27645
27646  OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27647  return got;
27648}
27649
27650
27651/*
27652** Write data from a buffer into a file.  Return SQLITE_OK on success
27653** or some other error code on failure.
27654*/
27655static int unixWrite(
27656  sqlite3_file *id,
27657  const void *pBuf,
27658  int amt,
27659  sqlite3_int64 offset
27660){
27661  unixFile *pFile = (unixFile*)id;
27662  int wrote = 0;
27663  assert( id );
27664  assert( amt>0 );
27665
27666  /* If this is a database file (not a journal, master-journal or temp
27667  ** file), the bytes in the locking range should never be read or written. */
27668#if 0
27669  assert( pFile->pUnused==0
27670       || offset>=PENDING_BYTE+512
27671       || offset+amt<=PENDING_BYTE
27672  );
27673#endif
27674
27675#ifndef NDEBUG
27676  /* If we are doing a normal write to a database file (as opposed to
27677  ** doing a hot-journal rollback or a write to some file other than a
27678  ** normal database file) then record the fact that the database
27679  ** has changed.  If the transaction counter is modified, record that
27680  ** fact too.
27681  */
27682  if( pFile->inNormalWrite ){
27683    pFile->dbUpdate = 1;  /* The database has been modified */
27684    if( offset<=24 && offset+amt>=27 ){
27685      int rc;
27686      char oldCntr[4];
27687      SimulateIOErrorBenign(1);
27688      rc = seekAndRead(pFile, 24, oldCntr, 4);
27689      SimulateIOErrorBenign(0);
27690      if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
27691        pFile->transCntrChng = 1;  /* The transaction counter has changed */
27692      }
27693    }
27694  }
27695#endif
27696
27697  while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
27698    amt -= wrote;
27699    offset += wrote;
27700    pBuf = &((char*)pBuf)[wrote];
27701  }
27702  SimulateIOError(( wrote=(-1), amt=1 ));
27703  SimulateDiskfullError(( wrote=0, amt=1 ));
27704
27705  if( amt>0 ){
27706    if( wrote<0 && pFile->lastErrno!=ENOSPC ){
27707      /* lastErrno set by seekAndWrite */
27708      return SQLITE_IOERR_WRITE;
27709    }else{
27710      pFile->lastErrno = 0; /* not a system error */
27711      return SQLITE_FULL;
27712    }
27713  }
27714
27715  return SQLITE_OK;
27716}
27717
27718#ifdef SQLITE_TEST
27719/*
27720** Count the number of fullsyncs and normal syncs.  This is used to test
27721** that syncs and fullsyncs are occurring at the right times.
27722*/
27723SQLITE_API int sqlite3_sync_count = 0;
27724SQLITE_API int sqlite3_fullsync_count = 0;
27725#endif
27726
27727/*
27728** We do not trust systems to provide a working fdatasync().  Some do.
27729** Others do no.  To be safe, we will stick with the (slightly slower)
27730** fsync(). If you know that your system does support fdatasync() correctly,
27731** then simply compile with -Dfdatasync=fdatasync
27732*/
27733#if !defined(fdatasync)
27734# define fdatasync fsync
27735#endif
27736
27737/*
27738** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
27739** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
27740** only available on Mac OS X.  But that could change.
27741*/
27742#ifdef F_FULLFSYNC
27743# define HAVE_FULLFSYNC 1
27744#else
27745# define HAVE_FULLFSYNC 0
27746#endif
27747
27748
27749/*
27750** The fsync() system call does not work as advertised on many
27751** unix systems.  The following procedure is an attempt to make
27752** it work better.
27753**
27754** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
27755** for testing when we want to run through the test suite quickly.
27756** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
27757** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
27758** or power failure will likely corrupt the database file.
27759**
27760** SQLite sets the dataOnly flag if the size of the file is unchanged.
27761** The idea behind dataOnly is that it should only write the file content
27762** to disk, not the inode.  We only set dataOnly if the file size is
27763** unchanged since the file size is part of the inode.  However,
27764** Ted Ts'o tells us that fdatasync() will also write the inode if the
27765** file size has changed.  The only real difference between fdatasync()
27766** and fsync(), Ted tells us, is that fdatasync() will not flush the
27767** inode if the mtime or owner or other inode attributes have changed.
27768** We only care about the file size, not the other file attributes, so
27769** as far as SQLite is concerned, an fdatasync() is always adequate.
27770** So, we always use fdatasync() if it is available, regardless of
27771** the value of the dataOnly flag.
27772*/
27773static int full_fsync(int fd, int fullSync, int dataOnly){
27774  int rc;
27775
27776  /* The following "ifdef/elif/else/" block has the same structure as
27777  ** the one below. It is replicated here solely to avoid cluttering
27778  ** up the real code with the UNUSED_PARAMETER() macros.
27779  */
27780#ifdef SQLITE_NO_SYNC
27781  UNUSED_PARAMETER(fd);
27782  UNUSED_PARAMETER(fullSync);
27783  UNUSED_PARAMETER(dataOnly);
27784#elif HAVE_FULLFSYNC
27785  UNUSED_PARAMETER(dataOnly);
27786#else
27787  UNUSED_PARAMETER(fullSync);
27788  UNUSED_PARAMETER(dataOnly);
27789#endif
27790
27791  /* Record the number of times that we do a normal fsync() and
27792  ** FULLSYNC.  This is used during testing to verify that this procedure
27793  ** gets called with the correct arguments.
27794  */
27795#ifdef SQLITE_TEST
27796  if( fullSync ) sqlite3_fullsync_count++;
27797  sqlite3_sync_count++;
27798#endif
27799
27800  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
27801  ** no-op
27802  */
27803#ifdef SQLITE_NO_SYNC
27804  rc = SQLITE_OK;
27805#elif HAVE_FULLFSYNC
27806  if( fullSync ){
27807    rc = osFcntl(fd, F_FULLFSYNC, 0);
27808  }else{
27809    rc = 1;
27810  }
27811  /* If the FULLFSYNC failed, fall back to attempting an fsync().
27812  ** It shouldn't be possible for fullfsync to fail on the local
27813  ** file system (on OSX), so failure indicates that FULLFSYNC
27814  ** isn't supported for this file system. So, attempt an fsync
27815  ** and (for now) ignore the overhead of a superfluous fcntl call.
27816  ** It'd be better to detect fullfsync support once and avoid
27817  ** the fcntl call every time sync is called.
27818  */
27819  if( rc ) rc = fsync(fd);
27820
27821#elif defined(__APPLE__)
27822  /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
27823  ** so currently we default to the macro that redefines fdatasync to fsync
27824  */
27825  rc = fsync(fd);
27826#else
27827  rc = fdatasync(fd);
27828#if OS_VXWORKS
27829  if( rc==-1 && errno==ENOTSUP ){
27830    rc = fsync(fd);
27831  }
27832#endif /* OS_VXWORKS */
27833#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
27834
27835  if( OS_VXWORKS && rc!= -1 ){
27836    rc = 0;
27837  }
27838  return rc;
27839}
27840
27841/*
27842** Open a file descriptor to the directory containing file zFilename.
27843** If successful, *pFd is set to the opened file descriptor and
27844** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
27845** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
27846** value.
27847**
27848** The directory file descriptor is used for only one thing - to
27849** fsync() a directory to make sure file creation and deletion events
27850** are flushed to disk.  Such fsyncs are not needed on newer
27851** journaling filesystems, but are required on older filesystems.
27852**
27853** This routine can be overridden using the xSetSysCall interface.
27854** The ability to override this routine was added in support of the
27855** chromium sandbox.  Opening a directory is a security risk (we are
27856** told) so making it overrideable allows the chromium sandbox to
27857** replace this routine with a harmless no-op.  To make this routine
27858** a no-op, replace it with a stub that returns SQLITE_OK but leaves
27859** *pFd set to a negative number.
27860**
27861** If SQLITE_OK is returned, the caller is responsible for closing
27862** the file descriptor *pFd using close().
27863*/
27864static int openDirectory(const char *zFilename, int *pFd){
27865  int ii;
27866  int fd = -1;
27867  char zDirname[MAX_PATHNAME+1];
27868
27869  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
27870  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
27871  if( ii>0 ){
27872    zDirname[ii] = '\0';
27873    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
27874    if( fd>=0 ){
27875#ifdef FD_CLOEXEC
27876      osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
27877#endif
27878      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
27879    }
27880  }
27881  *pFd = fd;
27882  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
27883}
27884
27885/*
27886** Make sure all writes to a particular file are committed to disk.
27887**
27888** If dataOnly==0 then both the file itself and its metadata (file
27889** size, access time, etc) are synced.  If dataOnly!=0 then only the
27890** file data is synced.
27891**
27892** Under Unix, also make sure that the directory entry for the file
27893** has been created by fsync-ing the directory that contains the file.
27894** If we do not do this and we encounter a power failure, the directory
27895** entry for the journal might not exist after we reboot.  The next
27896** SQLite to access the file will not know that the journal exists (because
27897** the directory entry for the journal was never created) and the transaction
27898** will not roll back - possibly leading to database corruption.
27899*/
27900static int unixSync(sqlite3_file *id, int flags){
27901  int rc;
27902  unixFile *pFile = (unixFile*)id;
27903
27904  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
27905  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
27906
27907  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
27908  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
27909      || (flags&0x0F)==SQLITE_SYNC_FULL
27910  );
27911
27912  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
27913  ** line is to test that doing so does not cause any problems.
27914  */
27915  SimulateDiskfullError( return SQLITE_FULL );
27916
27917  assert( pFile );
27918  OSTRACE(("SYNC    %-3d\n", pFile->h));
27919  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
27920  SimulateIOError( rc=1 );
27921  if( rc ){
27922    pFile->lastErrno = errno;
27923    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27924  }
27925
27926  /* Also fsync the directory containing the file if the DIRSYNC flag
27927  ** is set.  This is a one-time occurrance.  Many systems (examples: AIX)
27928  ** are unable to fsync a directory, so ignore errors on the fsync.
27929  */
27930  if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
27931    int dirfd;
27932    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
27933            HAVE_FULLFSYNC, isFullsync));
27934    rc = osOpenDirectory(pFile->zPath, &dirfd);
27935    if( rc==SQLITE_OK && dirfd>=0 ){
27936      full_fsync(dirfd, 0, 0);
27937      robust_close(pFile, dirfd, __LINE__);
27938    }else if( rc==SQLITE_CANTOPEN ){
27939      rc = SQLITE_OK;
27940    }
27941    pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27942  }
27943  return rc;
27944}
27945
27946/*
27947** Truncate an open file to a specified size
27948*/
27949static int unixTruncate(sqlite3_file *id, i64 nByte){
27950  unixFile *pFile = (unixFile *)id;
27951  int rc;
27952  assert( pFile );
27953  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
27954
27955  /* If the user has configured a chunk-size for this file, truncate the
27956  ** file so that it consists of an integer number of chunks (i.e. the
27957  ** actual file size after the operation may be larger than the requested
27958  ** size).
27959  */
27960  if( pFile->szChunk ){
27961    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
27962  }
27963
27964  rc = robust_ftruncate(pFile->h, (off_t)nByte);
27965  if( rc ){
27966    pFile->lastErrno = errno;
27967    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27968  }else{
27969#ifndef NDEBUG
27970    /* If we are doing a normal write to a database file (as opposed to
27971    ** doing a hot-journal rollback or a write to some file other than a
27972    ** normal database file) and we truncate the file to zero length,
27973    ** that effectively updates the change counter.  This might happen
27974    ** when restoring a database using the backup API from a zero-length
27975    ** source.
27976    */
27977    if( pFile->inNormalWrite && nByte==0 ){
27978      pFile->transCntrChng = 1;
27979    }
27980#endif
27981
27982    return SQLITE_OK;
27983  }
27984}
27985
27986/*
27987** Determine the current size of a file in bytes
27988*/
27989static int unixFileSize(sqlite3_file *id, i64 *pSize){
27990  int rc;
27991  struct stat buf;
27992  assert( id );
27993  rc = osFstat(((unixFile*)id)->h, &buf);
27994  SimulateIOError( rc=1 );
27995  if( rc!=0 ){
27996    ((unixFile*)id)->lastErrno = errno;
27997    return SQLITE_IOERR_FSTAT;
27998  }
27999  *pSize = buf.st_size;
28000
28001  /* When opening a zero-size database, the findInodeInfo() procedure
28002  ** writes a single byte into that file in order to work around a bug
28003  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
28004  ** layers, we need to report this file size as zero even though it is
28005  ** really 1.   Ticket #3260.
28006  */
28007  if( *pSize==1 ) *pSize = 0;
28008
28009
28010  return SQLITE_OK;
28011}
28012
28013#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28014/*
28015** Handler for proxy-locking file-control verbs.  Defined below in the
28016** proxying locking division.
28017*/
28018static int proxyFileControl(sqlite3_file*,int,void*);
28019#endif
28020
28021/*
28022** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
28023** file-control operation.  Enlarge the database to nBytes in size
28024** (rounded up to the next chunk-size).  If the database is already
28025** nBytes or larger, this routine is a no-op.
28026*/
28027static int fcntlSizeHint(unixFile *pFile, i64 nByte){
28028  if( pFile->szChunk>0 ){
28029    i64 nSize;                    /* Required file size */
28030    struct stat buf;              /* Used to hold return values of fstat() */
28031
28032    if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
28033
28034    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
28035    if( nSize>(i64)buf.st_size ){
28036
28037#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
28038      /* The code below is handling the return value of osFallocate()
28039      ** correctly. posix_fallocate() is defined to "returns zero on success,
28040      ** or an error number on  failure". See the manpage for details. */
28041      int err;
28042      do{
28043        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
28044      }while( err==EINTR );
28045      if( err ) return SQLITE_IOERR_WRITE;
28046#else
28047      /* If the OS does not have posix_fallocate(), fake it. First use
28048      ** ftruncate() to set the file size, then write a single byte to
28049      ** the last byte in each block within the extended region. This
28050      ** is the same technique used by glibc to implement posix_fallocate()
28051      ** on systems that do not have a real fallocate() system call.
28052      */
28053      int nBlk = buf.st_blksize;  /* File-system block size */
28054      i64 iWrite;                 /* Next offset to write to */
28055
28056      if( robust_ftruncate(pFile->h, nSize) ){
28057        pFile->lastErrno = errno;
28058        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
28059      }
28060      iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
28061      while( iWrite<nSize ){
28062        int nWrite = seekAndWrite(pFile, iWrite, "", 1);
28063        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
28064        iWrite += nBlk;
28065      }
28066#endif
28067    }
28068  }
28069
28070  return SQLITE_OK;
28071}
28072
28073/*
28074** Information and control of an open file handle.
28075*/
28076static int unixFileControl(sqlite3_file *id, int op, void *pArg){
28077  unixFile *pFile = (unixFile*)id;
28078  switch( op ){
28079    case SQLITE_FCNTL_LOCKSTATE: {
28080      *(int*)pArg = pFile->eFileLock;
28081      return SQLITE_OK;
28082    }
28083    case SQLITE_LAST_ERRNO: {
28084      *(int*)pArg = pFile->lastErrno;
28085      return SQLITE_OK;
28086    }
28087    case SQLITE_FCNTL_CHUNK_SIZE: {
28088      pFile->szChunk = *(int *)pArg;
28089      return SQLITE_OK;
28090    }
28091    case SQLITE_FCNTL_SIZE_HINT: {
28092      int rc;
28093      SimulateIOErrorBenign(1);
28094      rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28095      SimulateIOErrorBenign(0);
28096      return rc;
28097    }
28098    case SQLITE_FCNTL_PERSIST_WAL: {
28099      int bPersist = *(int*)pArg;
28100      if( bPersist<0 ){
28101        *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
28102      }else if( bPersist==0 ){
28103        pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL;
28104      }else{
28105        pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL;
28106      }
28107      return SQLITE_OK;
28108    }
28109#ifndef NDEBUG
28110    /* The pager calls this method to signal that it has done
28111    ** a rollback and that the database is therefore unchanged and
28112    ** it hence it is OK for the transaction change counter to be
28113    ** unchanged.
28114    */
28115    case SQLITE_FCNTL_DB_UNCHANGED: {
28116      ((unixFile*)id)->dbUpdate = 0;
28117      return SQLITE_OK;
28118    }
28119#endif
28120#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28121    case SQLITE_SET_LOCKPROXYFILE:
28122    case SQLITE_GET_LOCKPROXYFILE: {
28123      return proxyFileControl(id,op,pArg);
28124    }
28125#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
28126    case SQLITE_FCNTL_SYNC_OMITTED: {
28127      return SQLITE_OK;  /* A no-op */
28128    }
28129  }
28130  return SQLITE_NOTFOUND;
28131}
28132
28133/*
28134** Return the sector size in bytes of the underlying block device for
28135** the specified file. This is almost always 512 bytes, but may be
28136** larger for some devices.
28137**
28138** SQLite code assumes this function cannot fail. It also assumes that
28139** if two files are created in the same file-system directory (i.e.
28140** a database and its journal file) that the sector size will be the
28141** same for both.
28142*/
28143static int unixSectorSize(sqlite3_file *NotUsed){
28144  UNUSED_PARAMETER(NotUsed);
28145  return SQLITE_DEFAULT_SECTOR_SIZE;
28146}
28147
28148/*
28149** Return the device characteristics for the file. This is always 0 for unix.
28150*/
28151static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
28152  UNUSED_PARAMETER(NotUsed);
28153  return 0;
28154}
28155
28156#ifndef SQLITE_OMIT_WAL
28157
28158
28159/*
28160** Object used to represent an shared memory buffer.
28161**
28162** When multiple threads all reference the same wal-index, each thread
28163** has its own unixShm object, but they all point to a single instance
28164** of this unixShmNode object.  In other words, each wal-index is opened
28165** only once per process.
28166**
28167** Each unixShmNode object is connected to a single unixInodeInfo object.
28168** We could coalesce this object into unixInodeInfo, but that would mean
28169** every open file that does not use shared memory (in other words, most
28170** open files) would have to carry around this extra information.  So
28171** the unixInodeInfo object contains a pointer to this unixShmNode object
28172** and the unixShmNode object is created only when needed.
28173**
28174** unixMutexHeld() must be true when creating or destroying
28175** this object or while reading or writing the following fields:
28176**
28177**      nRef
28178**
28179** The following fields are read-only after the object is created:
28180**
28181**      fid
28182**      zFilename
28183**
28184** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
28185** unixMutexHeld() is true when reading or writing any other field
28186** in this structure.
28187*/
28188struct unixShmNode {
28189  unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
28190  sqlite3_mutex *mutex;      /* Mutex to access this object */
28191  char *zFilename;           /* Name of the mmapped file */
28192  int h;                     /* Open file descriptor */
28193  int szRegion;              /* Size of shared-memory regions */
28194  u16 nRegion;               /* Size of array apRegion */
28195  u8 isReadonly;             /* True if read-only */
28196  char **apRegion;           /* Array of mapped shared-memory regions */
28197  int nRef;                  /* Number of unixShm objects pointing to this */
28198  unixShm *pFirst;           /* All unixShm objects pointing to this */
28199#ifdef SQLITE_DEBUG
28200  u8 exclMask;               /* Mask of exclusive locks held */
28201  u8 sharedMask;             /* Mask of shared locks held */
28202  u8 nextShmId;              /* Next available unixShm.id value */
28203#endif
28204};
28205
28206/*
28207** Structure used internally by this VFS to record the state of an
28208** open shared memory connection.
28209**
28210** The following fields are initialized when this object is created and
28211** are read-only thereafter:
28212**
28213**    unixShm.pFile
28214**    unixShm.id
28215**
28216** All other fields are read/write.  The unixShm.pFile->mutex must be held
28217** while accessing any read/write fields.
28218*/
28219struct unixShm {
28220  unixShmNode *pShmNode;     /* The underlying unixShmNode object */
28221  unixShm *pNext;            /* Next unixShm with the same unixShmNode */
28222  u8 hasMutex;               /* True if holding the unixShmNode mutex */
28223  u8 id;                     /* Id of this connection within its unixShmNode */
28224  u16 sharedMask;            /* Mask of shared locks held */
28225  u16 exclMask;              /* Mask of exclusive locks held */
28226};
28227
28228/*
28229** Constants used for locking
28230*/
28231#define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
28232#define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
28233
28234/*
28235** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
28236**
28237** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
28238** otherwise.
28239*/
28240static int unixShmSystemLock(
28241  unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
28242  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
28243  int ofst,              /* First byte of the locking range */
28244  int n                  /* Number of bytes to lock */
28245){
28246  struct flock f;       /* The posix advisory locking structure */
28247  int rc = SQLITE_OK;   /* Result code form fcntl() */
28248
28249  /* Access to the unixShmNode object is serialized by the caller */
28250  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
28251
28252  /* Shared locks never span more than one byte */
28253  assert( n==1 || lockType!=F_RDLCK );
28254
28255  /* Locks are within range */
28256  assert( n>=1 && n<SQLITE_SHM_NLOCK );
28257
28258  if( pShmNode->h>=0 ){
28259    /* Initialize the locking parameters */
28260    memset(&f, 0, sizeof(f));
28261    f.l_type = lockType;
28262    f.l_whence = SEEK_SET;
28263    f.l_start = ofst;
28264    f.l_len = n;
28265
28266    rc = osFcntl(pShmNode->h, F_SETLK, &f);
28267    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
28268  }
28269
28270  /* Update the global lock state and do debug tracing */
28271#ifdef SQLITE_DEBUG
28272  { u16 mask;
28273  OSTRACE(("SHM-LOCK "));
28274  mask = (1<<(ofst+n)) - (1<<ofst);
28275  if( rc==SQLITE_OK ){
28276    if( lockType==F_UNLCK ){
28277      OSTRACE(("unlock %d ok", ofst));
28278      pShmNode->exclMask &= ~mask;
28279      pShmNode->sharedMask &= ~mask;
28280    }else if( lockType==F_RDLCK ){
28281      OSTRACE(("read-lock %d ok", ofst));
28282      pShmNode->exclMask &= ~mask;
28283      pShmNode->sharedMask |= mask;
28284    }else{
28285      assert( lockType==F_WRLCK );
28286      OSTRACE(("write-lock %d ok", ofst));
28287      pShmNode->exclMask |= mask;
28288      pShmNode->sharedMask &= ~mask;
28289    }
28290  }else{
28291    if( lockType==F_UNLCK ){
28292      OSTRACE(("unlock %d failed", ofst));
28293    }else if( lockType==F_RDLCK ){
28294      OSTRACE(("read-lock failed"));
28295    }else{
28296      assert( lockType==F_WRLCK );
28297      OSTRACE(("write-lock %d failed", ofst));
28298    }
28299  }
28300  OSTRACE((" - afterwards %03x,%03x\n",
28301           pShmNode->sharedMask, pShmNode->exclMask));
28302  }
28303#endif
28304
28305  return rc;
28306}
28307
28308
28309/*
28310** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
28311**
28312** This is not a VFS shared-memory method; it is a utility function called
28313** by VFS shared-memory methods.
28314*/
28315static void unixShmPurge(unixFile *pFd){
28316  unixShmNode *p = pFd->pInode->pShmNode;
28317  assert( unixMutexHeld() );
28318  if( p && p->nRef==0 ){
28319    int i;
28320    assert( p->pInode==pFd->pInode );
28321    sqlite3_mutex_free(p->mutex);
28322    for(i=0; i<p->nRegion; i++){
28323      if( p->h>=0 ){
28324        munmap(p->apRegion[i], p->szRegion);
28325      }else{
28326        sqlite3_free(p->apRegion[i]);
28327      }
28328    }
28329    sqlite3_free(p->apRegion);
28330    if( p->h>=0 ){
28331      robust_close(pFd, p->h, __LINE__);
28332      p->h = -1;
28333    }
28334    p->pInode->pShmNode = 0;
28335    sqlite3_free(p);
28336  }
28337}
28338
28339/*
28340** Open a shared-memory area associated with open database file pDbFd.
28341** This particular implementation uses mmapped files.
28342**
28343** The file used to implement shared-memory is in the same directory
28344** as the open database file and has the same name as the open database
28345** file with the "-shm" suffix added.  For example, if the database file
28346** is "/home/user1/config.db" then the file that is created and mmapped
28347** for shared memory will be called "/home/user1/config.db-shm".
28348**
28349** Another approach to is to use files in /dev/shm or /dev/tmp or an
28350** some other tmpfs mount. But if a file in a different directory
28351** from the database file is used, then differing access permissions
28352** or a chroot() might cause two different processes on the same
28353** database to end up using different files for shared memory -
28354** meaning that their memory would not really be shared - resulting
28355** in database corruption.  Nevertheless, this tmpfs file usage
28356** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
28357** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
28358** option results in an incompatible build of SQLite;  builds of SQLite
28359** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
28360** same database file at the same time, database corruption will likely
28361** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
28362** "unsupported" and may go away in a future SQLite release.
28363**
28364** When opening a new shared-memory file, if no other instances of that
28365** file are currently open, in this process or in other processes, then
28366** the file must be truncated to zero length or have its header cleared.
28367**
28368** If the original database file (pDbFd) is using the "unix-excl" VFS
28369** that means that an exclusive lock is held on the database file and
28370** that no other processes are able to read or write the database.  In
28371** that case, we do not really need shared memory.  No shared memory
28372** file is created.  The shared memory will be simulated with heap memory.
28373*/
28374static int unixOpenSharedMemory(unixFile *pDbFd){
28375  struct unixShm *p = 0;          /* The connection to be opened */
28376  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
28377  int rc;                         /* Result code */
28378  unixInodeInfo *pInode;          /* The inode of fd */
28379  char *zShmFilename;             /* Name of the file used for SHM */
28380  int nShmFilename;               /* Size of the SHM filename in bytes */
28381
28382  /* Allocate space for the new unixShm object. */
28383  p = sqlite3_malloc( sizeof(*p) );
28384  if( p==0 ) return SQLITE_NOMEM;
28385  memset(p, 0, sizeof(*p));
28386  assert( pDbFd->pShm==0 );
28387
28388  /* Check to see if a unixShmNode object already exists. Reuse an existing
28389  ** one if present. Create a new one if necessary.
28390  */
28391  unixEnterMutex();
28392  pInode = pDbFd->pInode;
28393  pShmNode = pInode->pShmNode;
28394  if( pShmNode==0 ){
28395    struct stat sStat;                 /* fstat() info for database file */
28396
28397    /* Call fstat() to figure out the permissions on the database file. If
28398    ** a new *-shm file is created, an attempt will be made to create it
28399    ** with the same permissions. The actual permissions the file is created
28400    ** with are subject to the current umask setting.
28401    */
28402    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
28403      rc = SQLITE_IOERR_FSTAT;
28404      goto shm_open_err;
28405    }
28406
28407#ifdef SQLITE_SHM_DIRECTORY
28408    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
28409#else
28410    nShmFilename = 5 + (int)strlen(pDbFd->zPath);
28411#endif
28412    pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
28413    if( pShmNode==0 ){
28414      rc = SQLITE_NOMEM;
28415      goto shm_open_err;
28416    }
28417    memset(pShmNode, 0, sizeof(*pShmNode));
28418    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
28419#ifdef SQLITE_SHM_DIRECTORY
28420    sqlite3_snprintf(nShmFilename, zShmFilename,
28421                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
28422                     (u32)sStat.st_ino, (u32)sStat.st_dev);
28423#else
28424    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
28425    sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
28426#endif
28427    pShmNode->h = -1;
28428    pDbFd->pInode->pShmNode = pShmNode;
28429    pShmNode->pInode = pDbFd->pInode;
28430    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
28431    if( pShmNode->mutex==0 ){
28432      rc = SQLITE_NOMEM;
28433      goto shm_open_err;
28434    }
28435
28436    if( pInode->bProcessLock==0 ){
28437      pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
28438                               (sStat.st_mode & 0777));
28439      if( pShmNode->h<0 ){
28440        const char *zRO;
28441        zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28442        if( zRO && sqlite3GetBoolean(zRO) ){
28443          pShmNode->h = robust_open(zShmFilename, O_RDONLY,
28444                                    (sStat.st_mode & 0777));
28445          pShmNode->isReadonly = 1;
28446        }
28447        if( pShmNode->h<0 ){
28448          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
28449          goto shm_open_err;
28450        }
28451      }
28452
28453      /* Check to see if another process is holding the dead-man switch.
28454      ** If not, truncate the file to zero length.
28455      */
28456      rc = SQLITE_OK;
28457      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
28458        if( robust_ftruncate(pShmNode->h, 0) ){
28459          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
28460        }
28461      }
28462      if( rc==SQLITE_OK ){
28463        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
28464      }
28465      if( rc ) goto shm_open_err;
28466    }
28467  }
28468
28469  /* Make the new connection a child of the unixShmNode */
28470  p->pShmNode = pShmNode;
28471#ifdef SQLITE_DEBUG
28472  p->id = pShmNode->nextShmId++;
28473#endif
28474  pShmNode->nRef++;
28475  pDbFd->pShm = p;
28476  unixLeaveMutex();
28477
28478  /* The reference count on pShmNode has already been incremented under
28479  ** the cover of the unixEnterMutex() mutex and the pointer from the
28480  ** new (struct unixShm) object to the pShmNode has been set. All that is
28481  ** left to do is to link the new object into the linked list starting
28482  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
28483  ** mutex.
28484  */
28485  sqlite3_mutex_enter(pShmNode->mutex);
28486  p->pNext = pShmNode->pFirst;
28487  pShmNode->pFirst = p;
28488  sqlite3_mutex_leave(pShmNode->mutex);
28489  return SQLITE_OK;
28490
28491  /* Jump here on any error */
28492shm_open_err:
28493  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
28494  sqlite3_free(p);
28495  unixLeaveMutex();
28496  return rc;
28497}
28498
28499/*
28500** This function is called to obtain a pointer to region iRegion of the
28501** shared-memory associated with the database file fd. Shared-memory regions
28502** are numbered starting from zero. Each shared-memory region is szRegion
28503** bytes in size.
28504**
28505** If an error occurs, an error code is returned and *pp is set to NULL.
28506**
28507** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
28508** region has not been allocated (by any client, including one running in a
28509** separate process), then *pp is set to NULL and SQLITE_OK returned. If
28510** bExtend is non-zero and the requested shared-memory region has not yet
28511** been allocated, it is allocated by this function.
28512**
28513** If the shared-memory region has already been allocated or is allocated by
28514** this call as described above, then it is mapped into this processes
28515** address space (if it is not already), *pp is set to point to the mapped
28516** memory and SQLITE_OK returned.
28517*/
28518static int unixShmMap(
28519  sqlite3_file *fd,               /* Handle open on database file */
28520  int iRegion,                    /* Region to retrieve */
28521  int szRegion,                   /* Size of regions */
28522  int bExtend,                    /* True to extend file if necessary */
28523  void volatile **pp              /* OUT: Mapped memory */
28524){
28525  unixFile *pDbFd = (unixFile*)fd;
28526  unixShm *p;
28527  unixShmNode *pShmNode;
28528  int rc = SQLITE_OK;
28529
28530  /* If the shared-memory file has not yet been opened, open it now. */
28531  if( pDbFd->pShm==0 ){
28532    rc = unixOpenSharedMemory(pDbFd);
28533    if( rc!=SQLITE_OK ) return rc;
28534  }
28535
28536  p = pDbFd->pShm;
28537  pShmNode = p->pShmNode;
28538  sqlite3_mutex_enter(pShmNode->mutex);
28539  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
28540  assert( pShmNode->pInode==pDbFd->pInode );
28541  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28542  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28543
28544  if( pShmNode->nRegion<=iRegion ){
28545    char **apNew;                      /* New apRegion[] array */
28546    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
28547    struct stat sStat;                 /* Used by fstat() */
28548
28549    pShmNode->szRegion = szRegion;
28550
28551    if( pShmNode->h>=0 ){
28552      /* The requested region is not mapped into this processes address space.
28553      ** Check to see if it has been allocated (i.e. if the wal-index file is
28554      ** large enough to contain the requested region).
28555      */
28556      if( osFstat(pShmNode->h, &sStat) ){
28557        rc = SQLITE_IOERR_SHMSIZE;
28558        goto shmpage_out;
28559      }
28560
28561      if( sStat.st_size<nByte ){
28562        /* The requested memory region does not exist. If bExtend is set to
28563        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
28564        **
28565        ** Alternatively, if bExtend is true, use ftruncate() to allocate
28566        ** the requested memory region.
28567        */
28568        if( !bExtend ) goto shmpage_out;
28569        if( robust_ftruncate(pShmNode->h, nByte) ){
28570          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
28571                            pShmNode->zFilename);
28572          goto shmpage_out;
28573        }
28574      }
28575    }
28576
28577    /* Map the requested memory region into this processes address space. */
28578    apNew = (char **)sqlite3_realloc(
28579        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
28580    );
28581    if( !apNew ){
28582      rc = SQLITE_IOERR_NOMEM;
28583      goto shmpage_out;
28584    }
28585    pShmNode->apRegion = apNew;
28586    while(pShmNode->nRegion<=iRegion){
28587      void *pMem;
28588      if( pShmNode->h>=0 ){
28589        pMem = mmap(0, szRegion,
28590            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
28591            MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
28592        );
28593        if( pMem==MAP_FAILED ){
28594          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
28595          goto shmpage_out;
28596        }
28597      }else{
28598        pMem = sqlite3_malloc(szRegion);
28599        if( pMem==0 ){
28600          rc = SQLITE_NOMEM;
28601          goto shmpage_out;
28602        }
28603        memset(pMem, 0, szRegion);
28604      }
28605      pShmNode->apRegion[pShmNode->nRegion] = pMem;
28606      pShmNode->nRegion++;
28607    }
28608  }
28609
28610shmpage_out:
28611  if( pShmNode->nRegion>iRegion ){
28612    *pp = pShmNode->apRegion[iRegion];
28613  }else{
28614    *pp = 0;
28615  }
28616  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
28617  sqlite3_mutex_leave(pShmNode->mutex);
28618  return rc;
28619}
28620
28621/*
28622** Change the lock state for a shared-memory segment.
28623**
28624** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
28625** different here than in posix.  In xShmLock(), one can go from unlocked
28626** to shared and back or from unlocked to exclusive and back.  But one may
28627** not go from shared to exclusive or from exclusive to shared.
28628*/
28629static int unixShmLock(
28630  sqlite3_file *fd,          /* Database file holding the shared memory */
28631  int ofst,                  /* First lock to acquire or release */
28632  int n,                     /* Number of locks to acquire or release */
28633  int flags                  /* What to do with the lock */
28634){
28635  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
28636  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
28637  unixShm *pX;                          /* For looping over all siblings */
28638  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
28639  int rc = SQLITE_OK;                   /* Result code */
28640  u16 mask;                             /* Mask of locks to take or release */
28641
28642  assert( pShmNode==pDbFd->pInode->pShmNode );
28643  assert( pShmNode->pInode==pDbFd->pInode );
28644  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
28645  assert( n>=1 );
28646  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
28647       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
28648       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
28649       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
28650  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
28651  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28652  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28653
28654  mask = (1<<(ofst+n)) - (1<<ofst);
28655  assert( n>1 || mask==(1<<ofst) );
28656  sqlite3_mutex_enter(pShmNode->mutex);
28657  if( flags & SQLITE_SHM_UNLOCK ){
28658    u16 allMask = 0; /* Mask of locks held by siblings */
28659
28660    /* See if any siblings hold this same lock */
28661    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28662      if( pX==p ) continue;
28663      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
28664      allMask |= pX->sharedMask;
28665    }
28666
28667    /* Unlock the system-level locks */
28668    if( (mask & allMask)==0 ){
28669      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
28670    }else{
28671      rc = SQLITE_OK;
28672    }
28673
28674    /* Undo the local locks */
28675    if( rc==SQLITE_OK ){
28676      p->exclMask &= ~mask;
28677      p->sharedMask &= ~mask;
28678    }
28679  }else if( flags & SQLITE_SHM_SHARED ){
28680    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
28681
28682    /* Find out which shared locks are already held by sibling connections.
28683    ** If any sibling already holds an exclusive lock, go ahead and return
28684    ** SQLITE_BUSY.
28685    */
28686    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28687      if( (pX->exclMask & mask)!=0 ){
28688        rc = SQLITE_BUSY;
28689        break;
28690      }
28691      allShared |= pX->sharedMask;
28692    }
28693
28694    /* Get shared locks at the system level, if necessary */
28695    if( rc==SQLITE_OK ){
28696      if( (allShared & mask)==0 ){
28697        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
28698      }else{
28699        rc = SQLITE_OK;
28700      }
28701    }
28702
28703    /* Get the local shared locks */
28704    if( rc==SQLITE_OK ){
28705      p->sharedMask |= mask;
28706    }
28707  }else{
28708    /* Make sure no sibling connections hold locks that will block this
28709    ** lock.  If any do, return SQLITE_BUSY right away.
28710    */
28711    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28712      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
28713        rc = SQLITE_BUSY;
28714        break;
28715      }
28716    }
28717
28718    /* Get the exclusive locks at the system level.  Then if successful
28719    ** also mark the local connection as being locked.
28720    */
28721    if( rc==SQLITE_OK ){
28722      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
28723      if( rc==SQLITE_OK ){
28724        assert( (p->sharedMask & mask)==0 );
28725        p->exclMask |= mask;
28726      }
28727    }
28728  }
28729  sqlite3_mutex_leave(pShmNode->mutex);
28730  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
28731           p->id, getpid(), p->sharedMask, p->exclMask));
28732  return rc;
28733}
28734
28735/*
28736** Implement a memory barrier or memory fence on shared memory.
28737**
28738** All loads and stores begun before the barrier must complete before
28739** any load or store begun after the barrier.
28740*/
28741static void unixShmBarrier(
28742  sqlite3_file *fd                /* Database file holding the shared memory */
28743){
28744  UNUSED_PARAMETER(fd);
28745  unixEnterMutex();
28746  unixLeaveMutex();
28747}
28748
28749/*
28750** Close a connection to shared-memory.  Delete the underlying
28751** storage if deleteFlag is true.
28752**
28753** If there is no shared memory associated with the connection then this
28754** routine is a harmless no-op.
28755*/
28756static int unixShmUnmap(
28757  sqlite3_file *fd,               /* The underlying database file */
28758  int deleteFlag                  /* Delete shared-memory if true */
28759){
28760  unixShm *p;                     /* The connection to be closed */
28761  unixShmNode *pShmNode;          /* The underlying shared-memory file */
28762  unixShm **pp;                   /* For looping over sibling connections */
28763  unixFile *pDbFd;                /* The underlying database file */
28764
28765  pDbFd = (unixFile*)fd;
28766  p = pDbFd->pShm;
28767  if( p==0 ) return SQLITE_OK;
28768  pShmNode = p->pShmNode;
28769
28770  assert( pShmNode==pDbFd->pInode->pShmNode );
28771  assert( pShmNode->pInode==pDbFd->pInode );
28772
28773  /* Remove connection p from the set of connections associated
28774  ** with pShmNode */
28775  sqlite3_mutex_enter(pShmNode->mutex);
28776  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
28777  *pp = p->pNext;
28778
28779  /* Free the connection p */
28780  sqlite3_free(p);
28781  pDbFd->pShm = 0;
28782  sqlite3_mutex_leave(pShmNode->mutex);
28783
28784  /* If pShmNode->nRef has reached 0, then close the underlying
28785  ** shared-memory file, too */
28786  unixEnterMutex();
28787  assert( pShmNode->nRef>0 );
28788  pShmNode->nRef--;
28789  if( pShmNode->nRef==0 ){
28790    if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
28791    unixShmPurge(pDbFd);
28792  }
28793  unixLeaveMutex();
28794
28795  return SQLITE_OK;
28796}
28797
28798
28799#else
28800# define unixShmMap     0
28801# define unixShmLock    0
28802# define unixShmBarrier 0
28803# define unixShmUnmap   0
28804#endif /* #ifndef SQLITE_OMIT_WAL */
28805
28806/*
28807** Here ends the implementation of all sqlite3_file methods.
28808**
28809********************** End sqlite3_file Methods *******************************
28810******************************************************************************/
28811
28812/*
28813** This division contains definitions of sqlite3_io_methods objects that
28814** implement various file locking strategies.  It also contains definitions
28815** of "finder" functions.  A finder-function is used to locate the appropriate
28816** sqlite3_io_methods object for a particular database file.  The pAppData
28817** field of the sqlite3_vfs VFS objects are initialized to be pointers to
28818** the correct finder-function for that VFS.
28819**
28820** Most finder functions return a pointer to a fixed sqlite3_io_methods
28821** object.  The only interesting finder-function is autolockIoFinder, which
28822** looks at the filesystem type and tries to guess the best locking
28823** strategy from that.
28824**
28825** For finder-funtion F, two objects are created:
28826**
28827**    (1) The real finder-function named "FImpt()".
28828**
28829**    (2) A constant pointer to this function named just "F".
28830**
28831**
28832** A pointer to the F pointer is used as the pAppData value for VFS
28833** objects.  We have to do this instead of letting pAppData point
28834** directly at the finder-function since C90 rules prevent a void*
28835** from be cast into a function pointer.
28836**
28837**
28838** Each instance of this macro generates two objects:
28839**
28840**   *  A constant sqlite3_io_methods object call METHOD that has locking
28841**      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
28842**
28843**   *  An I/O method finder function called FINDER that returns a pointer
28844**      to the METHOD object in the previous bullet.
28845*/
28846#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
28847static const sqlite3_io_methods METHOD = {                                   \
28848   VERSION,                    /* iVersion */                                \
28849   CLOSE,                      /* xClose */                                  \
28850   unixRead,                   /* xRead */                                   \
28851   unixWrite,                  /* xWrite */                                  \
28852   unixTruncate,               /* xTruncate */                               \
28853   unixSync,                   /* xSync */                                   \
28854   unixFileSize,               /* xFileSize */                               \
28855   LOCK,                       /* xLock */                                   \
28856   UNLOCK,                     /* xUnlock */                                 \
28857   CKLOCK,                     /* xCheckReservedLock */                      \
28858   unixFileControl,            /* xFileControl */                            \
28859   unixSectorSize,             /* xSectorSize */                             \
28860   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
28861   unixShmMap,                 /* xShmMap */                                 \
28862   unixShmLock,                /* xShmLock */                                \
28863   unixShmBarrier,             /* xShmBarrier */                             \
28864   unixShmUnmap                /* xShmUnmap */                               \
28865};                                                                           \
28866static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
28867  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
28868  return &METHOD;                                                            \
28869}                                                                            \
28870static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
28871    = FINDER##Impl;
28872
28873/*
28874** Here are all of the sqlite3_io_methods objects for each of the
28875** locking strategies.  Functions that return pointers to these methods
28876** are also created.
28877*/
28878IOMETHODS(
28879  posixIoFinder,            /* Finder function name */
28880  posixIoMethods,           /* sqlite3_io_methods object name */
28881  2,                        /* shared memory is enabled */
28882  unixClose,                /* xClose method */
28883  unixLock,                 /* xLock method */
28884  unixUnlock,               /* xUnlock method */
28885  unixCheckReservedLock     /* xCheckReservedLock method */
28886)
28887IOMETHODS(
28888  nolockIoFinder,           /* Finder function name */
28889  nolockIoMethods,          /* sqlite3_io_methods object name */
28890  1,                        /* shared memory is disabled */
28891  nolockClose,              /* xClose method */
28892  nolockLock,               /* xLock method */
28893  nolockUnlock,             /* xUnlock method */
28894  nolockCheckReservedLock   /* xCheckReservedLock method */
28895)
28896IOMETHODS(
28897  dotlockIoFinder,          /* Finder function name */
28898  dotlockIoMethods,         /* sqlite3_io_methods object name */
28899  1,                        /* shared memory is disabled */
28900  dotlockClose,             /* xClose method */
28901  dotlockLock,              /* xLock method */
28902  dotlockUnlock,            /* xUnlock method */
28903  dotlockCheckReservedLock  /* xCheckReservedLock method */
28904)
28905
28906#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
28907IOMETHODS(
28908  flockIoFinder,            /* Finder function name */
28909  flockIoMethods,           /* sqlite3_io_methods object name */
28910  1,                        /* shared memory is disabled */
28911  flockClose,               /* xClose method */
28912  flockLock,                /* xLock method */
28913  flockUnlock,              /* xUnlock method */
28914  flockCheckReservedLock    /* xCheckReservedLock method */
28915)
28916#endif
28917
28918#if OS_VXWORKS
28919IOMETHODS(
28920  semIoFinder,              /* Finder function name */
28921  semIoMethods,             /* sqlite3_io_methods object name */
28922  1,                        /* shared memory is disabled */
28923  semClose,                 /* xClose method */
28924  semLock,                  /* xLock method */
28925  semUnlock,                /* xUnlock method */
28926  semCheckReservedLock      /* xCheckReservedLock method */
28927)
28928#endif
28929
28930#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28931IOMETHODS(
28932  afpIoFinder,              /* Finder function name */
28933  afpIoMethods,             /* sqlite3_io_methods object name */
28934  1,                        /* shared memory is disabled */
28935  afpClose,                 /* xClose method */
28936  afpLock,                  /* xLock method */
28937  afpUnlock,                /* xUnlock method */
28938  afpCheckReservedLock      /* xCheckReservedLock method */
28939)
28940#endif
28941
28942/*
28943** The proxy locking method is a "super-method" in the sense that it
28944** opens secondary file descriptors for the conch and lock files and
28945** it uses proxy, dot-file, AFP, and flock() locking methods on those
28946** secondary files.  For this reason, the division that implements
28947** proxy locking is located much further down in the file.  But we need
28948** to go ahead and define the sqlite3_io_methods and finder function
28949** for proxy locking here.  So we forward declare the I/O methods.
28950*/
28951#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28952static int proxyClose(sqlite3_file*);
28953static int proxyLock(sqlite3_file*, int);
28954static int proxyUnlock(sqlite3_file*, int);
28955static int proxyCheckReservedLock(sqlite3_file*, int*);
28956IOMETHODS(
28957  proxyIoFinder,            /* Finder function name */
28958  proxyIoMethods,           /* sqlite3_io_methods object name */
28959  1,                        /* shared memory is disabled */
28960  proxyClose,               /* xClose method */
28961  proxyLock,                /* xLock method */
28962  proxyUnlock,              /* xUnlock method */
28963  proxyCheckReservedLock    /* xCheckReservedLock method */
28964)
28965#endif
28966
28967/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
28968#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28969IOMETHODS(
28970  nfsIoFinder,               /* Finder function name */
28971  nfsIoMethods,              /* sqlite3_io_methods object name */
28972  1,                         /* shared memory is disabled */
28973  unixClose,                 /* xClose method */
28974  unixLock,                  /* xLock method */
28975  nfsUnlock,                 /* xUnlock method */
28976  unixCheckReservedLock      /* xCheckReservedLock method */
28977)
28978#endif
28979
28980#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28981/*
28982** This "finder" function attempts to determine the best locking strategy
28983** for the database file "filePath".  It then returns the sqlite3_io_methods
28984** object that implements that strategy.
28985**
28986** This is for MacOSX only.
28987*/
28988static const sqlite3_io_methods *autolockIoFinderImpl(
28989  const char *filePath,    /* name of the database file */
28990  unixFile *pNew           /* open file object for the database file */
28991){
28992  static const struct Mapping {
28993    const char *zFilesystem;              /* Filesystem type name */
28994    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
28995  } aMap[] = {
28996    { "hfs",    &posixIoMethods },
28997    { "ufs",    &posixIoMethods },
28998    { "afpfs",  &afpIoMethods },
28999    { "smbfs",  &afpIoMethods },
29000    { "webdav", &nolockIoMethods },
29001    { 0, 0 }
29002  };
29003  int i;
29004  struct statfs fsInfo;
29005  struct flock lockInfo;
29006
29007  if( !filePath ){
29008    /* If filePath==NULL that means we are dealing with a transient file
29009    ** that does not need to be locked. */
29010    return &nolockIoMethods;
29011  }
29012  if( statfs(filePath, &fsInfo) != -1 ){
29013    if( fsInfo.f_flags & MNT_RDONLY ){
29014      return &nolockIoMethods;
29015    }
29016    for(i=0; aMap[i].zFilesystem; i++){
29017      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
29018        return aMap[i].pMethods;
29019      }
29020    }
29021  }
29022
29023  /* Default case. Handles, amongst others, "nfs".
29024  ** Test byte-range lock using fcntl(). If the call succeeds,
29025  ** assume that the file-system supports POSIX style locks.
29026  */
29027  lockInfo.l_len = 1;
29028  lockInfo.l_start = 0;
29029  lockInfo.l_whence = SEEK_SET;
29030  lockInfo.l_type = F_RDLCK;
29031  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
29032    if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
29033      return &nfsIoMethods;
29034    } else {
29035      return &posixIoMethods;
29036    }
29037  }else{
29038    return &dotlockIoMethods;
29039  }
29040}
29041static const sqlite3_io_methods
29042  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
29043
29044#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
29045
29046#if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
29047/*
29048** This "finder" function attempts to determine the best locking strategy
29049** for the database file "filePath".  It then returns the sqlite3_io_methods
29050** object that implements that strategy.
29051**
29052** This is for VXWorks only.
29053*/
29054static const sqlite3_io_methods *autolockIoFinderImpl(
29055  const char *filePath,    /* name of the database file */
29056  unixFile *pNew           /* the open file object */
29057){
29058  struct flock lockInfo;
29059
29060  if( !filePath ){
29061    /* If filePath==NULL that means we are dealing with a transient file
29062    ** that does not need to be locked. */
29063    return &nolockIoMethods;
29064  }
29065
29066  /* Test if fcntl() is supported and use POSIX style locks.
29067  ** Otherwise fall back to the named semaphore method.
29068  */
29069  lockInfo.l_len = 1;
29070  lockInfo.l_start = 0;
29071  lockInfo.l_whence = SEEK_SET;
29072  lockInfo.l_type = F_RDLCK;
29073  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
29074    return &posixIoMethods;
29075  }else{
29076    return &semIoMethods;
29077  }
29078}
29079static const sqlite3_io_methods
29080  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
29081
29082#endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
29083
29084/*
29085** An abstract type for a pointer to a IO method finder function:
29086*/
29087typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
29088
29089
29090/****************************************************************************
29091**************************** sqlite3_vfs methods ****************************
29092**
29093** This division contains the implementation of methods on the
29094** sqlite3_vfs object.
29095*/
29096
29097/*
29098** Initialize the contents of the unixFile structure pointed to by pId.
29099*/
29100static int fillInUnixFile(
29101  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
29102  int h,                  /* Open file descriptor of file being opened */
29103  int syncDir,            /* True to sync directory on first sync */
29104  sqlite3_file *pId,      /* Write to the unixFile structure here */
29105  const char *zFilename,  /* Name of the file being opened */
29106  int noLock,             /* Omit locking if true */
29107  int isDelete,           /* Delete on close if true */
29108  int isReadOnly          /* True if the file is opened read-only */
29109){
29110  const sqlite3_io_methods *pLockingStyle;
29111  unixFile *pNew = (unixFile *)pId;
29112  int rc = SQLITE_OK;
29113
29114  assert( pNew->pInode==NULL );
29115
29116  /* Parameter isDelete is only used on vxworks. Express this explicitly
29117  ** here to prevent compiler warnings about unused parameters.
29118  */
29119  UNUSED_PARAMETER(isDelete);
29120
29121  /* Usually the path zFilename should not be a relative pathname. The
29122  ** exception is when opening the proxy "conch" file in builds that
29123  ** include the special Apple locking styles.
29124  */
29125#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29126  assert( zFilename==0 || zFilename[0]=='/'
29127    || pVfs->pAppData==(void*)&autolockIoFinder );
29128#else
29129  assert( zFilename==0 || zFilename[0]=='/' );
29130#endif
29131
29132  OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
29133  pNew->h = h;
29134  pNew->zPath = zFilename;
29135  if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
29136    pNew->ctrlFlags = UNIXFILE_EXCL;
29137  }else{
29138    pNew->ctrlFlags = 0;
29139  }
29140  if( isReadOnly ){
29141    pNew->ctrlFlags |= UNIXFILE_RDONLY;
29142  }
29143  if( syncDir ){
29144    pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
29145  }
29146
29147#if OS_VXWORKS
29148  pNew->pId = vxworksFindFileId(zFilename);
29149  if( pNew->pId==0 ){
29150    noLock = 1;
29151    rc = SQLITE_NOMEM;
29152  }
29153#endif
29154
29155  if( noLock ){
29156    pLockingStyle = &nolockIoMethods;
29157  }else{
29158    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
29159#if SQLITE_ENABLE_LOCKING_STYLE
29160    /* Cache zFilename in the locking context (AFP and dotlock override) for
29161    ** proxyLock activation is possible (remote proxy is based on db name)
29162    ** zFilename remains valid until file is closed, to support */
29163    pNew->lockingContext = (void*)zFilename;
29164#endif
29165  }
29166
29167  if( pLockingStyle == &posixIoMethods
29168#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29169    || pLockingStyle == &nfsIoMethods
29170#endif
29171  ){
29172    unixEnterMutex();
29173    rc = findInodeInfo(pNew, &pNew->pInode);
29174    if( rc!=SQLITE_OK ){
29175      /* If an error occured in findInodeInfo(), close the file descriptor
29176      ** immediately, before releasing the mutex. findInodeInfo() may fail
29177      ** in two scenarios:
29178      **
29179      **   (a) A call to fstat() failed.
29180      **   (b) A malloc failed.
29181      **
29182      ** Scenario (b) may only occur if the process is holding no other
29183      ** file descriptors open on the same file. If there were other file
29184      ** descriptors on this file, then no malloc would be required by
29185      ** findInodeInfo(). If this is the case, it is quite safe to close
29186      ** handle h - as it is guaranteed that no posix locks will be released
29187      ** by doing so.
29188      **
29189      ** If scenario (a) caused the error then things are not so safe. The
29190      ** implicit assumption here is that if fstat() fails, things are in
29191      ** such bad shape that dropping a lock or two doesn't matter much.
29192      */
29193      robust_close(pNew, h, __LINE__);
29194      h = -1;
29195    }
29196    unixLeaveMutex();
29197  }
29198
29199#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29200  else if( pLockingStyle == &afpIoMethods ){
29201    /* AFP locking uses the file path so it needs to be included in
29202    ** the afpLockingContext.
29203    */
29204    afpLockingContext *pCtx;
29205    pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
29206    if( pCtx==0 ){
29207      rc = SQLITE_NOMEM;
29208    }else{
29209      /* NB: zFilename exists and remains valid until the file is closed
29210      ** according to requirement F11141.  So we do not need to make a
29211      ** copy of the filename. */
29212      pCtx->dbPath = zFilename;
29213      pCtx->reserved = 0;
29214      srandomdev();
29215      unixEnterMutex();
29216      rc = findInodeInfo(pNew, &pNew->pInode);
29217      if( rc!=SQLITE_OK ){
29218        sqlite3_free(pNew->lockingContext);
29219        robust_close(pNew, h, __LINE__);
29220        h = -1;
29221      }
29222      unixLeaveMutex();
29223    }
29224  }
29225#endif
29226
29227  else if( pLockingStyle == &dotlockIoMethods ){
29228    /* Dotfile locking uses the file path so it needs to be included in
29229    ** the dotlockLockingContext
29230    */
29231    char *zLockFile;
29232    int nFilename;
29233    nFilename = (int)strlen(zFilename) + 6;
29234    zLockFile = (char *)sqlite3_malloc(nFilename);
29235    if( zLockFile==0 ){
29236      rc = SQLITE_NOMEM;
29237    }else{
29238      sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
29239    }
29240    pNew->lockingContext = zLockFile;
29241  }
29242
29243#if OS_VXWORKS
29244  else if( pLockingStyle == &semIoMethods ){
29245    /* Named semaphore locking uses the file path so it needs to be
29246    ** included in the semLockingContext
29247    */
29248    unixEnterMutex();
29249    rc = findInodeInfo(pNew, &pNew->pInode);
29250    if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
29251      char *zSemName = pNew->pInode->aSemName;
29252      int n;
29253      sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
29254                       pNew->pId->zCanonicalName);
29255      for( n=1; zSemName[n]; n++ )
29256        if( zSemName[n]=='/' ) zSemName[n] = '_';
29257      pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
29258      if( pNew->pInode->pSem == SEM_FAILED ){
29259        rc = SQLITE_NOMEM;
29260        pNew->pInode->aSemName[0] = '\0';
29261      }
29262    }
29263    unixLeaveMutex();
29264  }
29265#endif
29266
29267  pNew->lastErrno = 0;
29268#if OS_VXWORKS
29269  if( rc!=SQLITE_OK ){
29270    if( h>=0 ) robust_close(pNew, h, __LINE__);
29271    h = -1;
29272    osUnlink(zFilename);
29273    isDelete = 0;
29274  }
29275  pNew->isDelete = isDelete;
29276#endif
29277  if( rc!=SQLITE_OK ){
29278    if( h>=0 ) robust_close(pNew, h, __LINE__);
29279  }else{
29280    pNew->pMethod = pLockingStyle;
29281    OpenCounter(+1);
29282  }
29283  return rc;
29284}
29285
29286/*
29287** Return the name of a directory in which to put temporary files.
29288** If no suitable temporary file directory can be found, return NULL.
29289*/
29290static const char *unixTempFileDir(void){
29291  static const char *azDirs[] = {
29292     0,
29293     0,
29294     "/var/tmp",
29295     "/usr/tmp",
29296     "/tmp",
29297     0        /* List terminator */
29298  };
29299  unsigned int i;
29300  struct stat buf;
29301  const char *zDir = 0;
29302
29303  azDirs[0] = sqlite3_temp_directory;
29304  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
29305  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
29306    if( zDir==0 ) continue;
29307    if( osStat(zDir, &buf) ) continue;
29308    if( !S_ISDIR(buf.st_mode) ) continue;
29309    if( osAccess(zDir, 07) ) continue;
29310    break;
29311  }
29312  return zDir;
29313}
29314
29315/*
29316** Create a temporary file name in zBuf.  zBuf must be allocated
29317** by the calling process and must be big enough to hold at least
29318** pVfs->mxPathname bytes.
29319*/
29320static int unixGetTempname(int nBuf, char *zBuf){
29321  static const unsigned char zChars[] =
29322    "abcdefghijklmnopqrstuvwxyz"
29323    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
29324    "0123456789";
29325  unsigned int i, j;
29326  const char *zDir;
29327
29328  /* It's odd to simulate an io-error here, but really this is just
29329  ** using the io-error infrastructure to test that SQLite handles this
29330  ** function failing.
29331  */
29332  SimulateIOError( return SQLITE_IOERR );
29333
29334  zDir = unixTempFileDir();
29335  if( zDir==0 ) zDir = ".";
29336
29337  /* Check that the output buffer is large enough for the temporary file
29338  ** name. If it is not, return SQLITE_ERROR.
29339  */
29340  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
29341    return SQLITE_ERROR;
29342  }
29343
29344  do{
29345    sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29346    j = (int)strlen(zBuf);
29347    sqlite3_randomness(15, &zBuf[j]);
29348    for(i=0; i<15; i++, j++){
29349      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29350    }
29351    zBuf[j] = 0;
29352  }while( osAccess(zBuf,0)==0 );
29353  return SQLITE_OK;
29354}
29355
29356#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29357/*
29358** Routine to transform a unixFile into a proxy-locking unixFile.
29359** Implementation in the proxy-lock division, but used by unixOpen()
29360** if SQLITE_PREFER_PROXY_LOCKING is defined.
29361*/
29362static int proxyTransformUnixFile(unixFile*, const char*);
29363#endif
29364
29365/*
29366** Search for an unused file descriptor that was opened on the database
29367** file (not a journal or master-journal file) identified by pathname
29368** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
29369** argument to this function.
29370**
29371** Such a file descriptor may exist if a database connection was closed
29372** but the associated file descriptor could not be closed because some
29373** other file descriptor open on the same file is holding a file-lock.
29374** Refer to comments in the unixClose() function and the lengthy comment
29375** describing "Posix Advisory Locking" at the start of this file for
29376** further details. Also, ticket #4018.
29377**
29378** If a suitable file descriptor is found, then it is returned. If no
29379** such file descriptor is located, -1 is returned.
29380*/
29381static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
29382  UnixUnusedFd *pUnused = 0;
29383
29384  /* Do not search for an unused file descriptor on vxworks. Not because
29385  ** vxworks would not benefit from the change (it might, we're not sure),
29386  ** but because no way to test it is currently available. It is better
29387  ** not to risk breaking vxworks support for the sake of such an obscure
29388  ** feature.  */
29389#if !OS_VXWORKS
29390  struct stat sStat;                   /* Results of stat() call */
29391
29392  /* A stat() call may fail for various reasons. If this happens, it is
29393  ** almost certain that an open() call on the same path will also fail.
29394  ** For this reason, if an error occurs in the stat() call here, it is
29395  ** ignored and -1 is returned. The caller will try to open a new file
29396  ** descriptor on the same path, fail, and return an error to SQLite.
29397  **
29398  ** Even if a subsequent open() call does succeed, the consequences of
29399  ** not searching for a resusable file descriptor are not dire.  */
29400  if( 0==osStat(zPath, &sStat) ){
29401    unixInodeInfo *pInode;
29402
29403    unixEnterMutex();
29404    pInode = inodeList;
29405    while( pInode && (pInode->fileId.dev!=sStat.st_dev
29406                     || pInode->fileId.ino!=sStat.st_ino) ){
29407       pInode = pInode->pNext;
29408    }
29409    if( pInode ){
29410      UnixUnusedFd **pp;
29411      for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
29412      pUnused = *pp;
29413      if( pUnused ){
29414        *pp = pUnused->pNext;
29415      }
29416    }
29417    unixLeaveMutex();
29418  }
29419#endif    /* if !OS_VXWORKS */
29420  return pUnused;
29421}
29422
29423/*
29424** This function is called by unixOpen() to determine the unix permissions
29425** to create new files with. If no error occurs, then SQLITE_OK is returned
29426** and a value suitable for passing as the third argument to open(2) is
29427** written to *pMode. If an IO error occurs, an SQLite error code is
29428** returned and the value of *pMode is not modified.
29429**
29430** If the file being opened is a temporary file, it is always created with
29431** the octal permissions 0600 (read/writable by owner only). If the file
29432** is a database or master journal file, it is created with the permissions
29433** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
29434**
29435** Finally, if the file being opened is a WAL or regular journal file, then
29436** this function queries the file-system for the permissions on the
29437** corresponding database file and sets *pMode to this value. Whenever
29438** possible, WAL and journal files are created using the same permissions
29439** as the associated database file.
29440**
29441** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
29442** original filename is unavailable.  But 8_3_NAMES is only used for
29443** FAT filesystems and permissions do not matter there, so just use
29444** the default permissions.
29445*/
29446static int findCreateFileMode(
29447  const char *zPath,              /* Path of file (possibly) being created */
29448  int flags,                      /* Flags passed as 4th argument to xOpen() */
29449  mode_t *pMode                   /* OUT: Permissions to open file with */
29450){
29451  int rc = SQLITE_OK;             /* Return Code */
29452  *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
29453  if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29454    char zDb[MAX_PATHNAME+1];     /* Database file path */
29455    int nDb;                      /* Number of valid bytes in zDb */
29456    struct stat sStat;            /* Output of stat() on database file */
29457
29458    /* zPath is a path to a WAL or journal file. The following block derives
29459    ** the path to the associated database file from zPath. This block handles
29460    ** the following naming conventions:
29461    **
29462    **   "<path to db>-journal"
29463    **   "<path to db>-wal"
29464    **   "<path to db>-journalNN"
29465    **   "<path to db>-walNN"
29466    **
29467    ** where NN is a 4 digit decimal number. The NN naming schemes are
29468    ** used by the test_multiplex.c module.
29469    */
29470    nDb = sqlite3Strlen30(zPath) - 1;
29471    while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29472    if( nDb==0 ) return SQLITE_OK;
29473    memcpy(zDb, zPath, nDb);
29474    zDb[nDb] = '\0';
29475
29476    if( 0==osStat(zDb, &sStat) ){
29477      *pMode = sStat.st_mode & 0777;
29478    }else{
29479      rc = SQLITE_IOERR_FSTAT;
29480    }
29481  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
29482    *pMode = 0600;
29483  }
29484  return rc;
29485}
29486
29487/*
29488** Open the file zPath.
29489**
29490** Previously, the SQLite OS layer used three functions in place of this
29491** one:
29492**
29493**     sqlite3OsOpenReadWrite();
29494**     sqlite3OsOpenReadOnly();
29495**     sqlite3OsOpenExclusive();
29496**
29497** These calls correspond to the following combinations of flags:
29498**
29499**     ReadWrite() ->     (READWRITE | CREATE)
29500**     ReadOnly()  ->     (READONLY)
29501**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
29502**
29503** The old OpenExclusive() accepted a boolean argument - "delFlag". If
29504** true, the file was configured to be automatically deleted when the
29505** file handle closed. To achieve the same effect using this new
29506** interface, add the DELETEONCLOSE flag to those specified above for
29507** OpenExclusive().
29508*/
29509static int unixOpen(
29510  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
29511  const char *zPath,           /* Pathname of file to be opened */
29512  sqlite3_file *pFile,         /* The file descriptor to be filled in */
29513  int flags,                   /* Input flags to control the opening */
29514  int *pOutFlags               /* Output flags returned to SQLite core */
29515){
29516  unixFile *p = (unixFile *)pFile;
29517  int fd = -1;                   /* File descriptor returned by open() */
29518  int openFlags = 0;             /* Flags to pass to open() */
29519  int eType = flags&0xFFFFFF00;  /* Type of file to open */
29520  int noLock;                    /* True to omit locking primitives */
29521  int rc = SQLITE_OK;            /* Function Return Code */
29522
29523  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
29524  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
29525  int isCreate     = (flags & SQLITE_OPEN_CREATE);
29526  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
29527  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
29528#if SQLITE_ENABLE_LOCKING_STYLE
29529  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
29530#endif
29531#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29532  struct statfs fsInfo;
29533#endif
29534
29535  /* If creating a master or main-file journal, this function will open
29536  ** a file-descriptor on the directory too. The first time unixSync()
29537  ** is called the directory file descriptor will be fsync()ed and close()d.
29538  */
29539  int syncDir = (isCreate && (
29540        eType==SQLITE_OPEN_MASTER_JOURNAL
29541     || eType==SQLITE_OPEN_MAIN_JOURNAL
29542     || eType==SQLITE_OPEN_WAL
29543  ));
29544
29545  /* If argument zPath is a NULL pointer, this function is required to open
29546  ** a temporary file. Use this buffer to store the file name in.
29547  */
29548  char zTmpname[MAX_PATHNAME+1];
29549  const char *zName = zPath;
29550
29551  /* Check the following statements are true:
29552  **
29553  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
29554  **   (b) if CREATE is set, then READWRITE must also be set, and
29555  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
29556  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
29557  */
29558  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
29559  assert(isCreate==0 || isReadWrite);
29560  assert(isExclusive==0 || isCreate);
29561  assert(isDelete==0 || isCreate);
29562
29563  /* The main DB, main journal, WAL file and master journal are never
29564  ** automatically deleted. Nor are they ever temporary files.  */
29565  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
29566  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
29567  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
29568  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
29569
29570  /* Assert that the upper layer has set one of the "file-type" flags. */
29571  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
29572       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
29573       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
29574       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
29575  );
29576
29577  memset(p, 0, sizeof(unixFile));
29578
29579  if( eType==SQLITE_OPEN_MAIN_DB ){
29580    UnixUnusedFd *pUnused;
29581    pUnused = findReusableFd(zName, flags);
29582    if( pUnused ){
29583      fd = pUnused->fd;
29584    }else{
29585      pUnused = sqlite3_malloc(sizeof(*pUnused));
29586      if( !pUnused ){
29587        return SQLITE_NOMEM;
29588      }
29589    }
29590    p->pUnused = pUnused;
29591  }else if( !zName ){
29592    /* If zName is NULL, the upper layer is requesting a temp file. */
29593    assert(isDelete && !syncDir);
29594    rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29595    if( rc!=SQLITE_OK ){
29596      return rc;
29597    }
29598    zName = zTmpname;
29599  }
29600
29601  /* Determine the value of the flags parameter passed to POSIX function
29602  ** open(). These must be calculated even if open() is not called, as
29603  ** they may be stored as part of the file handle and used by the
29604  ** 'conch file' locking functions later on.  */
29605  if( isReadonly )  openFlags |= O_RDONLY;
29606  if( isReadWrite ) openFlags |= O_RDWR;
29607  if( isCreate )    openFlags |= O_CREAT;
29608  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
29609  openFlags |= (O_LARGEFILE|O_BINARY);
29610
29611  if( fd<0 ){
29612    mode_t openMode;              /* Permissions to create file with */
29613    rc = findCreateFileMode(zName, flags, &openMode);
29614    if( rc!=SQLITE_OK ){
29615      assert( !p->pUnused );
29616      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
29617      return rc;
29618    }
29619    fd = robust_open(zName, openFlags, openMode);
29620    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
29621    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
29622      /* Failed to open the file for read/write access. Try read-only. */
29623      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
29624      openFlags &= ~(O_RDWR|O_CREAT);
29625      flags |= SQLITE_OPEN_READONLY;
29626      openFlags |= O_RDONLY;
29627      isReadonly = 1;
29628      fd = robust_open(zName, openFlags, openMode);
29629    }
29630    if( fd<0 ){
29631      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
29632      goto open_finished;
29633    }
29634  }
29635  assert( fd>=0 );
29636  if( pOutFlags ){
29637    *pOutFlags = flags;
29638  }
29639
29640  if( p->pUnused ){
29641    p->pUnused->fd = fd;
29642    p->pUnused->flags = flags;
29643  }
29644
29645  if( isDelete ){
29646#if OS_VXWORKS
29647    zPath = zName;
29648#else
29649    osUnlink(zName);
29650#endif
29651  }
29652#if SQLITE_ENABLE_LOCKING_STYLE
29653  else{
29654    p->openFlags = openFlags;
29655  }
29656#endif
29657
29658#ifdef FD_CLOEXEC
29659  osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29660#endif
29661
29662  noLock = eType!=SQLITE_OPEN_MAIN_DB;
29663
29664
29665#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29666  if( fstatfs(fd, &fsInfo) == -1 ){
29667    ((unixFile*)pFile)->lastErrno = errno;
29668    robust_close(p, fd, __LINE__);
29669    return SQLITE_IOERR_ACCESS;
29670  }
29671  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29672    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29673  }
29674#endif
29675
29676#if SQLITE_ENABLE_LOCKING_STYLE
29677#if SQLITE_PREFER_PROXY_LOCKING
29678  isAutoProxy = 1;
29679#endif
29680  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
29681    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
29682    int useProxy = 0;
29683
29684    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29685    ** never use proxy, NULL means use proxy for non-local files only.  */
29686    if( envforce!=NULL ){
29687      useProxy = atoi(envforce)>0;
29688    }else{
29689      if( statfs(zPath, &fsInfo) == -1 ){
29690        /* In theory, the close(fd) call is sub-optimal. If the file opened
29691        ** with fd is a database file, and there are other connections open
29692        ** on that file that are currently holding advisory locks on it,
29693        ** then the call to close() will cancel those locks. In practice,
29694        ** we're assuming that statfs() doesn't fail very often. At least
29695        ** not while other file descriptors opened by the same process on
29696        ** the same file are working.  */
29697        p->lastErrno = errno;
29698        robust_close(p, fd, __LINE__);
29699        rc = SQLITE_IOERR_ACCESS;
29700        goto open_finished;
29701      }
29702      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29703    }
29704    if( useProxy ){
29705      rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29706                          isDelete, isReadonly);
29707      if( rc==SQLITE_OK ){
29708        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29709        if( rc!=SQLITE_OK ){
29710          /* Use unixClose to clean up the resources added in fillInUnixFile
29711          ** and clear all the structure's references.  Specifically,
29712          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
29713          */
29714          unixClose(pFile);
29715          return rc;
29716        }
29717      }
29718      goto open_finished;
29719    }
29720  }
29721#endif
29722
29723  rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29724                      isDelete, isReadonly);
29725open_finished:
29726  if( rc!=SQLITE_OK ){
29727    sqlite3_free(p->pUnused);
29728  }
29729  return rc;
29730}
29731
29732
29733/*
29734** Delete the file at zPath. If the dirSync argument is true, fsync()
29735** the directory after deleting the file.
29736*/
29737static int unixDelete(
29738  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
29739  const char *zPath,        /* Name of file to be deleted */
29740  int dirSync               /* If true, fsync() directory after deleting file */
29741){
29742  int rc = SQLITE_OK;
29743  UNUSED_PARAMETER(NotUsed);
29744  SimulateIOError(return SQLITE_IOERR_DELETE);
29745  if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
29746    return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29747  }
29748#ifndef SQLITE_DISABLE_DIRSYNC
29749  if( dirSync ){
29750    int fd;
29751    rc = osOpenDirectory(zPath, &fd);
29752    if( rc==SQLITE_OK ){
29753#if OS_VXWORKS
29754      if( fsync(fd)==-1 )
29755#else
29756      if( fsync(fd) )
29757#endif
29758      {
29759        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29760      }
29761      robust_close(0, fd, __LINE__);
29762    }else if( rc==SQLITE_CANTOPEN ){
29763      rc = SQLITE_OK;
29764    }
29765  }
29766#endif
29767  return rc;
29768}
29769
29770/*
29771** Test the existance of or access permissions of file zPath. The
29772** test performed depends on the value of flags:
29773**
29774**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
29775**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
29776**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
29777**
29778** Otherwise return 0.
29779*/
29780static int unixAccess(
29781  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
29782  const char *zPath,      /* Path of the file to examine */
29783  int flags,              /* What do we want to learn about the zPath file? */
29784  int *pResOut            /* Write result boolean here */
29785){
29786  int amode = 0;
29787  UNUSED_PARAMETER(NotUsed);
29788  SimulateIOError( return SQLITE_IOERR_ACCESS; );
29789  switch( flags ){
29790    case SQLITE_ACCESS_EXISTS:
29791      amode = F_OK;
29792      break;
29793    case SQLITE_ACCESS_READWRITE:
29794      amode = W_OK|R_OK;
29795      break;
29796    case SQLITE_ACCESS_READ:
29797      amode = R_OK;
29798      break;
29799
29800    default:
29801      assert(!"Invalid flags argument");
29802  }
29803  *pResOut = (osAccess(zPath, amode)==0);
29804  if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29805    struct stat buf;
29806    if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
29807      *pResOut = 0;
29808    }
29809  }
29810  return SQLITE_OK;
29811}
29812
29813
29814/*
29815** Turn a relative pathname into a full pathname. The relative path
29816** is stored as a nul-terminated string in the buffer pointed to by
29817** zPath.
29818**
29819** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
29820** (in this case, MAX_PATHNAME bytes). The full-path is written to
29821** this buffer before returning.
29822*/
29823static int unixFullPathname(
29824  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
29825  const char *zPath,            /* Possibly relative input path */
29826  int nOut,                     /* Size of output buffer in bytes */
29827  char *zOut                    /* Output buffer */
29828){
29829
29830  /* It's odd to simulate an io-error here, but really this is just
29831  ** using the io-error infrastructure to test that SQLite handles this
29832  ** function failing. This function could fail if, for example, the
29833  ** current working directory has been unlinked.
29834  */
29835  SimulateIOError( return SQLITE_ERROR );
29836
29837  assert( pVfs->mxPathname==MAX_PATHNAME );
29838  UNUSED_PARAMETER(pVfs);
29839
29840  zOut[nOut-1] = '\0';
29841  if( zPath[0]=='/' ){
29842    sqlite3_snprintf(nOut, zOut, "%s", zPath);
29843  }else{
29844    int nCwd;
29845    if( osGetcwd(zOut, nOut-1)==0 ){
29846      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
29847    }
29848    nCwd = (int)strlen(zOut);
29849    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
29850  }
29851  return SQLITE_OK;
29852}
29853
29854
29855#ifndef SQLITE_OMIT_LOAD_EXTENSION
29856/*
29857** Interfaces for opening a shared library, finding entry points
29858** within the shared library, and closing the shared library.
29859*/
29860#include <dlfcn.h>
29861static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
29862  UNUSED_PARAMETER(NotUsed);
29863  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
29864}
29865
29866/*
29867** SQLite calls this function immediately after a call to unixDlSym() or
29868** unixDlOpen() fails (returns a null pointer). If a more detailed error
29869** message is available, it is written to zBufOut. If no error message
29870** is available, zBufOut is left unmodified and SQLite uses a default
29871** error message.
29872*/
29873static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
29874  const char *zErr;
29875  UNUSED_PARAMETER(NotUsed);
29876  unixEnterMutex();
29877  zErr = dlerror();
29878  if( zErr ){
29879    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
29880  }
29881  unixLeaveMutex();
29882}
29883static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
29884  /*
29885  ** GCC with -pedantic-errors says that C90 does not allow a void* to be
29886  ** cast into a pointer to a function.  And yet the library dlsym() routine
29887  ** returns a void* which is really a pointer to a function.  So how do we
29888  ** use dlsym() with -pedantic-errors?
29889  **
29890  ** Variable x below is defined to be a pointer to a function taking
29891  ** parameters void* and const char* and returning a pointer to a function.
29892  ** We initialize x by assigning it a pointer to the dlsym() function.
29893  ** (That assignment requires a cast.)  Then we call the function that
29894  ** x points to.
29895  **
29896  ** This work-around is unlikely to work correctly on any system where
29897  ** you really cannot cast a function pointer into void*.  But then, on the
29898  ** other hand, dlsym() will not work on such a system either, so we have
29899  ** not really lost anything.
29900  */
29901  void (*(*x)(void*,const char*))(void);
29902  UNUSED_PARAMETER(NotUsed);
29903  x = (void(*(*)(void*,const char*))(void))dlsym;
29904  return (*x)(p, zSym);
29905}
29906static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
29907  UNUSED_PARAMETER(NotUsed);
29908  dlclose(pHandle);
29909}
29910#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
29911  #define unixDlOpen  0
29912  #define unixDlError 0
29913  #define unixDlSym   0
29914  #define unixDlClose 0
29915#endif
29916
29917/*
29918** Write nBuf bytes of random data to the supplied buffer zBuf.
29919*/
29920static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
29921  UNUSED_PARAMETER(NotUsed);
29922  assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
29923
29924  /* We have to initialize zBuf to prevent valgrind from reporting
29925  ** errors.  The reports issued by valgrind are incorrect - we would
29926  ** prefer that the randomness be increased by making use of the
29927  ** uninitialized space in zBuf - but valgrind errors tend to worry
29928  ** some users.  Rather than argue, it seems easier just to initialize
29929  ** the whole array and silence valgrind, even if that means less randomness
29930  ** in the random seed.
29931  **
29932  ** When testing, initializing zBuf[] to zero is all we do.  That means
29933  ** that we always use the same random number sequence.  This makes the
29934  ** tests repeatable.
29935  */
29936  memset(zBuf, 0, nBuf);
29937#if !defined(SQLITE_TEST)
29938  {
29939    int pid, fd;
29940    fd = robust_open("/dev/urandom", O_RDONLY, 0);
29941    if( fd<0 ){
29942      time_t t;
29943      time(&t);
29944      memcpy(zBuf, &t, sizeof(t));
29945      pid = getpid();
29946      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
29947      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
29948      nBuf = sizeof(t) + sizeof(pid);
29949    }else{
29950      do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
29951      robust_close(0, fd, __LINE__);
29952    }
29953  }
29954#endif
29955  return nBuf;
29956}
29957
29958
29959/*
29960** Sleep for a little while.  Return the amount of time slept.
29961** The argument is the number of microseconds we want to sleep.
29962** The return value is the number of microseconds of sleep actually
29963** requested from the underlying operating system, a number which
29964** might be greater than or equal to the argument, but not less
29965** than the argument.
29966*/
29967static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
29968#if OS_VXWORKS
29969  struct timespec sp;
29970
29971  sp.tv_sec = microseconds / 1000000;
29972  sp.tv_nsec = (microseconds % 1000000) * 1000;
29973  nanosleep(&sp, NULL);
29974  UNUSED_PARAMETER(NotUsed);
29975  return microseconds;
29976#elif defined(HAVE_USLEEP) && HAVE_USLEEP
29977  usleep(microseconds);
29978  UNUSED_PARAMETER(NotUsed);
29979  return microseconds;
29980#else
29981  int seconds = (microseconds+999999)/1000000;
29982  sleep(seconds);
29983  UNUSED_PARAMETER(NotUsed);
29984  return seconds*1000000;
29985#endif
29986}
29987
29988/*
29989** The following variable, if set to a non-zero value, is interpreted as
29990** the number of seconds since 1970 and is used to set the result of
29991** sqlite3OsCurrentTime() during testing.
29992*/
29993#ifdef SQLITE_TEST
29994SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
29995#endif
29996
29997/*
29998** Find the current time (in Universal Coordinated Time).  Write into *piNow
29999** the current time and date as a Julian Day number times 86_400_000.  In
30000** other words, write into *piNow the number of milliseconds since the Julian
30001** epoch of noon in Greenwich on November 24, 4714 B.C according to the
30002** proleptic Gregorian calendar.
30003**
30004** On success, return 0.  Return 1 if the time and date cannot be found.
30005*/
30006static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
30007  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
30008#if defined(NO_GETTOD)
30009  time_t t;
30010  time(&t);
30011  *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
30012#elif OS_VXWORKS
30013  struct timespec sNow;
30014  clock_gettime(CLOCK_REALTIME, &sNow);
30015  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
30016#else
30017  struct timeval sNow;
30018  gettimeofday(&sNow, 0);
30019  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
30020#endif
30021
30022#ifdef SQLITE_TEST
30023  if( sqlite3_current_time ){
30024    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
30025  }
30026#endif
30027  UNUSED_PARAMETER(NotUsed);
30028  return 0;
30029}
30030
30031/*
30032** Find the current time (in Universal Coordinated Time).  Write the
30033** current time and date as a Julian Day number into *prNow and
30034** return 0.  Return 1 if the time and date cannot be found.
30035*/
30036static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
30037  sqlite3_int64 i;
30038  UNUSED_PARAMETER(NotUsed);
30039  unixCurrentTimeInt64(0, &i);
30040  *prNow = i/86400000.0;
30041  return 0;
30042}
30043
30044/*
30045** We added the xGetLastError() method with the intention of providing
30046** better low-level error messages when operating-system problems come up
30047** during SQLite operation.  But so far, none of that has been implemented
30048** in the core.  So this routine is never called.  For now, it is merely
30049** a place-holder.
30050*/
30051static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
30052  UNUSED_PARAMETER(NotUsed);
30053  UNUSED_PARAMETER(NotUsed2);
30054  UNUSED_PARAMETER(NotUsed3);
30055  return 0;
30056}
30057
30058
30059/*
30060************************ End of sqlite3_vfs methods ***************************
30061******************************************************************************/
30062
30063/******************************************************************************
30064************************** Begin Proxy Locking ********************************
30065**
30066** Proxy locking is a "uber-locking-method" in this sense:  It uses the
30067** other locking methods on secondary lock files.  Proxy locking is a
30068** meta-layer over top of the primitive locking implemented above.  For
30069** this reason, the division that implements of proxy locking is deferred
30070** until late in the file (here) after all of the other I/O methods have
30071** been defined - so that the primitive locking methods are available
30072** as services to help with the implementation of proxy locking.
30073**
30074****
30075**
30076** The default locking schemes in SQLite use byte-range locks on the
30077** database file to coordinate safe, concurrent access by multiple readers
30078** and writers [http://sqlite.org/lockingv3.html].  The five file locking
30079** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
30080** as POSIX read & write locks over fixed set of locations (via fsctl),
30081** on AFP and SMB only exclusive byte-range locks are available via fsctl
30082** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
30083** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
30084** address in the shared range is taken for a SHARED lock, the entire
30085** shared range is taken for an EXCLUSIVE lock):
30086**
30087**      PENDING_BYTE        0x40000000
30088**      RESERVED_BYTE       0x40000001
30089**      SHARED_RANGE        0x40000002 -> 0x40000200
30090**
30091** This works well on the local file system, but shows a nearly 100x
30092** slowdown in read performance on AFP because the AFP client disables
30093** the read cache when byte-range locks are present.  Enabling the read
30094** cache exposes a cache coherency problem that is present on all OS X
30095** supported network file systems.  NFS and AFP both observe the
30096** close-to-open semantics for ensuring cache coherency
30097** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
30098** address the requirements for concurrent database access by multiple
30099** readers and writers
30100** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
30101**
30102** To address the performance and cache coherency issues, proxy file locking
30103** changes the way database access is controlled by limiting access to a
30104** single host at a time and moving file locks off of the database file
30105** and onto a proxy file on the local file system.
30106**
30107**
30108** Using proxy locks
30109** -----------------
30110**
30111** C APIs
30112**
30113**  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
30114**                       <proxy_path> | ":auto:");
30115**  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
30116**
30117**
30118** SQL pragmas
30119**
30120**  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
30121**  PRAGMA [database.]lock_proxy_file
30122**
30123** Specifying ":auto:" means that if there is a conch file with a matching
30124** host ID in it, the proxy path in the conch file will be used, otherwise
30125** a proxy path based on the user's temp dir
30126** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
30127** actual proxy file name is generated from the name and path of the
30128** database file.  For example:
30129**
30130**       For database path "/Users/me/foo.db"
30131**       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
30132**
30133** Once a lock proxy is configured for a database connection, it can not
30134** be removed, however it may be switched to a different proxy path via
30135** the above APIs (assuming the conch file is not being held by another
30136** connection or process).
30137**
30138**
30139** How proxy locking works
30140** -----------------------
30141**
30142** Proxy file locking relies primarily on two new supporting files:
30143**
30144**   *  conch file to limit access to the database file to a single host
30145**      at a time
30146**
30147**   *  proxy file to act as a proxy for the advisory locks normally
30148**      taken on the database
30149**
30150** The conch file - to use a proxy file, sqlite must first "hold the conch"
30151** by taking an sqlite-style shared lock on the conch file, reading the
30152** contents and comparing the host's unique host ID (see below) and lock
30153** proxy path against the values stored in the conch.  The conch file is
30154** stored in the same directory as the database file and the file name
30155** is patterned after the database file name as ".<databasename>-conch".
30156** If the conch file does not exist, or it's contents do not match the
30157** host ID and/or proxy path, then the lock is escalated to an exclusive
30158** lock and the conch file contents is updated with the host ID and proxy
30159** path and the lock is downgraded to a shared lock again.  If the conch
30160** is held by another process (with a shared lock), the exclusive lock
30161** will fail and SQLITE_BUSY is returned.
30162**
30163** The proxy file - a single-byte file used for all advisory file locks
30164** normally taken on the database file.   This allows for safe sharing
30165** of the database file for multiple readers and writers on the same
30166** host (the conch ensures that they all use the same local lock file).
30167**
30168** Requesting the lock proxy does not immediately take the conch, it is
30169** only taken when the first request to lock database file is made.
30170** This matches the semantics of the traditional locking behavior, where
30171** opening a connection to a database file does not take a lock on it.
30172** The shared lock and an open file descriptor are maintained until
30173** the connection to the database is closed.
30174**
30175** The proxy file and the lock file are never deleted so they only need
30176** to be created the first time they are used.
30177**
30178** Configuration options
30179** ---------------------
30180**
30181**  SQLITE_PREFER_PROXY_LOCKING
30182**
30183**       Database files accessed on non-local file systems are
30184**       automatically configured for proxy locking, lock files are
30185**       named automatically using the same logic as
30186**       PRAGMA lock_proxy_file=":auto:"
30187**
30188**  SQLITE_PROXY_DEBUG
30189**
30190**       Enables the logging of error messages during host id file
30191**       retrieval and creation
30192**
30193**  LOCKPROXYDIR
30194**
30195**       Overrides the default directory used for lock proxy files that
30196**       are named automatically via the ":auto:" setting
30197**
30198**  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
30199**
30200**       Permissions to use when creating a directory for storing the
30201**       lock proxy files, only used when LOCKPROXYDIR is not set.
30202**
30203**
30204** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
30205** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
30206** force proxy locking to be used for every database file opened, and 0
30207** will force automatic proxy locking to be disabled for all database
30208** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
30209** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
30210*/
30211
30212/*
30213** Proxy locking is only available on MacOSX
30214*/
30215#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
30216
30217/*
30218** The proxyLockingContext has the path and file structures for the remote
30219** and local proxy files in it
30220*/
30221typedef struct proxyLockingContext proxyLockingContext;
30222struct proxyLockingContext {
30223  unixFile *conchFile;         /* Open conch file */
30224  char *conchFilePath;         /* Name of the conch file */
30225  unixFile *lockProxy;         /* Open proxy lock file */
30226  char *lockProxyPath;         /* Name of the proxy lock file */
30227  char *dbPath;                /* Name of the open file */
30228  int conchHeld;               /* 1 if the conch is held, -1 if lockless */
30229  void *oldLockingContext;     /* Original lockingcontext to restore on close */
30230  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
30231};
30232
30233/*
30234** The proxy lock file path for the database at dbPath is written into lPath,
30235** which must point to valid, writable memory large enough for a maxLen length
30236** file path.
30237*/
30238static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
30239  int len;
30240  int dbLen;
30241  int i;
30242
30243#ifdef LOCKPROXYDIR
30244  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
30245#else
30246# ifdef _CS_DARWIN_USER_TEMP_DIR
30247  {
30248    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
30249      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
30250               lPath, errno, getpid()));
30251      return SQLITE_IOERR_LOCK;
30252    }
30253    len = strlcat(lPath, "sqliteplocks", maxLen);
30254  }
30255# else
30256  len = strlcpy(lPath, "/tmp/", maxLen);
30257# endif
30258#endif
30259
30260  if( lPath[len-1]!='/' ){
30261    len = strlcat(lPath, "/", maxLen);
30262  }
30263
30264  /* transform the db path to a unique cache name */
30265  dbLen = (int)strlen(dbPath);
30266  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
30267    char c = dbPath[i];
30268    lPath[i+len] = (c=='/')?'_':c;
30269  }
30270  lPath[i+len]='\0';
30271  strlcat(lPath, ":auto:", maxLen);
30272  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
30273  return SQLITE_OK;
30274}
30275
30276/*
30277 ** Creates the lock file and any missing directories in lockPath
30278 */
30279static int proxyCreateLockPath(const char *lockPath){
30280  int i, len;
30281  char buf[MAXPATHLEN];
30282  int start = 0;
30283
30284  assert(lockPath!=NULL);
30285  /* try to create all the intermediate directories */
30286  len = (int)strlen(lockPath);
30287  buf[0] = lockPath[0];
30288  for( i=1; i<len; i++ ){
30289    if( lockPath[i] == '/' && (i - start > 0) ){
30290      /* only mkdir if leaf dir != "." or "/" or ".." */
30291      if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30292         || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30293        buf[i]='\0';
30294        if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30295          int err=errno;
30296          if( err!=EEXIST ) {
30297            OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
30298                     "'%s' proxy lock path=%s pid=%d\n",
30299                     buf, strerror(err), lockPath, getpid()));
30300            return err;
30301          }
30302        }
30303      }
30304      start=i+1;
30305    }
30306    buf[i] = lockPath[i];
30307  }
30308  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
30309  return 0;
30310}
30311
30312/*
30313** Create a new VFS file descriptor (stored in memory obtained from
30314** sqlite3_malloc) and open the file named "path" in the file descriptor.
30315**
30316** The caller is responsible not only for closing the file descriptor
30317** but also for freeing the memory associated with the file descriptor.
30318*/
30319static int proxyCreateUnixFile(
30320    const char *path,        /* path for the new unixFile */
30321    unixFile **ppFile,       /* unixFile created and returned by ref */
30322    int islockfile           /* if non zero missing dirs will be created */
30323) {
30324  int fd = -1;
30325  unixFile *pNew;
30326  int rc = SQLITE_OK;
30327  int openFlags = O_RDWR | O_CREAT;
30328  sqlite3_vfs dummyVfs;
30329  int terrno = 0;
30330  UnixUnusedFd *pUnused = NULL;
30331
30332  /* 1. first try to open/create the file
30333  ** 2. if that fails, and this is a lock file (not-conch), try creating
30334  ** the parent directories and then try again.
30335  ** 3. if that fails, try to open the file read-only
30336  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
30337  */
30338  pUnused = findReusableFd(path, openFlags);
30339  if( pUnused ){
30340    fd = pUnused->fd;
30341  }else{
30342    pUnused = sqlite3_malloc(sizeof(*pUnused));
30343    if( !pUnused ){
30344      return SQLITE_NOMEM;
30345    }
30346  }
30347  if( fd<0 ){
30348    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30349    terrno = errno;
30350    if( fd<0 && errno==ENOENT && islockfile ){
30351      if( proxyCreateLockPath(path) == SQLITE_OK ){
30352        fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30353      }
30354    }
30355  }
30356  if( fd<0 ){
30357    openFlags = O_RDONLY;
30358    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
30359    terrno = errno;
30360  }
30361  if( fd<0 ){
30362    if( islockfile ){
30363      return SQLITE_BUSY;
30364    }
30365    switch (terrno) {
30366      case EACCES:
30367        return SQLITE_PERM;
30368      case EIO:
30369        return SQLITE_IOERR_LOCK; /* even though it is the conch */
30370      default:
30371        return SQLITE_CANTOPEN_BKPT;
30372    }
30373  }
30374
30375  pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
30376  if( pNew==NULL ){
30377    rc = SQLITE_NOMEM;
30378    goto end_create_proxy;
30379  }
30380  memset(pNew, 0, sizeof(unixFile));
30381  pNew->openFlags = openFlags;
30382  memset(&dummyVfs, 0, sizeof(dummyVfs));
30383  dummyVfs.pAppData = (void*)&autolockIoFinder;
30384  dummyVfs.zName = "dummy";
30385  pUnused->fd = fd;
30386  pUnused->flags = openFlags;
30387  pNew->pUnused = pUnused;
30388
30389  rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
30390  if( rc==SQLITE_OK ){
30391    *ppFile = pNew;
30392    return SQLITE_OK;
30393  }
30394end_create_proxy:
30395  robust_close(pNew, fd, __LINE__);
30396  sqlite3_free(pNew);
30397  sqlite3_free(pUnused);
30398  return rc;
30399}
30400
30401#ifdef SQLITE_TEST
30402/* simulate multiple hosts by creating unique hostid file paths */
30403SQLITE_API int sqlite3_hostid_num = 0;
30404#endif
30405
30406#define PROXY_HOSTIDLEN    16  /* conch file host id length */
30407
30408/* Not always defined in the headers as it ought to be */
30409extern int gethostuuid(uuid_t id, const struct timespec *wait);
30410
30411/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
30412** bytes of writable memory.
30413*/
30414static int proxyGetHostID(unsigned char *pHostID, int *pError){
30415  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
30416  memset(pHostID, 0, PROXY_HOSTIDLEN);
30417#if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
30418               && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
30419  {
30420    static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
30421    if( gethostuuid(pHostID, &timeout) ){
30422      int err = errno;
30423      if( pError ){
30424        *pError = err;
30425      }
30426      return SQLITE_IOERR;
30427    }
30428  }
30429#else
30430  UNUSED_PARAMETER(pError);
30431#endif
30432#ifdef SQLITE_TEST
30433  /* simulate multiple hosts by creating unique hostid file paths */
30434  if( sqlite3_hostid_num != 0){
30435    pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
30436  }
30437#endif
30438
30439  return SQLITE_OK;
30440}
30441
30442/* The conch file contains the header, host id and lock file path
30443 */
30444#define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
30445#define PROXY_HEADERLEN    1   /* conch file header length */
30446#define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
30447#define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
30448
30449/*
30450** Takes an open conch file, copies the contents to a new path and then moves
30451** it back.  The newly created file's file descriptor is assigned to the
30452** conch file structure and finally the original conch file descriptor is
30453** closed.  Returns zero if successful.
30454*/
30455static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
30456  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30457  unixFile *conchFile = pCtx->conchFile;
30458  char tPath[MAXPATHLEN];
30459  char buf[PROXY_MAXCONCHLEN];
30460  char *cPath = pCtx->conchFilePath;
30461  size_t readLen = 0;
30462  size_t pathLen = 0;
30463  char errmsg[64] = "";
30464  int fd = -1;
30465  int rc = -1;
30466  UNUSED_PARAMETER(myHostID);
30467
30468  /* create a new path by replace the trailing '-conch' with '-break' */
30469  pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
30470  if( pathLen>MAXPATHLEN || pathLen<6 ||
30471     (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
30472    sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
30473    goto end_breaklock;
30474  }
30475  /* read the conch content */
30476  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
30477  if( readLen<PROXY_PATHINDEX ){
30478    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
30479    goto end_breaklock;
30480  }
30481  /* write it out to the temporary break file */
30482  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
30483                   SQLITE_DEFAULT_FILE_PERMISSIONS);
30484  if( fd<0 ){
30485    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
30486    goto end_breaklock;
30487  }
30488  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
30489    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
30490    goto end_breaklock;
30491  }
30492  if( rename(tPath, cPath) ){
30493    sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
30494    goto end_breaklock;
30495  }
30496  rc = 0;
30497  fprintf(stderr, "broke stale lock on %s\n", cPath);
30498  robust_close(pFile, conchFile->h, __LINE__);
30499  conchFile->h = fd;
30500  conchFile->openFlags = O_RDWR | O_CREAT;
30501
30502end_breaklock:
30503  if( rc ){
30504    if( fd>=0 ){
30505      osUnlink(tPath);
30506      robust_close(pFile, fd, __LINE__);
30507    }
30508    fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30509  }
30510  return rc;
30511}
30512
30513/* Take the requested lock on the conch file and break a stale lock if the
30514** host id matches.
30515*/
30516static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
30517  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30518  unixFile *conchFile = pCtx->conchFile;
30519  int rc = SQLITE_OK;
30520  int nTries = 0;
30521  struct timespec conchModTime;
30522
30523  memset(&conchModTime, 0, sizeof(conchModTime));
30524  do {
30525    rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30526    nTries ++;
30527    if( rc==SQLITE_BUSY ){
30528      /* If the lock failed (busy):
30529       * 1st try: get the mod time of the conch, wait 0.5s and try again.
30530       * 2nd try: fail if the mod time changed or host id is different, wait
30531       *           10 sec and try again
30532       * 3rd try: break the lock unless the mod time has changed.
30533       */
30534      struct stat buf;
30535      if( osFstat(conchFile->h, &buf) ){
30536        pFile->lastErrno = errno;
30537        return SQLITE_IOERR_LOCK;
30538      }
30539
30540      if( nTries==1 ){
30541        conchModTime = buf.st_mtimespec;
30542        usleep(500000); /* wait 0.5 sec and try the lock again*/
30543        continue;
30544      }
30545
30546      assert( nTries>1 );
30547      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
30548         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
30549        return SQLITE_BUSY;
30550      }
30551
30552      if( nTries==2 ){
30553        char tBuf[PROXY_MAXCONCHLEN];
30554        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
30555        if( len<0 ){
30556          pFile->lastErrno = errno;
30557          return SQLITE_IOERR_LOCK;
30558        }
30559        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
30560          /* don't break the lock if the host id doesn't match */
30561          if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
30562            return SQLITE_BUSY;
30563          }
30564        }else{
30565          /* don't break the lock on short read or a version mismatch */
30566          return SQLITE_BUSY;
30567        }
30568        usleep(10000000); /* wait 10 sec and try the lock again */
30569        continue;
30570      }
30571
30572      assert( nTries==3 );
30573      if( 0==proxyBreakConchLock(pFile, myHostID) ){
30574        rc = SQLITE_OK;
30575        if( lockType==EXCLUSIVE_LOCK ){
30576          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
30577        }
30578        if( !rc ){
30579          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30580        }
30581      }
30582    }
30583  } while( rc==SQLITE_BUSY && nTries<3 );
30584
30585  return rc;
30586}
30587
30588/* Takes the conch by taking a shared lock and read the contents conch, if
30589** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
30590** lockPath means that the lockPath in the conch file will be used if the
30591** host IDs match, or a new lock path will be generated automatically
30592** and written to the conch file.
30593*/
30594static int proxyTakeConch(unixFile *pFile){
30595  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30596
30597  if( pCtx->conchHeld!=0 ){
30598    return SQLITE_OK;
30599  }else{
30600    unixFile *conchFile = pCtx->conchFile;
30601    uuid_t myHostID;
30602    int pError = 0;
30603    char readBuf[PROXY_MAXCONCHLEN];
30604    char lockPath[MAXPATHLEN];
30605    char *tempLockPath = NULL;
30606    int rc = SQLITE_OK;
30607    int createConch = 0;
30608    int hostIdMatch = 0;
30609    int readLen = 0;
30610    int tryOldLockPath = 0;
30611    int forceNewLockPath = 0;
30612
30613    OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
30614             (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
30615
30616    rc = proxyGetHostID(myHostID, &pError);
30617    if( (rc&0xff)==SQLITE_IOERR ){
30618      pFile->lastErrno = pError;
30619      goto end_takeconch;
30620    }
30621    rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
30622    if( rc!=SQLITE_OK ){
30623      goto end_takeconch;
30624    }
30625    /* read the existing conch file */
30626    readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
30627    if( readLen<0 ){
30628      /* I/O error: lastErrno set by seekAndRead */
30629      pFile->lastErrno = conchFile->lastErrno;
30630      rc = SQLITE_IOERR_READ;
30631      goto end_takeconch;
30632    }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
30633             readBuf[0]!=(char)PROXY_CONCHVERSION ){
30634      /* a short read or version format mismatch means we need to create a new
30635      ** conch file.
30636      */
30637      createConch = 1;
30638    }
30639    /* if the host id matches and the lock path already exists in the conch
30640    ** we'll try to use the path there, if we can't open that path, we'll
30641    ** retry with a new auto-generated path
30642    */
30643    do { /* in case we need to try again for an :auto: named lock file */
30644
30645      if( !createConch && !forceNewLockPath ){
30646        hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
30647                                  PROXY_HOSTIDLEN);
30648        /* if the conch has data compare the contents */
30649        if( !pCtx->lockProxyPath ){
30650          /* for auto-named local lock file, just check the host ID and we'll
30651           ** use the local lock file path that's already in there
30652           */
30653          if( hostIdMatch ){
30654            size_t pathLen = (readLen - PROXY_PATHINDEX);
30655
30656            if( pathLen>=MAXPATHLEN ){
30657              pathLen=MAXPATHLEN-1;
30658            }
30659            memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
30660            lockPath[pathLen] = 0;
30661            tempLockPath = lockPath;
30662            tryOldLockPath = 1;
30663            /* create a copy of the lock path if the conch is taken */
30664            goto end_takeconch;
30665          }
30666        }else if( hostIdMatch
30667               && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
30668                           readLen-PROXY_PATHINDEX)
30669        ){
30670          /* conch host and lock path match */
30671          goto end_takeconch;
30672        }
30673      }
30674
30675      /* if the conch isn't writable and doesn't match, we can't take it */
30676      if( (conchFile->openFlags&O_RDWR) == 0 ){
30677        rc = SQLITE_BUSY;
30678        goto end_takeconch;
30679      }
30680
30681      /* either the conch didn't match or we need to create a new one */
30682      if( !pCtx->lockProxyPath ){
30683        proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
30684        tempLockPath = lockPath;
30685        /* create a copy of the lock path _only_ if the conch is taken */
30686      }
30687
30688      /* update conch with host and path (this will fail if other process
30689      ** has a shared lock already), if the host id matches, use the big
30690      ** stick.
30691      */
30692      futimes(conchFile->h, NULL);
30693      if( hostIdMatch && !createConch ){
30694        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
30695          /* We are trying for an exclusive lock but another thread in this
30696           ** same process is still holding a shared lock. */
30697          rc = SQLITE_BUSY;
30698        } else {
30699          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
30700        }
30701      }else{
30702        rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
30703      }
30704      if( rc==SQLITE_OK ){
30705        char writeBuffer[PROXY_MAXCONCHLEN];
30706        int writeSize = 0;
30707
30708        writeBuffer[0] = (char)PROXY_CONCHVERSION;
30709        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
30710        if( pCtx->lockProxyPath!=NULL ){
30711          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
30712        }else{
30713          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
30714        }
30715        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
30716        robust_ftruncate(conchFile->h, writeSize);
30717        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
30718        fsync(conchFile->h);
30719        /* If we created a new conch file (not just updated the contents of a
30720         ** valid conch file), try to match the permissions of the database
30721         */
30722        if( rc==SQLITE_OK && createConch ){
30723          struct stat buf;
30724          int err = osFstat(pFile->h, &buf);
30725          if( err==0 ){
30726            mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
30727                                        S_IROTH|S_IWOTH);
30728            /* try to match the database file R/W permissions, ignore failure */
30729#ifndef SQLITE_PROXY_DEBUG
30730            osFchmod(conchFile->h, cmode);
30731#else
30732            do{
30733              rc = osFchmod(conchFile->h, cmode);
30734            }while( rc==(-1) && errno==EINTR );
30735            if( rc!=0 ){
30736              int code = errno;
30737              fprintf(stderr, "fchmod %o FAILED with %d %s\n",
30738                      cmode, code, strerror(code));
30739            } else {
30740              fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
30741            }
30742          }else{
30743            int code = errno;
30744            fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
30745                    err, code, strerror(code));
30746#endif
30747          }
30748        }
30749      }
30750      conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30751
30752    end_takeconch:
30753      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
30754      if( rc==SQLITE_OK && pFile->openFlags ){
30755        int fd;
30756        if( pFile->h>=0 ){
30757          robust_close(pFile, pFile->h, __LINE__);
30758        }
30759        pFile->h = -1;
30760        fd = robust_open(pCtx->dbPath, pFile->openFlags,
30761                      SQLITE_DEFAULT_FILE_PERMISSIONS);
30762        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
30763        if( fd>=0 ){
30764          pFile->h = fd;
30765        }else{
30766          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
30767           during locking */
30768        }
30769      }
30770      if( rc==SQLITE_OK && !pCtx->lockProxy ){
30771        char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
30772        rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
30773        if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
30774          /* we couldn't create the proxy lock file with the old lock file path
30775           ** so try again via auto-naming
30776           */
30777          forceNewLockPath = 1;
30778          tryOldLockPath = 0;
30779          continue; /* go back to the do {} while start point, try again */
30780        }
30781      }
30782      if( rc==SQLITE_OK ){
30783        /* Need to make a copy of path if we extracted the value
30784         ** from the conch file or the path was allocated on the stack
30785         */
30786        if( tempLockPath ){
30787          pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
30788          if( !pCtx->lockProxyPath ){
30789            rc = SQLITE_NOMEM;
30790          }
30791        }
30792      }
30793      if( rc==SQLITE_OK ){
30794        pCtx->conchHeld = 1;
30795
30796        if( pCtx->lockProxy->pMethod == &afpIoMethods ){
30797          afpLockingContext *afpCtx;
30798          afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
30799          afpCtx->dbPath = pCtx->lockProxyPath;
30800        }
30801      } else {
30802        conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30803      }
30804      OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
30805               rc==SQLITE_OK?"ok":"failed"));
30806      return rc;
30807    } while (1); /* in case we need to retry the :auto: lock file -
30808                 ** we should never get here except via the 'continue' call. */
30809  }
30810}
30811
30812/*
30813** If pFile holds a lock on a conch file, then release that lock.
30814*/
30815static int proxyReleaseConch(unixFile *pFile){
30816  int rc = SQLITE_OK;         /* Subroutine return code */
30817  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
30818  unixFile *conchFile;        /* Name of the conch file */
30819
30820  pCtx = (proxyLockingContext *)pFile->lockingContext;
30821  conchFile = pCtx->conchFile;
30822  OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
30823           (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
30824           getpid()));
30825  if( pCtx->conchHeld>0 ){
30826    rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30827  }
30828  pCtx->conchHeld = 0;
30829  OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
30830           (rc==SQLITE_OK ? "ok" : "failed")));
30831  return rc;
30832}
30833
30834/*
30835** Given the name of a database file, compute the name of its conch file.
30836** Store the conch filename in memory obtained from sqlite3_malloc().
30837** Make *pConchPath point to the new name.  Return SQLITE_OK on success
30838** or SQLITE_NOMEM if unable to obtain memory.
30839**
30840** The caller is responsible for ensuring that the allocated memory
30841** space is eventually freed.
30842**
30843** *pConchPath is set to NULL if a memory allocation error occurs.
30844*/
30845static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
30846  int i;                        /* Loop counter */
30847  int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
30848  char *conchPath;              /* buffer in which to construct conch name */
30849
30850  /* Allocate space for the conch filename and initialize the name to
30851  ** the name of the original database file. */
30852  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
30853  if( conchPath==0 ){
30854    return SQLITE_NOMEM;
30855  }
30856  memcpy(conchPath, dbPath, len+1);
30857
30858  /* now insert a "." before the last / character */
30859  for( i=(len-1); i>=0; i-- ){
30860    if( conchPath[i]=='/' ){
30861      i++;
30862      break;
30863    }
30864  }
30865  conchPath[i]='.';
30866  while ( i<len ){
30867    conchPath[i+1]=dbPath[i];
30868    i++;
30869  }
30870
30871  /* append the "-conch" suffix to the file */
30872  memcpy(&conchPath[i+1], "-conch", 7);
30873  assert( (int)strlen(conchPath) == len+7 );
30874
30875  return SQLITE_OK;
30876}
30877
30878
30879/* Takes a fully configured proxy locking-style unix file and switches
30880** the local lock file path
30881*/
30882static int switchLockProxyPath(unixFile *pFile, const char *path) {
30883  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30884  char *oldPath = pCtx->lockProxyPath;
30885  int rc = SQLITE_OK;
30886
30887  if( pFile->eFileLock!=NO_LOCK ){
30888    return SQLITE_BUSY;
30889  }
30890
30891  /* nothing to do if the path is NULL, :auto: or matches the existing path */
30892  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
30893    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
30894    return SQLITE_OK;
30895  }else{
30896    unixFile *lockProxy = pCtx->lockProxy;
30897    pCtx->lockProxy=NULL;
30898    pCtx->conchHeld = 0;
30899    if( lockProxy!=NULL ){
30900      rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
30901      if( rc ) return rc;
30902      sqlite3_free(lockProxy);
30903    }
30904    sqlite3_free(oldPath);
30905    pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
30906  }
30907
30908  return rc;
30909}
30910
30911/*
30912** pFile is a file that has been opened by a prior xOpen call.  dbPath
30913** is a string buffer at least MAXPATHLEN+1 characters in size.
30914**
30915** This routine find the filename associated with pFile and writes it
30916** int dbPath.
30917*/
30918static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
30919#if defined(__APPLE__)
30920  if( pFile->pMethod == &afpIoMethods ){
30921    /* afp style keeps a reference to the db path in the filePath field
30922    ** of the struct */
30923    assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30924    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
30925  } else
30926#endif
30927  if( pFile->pMethod == &dotlockIoMethods ){
30928    /* dot lock style uses the locking context to store the dot lock
30929    ** file path */
30930    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
30931    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
30932  }else{
30933    /* all other styles use the locking context to store the db file path */
30934    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30935    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
30936  }
30937  return SQLITE_OK;
30938}
30939
30940/*
30941** Takes an already filled in unix file and alters it so all file locking
30942** will be performed on the local proxy lock file.  The following fields
30943** are preserved in the locking context so that they can be restored and
30944** the unix structure properly cleaned up at close time:
30945**  ->lockingContext
30946**  ->pMethod
30947*/
30948static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
30949  proxyLockingContext *pCtx;
30950  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
30951  char *lockPath=NULL;
30952  int rc = SQLITE_OK;
30953
30954  if( pFile->eFileLock!=NO_LOCK ){
30955    return SQLITE_BUSY;
30956  }
30957  proxyGetDbPathForUnixFile(pFile, dbPath);
30958  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
30959    lockPath=NULL;
30960  }else{
30961    lockPath=(char *)path;
30962  }
30963
30964  OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
30965           (lockPath ? lockPath : ":auto:"), getpid()));
30966
30967  pCtx = sqlite3_malloc( sizeof(*pCtx) );
30968  if( pCtx==0 ){
30969    return SQLITE_NOMEM;
30970  }
30971  memset(pCtx, 0, sizeof(*pCtx));
30972
30973  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
30974  if( rc==SQLITE_OK ){
30975    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
30976    if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
30977      /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
30978      ** (c) the file system is read-only, then enable no-locking access.
30979      ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
30980      ** that openFlags will have only one of O_RDONLY or O_RDWR.
30981      */
30982      struct statfs fsInfo;
30983      struct stat conchInfo;
30984      int goLockless = 0;
30985
30986      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
30987        int err = errno;
30988        if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
30989          goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
30990        }
30991      }
30992      if( goLockless ){
30993        pCtx->conchHeld = -1; /* read only FS/ lockless */
30994        rc = SQLITE_OK;
30995      }
30996    }
30997  }
30998  if( rc==SQLITE_OK && lockPath ){
30999    pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
31000  }
31001
31002  if( rc==SQLITE_OK ){
31003    pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
31004    if( pCtx->dbPath==NULL ){
31005      rc = SQLITE_NOMEM;
31006    }
31007  }
31008  if( rc==SQLITE_OK ){
31009    /* all memory is allocated, proxys are created and assigned,
31010    ** switch the locking context and pMethod then return.
31011    */
31012    pCtx->oldLockingContext = pFile->lockingContext;
31013    pFile->lockingContext = pCtx;
31014    pCtx->pOldMethod = pFile->pMethod;
31015    pFile->pMethod = &proxyIoMethods;
31016  }else{
31017    if( pCtx->conchFile ){
31018      pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
31019      sqlite3_free(pCtx->conchFile);
31020    }
31021    sqlite3DbFree(0, pCtx->lockProxyPath);
31022    sqlite3_free(pCtx->conchFilePath);
31023    sqlite3_free(pCtx);
31024  }
31025  OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
31026           (rc==SQLITE_OK ? "ok" : "failed")));
31027  return rc;
31028}
31029
31030
31031/*
31032** This routine handles sqlite3_file_control() calls that are specific
31033** to proxy locking.
31034*/
31035static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
31036  switch( op ){
31037    case SQLITE_GET_LOCKPROXYFILE: {
31038      unixFile *pFile = (unixFile*)id;
31039      if( pFile->pMethod == &proxyIoMethods ){
31040        proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
31041        proxyTakeConch(pFile);
31042        if( pCtx->lockProxyPath ){
31043          *(const char **)pArg = pCtx->lockProxyPath;
31044        }else{
31045          *(const char **)pArg = ":auto: (not held)";
31046        }
31047      } else {
31048        *(const char **)pArg = NULL;
31049      }
31050      return SQLITE_OK;
31051    }
31052    case SQLITE_SET_LOCKPROXYFILE: {
31053      unixFile *pFile = (unixFile*)id;
31054      int rc = SQLITE_OK;
31055      int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
31056      if( pArg==NULL || (const char *)pArg==0 ){
31057        if( isProxyStyle ){
31058          /* turn off proxy locking - not supported */
31059          rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
31060        }else{
31061          /* turn off proxy locking - already off - NOOP */
31062          rc = SQLITE_OK;
31063        }
31064      }else{
31065        const char *proxyPath = (const char *)pArg;
31066        if( isProxyStyle ){
31067          proxyLockingContext *pCtx =
31068            (proxyLockingContext*)pFile->lockingContext;
31069          if( !strcmp(pArg, ":auto:")
31070           || (pCtx->lockProxyPath &&
31071               !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
31072          ){
31073            rc = SQLITE_OK;
31074          }else{
31075            rc = switchLockProxyPath(pFile, proxyPath);
31076          }
31077        }else{
31078          /* turn on proxy file locking */
31079          rc = proxyTransformUnixFile(pFile, proxyPath);
31080        }
31081      }
31082      return rc;
31083    }
31084    default: {
31085      assert( 0 );  /* The call assures that only valid opcodes are sent */
31086    }
31087  }
31088  /*NOTREACHED*/
31089  return SQLITE_ERROR;
31090}
31091
31092/*
31093** Within this division (the proxying locking implementation) the procedures
31094** above this point are all utilities.  The lock-related methods of the
31095** proxy-locking sqlite3_io_method object follow.
31096*/
31097
31098
31099/*
31100** This routine checks if there is a RESERVED lock held on the specified
31101** file by this or any other process. If such a lock is held, set *pResOut
31102** to a non-zero value otherwise *pResOut is set to zero.  The return value
31103** is set to SQLITE_OK unless an I/O error occurs during lock checking.
31104*/
31105static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
31106  unixFile *pFile = (unixFile*)id;
31107  int rc = proxyTakeConch(pFile);
31108  if( rc==SQLITE_OK ){
31109    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31110    if( pCtx->conchHeld>0 ){
31111      unixFile *proxy = pCtx->lockProxy;
31112      return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
31113    }else{ /* conchHeld < 0 is lockless */
31114      pResOut=0;
31115    }
31116  }
31117  return rc;
31118}
31119
31120/*
31121** Lock the file with the lock specified by parameter eFileLock - one
31122** of the following:
31123**
31124**     (1) SHARED_LOCK
31125**     (2) RESERVED_LOCK
31126**     (3) PENDING_LOCK
31127**     (4) EXCLUSIVE_LOCK
31128**
31129** Sometimes when requesting one lock state, additional lock states
31130** are inserted in between.  The locking might fail on one of the later
31131** transitions leaving the lock state different from what it started but
31132** still short of its goal.  The following chart shows the allowed
31133** transitions and the inserted intermediate states:
31134**
31135**    UNLOCKED -> SHARED
31136**    SHARED -> RESERVED
31137**    SHARED -> (PENDING) -> EXCLUSIVE
31138**    RESERVED -> (PENDING) -> EXCLUSIVE
31139**    PENDING -> EXCLUSIVE
31140**
31141** This routine will only increase a lock.  Use the sqlite3OsUnlock()
31142** routine to lower a locking level.
31143*/
31144static int proxyLock(sqlite3_file *id, int eFileLock) {
31145  unixFile *pFile = (unixFile*)id;
31146  int rc = proxyTakeConch(pFile);
31147  if( rc==SQLITE_OK ){
31148    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31149    if( pCtx->conchHeld>0 ){
31150      unixFile *proxy = pCtx->lockProxy;
31151      rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
31152      pFile->eFileLock = proxy->eFileLock;
31153    }else{
31154      /* conchHeld < 0 is lockless */
31155    }
31156  }
31157  return rc;
31158}
31159
31160
31161/*
31162** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
31163** must be either NO_LOCK or SHARED_LOCK.
31164**
31165** If the locking level of the file descriptor is already at or below
31166** the requested locking level, this routine is a no-op.
31167*/
31168static int proxyUnlock(sqlite3_file *id, int eFileLock) {
31169  unixFile *pFile = (unixFile*)id;
31170  int rc = proxyTakeConch(pFile);
31171  if( rc==SQLITE_OK ){
31172    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31173    if( pCtx->conchHeld>0 ){
31174      unixFile *proxy = pCtx->lockProxy;
31175      rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
31176      pFile->eFileLock = proxy->eFileLock;
31177    }else{
31178      /* conchHeld < 0 is lockless */
31179    }
31180  }
31181  return rc;
31182}
31183
31184/*
31185** Close a file that uses proxy locks.
31186*/
31187static int proxyClose(sqlite3_file *id) {
31188  if( id ){
31189    unixFile *pFile = (unixFile*)id;
31190    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
31191    unixFile *lockProxy = pCtx->lockProxy;
31192    unixFile *conchFile = pCtx->conchFile;
31193    int rc = SQLITE_OK;
31194
31195    if( lockProxy ){
31196      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
31197      if( rc ) return rc;
31198      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
31199      if( rc ) return rc;
31200      sqlite3_free(lockProxy);
31201      pCtx->lockProxy = 0;
31202    }
31203    if( conchFile ){
31204      if( pCtx->conchHeld ){
31205        rc = proxyReleaseConch(pFile);
31206        if( rc ) return rc;
31207      }
31208      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
31209      if( rc ) return rc;
31210      sqlite3_free(conchFile);
31211    }
31212    sqlite3DbFree(0, pCtx->lockProxyPath);
31213    sqlite3_free(pCtx->conchFilePath);
31214    sqlite3DbFree(0, pCtx->dbPath);
31215    /* restore the original locking context and pMethod then close it */
31216    pFile->lockingContext = pCtx->oldLockingContext;
31217    pFile->pMethod = pCtx->pOldMethod;
31218    sqlite3_free(pCtx);
31219    return pFile->pMethod->xClose(id);
31220  }
31221  return SQLITE_OK;
31222}
31223
31224
31225
31226#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
31227/*
31228** The proxy locking style is intended for use with AFP filesystems.
31229** And since AFP is only supported on MacOSX, the proxy locking is also
31230** restricted to MacOSX.
31231**
31232**
31233******************* End of the proxy lock implementation **********************
31234******************************************************************************/
31235
31236/*
31237** Initialize the operating system interface.
31238**
31239** This routine registers all VFS implementations for unix-like operating
31240** systems.  This routine, and the sqlite3_os_end() routine that follows,
31241** should be the only routines in this file that are visible from other
31242** files.
31243**
31244** This routine is called once during SQLite initialization and by a
31245** single thread.  The memory allocation and mutex subsystems have not
31246** necessarily been initialized when this routine is called, and so they
31247** should not be used.
31248*/
31249SQLITE_API int sqlite3_os_init(void){
31250  /*
31251  ** The following macro defines an initializer for an sqlite3_vfs object.
31252  ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
31253  ** to the "finder" function.  (pAppData is a pointer to a pointer because
31254  ** silly C90 rules prohibit a void* from being cast to a function pointer
31255  ** and so we have to go through the intermediate pointer to avoid problems
31256  ** when compiling with -pedantic-errors on GCC.)
31257  **
31258  ** The FINDER parameter to this macro is the name of the pointer to the
31259  ** finder-function.  The finder-function returns a pointer to the
31260  ** sqlite_io_methods object that implements the desired locking
31261  ** behaviors.  See the division above that contains the IOMETHODS
31262  ** macro for addition information on finder-functions.
31263  **
31264  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
31265  ** object.  But the "autolockIoFinder" available on MacOSX does a little
31266  ** more than that; it looks at the filesystem type that hosts the
31267  ** database file and tries to choose an locking method appropriate for
31268  ** that filesystem time.
31269  */
31270  #define UNIXVFS(VFSNAME, FINDER) {                        \
31271    3,                    /* iVersion */                    \
31272    sizeof(unixFile),     /* szOsFile */                    \
31273    MAX_PATHNAME,         /* mxPathname */                  \
31274    0,                    /* pNext */                       \
31275    VFSNAME,              /* zName */                       \
31276    (void*)&FINDER,       /* pAppData */                    \
31277    unixOpen,             /* xOpen */                       \
31278    unixDelete,           /* xDelete */                     \
31279    unixAccess,           /* xAccess */                     \
31280    unixFullPathname,     /* xFullPathname */               \
31281    unixDlOpen,           /* xDlOpen */                     \
31282    unixDlError,          /* xDlError */                    \
31283    unixDlSym,            /* xDlSym */                      \
31284    unixDlClose,          /* xDlClose */                    \
31285    unixRandomness,       /* xRandomness */                 \
31286    unixSleep,            /* xSleep */                      \
31287    unixCurrentTime,      /* xCurrentTime */                \
31288    unixGetLastError,     /* xGetLastError */               \
31289    unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
31290    unixSetSystemCall,    /* xSetSystemCall */              \
31291    unixGetSystemCall,    /* xGetSystemCall */              \
31292    unixNextSystemCall,   /* xNextSystemCall */             \
31293  }
31294
31295  /*
31296  ** All default VFSes for unix are contained in the following array.
31297  **
31298  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
31299  ** by the SQLite core when the VFS is registered.  So the following
31300  ** array cannot be const.
31301  */
31302  static sqlite3_vfs aVfs[] = {
31303#if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
31304    UNIXVFS("unix",          autolockIoFinder ),
31305#else
31306    UNIXVFS("unix",          posixIoFinder ),
31307#endif
31308    UNIXVFS("unix-none",     nolockIoFinder ),
31309    UNIXVFS("unix-dotfile",  dotlockIoFinder ),
31310    UNIXVFS("unix-excl",     posixIoFinder ),
31311#if OS_VXWORKS
31312    UNIXVFS("unix-namedsem", semIoFinder ),
31313#endif
31314#if SQLITE_ENABLE_LOCKING_STYLE
31315    UNIXVFS("unix-posix",    posixIoFinder ),
31316#if !OS_VXWORKS
31317    UNIXVFS("unix-flock",    flockIoFinder ),
31318#endif
31319#endif
31320#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31321    UNIXVFS("unix-afp",      afpIoFinder ),
31322    UNIXVFS("unix-nfs",      nfsIoFinder ),
31323    UNIXVFS("unix-proxy",    proxyIoFinder ),
31324#endif
31325  };
31326  unsigned int i;          /* Loop counter */
31327
31328  /* Double-check that the aSyscall[] array has been constructed
31329  ** correctly.  See ticket [bb3a86e890c8e96ab] */
31330  assert( ArraySize(aSyscall)==18 );
31331
31332  /* Register all VFSes defined in the aVfs[] array */
31333  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31334    sqlite3_vfs_register(&aVfs[i], i==0);
31335  }
31336  return SQLITE_OK;
31337}
31338
31339/*
31340** Shutdown the operating system interface.
31341**
31342** Some operating systems might need to do some cleanup in this routine,
31343** to release dynamically allocated objects.  But not on unix.
31344** This routine is a no-op for unix.
31345*/
31346SQLITE_API int sqlite3_os_end(void){
31347  return SQLITE_OK;
31348}
31349
31350#endif /* SQLITE_OS_UNIX */
31351
31352/************** End of os_unix.c *********************************************/
31353/************** Begin file os_win.c ******************************************/
31354/*
31355** 2004 May 22
31356**
31357** The author disclaims copyright to this source code.  In place of
31358** a legal notice, here is a blessing:
31359**
31360**    May you do good and not evil.
31361**    May you find forgiveness for yourself and forgive others.
31362**    May you share freely, never taking more than you give.
31363**
31364******************************************************************************
31365**
31366** This file contains code that is specific to windows.
31367*/
31368#if SQLITE_OS_WIN               /* This file is used for windows only */
31369
31370
31371/*
31372** A Note About Memory Allocation:
31373**
31374** This driver uses malloc()/free() directly rather than going through
31375** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
31376** are designed for use on embedded systems where memory is scarce and
31377** malloc failures happen frequently.  Win32 does not typically run on
31378** embedded systems, and when it does the developers normally have bigger
31379** problems to worry about than running out of memory.  So there is not
31380** a compelling need to use the wrappers.
31381**
31382** But there is a good reason to not use the wrappers.  If we use the
31383** wrappers then we will get simulated malloc() failures within this
31384** driver.  And that causes all kinds of problems for our tests.  We
31385** could enhance SQLite to deal with simulated malloc failures within
31386** the OS driver, but the code to deal with those failure would not
31387** be exercised on Linux (which does not need to malloc() in the driver)
31388** and so we would have difficulty writing coverage tests for that
31389** code.  Better to leave the code out, we think.
31390**
31391** The point of this discussion is as follows:  When creating a new
31392** OS layer for an embedded system, if you use this file as an example,
31393** avoid the use of malloc()/free().  Those routines work ok on windows
31394** desktops but not so well in embedded systems.
31395*/
31396
31397#include <winbase.h>
31398
31399#ifdef __CYGWIN__
31400# include <sys/cygwin.h>
31401#endif
31402
31403/*
31404** Macros used to determine whether or not to use threads.
31405*/
31406#if defined(THREADSAFE) && THREADSAFE
31407# define SQLITE_W32_THREADS 1
31408#endif
31409
31410/*
31411** Include code that is common to all os_*.c files
31412*/
31413/************** Include os_common.h in the middle of os_win.c ****************/
31414/************** Begin file os_common.h ***************************************/
31415/*
31416** 2004 May 22
31417**
31418** The author disclaims copyright to this source code.  In place of
31419** a legal notice, here is a blessing:
31420**
31421**    May you do good and not evil.
31422**    May you find forgiveness for yourself and forgive others.
31423**    May you share freely, never taking more than you give.
31424**
31425******************************************************************************
31426**
31427** This file contains macros and a little bit of code that is common to
31428** all of the platform-specific files (os_*.c) and is #included into those
31429** files.
31430**
31431** This file should be #included by the os_*.c files only.  It is not a
31432** general purpose header file.
31433*/
31434#ifndef _OS_COMMON_H_
31435#define _OS_COMMON_H_
31436
31437/*
31438** At least two bugs have slipped in because we changed the MEMORY_DEBUG
31439** macro to SQLITE_DEBUG and some older makefiles have not yet made the
31440** switch.  The following code should catch this problem at compile-time.
31441*/
31442#ifdef MEMORY_DEBUG
31443# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
31444#endif
31445
31446#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31447# ifndef SQLITE_DEBUG_OS_TRACE
31448#   define SQLITE_DEBUG_OS_TRACE 0
31449# endif
31450  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31451# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
31452#else
31453# define OSTRACE(X)
31454#endif
31455
31456/*
31457** Macros for performance tracing.  Normally turned off.  Only works
31458** on i486 hardware.
31459*/
31460#ifdef SQLITE_PERFORMANCE_TRACE
31461
31462/*
31463** hwtime.h contains inline assembler code for implementing
31464** high-performance timing routines.
31465*/
31466/************** Include hwtime.h in the middle of os_common.h ****************/
31467/************** Begin file hwtime.h ******************************************/
31468/*
31469** 2008 May 27
31470**
31471** The author disclaims copyright to this source code.  In place of
31472** a legal notice, here is a blessing:
31473**
31474**    May you do good and not evil.
31475**    May you find forgiveness for yourself and forgive others.
31476**    May you share freely, never taking more than you give.
31477**
31478******************************************************************************
31479**
31480** This file contains inline asm code for retrieving "high-performance"
31481** counters for x86 class CPUs.
31482*/
31483#ifndef _HWTIME_H_
31484#define _HWTIME_H_
31485
31486/*
31487** The following routine only works on pentium-class (or newer) processors.
31488** It uses the RDTSC opcode to read the cycle count value out of the
31489** processor and returns that value.  This can be used for high-res
31490** profiling.
31491*/
31492#if (defined(__GNUC__) || defined(_MSC_VER)) && \
31493      (defined(i386) || defined(__i386__) || defined(_M_IX86))
31494
31495  #if defined(__GNUC__)
31496
31497  __inline__ sqlite_uint64 sqlite3Hwtime(void){
31498     unsigned int lo, hi;
31499     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
31500     return (sqlite_uint64)hi << 32 | lo;
31501  }
31502
31503  #elif defined(_MSC_VER)
31504
31505  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
31506     __asm {
31507        rdtsc
31508        ret       ; return value at EDX:EAX
31509     }
31510  }
31511
31512  #endif
31513
31514#elif (defined(__GNUC__) && defined(__x86_64__))
31515
31516  __inline__ sqlite_uint64 sqlite3Hwtime(void){
31517      unsigned long val;
31518      __asm__ __volatile__ ("rdtsc" : "=A" (val));
31519      return val;
31520  }
31521
31522#elif (defined(__GNUC__) && defined(__ppc__))
31523
31524  __inline__ sqlite_uint64 sqlite3Hwtime(void){
31525      unsigned long long retval;
31526      unsigned long junk;
31527      __asm__ __volatile__ ("\n\
31528          1:      mftbu   %1\n\
31529                  mftb    %L0\n\
31530                  mftbu   %0\n\
31531                  cmpw    %0,%1\n\
31532                  bne     1b"
31533                  : "=r" (retval), "=r" (junk));
31534      return retval;
31535  }
31536
31537#else
31538
31539  #error Need implementation of sqlite3Hwtime() for your platform.
31540
31541  /*
31542  ** To compile without implementing sqlite3Hwtime() for your platform,
31543  ** you can remove the above #error and use the following
31544  ** stub function.  You will lose timing support for many
31545  ** of the debugging and testing utilities, but it should at
31546  ** least compile and run.
31547  */
31548SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
31549
31550#endif
31551
31552#endif /* !defined(_HWTIME_H_) */
31553
31554/************** End of hwtime.h **********************************************/
31555/************** Continuing where we left off in os_common.h ******************/
31556
31557static sqlite_uint64 g_start;
31558static sqlite_uint64 g_elapsed;
31559#define TIMER_START       g_start=sqlite3Hwtime()
31560#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
31561#define TIMER_ELAPSED     g_elapsed
31562#else
31563#define TIMER_START
31564#define TIMER_END
31565#define TIMER_ELAPSED     ((sqlite_uint64)0)
31566#endif
31567
31568/*
31569** If we compile with the SQLITE_TEST macro set, then the following block
31570** of code will give us the ability to simulate a disk I/O error.  This
31571** is used for testing the I/O recovery logic.
31572*/
31573#ifdef SQLITE_TEST
31574SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
31575SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
31576SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
31577SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
31578SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
31579SQLITE_API int sqlite3_diskfull_pending = 0;
31580SQLITE_API int sqlite3_diskfull = 0;
31581#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
31582#define SimulateIOError(CODE)  \
31583  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
31584       || sqlite3_io_error_pending-- == 1 )  \
31585              { local_ioerr(); CODE; }
31586static void local_ioerr(){
31587  IOTRACE(("IOERR\n"));
31588  sqlite3_io_error_hit++;
31589  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
31590}
31591#define SimulateDiskfullError(CODE) \
31592   if( sqlite3_diskfull_pending ){ \
31593     if( sqlite3_diskfull_pending == 1 ){ \
31594       local_ioerr(); \
31595       sqlite3_diskfull = 1; \
31596       sqlite3_io_error_hit = 1; \
31597       CODE; \
31598     }else{ \
31599       sqlite3_diskfull_pending--; \
31600     } \
31601   }
31602#else
31603#define SimulateIOErrorBenign(X)
31604#define SimulateIOError(A)
31605#define SimulateDiskfullError(A)
31606#endif
31607
31608/*
31609** When testing, keep a count of the number of open files.
31610*/
31611#ifdef SQLITE_TEST
31612SQLITE_API int sqlite3_open_file_count = 0;
31613#define OpenCounter(X)  sqlite3_open_file_count+=(X)
31614#else
31615#define OpenCounter(X)
31616#endif
31617
31618#endif /* !defined(_OS_COMMON_H_) */
31619
31620/************** End of os_common.h *******************************************/
31621/************** Continuing where we left off in os_win.c *********************/
31622
31623/*
31624** Some microsoft compilers lack this definition.
31625*/
31626#ifndef INVALID_FILE_ATTRIBUTES
31627# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31628#endif
31629
31630/*
31631** Determine if we are dealing with WindowsCE - which has a much
31632** reduced API.
31633*/
31634#if SQLITE_OS_WINCE
31635# define AreFileApisANSI() 1
31636# define FormatMessageW(a,b,c,d,e,f,g) 0
31637#endif
31638
31639/* Forward references */
31640typedef struct winShm winShm;           /* A connection to shared-memory */
31641typedef struct winShmNode winShmNode;   /* A region of shared-memory */
31642
31643/*
31644** WinCE lacks native support for file locking so we have to fake it
31645** with some code of our own.
31646*/
31647#if SQLITE_OS_WINCE
31648typedef struct winceLock {
31649  int nReaders;       /* Number of reader locks obtained */
31650  BOOL bPending;      /* Indicates a pending lock has been obtained */
31651  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
31652  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
31653} winceLock;
31654#endif
31655
31656/*
31657** The winFile structure is a subclass of sqlite3_file* specific to the win32
31658** portability layer.
31659*/
31660typedef struct winFile winFile;
31661struct winFile {
31662  const sqlite3_io_methods *pMethod; /*** Must be first ***/
31663  sqlite3_vfs *pVfs;      /* The VFS used to open this file */
31664  HANDLE h;               /* Handle for accessing the file */
31665  u8 locktype;            /* Type of lock currently held on this file */
31666  short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
31667  u8 bPersistWal;         /* True to persist WAL files */
31668  DWORD lastErrno;        /* The Windows errno from the last I/O error */
31669  DWORD sectorSize;       /* Sector size of the device file is on */
31670  winShm *pShm;           /* Instance of shared memory on this file */
31671  const char *zPath;      /* Full pathname of this file */
31672  int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
31673#if SQLITE_OS_WINCE
31674  WCHAR *zDeleteOnClose;  /* Name of file to delete when closing */
31675  HANDLE hMutex;          /* Mutex used to control access to shared lock */
31676  HANDLE hShared;         /* Shared memory segment used for locking */
31677  winceLock local;        /* Locks obtained by this instance of winFile */
31678  winceLock *shared;      /* Global shared lock memory for the file  */
31679#endif
31680};
31681
31682/*
31683 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31684 * various Win32 API heap functions instead of our own.
31685 */
31686#ifdef SQLITE_WIN32_MALLOC
31687/*
31688 * The initial size of the Win32-specific heap.  This value may be zero.
31689 */
31690#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
31691#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
31692                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
31693#endif
31694
31695/*
31696 * The maximum size of the Win32-specific heap.  This value may be zero.
31697 */
31698#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
31699#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
31700#endif
31701
31702/*
31703 * The extra flags to use in calls to the Win32 heap APIs.  This value may be
31704 * zero for the default behavior.
31705 */
31706#ifndef SQLITE_WIN32_HEAP_FLAGS
31707#  define SQLITE_WIN32_HEAP_FLAGS     (0)
31708#endif
31709
31710/*
31711** The winMemData structure stores information required by the Win32-specific
31712** sqlite3_mem_methods implementation.
31713*/
31714typedef struct winMemData winMemData;
31715struct winMemData {
31716#ifndef NDEBUG
31717  u32 magic;    /* Magic number to detect structure corruption. */
31718#endif
31719  HANDLE hHeap; /* The handle to our heap. */
31720  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
31721};
31722
31723#ifndef NDEBUG
31724#define WINMEM_MAGIC     0x42b2830b
31725#endif
31726
31727static struct winMemData win_mem_data = {
31728#ifndef NDEBUG
31729  WINMEM_MAGIC,
31730#endif
31731  NULL, FALSE
31732};
31733
31734#ifndef NDEBUG
31735#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
31736#else
31737#define winMemAssertMagic()
31738#endif
31739
31740#define winMemGetHeap() win_mem_data.hHeap
31741
31742static void *winMemMalloc(int nBytes);
31743static void winMemFree(void *pPrior);
31744static void *winMemRealloc(void *pPrior, int nBytes);
31745static int winMemSize(void *p);
31746static int winMemRoundup(int n);
31747static int winMemInit(void *pAppData);
31748static void winMemShutdown(void *pAppData);
31749
31750SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31751#endif /* SQLITE_WIN32_MALLOC */
31752
31753/*
31754** Forward prototypes.
31755*/
31756static int getSectorSize(
31757    sqlite3_vfs *pVfs,
31758    const char *zRelative     /* UTF-8 file name */
31759);
31760
31761/*
31762** The following variable is (normally) set once and never changes
31763** thereafter.  It records whether the operating system is Win95
31764** or WinNT.
31765**
31766** 0:   Operating system unknown.
31767** 1:   Operating system is Win95.
31768** 2:   Operating system is WinNT.
31769**
31770** In order to facilitate testing on a WinNT system, the test fixture
31771** can manually set this value to 1 to emulate Win98 behavior.
31772*/
31773#ifdef SQLITE_TEST
31774SQLITE_API int sqlite3_os_type = 0;
31775#else
31776static int sqlite3_os_type = 0;
31777#endif
31778
31779/*
31780** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
31781** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
31782**
31783** Here is an interesting observation:  Win95, Win98, and WinME lack
31784** the LockFileEx() API.  But we can still statically link against that
31785** API as long as we don't call it when running Win95/98/ME.  A call to
31786** this routine is used to determine if the host is Win95/98/ME or
31787** WinNT/2K/XP so that we will know whether or not we can safely call
31788** the LockFileEx() API.
31789*/
31790#if SQLITE_OS_WINCE
31791# define isNT()  (1)
31792#else
31793  static int isNT(void){
31794    if( sqlite3_os_type==0 ){
31795      OSVERSIONINFO sInfo;
31796      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31797      GetVersionEx(&sInfo);
31798      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
31799    }
31800    return sqlite3_os_type==2;
31801  }
31802#endif /* SQLITE_OS_WINCE */
31803
31804#ifdef SQLITE_WIN32_MALLOC
31805/*
31806** Allocate nBytes of memory.
31807*/
31808static void *winMemMalloc(int nBytes){
31809  HANDLE hHeap;
31810  void *p;
31811
31812  winMemAssertMagic();
31813  hHeap = winMemGetHeap();
31814  assert( hHeap!=0 );
31815  assert( hHeap!=INVALID_HANDLE_VALUE );
31816#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31817  assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31818#endif
31819  assert( nBytes>=0 );
31820  p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31821  if( !p ){
31822    sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
31823        nBytes, GetLastError(), (void*)hHeap);
31824  }
31825  return p;
31826}
31827
31828/*
31829** Free memory.
31830*/
31831static void winMemFree(void *pPrior){
31832  HANDLE hHeap;
31833
31834  winMemAssertMagic();
31835  hHeap = winMemGetHeap();
31836  assert( hHeap!=0 );
31837  assert( hHeap!=INVALID_HANDLE_VALUE );
31838#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31839  assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31840#endif
31841  if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
31842  if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
31843    sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
31844        pPrior, GetLastError(), (void*)hHeap);
31845  }
31846}
31847
31848/*
31849** Change the size of an existing memory allocation
31850*/
31851static void *winMemRealloc(void *pPrior, int nBytes){
31852  HANDLE hHeap;
31853  void *p;
31854
31855  winMemAssertMagic();
31856  hHeap = winMemGetHeap();
31857  assert( hHeap!=0 );
31858  assert( hHeap!=INVALID_HANDLE_VALUE );
31859#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31860  assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31861#endif
31862  assert( nBytes>=0 );
31863  if( !pPrior ){
31864    p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31865  }else{
31866    p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
31867  }
31868  if( !p ){
31869    sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
31870        pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
31871        (void*)hHeap);
31872  }
31873  return p;
31874}
31875
31876/*
31877** Return the size of an outstanding allocation, in bytes.
31878*/
31879static int winMemSize(void *p){
31880  HANDLE hHeap;
31881  SIZE_T n;
31882
31883  winMemAssertMagic();
31884  hHeap = winMemGetHeap();
31885  assert( hHeap!=0 );
31886  assert( hHeap!=INVALID_HANDLE_VALUE );
31887#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31888  assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31889#endif
31890  if( !p ) return 0;
31891  n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
31892  if( n==(SIZE_T)-1 ){
31893    sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
31894        p, GetLastError(), (void*)hHeap);
31895    return 0;
31896  }
31897  return (int)n;
31898}
31899
31900/*
31901** Round up a request size to the next valid allocation size.
31902*/
31903static int winMemRoundup(int n){
31904  return n;
31905}
31906
31907/*
31908** Initialize this module.
31909*/
31910static int winMemInit(void *pAppData){
31911  winMemData *pWinMemData = (winMemData *)pAppData;
31912
31913  if( !pWinMemData ) return SQLITE_ERROR;
31914  assert( pWinMemData->magic==WINMEM_MAGIC );
31915  if( !pWinMemData->hHeap ){
31916    pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS,
31917                                    SQLITE_WIN32_HEAP_INIT_SIZE,
31918                                    SQLITE_WIN32_HEAP_MAX_SIZE);
31919    if( !pWinMemData->hHeap ){
31920      sqlite3_log(SQLITE_NOMEM,
31921          "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
31922          GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE,
31923          SQLITE_WIN32_HEAP_MAX_SIZE);
31924      return SQLITE_NOMEM;
31925    }
31926    pWinMemData->bOwned = TRUE;
31927  }
31928  assert( pWinMemData->hHeap!=0 );
31929  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31930#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31931  assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31932#endif
31933  return SQLITE_OK;
31934}
31935
31936/*
31937** Deinitialize this module.
31938*/
31939static void winMemShutdown(void *pAppData){
31940  winMemData *pWinMemData = (winMemData *)pAppData;
31941
31942  if( !pWinMemData ) return;
31943  if( pWinMemData->hHeap ){
31944    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31945#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31946    assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31947#endif
31948    if( pWinMemData->bOwned ){
31949      if( !HeapDestroy(pWinMemData->hHeap) ){
31950        sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
31951            GetLastError(), (void*)pWinMemData->hHeap);
31952      }
31953      pWinMemData->bOwned = FALSE;
31954    }
31955    pWinMemData->hHeap = NULL;
31956  }
31957}
31958
31959/*
31960** Populate the low-level memory allocation function pointers in
31961** sqlite3GlobalConfig.m with pointers to the routines in this file. The
31962** arguments specify the block of memory to manage.
31963**
31964** This routine is only called by sqlite3_config(), and therefore
31965** is not required to be threadsafe (it is not).
31966*/
31967SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
31968  static const sqlite3_mem_methods winMemMethods = {
31969    winMemMalloc,
31970    winMemFree,
31971    winMemRealloc,
31972    winMemSize,
31973    winMemRoundup,
31974    winMemInit,
31975    winMemShutdown,
31976    &win_mem_data
31977  };
31978  return &winMemMethods;
31979}
31980
31981SQLITE_PRIVATE void sqlite3MemSetDefault(void){
31982  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
31983}
31984#endif /* SQLITE_WIN32_MALLOC */
31985
31986/*
31987** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31988**
31989** Space to hold the returned string is obtained from malloc.
31990*/
31991static WCHAR *utf8ToUnicode(const char *zFilename){
31992  int nChar;
31993  WCHAR *zWideFilename;
31994
31995  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
31996  zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
31997  if( zWideFilename==0 ){
31998    return 0;
31999  }
32000  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
32001  if( nChar==0 ){
32002    free(zWideFilename);
32003    zWideFilename = 0;
32004  }
32005  return zWideFilename;
32006}
32007
32008/*
32009** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
32010** obtained from malloc().
32011*/
32012static char *unicodeToUtf8(const WCHAR *zWideFilename){
32013  int nByte;
32014  char *zFilename;
32015
32016  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32017  zFilename = malloc( nByte );
32018  if( zFilename==0 ){
32019    return 0;
32020  }
32021  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32022                              0, 0);
32023  if( nByte == 0 ){
32024    free(zFilename);
32025    zFilename = 0;
32026  }
32027  return zFilename;
32028}
32029
32030/*
32031** Convert an ansi string to microsoft unicode, based on the
32032** current codepage settings for file apis.
32033**
32034** Space to hold the returned string is obtained
32035** from malloc.
32036*/
32037static WCHAR *mbcsToUnicode(const char *zFilename){
32038  int nByte;
32039  WCHAR *zMbcsFilename;
32040  int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32041
32042  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
32043  zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
32044  if( zMbcsFilename==0 ){
32045    return 0;
32046  }
32047  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
32048  if( nByte==0 ){
32049    free(zMbcsFilename);
32050    zMbcsFilename = 0;
32051  }
32052  return zMbcsFilename;
32053}
32054
32055/*
32056** Convert microsoft unicode to multibyte character string, based on the
32057** user's Ansi codepage.
32058**
32059** Space to hold the returned string is obtained from
32060** malloc().
32061*/
32062static char *unicodeToMbcs(const WCHAR *zWideFilename){
32063  int nByte;
32064  char *zFilename;
32065  int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32066
32067  nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32068  zFilename = malloc( nByte );
32069  if( zFilename==0 ){
32070    return 0;
32071  }
32072  nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
32073                              0, 0);
32074  if( nByte == 0 ){
32075    free(zFilename);
32076    zFilename = 0;
32077  }
32078  return zFilename;
32079}
32080
32081/*
32082** Convert multibyte character string to UTF-8.  Space to hold the
32083** returned string is obtained from malloc().
32084*/
32085SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32086  char *zFilenameUtf8;
32087  WCHAR *zTmpWide;
32088
32089  zTmpWide = mbcsToUnicode(zFilename);
32090  if( zTmpWide==0 ){
32091    return 0;
32092  }
32093  zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32094  free(zTmpWide);
32095  return zFilenameUtf8;
32096}
32097
32098/*
32099** Convert UTF-8 to multibyte character string.  Space to hold the
32100** returned string is obtained from malloc().
32101*/
32102SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32103  char *zFilenameMbcs;
32104  WCHAR *zTmpWide;
32105
32106  zTmpWide = utf8ToUnicode(zFilename);
32107  if( zTmpWide==0 ){
32108    return 0;
32109  }
32110  zFilenameMbcs = unicodeToMbcs(zTmpWide);
32111  free(zTmpWide);
32112  return zFilenameMbcs;
32113}
32114
32115
32116/*
32117** The return value of getLastErrorMsg
32118** is zero if the error message fits in the buffer, or non-zero
32119** otherwise (if the message was truncated).
32120*/
32121static int getLastErrorMsg(int nBuf, char *zBuf){
32122  /* FormatMessage returns 0 on failure.  Otherwise it
32123  ** returns the number of TCHARs written to the output
32124  ** buffer, excluding the terminating null char.
32125  */
32126  DWORD error = GetLastError();
32127  DWORD dwLen = 0;
32128  char *zOut = 0;
32129
32130  if( isNT() ){
32131    WCHAR *zTempWide = NULL;
32132    dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32133                           NULL,
32134                           error,
32135                           0,
32136                           (LPWSTR) &zTempWide,
32137                           0,
32138                           0);
32139    if( dwLen > 0 ){
32140      /* allocate a buffer and convert to UTF8 */
32141      zOut = unicodeToUtf8(zTempWide);
32142      /* free the system buffer allocated by FormatMessage */
32143      LocalFree(zTempWide);
32144    }
32145/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32146** Since the ASCII version of these Windows API do not exist for WINCE,
32147** it's important to not reference them for WINCE builds.
32148*/
32149#if SQLITE_OS_WINCE==0
32150  }else{
32151    char *zTemp = NULL;
32152    dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32153                           NULL,
32154                           error,
32155                           0,
32156                           (LPSTR) &zTemp,
32157                           0,
32158                           0);
32159    if( dwLen > 0 ){
32160      /* allocate a buffer and convert to UTF8 */
32161      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
32162      /* free the system buffer allocated by FormatMessage */
32163      LocalFree(zTemp);
32164    }
32165#endif
32166  }
32167  if( 0 == dwLen ){
32168    sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
32169  }else{
32170    /* copy a maximum of nBuf chars to output buffer */
32171    sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
32172    /* free the UTF8 buffer */
32173    free(zOut);
32174  }
32175  return 0;
32176}
32177
32178/*
32179**
32180** This function - winLogErrorAtLine() - is only ever called via the macro
32181** winLogError().
32182**
32183** This routine is invoked after an error occurs in an OS function.
32184** It logs a message using sqlite3_log() containing the current value of
32185** error code and, if possible, the human-readable equivalent from
32186** FormatMessage.
32187**
32188** The first argument passed to the macro should be the error code that
32189** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
32190** The two subsequent arguments should be the name of the OS function that
32191** failed and the the associated file-system path, if any.
32192*/
32193#define winLogError(a,b,c)     winLogErrorAtLine(a,b,c,__LINE__)
32194static int winLogErrorAtLine(
32195  int errcode,                    /* SQLite error code */
32196  const char *zFunc,              /* Name of OS function that failed */
32197  const char *zPath,              /* File path associated with error */
32198  int iLine                       /* Source line number where error occurred */
32199){
32200  char zMsg[500];                 /* Human readable error text */
32201  int i;                          /* Loop counter */
32202  DWORD iErrno = GetLastError();  /* Error code */
32203
32204  zMsg[0] = 0;
32205  getLastErrorMsg(sizeof(zMsg), zMsg);
32206  assert( errcode!=SQLITE_OK );
32207  if( zPath==0 ) zPath = "";
32208  for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32209  zMsg[i] = 0;
32210  sqlite3_log(errcode,
32211      "os_win.c:%d: (%d) %s(%s) - %s",
32212      iLine, iErrno, zFunc, zPath, zMsg
32213  );
32214
32215  return errcode;
32216}
32217
32218/*
32219** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
32220** will be retried following a locking error - probably caused by
32221** antivirus software.  Also the initial delay before the first retry.
32222** The delay increases linearly with each retry.
32223*/
32224#ifndef SQLITE_WIN32_IOERR_RETRY
32225# define SQLITE_WIN32_IOERR_RETRY 10
32226#endif
32227#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
32228# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
32229#endif
32230static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32231static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32232
32233/*
32234** If a ReadFile() or WriteFile() error occurs, invoke this routine
32235** to see if it should be retried.  Return TRUE to retry.  Return FALSE
32236** to give up with an error.
32237*/
32238static int retryIoerr(int *pnRetry){
32239  DWORD e;
32240  if( *pnRetry>=win32IoerrRetry ){
32241    return 0;
32242  }
32243  e = GetLastError();
32244  if( e==ERROR_ACCESS_DENIED ||
32245      e==ERROR_LOCK_VIOLATION ||
32246      e==ERROR_SHARING_VIOLATION ){
32247    Sleep(win32IoerrRetryDelay*(1+*pnRetry));
32248    ++*pnRetry;
32249    return 1;
32250  }
32251  return 0;
32252}
32253
32254/*
32255** Log a I/O error retry episode.
32256*/
32257static void logIoerr(int nRetry){
32258  if( nRetry ){
32259    sqlite3_log(SQLITE_IOERR,
32260      "delayed %dms for lock/sharing conflict",
32261      win32IoerrRetryDelay*nRetry*(nRetry+1)/2
32262    );
32263  }
32264}
32265
32266#if SQLITE_OS_WINCE
32267/*************************************************************************
32268** This section contains code for WinCE only.
32269*/
32270/*
32271** WindowsCE does not have a localtime() function.  So create a
32272** substitute.
32273*/
32274/* #include <time.h> */
32275struct tm *__cdecl localtime(const time_t *t)
32276{
32277  static struct tm y;
32278  FILETIME uTm, lTm;
32279  SYSTEMTIME pTm;
32280  sqlite3_int64 t64;
32281  t64 = *t;
32282  t64 = (t64 + 11644473600)*10000000;
32283  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
32284  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
32285  FileTimeToLocalFileTime(&uTm,&lTm);
32286  FileTimeToSystemTime(&lTm,&pTm);
32287  y.tm_year = pTm.wYear - 1900;
32288  y.tm_mon = pTm.wMonth - 1;
32289  y.tm_wday = pTm.wDayOfWeek;
32290  y.tm_mday = pTm.wDay;
32291  y.tm_hour = pTm.wHour;
32292  y.tm_min = pTm.wMinute;
32293  y.tm_sec = pTm.wSecond;
32294  return &y;
32295}
32296
32297/* This will never be called, but defined to make the code compile */
32298#define GetTempPathA(a,b)
32299
32300#define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
32301#define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
32302#define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
32303
32304#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
32305
32306/*
32307** Acquire a lock on the handle h
32308*/
32309static void winceMutexAcquire(HANDLE h){
32310   DWORD dwErr;
32311   do {
32312     dwErr = WaitForSingleObject(h, INFINITE);
32313   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
32314}
32315/*
32316** Release a lock acquired by winceMutexAcquire()
32317*/
32318#define winceMutexRelease(h) ReleaseMutex(h)
32319
32320/*
32321** Create the mutex and shared memory used for locking in the file
32322** descriptor pFile
32323*/
32324static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
32325  WCHAR *zTok;
32326  WCHAR *zName = utf8ToUnicode(zFilename);
32327  BOOL bInit = TRUE;
32328
32329  /* Initialize the local lockdata */
32330  ZeroMemory(&pFile->local, sizeof(pFile->local));
32331
32332  /* Replace the backslashes from the filename and lowercase it
32333  ** to derive a mutex name. */
32334  zTok = CharLowerW(zName);
32335  for (;*zTok;zTok++){
32336    if (*zTok == '\\') *zTok = '_';
32337  }
32338
32339  /* Create/open the named mutex */
32340  pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
32341  if (!pFile->hMutex){
32342    pFile->lastErrno = GetLastError();
32343    winLogError(SQLITE_ERROR, "winceCreateLock1", zFilename);
32344    free(zName);
32345    return FALSE;
32346  }
32347
32348  /* Acquire the mutex before continuing */
32349  winceMutexAcquire(pFile->hMutex);
32350
32351  /* Since the names of named mutexes, semaphores, file mappings etc are
32352  ** case-sensitive, take advantage of that by uppercasing the mutex name
32353  ** and using that as the shared filemapping name.
32354  */
32355  CharUpperW(zName);
32356  pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
32357                                       PAGE_READWRITE, 0, sizeof(winceLock),
32358                                       zName);
32359
32360  /* Set a flag that indicates we're the first to create the memory so it
32361  ** must be zero-initialized */
32362  if (GetLastError() == ERROR_ALREADY_EXISTS){
32363    bInit = FALSE;
32364  }
32365
32366  free(zName);
32367
32368  /* If we succeeded in making the shared memory handle, map it. */
32369  if (pFile->hShared){
32370    pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
32371             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
32372    /* If mapping failed, close the shared memory handle and erase it */
32373    if (!pFile->shared){
32374      pFile->lastErrno = GetLastError();
32375      winLogError(SQLITE_ERROR, "winceCreateLock2", zFilename);
32376      CloseHandle(pFile->hShared);
32377      pFile->hShared = NULL;
32378    }
32379  }
32380
32381  /* If shared memory could not be created, then close the mutex and fail */
32382  if (pFile->hShared == NULL){
32383    winceMutexRelease(pFile->hMutex);
32384    CloseHandle(pFile->hMutex);
32385    pFile->hMutex = NULL;
32386    return FALSE;
32387  }
32388
32389  /* Initialize the shared memory if we're supposed to */
32390  if (bInit) {
32391    ZeroMemory(pFile->shared, sizeof(winceLock));
32392  }
32393
32394  winceMutexRelease(pFile->hMutex);
32395  return TRUE;
32396}
32397
32398/*
32399** Destroy the part of winFile that deals with wince locks
32400*/
32401static void winceDestroyLock(winFile *pFile){
32402  if (pFile->hMutex){
32403    /* Acquire the mutex */
32404    winceMutexAcquire(pFile->hMutex);
32405
32406    /* The following blocks should probably assert in debug mode, but they
32407       are to cleanup in case any locks remained open */
32408    if (pFile->local.nReaders){
32409      pFile->shared->nReaders --;
32410    }
32411    if (pFile->local.bReserved){
32412      pFile->shared->bReserved = FALSE;
32413    }
32414    if (pFile->local.bPending){
32415      pFile->shared->bPending = FALSE;
32416    }
32417    if (pFile->local.bExclusive){
32418      pFile->shared->bExclusive = FALSE;
32419    }
32420
32421    /* De-reference and close our copy of the shared memory handle */
32422    UnmapViewOfFile(pFile->shared);
32423    CloseHandle(pFile->hShared);
32424
32425    /* Done with the mutex */
32426    winceMutexRelease(pFile->hMutex);
32427    CloseHandle(pFile->hMutex);
32428    pFile->hMutex = NULL;
32429  }
32430}
32431
32432/*
32433** An implementation of the LockFile() API of windows for wince
32434*/
32435static BOOL winceLockFile(
32436  HANDLE *phFile,
32437  DWORD dwFileOffsetLow,
32438  DWORD dwFileOffsetHigh,
32439  DWORD nNumberOfBytesToLockLow,
32440  DWORD nNumberOfBytesToLockHigh
32441){
32442  winFile *pFile = HANDLE_TO_WINFILE(phFile);
32443  BOOL bReturn = FALSE;
32444
32445  UNUSED_PARAMETER(dwFileOffsetHigh);
32446  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
32447
32448  if (!pFile->hMutex) return TRUE;
32449  winceMutexAcquire(pFile->hMutex);
32450
32451  /* Wanting an exclusive lock? */
32452  if (dwFileOffsetLow == (DWORD)SHARED_FIRST
32453       && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
32454    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
32455       pFile->shared->bExclusive = TRUE;
32456       pFile->local.bExclusive = TRUE;
32457       bReturn = TRUE;
32458    }
32459  }
32460
32461  /* Want a read-only lock? */
32462  else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
32463           nNumberOfBytesToLockLow == 1){
32464    if (pFile->shared->bExclusive == 0){
32465      pFile->local.nReaders ++;
32466      if (pFile->local.nReaders == 1){
32467        pFile->shared->nReaders ++;
32468      }
32469      bReturn = TRUE;
32470    }
32471  }
32472
32473  /* Want a pending lock? */
32474  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
32475    /* If no pending lock has been acquired, then acquire it */
32476    if (pFile->shared->bPending == 0) {
32477      pFile->shared->bPending = TRUE;
32478      pFile->local.bPending = TRUE;
32479      bReturn = TRUE;
32480    }
32481  }
32482
32483  /* Want a reserved lock? */
32484  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
32485    if (pFile->shared->bReserved == 0) {
32486      pFile->shared->bReserved = TRUE;
32487      pFile->local.bReserved = TRUE;
32488      bReturn = TRUE;
32489    }
32490  }
32491
32492  winceMutexRelease(pFile->hMutex);
32493  return bReturn;
32494}
32495
32496/*
32497** An implementation of the UnlockFile API of windows for wince
32498*/
32499static BOOL winceUnlockFile(
32500  HANDLE *phFile,
32501  DWORD dwFileOffsetLow,
32502  DWORD dwFileOffsetHigh,
32503  DWORD nNumberOfBytesToUnlockLow,
32504  DWORD nNumberOfBytesToUnlockHigh
32505){
32506  winFile *pFile = HANDLE_TO_WINFILE(phFile);
32507  BOOL bReturn = FALSE;
32508
32509  UNUSED_PARAMETER(dwFileOffsetHigh);
32510  UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
32511
32512  if (!pFile->hMutex) return TRUE;
32513  winceMutexAcquire(pFile->hMutex);
32514
32515  /* Releasing a reader lock or an exclusive lock */
32516  if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
32517    /* Did we have an exclusive lock? */
32518    if (pFile->local.bExclusive){
32519      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
32520      pFile->local.bExclusive = FALSE;
32521      pFile->shared->bExclusive = FALSE;
32522      bReturn = TRUE;
32523    }
32524
32525    /* Did we just have a reader lock? */
32526    else if (pFile->local.nReaders){
32527      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
32528      pFile->local.nReaders --;
32529      if (pFile->local.nReaders == 0)
32530      {
32531        pFile->shared->nReaders --;
32532      }
32533      bReturn = TRUE;
32534    }
32535  }
32536
32537  /* Releasing a pending lock */
32538  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
32539    if (pFile->local.bPending){
32540      pFile->local.bPending = FALSE;
32541      pFile->shared->bPending = FALSE;
32542      bReturn = TRUE;
32543    }
32544  }
32545  /* Releasing a reserved lock */
32546  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
32547    if (pFile->local.bReserved) {
32548      pFile->local.bReserved = FALSE;
32549      pFile->shared->bReserved = FALSE;
32550      bReturn = TRUE;
32551    }
32552  }
32553
32554  winceMutexRelease(pFile->hMutex);
32555  return bReturn;
32556}
32557
32558/*
32559** An implementation of the LockFileEx() API of windows for wince
32560*/
32561static BOOL winceLockFileEx(
32562  HANDLE *phFile,
32563  DWORD dwFlags,
32564  DWORD dwReserved,
32565  DWORD nNumberOfBytesToLockLow,
32566  DWORD nNumberOfBytesToLockHigh,
32567  LPOVERLAPPED lpOverlapped
32568){
32569  UNUSED_PARAMETER(dwReserved);
32570  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
32571
32572  /* If the caller wants a shared read lock, forward this call
32573  ** to winceLockFile */
32574  if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
32575      dwFlags == 1 &&
32576      nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
32577    return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
32578  }
32579  return FALSE;
32580}
32581/*
32582** End of the special code for wince
32583*****************************************************************************/
32584#endif /* SQLITE_OS_WINCE */
32585
32586/*****************************************************************************
32587** The next group of routines implement the I/O methods specified
32588** by the sqlite3_io_methods object.
32589******************************************************************************/
32590
32591/*
32592** Some microsoft compilers lack this definition.
32593*/
32594#ifndef INVALID_SET_FILE_POINTER
32595# define INVALID_SET_FILE_POINTER ((DWORD)-1)
32596#endif
32597
32598/*
32599** Move the current position of the file handle passed as the first
32600** argument to offset iOffset within the file. If successful, return 0.
32601** Otherwise, set pFile->lastErrno and return non-zero.
32602*/
32603static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32604  LONG upperBits;                 /* Most sig. 32 bits of new offset */
32605  LONG lowerBits;                 /* Least sig. 32 bits of new offset */
32606  DWORD dwRet;                    /* Value returned by SetFilePointer() */
32607
32608  upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
32609  lowerBits = (LONG)(iOffset & 0xffffffff);
32610
32611  /* API oddity: If successful, SetFilePointer() returns a dword
32612  ** containing the lower 32-bits of the new file-offset. Or, if it fails,
32613  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
32614  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
32615  ** whether an error has actually occured, it is also necessary to call
32616  ** GetLastError().
32617  */
32618  dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
32619  if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
32620    pFile->lastErrno = GetLastError();
32621    winLogError(SQLITE_IOERR_SEEK, "seekWinFile", pFile->zPath);
32622    return 1;
32623  }
32624
32625  return 0;
32626}
32627
32628/*
32629** Close a file.
32630**
32631** It is reported that an attempt to close a handle might sometimes
32632** fail.  This is a very unreasonable result, but windows is notorious
32633** for being unreasonable so I do not doubt that it might happen.  If
32634** the close fails, we pause for 100 milliseconds and try again.  As
32635** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
32636** giving up and returning an error.
32637*/
32638#define MX_CLOSE_ATTEMPT 3
32639static int winClose(sqlite3_file *id){
32640  int rc, cnt = 0;
32641  winFile *pFile = (winFile*)id;
32642
32643  assert( id!=0 );
32644  assert( pFile->pShm==0 );
32645  OSTRACE(("CLOSE %d\n", pFile->h));
32646  do{
32647    rc = CloseHandle(pFile->h);
32648    /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32649  }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
32650#if SQLITE_OS_WINCE
32651#define WINCE_DELETION_ATTEMPTS 3
32652  winceDestroyLock(pFile);
32653  if( pFile->zDeleteOnClose ){
32654    int cnt = 0;
32655    while(
32656           DeleteFileW(pFile->zDeleteOnClose)==0
32657        && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
32658        && cnt++ < WINCE_DELETION_ATTEMPTS
32659    ){
32660       Sleep(100);  /* Wait a little before trying again */
32661    }
32662    free(pFile->zDeleteOnClose);
32663  }
32664#endif
32665  OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
32666  OpenCounter(-1);
32667  return rc ? SQLITE_OK
32668            : winLogError(SQLITE_IOERR_CLOSE, "winClose", pFile->zPath);
32669}
32670
32671/*
32672** Read data from a file into a buffer.  Return SQLITE_OK if all
32673** bytes were read successfully and SQLITE_IOERR if anything goes
32674** wrong.
32675*/
32676static int winRead(
32677  sqlite3_file *id,          /* File to read from */
32678  void *pBuf,                /* Write content into this buffer */
32679  int amt,                   /* Number of bytes to read */
32680  sqlite3_int64 offset       /* Begin reading at this offset */
32681){
32682  winFile *pFile = (winFile*)id;  /* file handle */
32683  DWORD nRead;                    /* Number of bytes actually read from file */
32684  int nRetry = 0;                 /* Number of retrys */
32685
32686  assert( id!=0 );
32687  SimulateIOError(return SQLITE_IOERR_READ);
32688  OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
32689
32690  if( seekWinFile(pFile, offset) ){
32691    return SQLITE_FULL;
32692  }
32693  while( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32694    if( retryIoerr(&nRetry) ) continue;
32695    pFile->lastErrno = GetLastError();
32696    return winLogError(SQLITE_IOERR_READ, "winRead", pFile->zPath);
32697  }
32698  logIoerr(nRetry);
32699  if( nRead<(DWORD)amt ){
32700    /* Unread parts of the buffer must be zero-filled */
32701    memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32702    return SQLITE_IOERR_SHORT_READ;
32703  }
32704
32705  return SQLITE_OK;
32706}
32707
32708/*
32709** Write data from a buffer into a file.  Return SQLITE_OK on success
32710** or some other error code on failure.
32711*/
32712static int winWrite(
32713  sqlite3_file *id,               /* File to write into */
32714  const void *pBuf,               /* The bytes to be written */
32715  int amt,                        /* Number of bytes to write */
32716  sqlite3_int64 offset            /* Offset into the file to begin writing at */
32717){
32718  int rc;                         /* True if error has occured, else false */
32719  winFile *pFile = (winFile*)id;  /* File handle */
32720  int nRetry = 0;                 /* Number of retries */
32721
32722  assert( amt>0 );
32723  assert( pFile );
32724  SimulateIOError(return SQLITE_IOERR_WRITE);
32725  SimulateDiskfullError(return SQLITE_FULL);
32726
32727  OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
32728
32729  rc = seekWinFile(pFile, offset);
32730  if( rc==0 ){
32731    u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
32732    int nRem = amt;               /* Number of bytes yet to be written */
32733    DWORD nWrite;                 /* Bytes written by each WriteFile() call */
32734
32735    while( nRem>0 ){
32736      if( !WriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
32737        if( retryIoerr(&nRetry) ) continue;
32738        break;
32739      }
32740      if( nWrite<=0 ) break;
32741      aRem += nWrite;
32742      nRem -= nWrite;
32743    }
32744    if( nRem>0 ){
32745      pFile->lastErrno = GetLastError();
32746      rc = 1;
32747    }
32748  }
32749
32750  if( rc ){
32751    if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
32752       || ( pFile->lastErrno==ERROR_DISK_FULL )){
32753      return SQLITE_FULL;
32754    }
32755    return winLogError(SQLITE_IOERR_WRITE, "winWrite", pFile->zPath);
32756  }else{
32757    logIoerr(nRetry);
32758  }
32759  return SQLITE_OK;
32760}
32761
32762/*
32763** Truncate an open file to a specified size
32764*/
32765static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
32766  winFile *pFile = (winFile*)id;  /* File handle object */
32767  int rc = SQLITE_OK;             /* Return code for this function */
32768
32769  assert( pFile );
32770
32771  OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
32772  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
32773
32774  /* If the user has configured a chunk-size for this file, truncate the
32775  ** file so that it consists of an integer number of chunks (i.e. the
32776  ** actual file size after the operation may be larger than the requested
32777  ** size).
32778  */
32779  if( pFile->szChunk>0 ){
32780    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
32781  }
32782
32783  /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32784  if( seekWinFile(pFile, nByte) ){
32785    rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
32786  }else if( 0==SetEndOfFile(pFile->h) ){
32787    pFile->lastErrno = GetLastError();
32788    rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate2", pFile->zPath);
32789  }
32790
32791  OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
32792  return rc;
32793}
32794
32795#ifdef SQLITE_TEST
32796/*
32797** Count the number of fullsyncs and normal syncs.  This is used to test
32798** that syncs and fullsyncs are occuring at the right times.
32799*/
32800SQLITE_API int sqlite3_sync_count = 0;
32801SQLITE_API int sqlite3_fullsync_count = 0;
32802#endif
32803
32804/*
32805** Make sure all writes to a particular file are committed to disk.
32806*/
32807static int winSync(sqlite3_file *id, int flags){
32808#ifndef SQLITE_NO_SYNC
32809  /*
32810  ** Used only when SQLITE_NO_SYNC is not defined.
32811   */
32812  BOOL rc;
32813#endif
32814#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
32815    (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
32816  /*
32817  ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
32818  ** OSTRACE() macros.
32819   */
32820  winFile *pFile = (winFile*)id;
32821#else
32822  UNUSED_PARAMETER(id);
32823#endif
32824
32825  assert( pFile );
32826  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
32827  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
32828      || (flags&0x0F)==SQLITE_SYNC_FULL
32829  );
32830
32831  OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
32832
32833  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
32834  ** line is to test that doing so does not cause any problems.
32835  */
32836  SimulateDiskfullError( return SQLITE_FULL );
32837
32838#ifndef SQLITE_TEST
32839  UNUSED_PARAMETER(flags);
32840#else
32841  if( (flags&0x0F)==SQLITE_SYNC_FULL ){
32842    sqlite3_fullsync_count++;
32843  }
32844  sqlite3_sync_count++;
32845#endif
32846
32847  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
32848  ** no-op
32849  */
32850#ifdef SQLITE_NO_SYNC
32851  return SQLITE_OK;
32852#else
32853  rc = FlushFileBuffers(pFile->h);
32854  SimulateIOError( rc=FALSE );
32855  if( rc ){
32856    return SQLITE_OK;
32857  }else{
32858    pFile->lastErrno = GetLastError();
32859    return winLogError(SQLITE_IOERR_FSYNC, "winSync", pFile->zPath);
32860  }
32861#endif
32862}
32863
32864/*
32865** Determine the current size of a file in bytes
32866*/
32867static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
32868  DWORD upperBits;
32869  DWORD lowerBits;
32870  winFile *pFile = (winFile*)id;
32871  DWORD error;
32872
32873  assert( id!=0 );
32874  SimulateIOError(return SQLITE_IOERR_FSTAT);
32875  lowerBits = GetFileSize(pFile->h, &upperBits);
32876  if(   (lowerBits == INVALID_FILE_SIZE)
32877     && ((error = GetLastError()) != NO_ERROR) )
32878  {
32879    pFile->lastErrno = error;
32880    return winLogError(SQLITE_IOERR_FSTAT, "winFileSize", pFile->zPath);
32881  }
32882  *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
32883  return SQLITE_OK;
32884}
32885
32886/*
32887** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
32888*/
32889#ifndef LOCKFILE_FAIL_IMMEDIATELY
32890# define LOCKFILE_FAIL_IMMEDIATELY 1
32891#endif
32892
32893/*
32894** Acquire a reader lock.
32895** Different API routines are called depending on whether or not this
32896** is Win95 or WinNT.
32897*/
32898static int getReadLock(winFile *pFile){
32899  int res;
32900  if( isNT() ){
32901    OVERLAPPED ovlp;
32902    ovlp.Offset = SHARED_FIRST;
32903    ovlp.OffsetHigh = 0;
32904    ovlp.hEvent = 0;
32905    res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
32906                     0, SHARED_SIZE, 0, &ovlp);
32907/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32908*/
32909#if SQLITE_OS_WINCE==0
32910  }else{
32911    int lk;
32912    sqlite3_randomness(sizeof(lk), &lk);
32913    pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
32914    res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
32915#endif
32916  }
32917  if( res == 0 ){
32918    pFile->lastErrno = GetLastError();
32919    /* No need to log a failure to lock */
32920  }
32921  return res;
32922}
32923
32924/*
32925** Undo a readlock
32926*/
32927static int unlockReadLock(winFile *pFile){
32928  int res;
32929  if( isNT() ){
32930    res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32931/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32932*/
32933#if SQLITE_OS_WINCE==0
32934  }else{
32935    res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
32936#endif
32937  }
32938  if( res==0 && GetLastError()!=ERROR_NOT_LOCKED ){
32939    pFile->lastErrno = GetLastError();
32940    winLogError(SQLITE_IOERR_UNLOCK, "unlockReadLock", pFile->zPath);
32941  }
32942  return res;
32943}
32944
32945/*
32946** Lock the file with the lock specified by parameter locktype - one
32947** of the following:
32948**
32949**     (1) SHARED_LOCK
32950**     (2) RESERVED_LOCK
32951**     (3) PENDING_LOCK
32952**     (4) EXCLUSIVE_LOCK
32953**
32954** Sometimes when requesting one lock state, additional lock states
32955** are inserted in between.  The locking might fail on one of the later
32956** transitions leaving the lock state different from what it started but
32957** still short of its goal.  The following chart shows the allowed
32958** transitions and the inserted intermediate states:
32959**
32960**    UNLOCKED -> SHARED
32961**    SHARED -> RESERVED
32962**    SHARED -> (PENDING) -> EXCLUSIVE
32963**    RESERVED -> (PENDING) -> EXCLUSIVE
32964**    PENDING -> EXCLUSIVE
32965**
32966** This routine will only increase a lock.  The winUnlock() routine
32967** erases all locks at once and returns us immediately to locking level 0.
32968** It is not possible to lower the locking level one step at a time.  You
32969** must go straight to locking level 0.
32970*/
32971static int winLock(sqlite3_file *id, int locktype){
32972  int rc = SQLITE_OK;    /* Return code from subroutines */
32973  int res = 1;           /* Result of a windows lock call */
32974  int newLocktype;       /* Set pFile->locktype to this value before exiting */
32975  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
32976  winFile *pFile = (winFile*)id;
32977  DWORD error = NO_ERROR;
32978
32979  assert( id!=0 );
32980  OSTRACE(("LOCK %d %d was %d(%d)\n",
32981           pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
32982
32983  /* If there is already a lock of this type or more restrictive on the
32984  ** OsFile, do nothing. Don't use the end_lock: exit path, as
32985  ** sqlite3OsEnterMutex() hasn't been called yet.
32986  */
32987  if( pFile->locktype>=locktype ){
32988    return SQLITE_OK;
32989  }
32990
32991  /* Make sure the locking sequence is correct
32992  */
32993  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
32994  assert( locktype!=PENDING_LOCK );
32995  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
32996
32997  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
32998  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
32999  ** the PENDING_LOCK byte is temporary.
33000  */
33001  newLocktype = pFile->locktype;
33002  if(   (pFile->locktype==NO_LOCK)
33003     || (   (locktype==EXCLUSIVE_LOCK)
33004         && (pFile->locktype==RESERVED_LOCK))
33005  ){
33006    int cnt = 3;
33007    while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
33008      /* Try 3 times to get the pending lock.  The pending lock might be
33009      ** held by another reader process who will release it momentarily.
33010      */
33011      OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
33012      Sleep(1);
33013    }
33014    gotPendingLock = res;
33015    if( !res ){
33016      error = GetLastError();
33017    }
33018  }
33019
33020  /* Acquire a shared lock
33021  */
33022  if( locktype==SHARED_LOCK && res ){
33023    assert( pFile->locktype==NO_LOCK );
33024    res = getReadLock(pFile);
33025    if( res ){
33026      newLocktype = SHARED_LOCK;
33027    }else{
33028      error = GetLastError();
33029    }
33030  }
33031
33032  /* Acquire a RESERVED lock
33033  */
33034  if( locktype==RESERVED_LOCK && res ){
33035    assert( pFile->locktype==SHARED_LOCK );
33036    res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33037    if( res ){
33038      newLocktype = RESERVED_LOCK;
33039    }else{
33040      error = GetLastError();
33041    }
33042  }
33043
33044  /* Acquire a PENDING lock
33045  */
33046  if( locktype==EXCLUSIVE_LOCK && res ){
33047    newLocktype = PENDING_LOCK;
33048    gotPendingLock = 0;
33049  }
33050
33051  /* Acquire an EXCLUSIVE lock
33052  */
33053  if( locktype==EXCLUSIVE_LOCK && res ){
33054    assert( pFile->locktype>=SHARED_LOCK );
33055    res = unlockReadLock(pFile);
33056    OSTRACE(("unreadlock = %d\n", res));
33057    res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33058    if( res ){
33059      newLocktype = EXCLUSIVE_LOCK;
33060    }else{
33061      error = GetLastError();
33062      OSTRACE(("error-code = %d\n", error));
33063      getReadLock(pFile);
33064    }
33065  }
33066
33067  /* If we are holding a PENDING lock that ought to be released, then
33068  ** release it now.
33069  */
33070  if( gotPendingLock && locktype==SHARED_LOCK ){
33071    UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
33072  }
33073
33074  /* Update the state of the lock has held in the file descriptor then
33075  ** return the appropriate result code.
33076  */
33077  if( res ){
33078    rc = SQLITE_OK;
33079  }else{
33080    OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
33081           locktype, newLocktype));
33082    pFile->lastErrno = error;
33083    rc = SQLITE_BUSY;
33084  }
33085  pFile->locktype = (u8)newLocktype;
33086  return rc;
33087}
33088
33089/*
33090** This routine checks if there is a RESERVED lock held on the specified
33091** file by this or any other process. If such a lock is held, return
33092** non-zero, otherwise zero.
33093*/
33094static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
33095  int rc;
33096  winFile *pFile = (winFile*)id;
33097
33098  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
33099
33100  assert( id!=0 );
33101  if( pFile->locktype>=RESERVED_LOCK ){
33102    rc = 1;
33103    OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
33104  }else{
33105    rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33106    if( rc ){
33107      UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33108    }
33109    rc = !rc;
33110    OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
33111  }
33112  *pResOut = rc;
33113  return SQLITE_OK;
33114}
33115
33116/*
33117** Lower the locking level on file descriptor id to locktype.  locktype
33118** must be either NO_LOCK or SHARED_LOCK.
33119**
33120** If the locking level of the file descriptor is already at or below
33121** the requested locking level, this routine is a no-op.
33122**
33123** It is not possible for this routine to fail if the second argument
33124** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
33125** might return SQLITE_IOERR;
33126*/
33127static int winUnlock(sqlite3_file *id, int locktype){
33128  int type;
33129  winFile *pFile = (winFile*)id;
33130  int rc = SQLITE_OK;
33131  assert( pFile!=0 );
33132  assert( locktype<=SHARED_LOCK );
33133  OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
33134          pFile->locktype, pFile->sharedLockByte));
33135  type = pFile->locktype;
33136  if( type>=EXCLUSIVE_LOCK ){
33137    UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33138    if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33139      /* This should never happen.  We should always be able to
33140      ** reacquire the read lock */
33141      rc = winLogError(SQLITE_IOERR_UNLOCK, "winUnlock", pFile->zPath);
33142    }
33143  }
33144  if( type>=RESERVED_LOCK ){
33145    UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33146  }
33147  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33148    unlockReadLock(pFile);
33149  }
33150  if( type>=PENDING_LOCK ){
33151    UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
33152  }
33153  pFile->locktype = (u8)locktype;
33154  return rc;
33155}
33156
33157/*
33158** Control and query of the open file handle.
33159*/
33160static int winFileControl(sqlite3_file *id, int op, void *pArg){
33161  winFile *pFile = (winFile*)id;
33162  switch( op ){
33163    case SQLITE_FCNTL_LOCKSTATE: {
33164      *(int*)pArg = pFile->locktype;
33165      return SQLITE_OK;
33166    }
33167    case SQLITE_LAST_ERRNO: {
33168      *(int*)pArg = (int)pFile->lastErrno;
33169      return SQLITE_OK;
33170    }
33171    case SQLITE_FCNTL_CHUNK_SIZE: {
33172      pFile->szChunk = *(int *)pArg;
33173      return SQLITE_OK;
33174    }
33175    case SQLITE_FCNTL_SIZE_HINT: {
33176      if( pFile->szChunk>0 ){
33177        sqlite3_int64 oldSz;
33178        int rc = winFileSize(id, &oldSz);
33179        if( rc==SQLITE_OK ){
33180          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
33181          if( newSz>oldSz ){
33182            SimulateIOErrorBenign(1);
33183            rc = winTruncate(id, newSz);
33184            SimulateIOErrorBenign(0);
33185          }
33186        }
33187        return rc;
33188      }
33189      return SQLITE_OK;
33190    }
33191    case SQLITE_FCNTL_PERSIST_WAL: {
33192      int bPersist = *(int*)pArg;
33193      if( bPersist<0 ){
33194        *(int*)pArg = pFile->bPersistWal;
33195      }else{
33196        pFile->bPersistWal = bPersist!=0;
33197      }
33198      return SQLITE_OK;
33199    }
33200    case SQLITE_FCNTL_SYNC_OMITTED: {
33201      return SQLITE_OK;
33202    }
33203    case SQLITE_FCNTL_WIN32_AV_RETRY: {
33204      int *a = (int*)pArg;
33205      if( a[0]>0 ){
33206        win32IoerrRetry = a[0];
33207      }else{
33208        a[0] = win32IoerrRetry;
33209      }
33210      if( a[1]>0 ){
33211        win32IoerrRetryDelay = a[1];
33212      }else{
33213        a[1] = win32IoerrRetryDelay;
33214      }
33215      return SQLITE_OK;
33216    }
33217  }
33218  return SQLITE_NOTFOUND;
33219}
33220
33221/*
33222** Return the sector size in bytes of the underlying block device for
33223** the specified file. This is almost always 512 bytes, but may be
33224** larger for some devices.
33225**
33226** SQLite code assumes this function cannot fail. It also assumes that
33227** if two files are created in the same file-system directory (i.e.
33228** a database and its journal file) that the sector size will be the
33229** same for both.
33230*/
33231static int winSectorSize(sqlite3_file *id){
33232  assert( id!=0 );
33233  return (int)(((winFile*)id)->sectorSize);
33234}
33235
33236/*
33237** Return a vector of device characteristics.
33238*/
33239static int winDeviceCharacteristics(sqlite3_file *id){
33240  UNUSED_PARAMETER(id);
33241  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
33242}
33243
33244#ifndef SQLITE_OMIT_WAL
33245
33246/*
33247** Windows will only let you create file view mappings
33248** on allocation size granularity boundaries.
33249** During sqlite3_os_init() we do a GetSystemInfo()
33250** to get the granularity size.
33251*/
33252SYSTEM_INFO winSysInfo;
33253
33254/*
33255** Helper functions to obtain and relinquish the global mutex. The
33256** global mutex is used to protect the winLockInfo objects used by
33257** this file, all of which may be shared by multiple threads.
33258**
33259** Function winShmMutexHeld() is used to assert() that the global mutex
33260** is held when required. This function is only used as part of assert()
33261** statements. e.g.
33262**
33263**   winShmEnterMutex()
33264**     assert( winShmMutexHeld() );
33265**   winShmLeaveMutex()
33266*/
33267static void winShmEnterMutex(void){
33268  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
33269}
33270static void winShmLeaveMutex(void){
33271  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
33272}
33273#ifdef SQLITE_DEBUG
33274static int winShmMutexHeld(void) {
33275  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
33276}
33277#endif
33278
33279/*
33280** Object used to represent a single file opened and mmapped to provide
33281** shared memory.  When multiple threads all reference the same
33282** log-summary, each thread has its own winFile object, but they all
33283** point to a single instance of this object.  In other words, each
33284** log-summary is opened only once per process.
33285**
33286** winShmMutexHeld() must be true when creating or destroying
33287** this object or while reading or writing the following fields:
33288**
33289**      nRef
33290**      pNext
33291**
33292** The following fields are read-only after the object is created:
33293**
33294**      fid
33295**      zFilename
33296**
33297** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
33298** winShmMutexHeld() is true when reading or writing any other field
33299** in this structure.
33300**
33301*/
33302struct winShmNode {
33303  sqlite3_mutex *mutex;      /* Mutex to access this object */
33304  char *zFilename;           /* Name of the file */
33305  winFile hFile;             /* File handle from winOpen */
33306
33307  int szRegion;              /* Size of shared-memory regions */
33308  int nRegion;               /* Size of array apRegion */
33309  struct ShmRegion {
33310    HANDLE hMap;             /* File handle from CreateFileMapping */
33311    void *pMap;
33312  } *aRegion;
33313  DWORD lastErrno;           /* The Windows errno from the last I/O error */
33314
33315  int nRef;                  /* Number of winShm objects pointing to this */
33316  winShm *pFirst;            /* All winShm objects pointing to this */
33317  winShmNode *pNext;         /* Next in list of all winShmNode objects */
33318#ifdef SQLITE_DEBUG
33319  u8 nextShmId;              /* Next available winShm.id value */
33320#endif
33321};
33322
33323/*
33324** A global array of all winShmNode objects.
33325**
33326** The winShmMutexHeld() must be true while reading or writing this list.
33327*/
33328static winShmNode *winShmNodeList = 0;
33329
33330/*
33331** Structure used internally by this VFS to record the state of an
33332** open shared memory connection.
33333**
33334** The following fields are initialized when this object is created and
33335** are read-only thereafter:
33336**
33337**    winShm.pShmNode
33338**    winShm.id
33339**
33340** All other fields are read/write.  The winShm.pShmNode->mutex must be held
33341** while accessing any read/write fields.
33342*/
33343struct winShm {
33344  winShmNode *pShmNode;      /* The underlying winShmNode object */
33345  winShm *pNext;             /* Next winShm with the same winShmNode */
33346  u8 hasMutex;               /* True if holding the winShmNode mutex */
33347  u16 sharedMask;            /* Mask of shared locks held */
33348  u16 exclMask;              /* Mask of exclusive locks held */
33349#ifdef SQLITE_DEBUG
33350  u8 id;                     /* Id of this connection with its winShmNode */
33351#endif
33352};
33353
33354/*
33355** Constants used for locking
33356*/
33357#define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
33358#define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
33359
33360/*
33361** Apply advisory locks for all n bytes beginning at ofst.
33362*/
33363#define _SHM_UNLCK  1
33364#define _SHM_RDLCK  2
33365#define _SHM_WRLCK  3
33366static int winShmSystemLock(
33367  winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
33368  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
33369  int ofst,             /* Offset to first byte to be locked/unlocked */
33370  int nByte             /* Number of bytes to lock or unlock */
33371){
33372  OVERLAPPED ovlp;
33373  DWORD dwFlags;
33374  int rc = 0;           /* Result code form Lock/UnlockFileEx() */
33375
33376  /* Access to the winShmNode object is serialized by the caller */
33377  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
33378
33379  /* Initialize the locking parameters */
33380  dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
33381  if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
33382
33383  memset(&ovlp, 0, sizeof(OVERLAPPED));
33384  ovlp.Offset = ofst;
33385
33386  /* Release/Acquire the system-level lock */
33387  if( lockType==_SHM_UNLCK ){
33388    rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
33389  }else{
33390    rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
33391  }
33392
33393  if( rc!= 0 ){
33394    rc = SQLITE_OK;
33395  }else{
33396    pFile->lastErrno =  GetLastError();
33397    rc = SQLITE_BUSY;
33398  }
33399
33400  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
33401           pFile->hFile.h,
33402           rc==SQLITE_OK ? "ok" : "failed",
33403           lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
33404           pFile->lastErrno));
33405
33406  return rc;
33407}
33408
33409/* Forward references to VFS methods */
33410static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
33411static int winDelete(sqlite3_vfs *,const char*,int);
33412
33413/*
33414** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
33415**
33416** This is not a VFS shared-memory method; it is a utility function called
33417** by VFS shared-memory methods.
33418*/
33419static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
33420  winShmNode **pp;
33421  winShmNode *p;
33422  BOOL bRc;
33423  assert( winShmMutexHeld() );
33424  pp = &winShmNodeList;
33425  while( (p = *pp)!=0 ){
33426    if( p->nRef==0 ){
33427      int i;
33428      if( p->mutex ) sqlite3_mutex_free(p->mutex);
33429      for(i=0; i<p->nRegion; i++){
33430        bRc = UnmapViewOfFile(p->aRegion[i].pMap);
33431        OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
33432                 (int)GetCurrentProcessId(), i,
33433                 bRc ? "ok" : "failed"));
33434        bRc = CloseHandle(p->aRegion[i].hMap);
33435        OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
33436                 (int)GetCurrentProcessId(), i,
33437                 bRc ? "ok" : "failed"));
33438      }
33439      if( p->hFile.h != INVALID_HANDLE_VALUE ){
33440        SimulateIOErrorBenign(1);
33441        winClose((sqlite3_file *)&p->hFile);
33442        SimulateIOErrorBenign(0);
33443      }
33444      if( deleteFlag ){
33445        SimulateIOErrorBenign(1);
33446        winDelete(pVfs, p->zFilename, 0);
33447        SimulateIOErrorBenign(0);
33448      }
33449      *pp = p->pNext;
33450      sqlite3_free(p->aRegion);
33451      sqlite3_free(p);
33452    }else{
33453      pp = &p->pNext;
33454    }
33455  }
33456}
33457
33458/*
33459** Open the shared-memory area associated with database file pDbFd.
33460**
33461** When opening a new shared-memory file, if no other instances of that
33462** file are currently open, in this process or in other processes, then
33463** the file must be truncated to zero length or have its header cleared.
33464*/
33465static int winOpenSharedMemory(winFile *pDbFd){
33466  struct winShm *p;                  /* The connection to be opened */
33467  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
33468  int rc;                            /* Result code */
33469  struct winShmNode *pNew;           /* Newly allocated winShmNode */
33470  int nName;                         /* Size of zName in bytes */
33471
33472  assert( pDbFd->pShm==0 );    /* Not previously opened */
33473
33474  /* Allocate space for the new sqlite3_shm object.  Also speculatively
33475  ** allocate space for a new winShmNode and filename.
33476  */
33477  p = sqlite3_malloc( sizeof(*p) );
33478  if( p==0 ) return SQLITE_NOMEM;
33479  memset(p, 0, sizeof(*p));
33480  nName = sqlite3Strlen30(pDbFd->zPath);
33481  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
33482  if( pNew==0 ){
33483    sqlite3_free(p);
33484    return SQLITE_NOMEM;
33485  }
33486  memset(pNew, 0, sizeof(*pNew));
33487  pNew->zFilename = (char*)&pNew[1];
33488  sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
33489  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
33490
33491  /* Look to see if there is an existing winShmNode that can be used.
33492  ** If no matching winShmNode currently exists, create a new one.
33493  */
33494  winShmEnterMutex();
33495  for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
33496    /* TBD need to come up with better match here.  Perhaps
33497    ** use FILE_ID_BOTH_DIR_INFO Structure.
33498    */
33499    if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
33500  }
33501  if( pShmNode ){
33502    sqlite3_free(pNew);
33503  }else{
33504    pShmNode = pNew;
33505    pNew = 0;
33506    ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
33507    pShmNode->pNext = winShmNodeList;
33508    winShmNodeList = pShmNode;
33509
33510    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
33511    if( pShmNode->mutex==0 ){
33512      rc = SQLITE_NOMEM;
33513      goto shm_open_err;
33514    }
33515
33516    rc = winOpen(pDbFd->pVfs,
33517                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
33518                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
33519                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
33520                 0);
33521    if( SQLITE_OK!=rc ){
33522      rc = SQLITE_CANTOPEN_BKPT;
33523      goto shm_open_err;
33524    }
33525
33526    /* Check to see if another process is holding the dead-man switch.
33527    ** If not, truncate the file to zero length.
33528    */
33529    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
33530      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
33531      if( rc!=SQLITE_OK ){
33532        rc = winLogError(SQLITE_IOERR_SHMOPEN, "winOpenShm", pDbFd->zPath);
33533      }
33534    }
33535    if( rc==SQLITE_OK ){
33536      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33537      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
33538    }
33539    if( rc ) goto shm_open_err;
33540  }
33541
33542  /* Make the new connection a child of the winShmNode */
33543  p->pShmNode = pShmNode;
33544#ifdef SQLITE_DEBUG
33545  p->id = pShmNode->nextShmId++;
33546#endif
33547  pShmNode->nRef++;
33548  pDbFd->pShm = p;
33549  winShmLeaveMutex();
33550
33551  /* The reference count on pShmNode has already been incremented under
33552  ** the cover of the winShmEnterMutex() mutex and the pointer from the
33553  ** new (struct winShm) object to the pShmNode has been set. All that is
33554  ** left to do is to link the new object into the linked list starting
33555  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
33556  ** mutex.
33557  */
33558  sqlite3_mutex_enter(pShmNode->mutex);
33559  p->pNext = pShmNode->pFirst;
33560  pShmNode->pFirst = p;
33561  sqlite3_mutex_leave(pShmNode->mutex);
33562  return SQLITE_OK;
33563
33564  /* Jump here on any error */
33565shm_open_err:
33566  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33567  winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
33568  sqlite3_free(p);
33569  sqlite3_free(pNew);
33570  winShmLeaveMutex();
33571  return rc;
33572}
33573
33574/*
33575** Close a connection to shared-memory.  Delete the underlying
33576** storage if deleteFlag is true.
33577*/
33578static int winShmUnmap(
33579  sqlite3_file *fd,          /* Database holding shared memory */
33580  int deleteFlag             /* Delete after closing if true */
33581){
33582  winFile *pDbFd;       /* Database holding shared-memory */
33583  winShm *p;            /* The connection to be closed */
33584  winShmNode *pShmNode; /* The underlying shared-memory file */
33585  winShm **pp;          /* For looping over sibling connections */
33586
33587  pDbFd = (winFile*)fd;
33588  p = pDbFd->pShm;
33589  if( p==0 ) return SQLITE_OK;
33590  pShmNode = p->pShmNode;
33591
33592  /* Remove connection p from the set of connections associated
33593  ** with pShmNode */
33594  sqlite3_mutex_enter(pShmNode->mutex);
33595  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
33596  *pp = p->pNext;
33597
33598  /* Free the connection p */
33599  sqlite3_free(p);
33600  pDbFd->pShm = 0;
33601  sqlite3_mutex_leave(pShmNode->mutex);
33602
33603  /* If pShmNode->nRef has reached 0, then close the underlying
33604  ** shared-memory file, too */
33605  winShmEnterMutex();
33606  assert( pShmNode->nRef>0 );
33607  pShmNode->nRef--;
33608  if( pShmNode->nRef==0 ){
33609    winShmPurge(pDbFd->pVfs, deleteFlag);
33610  }
33611  winShmLeaveMutex();
33612
33613  return SQLITE_OK;
33614}
33615
33616/*
33617** Change the lock state for a shared-memory segment.
33618*/
33619static int winShmLock(
33620  sqlite3_file *fd,          /* Database file holding the shared memory */
33621  int ofst,                  /* First lock to acquire or release */
33622  int n,                     /* Number of locks to acquire or release */
33623  int flags                  /* What to do with the lock */
33624){
33625  winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
33626  winShm *p = pDbFd->pShm;              /* The shared memory being locked */
33627  winShm *pX;                           /* For looping over all siblings */
33628  winShmNode *pShmNode = p->pShmNode;
33629  int rc = SQLITE_OK;                   /* Result code */
33630  u16 mask;                             /* Mask of locks to take or release */
33631
33632  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
33633  assert( n>=1 );
33634  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
33635       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
33636       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
33637       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
33638  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
33639
33640  mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
33641  assert( n>1 || mask==(1<<ofst) );
33642  sqlite3_mutex_enter(pShmNode->mutex);
33643  if( flags & SQLITE_SHM_UNLOCK ){
33644    u16 allMask = 0; /* Mask of locks held by siblings */
33645
33646    /* See if any siblings hold this same lock */
33647    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33648      if( pX==p ) continue;
33649      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
33650      allMask |= pX->sharedMask;
33651    }
33652
33653    /* Unlock the system-level locks */
33654    if( (mask & allMask)==0 ){
33655      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
33656    }else{
33657      rc = SQLITE_OK;
33658    }
33659
33660    /* Undo the local locks */
33661    if( rc==SQLITE_OK ){
33662      p->exclMask &= ~mask;
33663      p->sharedMask &= ~mask;
33664    }
33665  }else if( flags & SQLITE_SHM_SHARED ){
33666    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
33667
33668    /* Find out which shared locks are already held by sibling connections.
33669    ** If any sibling already holds an exclusive lock, go ahead and return
33670    ** SQLITE_BUSY.
33671    */
33672    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33673      if( (pX->exclMask & mask)!=0 ){
33674        rc = SQLITE_BUSY;
33675        break;
33676      }
33677      allShared |= pX->sharedMask;
33678    }
33679
33680    /* Get shared locks at the system level, if necessary */
33681    if( rc==SQLITE_OK ){
33682      if( (allShared & mask)==0 ){
33683        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
33684      }else{
33685        rc = SQLITE_OK;
33686      }
33687    }
33688
33689    /* Get the local shared locks */
33690    if( rc==SQLITE_OK ){
33691      p->sharedMask |= mask;
33692    }
33693  }else{
33694    /* Make sure no sibling connections hold locks that will block this
33695    ** lock.  If any do, return SQLITE_BUSY right away.
33696    */
33697    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
33698      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
33699        rc = SQLITE_BUSY;
33700        break;
33701      }
33702    }
33703
33704    /* Get the exclusive locks at the system level.  Then if successful
33705    ** also mark the local connection as being locked.
33706    */
33707    if( rc==SQLITE_OK ){
33708      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
33709      if( rc==SQLITE_OK ){
33710        assert( (p->sharedMask & mask)==0 );
33711        p->exclMask |= mask;
33712      }
33713    }
33714  }
33715  sqlite3_mutex_leave(pShmNode->mutex);
33716  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
33717           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
33718           rc ? "failed" : "ok"));
33719  return rc;
33720}
33721
33722/*
33723** Implement a memory barrier or memory fence on shared memory.
33724**
33725** All loads and stores begun before the barrier must complete before
33726** any load or store begun after the barrier.
33727*/
33728static void winShmBarrier(
33729  sqlite3_file *fd          /* Database holding the shared memory */
33730){
33731  UNUSED_PARAMETER(fd);
33732  /* MemoryBarrier(); // does not work -- do not know why not */
33733  winShmEnterMutex();
33734  winShmLeaveMutex();
33735}
33736
33737/*
33738** This function is called to obtain a pointer to region iRegion of the
33739** shared-memory associated with the database file fd. Shared-memory regions
33740** are numbered starting from zero. Each shared-memory region is szRegion
33741** bytes in size.
33742**
33743** If an error occurs, an error code is returned and *pp is set to NULL.
33744**
33745** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
33746** region has not been allocated (by any client, including one running in a
33747** separate process), then *pp is set to NULL and SQLITE_OK returned. If
33748** isWrite is non-zero and the requested shared-memory region has not yet
33749** been allocated, it is allocated by this function.
33750**
33751** If the shared-memory region has already been allocated or is allocated by
33752** this call as described above, then it is mapped into this processes
33753** address space (if it is not already), *pp is set to point to the mapped
33754** memory and SQLITE_OK returned.
33755*/
33756static int winShmMap(
33757  sqlite3_file *fd,               /* Handle open on database file */
33758  int iRegion,                    /* Region to retrieve */
33759  int szRegion,                   /* Size of regions */
33760  int isWrite,                    /* True to extend file if necessary */
33761  void volatile **pp              /* OUT: Mapped memory */
33762){
33763  winFile *pDbFd = (winFile*)fd;
33764  winShm *p = pDbFd->pShm;
33765  winShmNode *pShmNode;
33766  int rc = SQLITE_OK;
33767
33768  if( !p ){
33769    rc = winOpenSharedMemory(pDbFd);
33770    if( rc!=SQLITE_OK ) return rc;
33771    p = pDbFd->pShm;
33772  }
33773  pShmNode = p->pShmNode;
33774
33775  sqlite3_mutex_enter(pShmNode->mutex);
33776  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
33777
33778  if( pShmNode->nRegion<=iRegion ){
33779    struct ShmRegion *apNew;           /* New aRegion[] array */
33780    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
33781    sqlite3_int64 sz;                  /* Current size of wal-index file */
33782
33783    pShmNode->szRegion = szRegion;
33784
33785    /* The requested region is not mapped into this processes address space.
33786    ** Check to see if it has been allocated (i.e. if the wal-index file is
33787    ** large enough to contain the requested region).
33788    */
33789    rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
33790    if( rc!=SQLITE_OK ){
33791      rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap1", pDbFd->zPath);
33792      goto shmpage_out;
33793    }
33794
33795    if( sz<nByte ){
33796      /* The requested memory region does not exist. If isWrite is set to
33797      ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
33798      **
33799      ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
33800      ** the requested memory region.
33801      */
33802      if( !isWrite ) goto shmpage_out;
33803      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
33804      if( rc!=SQLITE_OK ){
33805        rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap2", pDbFd->zPath);
33806        goto shmpage_out;
33807      }
33808    }
33809
33810    /* Map the requested memory region into this processes address space. */
33811    apNew = (struct ShmRegion *)sqlite3_realloc(
33812        pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
33813    );
33814    if( !apNew ){
33815      rc = SQLITE_IOERR_NOMEM;
33816      goto shmpage_out;
33817    }
33818    pShmNode->aRegion = apNew;
33819
33820    while( pShmNode->nRegion<=iRegion ){
33821      HANDLE hMap;                /* file-mapping handle */
33822      void *pMap = 0;             /* Mapped memory region */
33823
33824      hMap = CreateFileMapping(pShmNode->hFile.h,
33825          NULL, PAGE_READWRITE, 0, nByte, NULL
33826      );
33827      OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
33828               (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
33829               hMap ? "ok" : "failed"));
33830      if( hMap ){
33831        int iOffset = pShmNode->nRegion*szRegion;
33832        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33833        pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
33834            0, iOffset - iOffsetShift, szRegion + iOffsetShift
33835        );
33836        OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
33837                 (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
33838                 pMap ? "ok" : "failed"));
33839      }
33840      if( !pMap ){
33841        pShmNode->lastErrno = GetLastError();
33842        rc = winLogError(SQLITE_IOERR_SHMMAP, "winShmMap3", pDbFd->zPath);
33843        if( hMap ) CloseHandle(hMap);
33844        goto shmpage_out;
33845      }
33846
33847      pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
33848      pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
33849      pShmNode->nRegion++;
33850    }
33851  }
33852
33853shmpage_out:
33854  if( pShmNode->nRegion>iRegion ){
33855    int iOffset = iRegion*szRegion;
33856    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33857    char *p = (char *)pShmNode->aRegion[iRegion].pMap;
33858    *pp = (void *)&p[iOffsetShift];
33859  }else{
33860    *pp = 0;
33861  }
33862  sqlite3_mutex_leave(pShmNode->mutex);
33863  return rc;
33864}
33865
33866#else
33867# define winShmMap     0
33868# define winShmLock    0
33869# define winShmBarrier 0
33870# define winShmUnmap   0
33871#endif /* #ifndef SQLITE_OMIT_WAL */
33872
33873/*
33874** Here ends the implementation of all sqlite3_file methods.
33875**
33876********************** End sqlite3_file Methods *******************************
33877******************************************************************************/
33878
33879/*
33880** This vector defines all the methods that can operate on an
33881** sqlite3_file for win32.
33882*/
33883static const sqlite3_io_methods winIoMethod = {
33884  2,                              /* iVersion */
33885  winClose,                       /* xClose */
33886  winRead,                        /* xRead */
33887  winWrite,                       /* xWrite */
33888  winTruncate,                    /* xTruncate */
33889  winSync,                        /* xSync */
33890  winFileSize,                    /* xFileSize */
33891  winLock,                        /* xLock */
33892  winUnlock,                      /* xUnlock */
33893  winCheckReservedLock,           /* xCheckReservedLock */
33894  winFileControl,                 /* xFileControl */
33895  winSectorSize,                  /* xSectorSize */
33896  winDeviceCharacteristics,       /* xDeviceCharacteristics */
33897  winShmMap,                      /* xShmMap */
33898  winShmLock,                     /* xShmLock */
33899  winShmBarrier,                  /* xShmBarrier */
33900  winShmUnmap                     /* xShmUnmap */
33901};
33902
33903/****************************************************************************
33904**************************** sqlite3_vfs methods ****************************
33905**
33906** This division contains the implementation of methods on the
33907** sqlite3_vfs object.
33908*/
33909
33910/*
33911** Convert a UTF-8 filename into whatever form the underlying
33912** operating system wants filenames in.  Space to hold the result
33913** is obtained from malloc and must be freed by the calling
33914** function.
33915*/
33916static void *convertUtf8Filename(const char *zFilename){
33917  void *zConverted = 0;
33918  if( isNT() ){
33919    zConverted = utf8ToUnicode(zFilename);
33920/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33921*/
33922#if SQLITE_OS_WINCE==0
33923  }else{
33924    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
33925#endif
33926  }
33927  /* caller will handle out of memory */
33928  return zConverted;
33929}
33930
33931/*
33932** Create a temporary file name in zBuf.  zBuf must be big enough to
33933** hold at pVfs->mxPathname characters.
33934*/
33935static int getTempname(int nBuf, char *zBuf){
33936  static char zChars[] =
33937    "abcdefghijklmnopqrstuvwxyz"
33938    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33939    "0123456789";
33940  size_t i, j;
33941  char zTempPath[MAX_PATH+1];
33942
33943  /* It's odd to simulate an io-error here, but really this is just
33944  ** using the io-error infrastructure to test that SQLite handles this
33945  ** function failing.
33946  */
33947  SimulateIOError( return SQLITE_IOERR );
33948
33949  if( sqlite3_temp_directory ){
33950    sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
33951  }else if( isNT() ){
33952    char *zMulti;
33953    WCHAR zWidePath[MAX_PATH];
33954    GetTempPathW(MAX_PATH-30, zWidePath);
33955    zMulti = unicodeToUtf8(zWidePath);
33956    if( zMulti ){
33957      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
33958      free(zMulti);
33959    }else{
33960      return SQLITE_NOMEM;
33961    }
33962/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33963** Since the ASCII version of these Windows API do not exist for WINCE,
33964** it's important to not reference them for WINCE builds.
33965*/
33966#if SQLITE_OS_WINCE==0
33967  }else{
33968    char *zUtf8;
33969    char zMbcsPath[MAX_PATH];
33970    GetTempPathA(MAX_PATH-30, zMbcsPath);
33971    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
33972    if( zUtf8 ){
33973      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
33974      free(zUtf8);
33975    }else{
33976      return SQLITE_NOMEM;
33977    }
33978#endif
33979  }
33980
33981  /* Check that the output buffer is large enough for the temporary file
33982  ** name. If it is not, return SQLITE_ERROR.
33983  */
33984  if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
33985    return SQLITE_ERROR;
33986  }
33987
33988  for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
33989  zTempPath[i] = 0;
33990
33991  sqlite3_snprintf(nBuf-17, zBuf,
33992                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
33993  j = sqlite3Strlen30(zBuf);
33994  sqlite3_randomness(15, &zBuf[j]);
33995  for(i=0; i<15; i++, j++){
33996    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
33997  }
33998  zBuf[j] = 0;
33999
34000  OSTRACE(("TEMP FILENAME: %s\n", zBuf));
34001  return SQLITE_OK;
34002}
34003
34004/*
34005** Open a file.
34006*/
34007static int winOpen(
34008  sqlite3_vfs *pVfs,        /* Not used */
34009  const char *zName,        /* Name of the file (UTF-8) */
34010  sqlite3_file *id,         /* Write the SQLite file handle here */
34011  int flags,                /* Open mode flags */
34012  int *pOutFlags            /* Status return flags */
34013){
34014  HANDLE h;
34015  DWORD dwDesiredAccess;
34016  DWORD dwShareMode;
34017  DWORD dwCreationDisposition;
34018  DWORD dwFlagsAndAttributes = 0;
34019#if SQLITE_OS_WINCE
34020  int isTemp = 0;
34021#endif
34022  winFile *pFile = (winFile*)id;
34023  void *zConverted;              /* Filename in OS encoding */
34024  const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
34025  int cnt = 0;
34026
34027  /* If argument zPath is a NULL pointer, this function is required to open
34028  ** a temporary file. Use this buffer to store the file name in.
34029  */
34030  char zTmpname[MAX_PATH+1];     /* Buffer used to create temp filename */
34031
34032  int rc = SQLITE_OK;            /* Function Return Code */
34033#if !defined(NDEBUG) || SQLITE_OS_WINCE
34034  int eType = flags&0xFFFFFF00;  /* Type of file to open */
34035#endif
34036
34037  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
34038  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
34039  int isCreate     = (flags & SQLITE_OPEN_CREATE);
34040#ifndef NDEBUG
34041  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
34042#endif
34043  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
34044
34045#ifndef NDEBUG
34046  int isOpenJournal = (isCreate && (
34047        eType==SQLITE_OPEN_MASTER_JOURNAL
34048     || eType==SQLITE_OPEN_MAIN_JOURNAL
34049     || eType==SQLITE_OPEN_WAL
34050  ));
34051#endif
34052
34053  /* Check the following statements are true:
34054  **
34055  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
34056  **   (b) if CREATE is set, then READWRITE must also be set, and
34057  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
34058  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
34059  */
34060  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
34061  assert(isCreate==0 || isReadWrite);
34062  assert(isExclusive==0 || isCreate);
34063  assert(isDelete==0 || isCreate);
34064
34065  /* The main DB, main journal, WAL file and master journal are never
34066  ** automatically deleted. Nor are they ever temporary files.  */
34067  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
34068  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
34069  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
34070  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
34071
34072  /* Assert that the upper layer has set one of the "file-type" flags. */
34073  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
34074       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
34075       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
34076       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
34077  );
34078
34079  assert( id!=0 );
34080  UNUSED_PARAMETER(pVfs);
34081
34082  pFile->h = INVALID_HANDLE_VALUE;
34083
34084  /* If the second argument to this function is NULL, generate a
34085  ** temporary file name to use
34086  */
34087  if( !zUtf8Name ){
34088    assert(isDelete && !isOpenJournal);
34089    rc = getTempname(MAX_PATH+1, zTmpname);
34090    if( rc!=SQLITE_OK ){
34091      return rc;
34092    }
34093    zUtf8Name = zTmpname;
34094  }
34095
34096  /* Convert the filename to the system encoding. */
34097  zConverted = convertUtf8Filename(zUtf8Name);
34098  if( zConverted==0 ){
34099    return SQLITE_NOMEM;
34100  }
34101
34102  if( isReadWrite ){
34103    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
34104  }else{
34105    dwDesiredAccess = GENERIC_READ;
34106  }
34107
34108  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
34109  ** created. SQLite doesn't use it to indicate "exclusive access"
34110  ** as it is usually understood.
34111  */
34112  if( isExclusive ){
34113    /* Creates a new file, only if it does not already exist. */
34114    /* If the file exists, it fails. */
34115    dwCreationDisposition = CREATE_NEW;
34116  }else if( isCreate ){
34117    /* Open existing file, or create if it doesn't exist */
34118    dwCreationDisposition = OPEN_ALWAYS;
34119  }else{
34120    /* Opens a file, only if it exists. */
34121    dwCreationDisposition = OPEN_EXISTING;
34122  }
34123
34124  dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
34125
34126  if( isDelete ){
34127#if SQLITE_OS_WINCE
34128    dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
34129    isTemp = 1;
34130#else
34131    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
34132                               | FILE_ATTRIBUTE_HIDDEN
34133                               | FILE_FLAG_DELETE_ON_CLOSE;
34134#endif
34135  }else{
34136    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
34137  }
34138  /* Reports from the internet are that performance is always
34139  ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
34140#if SQLITE_OS_WINCE
34141  dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
34142#endif
34143
34144  if( isNT() ){
34145    while( (h = CreateFileW((WCHAR*)zConverted,
34146                            dwDesiredAccess,
34147                            dwShareMode, NULL,
34148                            dwCreationDisposition,
34149                            dwFlagsAndAttributes,
34150                            NULL))==INVALID_HANDLE_VALUE &&
34151                            retryIoerr(&cnt) ){}
34152/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34153** Since the ASCII version of these Windows API do not exist for WINCE,
34154** it's important to not reference them for WINCE builds.
34155*/
34156#if SQLITE_OS_WINCE==0
34157  }else{
34158    while( (h = CreateFileA((char*)zConverted,
34159                            dwDesiredAccess,
34160                            dwShareMode, NULL,
34161                            dwCreationDisposition,
34162                            dwFlagsAndAttributes,
34163                            NULL))==INVALID_HANDLE_VALUE &&
34164                            retryIoerr(&cnt) ){}
34165#endif
34166  }
34167
34168  logIoerr(cnt);
34169
34170  OSTRACE(("OPEN %d %s 0x%lx %s\n",
34171           h, zName, dwDesiredAccess,
34172           h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
34173
34174  if( h==INVALID_HANDLE_VALUE ){
34175    pFile->lastErrno = GetLastError();
34176    winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
34177    free(zConverted);
34178    if( isReadWrite ){
34179      return winOpen(pVfs, zName, id,
34180             ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
34181    }else{
34182      return SQLITE_CANTOPEN_BKPT;
34183    }
34184  }
34185
34186  if( pOutFlags ){
34187    if( isReadWrite ){
34188      *pOutFlags = SQLITE_OPEN_READWRITE;
34189    }else{
34190      *pOutFlags = SQLITE_OPEN_READONLY;
34191    }
34192  }
34193
34194  memset(pFile, 0, sizeof(*pFile));
34195  pFile->pMethod = &winIoMethod;
34196  pFile->h = h;
34197  pFile->lastErrno = NO_ERROR;
34198  pFile->pVfs = pVfs;
34199  pFile->pShm = 0;
34200  pFile->zPath = zName;
34201  pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
34202
34203#if SQLITE_OS_WINCE
34204  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
34205       && !winceCreateLock(zName, pFile)
34206  ){
34207    CloseHandle(h);
34208    free(zConverted);
34209    return SQLITE_CANTOPEN_BKPT;
34210  }
34211  if( isTemp ){
34212    pFile->zDeleteOnClose = zConverted;
34213  }else
34214#endif
34215  {
34216    free(zConverted);
34217  }
34218
34219  OpenCounter(+1);
34220  return rc;
34221}
34222
34223/*
34224** Delete the named file.
34225**
34226** Note that windows does not allow a file to be deleted if some other
34227** process has it open.  Sometimes a virus scanner or indexing program
34228** will open a journal file shortly after it is created in order to do
34229** whatever it does.  While this other process is holding the
34230** file open, we will be unable to delete it.  To work around this
34231** problem, we delay 100 milliseconds and try to delete again.  Up
34232** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
34233** up and returning an error.
34234*/
34235static int winDelete(
34236  sqlite3_vfs *pVfs,          /* Not used on win32 */
34237  const char *zFilename,      /* Name of file to delete */
34238  int syncDir                 /* Not used on win32 */
34239){
34240  int cnt = 0;
34241  int rc;
34242  void *zConverted;
34243  UNUSED_PARAMETER(pVfs);
34244  UNUSED_PARAMETER(syncDir);
34245
34246  SimulateIOError(return SQLITE_IOERR_DELETE);
34247  zConverted = convertUtf8Filename(zFilename);
34248  if( zConverted==0 ){
34249    return SQLITE_NOMEM;
34250  }
34251  if( isNT() ){
34252    rc = 1;
34253    while( GetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34254           (rc = DeleteFileW(zConverted))==0 && retryIoerr(&cnt) ){}
34255    rc = rc ? SQLITE_OK : SQLITE_ERROR;
34256/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34257** Since the ASCII version of these Windows API do not exist for WINCE,
34258** it's important to not reference them for WINCE builds.
34259*/
34260#if SQLITE_OS_WINCE==0
34261  }else{
34262    rc = 1;
34263    while( GetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34264           (rc = DeleteFileA(zConverted))==0 && retryIoerr(&cnt) ){}
34265    rc = rc ? SQLITE_OK : SQLITE_ERROR;
34266#endif
34267  }
34268  if( rc ){
34269    rc = winLogError(SQLITE_IOERR_DELETE, "winDelete", zFilename);
34270  }else{
34271    logIoerr(cnt);
34272  }
34273  free(zConverted);
34274  OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
34275  return rc;
34276}
34277
34278/*
34279** Check the existance and status of a file.
34280*/
34281static int winAccess(
34282  sqlite3_vfs *pVfs,         /* Not used on win32 */
34283  const char *zFilename,     /* Name of file to check */
34284  int flags,                 /* Type of test to make on this file */
34285  int *pResOut               /* OUT: Result */
34286){
34287  DWORD attr;
34288  int rc = 0;
34289  void *zConverted;
34290  UNUSED_PARAMETER(pVfs);
34291
34292  SimulateIOError( return SQLITE_IOERR_ACCESS; );
34293  zConverted = convertUtf8Filename(zFilename);
34294  if( zConverted==0 ){
34295    return SQLITE_NOMEM;
34296  }
34297  if( isNT() ){
34298    int cnt = 0;
34299    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34300    memset(&sAttrData, 0, sizeof(sAttrData));
34301    while( !(rc = GetFileAttributesExW((WCHAR*)zConverted,
34302                             GetFileExInfoStandard,
34303                             &sAttrData)) && retryIoerr(&cnt) ){}
34304    if( rc ){
34305      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
34306      ** as if it does not exist.
34307      */
34308      if(    flags==SQLITE_ACCESS_EXISTS
34309          && sAttrData.nFileSizeHigh==0
34310          && sAttrData.nFileSizeLow==0 ){
34311        attr = INVALID_FILE_ATTRIBUTES;
34312      }else{
34313        attr = sAttrData.dwFileAttributes;
34314      }
34315    }else{
34316      logIoerr(cnt);
34317      if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
34318        winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
34319        free(zConverted);
34320        return SQLITE_IOERR_ACCESS;
34321      }else{
34322        attr = INVALID_FILE_ATTRIBUTES;
34323      }
34324    }
34325/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34326** Since the ASCII version of these Windows API do not exist for WINCE,
34327** it's important to not reference them for WINCE builds.
34328*/
34329#if SQLITE_OS_WINCE==0
34330  }else{
34331    attr = GetFileAttributesA((char*)zConverted);
34332#endif
34333  }
34334  free(zConverted);
34335  switch( flags ){
34336    case SQLITE_ACCESS_READ:
34337    case SQLITE_ACCESS_EXISTS:
34338      rc = attr!=INVALID_FILE_ATTRIBUTES;
34339      break;
34340    case SQLITE_ACCESS_READWRITE:
34341      rc = attr!=INVALID_FILE_ATTRIBUTES &&
34342             (attr & FILE_ATTRIBUTE_READONLY)==0;
34343      break;
34344    default:
34345      assert(!"Invalid flags argument");
34346  }
34347  *pResOut = rc;
34348  return SQLITE_OK;
34349}
34350
34351
34352/*
34353** Turn a relative pathname into a full pathname.  Write the full
34354** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
34355** bytes in size.
34356*/
34357static int winFullPathname(
34358  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
34359  const char *zRelative,        /* Possibly relative input path */
34360  int nFull,                    /* Size of output buffer in bytes */
34361  char *zFull                   /* Output buffer */
34362){
34363
34364#if defined(__CYGWIN__)
34365  SimulateIOError( return SQLITE_ERROR );
34366  UNUSED_PARAMETER(nFull);
34367  cygwin_conv_to_full_win32_path(zRelative, zFull);
34368  return SQLITE_OK;
34369#endif
34370
34371#if SQLITE_OS_WINCE
34372  SimulateIOError( return SQLITE_ERROR );
34373  UNUSED_PARAMETER(nFull);
34374  /* WinCE has no concept of a relative pathname, or so I am told. */
34375  sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
34376  return SQLITE_OK;
34377#endif
34378
34379#if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
34380  int nByte;
34381  void *zConverted;
34382  char *zOut;
34383
34384  /* If this path name begins with "/X:", where "X" is any alphabetic
34385  ** character, discard the initial "/" from the pathname.
34386  */
34387  if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
34388    zRelative++;
34389  }
34390
34391  /* It's odd to simulate an io-error here, but really this is just
34392  ** using the io-error infrastructure to test that SQLite handles this
34393  ** function failing. This function could fail if, for example, the
34394  ** current working directory has been unlinked.
34395  */
34396  SimulateIOError( return SQLITE_ERROR );
34397  UNUSED_PARAMETER(nFull);
34398  zConverted = convertUtf8Filename(zRelative);
34399  if( isNT() ){
34400    WCHAR *zTemp;
34401    nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
34402    zTemp = malloc( nByte*sizeof(zTemp[0]) );
34403    if( zTemp==0 ){
34404      free(zConverted);
34405      return SQLITE_NOMEM;
34406    }
34407    GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
34408    free(zConverted);
34409    zOut = unicodeToUtf8(zTemp);
34410    free(zTemp);
34411/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34412** Since the ASCII version of these Windows API do not exist for WINCE,
34413** it's important to not reference them for WINCE builds.
34414*/
34415#if SQLITE_OS_WINCE==0
34416  }else{
34417    char *zTemp;
34418    nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
34419    zTemp = malloc( nByte*sizeof(zTemp[0]) );
34420    if( zTemp==0 ){
34421      free(zConverted);
34422      return SQLITE_NOMEM;
34423    }
34424    GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
34425    free(zConverted);
34426    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
34427    free(zTemp);
34428#endif
34429  }
34430  if( zOut ){
34431    sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
34432    free(zOut);
34433    return SQLITE_OK;
34434  }else{
34435    return SQLITE_NOMEM;
34436  }
34437#endif
34438}
34439
34440/*
34441** Get the sector size of the device used to store
34442** file.
34443*/
34444static int getSectorSize(
34445    sqlite3_vfs *pVfs,
34446    const char *zRelative     /* UTF-8 file name */
34447){
34448  DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34449  /* GetDiskFreeSpace is not supported under WINCE */
34450#if SQLITE_OS_WINCE
34451  UNUSED_PARAMETER(pVfs);
34452  UNUSED_PARAMETER(zRelative);
34453#else
34454  char zFullpath[MAX_PATH+1];
34455  int rc;
34456  DWORD dwRet = 0;
34457  DWORD dwDummy;
34458
34459  /*
34460  ** We need to get the full path name of the file
34461  ** to get the drive letter to look up the sector
34462  ** size.
34463  */
34464  SimulateIOErrorBenign(1);
34465  rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
34466  SimulateIOErrorBenign(0);
34467  if( rc == SQLITE_OK )
34468  {
34469    void *zConverted = convertUtf8Filename(zFullpath);
34470    if( zConverted ){
34471      if( isNT() ){
34472        /* trim path to just drive reference */
34473        WCHAR *p = zConverted;
34474        for(;*p;p++){
34475          if( *p == '\\' ){
34476            *p = '\0';
34477            break;
34478          }
34479        }
34480        dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
34481                                  &dwDummy,
34482                                  &bytesPerSector,
34483                                  &dwDummy,
34484                                  &dwDummy);
34485      }else{
34486        /* trim path to just drive reference */
34487        char *p = (char *)zConverted;
34488        for(;*p;p++){
34489          if( *p == '\\' ){
34490            *p = '\0';
34491            break;
34492          }
34493        }
34494        dwRet = GetDiskFreeSpaceA((char*)zConverted,
34495                                  &dwDummy,
34496                                  &bytesPerSector,
34497                                  &dwDummy,
34498                                  &dwDummy);
34499      }
34500      free(zConverted);
34501    }
34502    if( !dwRet ){
34503      bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34504    }
34505  }
34506#endif
34507  return (int) bytesPerSector;
34508}
34509
34510#ifndef SQLITE_OMIT_LOAD_EXTENSION
34511/*
34512** Interfaces for opening a shared library, finding entry points
34513** within the shared library, and closing the shared library.
34514*/
34515/*
34516** Interfaces for opening a shared library, finding entry points
34517** within the shared library, and closing the shared library.
34518*/
34519static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
34520  HANDLE h;
34521  void *zConverted = convertUtf8Filename(zFilename);
34522  UNUSED_PARAMETER(pVfs);
34523  if( zConverted==0 ){
34524    return 0;
34525  }
34526  if( isNT() ){
34527    h = LoadLibraryW((WCHAR*)zConverted);
34528/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34529** Since the ASCII version of these Windows API do not exist for WINCE,
34530** it's important to not reference them for WINCE builds.
34531*/
34532#if SQLITE_OS_WINCE==0
34533  }else{
34534    h = LoadLibraryA((char*)zConverted);
34535#endif
34536  }
34537  free(zConverted);
34538  return (void*)h;
34539}
34540static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
34541  UNUSED_PARAMETER(pVfs);
34542  getLastErrorMsg(nBuf, zBufOut);
34543}
34544void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34545  UNUSED_PARAMETER(pVfs);
34546#if SQLITE_OS_WINCE
34547  /* The GetProcAddressA() routine is only available on wince. */
34548  return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34549#else
34550  /* All other windows platforms expect GetProcAddress() to take
34551  ** an Ansi string regardless of the _UNICODE setting */
34552  return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34553#endif
34554}
34555void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34556  UNUSED_PARAMETER(pVfs);
34557  FreeLibrary((HANDLE)pHandle);
34558}
34559#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
34560  #define winDlOpen  0
34561  #define winDlError 0
34562  #define winDlSym   0
34563  #define winDlClose 0
34564#endif
34565
34566
34567/*
34568** Write up to nBuf bytes of randomness into zBuf.
34569*/
34570static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34571  int n = 0;
34572  UNUSED_PARAMETER(pVfs);
34573#if defined(SQLITE_TEST)
34574  n = nBuf;
34575  memset(zBuf, 0, nBuf);
34576#else
34577  if( sizeof(SYSTEMTIME)<=nBuf-n ){
34578    SYSTEMTIME x;
34579    GetSystemTime(&x);
34580    memcpy(&zBuf[n], &x, sizeof(x));
34581    n += sizeof(x);
34582  }
34583  if( sizeof(DWORD)<=nBuf-n ){
34584    DWORD pid = GetCurrentProcessId();
34585    memcpy(&zBuf[n], &pid, sizeof(pid));
34586    n += sizeof(pid);
34587  }
34588  if( sizeof(DWORD)<=nBuf-n ){
34589    DWORD cnt = GetTickCount();
34590    memcpy(&zBuf[n], &cnt, sizeof(cnt));
34591    n += sizeof(cnt);
34592  }
34593  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
34594    LARGE_INTEGER i;
34595    QueryPerformanceCounter(&i);
34596    memcpy(&zBuf[n], &i, sizeof(i));
34597    n += sizeof(i);
34598  }
34599#endif
34600  return n;
34601}
34602
34603
34604/*
34605** Sleep for a little while.  Return the amount of time slept.
34606*/
34607static int winSleep(sqlite3_vfs *pVfs, int microsec){
34608  Sleep((microsec+999)/1000);
34609  UNUSED_PARAMETER(pVfs);
34610  return ((microsec+999)/1000)*1000;
34611}
34612
34613/*
34614** The following variable, if set to a non-zero value, is interpreted as
34615** the number of seconds since 1970 and is used to set the result of
34616** sqlite3OsCurrentTime() during testing.
34617*/
34618#ifdef SQLITE_TEST
34619SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
34620#endif
34621
34622/*
34623** Find the current time (in Universal Coordinated Time).  Write into *piNow
34624** the current time and date as a Julian Day number times 86_400_000.  In
34625** other words, write into *piNow the number of milliseconds since the Julian
34626** epoch of noon in Greenwich on November 24, 4714 B.C according to the
34627** proleptic Gregorian calendar.
34628**
34629** On success, return 0.  Return 1 if the time and date cannot be found.
34630*/
34631static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
34632  /* FILETIME structure is a 64-bit value representing the number of
34633     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
34634  */
34635  FILETIME ft;
34636  static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
34637#ifdef SQLITE_TEST
34638  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
34639#endif
34640  /* 2^32 - to avoid use of LL and warnings in gcc */
34641  static const sqlite3_int64 max32BitValue =
34642      (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
34643
34644#if SQLITE_OS_WINCE
34645  SYSTEMTIME time;
34646  GetSystemTime(&time);
34647  /* if SystemTimeToFileTime() fails, it returns zero. */
34648  if (!SystemTimeToFileTime(&time,&ft)){
34649    return 1;
34650  }
34651#else
34652  GetSystemTimeAsFileTime( &ft );
34653#endif
34654
34655  *piNow = winFiletimeEpoch +
34656            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
34657               (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
34658
34659#ifdef SQLITE_TEST
34660  if( sqlite3_current_time ){
34661    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
34662  }
34663#endif
34664  UNUSED_PARAMETER(pVfs);
34665  return 0;
34666}
34667
34668/*
34669** Find the current time (in Universal Coordinated Time).  Write the
34670** current time and date as a Julian Day number into *prNow and
34671** return 0.  Return 1 if the time and date cannot be found.
34672*/
34673int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
34674  int rc;
34675  sqlite3_int64 i;
34676  rc = winCurrentTimeInt64(pVfs, &i);
34677  if( !rc ){
34678    *prNow = i/86400000.0;
34679  }
34680  return rc;
34681}
34682
34683/*
34684** The idea is that this function works like a combination of
34685** GetLastError() and FormatMessage() on windows (or errno and
34686** strerror_r() on unix). After an error is returned by an OS
34687** function, SQLite calls this function with zBuf pointing to
34688** a buffer of nBuf bytes. The OS layer should populate the
34689** buffer with a nul-terminated UTF-8 encoded error message
34690** describing the last IO error to have occurred within the calling
34691** thread.
34692**
34693** If the error message is too large for the supplied buffer,
34694** it should be truncated. The return value of xGetLastError
34695** is zero if the error message fits in the buffer, or non-zero
34696** otherwise (if the message was truncated). If non-zero is returned,
34697** then it is not necessary to include the nul-terminator character
34698** in the output buffer.
34699**
34700** Not supplying an error message will have no adverse effect
34701** on SQLite. It is fine to have an implementation that never
34702** returns an error message:
34703**
34704**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34705**     assert(zBuf[0]=='\0');
34706**     return 0;
34707**   }
34708**
34709** However if an error message is supplied, it will be incorporated
34710** by sqlite into the error message available to the user using
34711** sqlite3_errmsg(), possibly making IO errors easier to debug.
34712*/
34713static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34714  UNUSED_PARAMETER(pVfs);
34715  return getLastErrorMsg(nBuf, zBuf);
34716}
34717
34718
34719
34720/*
34721** Initialize and deinitialize the operating system interface.
34722*/
34723SQLITE_API int sqlite3_os_init(void){
34724  static sqlite3_vfs winVfs = {
34725    3,                   /* iVersion */
34726    sizeof(winFile),     /* szOsFile */
34727    MAX_PATH,            /* mxPathname */
34728    0,                   /* pNext */
34729    "win32",             /* zName */
34730    0,                   /* pAppData */
34731    winOpen,             /* xOpen */
34732    winDelete,           /* xDelete */
34733    winAccess,           /* xAccess */
34734    winFullPathname,     /* xFullPathname */
34735    winDlOpen,           /* xDlOpen */
34736    winDlError,          /* xDlError */
34737    winDlSym,            /* xDlSym */
34738    winDlClose,          /* xDlClose */
34739    winRandomness,       /* xRandomness */
34740    winSleep,            /* xSleep */
34741    winCurrentTime,      /* xCurrentTime */
34742    winGetLastError,     /* xGetLastError */
34743    winCurrentTimeInt64, /* xCurrentTimeInt64 */
34744    0,                   /* xSetSystemCall */
34745    0,                   /* xGetSystemCall */
34746    0,                   /* xNextSystemCall */
34747  };
34748
34749#ifndef SQLITE_OMIT_WAL
34750  /* get memory map allocation granularity */
34751  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
34752  GetSystemInfo(&winSysInfo);
34753  assert(winSysInfo.dwAllocationGranularity > 0);
34754#endif
34755
34756  sqlite3_vfs_register(&winVfs, 1);
34757  return SQLITE_OK;
34758}
34759SQLITE_API int sqlite3_os_end(void){
34760  return SQLITE_OK;
34761}
34762
34763#endif /* SQLITE_OS_WIN */
34764
34765/************** End of os_win.c **********************************************/
34766/************** Begin file bitvec.c ******************************************/
34767/*
34768** 2008 February 16
34769**
34770** The author disclaims copyright to this source code.  In place of
34771** a legal notice, here is a blessing:
34772**
34773**    May you do good and not evil.
34774**    May you find forgiveness for yourself and forgive others.
34775**    May you share freely, never taking more than you give.
34776**
34777*************************************************************************
34778** This file implements an object that represents a fixed-length
34779** bitmap.  Bits are numbered starting with 1.
34780**
34781** A bitmap is used to record which pages of a database file have been
34782** journalled during a transaction, or which pages have the "dont-write"
34783** property.  Usually only a few pages are meet either condition.
34784** So the bitmap is usually sparse and has low cardinality.
34785** But sometimes (for example when during a DROP of a large table) most
34786** or all of the pages in a database can get journalled.  In those cases,
34787** the bitmap becomes dense with high cardinality.  The algorithm needs
34788** to handle both cases well.
34789**
34790** The size of the bitmap is fixed when the object is created.
34791**
34792** All bits are clear when the bitmap is created.  Individual bits
34793** may be set or cleared one at a time.
34794**
34795** Test operations are about 100 times more common that set operations.
34796** Clear operations are exceedingly rare.  There are usually between
34797** 5 and 500 set operations per Bitvec object, though the number of sets can
34798** sometimes grow into tens of thousands or larger.  The size of the
34799** Bitvec object is the number of pages in the database file at the
34800** start of a transaction, and is thus usually less than a few thousand,
34801** but can be as large as 2 billion for a really big database.
34802*/
34803
34804/* Size of the Bitvec structure in bytes. */
34805#define BITVEC_SZ        512
34806
34807/* Round the union size down to the nearest pointer boundary, since that's how
34808** it will be aligned within the Bitvec struct. */
34809#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
34810
34811/* Type of the array "element" for the bitmap representation.
34812** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
34813** Setting this to the "natural word" size of your CPU may improve
34814** performance. */
34815#define BITVEC_TELEM     u8
34816/* Size, in bits, of the bitmap element. */
34817#define BITVEC_SZELEM    8
34818/* Number of elements in a bitmap array. */
34819#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
34820/* Number of bits in the bitmap array. */
34821#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
34822
34823/* Number of u32 values in hash table. */
34824#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
34825/* Maximum number of entries in hash table before
34826** sub-dividing and re-hashing. */
34827#define BITVEC_MXHASH    (BITVEC_NINT/2)
34828/* Hashing function for the aHash representation.
34829** Empirical testing showed that the *37 multiplier
34830** (an arbitrary prime)in the hash function provided
34831** no fewer collisions than the no-op *1. */
34832#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
34833
34834#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
34835
34836
34837/*
34838** A bitmap is an instance of the following structure.
34839**
34840** This bitmap records the existance of zero or more bits
34841** with values between 1 and iSize, inclusive.
34842**
34843** There are three possible representations of the bitmap.
34844** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
34845** bitmap.  The least significant bit is bit 1.
34846**
34847** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
34848** a hash table that will hold up to BITVEC_MXHASH distinct values.
34849**
34850** Otherwise, the value i is redirected into one of BITVEC_NPTR
34851** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
34852** handles up to iDivisor separate values of i.  apSub[0] holds
34853** values between 1 and iDivisor.  apSub[1] holds values between
34854** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
34855** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
34856** to hold deal with values between 1 and iDivisor.
34857*/
34858struct Bitvec {
34859  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
34860  u32 nSet;       /* Number of bits that are set - only valid for aHash
34861                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
34862                  ** this would be 125. */
34863  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
34864                  /* Should >=0 for apSub element. */
34865                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
34866                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
34867  union {
34868    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
34869    u32 aHash[BITVEC_NINT];      /* Hash table representation */
34870    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
34871  } u;
34872};
34873
34874/*
34875** Create a new bitmap object able to handle bits between 0 and iSize,
34876** inclusive.  Return a pointer to the new object.  Return NULL if
34877** malloc fails.
34878*/
34879SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
34880  Bitvec *p;
34881  assert( sizeof(*p)==BITVEC_SZ );
34882  p = sqlite3MallocZero( sizeof(*p) );
34883  if( p ){
34884    p->iSize = iSize;
34885  }
34886  return p;
34887}
34888
34889/*
34890** Check to see if the i-th bit is set.  Return true or false.
34891** If p is NULL (if the bitmap has not been created) or if
34892** i is out of range, then return false.
34893*/
34894SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
34895  if( p==0 ) return 0;
34896  if( i>p->iSize || i==0 ) return 0;
34897  i--;
34898  while( p->iDivisor ){
34899    u32 bin = i/p->iDivisor;
34900    i = i%p->iDivisor;
34901    p = p->u.apSub[bin];
34902    if (!p) {
34903      return 0;
34904    }
34905  }
34906  if( p->iSize<=BITVEC_NBIT ){
34907    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
34908  } else{
34909    u32 h = BITVEC_HASH(i++);
34910    while( p->u.aHash[h] ){
34911      if( p->u.aHash[h]==i ) return 1;
34912      h = (h+1) % BITVEC_NINT;
34913    }
34914    return 0;
34915  }
34916}
34917
34918/*
34919** Set the i-th bit.  Return 0 on success and an error code if
34920** anything goes wrong.
34921**
34922** This routine might cause sub-bitmaps to be allocated.  Failing
34923** to get the memory needed to hold the sub-bitmap is the only
34924** that can go wrong with an insert, assuming p and i are valid.
34925**
34926** The calling function must ensure that p is a valid Bitvec object
34927** and that the value for "i" is within range of the Bitvec object.
34928** Otherwise the behavior is undefined.
34929*/
34930SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
34931  u32 h;
34932  if( p==0 ) return SQLITE_OK;
34933  assert( i>0 );
34934  assert( i<=p->iSize );
34935  i--;
34936  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
34937    u32 bin = i/p->iDivisor;
34938    i = i%p->iDivisor;
34939    if( p->u.apSub[bin]==0 ){
34940      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
34941      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
34942    }
34943    p = p->u.apSub[bin];
34944  }
34945  if( p->iSize<=BITVEC_NBIT ){
34946    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
34947    return SQLITE_OK;
34948  }
34949  h = BITVEC_HASH(i++);
34950  /* if there wasn't a hash collision, and this doesn't */
34951  /* completely fill the hash, then just add it without */
34952  /* worring about sub-dividing and re-hashing. */
34953  if( !p->u.aHash[h] ){
34954    if (p->nSet<(BITVEC_NINT-1)) {
34955      goto bitvec_set_end;
34956    } else {
34957      goto bitvec_set_rehash;
34958    }
34959  }
34960  /* there was a collision, check to see if it's already */
34961  /* in hash, if not, try to find a spot for it */
34962  do {
34963    if( p->u.aHash[h]==i ) return SQLITE_OK;
34964    h++;
34965    if( h>=BITVEC_NINT ) h = 0;
34966  } while( p->u.aHash[h] );
34967  /* we didn't find it in the hash.  h points to the first */
34968  /* available free spot. check to see if this is going to */
34969  /* make our hash too "full".  */
34970bitvec_set_rehash:
34971  if( p->nSet>=BITVEC_MXHASH ){
34972    unsigned int j;
34973    int rc;
34974    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
34975    if( aiValues==0 ){
34976      return SQLITE_NOMEM;
34977    }else{
34978      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
34979      memset(p->u.apSub, 0, sizeof(p->u.apSub));
34980      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
34981      rc = sqlite3BitvecSet(p, i);
34982      for(j=0; j<BITVEC_NINT; j++){
34983        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
34984      }
34985      sqlite3StackFree(0, aiValues);
34986      return rc;
34987    }
34988  }
34989bitvec_set_end:
34990  p->nSet++;
34991  p->u.aHash[h] = i;
34992  return SQLITE_OK;
34993}
34994
34995/*
34996** Clear the i-th bit.
34997**
34998** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
34999** that BitvecClear can use to rebuilt its hash table.
35000*/
35001SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
35002  if( p==0 ) return;
35003  assert( i>0 );
35004  i--;
35005  while( p->iDivisor ){
35006    u32 bin = i/p->iDivisor;
35007    i = i%p->iDivisor;
35008    p = p->u.apSub[bin];
35009    if (!p) {
35010      return;
35011    }
35012  }
35013  if( p->iSize<=BITVEC_NBIT ){
35014    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
35015  }else{
35016    unsigned int j;
35017    u32 *aiValues = pBuf;
35018    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
35019    memset(p->u.aHash, 0, sizeof(p->u.aHash));
35020    p->nSet = 0;
35021    for(j=0; j<BITVEC_NINT; j++){
35022      if( aiValues[j] && aiValues[j]!=(i+1) ){
35023        u32 h = BITVEC_HASH(aiValues[j]-1);
35024        p->nSet++;
35025        while( p->u.aHash[h] ){
35026          h++;
35027          if( h>=BITVEC_NINT ) h = 0;
35028        }
35029        p->u.aHash[h] = aiValues[j];
35030      }
35031    }
35032  }
35033}
35034
35035/*
35036** Destroy a bitmap object.  Reclaim all memory used.
35037*/
35038SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
35039  if( p==0 ) return;
35040  if( p->iDivisor ){
35041    unsigned int i;
35042    for(i=0; i<BITVEC_NPTR; i++){
35043      sqlite3BitvecDestroy(p->u.apSub[i]);
35044    }
35045  }
35046  sqlite3_free(p);
35047}
35048
35049/*
35050** Return the value of the iSize parameter specified when Bitvec *p
35051** was created.
35052*/
35053SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
35054  return p->iSize;
35055}
35056
35057#ifndef SQLITE_OMIT_BUILTIN_TEST
35058/*
35059** Let V[] be an array of unsigned characters sufficient to hold
35060** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
35061** Then the following macros can be used to set, clear, or test
35062** individual bits within V.
35063*/
35064#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
35065#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
35066#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
35067
35068/*
35069** This routine runs an extensive test of the Bitvec code.
35070**
35071** The input is an array of integers that acts as a program
35072** to test the Bitvec.  The integers are opcodes followed
35073** by 0, 1, or 3 operands, depending on the opcode.  Another
35074** opcode follows immediately after the last operand.
35075**
35076** There are 6 opcodes numbered from 0 through 5.  0 is the
35077** "halt" opcode and causes the test to end.
35078**
35079**    0          Halt and return the number of errors
35080**    1 N S X    Set N bits beginning with S and incrementing by X
35081**    2 N S X    Clear N bits beginning with S and incrementing by X
35082**    3 N        Set N randomly chosen bits
35083**    4 N        Clear N randomly chosen bits
35084**    5 N S X    Set N bits from S increment X in array only, not in bitvec
35085**
35086** The opcodes 1 through 4 perform set and clear operations are performed
35087** on both a Bitvec object and on a linear array of bits obtained from malloc.
35088** Opcode 5 works on the linear array only, not on the Bitvec.
35089** Opcode 5 is used to deliberately induce a fault in order to
35090** confirm that error detection works.
35091**
35092** At the conclusion of the test the linear array is compared
35093** against the Bitvec object.  If there are any differences,
35094** an error is returned.  If they are the same, zero is returned.
35095**
35096** If a memory allocation error occurs, return -1.
35097*/
35098SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
35099  Bitvec *pBitvec = 0;
35100  unsigned char *pV = 0;
35101  int rc = -1;
35102  int i, nx, pc, op;
35103  void *pTmpSpace;
35104
35105  /* Allocate the Bitvec to be tested and a linear array of
35106  ** bits to act as the reference */
35107  pBitvec = sqlite3BitvecCreate( sz );
35108  pV = sqlite3_malloc( (sz+7)/8 + 1 );
35109  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
35110  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
35111  memset(pV, 0, (sz+7)/8 + 1);
35112
35113  /* NULL pBitvec tests */
35114  sqlite3BitvecSet(0, 1);
35115  sqlite3BitvecClear(0, 1, pTmpSpace);
35116
35117  /* Run the program */
35118  pc = 0;
35119  while( (op = aOp[pc])!=0 ){
35120    switch( op ){
35121      case 1:
35122      case 2:
35123      case 5: {
35124        nx = 4;
35125        i = aOp[pc+2] - 1;
35126        aOp[pc+2] += aOp[pc+3];
35127        break;
35128      }
35129      case 3:
35130      case 4:
35131      default: {
35132        nx = 2;
35133        sqlite3_randomness(sizeof(i), &i);
35134        break;
35135      }
35136    }
35137    if( (--aOp[pc+1]) > 0 ) nx = 0;
35138    pc += nx;
35139    i = (i & 0x7fffffff)%sz;
35140    if( (op & 1)!=0 ){
35141      SETBIT(pV, (i+1));
35142      if( op!=5 ){
35143        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
35144      }
35145    }else{
35146      CLEARBIT(pV, (i+1));
35147      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
35148    }
35149  }
35150
35151  /* Test to make sure the linear array exactly matches the
35152  ** Bitvec object.  Start with the assumption that they do
35153  ** match (rc==0).  Change rc to non-zero if a discrepancy
35154  ** is found.
35155  */
35156  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
35157          + sqlite3BitvecTest(pBitvec, 0)
35158          + (sqlite3BitvecSize(pBitvec) - sz);
35159  for(i=1; i<=sz; i++){
35160    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
35161      rc = i;
35162      break;
35163    }
35164  }
35165
35166  /* Free allocated structure */
35167bitvec_end:
35168  sqlite3_free(pTmpSpace);
35169  sqlite3_free(pV);
35170  sqlite3BitvecDestroy(pBitvec);
35171  return rc;
35172}
35173#endif /* SQLITE_OMIT_BUILTIN_TEST */
35174
35175/************** End of bitvec.c **********************************************/
35176/************** Begin file pcache.c ******************************************/
35177/*
35178** 2008 August 05
35179**
35180** The author disclaims copyright to this source code.  In place of
35181** a legal notice, here is a blessing:
35182**
35183**    May you do good and not evil.
35184**    May you find forgiveness for yourself and forgive others.
35185**    May you share freely, never taking more than you give.
35186**
35187*************************************************************************
35188** This file implements that page cache.
35189*/
35190
35191/*
35192** A complete page cache is an instance of this structure.
35193*/
35194struct PCache {
35195  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
35196  PgHdr *pSynced;                     /* Last synced page in dirty page list */
35197  int nRef;                           /* Number of referenced pages */
35198  int nMax;                           /* Configured cache size */
35199  int szPage;                         /* Size of every page in this cache */
35200  int szExtra;                        /* Size of extra space for each page */
35201  int bPurgeable;                     /* True if pages are on backing store */
35202  int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
35203  void *pStress;                      /* Argument to xStress */
35204  sqlite3_pcache *pCache;             /* Pluggable cache module */
35205  PgHdr *pPage1;                      /* Reference to page 1 */
35206};
35207
35208/*
35209** Some of the assert() macros in this code are too expensive to run
35210** even during normal debugging.  Use them only rarely on long-running
35211** tests.  Enable the expensive asserts using the
35212** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
35213*/
35214#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
35215# define expensive_assert(X)  assert(X)
35216#else
35217# define expensive_assert(X)
35218#endif
35219
35220/********************************** Linked List Management ********************/
35221
35222#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
35223/*
35224** Check that the pCache->pSynced variable is set correctly. If it
35225** is not, either fail an assert or return zero. Otherwise, return
35226** non-zero. This is only used in debugging builds, as follows:
35227**
35228**   expensive_assert( pcacheCheckSynced(pCache) );
35229*/
35230static int pcacheCheckSynced(PCache *pCache){
35231  PgHdr *p;
35232  for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
35233    assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
35234  }
35235  return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
35236}
35237#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
35238
35239/*
35240** Remove page pPage from the list of dirty pages.
35241*/
35242static void pcacheRemoveFromDirtyList(PgHdr *pPage){
35243  PCache *p = pPage->pCache;
35244
35245  assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
35246  assert( pPage->pDirtyPrev || pPage==p->pDirty );
35247
35248  /* Update the PCache1.pSynced variable if necessary. */
35249  if( p->pSynced==pPage ){
35250    PgHdr *pSynced = pPage->pDirtyPrev;
35251    while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
35252      pSynced = pSynced->pDirtyPrev;
35253    }
35254    p->pSynced = pSynced;
35255  }
35256
35257  if( pPage->pDirtyNext ){
35258    pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
35259  }else{
35260    assert( pPage==p->pDirtyTail );
35261    p->pDirtyTail = pPage->pDirtyPrev;
35262  }
35263  if( pPage->pDirtyPrev ){
35264    pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
35265  }else{
35266    assert( pPage==p->pDirty );
35267    p->pDirty = pPage->pDirtyNext;
35268  }
35269  pPage->pDirtyNext = 0;
35270  pPage->pDirtyPrev = 0;
35271
35272  expensive_assert( pcacheCheckSynced(p) );
35273}
35274
35275/*
35276** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
35277** pPage).
35278*/
35279static void pcacheAddToDirtyList(PgHdr *pPage){
35280  PCache *p = pPage->pCache;
35281
35282  assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
35283
35284  pPage->pDirtyNext = p->pDirty;
35285  if( pPage->pDirtyNext ){
35286    assert( pPage->pDirtyNext->pDirtyPrev==0 );
35287    pPage->pDirtyNext->pDirtyPrev = pPage;
35288  }
35289  p->pDirty = pPage;
35290  if( !p->pDirtyTail ){
35291    p->pDirtyTail = pPage;
35292  }
35293  if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
35294    p->pSynced = pPage;
35295  }
35296  expensive_assert( pcacheCheckSynced(p) );
35297}
35298
35299/*
35300** Wrapper around the pluggable caches xUnpin method. If the cache is
35301** being used for an in-memory database, this function is a no-op.
35302*/
35303static void pcacheUnpin(PgHdr *p){
35304  PCache *pCache = p->pCache;
35305  if( pCache->bPurgeable ){
35306    if( p->pgno==1 ){
35307      pCache->pPage1 = 0;
35308    }
35309    sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
35310  }
35311}
35312
35313/*************************************************** General Interfaces ******
35314**
35315** Initialize and shutdown the page cache subsystem. Neither of these
35316** functions are threadsafe.
35317*/
35318SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
35319  if( sqlite3GlobalConfig.pcache.xInit==0 ){
35320    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
35321    ** built-in default page cache is used instead of the application defined
35322    ** page cache. */
35323    sqlite3PCacheSetDefault();
35324  }
35325  return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
35326}
35327SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
35328  if( sqlite3GlobalConfig.pcache.xShutdown ){
35329    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
35330    sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
35331  }
35332}
35333
35334/*
35335** Return the size in bytes of a PCache object.
35336*/
35337SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
35338
35339/*
35340** Create a new PCache object. Storage space to hold the object
35341** has already been allocated and is passed in as the p pointer.
35342** The caller discovers how much space needs to be allocated by
35343** calling sqlite3PcacheSize().
35344*/
35345SQLITE_PRIVATE void sqlite3PcacheOpen(
35346  int szPage,                  /* Size of every page */
35347  int szExtra,                 /* Extra space associated with each page */
35348  int bPurgeable,              /* True if pages are on backing store */
35349  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
35350  void *pStress,               /* Argument to xStress */
35351  PCache *p                    /* Preallocated space for the PCache */
35352){
35353  memset(p, 0, sizeof(PCache));
35354  p->szPage = szPage;
35355  p->szExtra = szExtra;
35356  p->bPurgeable = bPurgeable;
35357  p->xStress = xStress;
35358  p->pStress = pStress;
35359  p->nMax = 100;
35360}
35361
35362/*
35363** Change the page size for PCache object. The caller must ensure that there
35364** are no outstanding page references when this function is called.
35365*/
35366SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
35367  assert( pCache->nRef==0 && pCache->pDirty==0 );
35368  if( pCache->pCache ){
35369    sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
35370    pCache->pCache = 0;
35371    pCache->pPage1 = 0;
35372  }
35373  pCache->szPage = szPage;
35374}
35375
35376/*
35377** Try to obtain a page from the cache.
35378*/
35379SQLITE_PRIVATE int sqlite3PcacheFetch(
35380  PCache *pCache,       /* Obtain the page from this cache */
35381  Pgno pgno,            /* Page number to obtain */
35382  int createFlag,       /* If true, create page if it does not exist already */
35383  PgHdr **ppPage        /* Write the page here */
35384){
35385  PgHdr *pPage = 0;
35386  int eCreate;
35387
35388  assert( pCache!=0 );
35389  assert( createFlag==1 || createFlag==0 );
35390  assert( pgno>0 );
35391
35392  /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
35393  ** allocate it now.
35394  */
35395  if( !pCache->pCache && createFlag ){
35396    sqlite3_pcache *p;
35397    int nByte;
35398    nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
35399    p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
35400    if( !p ){
35401      return SQLITE_NOMEM;
35402    }
35403    sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
35404    pCache->pCache = p;
35405  }
35406
35407  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
35408  if( pCache->pCache ){
35409    pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
35410  }
35411
35412  if( !pPage && eCreate==1 ){
35413    PgHdr *pPg;
35414
35415    /* Find a dirty page to write-out and recycle. First try to find a
35416    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
35417    ** cleared), but if that is not possible settle for any other
35418    ** unreferenced dirty page.
35419    */
35420    expensive_assert( pcacheCheckSynced(pCache) );
35421    for(pPg=pCache->pSynced;
35422        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
35423        pPg=pPg->pDirtyPrev
35424    );
35425    pCache->pSynced = pPg;
35426    if( !pPg ){
35427      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
35428    }
35429    if( pPg ){
35430      int rc;
35431#ifdef SQLITE_LOG_CACHE_SPILL
35432      sqlite3_log(SQLITE_FULL,
35433                  "spill page %d making room for %d - cache used: %d/%d",
35434                  pPg->pgno, pgno,
35435                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
35436                  pCache->nMax);
35437#endif
35438      rc = pCache->xStress(pCache->pStress, pPg);
35439      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
35440        return rc;
35441      }
35442    }
35443
35444    pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
35445  }
35446
35447  if( pPage ){
35448    if( !pPage->pData ){
35449      memset(pPage, 0, sizeof(PgHdr));
35450      pPage->pData = (void *)&pPage[1];
35451      pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
35452      memset(pPage->pExtra, 0, pCache->szExtra);
35453      pPage->pCache = pCache;
35454      pPage->pgno = pgno;
35455    }
35456    assert( pPage->pCache==pCache );
35457    assert( pPage->pgno==pgno );
35458    assert( pPage->pData==(void *)&pPage[1] );
35459    assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
35460
35461    if( 0==pPage->nRef ){
35462      pCache->nRef++;
35463    }
35464    pPage->nRef++;
35465    if( pgno==1 ){
35466      pCache->pPage1 = pPage;
35467    }
35468  }
35469  *ppPage = pPage;
35470  return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
35471}
35472
35473/*
35474** Decrement the reference count on a page. If the page is clean and the
35475** reference count drops to 0, then it is made elible for recycling.
35476*/
35477SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
35478  assert( p->nRef>0 );
35479  p->nRef--;
35480  if( p->nRef==0 ){
35481    PCache *pCache = p->pCache;
35482    pCache->nRef--;
35483    if( (p->flags&PGHDR_DIRTY)==0 ){
35484      pcacheUnpin(p);
35485    }else{
35486      /* Move the page to the head of the dirty list. */
35487      pcacheRemoveFromDirtyList(p);
35488      pcacheAddToDirtyList(p);
35489    }
35490  }
35491}
35492
35493/*
35494** Increase the reference count of a supplied page by 1.
35495*/
35496SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
35497  assert(p->nRef>0);
35498  p->nRef++;
35499}
35500
35501/*
35502** Drop a page from the cache. There must be exactly one reference to the
35503** page. This function deletes that reference, so after it returns the
35504** page pointed to by p is invalid.
35505*/
35506SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
35507  PCache *pCache;
35508  assert( p->nRef==1 );
35509  if( p->flags&PGHDR_DIRTY ){
35510    pcacheRemoveFromDirtyList(p);
35511  }
35512  pCache = p->pCache;
35513  pCache->nRef--;
35514  if( p->pgno==1 ){
35515    pCache->pPage1 = 0;
35516  }
35517  sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
35518}
35519
35520/*
35521** Make sure the page is marked as dirty. If it isn't dirty already,
35522** make it so.
35523*/
35524SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
35525  p->flags &= ~PGHDR_DONT_WRITE;
35526  assert( p->nRef>0 );
35527  if( 0==(p->flags & PGHDR_DIRTY) ){
35528    p->flags |= PGHDR_DIRTY;
35529    pcacheAddToDirtyList( p);
35530  }
35531}
35532
35533/*
35534** Make sure the page is marked as clean. If it isn't clean already,
35535** make it so.
35536*/
35537SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
35538  if( (p->flags & PGHDR_DIRTY) ){
35539    pcacheRemoveFromDirtyList(p);
35540    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
35541    if( p->nRef==0 ){
35542      pcacheUnpin(p);
35543    }
35544  }
35545}
35546
35547/*
35548** Make every page in the cache clean.
35549*/
35550SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
35551  PgHdr *p;
35552  while( (p = pCache->pDirty)!=0 ){
35553    sqlite3PcacheMakeClean(p);
35554  }
35555}
35556
35557/*
35558** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
35559*/
35560SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
35561  PgHdr *p;
35562  for(p=pCache->pDirty; p; p=p->pDirtyNext){
35563    p->flags &= ~PGHDR_NEED_SYNC;
35564  }
35565  pCache->pSynced = pCache->pDirtyTail;
35566}
35567
35568/*
35569** Change the page number of page p to newPgno.
35570*/
35571SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
35572  PCache *pCache = p->pCache;
35573  assert( p->nRef>0 );
35574  assert( newPgno>0 );
35575  sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
35576  p->pgno = newPgno;
35577  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
35578    pcacheRemoveFromDirtyList(p);
35579    pcacheAddToDirtyList(p);
35580  }
35581}
35582
35583/*
35584** Drop every cache entry whose page number is greater than "pgno". The
35585** caller must ensure that there are no outstanding references to any pages
35586** other than page 1 with a page number greater than pgno.
35587**
35588** If there is a reference to page 1 and the pgno parameter passed to this
35589** function is 0, then the data area associated with page 1 is zeroed, but
35590** the page object is not dropped.
35591*/
35592SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
35593  if( pCache->pCache ){
35594    PgHdr *p;
35595    PgHdr *pNext;
35596    for(p=pCache->pDirty; p; p=pNext){
35597      pNext = p->pDirtyNext;
35598      /* This routine never gets call with a positive pgno except right
35599      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
35600      ** it must be that pgno==0.
35601      */
35602      assert( p->pgno>0 );
35603      if( ALWAYS(p->pgno>pgno) ){
35604        assert( p->flags&PGHDR_DIRTY );
35605        sqlite3PcacheMakeClean(p);
35606      }
35607    }
35608    if( pgno==0 && pCache->pPage1 ){
35609      memset(pCache->pPage1->pData, 0, pCache->szPage);
35610      pgno = 1;
35611    }
35612    sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
35613  }
35614}
35615
35616/*
35617** Close a cache.
35618*/
35619SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
35620  if( pCache->pCache ){
35621    sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
35622  }
35623}
35624
35625/*
35626** Discard the contents of the cache.
35627*/
35628SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
35629  sqlite3PcacheTruncate(pCache, 0);
35630}
35631
35632/*
35633** Merge two lists of pages connected by pDirty and in pgno order.
35634** Do not both fixing the pDirtyPrev pointers.
35635*/
35636static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
35637  PgHdr result, *pTail;
35638  pTail = &result;
35639  while( pA && pB ){
35640    if( pA->pgno<pB->pgno ){
35641      pTail->pDirty = pA;
35642      pTail = pA;
35643      pA = pA->pDirty;
35644    }else{
35645      pTail->pDirty = pB;
35646      pTail = pB;
35647      pB = pB->pDirty;
35648    }
35649  }
35650  if( pA ){
35651    pTail->pDirty = pA;
35652  }else if( pB ){
35653    pTail->pDirty = pB;
35654  }else{
35655    pTail->pDirty = 0;
35656  }
35657  return result.pDirty;
35658}
35659
35660/*
35661** Sort the list of pages in accending order by pgno.  Pages are
35662** connected by pDirty pointers.  The pDirtyPrev pointers are
35663** corrupted by this sort.
35664**
35665** Since there cannot be more than 2^31 distinct pages in a database,
35666** there cannot be more than 31 buckets required by the merge sorter.
35667** One extra bucket is added to catch overflow in case something
35668** ever changes to make the previous sentence incorrect.
35669*/
35670#define N_SORT_BUCKET  32
35671static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
35672  PgHdr *a[N_SORT_BUCKET], *p;
35673  int i;
35674  memset(a, 0, sizeof(a));
35675  while( pIn ){
35676    p = pIn;
35677    pIn = p->pDirty;
35678    p->pDirty = 0;
35679    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
35680      if( a[i]==0 ){
35681        a[i] = p;
35682        break;
35683      }else{
35684        p = pcacheMergeDirtyList(a[i], p);
35685        a[i] = 0;
35686      }
35687    }
35688    if( NEVER(i==N_SORT_BUCKET-1) ){
35689      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
35690      ** the input list.  But that is impossible.
35691      */
35692      a[i] = pcacheMergeDirtyList(a[i], p);
35693    }
35694  }
35695  p = a[0];
35696  for(i=1; i<N_SORT_BUCKET; i++){
35697    p = pcacheMergeDirtyList(p, a[i]);
35698  }
35699  return p;
35700}
35701
35702/*
35703** Return a list of all dirty pages in the cache, sorted by page number.
35704*/
35705SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
35706  PgHdr *p;
35707  for(p=pCache->pDirty; p; p=p->pDirtyNext){
35708    p->pDirty = p->pDirtyNext;
35709  }
35710  return pcacheSortDirtyList(pCache->pDirty);
35711}
35712
35713/*
35714** Return the total number of referenced pages held by the cache.
35715*/
35716SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
35717  return pCache->nRef;
35718}
35719
35720/*
35721** Return the number of references to the page supplied as an argument.
35722*/
35723SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
35724  return p->nRef;
35725}
35726
35727/*
35728** Return the total number of pages in the cache.
35729*/
35730SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
35731  int nPage = 0;
35732  if( pCache->pCache ){
35733    nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
35734  }
35735  return nPage;
35736}
35737
35738#ifdef SQLITE_TEST
35739/*
35740** Get the suggested cache-size value.
35741*/
35742SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
35743  return pCache->nMax;
35744}
35745#endif
35746
35747/*
35748** Set the suggested cache-size value.
35749*/
35750SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
35751  pCache->nMax = mxPage;
35752  if( pCache->pCache ){
35753    sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
35754  }
35755}
35756
35757#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
35758/*
35759** For all dirty pages currently in the cache, invoke the specified
35760** callback. This is only used if the SQLITE_CHECK_PAGES macro is
35761** defined.
35762*/
35763SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
35764  PgHdr *pDirty;
35765  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
35766    xIter(pDirty);
35767  }
35768}
35769#endif
35770
35771/************** End of pcache.c **********************************************/
35772/************** Begin file pcache1.c *****************************************/
35773/*
35774** 2008 November 05
35775**
35776** The author disclaims copyright to this source code.  In place of
35777** a legal notice, here is a blessing:
35778**
35779**    May you do good and not evil.
35780**    May you find forgiveness for yourself and forgive others.
35781**    May you share freely, never taking more than you give.
35782**
35783*************************************************************************
35784**
35785** This file implements the default page cache implementation (the
35786** sqlite3_pcache interface). It also contains part of the implementation
35787** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
35788** If the default page cache implementation is overriden, then neither of
35789** these two features are available.
35790*/
35791
35792
35793typedef struct PCache1 PCache1;
35794typedef struct PgHdr1 PgHdr1;
35795typedef struct PgFreeslot PgFreeslot;
35796typedef struct PGroup PGroup;
35797
35798typedef struct PGroupBlock PGroupBlock;
35799typedef struct PGroupBlockList PGroupBlockList;
35800
35801/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
35802** of one or more PCaches that are able to recycle each others unpinned
35803** pages when they are under memory pressure.  A PGroup is an instance of
35804** the following object.
35805**
35806** This page cache implementation works in one of two modes:
35807**
35808**   (1)  Every PCache is the sole member of its own PGroup.  There is
35809**        one PGroup per PCache.
35810**
35811**   (2)  There is a single global PGroup that all PCaches are a member
35812**        of.
35813**
35814** Mode 1 uses more memory (since PCache instances are not able to rob
35815** unused pages from other PCaches) but it also operates without a mutex,
35816** and is therefore often faster.  Mode 2 requires a mutex in order to be
35817** threadsafe, but is able recycle pages more efficient.
35818**
35819** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
35820** PGroup which is the pcache1.grp global variable and its mutex is
35821** SQLITE_MUTEX_STATIC_LRU.
35822*/
35823struct PGroup {
35824  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
35825  int nMaxPage;                  /* Sum of nMax for purgeable caches */
35826  int nMinPage;                  /* Sum of nMin for purgeable caches */
35827  int mxPinned;                  /* nMaxpage + 10 - nMinPage */
35828  int nCurrentPage;              /* Number of purgeable pages allocated */
35829  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
35830#ifdef SQLITE_PAGECACHE_BLOCKALLOC
35831  int isBusy;                    /* Do not run ReleaseMemory() if true */
35832  PGroupBlockList *pBlockList;   /* List of block-lists for this group */
35833#endif
35834};
35835
35836/*
35837** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35838** each PGroup structure has a linked list of the the following starting
35839** at PGroup.pBlockList. There is one entry for each distinct page-size
35840** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35841** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35842** size requested by each pcache, which is the database page-size plus
35843** the various header structures used by the pcache, pager and btree layers.
35844** Usually around (pgsz+200) bytes.
35845**
35846** This size (pgsz+200) bytes is not allocated efficiently by some
35847** implementations of malloc. In particular, some implementations are only
35848** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35849** integer value. Since the page-size is a power of 2, this means we
35850** end up wasting (pgsz-200) bytes in each allocation.
35851**
35852** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35853** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35854** are requested from malloc allocator. After a block is returned,
35855** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35856** allocations can fit in the space returned by malloc(). This value may
35857** be more than M.
35858**
35859** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35860** contains the number of allocations that will fit in the aData[] space.
35861** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35862** within aData is in use, the corresponding bit in mUsed is set. Thus
35863** when (mUsed+1==(1 << nEntry)) the block is completely full.
35864**
35865** Each time a slot within a block is freed, the block is moved to the start
35866** of the linked-list. And if a block becomes completely full, then it is
35867** moved to the end of the list. As a result, when searching for a free
35868** slot, only the first block in the list need be examined. If it is full,
35869** then it is guaranteed that all blocks are full.
35870*/
35871struct PGroupBlockList {
35872  int nByte;                     /* Size of each allocation in bytes */
35873  PGroupBlock *pFirst;           /* First PGroupBlock in list */
35874  PGroupBlock *pLast;            /* Last PGroupBlock in list */
35875  PGroupBlockList *pNext;        /* Next block-list attached to group */
35876};
35877
35878struct PGroupBlock {
35879  Bitmask mUsed;                 /* Mask of used slots */
35880  int nEntry;                    /* Maximum number of allocations in aData[] */
35881  u8 *aData;                     /* Pointer to data block */
35882  PGroupBlock *pNext;            /* Next PGroupBlock in list */
35883  PGroupBlock *pPrev;            /* Previous PGroupBlock in list */
35884  PGroupBlockList *pList;        /* Owner list */
35885};
35886
35887/* Minimum value for PGroupBlock.nEntry */
35888#define PAGECACHE_BLOCKALLOC_MINENTRY 15
35889
35890/* Each page cache is an instance of the following object.  Every
35891** open database file (including each in-memory database and each
35892** temporary or transient database) has a single page cache which
35893** is an instance of this object.
35894**
35895** Pointers to structures of this type are cast and returned as
35896** opaque sqlite3_pcache* handles.
35897*/
35898struct PCache1 {
35899  /* Cache configuration parameters. Page size (szPage) and the purgeable
35900  ** flag (bPurgeable) are set when the cache is created. nMax may be
35901  ** modified at any time by a call to the pcache1CacheSize() method.
35902  ** The PGroup mutex must be held when accessing nMax.
35903  */
35904  PGroup *pGroup;                     /* PGroup this cache belongs to */
35905  int szPage;                         /* Size of allocated pages in bytes */
35906  int bPurgeable;                     /* True if cache is purgeable */
35907  unsigned int nMin;                  /* Minimum number of pages reserved */
35908  unsigned int nMax;                  /* Configured "cache_size" value */
35909  unsigned int n90pct;                /* nMax*9/10 */
35910
35911  /* Hash table of all pages. The following variables may only be accessed
35912  ** when the accessor is holding the PGroup mutex.
35913  */
35914  unsigned int nRecyclable;           /* Number of pages in the LRU list */
35915  unsigned int nPage;                 /* Total number of pages in apHash */
35916  unsigned int nHash;                 /* Number of slots in apHash[] */
35917  PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
35918
35919  unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
35920};
35921
35922/*
35923** Each cache entry is represented by an instance of the following
35924** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
35925** directly before this structure in memory (see the PGHDR1_TO_PAGE()
35926** macro below).
35927*/
35928struct PgHdr1 {
35929  unsigned int iKey;             /* Key value (page number) */
35930  PgHdr1 *pNext;                 /* Next in hash table chain */
35931  PCache1 *pCache;               /* Cache that currently owns this page */
35932  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
35933  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
35934};
35935
35936/*
35937** Free slots in the allocator used to divide up the buffer provided using
35938** the SQLITE_CONFIG_PAGECACHE mechanism.
35939*/
35940struct PgFreeslot {
35941  PgFreeslot *pNext;  /* Next free slot */
35942};
35943
35944/*
35945** Global data used by this cache.
35946*/
35947static SQLITE_WSD struct PCacheGlobal {
35948  PGroup grp;                    /* The global PGroup for mode (2) */
35949
35950  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
35951  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
35952  ** fixed at sqlite3_initialize() time and do not require mutex protection.
35953  ** The nFreeSlot and pFree values do require mutex protection.
35954  */
35955  int isInit;                    /* True if initialized */
35956  int szSlot;                    /* Size of each free slot */
35957  int nSlot;                     /* The number of pcache slots */
35958  int nReserve;                  /* Try to keep nFreeSlot above this */
35959  void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
35960  /* Above requires no mutex.  Use mutex below for variable that follow. */
35961  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
35962  int nFreeSlot;                 /* Number of unused pcache slots */
35963  PgFreeslot *pFree;             /* Free page blocks */
35964  /* The following value requires a mutex to change.  We skip the mutex on
35965  ** reading because (1) most platforms read a 32-bit integer atomically and
35966  ** (2) even if an incorrect value is read, no great harm is done since this
35967  ** is really just an optimization. */
35968  int bUnderPressure;            /* True if low on PAGECACHE memory */
35969} pcache1_g;
35970
35971/*
35972** All code in this file should access the global structure above via the
35973** alias "pcache1". This ensures that the WSD emulation is used when
35974** compiling for systems that do not support real WSD.
35975*/
35976#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
35977
35978/*
35979** When a PgHdr1 structure is allocated, the associated PCache1.szPage
35980** bytes of data are located directly before it in memory (i.e. the total
35981** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
35982** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
35983** an argument and returns a pointer to the associated block of szPage
35984** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
35985** a pointer to a block of szPage bytes of data and the return value is
35986** a pointer to the associated PgHdr1 structure.
35987**
35988**   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35989*/
35990#define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
35991#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35992
35993/*
35994** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35995** a PGroupBlock pointer based on a pointer to a page buffer.
35996*/
35997#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35998  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35999
36000#define PAGE_GET_BLOCKPTR(pCache, pPg) \
36001  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
36002
36003
36004/*
36005** Macros to enter and leave the PCache LRU mutex.
36006*/
36007#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
36008#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
36009
36010/******************************************************************************/
36011/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
36012
36013/*
36014** This function is called during initialization if a static buffer is
36015** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
36016** verb to sqlite3_config(). Parameter pBuf points to an allocation large
36017** enough to contain 'n' buffers of 'sz' bytes each.
36018**
36019** This routine is called from sqlite3_initialize() and so it is guaranteed
36020** to be serialized already.  There is no need for further mutexing.
36021*/
36022SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
36023  if( pcache1.isInit ){
36024    PgFreeslot *p;
36025    sz = ROUNDDOWN8(sz);
36026    pcache1.szSlot = sz;
36027    pcache1.nSlot = pcache1.nFreeSlot = n;
36028    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
36029    pcache1.pStart = pBuf;
36030    pcache1.pFree = 0;
36031    pcache1.bUnderPressure = 0;
36032    while( n-- ){
36033      p = (PgFreeslot*)pBuf;
36034      p->pNext = pcache1.pFree;
36035      pcache1.pFree = p;
36036      pBuf = (void*)&((char*)pBuf)[sz];
36037    }
36038    pcache1.pEnd = pBuf;
36039  }
36040}
36041
36042/*
36043** Malloc function used within this file to allocate space from the buffer
36044** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
36045** such buffer exists or there is no space left in it, this function falls
36046** back to sqlite3Malloc().
36047**
36048** Multiple threads can run this routine at the same time.  Global variables
36049** in pcache1 need to be protected via mutex.
36050*/
36051static void *pcache1Alloc(int nByte){
36052  void *p = 0;
36053  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36054  sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
36055  if( nByte<=pcache1.szSlot ){
36056    sqlite3_mutex_enter(pcache1.mutex);
36057    p = (PgHdr1 *)pcache1.pFree;
36058    if( p ){
36059      pcache1.pFree = pcache1.pFree->pNext;
36060      pcache1.nFreeSlot--;
36061      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
36062      assert( pcache1.nFreeSlot>=0 );
36063      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
36064    }
36065    sqlite3_mutex_leave(pcache1.mutex);
36066  }
36067  if( p==0 ){
36068    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
36069    ** it from sqlite3Malloc instead.
36070    */
36071    p = sqlite3Malloc(nByte);
36072    if( p ){
36073      int sz = sqlite3MallocSize(p);
36074      sqlite3_mutex_enter(pcache1.mutex);
36075      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36076      sqlite3_mutex_leave(pcache1.mutex);
36077    }
36078    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
36079  }
36080  return p;
36081}
36082
36083/*
36084** Free an allocated buffer obtained from pcache1Alloc().
36085*/
36086static void pcache1Free(void *p){
36087  if( p==0 ) return;
36088  if( p>=pcache1.pStart && p<pcache1.pEnd ){
36089    PgFreeslot *pSlot;
36090    sqlite3_mutex_enter(pcache1.mutex);
36091    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
36092    pSlot = (PgFreeslot*)p;
36093    pSlot->pNext = pcache1.pFree;
36094    pcache1.pFree = pSlot;
36095    pcache1.nFreeSlot++;
36096    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
36097    assert( pcache1.nFreeSlot<=pcache1.nSlot );
36098    sqlite3_mutex_leave(pcache1.mutex);
36099  }else{
36100    int iSize;
36101    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
36102    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
36103    iSize = sqlite3MallocSize(p);
36104    sqlite3_mutex_enter(pcache1.mutex);
36105    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
36106    sqlite3_mutex_leave(pcache1.mutex);
36107    sqlite3_free(p);
36108  }
36109}
36110
36111#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36112/*
36113** Return the size of a pcache allocation
36114*/
36115static int pcache1MemSize(void *p){
36116  if( p>=pcache1.pStart && p<pcache1.pEnd ){
36117    return pcache1.szSlot;
36118  }else{
36119    int iSize;
36120    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
36121    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
36122    iSize = sqlite3MallocSize(p);
36123    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
36124    return iSize;
36125  }
36126}
36127#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36128
36129#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36130/*
36131** The block pBlock belongs to list pList but is not currently linked in.
36132** Insert it into the start of the list.
36133*/
36134static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36135  pBlock->pPrev = 0;
36136  pBlock->pNext = pList->pFirst;
36137  pList->pFirst = pBlock;
36138  if( pBlock->pNext ){
36139    pBlock->pNext->pPrev = pBlock;
36140  }else{
36141    assert( pList->pLast==0 );
36142    pList->pLast = pBlock;
36143  }
36144}
36145
36146/*
36147** If there are no blocks in the list headed by pList, remove pList
36148** from the pGroup->pBlockList list and free it with sqlite3_free().
36149*/
36150static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36151  assert( sqlite3_mutex_held(pGroup->mutex) );
36152  if( pList->pFirst==0 ){
36153    PGroupBlockList **pp;
36154    for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36155    *pp = (*pp)->pNext;
36156    sqlite3_free(pList);
36157  }
36158}
36159#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36160
36161/*
36162** Allocate a new page object initially associated with cache pCache.
36163*/
36164static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36165  int nByte = sizeof(PgHdr1) + pCache->szPage;
36166  void *pPg = 0;
36167  PgHdr1 *p;
36168
36169#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36170  PGroup *pGroup = pCache->pGroup;
36171  PGroupBlockList *pList;
36172  PGroupBlock *pBlock;
36173  int i;
36174
36175  nByte += sizeof(PGroupBlockList *);
36176  nByte = ROUND8(nByte);
36177
36178  for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36179    if( pList->nByte==nByte ) break;
36180  }
36181  if( pList==0 ){
36182    PGroupBlockList *pNew;
36183    assert( pGroup->isBusy==0 );
36184    assert( sqlite3_mutex_held(pGroup->mutex) );
36185    pGroup->isBusy = 1;  /* Disable sqlite3PcacheReleaseMemory() */
36186    pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36187    pGroup->isBusy = 0;  /* Reenable sqlite3PcacheReleaseMemory() */
36188    if( pNew==0 ){
36189      /* malloc() failure. Return early. */
36190      return 0;
36191    }
36192#ifdef SQLITE_DEBUG
36193    for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36194      assert( pList->nByte!=nByte );
36195    }
36196#endif
36197    pNew->nByte = nByte;
36198    pNew->pNext = pGroup->pBlockList;
36199    pGroup->pBlockList = pNew;
36200    pList = pNew;
36201  }
36202
36203  pBlock = pList->pFirst;
36204  if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36205    int sz;
36206
36207    /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36208    ** structure and MINENTRY allocations of nByte bytes each. If the
36209    ** allocator returns more memory than requested, then more than MINENTRY
36210    ** allocations may fit in it. */
36211    assert( sqlite3_mutex_held(pGroup->mutex) );
36212    pcache1LeaveMutex(pCache->pGroup);
36213    sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36214    pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36215    pcache1EnterMutex(pCache->pGroup);
36216
36217    if( !pBlock ){
36218      freeListIfEmpty(pGroup, pList);
36219      return 0;
36220    }
36221    pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36222    if( pBlock->nEntry>=BMS ){
36223      pBlock->nEntry = BMS-1;
36224    }
36225    pBlock->pList = pList;
36226    pBlock->mUsed = 0;
36227    pBlock->aData = (u8 *)&pBlock[1];
36228    addBlockToList(pList, pBlock);
36229
36230    sz = sqlite3MallocSize(pBlock);
36231    sqlite3_mutex_enter(pcache1.mutex);
36232    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36233    sqlite3_mutex_leave(pcache1.mutex);
36234  }
36235
36236  for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36237    if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36238      pBlock->mUsed |= ((Bitmask)1<<i);
36239      pPg = (void *)&pBlock->aData[pList->nByte * i];
36240    }
36241  }
36242  assert( pPg );
36243  PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36244
36245  /* If the block is now full, shift it to the end of the list */
36246  if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36247    assert( pList->pFirst==pBlock );
36248    assert( pBlock->pPrev==0 );
36249    assert( pList->pLast->pNext==0 );
36250    pList->pFirst = pBlock->pNext;
36251    pList->pFirst->pPrev = 0;
36252    pBlock->pPrev = pList->pLast;
36253    pBlock->pNext = 0;
36254    pList->pLast->pNext = pBlock;
36255    pList->pLast = pBlock;
36256  }
36257  p = PAGE_TO_PGHDR1(pCache, pPg);
36258  if( pCache->bPurgeable ){
36259    pCache->pGroup->nCurrentPage++;
36260  }
36261#else
36262  /* The group mutex must be released before pcache1Alloc() is called. This
36263  ** is because it may call sqlite3_release_memory(), which assumes that
36264  ** this mutex is not held. */
36265  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36266  pcache1LeaveMutex(pCache->pGroup);
36267  pPg = pcache1Alloc(nByte);
36268  pcache1EnterMutex(pCache->pGroup);
36269  if( pPg ){
36270    p = PAGE_TO_PGHDR1(pCache, pPg);
36271    if( pCache->bPurgeable ){
36272      pCache->pGroup->nCurrentPage++;
36273    }
36274  }else{
36275    p = 0;
36276  }
36277#endif
36278  return p;
36279}
36280
36281/*
36282** Free a page object allocated by pcache1AllocPage().
36283**
36284** The pointer is allowed to be NULL, which is prudent.  But it turns out
36285** that the current implementation happens to never call this routine
36286** with a NULL pointer, so we mark the NULL test with ALWAYS().
36287*/
36288static void pcache1FreePage(PgHdr1 *p){
36289  if( ALWAYS(p) ){
36290    PCache1 *pCache = p->pCache;
36291    void *pPg = PGHDR1_TO_PAGE(p);
36292
36293#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36294    PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36295    PGroupBlockList *pList = pBlock->pList;
36296    int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36297
36298    assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36299    assert( pBlock->mUsed & ((Bitmask)1<<i) );
36300    pBlock->mUsed &= ~((Bitmask)1<<i);
36301
36302    /* Remove the block from the list. If it is completely empty, free it.
36303    ** Or if it is not completely empty, re-insert it at the start of the
36304    ** list. */
36305    if( pList->pFirst==pBlock ){
36306      pList->pFirst = pBlock->pNext;
36307      if( pList->pFirst ) pList->pFirst->pPrev = 0;
36308    }else{
36309      pBlock->pPrev->pNext = pBlock->pNext;
36310    }
36311    if( pList->pLast==pBlock ){
36312      pList->pLast = pBlock->pPrev;
36313      if( pList->pLast ) pList->pLast->pNext = 0;
36314    }else{
36315      pBlock->pNext->pPrev = pBlock->pPrev;
36316    }
36317
36318    if( pBlock->mUsed==0 ){
36319      PGroup *pGroup = p->pCache->pGroup;
36320
36321      int sz = sqlite3MallocSize(pBlock);
36322      sqlite3_mutex_enter(pcache1.mutex);
36323      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36324      sqlite3_mutex_leave(pcache1.mutex);
36325      freeListIfEmpty(pGroup, pList);
36326      sqlite3_free(pBlock);
36327    }else{
36328      addBlockToList(pList, pBlock);
36329    }
36330#else
36331    assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36332    pcache1Free(pPg);
36333#endif
36334    if( pCache->bPurgeable ){
36335      pCache->pGroup->nCurrentPage--;
36336    }
36337  }
36338}
36339
36340/*
36341** Malloc function used by SQLite to obtain space from the buffer configured
36342** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
36343** exists, this function falls back to sqlite3Malloc().
36344*/
36345SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
36346  return pcache1Alloc(sz);
36347}
36348
36349/*
36350** Free an allocated buffer obtained from sqlite3PageMalloc().
36351*/
36352SQLITE_PRIVATE void sqlite3PageFree(void *p){
36353  pcache1Free(p);
36354}
36355
36356
36357/*
36358** Return true if it desirable to avoid allocating a new page cache
36359** entry.
36360**
36361** If memory was allocated specifically to the page cache using
36362** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
36363** it is desirable to avoid allocating a new page cache entry because
36364** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
36365** for all page cache needs and we should not need to spill the
36366** allocation onto the heap.
36367**
36368** Or, the heap is used for all page cache memory put the heap is
36369** under memory pressure, then again it is desirable to avoid
36370** allocating a new page cache entry in order to avoid stressing
36371** the heap even further.
36372*/
36373static int pcache1UnderMemoryPressure(PCache1 *pCache){
36374  if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
36375    return pcache1.bUnderPressure;
36376  }else{
36377    return sqlite3HeapNearlyFull();
36378  }
36379}
36380
36381/******************************************************************************/
36382/******** General Implementation Functions ************************************/
36383
36384/*
36385** This function is used to resize the hash table used by the cache passed
36386** as the first argument.
36387**
36388** The PCache mutex must be held when this function is called.
36389*/
36390static int pcache1ResizeHash(PCache1 *p){
36391  PgHdr1 **apNew;
36392  unsigned int nNew;
36393  unsigned int i;
36394
36395  assert( sqlite3_mutex_held(p->pGroup->mutex) );
36396
36397  nNew = p->nHash*2;
36398  if( nNew<256 ){
36399    nNew = 256;
36400  }
36401
36402  pcache1LeaveMutex(p->pGroup);
36403  if( p->nHash ){ sqlite3BeginBenignMalloc(); }
36404  apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
36405  if( p->nHash ){ sqlite3EndBenignMalloc(); }
36406  pcache1EnterMutex(p->pGroup);
36407  if( apNew ){
36408    memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
36409    for(i=0; i<p->nHash; i++){
36410      PgHdr1 *pPage;
36411      PgHdr1 *pNext = p->apHash[i];
36412      while( (pPage = pNext)!=0 ){
36413        unsigned int h = pPage->iKey % nNew;
36414        pNext = pPage->pNext;
36415        pPage->pNext = apNew[h];
36416        apNew[h] = pPage;
36417      }
36418    }
36419    sqlite3_free(p->apHash);
36420    p->apHash = apNew;
36421    p->nHash = nNew;
36422  }
36423
36424  return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
36425}
36426
36427/*
36428** This function is used internally to remove the page pPage from the
36429** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
36430** LRU list, then this function is a no-op.
36431**
36432** The PGroup mutex must be held when this function is called.
36433**
36434** If pPage is NULL then this routine is a no-op.
36435*/
36436static void pcache1PinPage(PgHdr1 *pPage){
36437  PCache1 *pCache;
36438  PGroup *pGroup;
36439
36440  if( pPage==0 ) return;
36441  pCache = pPage->pCache;
36442  pGroup = pCache->pGroup;
36443  assert( sqlite3_mutex_held(pGroup->mutex) );
36444  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
36445    if( pPage->pLruPrev ){
36446      pPage->pLruPrev->pLruNext = pPage->pLruNext;
36447    }
36448    if( pPage->pLruNext ){
36449      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
36450    }
36451    if( pGroup->pLruHead==pPage ){
36452      pGroup->pLruHead = pPage->pLruNext;
36453    }
36454    if( pGroup->pLruTail==pPage ){
36455      pGroup->pLruTail = pPage->pLruPrev;
36456    }
36457    pPage->pLruNext = 0;
36458    pPage->pLruPrev = 0;
36459    pPage->pCache->nRecyclable--;
36460  }
36461}
36462
36463
36464/*
36465** Remove the page supplied as an argument from the hash table
36466** (PCache1.apHash structure) that it is currently stored in.
36467**
36468** The PGroup mutex must be held when this function is called.
36469*/
36470static void pcache1RemoveFromHash(PgHdr1 *pPage){
36471  unsigned int h;
36472  PCache1 *pCache = pPage->pCache;
36473  PgHdr1 **pp;
36474
36475  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36476  h = pPage->iKey % pCache->nHash;
36477  for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
36478  *pp = (*pp)->pNext;
36479
36480  pCache->nPage--;
36481}
36482
36483/*
36484** If there are currently more than nMaxPage pages allocated, try
36485** to recycle pages to reduce the number allocated to nMaxPage.
36486*/
36487static void pcache1EnforceMaxPage(PGroup *pGroup){
36488  assert( sqlite3_mutex_held(pGroup->mutex) );
36489  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
36490    PgHdr1 *p = pGroup->pLruTail;
36491    assert( p->pCache->pGroup==pGroup );
36492    pcache1PinPage(p);
36493    pcache1RemoveFromHash(p);
36494    pcache1FreePage(p);
36495  }
36496}
36497
36498/*
36499** Discard all pages from cache pCache with a page number (key value)
36500** greater than or equal to iLimit. Any pinned pages that meet this
36501** criteria are unpinned before they are discarded.
36502**
36503** The PCache mutex must be held when this function is called.
36504*/
36505static void pcache1TruncateUnsafe(
36506  PCache1 *pCache,             /* The cache to truncate */
36507  unsigned int iLimit          /* Drop pages with this pgno or larger */
36508){
36509  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
36510  unsigned int h;
36511  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36512  for(h=0; h<pCache->nHash; h++){
36513    PgHdr1 **pp = &pCache->apHash[h];
36514    PgHdr1 *pPage;
36515    while( (pPage = *pp)!=0 ){
36516      if( pPage->iKey>=iLimit ){
36517        pCache->nPage--;
36518        *pp = pPage->pNext;
36519        pcache1PinPage(pPage);
36520        pcache1FreePage(pPage);
36521      }else{
36522        pp = &pPage->pNext;
36523        TESTONLY( nPage++; )
36524      }
36525    }
36526  }
36527  assert( pCache->nPage==nPage );
36528}
36529
36530/******************************************************************************/
36531/******** sqlite3_pcache Methods **********************************************/
36532
36533/*
36534** Implementation of the sqlite3_pcache.xInit method.
36535*/
36536static int pcache1Init(void *NotUsed){
36537  UNUSED_PARAMETER(NotUsed);
36538  assert( pcache1.isInit==0 );
36539  memset(&pcache1, 0, sizeof(pcache1));
36540  if( sqlite3GlobalConfig.bCoreMutex ){
36541    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
36542    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
36543  }
36544  pcache1.grp.mxPinned = 10;
36545  pcache1.isInit = 1;
36546  return SQLITE_OK;
36547}
36548
36549/*
36550** Implementation of the sqlite3_pcache.xShutdown method.
36551** Note that the static mutex allocated in xInit does
36552** not need to be freed.
36553*/
36554static void pcache1Shutdown(void *NotUsed){
36555  UNUSED_PARAMETER(NotUsed);
36556  assert( pcache1.isInit!=0 );
36557  memset(&pcache1, 0, sizeof(pcache1));
36558}
36559
36560/*
36561** Implementation of the sqlite3_pcache.xCreate method.
36562**
36563** Allocate a new cache.
36564*/
36565static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
36566  PCache1 *pCache;      /* The newly created page cache */
36567  PGroup *pGroup;       /* The group the new page cache will belong to */
36568  int sz;               /* Bytes of memory required to allocate the new cache */
36569
36570  /*
36571  ** The seperateCache variable is true if each PCache has its own private
36572  ** PGroup.  In other words, separateCache is true for mode (1) where no
36573  ** mutexing is required.
36574  **
36575  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
36576  **
36577  **   *  Always use a unified cache in single-threaded applications
36578  **
36579  **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
36580  **      use separate caches (mode-1)
36581  */
36582#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
36583  const int separateCache = 0;
36584#else
36585  int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
36586#endif
36587
36588  sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
36589  pCache = (PCache1 *)sqlite3_malloc(sz);
36590  if( pCache ){
36591    memset(pCache, 0, sz);
36592    if( separateCache ){
36593      pGroup = (PGroup*)&pCache[1];
36594      pGroup->mxPinned = 10;
36595    }else{
36596      pGroup = &pcache1.grp;
36597    }
36598    pCache->pGroup = pGroup;
36599    pCache->szPage = szPage;
36600    pCache->bPurgeable = (bPurgeable ? 1 : 0);
36601    if( bPurgeable ){
36602      pCache->nMin = 10;
36603      pcache1EnterMutex(pGroup);
36604      pGroup->nMinPage += pCache->nMin;
36605      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
36606      pcache1LeaveMutex(pGroup);
36607    }
36608  }
36609  return (sqlite3_pcache *)pCache;
36610}
36611
36612/*
36613** Implementation of the sqlite3_pcache.xCachesize method.
36614**
36615** Configure the cache_size limit for a cache.
36616*/
36617static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
36618  PCache1 *pCache = (PCache1 *)p;
36619  if( pCache->bPurgeable ){
36620    PGroup *pGroup = pCache->pGroup;
36621    pcache1EnterMutex(pGroup);
36622    pGroup->nMaxPage += (nMax - pCache->nMax);
36623    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
36624    pCache->nMax = nMax;
36625    pCache->n90pct = pCache->nMax*9/10;
36626    pcache1EnforceMaxPage(pGroup);
36627    pcache1LeaveMutex(pGroup);
36628  }
36629}
36630
36631/*
36632** Implementation of the sqlite3_pcache.xPagecount method.
36633*/
36634static int pcache1Pagecount(sqlite3_pcache *p){
36635  int n;
36636  PCache1 *pCache = (PCache1*)p;
36637  pcache1EnterMutex(pCache->pGroup);
36638  n = pCache->nPage;
36639  pcache1LeaveMutex(pCache->pGroup);
36640  return n;
36641}
36642
36643/*
36644** Implementation of the sqlite3_pcache.xFetch method.
36645**
36646** Fetch a page by key value.
36647**
36648** Whether or not a new page may be allocated by this function depends on
36649** the value of the createFlag argument.  0 means do not allocate a new
36650** page.  1 means allocate a new page if space is easily available.  2
36651** means to try really hard to allocate a new page.
36652**
36653** For a non-purgeable cache (a cache used as the storage for an in-memory
36654** database) there is really no difference between createFlag 1 and 2.  So
36655** the calling function (pcache.c) will never have a createFlag of 1 on
36656** a non-purgable cache.
36657**
36658** There are three different approaches to obtaining space for a page,
36659** depending on the value of parameter createFlag (which may be 0, 1 or 2).
36660**
36661**   1. Regardless of the value of createFlag, the cache is searched for a
36662**      copy of the requested page. If one is found, it is returned.
36663**
36664**   2. If createFlag==0 and the page is not already in the cache, NULL is
36665**      returned.
36666**
36667**   3. If createFlag is 1, and the page is not already in the cache, then
36668**      return NULL (do not allocate a new page) if any of the following
36669**      conditions are true:
36670**
36671**       (a) the number of pages pinned by the cache is greater than
36672**           PCache1.nMax, or
36673**
36674**       (b) the number of pages pinned by the cache is greater than
36675**           the sum of nMax for all purgeable caches, less the sum of
36676**           nMin for all other purgeable caches, or
36677**
36678**   4. If none of the first three conditions apply and the cache is marked
36679**      as purgeable, and if one of the following is true:
36680**
36681**       (a) The number of pages allocated for the cache is already
36682**           PCache1.nMax, or
36683**
36684**       (b) The number of pages allocated for all purgeable caches is
36685**           already equal to or greater than the sum of nMax for all
36686**           purgeable caches,
36687**
36688**       (c) The system is under memory pressure and wants to avoid
36689**           unnecessary pages cache entry allocations
36690**
36691**      then attempt to recycle a page from the LRU list. If it is the right
36692**      size, return the recycled buffer. Otherwise, free the buffer and
36693**      proceed to step 5.
36694**
36695**   5. Otherwise, allocate and return a new page buffer.
36696*/
36697static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
36698  int nPinned;
36699  PCache1 *pCache = (PCache1 *)p;
36700  PGroup *pGroup;
36701  PgHdr1 *pPage = 0;
36702
36703  assert( pCache->bPurgeable || createFlag!=1 );
36704  assert( pCache->bPurgeable || pCache->nMin==0 );
36705  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
36706  assert( pCache->nMin==0 || pCache->bPurgeable );
36707  pcache1EnterMutex(pGroup = pCache->pGroup);
36708
36709  /* Step 1: Search the hash table for an existing entry. */
36710  if( pCache->nHash>0 ){
36711    unsigned int h = iKey % pCache->nHash;
36712    for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
36713  }
36714
36715  /* Step 2: Abort if no existing page is found and createFlag is 0 */
36716  if( pPage || createFlag==0 ){
36717    pcache1PinPage(pPage);
36718    goto fetch_out;
36719  }
36720
36721  /* The pGroup local variable will normally be initialized by the
36722  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
36723  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
36724  ** local variable here.  Delaying the initialization of pGroup is an
36725  ** optimization:  The common case is to exit the module before reaching
36726  ** this point.
36727  */
36728#ifdef SQLITE_MUTEX_OMIT
36729  pGroup = pCache->pGroup;
36730#endif
36731
36732
36733  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
36734  nPinned = pCache->nPage - pCache->nRecyclable;
36735  assert( nPinned>=0 );
36736  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
36737  assert( pCache->n90pct == pCache->nMax*9/10 );
36738  if( createFlag==1 && (
36739        nPinned>=pGroup->mxPinned
36740     || nPinned>=(int)pCache->n90pct
36741     || pcache1UnderMemoryPressure(pCache)
36742  )){
36743    goto fetch_out;
36744  }
36745
36746  if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
36747    goto fetch_out;
36748  }
36749
36750  /* Step 4. Try to recycle a page. */
36751  if( pCache->bPurgeable && pGroup->pLruTail && (
36752         (pCache->nPage+1>=pCache->nMax)
36753      || pGroup->nCurrentPage>=pGroup->nMaxPage
36754      || pcache1UnderMemoryPressure(pCache)
36755  )){
36756    PCache1 *pOtherCache;
36757    pPage = pGroup->pLruTail;
36758    pcache1RemoveFromHash(pPage);
36759    pcache1PinPage(pPage);
36760    if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
36761      pcache1FreePage(pPage);
36762      pPage = 0;
36763    }else{
36764      pGroup->nCurrentPage -=
36765               (pOtherCache->bPurgeable - pCache->bPurgeable);
36766    }
36767  }
36768
36769  /* Step 5. If a usable page buffer has still not been found,
36770  ** attempt to allocate a new one.
36771  */
36772  if( !pPage ){
36773    if( createFlag==1 ) sqlite3BeginBenignMalloc();
36774    pPage = pcache1AllocPage(pCache);
36775    if( createFlag==1 ) sqlite3EndBenignMalloc();
36776  }
36777
36778  if( pPage ){
36779    unsigned int h = iKey % pCache->nHash;
36780    pCache->nPage++;
36781    pPage->iKey = iKey;
36782    pPage->pNext = pCache->apHash[h];
36783    pPage->pCache = pCache;
36784    pPage->pLruPrev = 0;
36785    pPage->pLruNext = 0;
36786    *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
36787    pCache->apHash[h] = pPage;
36788  }
36789
36790fetch_out:
36791  if( pPage && iKey>pCache->iMaxKey ){
36792    pCache->iMaxKey = iKey;
36793  }
36794  pcache1LeaveMutex(pGroup);
36795  return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
36796}
36797
36798
36799/*
36800** Implementation of the sqlite3_pcache.xUnpin method.
36801**
36802** Mark a page as unpinned (eligible for asynchronous recycling).
36803*/
36804static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
36805  PCache1 *pCache = (PCache1 *)p;
36806  PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36807  PGroup *pGroup = pCache->pGroup;
36808
36809  assert( pPage->pCache==pCache );
36810  pcache1EnterMutex(pGroup);
36811
36812  /* It is an error to call this function if the page is already
36813  ** part of the PGroup LRU list.
36814  */
36815  assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
36816  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
36817
36818  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
36819    pcache1RemoveFromHash(pPage);
36820    pcache1FreePage(pPage);
36821  }else{
36822    /* Add the page to the PGroup LRU list. */
36823    if( pGroup->pLruHead ){
36824      pGroup->pLruHead->pLruPrev = pPage;
36825      pPage->pLruNext = pGroup->pLruHead;
36826      pGroup->pLruHead = pPage;
36827    }else{
36828      pGroup->pLruTail = pPage;
36829      pGroup->pLruHead = pPage;
36830    }
36831    pCache->nRecyclable++;
36832  }
36833
36834  pcache1LeaveMutex(pCache->pGroup);
36835}
36836
36837/*
36838** Implementation of the sqlite3_pcache.xRekey method.
36839*/
36840static void pcache1Rekey(
36841  sqlite3_pcache *p,
36842  void *pPg,
36843  unsigned int iOld,
36844  unsigned int iNew
36845){
36846  PCache1 *pCache = (PCache1 *)p;
36847  PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36848  PgHdr1 **pp;
36849  unsigned int h;
36850  assert( pPage->iKey==iOld );
36851  assert( pPage->pCache==pCache );
36852
36853  pcache1EnterMutex(pCache->pGroup);
36854
36855  h = iOld%pCache->nHash;
36856  pp = &pCache->apHash[h];
36857  while( (*pp)!=pPage ){
36858    pp = &(*pp)->pNext;
36859  }
36860  *pp = pPage->pNext;
36861
36862  h = iNew%pCache->nHash;
36863  pPage->iKey = iNew;
36864  pPage->pNext = pCache->apHash[h];
36865  pCache->apHash[h] = pPage;
36866  if( iNew>pCache->iMaxKey ){
36867    pCache->iMaxKey = iNew;
36868  }
36869
36870  pcache1LeaveMutex(pCache->pGroup);
36871}
36872
36873/*
36874** Implementation of the sqlite3_pcache.xTruncate method.
36875**
36876** Discard all unpinned pages in the cache with a page number equal to
36877** or greater than parameter iLimit. Any pinned pages with a page number
36878** equal to or greater than iLimit are implicitly unpinned.
36879*/
36880static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
36881  PCache1 *pCache = (PCache1 *)p;
36882  pcache1EnterMutex(pCache->pGroup);
36883  if( iLimit<=pCache->iMaxKey ){
36884    pcache1TruncateUnsafe(pCache, iLimit);
36885    pCache->iMaxKey = iLimit-1;
36886  }
36887  pcache1LeaveMutex(pCache->pGroup);
36888}
36889
36890/*
36891** Implementation of the sqlite3_pcache.xDestroy method.
36892**
36893** Destroy a cache allocated using pcache1Create().
36894*/
36895static void pcache1Destroy(sqlite3_pcache *p){
36896  PCache1 *pCache = (PCache1 *)p;
36897  PGroup *pGroup = pCache->pGroup;
36898  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
36899  pcache1EnterMutex(pGroup);
36900  pcache1TruncateUnsafe(pCache, 0);
36901  pGroup->nMaxPage -= pCache->nMax;
36902  pGroup->nMinPage -= pCache->nMin;
36903  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
36904  pcache1EnforceMaxPage(pGroup);
36905  pcache1LeaveMutex(pGroup);
36906  sqlite3_free(pCache->apHash);
36907  sqlite3_free(pCache);
36908}
36909
36910/*
36911** This function is called during initialization (sqlite3_initialize()) to
36912** install the default pluggable cache module, assuming the user has not
36913** already provided an alternative.
36914*/
36915SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
36916  static const sqlite3_pcache_methods defaultMethods = {
36917    0,                       /* pArg */
36918    pcache1Init,             /* xInit */
36919    pcache1Shutdown,         /* xShutdown */
36920    pcache1Create,           /* xCreate */
36921    pcache1Cachesize,        /* xCachesize */
36922    pcache1Pagecount,        /* xPagecount */
36923    pcache1Fetch,            /* xFetch */
36924    pcache1Unpin,            /* xUnpin */
36925    pcache1Rekey,            /* xRekey */
36926    pcache1Truncate,         /* xTruncate */
36927    pcache1Destroy           /* xDestroy */
36928  };
36929  sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
36930}
36931
36932#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36933/*
36934** This function is called to free superfluous dynamically allocated memory
36935** held by the pager system. Memory in use by any SQLite pager allocated
36936** by the current thread may be sqlite3_free()ed.
36937**
36938** nReq is the number of bytes of memory required. Once this much has
36939** been released, the function returns. The return value is the total number
36940** of bytes of memory released.
36941*/
36942SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36943  int nFree = 0;
36944#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36945  if( pcache1.grp.isBusy ) return 0;
36946#endif
36947  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36948  assert( sqlite3_mutex_notheld(pcache1.mutex) );
36949  if( pcache1.pStart==0 ){
36950    PgHdr1 *p;
36951    pcache1EnterMutex(&pcache1.grp);
36952    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
36953      nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
36954      pcache1PinPage(p);
36955      pcache1RemoveFromHash(p);
36956      pcache1FreePage(p);
36957    }
36958    pcache1LeaveMutex(&pcache1.grp);
36959  }
36960  return nFree;
36961}
36962#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36963
36964#ifdef SQLITE_TEST
36965/*
36966** This function is used by test procedures to inspect the internal state
36967** of the global cache.
36968*/
36969SQLITE_PRIVATE void sqlite3PcacheStats(
36970  int *pnCurrent,      /* OUT: Total number of pages cached */
36971  int *pnMax,          /* OUT: Global maximum cache size */
36972  int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
36973  int *pnRecyclable    /* OUT: Total number of pages available for recycling */
36974){
36975  PgHdr1 *p;
36976  int nRecyclable = 0;
36977  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
36978    nRecyclable++;
36979  }
36980  *pnCurrent = pcache1.grp.nCurrentPage;
36981  *pnMax = pcache1.grp.nMaxPage;
36982  *pnMin = pcache1.grp.nMinPage;
36983  *pnRecyclable = nRecyclable;
36984}
36985#endif
36986
36987/************** End of pcache1.c *********************************************/
36988/************** Begin file rowset.c ******************************************/
36989/*
36990** 2008 December 3
36991**
36992** The author disclaims copyright to this source code.  In place of
36993** a legal notice, here is a blessing:
36994**
36995**    May you do good and not evil.
36996**    May you find forgiveness for yourself and forgive others.
36997**    May you share freely, never taking more than you give.
36998**
36999*************************************************************************
37000**
37001** This module implements an object we call a "RowSet".
37002**
37003** The RowSet object is a collection of rowids.  Rowids
37004** are inserted into the RowSet in an arbitrary order.  Inserts
37005** can be intermixed with tests to see if a given rowid has been
37006** previously inserted into the RowSet.
37007**
37008** After all inserts are finished, it is possible to extract the
37009** elements of the RowSet in sorted order.  Once this extraction
37010** process has started, no new elements may be inserted.
37011**
37012** Hence, the primitive operations for a RowSet are:
37013**
37014**    CREATE
37015**    INSERT
37016**    TEST
37017**    SMALLEST
37018**    DESTROY
37019**
37020** The CREATE and DESTROY primitives are the constructor and destructor,
37021** obviously.  The INSERT primitive adds a new element to the RowSet.
37022** TEST checks to see if an element is already in the RowSet.  SMALLEST
37023** extracts the least value from the RowSet.
37024**
37025** The INSERT primitive might allocate additional memory.  Memory is
37026** allocated in chunks so most INSERTs do no allocation.  There is an
37027** upper bound on the size of allocated memory.  No memory is freed
37028** until DESTROY.
37029**
37030** The TEST primitive includes a "batch" number.  The TEST primitive
37031** will only see elements that were inserted before the last change
37032** in the batch number.  In other words, if an INSERT occurs between
37033** two TESTs where the TESTs have the same batch nubmer, then the
37034** value added by the INSERT will not be visible to the second TEST.
37035** The initial batch number is zero, so if the very first TEST contains
37036** a non-zero batch number, it will see all prior INSERTs.
37037**
37038** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
37039** that is attempted.
37040**
37041** The cost of an INSERT is roughly constant.  (Sometime new memory
37042** has to be allocated on an INSERT.)  The cost of a TEST with a new
37043** batch number is O(NlogN) where N is the number of elements in the RowSet.
37044** The cost of a TEST using the same batch number is O(logN).  The cost
37045** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
37046** primitives are constant time.  The cost of DESTROY is O(N).
37047**
37048** There is an added cost of O(N) when switching between TEST and
37049** SMALLEST primitives.
37050*/
37051
37052
37053/*
37054** Target size for allocation chunks.
37055*/
37056#define ROWSET_ALLOCATION_SIZE 1024
37057
37058/*
37059** The number of rowset entries per allocation chunk.
37060*/
37061#define ROWSET_ENTRY_PER_CHUNK  \
37062                       ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
37063
37064/*
37065** Each entry in a RowSet is an instance of the following object.
37066*/
37067struct RowSetEntry {
37068  i64 v;                        /* ROWID value for this entry */
37069  struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
37070  struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
37071};
37072
37073/*
37074** RowSetEntry objects are allocated in large chunks (instances of the
37075** following structure) to reduce memory allocation overhead.  The
37076** chunks are kept on a linked list so that they can be deallocated
37077** when the RowSet is destroyed.
37078*/
37079struct RowSetChunk {
37080  struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
37081  struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
37082};
37083
37084/*
37085** A RowSet in an instance of the following structure.
37086**
37087** A typedef of this structure if found in sqliteInt.h.
37088*/
37089struct RowSet {
37090  struct RowSetChunk *pChunk;    /* List of all chunk allocations */
37091  sqlite3 *db;                   /* The database connection */
37092  struct RowSetEntry *pEntry;    /* List of entries using pRight */
37093  struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
37094  struct RowSetEntry *pFresh;    /* Source of new entry objects */
37095  struct RowSetEntry *pTree;     /* Binary tree of entries */
37096  u16 nFresh;                    /* Number of objects on pFresh */
37097  u8 isSorted;                   /* True if pEntry is sorted */
37098  u8 iBatch;                     /* Current insert batch */
37099};
37100
37101/*
37102** Turn bulk memory into a RowSet object.  N bytes of memory
37103** are available at pSpace.  The db pointer is used as a memory context
37104** for any subsequent allocations that need to occur.
37105** Return a pointer to the new RowSet object.
37106**
37107** It must be the case that N is sufficient to make a Rowset.  If not
37108** an assertion fault occurs.
37109**
37110** If N is larger than the minimum, use the surplus as an initial
37111** allocation of entries available to be filled.
37112*/
37113SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
37114  RowSet *p;
37115  assert( N >= ROUND8(sizeof(*p)) );
37116  p = pSpace;
37117  p->pChunk = 0;
37118  p->db = db;
37119  p->pEntry = 0;
37120  p->pLast = 0;
37121  p->pTree = 0;
37122  p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
37123  p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
37124  p->isSorted = 1;
37125  p->iBatch = 0;
37126  return p;
37127}
37128
37129/*
37130** Deallocate all chunks from a RowSet.  This frees all memory that
37131** the RowSet has allocated over its lifetime.  This routine is
37132** the destructor for the RowSet.
37133*/
37134SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
37135  struct RowSetChunk *pChunk, *pNextChunk;
37136  for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
37137    pNextChunk = pChunk->pNextChunk;
37138    sqlite3DbFree(p->db, pChunk);
37139  }
37140  p->pChunk = 0;
37141  p->nFresh = 0;
37142  p->pEntry = 0;
37143  p->pLast = 0;
37144  p->pTree = 0;
37145  p->isSorted = 1;
37146}
37147
37148/*
37149** Insert a new value into a RowSet.
37150**
37151** The mallocFailed flag of the database connection is set if a
37152** memory allocation fails.
37153*/
37154SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
37155  struct RowSetEntry *pEntry;  /* The new entry */
37156  struct RowSetEntry *pLast;   /* The last prior entry */
37157  assert( p!=0 );
37158  if( p->nFresh==0 ){
37159    struct RowSetChunk *pNew;
37160    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
37161    if( pNew==0 ){
37162      return;
37163    }
37164    pNew->pNextChunk = p->pChunk;
37165    p->pChunk = pNew;
37166    p->pFresh = pNew->aEntry;
37167    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
37168  }
37169  pEntry = p->pFresh++;
37170  p->nFresh--;
37171  pEntry->v = rowid;
37172  pEntry->pRight = 0;
37173  pLast = p->pLast;
37174  if( pLast ){
37175    if( p->isSorted && rowid<=pLast->v ){
37176      p->isSorted = 0;
37177    }
37178    pLast->pRight = pEntry;
37179  }else{
37180    assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
37181    p->pEntry = pEntry;
37182  }
37183  p->pLast = pEntry;
37184}
37185
37186/*
37187** Merge two lists of RowSetEntry objects.  Remove duplicates.
37188**
37189** The input lists are connected via pRight pointers and are
37190** assumed to each already be in sorted order.
37191*/
37192static struct RowSetEntry *rowSetMerge(
37193  struct RowSetEntry *pA,    /* First sorted list to be merged */
37194  struct RowSetEntry *pB     /* Second sorted list to be merged */
37195){
37196  struct RowSetEntry head;
37197  struct RowSetEntry *pTail;
37198
37199  pTail = &head;
37200  while( pA && pB ){
37201    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
37202    assert( pB->pRight==0 || pB->v<=pB->pRight->v );
37203    if( pA->v<pB->v ){
37204      pTail->pRight = pA;
37205      pA = pA->pRight;
37206      pTail = pTail->pRight;
37207    }else if( pB->v<pA->v ){
37208      pTail->pRight = pB;
37209      pB = pB->pRight;
37210      pTail = pTail->pRight;
37211    }else{
37212      pA = pA->pRight;
37213    }
37214  }
37215  if( pA ){
37216    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
37217    pTail->pRight = pA;
37218  }else{
37219    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
37220    pTail->pRight = pB;
37221  }
37222  return head.pRight;
37223}
37224
37225/*
37226** Sort all elements on the pEntry list of the RowSet into ascending order.
37227*/
37228static void rowSetSort(RowSet *p){
37229  unsigned int i;
37230  struct RowSetEntry *pEntry;
37231  struct RowSetEntry *aBucket[40];
37232
37233  assert( p->isSorted==0 );
37234  memset(aBucket, 0, sizeof(aBucket));
37235  while( p->pEntry ){
37236    pEntry = p->pEntry;
37237    p->pEntry = pEntry->pRight;
37238    pEntry->pRight = 0;
37239    for(i=0; aBucket[i]; i++){
37240      pEntry = rowSetMerge(aBucket[i], pEntry);
37241      aBucket[i] = 0;
37242    }
37243    aBucket[i] = pEntry;
37244  }
37245  pEntry = 0;
37246  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
37247    pEntry = rowSetMerge(pEntry, aBucket[i]);
37248  }
37249  p->pEntry = pEntry;
37250  p->pLast = 0;
37251  p->isSorted = 1;
37252}
37253
37254
37255/*
37256** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
37257** Convert this tree into a linked list connected by the pRight pointers
37258** and return pointers to the first and last elements of the new list.
37259*/
37260static void rowSetTreeToList(
37261  struct RowSetEntry *pIn,         /* Root of the input tree */
37262  struct RowSetEntry **ppFirst,    /* Write head of the output list here */
37263  struct RowSetEntry **ppLast      /* Write tail of the output list here */
37264){
37265  assert( pIn!=0 );
37266  if( pIn->pLeft ){
37267    struct RowSetEntry *p;
37268    rowSetTreeToList(pIn->pLeft, ppFirst, &p);
37269    p->pRight = pIn;
37270  }else{
37271    *ppFirst = pIn;
37272  }
37273  if( pIn->pRight ){
37274    rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
37275  }else{
37276    *ppLast = pIn;
37277  }
37278  assert( (*ppLast)->pRight==0 );
37279}
37280
37281
37282/*
37283** Convert a sorted list of elements (connected by pRight) into a binary
37284** tree with depth of iDepth.  A depth of 1 means the tree contains a single
37285** node taken from the head of *ppList.  A depth of 2 means a tree with
37286** three nodes.  And so forth.
37287**
37288** Use as many entries from the input list as required and update the
37289** *ppList to point to the unused elements of the list.  If the input
37290** list contains too few elements, then construct an incomplete tree
37291** and leave *ppList set to NULL.
37292**
37293** Return a pointer to the root of the constructed binary tree.
37294*/
37295static struct RowSetEntry *rowSetNDeepTree(
37296  struct RowSetEntry **ppList,
37297  int iDepth
37298){
37299  struct RowSetEntry *p;         /* Root of the new tree */
37300  struct RowSetEntry *pLeft;     /* Left subtree */
37301  if( *ppList==0 ){
37302    return 0;
37303  }
37304  if( iDepth==1 ){
37305    p = *ppList;
37306    *ppList = p->pRight;
37307    p->pLeft = p->pRight = 0;
37308    return p;
37309  }
37310  pLeft = rowSetNDeepTree(ppList, iDepth-1);
37311  p = *ppList;
37312  if( p==0 ){
37313    return pLeft;
37314  }
37315  p->pLeft = pLeft;
37316  *ppList = p->pRight;
37317  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
37318  return p;
37319}
37320
37321/*
37322** Convert a sorted list of elements into a binary tree. Make the tree
37323** as deep as it needs to be in order to contain the entire list.
37324*/
37325static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
37326  int iDepth;           /* Depth of the tree so far */
37327  struct RowSetEntry *p;       /* Current tree root */
37328  struct RowSetEntry *pLeft;   /* Left subtree */
37329
37330  assert( pList!=0 );
37331  p = pList;
37332  pList = p->pRight;
37333  p->pLeft = p->pRight = 0;
37334  for(iDepth=1; pList; iDepth++){
37335    pLeft = p;
37336    p = pList;
37337    pList = p->pRight;
37338    p->pLeft = pLeft;
37339    p->pRight = rowSetNDeepTree(&pList, iDepth);
37340  }
37341  return p;
37342}
37343
37344/*
37345** Convert the list in p->pEntry into a sorted list if it is not
37346** sorted already.  If there is a binary tree on p->pTree, then
37347** convert it into a list too and merge it into the p->pEntry list.
37348*/
37349static void rowSetToList(RowSet *p){
37350  if( !p->isSorted ){
37351    rowSetSort(p);
37352  }
37353  if( p->pTree ){
37354    struct RowSetEntry *pHead, *pTail;
37355    rowSetTreeToList(p->pTree, &pHead, &pTail);
37356    p->pTree = 0;
37357    p->pEntry = rowSetMerge(p->pEntry, pHead);
37358  }
37359}
37360
37361/*
37362** Extract the smallest element from the RowSet.
37363** Write the element into *pRowid.  Return 1 on success.  Return
37364** 0 if the RowSet is already empty.
37365**
37366** After this routine has been called, the sqlite3RowSetInsert()
37367** routine may not be called again.
37368*/
37369SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
37370  rowSetToList(p);
37371  if( p->pEntry ){
37372    *pRowid = p->pEntry->v;
37373    p->pEntry = p->pEntry->pRight;
37374    if( p->pEntry==0 ){
37375      sqlite3RowSetClear(p);
37376    }
37377    return 1;
37378  }else{
37379    return 0;
37380  }
37381}
37382
37383/*
37384** Check to see if element iRowid was inserted into the the rowset as
37385** part of any insert batch prior to iBatch.  Return 1 or 0.
37386*/
37387SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
37388  struct RowSetEntry *p;
37389  if( iBatch!=pRowSet->iBatch ){
37390    if( pRowSet->pEntry ){
37391      rowSetToList(pRowSet);
37392      pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
37393      pRowSet->pEntry = 0;
37394      pRowSet->pLast = 0;
37395    }
37396    pRowSet->iBatch = iBatch;
37397  }
37398  p = pRowSet->pTree;
37399  while( p ){
37400    if( p->v<iRowid ){
37401      p = p->pRight;
37402    }else if( p->v>iRowid ){
37403      p = p->pLeft;
37404    }else{
37405      return 1;
37406    }
37407  }
37408  return 0;
37409}
37410
37411/************** End of rowset.c **********************************************/
37412/************** Begin file pager.c *******************************************/
37413/*
37414** 2001 September 15
37415**
37416** The author disclaims copyright to this source code.  In place of
37417** a legal notice, here is a blessing:
37418**
37419**    May you do good and not evil.
37420**    May you find forgiveness for yourself and forgive others.
37421**    May you share freely, never taking more than you give.
37422**
37423*************************************************************************
37424** This is the implementation of the page cache subsystem or "pager".
37425**
37426** The pager is used to access a database disk file.  It implements
37427** atomic commit and rollback through the use of a journal file that
37428** is separate from the database file.  The pager also implements file
37429** locking to prevent two processes from writing the same database
37430** file simultaneously, or one process from reading the database while
37431** another is writing.
37432*/
37433#ifndef SQLITE_OMIT_DISKIO
37434/************** Include wal.h in the middle of pager.c ***********************/
37435/************** Begin file wal.h *********************************************/
37436/*
37437** 2010 February 1
37438**
37439** The author disclaims copyright to this source code.  In place of
37440** a legal notice, here is a blessing:
37441**
37442**    May you do good and not evil.
37443**    May you find forgiveness for yourself and forgive others.
37444**    May you share freely, never taking more than you give.
37445**
37446*************************************************************************
37447** This header file defines the interface to the write-ahead logging
37448** system. Refer to the comments below and the header comment attached to
37449** the implementation of each function in log.c for further details.
37450*/
37451
37452#ifndef _WAL_H_
37453#define _WAL_H_
37454
37455
37456#ifdef SQLITE_OMIT_WAL
37457# define sqlite3WalOpen(x,y,z)                   0
37458# define sqlite3WalLimit(x,y)
37459# define sqlite3WalClose(w,x,y,z)                0
37460# define sqlite3WalBeginReadTransaction(y,z)     0
37461# define sqlite3WalEndReadTransaction(z)
37462# define sqlite3WalRead(v,w,x,y,z)               0
37463# define sqlite3WalDbsize(y)                     0
37464# define sqlite3WalBeginWriteTransaction(y)      0
37465# define sqlite3WalEndWriteTransaction(x)        0
37466# define sqlite3WalUndo(x,y,z)                   0
37467# define sqlite3WalSavepoint(y,z)
37468# define sqlite3WalSavepointUndo(y,z)            0
37469# define sqlite3WalFrames(u,v,w,x,y,z)           0
37470# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
37471# define sqlite3WalCallback(z)                   0
37472# define sqlite3WalExclusiveMode(y,z)            0
37473# define sqlite3WalHeapMemory(z)                 0
37474#else
37475
37476#define WAL_SAVEPOINT_NDATA 4
37477
37478/* Connection to a write-ahead log (WAL) file.
37479** There is one object of this type for each pager.
37480*/
37481typedef struct Wal Wal;
37482
37483/* Open and close a connection to a write-ahead log. */
37484SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
37485SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
37486
37487/* Set the limiting size of a WAL file. */
37488SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
37489
37490/* Used by readers to open (lock) and close (unlock) a snapshot.  A
37491** snapshot is like a read-transaction.  It is the state of the database
37492** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
37493** preserves the current state even if the other threads or processes
37494** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
37495** transaction and releases the lock.
37496*/
37497SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
37498SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
37499
37500/* Read a page from the write-ahead log, if it is present. */
37501SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
37502
37503/* If the WAL is not empty, return the size of the database. */
37504SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
37505
37506/* Obtain or release the WRITER lock. */
37507SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
37508SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
37509
37510/* Undo any frames written (but not committed) to the log */
37511SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
37512
37513/* Return an integer that records the current (uncommitted) write
37514** position in the WAL */
37515SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
37516
37517/* Move the write position of the WAL back to iFrame.  Called in
37518** response to a ROLLBACK TO command. */
37519SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
37520
37521/* Write a frame or frames to the log. */
37522SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
37523
37524/* Copy pages from the log to the database file */
37525SQLITE_PRIVATE int sqlite3WalCheckpoint(
37526  Wal *pWal,                      /* Write-ahead log connection */
37527  int eMode,                      /* One of PASSIVE, FULL and RESTART */
37528  int (*xBusy)(void*),            /* Function to call when busy */
37529  void *pBusyArg,                 /* Context argument for xBusyHandler */
37530  int sync_flags,                 /* Flags to sync db file with (or 0) */
37531  int nBuf,                       /* Size of buffer nBuf */
37532  u8 *zBuf,                       /* Temporary buffer to use */
37533  int *pnLog,                     /* OUT: Number of frames in WAL */
37534  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
37535);
37536
37537/* Return the value to pass to a sqlite3_wal_hook callback, the
37538** number of frames in the WAL at the point of the last commit since
37539** sqlite3WalCallback() was called.  If no commits have occurred since
37540** the last call, then return 0.
37541*/
37542SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
37543
37544/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
37545** by the pager layer on the database file.
37546*/
37547SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
37548
37549/* Return true if the argument is non-NULL and the WAL module is using
37550** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
37551** WAL module is using shared-memory, return false.
37552*/
37553SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
37554
37555#endif /* ifndef SQLITE_OMIT_WAL */
37556#endif /* _WAL_H_ */
37557
37558/************** End of wal.h *************************************************/
37559/************** Continuing where we left off in pager.c **********************/
37560
37561
37562/******************* NOTES ON THE DESIGN OF THE PAGER ************************
37563**
37564** This comment block describes invariants that hold when using a rollback
37565** journal.  These invariants do not apply for journal_mode=WAL,
37566** journal_mode=MEMORY, or journal_mode=OFF.
37567**
37568** Within this comment block, a page is deemed to have been synced
37569** automatically as soon as it is written when PRAGMA synchronous=OFF.
37570** Otherwise, the page is not synced until the xSync method of the VFS
37571** is called successfully on the file containing the page.
37572**
37573** Definition:  A page of the database file is said to be "overwriteable" if
37574** one or more of the following are true about the page:
37575**
37576**     (a)  The original content of the page as it was at the beginning of
37577**          the transaction has been written into the rollback journal and
37578**          synced.
37579**
37580**     (b)  The page was a freelist leaf page at the start of the transaction.
37581**
37582**     (c)  The page number is greater than the largest page that existed in
37583**          the database file at the start of the transaction.
37584**
37585** (1) A page of the database file is never overwritten unless one of the
37586**     following are true:
37587**
37588**     (a) The page and all other pages on the same sector are overwriteable.
37589**
37590**     (b) The atomic page write optimization is enabled, and the entire
37591**         transaction other than the update of the transaction sequence
37592**         number consists of a single page change.
37593**
37594** (2) The content of a page written into the rollback journal exactly matches
37595**     both the content in the database when the rollback journal was written
37596**     and the content in the database at the beginning of the current
37597**     transaction.
37598**
37599** (3) Writes to the database file are an integer multiple of the page size
37600**     in length and are aligned on a page boundary.
37601**
37602** (4) Reads from the database file are either aligned on a page boundary and
37603**     an integer multiple of the page size in length or are taken from the
37604**     first 100 bytes of the database file.
37605**
37606** (5) All writes to the database file are synced prior to the rollback journal
37607**     being deleted, truncated, or zeroed.
37608**
37609** (6) If a master journal file is used, then all writes to the database file
37610**     are synced prior to the master journal being deleted.
37611**
37612** Definition: Two databases (or the same database at two points it time)
37613** are said to be "logically equivalent" if they give the same answer to
37614** all queries.  Note in particular the the content of freelist leaf
37615** pages can be changed arbitarily without effecting the logical equivalence
37616** of the database.
37617**
37618** (7) At any time, if any subset, including the empty set and the total set,
37619**     of the unsynced changes to a rollback journal are removed and the
37620**     journal is rolled back, the resulting database file will be logical
37621**     equivalent to the database file at the beginning of the transaction.
37622**
37623** (8) When a transaction is rolled back, the xTruncate method of the VFS
37624**     is called to restore the database file to the same size it was at
37625**     the beginning of the transaction.  (In some VFSes, the xTruncate
37626**     method is a no-op, but that does not change the fact the SQLite will
37627**     invoke it.)
37628**
37629** (9) Whenever the database file is modified, at least one bit in the range
37630**     of bytes from 24 through 39 inclusive will be changed prior to releasing
37631**     the EXCLUSIVE lock, thus signaling other connections on the same
37632**     database to flush their caches.
37633**
37634** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
37635**      than one billion transactions.
37636**
37637** (11) A database file is well-formed at the beginning and at the conclusion
37638**      of every transaction.
37639**
37640** (12) An EXCLUSIVE lock is held on the database file when writing to
37641**      the database file.
37642**
37643** (13) A SHARED lock is held on the database file while reading any
37644**      content out of the database file.
37645**
37646******************************************************************************/
37647
37648/*
37649** Macros for troubleshooting.  Normally turned off
37650*/
37651#if 0
37652int sqlite3PagerTrace=1;  /* True to enable tracing */
37653#define sqlite3DebugPrintf printf
37654#define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
37655#else
37656#define PAGERTRACE(X)
37657#endif
37658
37659/*
37660** The following two macros are used within the PAGERTRACE() macros above
37661** to print out file-descriptors.
37662**
37663** PAGERID() takes a pointer to a Pager struct as its argument. The
37664** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
37665** struct as its argument.
37666*/
37667#define PAGERID(p) ((int)(p->fd))
37668#define FILEHANDLEID(fd) ((int)fd)
37669
37670/*
37671** The Pager.eState variable stores the current 'state' of a pager. A
37672** pager may be in any one of the seven states shown in the following
37673** state diagram.
37674**
37675**                            OPEN <------+------+
37676**                              |         |      |
37677**                              V         |      |
37678**               +---------> READER-------+      |
37679**               |              |                |
37680**               |              V                |
37681**               |<-------WRITER_LOCKED------> ERROR
37682**               |              |                ^
37683**               |              V                |
37684**               |<------WRITER_CACHEMOD-------->|
37685**               |              |                |
37686**               |              V                |
37687**               |<-------WRITER_DBMOD---------->|
37688**               |              |                |
37689**               |              V                |
37690**               +<------WRITER_FINISHED-------->+
37691**
37692**
37693** List of state transitions and the C [function] that performs each:
37694**
37695**   OPEN              -> READER              [sqlite3PagerSharedLock]
37696**   READER            -> OPEN                [pager_unlock]
37697**
37698**   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
37699**   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
37700**   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
37701**   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
37702**   WRITER_***        -> READER              [pager_end_transaction]
37703**
37704**   WRITER_***        -> ERROR               [pager_error]
37705**   ERROR             -> OPEN                [pager_unlock]
37706**
37707**
37708**  OPEN:
37709**
37710**    The pager starts up in this state. Nothing is guaranteed in this
37711**    state - the file may or may not be locked and the database size is
37712**    unknown. The database may not be read or written.
37713**
37714**    * No read or write transaction is active.
37715**    * Any lock, or no lock at all, may be held on the database file.
37716**    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
37717**
37718**  READER:
37719**
37720**    In this state all the requirements for reading the database in
37721**    rollback (non-WAL) mode are met. Unless the pager is (or recently
37722**    was) in exclusive-locking mode, a user-level read transaction is
37723**    open. The database size is known in this state.
37724**
37725**    A connection running with locking_mode=normal enters this state when
37726**    it opens a read-transaction on the database and returns to state
37727**    OPEN after the read-transaction is completed. However a connection
37728**    running in locking_mode=exclusive (including temp databases) remains in
37729**    this state even after the read-transaction is closed. The only way
37730**    a locking_mode=exclusive connection can transition from READER to OPEN
37731**    is via the ERROR state (see below).
37732**
37733**    * A read transaction may be active (but a write-transaction cannot).
37734**    * A SHARED or greater lock is held on the database file.
37735**    * The dbSize variable may be trusted (even if a user-level read
37736**      transaction is not active). The dbOrigSize and dbFileSize variables
37737**      may not be trusted at this point.
37738**    * If the database is a WAL database, then the WAL connection is open.
37739**    * Even if a read-transaction is not open, it is guaranteed that
37740**      there is no hot-journal in the file-system.
37741**
37742**  WRITER_LOCKED:
37743**
37744**    The pager moves to this state from READER when a write-transaction
37745**    is first opened on the database. In WRITER_LOCKED state, all locks
37746**    required to start a write-transaction are held, but no actual
37747**    modifications to the cache or database have taken place.
37748**
37749**    In rollback mode, a RESERVED or (if the transaction was opened with
37750**    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
37751**    moving to this state, but the journal file is not written to or opened
37752**    to in this state. If the transaction is committed or rolled back while
37753**    in WRITER_LOCKED state, all that is required is to unlock the database
37754**    file.
37755**
37756**    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
37757**    If the connection is running with locking_mode=exclusive, an attempt
37758**    is made to obtain an EXCLUSIVE lock on the database file.
37759**
37760**    * A write transaction is active.
37761**    * If the connection is open in rollback-mode, a RESERVED or greater
37762**      lock is held on the database file.
37763**    * If the connection is open in WAL-mode, a WAL write transaction
37764**      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
37765**      called).
37766**    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
37767**    * The contents of the pager cache have not been modified.
37768**    * The journal file may or may not be open.
37769**    * Nothing (not even the first header) has been written to the journal.
37770**
37771**  WRITER_CACHEMOD:
37772**
37773**    A pager moves from WRITER_LOCKED state to this state when a page is
37774**    first modified by the upper layer. In rollback mode the journal file
37775**    is opened (if it is not already open) and a header written to the
37776**    start of it. The database file on disk has not been modified.
37777**
37778**    * A write transaction is active.
37779**    * A RESERVED or greater lock is held on the database file.
37780**    * The journal file is open and the first header has been written
37781**      to it, but the header has not been synced to disk.
37782**    * The contents of the page cache have been modified.
37783**
37784**  WRITER_DBMOD:
37785**
37786**    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
37787**    when it modifies the contents of the database file. WAL connections
37788**    never enter this state (since they do not modify the database file,
37789**    just the log file).
37790**
37791**    * A write transaction is active.
37792**    * An EXCLUSIVE or greater lock is held on the database file.
37793**    * The journal file is open and the first header has been written
37794**      and synced to disk.
37795**    * The contents of the page cache have been modified (and possibly
37796**      written to disk).
37797**
37798**  WRITER_FINISHED:
37799**
37800**    It is not possible for a WAL connection to enter this state.
37801**
37802**    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
37803**    state after the entire transaction has been successfully written into the
37804**    database file. In this state the transaction may be committed simply
37805**    by finalizing the journal file. Once in WRITER_FINISHED state, it is
37806**    not possible to modify the database further. At this point, the upper
37807**    layer must either commit or rollback the transaction.
37808**
37809**    * A write transaction is active.
37810**    * An EXCLUSIVE or greater lock is held on the database file.
37811**    * All writing and syncing of journal and database data has finished.
37812**      If no error occured, all that remains is to finalize the journal to
37813**      commit the transaction. If an error did occur, the caller will need
37814**      to rollback the transaction.
37815**
37816**  ERROR:
37817**
37818**    The ERROR state is entered when an IO or disk-full error (including
37819**    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
37820**    difficult to be sure that the in-memory pager state (cache contents,
37821**    db size etc.) are consistent with the contents of the file-system.
37822**
37823**    Temporary pager files may enter the ERROR state, but in-memory pagers
37824**    cannot.
37825**
37826**    For example, if an IO error occurs while performing a rollback,
37827**    the contents of the page-cache may be left in an inconsistent state.
37828**    At this point it would be dangerous to change back to READER state
37829**    (as usually happens after a rollback). Any subsequent readers might
37830**    report database corruption (due to the inconsistent cache), and if
37831**    they upgrade to writers, they may inadvertently corrupt the database
37832**    file. To avoid this hazard, the pager switches into the ERROR state
37833**    instead of READER following such an error.
37834**
37835**    Once it has entered the ERROR state, any attempt to use the pager
37836**    to read or write data returns an error. Eventually, once all
37837**    outstanding transactions have been abandoned, the pager is able to
37838**    transition back to OPEN state, discarding the contents of the
37839**    page-cache and any other in-memory state at the same time. Everything
37840**    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
37841**    when a read-transaction is next opened on the pager (transitioning
37842**    the pager into READER state). At that point the system has recovered
37843**    from the error.
37844**
37845**    Specifically, the pager jumps into the ERROR state if:
37846**
37847**      1. An error occurs while attempting a rollback. This happens in
37848**         function sqlite3PagerRollback().
37849**
37850**      2. An error occurs while attempting to finalize a journal file
37851**         following a commit in function sqlite3PagerCommitPhaseTwo().
37852**
37853**      3. An error occurs while attempting to write to the journal or
37854**         database file in function pagerStress() in order to free up
37855**         memory.
37856**
37857**    In other cases, the error is returned to the b-tree layer. The b-tree
37858**    layer then attempts a rollback operation. If the error condition
37859**    persists, the pager enters the ERROR state via condition (1) above.
37860**
37861**    Condition (3) is necessary because it can be triggered by a read-only
37862**    statement executed within a transaction. In this case, if the error
37863**    code were simply returned to the user, the b-tree layer would not
37864**    automatically attempt a rollback, as it assumes that an error in a
37865**    read-only statement cannot leave the pager in an internally inconsistent
37866**    state.
37867**
37868**    * The Pager.errCode variable is set to something other than SQLITE_OK.
37869**    * There are one or more outstanding references to pages (after the
37870**      last reference is dropped the pager should move back to OPEN state).
37871**    * The pager is not an in-memory pager.
37872**
37873**
37874** Notes:
37875**
37876**   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
37877**     connection is open in WAL mode. A WAL connection is always in one
37878**     of the first four states.
37879**
37880**   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
37881**     state. There are two exceptions: immediately after exclusive-mode has
37882**     been turned on (and before any read or write transactions are
37883**     executed), and when the pager is leaving the "error state".
37884**
37885**   * See also: assert_pager_state().
37886*/
37887#define PAGER_OPEN                  0
37888#define PAGER_READER                1
37889#define PAGER_WRITER_LOCKED         2
37890#define PAGER_WRITER_CACHEMOD       3
37891#define PAGER_WRITER_DBMOD          4
37892#define PAGER_WRITER_FINISHED       5
37893#define PAGER_ERROR                 6
37894
37895/*
37896** The Pager.eLock variable is almost always set to one of the
37897** following locking-states, according to the lock currently held on
37898** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
37899** This variable is kept up to date as locks are taken and released by
37900** the pagerLockDb() and pagerUnlockDb() wrappers.
37901**
37902** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
37903** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
37904** the operation was successful. In these circumstances pagerLockDb() and
37905** pagerUnlockDb() take a conservative approach - eLock is always updated
37906** when unlocking the file, and only updated when locking the file if the
37907** VFS call is successful. This way, the Pager.eLock variable may be set
37908** to a less exclusive (lower) value than the lock that is actually held
37909** at the system level, but it is never set to a more exclusive value.
37910**
37911** This is usually safe. If an xUnlock fails or appears to fail, there may
37912** be a few redundant xLock() calls or a lock may be held for longer than
37913** required, but nothing really goes wrong.
37914**
37915** The exception is when the database file is unlocked as the pager moves
37916** from ERROR to OPEN state. At this point there may be a hot-journal file
37917** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
37918** transition, by the same pager or any other). If the call to xUnlock()
37919** fails at this point and the pager is left holding an EXCLUSIVE lock, this
37920** can confuse the call to xCheckReservedLock() call made later as part
37921** of hot-journal detection.
37922**
37923** xCheckReservedLock() is defined as returning true "if there is a RESERVED
37924** lock held by this process or any others". So xCheckReservedLock may
37925** return true because the caller itself is holding an EXCLUSIVE lock (but
37926** doesn't know it because of a previous error in xUnlock). If this happens
37927** a hot-journal may be mistaken for a journal being created by an active
37928** transaction in another process, causing SQLite to read from the database
37929** without rolling it back.
37930**
37931** To work around this, if a call to xUnlock() fails when unlocking the
37932** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
37933** is only changed back to a real locking state after a successful call
37934** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
37935** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
37936** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
37937** lock on the database file before attempting to roll it back. See function
37938** PagerSharedLock() for more detail.
37939**
37940** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
37941** PAGER_OPEN state.
37942*/
37943#define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
37944
37945/*
37946** A macro used for invoking the codec if there is one
37947*/
37948#ifdef SQLITE_HAS_CODEC
37949# define CODEC1(P,D,N,X,E) \
37950    if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
37951# define CODEC2(P,D,N,X,E,O) \
37952    if( P->xCodec==0 ){ O=(char*)D; }else \
37953    if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
37954#else
37955# define CODEC1(P,D,N,X,E)   /* NO-OP */
37956# define CODEC2(P,D,N,X,E,O) O=(char*)D
37957#endif
37958
37959/*
37960** The maximum allowed sector size. 64KiB. If the xSectorsize() method
37961** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
37962** This could conceivably cause corruption following a power failure on
37963** such a system. This is currently an undocumented limit.
37964*/
37965#define MAX_SECTOR_SIZE 0x10000
37966
37967/*
37968** An instance of the following structure is allocated for each active
37969** savepoint and statement transaction in the system. All such structures
37970** are stored in the Pager.aSavepoint[] array, which is allocated and
37971** resized using sqlite3Realloc().
37972**
37973** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
37974** set to 0. If a journal-header is written into the main journal while
37975** the savepoint is active, then iHdrOffset is set to the byte offset
37976** immediately following the last journal record written into the main
37977** journal before the journal-header. This is required during savepoint
37978** rollback (see pagerPlaybackSavepoint()).
37979*/
37980typedef struct PagerSavepoint PagerSavepoint;
37981struct PagerSavepoint {
37982  i64 iOffset;                 /* Starting offset in main journal */
37983  i64 iHdrOffset;              /* See above */
37984  Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
37985  Pgno nOrig;                  /* Original number of pages in file */
37986  Pgno iSubRec;                /* Index of first record in sub-journal */
37987#ifndef SQLITE_OMIT_WAL
37988  u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
37989#endif
37990};
37991
37992/*
37993** A open page cache is an instance of struct Pager. A description of
37994** some of the more important member variables follows:
37995**
37996** eState
37997**
37998**   The current 'state' of the pager object. See the comment and state
37999**   diagram above for a description of the pager state.
38000**
38001** eLock
38002**
38003**   For a real on-disk database, the current lock held on the database file -
38004**   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
38005**
38006**   For a temporary or in-memory database (neither of which require any
38007**   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
38008**   databases always have Pager.exclusiveMode==1, this tricks the pager
38009**   logic into thinking that it already has all the locks it will ever
38010**   need (and no reason to release them).
38011**
38012**   In some (obscure) circumstances, this variable may also be set to
38013**   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
38014**   details.
38015**
38016** changeCountDone
38017**
38018**   This boolean variable is used to make sure that the change-counter
38019**   (the 4-byte header field at byte offset 24 of the database file) is
38020**   not updated more often than necessary.
38021**
38022**   It is set to true when the change-counter field is updated, which
38023**   can only happen if an exclusive lock is held on the database file.
38024**   It is cleared (set to false) whenever an exclusive lock is
38025**   relinquished on the database file. Each time a transaction is committed,
38026**   The changeCountDone flag is inspected. If it is true, the work of
38027**   updating the change-counter is omitted for the current transaction.
38028**
38029**   This mechanism means that when running in exclusive mode, a connection
38030**   need only update the change-counter once, for the first transaction
38031**   committed.
38032**
38033** setMaster
38034**
38035**   When PagerCommitPhaseOne() is called to commit a transaction, it may
38036**   (or may not) specify a master-journal name to be written into the
38037**   journal file before it is synced to disk.
38038**
38039**   Whether or not a journal file contains a master-journal pointer affects
38040**   the way in which the journal file is finalized after the transaction is
38041**   committed or rolled back when running in "journal_mode=PERSIST" mode.
38042**   If a journal file does not contain a master-journal pointer, it is
38043**   finalized by overwriting the first journal header with zeroes. If
38044**   it does contain a master-journal pointer the journal file is finalized
38045**   by truncating it to zero bytes, just as if the connection were
38046**   running in "journal_mode=truncate" mode.
38047**
38048**   Journal files that contain master journal pointers cannot be finalized
38049**   simply by overwriting the first journal-header with zeroes, as the
38050**   master journal pointer could interfere with hot-journal rollback of any
38051**   subsequently interrupted transaction that reuses the journal file.
38052**
38053**   The flag is cleared as soon as the journal file is finalized (either
38054**   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
38055**   journal file from being successfully finalized, the setMaster flag
38056**   is cleared anyway (and the pager will move to ERROR state).
38057**
38058** doNotSpill, doNotSyncSpill
38059**
38060**   These two boolean variables control the behaviour of cache-spills
38061**   (calls made by the pcache module to the pagerStress() routine to
38062**   write cached data to the file-system in order to free up memory).
38063**
38064**   When doNotSpill is non-zero, writing to the database from pagerStress()
38065**   is disabled altogether. This is done in a very obscure case that
38066**   comes up during savepoint rollback that requires the pcache module
38067**   to allocate a new page to prevent the journal file from being written
38068**   while it is being traversed by code in pager_playback().
38069**
38070**   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
38071**   is permitted, but syncing the journal file is not. This flag is set
38072**   by sqlite3PagerWrite() when the file-system sector-size is larger than
38073**   the database page-size in order to prevent a journal sync from happening
38074**   in between the journalling of two pages on the same sector.
38075**
38076** subjInMemory
38077**
38078**   This is a boolean variable. If true, then any required sub-journal
38079**   is opened as an in-memory journal file. If false, then in-memory
38080**   sub-journals are only used for in-memory pager files.
38081**
38082**   This variable is updated by the upper layer each time a new
38083**   write-transaction is opened.
38084**
38085** dbSize, dbOrigSize, dbFileSize
38086**
38087**   Variable dbSize is set to the number of pages in the database file.
38088**   It is valid in PAGER_READER and higher states (all states except for
38089**   OPEN and ERROR).
38090**
38091**   dbSize is set based on the size of the database file, which may be
38092**   larger than the size of the database (the value stored at offset
38093**   28 of the database header by the btree). If the size of the file
38094**   is not an integer multiple of the page-size, the value stored in
38095**   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
38096**   Except, any file that is greater than 0 bytes in size is considered
38097**   to have at least one page. (i.e. a 1KB file with 2K page-size leads
38098**   to dbSize==1).
38099**
38100**   During a write-transaction, if pages with page-numbers greater than
38101**   dbSize are modified in the cache, dbSize is updated accordingly.
38102**   Similarly, if the database is truncated using PagerTruncateImage(),
38103**   dbSize is updated.
38104**
38105**   Variables dbOrigSize and dbFileSize are valid in states
38106**   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
38107**   variable at the start of the transaction. It is used during rollback,
38108**   and to determine whether or not pages need to be journalled before
38109**   being modified.
38110**
38111**   Throughout a write-transaction, dbFileSize contains the size of
38112**   the file on disk in pages. It is set to a copy of dbSize when the
38113**   write-transaction is first opened, and updated when VFS calls are made
38114**   to write or truncate the database file on disk.
38115**
38116**   The only reason the dbFileSize variable is required is to suppress
38117**   unnecessary calls to xTruncate() after committing a transaction. If,
38118**   when a transaction is committed, the dbFileSize variable indicates
38119**   that the database file is larger than the database image (Pager.dbSize),
38120**   pager_truncate() is called. The pager_truncate() call uses xFilesize()
38121**   to measure the database file on disk, and then truncates it if required.
38122**   dbFileSize is not used when rolling back a transaction. In this case
38123**   pager_truncate() is called unconditionally (which means there may be
38124**   a call to xFilesize() that is not strictly required). In either case,
38125**   pager_truncate() may cause the file to become smaller or larger.
38126**
38127** dbHintSize
38128**
38129**   The dbHintSize variable is used to limit the number of calls made to
38130**   the VFS xFileControl(FCNTL_SIZE_HINT) method.
38131**
38132**   dbHintSize is set to a copy of the dbSize variable when a
38133**   write-transaction is opened (at the same time as dbFileSize and
38134**   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
38135**   dbHintSize is increased to the number of pages that correspond to the
38136**   size-hint passed to the method call. See pager_write_pagelist() for
38137**   details.
38138**
38139** errCode
38140**
38141**   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
38142**   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
38143**   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
38144**   sub-codes.
38145*/
38146struct Pager {
38147  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
38148  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
38149  u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
38150  u8 useJournal;              /* Use a rollback journal on this file */
38151  u8 noReadlock;              /* Do not bother to obtain readlocks */
38152  u8 noSync;                  /* Do not sync the journal if true */
38153  u8 fullSync;                /* Do extra syncs of the journal for robustness */
38154  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
38155  u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
38156  u8 tempFile;                /* zFilename is a temporary file */
38157  u8 readOnly;                /* True for a read-only database */
38158  u8 memDb;                   /* True to inhibit all file I/O */
38159
38160  /**************************************************************************
38161  ** The following block contains those class members that change during
38162  ** routine opertion.  Class members not in this block are either fixed
38163  ** when the pager is first created or else only change when there is a
38164  ** significant mode change (such as changing the page_size, locking_mode,
38165  ** or the journal_mode).  From another view, these class members describe
38166  ** the "state" of the pager, while other class members describe the
38167  ** "configuration" of the pager.
38168  */
38169  u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
38170  u8 eLock;                   /* Current lock held on database file */
38171  u8 changeCountDone;         /* Set after incrementing the change-counter */
38172  u8 setMaster;               /* True if a m-j name has been written to jrnl */
38173  u8 doNotSpill;              /* Do not spill the cache when non-zero */
38174  u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
38175  u8 subjInMemory;            /* True to use in-memory sub-journals */
38176  Pgno dbSize;                /* Number of pages in the database */
38177  Pgno dbOrigSize;            /* dbSize before the current transaction */
38178  Pgno dbFileSize;            /* Number of pages in the database file */
38179  Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
38180  int errCode;                /* One of several kinds of errors */
38181  int nRec;                   /* Pages journalled since last j-header written */
38182  u32 cksumInit;              /* Quasi-random value added to every checksum */
38183  u32 nSubRec;                /* Number of records written to sub-journal */
38184  Bitvec *pInJournal;         /* One bit for each page in the database file */
38185  sqlite3_file *fd;           /* File descriptor for database */
38186  sqlite3_file *jfd;          /* File descriptor for main journal */
38187  sqlite3_file *sjfd;         /* File descriptor for sub-journal */
38188  i64 journalOff;             /* Current write offset in the journal file */
38189  i64 journalHdr;             /* Byte offset to previous journal header */
38190  sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
38191  PagerSavepoint *aSavepoint; /* Array of active savepoints */
38192  int nSavepoint;             /* Number of elements in aSavepoint[] */
38193  char dbFileVers[16];        /* Changes whenever database file changes */
38194  /*
38195  ** End of the routinely-changing class members
38196  ***************************************************************************/
38197
38198  u16 nExtra;                 /* Add this many bytes to each in-memory page */
38199  i16 nReserve;               /* Number of unused bytes at end of each page */
38200  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
38201  u32 sectorSize;             /* Assumed sector size during rollback */
38202  int pageSize;               /* Number of bytes in a page */
38203  Pgno mxPgno;                /* Maximum allowed size of the database */
38204  i64 journalSizeLimit;       /* Size limit for persistent journal files */
38205  char *zFilename;            /* Name of the database file */
38206  char *zJournal;             /* Name of the journal file */
38207  int (*xBusyHandler)(void*); /* Function to call when busy */
38208  void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
38209#ifdef SQLITE_TEST
38210  int nHit, nMiss;            /* Cache hits and missing */
38211  int nRead, nWrite;          /* Database pages read/written */
38212#endif
38213  void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
38214#ifdef SQLITE_HAS_CODEC
38215  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
38216  void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
38217  void (*xCodecFree)(void*);             /* Destructor for the codec */
38218  void *pCodec;               /* First argument to xCodec... methods */
38219#endif
38220  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
38221  PCache *pPCache;            /* Pointer to page cache object */
38222#ifndef SQLITE_OMIT_WAL
38223  Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
38224  char *zWal;                 /* File name for write-ahead log */
38225#endif
38226};
38227
38228/*
38229** The following global variables hold counters used for
38230** testing purposes only.  These variables do not exist in
38231** a non-testing build.  These variables are not thread-safe.
38232*/
38233#ifdef SQLITE_TEST
38234SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
38235SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
38236SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
38237# define PAGER_INCR(v)  v++
38238#else
38239# define PAGER_INCR(v)
38240#endif
38241
38242
38243
38244/*
38245** Journal files begin with the following magic string.  The data
38246** was obtained from /dev/random.  It is used only as a sanity check.
38247**
38248** Since version 2.8.0, the journal format contains additional sanity
38249** checking information.  If the power fails while the journal is being
38250** written, semi-random garbage data might appear in the journal
38251** file after power is restored.  If an attempt is then made
38252** to roll the journal back, the database could be corrupted.  The additional
38253** sanity checking data is an attempt to discover the garbage in the
38254** journal and ignore it.
38255**
38256** The sanity checking information for the new journal format consists
38257** of a 32-bit checksum on each page of data.  The checksum covers both
38258** the page number and the pPager->pageSize bytes of data for the page.
38259** This cksum is initialized to a 32-bit random value that appears in the
38260** journal file right after the header.  The random initializer is important,
38261** because garbage data that appears at the end of a journal is likely
38262** data that was once in other files that have now been deleted.  If the
38263** garbage data came from an obsolete journal file, the checksums might
38264** be correct.  But by initializing the checksum to random value which
38265** is different for every journal, we minimize that risk.
38266*/
38267static const unsigned char aJournalMagic[] = {
38268  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
38269};
38270
38271/*
38272** The size of the of each page record in the journal is given by
38273** the following macro.
38274*/
38275#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
38276
38277/*
38278** The journal header size for this pager. This is usually the same
38279** size as a single disk sector. See also setSectorSize().
38280*/
38281#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
38282
38283/*
38284** The macro MEMDB is true if we are dealing with an in-memory database.
38285** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
38286** the value of MEMDB will be a constant and the compiler will optimize
38287** out code that would never execute.
38288*/
38289#ifdef SQLITE_OMIT_MEMORYDB
38290# define MEMDB 0
38291#else
38292# define MEMDB pPager->memDb
38293#endif
38294
38295/*
38296** The maximum legal page number is (2^31 - 1).
38297*/
38298#define PAGER_MAX_PGNO 2147483647
38299
38300/*
38301** The argument to this macro is a file descriptor (type sqlite3_file*).
38302** Return 0 if it is not open, or non-zero (but not 1) if it is.
38303**
38304** This is so that expressions can be written as:
38305**
38306**   if( isOpen(pPager->jfd) ){ ...
38307**
38308** instead of
38309**
38310**   if( pPager->jfd->pMethods ){ ...
38311*/
38312#define isOpen(pFd) ((pFd)->pMethods)
38313
38314/*
38315** Return true if this pager uses a write-ahead log instead of the usual
38316** rollback journal. Otherwise false.
38317*/
38318#ifndef SQLITE_OMIT_WAL
38319static int pagerUseWal(Pager *pPager){
38320  return (pPager->pWal!=0);
38321}
38322#else
38323# define pagerUseWal(x) 0
38324# define pagerRollbackWal(x) 0
38325# define pagerWalFrames(v,w,x,y,z) 0
38326# define pagerOpenWalIfPresent(z) SQLITE_OK
38327# define pagerBeginReadTransaction(z) SQLITE_OK
38328#endif
38329
38330#ifndef NDEBUG
38331/*
38332** Usage:
38333**
38334**   assert( assert_pager_state(pPager) );
38335**
38336** This function runs many asserts to try to find inconsistencies in
38337** the internal state of the Pager object.
38338*/
38339static int assert_pager_state(Pager *p){
38340  Pager *pPager = p;
38341
38342  /* State must be valid. */
38343  assert( p->eState==PAGER_OPEN
38344       || p->eState==PAGER_READER
38345       || p->eState==PAGER_WRITER_LOCKED
38346       || p->eState==PAGER_WRITER_CACHEMOD
38347       || p->eState==PAGER_WRITER_DBMOD
38348       || p->eState==PAGER_WRITER_FINISHED
38349       || p->eState==PAGER_ERROR
38350  );
38351
38352  /* Regardless of the current state, a temp-file connection always behaves
38353  ** as if it has an exclusive lock on the database file. It never updates
38354  ** the change-counter field, so the changeCountDone flag is always set.
38355  */
38356  assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
38357  assert( p->tempFile==0 || pPager->changeCountDone );
38358
38359  /* If the useJournal flag is clear, the journal-mode must be "OFF".
38360  ** And if the journal-mode is "OFF", the journal file must not be open.
38361  */
38362  assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
38363  assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
38364
38365  /* Check that MEMDB implies noSync. And an in-memory journal. Since
38366  ** this means an in-memory pager performs no IO at all, it cannot encounter
38367  ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
38368  ** a journal file. (although the in-memory journal implementation may
38369  ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
38370  ** is therefore not possible for an in-memory pager to enter the ERROR
38371  ** state.
38372  */
38373  if( MEMDB ){
38374    assert( p->noSync );
38375    assert( p->journalMode==PAGER_JOURNALMODE_OFF
38376         || p->journalMode==PAGER_JOURNALMODE_MEMORY
38377    );
38378    assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
38379    assert( pagerUseWal(p)==0 );
38380  }
38381
38382  /* If changeCountDone is set, a RESERVED lock or greater must be held
38383  ** on the file.
38384  */
38385  assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
38386  assert( p->eLock!=PENDING_LOCK );
38387
38388  switch( p->eState ){
38389    case PAGER_OPEN:
38390      assert( !MEMDB );
38391      assert( pPager->errCode==SQLITE_OK );
38392      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
38393      break;
38394
38395    case PAGER_READER:
38396      assert( pPager->errCode==SQLITE_OK );
38397      assert( p->eLock!=UNKNOWN_LOCK );
38398      assert( p->eLock>=SHARED_LOCK || p->noReadlock );
38399      break;
38400
38401    case PAGER_WRITER_LOCKED:
38402      assert( p->eLock!=UNKNOWN_LOCK );
38403      assert( pPager->errCode==SQLITE_OK );
38404      if( !pagerUseWal(pPager) ){
38405        assert( p->eLock>=RESERVED_LOCK );
38406      }
38407      assert( pPager->dbSize==pPager->dbOrigSize );
38408      assert( pPager->dbOrigSize==pPager->dbFileSize );
38409      assert( pPager->dbOrigSize==pPager->dbHintSize );
38410      assert( pPager->setMaster==0 );
38411      break;
38412
38413    case PAGER_WRITER_CACHEMOD:
38414      assert( p->eLock!=UNKNOWN_LOCK );
38415      assert( pPager->errCode==SQLITE_OK );
38416      if( !pagerUseWal(pPager) ){
38417        /* It is possible that if journal_mode=wal here that neither the
38418        ** journal file nor the WAL file are open. This happens during
38419        ** a rollback transaction that switches from journal_mode=off
38420        ** to journal_mode=wal.
38421        */
38422        assert( p->eLock>=RESERVED_LOCK );
38423        assert( isOpen(p->jfd)
38424             || p->journalMode==PAGER_JOURNALMODE_OFF
38425             || p->journalMode==PAGER_JOURNALMODE_WAL
38426        );
38427      }
38428      assert( pPager->dbOrigSize==pPager->dbFileSize );
38429      assert( pPager->dbOrigSize==pPager->dbHintSize );
38430      break;
38431
38432    case PAGER_WRITER_DBMOD:
38433      assert( p->eLock==EXCLUSIVE_LOCK );
38434      assert( pPager->errCode==SQLITE_OK );
38435      assert( !pagerUseWal(pPager) );
38436      assert( p->eLock>=EXCLUSIVE_LOCK );
38437      assert( isOpen(p->jfd)
38438           || p->journalMode==PAGER_JOURNALMODE_OFF
38439           || p->journalMode==PAGER_JOURNALMODE_WAL
38440      );
38441      assert( pPager->dbOrigSize<=pPager->dbHintSize );
38442      break;
38443
38444    case PAGER_WRITER_FINISHED:
38445      assert( p->eLock==EXCLUSIVE_LOCK );
38446      assert( pPager->errCode==SQLITE_OK );
38447      assert( !pagerUseWal(pPager) );
38448      assert( isOpen(p->jfd)
38449           || p->journalMode==PAGER_JOURNALMODE_OFF
38450           || p->journalMode==PAGER_JOURNALMODE_WAL
38451      );
38452      break;
38453
38454    case PAGER_ERROR:
38455      /* There must be at least one outstanding reference to the pager if
38456      ** in ERROR state. Otherwise the pager should have already dropped
38457      ** back to OPEN state.
38458      */
38459      assert( pPager->errCode!=SQLITE_OK );
38460      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
38461      break;
38462  }
38463
38464  return 1;
38465}
38466#endif /* ifndef NDEBUG */
38467
38468#ifdef SQLITE_DEBUG
38469/*
38470** Return a pointer to a human readable string in a static buffer
38471** containing the state of the Pager object passed as an argument. This
38472** is intended to be used within debuggers. For example, as an alternative
38473** to "print *pPager" in gdb:
38474**
38475** (gdb) printf "%s", print_pager_state(pPager)
38476*/
38477static char *print_pager_state(Pager *p){
38478  static char zRet[1024];
38479
38480  sqlite3_snprintf(1024, zRet,
38481      "Filename:      %s\n"
38482      "State:         %s errCode=%d\n"
38483      "Lock:          %s\n"
38484      "Locking mode:  locking_mode=%s\n"
38485      "Journal mode:  journal_mode=%s\n"
38486      "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
38487      "Journal:       journalOff=%lld journalHdr=%lld\n"
38488      "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
38489      , p->zFilename
38490      , p->eState==PAGER_OPEN            ? "OPEN" :
38491        p->eState==PAGER_READER          ? "READER" :
38492        p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
38493        p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
38494        p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
38495        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
38496        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
38497      , (int)p->errCode
38498      , p->eLock==NO_LOCK         ? "NO_LOCK" :
38499        p->eLock==RESERVED_LOCK   ? "RESERVED" :
38500        p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
38501        p->eLock==SHARED_LOCK     ? "SHARED" :
38502        p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
38503      , p->exclusiveMode ? "exclusive" : "normal"
38504      , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
38505        p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
38506        p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
38507        p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
38508        p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
38509        p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
38510      , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
38511      , p->journalOff, p->journalHdr
38512      , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
38513  );
38514
38515  return zRet;
38516}
38517#endif
38518
38519/*
38520** Return true if it is necessary to write page *pPg into the sub-journal.
38521** A page needs to be written into the sub-journal if there exists one
38522** or more open savepoints for which:
38523**
38524**   * The page-number is less than or equal to PagerSavepoint.nOrig, and
38525**   * The bit corresponding to the page-number is not set in
38526**     PagerSavepoint.pInSavepoint.
38527*/
38528static int subjRequiresPage(PgHdr *pPg){
38529  Pgno pgno = pPg->pgno;
38530  Pager *pPager = pPg->pPager;
38531  int i;
38532  for(i=0; i<pPager->nSavepoint; i++){
38533    PagerSavepoint *p = &pPager->aSavepoint[i];
38534    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
38535      return 1;
38536    }
38537  }
38538  return 0;
38539}
38540
38541/*
38542** Return true if the page is already in the journal file.
38543*/
38544static int pageInJournal(PgHdr *pPg){
38545  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
38546}
38547
38548/*
38549** Read a 32-bit integer from the given file descriptor.  Store the integer
38550** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
38551** error code is something goes wrong.
38552**
38553** All values are stored on disk as big-endian.
38554*/
38555static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
38556  unsigned char ac[4];
38557  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
38558  if( rc==SQLITE_OK ){
38559    *pRes = sqlite3Get4byte(ac);
38560  }
38561  return rc;
38562}
38563
38564/*
38565** Write a 32-bit integer into a string buffer in big-endian byte order.
38566*/
38567#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
38568
38569
38570/*
38571** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
38572** on success or an error code is something goes wrong.
38573*/
38574static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
38575  char ac[4];
38576  put32bits(ac, val);
38577  return sqlite3OsWrite(fd, ac, 4, offset);
38578}
38579
38580/*
38581** Unlock the database file to level eLock, which must be either NO_LOCK
38582** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
38583** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
38584**
38585** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
38586** called, do not modify it. See the comment above the #define of
38587** UNKNOWN_LOCK for an explanation of this.
38588*/
38589static int pagerUnlockDb(Pager *pPager, int eLock){
38590  int rc = SQLITE_OK;
38591
38592  assert( !pPager->exclusiveMode || pPager->eLock==eLock );
38593  assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
38594  assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
38595  if( isOpen(pPager->fd) ){
38596    assert( pPager->eLock>=eLock );
38597    rc = sqlite3OsUnlock(pPager->fd, eLock);
38598    if( pPager->eLock!=UNKNOWN_LOCK ){
38599      pPager->eLock = (u8)eLock;
38600    }
38601    IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
38602  }
38603  return rc;
38604}
38605
38606/*
38607** Lock the database file to level eLock, which must be either SHARED_LOCK,
38608** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
38609** Pager.eLock variable to the new locking state.
38610**
38611** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
38612** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
38613** See the comment above the #define of UNKNOWN_LOCK for an explanation
38614** of this.
38615*/
38616static int pagerLockDb(Pager *pPager, int eLock){
38617  int rc = SQLITE_OK;
38618
38619  assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
38620  if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
38621    rc = sqlite3OsLock(pPager->fd, eLock);
38622    if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
38623      pPager->eLock = (u8)eLock;
38624      IOTRACE(("LOCK %p %d\n", pPager, eLock))
38625    }
38626  }
38627  return rc;
38628}
38629
38630/*
38631** This function determines whether or not the atomic-write optimization
38632** can be used with this pager. The optimization can be used if:
38633**
38634**  (a) the value returned by OsDeviceCharacteristics() indicates that
38635**      a database page may be written atomically, and
38636**  (b) the value returned by OsSectorSize() is less than or equal
38637**      to the page size.
38638**
38639** The optimization is also always enabled for temporary files. It is
38640** an error to call this function if pPager is opened on an in-memory
38641** database.
38642**
38643** If the optimization cannot be used, 0 is returned. If it can be used,
38644** then the value returned is the size of the journal file when it
38645** contains rollback data for exactly one page.
38646*/
38647#ifdef SQLITE_ENABLE_ATOMIC_WRITE
38648static int jrnlBufferSize(Pager *pPager){
38649  assert( !MEMDB );
38650  if( !pPager->tempFile ){
38651    int dc;                           /* Device characteristics */
38652    int nSector;                      /* Sector size */
38653    int szPage;                       /* Page size */
38654
38655    assert( isOpen(pPager->fd) );
38656    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
38657    nSector = pPager->sectorSize;
38658    szPage = pPager->pageSize;
38659
38660    assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
38661    assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
38662    if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
38663      return 0;
38664    }
38665  }
38666
38667  return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
38668}
38669#endif
38670
38671/*
38672** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
38673** on the cache using a hash function.  This is used for testing
38674** and debugging only.
38675*/
38676#ifdef SQLITE_CHECK_PAGES
38677/*
38678** Return a 32-bit hash of the page data for pPage.
38679*/
38680static u32 pager_datahash(int nByte, unsigned char *pData){
38681  u32 hash = 0;
38682  int i;
38683  for(i=0; i<nByte; i++){
38684    hash = (hash*1039) + pData[i];
38685  }
38686  return hash;
38687}
38688static u32 pager_pagehash(PgHdr *pPage){
38689  return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
38690}
38691static void pager_set_pagehash(PgHdr *pPage){
38692  pPage->pageHash = pager_pagehash(pPage);
38693}
38694
38695/*
38696** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
38697** is defined, and NDEBUG is not defined, an assert() statement checks
38698** that the page is either dirty or still matches the calculated page-hash.
38699*/
38700#define CHECK_PAGE(x) checkPage(x)
38701static void checkPage(PgHdr *pPg){
38702  Pager *pPager = pPg->pPager;
38703  assert( pPager->eState!=PAGER_ERROR );
38704  assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
38705}
38706
38707#else
38708#define pager_datahash(X,Y)  0
38709#define pager_pagehash(X)  0
38710#define pager_set_pagehash(X)
38711#define CHECK_PAGE(x)
38712#endif  /* SQLITE_CHECK_PAGES */
38713
38714/*
38715** When this is called the journal file for pager pPager must be open.
38716** This function attempts to read a master journal file name from the
38717** end of the file and, if successful, copies it into memory supplied
38718** by the caller. See comments above writeMasterJournal() for the format
38719** used to store a master journal file name at the end of a journal file.
38720**
38721** zMaster must point to a buffer of at least nMaster bytes allocated by
38722** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
38723** enough space to write the master journal name). If the master journal
38724** name in the journal is longer than nMaster bytes (including a
38725** nul-terminator), then this is handled as if no master journal name
38726** were present in the journal.
38727**
38728** If a master journal file name is present at the end of the journal
38729** file, then it is copied into the buffer pointed to by zMaster. A
38730** nul-terminator byte is appended to the buffer following the master
38731** journal file name.
38732**
38733** If it is determined that no master journal file name is present
38734** zMaster[0] is set to 0 and SQLITE_OK returned.
38735**
38736** If an error occurs while reading from the journal file, an SQLite
38737** error code is returned.
38738*/
38739static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
38740  int rc;                    /* Return code */
38741  u32 len;                   /* Length in bytes of master journal name */
38742  i64 szJ;                   /* Total size in bytes of journal file pJrnl */
38743  u32 cksum;                 /* MJ checksum value read from journal */
38744  u32 u;                     /* Unsigned loop counter */
38745  unsigned char aMagic[8];   /* A buffer to hold the magic header */
38746  zMaster[0] = '\0';
38747
38748  if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
38749   || szJ<16
38750   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
38751   || len>=nMaster
38752   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
38753   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
38754   || memcmp(aMagic, aJournalMagic, 8)
38755   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
38756  ){
38757    return rc;
38758  }
38759
38760  /* See if the checksum matches the master journal name */
38761  for(u=0; u<len; u++){
38762    cksum -= zMaster[u];
38763  }
38764  if( cksum ){
38765    /* If the checksum doesn't add up, then one or more of the disk sectors
38766    ** containing the master journal filename is corrupted. This means
38767    ** definitely roll back, so just return SQLITE_OK and report a (nul)
38768    ** master-journal filename.
38769    */
38770    len = 0;
38771  }
38772  zMaster[len] = '\0';
38773
38774  return SQLITE_OK;
38775}
38776
38777/*
38778** Return the offset of the sector boundary at or immediately
38779** following the value in pPager->journalOff, assuming a sector
38780** size of pPager->sectorSize bytes.
38781**
38782** i.e for a sector size of 512:
38783**
38784**   Pager.journalOff          Return value
38785**   ---------------------------------------
38786**   0                         0
38787**   512                       512
38788**   100                       512
38789**   2000                      2048
38790**
38791*/
38792static i64 journalHdrOffset(Pager *pPager){
38793  i64 offset = 0;
38794  i64 c = pPager->journalOff;
38795  if( c ){
38796    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
38797  }
38798  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
38799  assert( offset>=c );
38800  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
38801  return offset;
38802}
38803
38804/*
38805** The journal file must be open when this function is called.
38806**
38807** This function is a no-op if the journal file has not been written to
38808** within the current transaction (i.e. if Pager.journalOff==0).
38809**
38810** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
38811** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
38812** zero the 28-byte header at the start of the journal file. In either case,
38813** if the pager is not in no-sync mode, sync the journal file immediately
38814** after writing or truncating it.
38815**
38816** If Pager.journalSizeLimit is set to a positive, non-zero value, and
38817** following the truncation or zeroing described above the size of the
38818** journal file in bytes is larger than this value, then truncate the
38819** journal file to Pager.journalSizeLimit bytes. The journal file does
38820** not need to be synced following this operation.
38821**
38822** If an IO error occurs, abandon processing and return the IO error code.
38823** Otherwise, return SQLITE_OK.
38824*/
38825static int zeroJournalHdr(Pager *pPager, int doTruncate){
38826  int rc = SQLITE_OK;                               /* Return code */
38827  assert( isOpen(pPager->jfd) );
38828  if( pPager->journalOff ){
38829    const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
38830
38831    IOTRACE(("JZEROHDR %p\n", pPager))
38832    if( doTruncate || iLimit==0 ){
38833      rc = sqlite3OsTruncate(pPager->jfd, 0);
38834    }else{
38835      static const char zeroHdr[28] = {0};
38836      rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
38837    }
38838    if( rc==SQLITE_OK && !pPager->noSync ){
38839      rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
38840    }
38841
38842    /* At this point the transaction is committed but the write lock
38843    ** is still held on the file. If there is a size limit configured for
38844    ** the persistent journal and the journal file currently consumes more
38845    ** space than that limit allows for, truncate it now. There is no need
38846    ** to sync the file following this operation.
38847    */
38848    if( rc==SQLITE_OK && iLimit>0 ){
38849      i64 sz;
38850      rc = sqlite3OsFileSize(pPager->jfd, &sz);
38851      if( rc==SQLITE_OK && sz>iLimit ){
38852        rc = sqlite3OsTruncate(pPager->jfd, iLimit);
38853      }
38854    }
38855  }
38856  return rc;
38857}
38858
38859/*
38860** The journal file must be open when this routine is called. A journal
38861** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
38862** current location.
38863**
38864** The format for the journal header is as follows:
38865** - 8 bytes: Magic identifying journal format.
38866** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
38867** - 4 bytes: Random number used for page hash.
38868** - 4 bytes: Initial database page count.
38869** - 4 bytes: Sector size used by the process that wrote this journal.
38870** - 4 bytes: Database page size.
38871**
38872** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
38873*/
38874static int writeJournalHdr(Pager *pPager){
38875  int rc = SQLITE_OK;                 /* Return code */
38876  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
38877  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
38878  u32 nWrite;                         /* Bytes of header sector written */
38879  int ii;                             /* Loop counter */
38880
38881  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
38882
38883  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
38884    nHeader = JOURNAL_HDR_SZ(pPager);
38885  }
38886
38887  /* If there are active savepoints and any of them were created
38888  ** since the most recent journal header was written, update the
38889  ** PagerSavepoint.iHdrOffset fields now.
38890  */
38891  for(ii=0; ii<pPager->nSavepoint; ii++){
38892    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
38893      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
38894    }
38895  }
38896
38897  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
38898
38899  /*
38900  ** Write the nRec Field - the number of page records that follow this
38901  ** journal header. Normally, zero is written to this value at this time.
38902  ** After the records are added to the journal (and the journal synced,
38903  ** if in full-sync mode), the zero is overwritten with the true number
38904  ** of records (see syncJournal()).
38905  **
38906  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
38907  ** reading the journal this value tells SQLite to assume that the
38908  ** rest of the journal file contains valid page records. This assumption
38909  ** is dangerous, as if a failure occurred whilst writing to the journal
38910  ** file it may contain some garbage data. There are two scenarios
38911  ** where this risk can be ignored:
38912  **
38913  **   * When the pager is in no-sync mode. Corruption can follow a
38914  **     power failure in this case anyway.
38915  **
38916  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
38917  **     that garbage data is never appended to the journal file.
38918  */
38919  assert( isOpen(pPager->fd) || pPager->noSync );
38920  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
38921   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
38922  ){
38923    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
38924    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
38925  }else{
38926    memset(zHeader, 0, sizeof(aJournalMagic)+4);
38927  }
38928
38929  /* The random check-hash initialiser */
38930  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
38931  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
38932  /* The initial database size */
38933  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
38934  /* The assumed sector size for this process */
38935  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
38936
38937  /* The page size */
38938  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
38939
38940  /* Initializing the tail of the buffer is not necessary.  Everything
38941  ** works find if the following memset() is omitted.  But initializing
38942  ** the memory prevents valgrind from complaining, so we are willing to
38943  ** take the performance hit.
38944  */
38945  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
38946         nHeader-(sizeof(aJournalMagic)+20));
38947
38948  /* In theory, it is only necessary to write the 28 bytes that the
38949  ** journal header consumes to the journal file here. Then increment the
38950  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
38951  ** record is written to the following sector (leaving a gap in the file
38952  ** that will be implicitly filled in by the OS).
38953  **
38954  ** However it has been discovered that on some systems this pattern can
38955  ** be significantly slower than contiguously writing data to the file,
38956  ** even if that means explicitly writing data to the block of
38957  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
38958  ** is done.
38959  **
38960  ** The loop is required here in case the sector-size is larger than the
38961  ** database page size. Since the zHeader buffer is only Pager.pageSize
38962  ** bytes in size, more than one call to sqlite3OsWrite() may be required
38963  ** to populate the entire journal header sector.
38964  */
38965  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
38966    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
38967    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
38968    assert( pPager->journalHdr <= pPager->journalOff );
38969    pPager->journalOff += nHeader;
38970  }
38971
38972  return rc;
38973}
38974
38975/*
38976** The journal file must be open when this is called. A journal header file
38977** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
38978** file. The current location in the journal file is given by
38979** pPager->journalOff. See comments above function writeJournalHdr() for
38980** a description of the journal header format.
38981**
38982** If the header is read successfully, *pNRec is set to the number of
38983** page records following this header and *pDbSize is set to the size of the
38984** database before the transaction began, in pages. Also, pPager->cksumInit
38985** is set to the value read from the journal header. SQLITE_OK is returned
38986** in this case.
38987**
38988** If the journal header file appears to be corrupted, SQLITE_DONE is
38989** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
38990** cannot be read from the journal file an error code is returned.
38991*/
38992static int readJournalHdr(
38993  Pager *pPager,               /* Pager object */
38994  int isHot,
38995  i64 journalSize,             /* Size of the open journal file in bytes */
38996  u32 *pNRec,                  /* OUT: Value read from the nRec field */
38997  u32 *pDbSize                 /* OUT: Value of original database size field */
38998){
38999  int rc;                      /* Return code */
39000  unsigned char aMagic[8];     /* A buffer to hold the magic header */
39001  i64 iHdrOff;                 /* Offset of journal header being read */
39002
39003  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
39004
39005  /* Advance Pager.journalOff to the start of the next sector. If the
39006  ** journal file is too small for there to be a header stored at this
39007  ** point, return SQLITE_DONE.
39008  */
39009  pPager->journalOff = journalHdrOffset(pPager);
39010  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
39011    return SQLITE_DONE;
39012  }
39013  iHdrOff = pPager->journalOff;
39014
39015  /* Read in the first 8 bytes of the journal header. If they do not match
39016  ** the  magic string found at the start of each journal header, return
39017  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
39018  ** proceed.
39019  */
39020  if( isHot || iHdrOff!=pPager->journalHdr ){
39021    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
39022    if( rc ){
39023      return rc;
39024    }
39025    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
39026      return SQLITE_DONE;
39027    }
39028  }
39029
39030  /* Read the first three 32-bit fields of the journal header: The nRec
39031  ** field, the checksum-initializer and the database size at the start
39032  ** of the transaction. Return an error code if anything goes wrong.
39033  */
39034  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
39035   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
39036   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
39037  ){
39038    return rc;
39039  }
39040
39041  if( pPager->journalOff==0 ){
39042    u32 iPageSize;               /* Page-size field of journal header */
39043    u32 iSectorSize;             /* Sector-size field of journal header */
39044
39045    /* Read the page-size and sector-size journal header fields. */
39046    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
39047     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
39048    ){
39049      return rc;
39050    }
39051
39052    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
39053    ** journal header to zero. In this case, assume that the Pager.pageSize
39054    ** variable is already set to the correct page size.
39055    */
39056    if( iPageSize==0 ){
39057      iPageSize = pPager->pageSize;
39058    }
39059
39060    /* Check that the values read from the page-size and sector-size fields
39061    ** are within range. To be 'in range', both values need to be a power
39062    ** of two greater than or equal to 512 or 32, and not greater than their
39063    ** respective compile time maximum limits.
39064    */
39065    if( iPageSize<512                  || iSectorSize<32
39066     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
39067     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
39068    ){
39069      /* If the either the page-size or sector-size in the journal-header is
39070      ** invalid, then the process that wrote the journal-header must have
39071      ** crashed before the header was synced. In this case stop reading
39072      ** the journal file here.
39073      */
39074      return SQLITE_DONE;
39075    }
39076
39077    /* Update the page-size to match the value read from the journal.
39078    ** Use a testcase() macro to make sure that malloc failure within
39079    ** PagerSetPagesize() is tested.
39080    */
39081    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
39082    testcase( rc!=SQLITE_OK );
39083
39084    /* Update the assumed sector-size to match the value used by
39085    ** the process that created this journal. If this journal was
39086    ** created by a process other than this one, then this routine
39087    ** is being called from within pager_playback(). The local value
39088    ** of Pager.sectorSize is restored at the end of that routine.
39089    */
39090    pPager->sectorSize = iSectorSize;
39091  }
39092
39093  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
39094  return rc;
39095}
39096
39097
39098/*
39099** Write the supplied master journal name into the journal file for pager
39100** pPager at the current location. The master journal name must be the last
39101** thing written to a journal file. If the pager is in full-sync mode, the
39102** journal file descriptor is advanced to the next sector boundary before
39103** anything is written. The format is:
39104**
39105**   + 4 bytes: PAGER_MJ_PGNO.
39106**   + N bytes: Master journal filename in utf-8.
39107**   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
39108**   + 4 bytes: Master journal name checksum.
39109**   + 8 bytes: aJournalMagic[].
39110**
39111** The master journal page checksum is the sum of the bytes in the master
39112** journal name, where each byte is interpreted as a signed 8-bit integer.
39113**
39114** If zMaster is a NULL pointer (occurs for a single database transaction),
39115** this call is a no-op.
39116*/
39117static int writeMasterJournal(Pager *pPager, const char *zMaster){
39118  int rc;                          /* Return code */
39119  int nMaster;                     /* Length of string zMaster */
39120  i64 iHdrOff;                     /* Offset of header in journal file */
39121  i64 jrnlSize;                    /* Size of journal file on disk */
39122  u32 cksum = 0;                   /* Checksum of string zMaster */
39123
39124  assert( pPager->setMaster==0 );
39125  assert( !pagerUseWal(pPager) );
39126
39127  if( !zMaster
39128   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
39129   || pPager->journalMode==PAGER_JOURNALMODE_OFF
39130  ){
39131    return SQLITE_OK;
39132  }
39133  pPager->setMaster = 1;
39134  assert( isOpen(pPager->jfd) );
39135  assert( pPager->journalHdr <= pPager->journalOff );
39136
39137  /* Calculate the length in bytes and the checksum of zMaster */
39138  for(nMaster=0; zMaster[nMaster]; nMaster++){
39139    cksum += zMaster[nMaster];
39140  }
39141
39142  /* If in full-sync mode, advance to the next disk sector before writing
39143  ** the master journal name. This is in case the previous page written to
39144  ** the journal has already been synced.
39145  */
39146  if( pPager->fullSync ){
39147    pPager->journalOff = journalHdrOffset(pPager);
39148  }
39149  iHdrOff = pPager->journalOff;
39150
39151  /* Write the master journal data to the end of the journal file. If
39152  ** an error occurs, return the error code to the caller.
39153  */
39154  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
39155   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
39156   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
39157   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
39158   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
39159  ){
39160    return rc;
39161  }
39162  pPager->journalOff += (nMaster+20);
39163
39164  /* If the pager is in peristent-journal mode, then the physical
39165  ** journal-file may extend past the end of the master-journal name
39166  ** and 8 bytes of magic data just written to the file. This is
39167  ** dangerous because the code to rollback a hot-journal file
39168  ** will not be able to find the master-journal name to determine
39169  ** whether or not the journal is hot.
39170  **
39171  ** Easiest thing to do in this scenario is to truncate the journal
39172  ** file to the required size.
39173  */
39174  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
39175   && jrnlSize>pPager->journalOff
39176  ){
39177    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
39178  }
39179  return rc;
39180}
39181
39182/*
39183** Find a page in the hash table given its page number. Return
39184** a pointer to the page or NULL if the requested page is not
39185** already in memory.
39186*/
39187static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
39188  PgHdr *p;                         /* Return value */
39189
39190  /* It is not possible for a call to PcacheFetch() with createFlag==0 to
39191  ** fail, since no attempt to allocate dynamic memory will be made.
39192  */
39193  (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
39194  return p;
39195}
39196
39197/*
39198** Discard the entire contents of the in-memory page-cache.
39199*/
39200static void pager_reset(Pager *pPager){
39201  sqlite3BackupRestart(pPager->pBackup);
39202  sqlite3PcacheClear(pPager->pPCache);
39203}
39204
39205/*
39206** Free all structures in the Pager.aSavepoint[] array and set both
39207** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
39208** if it is open and the pager is not in exclusive mode.
39209*/
39210static void releaseAllSavepoints(Pager *pPager){
39211  int ii;               /* Iterator for looping through Pager.aSavepoint */
39212  for(ii=0; ii<pPager->nSavepoint; ii++){
39213    sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
39214  }
39215  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
39216    sqlite3OsClose(pPager->sjfd);
39217  }
39218  sqlite3_free(pPager->aSavepoint);
39219  pPager->aSavepoint = 0;
39220  pPager->nSavepoint = 0;
39221  pPager->nSubRec = 0;
39222}
39223
39224/*
39225** Set the bit number pgno in the PagerSavepoint.pInSavepoint
39226** bitvecs of all open savepoints. Return SQLITE_OK if successful
39227** or SQLITE_NOMEM if a malloc failure occurs.
39228*/
39229static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
39230  int ii;                   /* Loop counter */
39231  int rc = SQLITE_OK;       /* Result code */
39232
39233  for(ii=0; ii<pPager->nSavepoint; ii++){
39234    PagerSavepoint *p = &pPager->aSavepoint[ii];
39235    if( pgno<=p->nOrig ){
39236      rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
39237      testcase( rc==SQLITE_NOMEM );
39238      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
39239    }
39240  }
39241  return rc;
39242}
39243
39244/*
39245** This function is a no-op if the pager is in exclusive mode and not
39246** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
39247** state.
39248**
39249** If the pager is not in exclusive-access mode, the database file is
39250** completely unlocked. If the file is unlocked and the file-system does
39251** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
39252** closed (if it is open).
39253**
39254** If the pager is in ERROR state when this function is called, the
39255** contents of the pager cache are discarded before switching back to
39256** the OPEN state. Regardless of whether the pager is in exclusive-mode
39257** or not, any journal file left in the file-system will be treated
39258** as a hot-journal and rolled back the next time a read-transaction
39259** is opened (by this or by any other connection).
39260*/
39261static void pager_unlock(Pager *pPager){
39262
39263  assert( pPager->eState==PAGER_READER
39264       || pPager->eState==PAGER_OPEN
39265       || pPager->eState==PAGER_ERROR
39266  );
39267
39268  sqlite3BitvecDestroy(pPager->pInJournal);
39269  pPager->pInJournal = 0;
39270  releaseAllSavepoints(pPager);
39271
39272  if( pagerUseWal(pPager) ){
39273    assert( !isOpen(pPager->jfd) );
39274    sqlite3WalEndReadTransaction(pPager->pWal);
39275    pPager->eState = PAGER_OPEN;
39276  }else if( !pPager->exclusiveMode ){
39277    int rc;                       /* Error code returned by pagerUnlockDb() */
39278    int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
39279
39280    /* If the operating system support deletion of open files, then
39281    ** close the journal file when dropping the database lock.  Otherwise
39282    ** another connection with journal_mode=delete might delete the file
39283    ** out from under us.
39284    */
39285    assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
39286    assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
39287    assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
39288    assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
39289    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
39290    assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
39291    if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
39292     || 1!=(pPager->journalMode & 5)
39293    ){
39294      sqlite3OsClose(pPager->jfd);
39295    }
39296
39297    /* If the pager is in the ERROR state and the call to unlock the database
39298    ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
39299    ** above the #define for UNKNOWN_LOCK for an explanation of why this
39300    ** is necessary.
39301    */
39302    rc = pagerUnlockDb(pPager, NO_LOCK);
39303    if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
39304      pPager->eLock = UNKNOWN_LOCK;
39305    }
39306
39307    /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
39308    ** without clearing the error code. This is intentional - the error
39309    ** code is cleared and the cache reset in the block below.
39310    */
39311    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
39312    pPager->changeCountDone = 0;
39313    pPager->eState = PAGER_OPEN;
39314  }
39315
39316  /* If Pager.errCode is set, the contents of the pager cache cannot be
39317  ** trusted. Now that there are no outstanding references to the pager,
39318  ** it can safely move back to PAGER_OPEN state. This happens in both
39319  ** normal and exclusive-locking mode.
39320  */
39321  if( pPager->errCode ){
39322    assert( !MEMDB );
39323    pager_reset(pPager);
39324    pPager->changeCountDone = pPager->tempFile;
39325    pPager->eState = PAGER_OPEN;
39326    pPager->errCode = SQLITE_OK;
39327  }
39328
39329  pPager->journalOff = 0;
39330  pPager->journalHdr = 0;
39331  pPager->setMaster = 0;
39332}
39333
39334/*
39335** This function is called whenever an IOERR or FULL error that requires
39336** the pager to transition into the ERROR state may ahve occurred.
39337** The first argument is a pointer to the pager structure, the second
39338** the error-code about to be returned by a pager API function. The
39339** value returned is a copy of the second argument to this function.
39340**
39341** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
39342** IOERR sub-codes, the pager enters the ERROR state and the error code
39343** is stored in Pager.errCode. While the pager remains in the ERROR state,
39344** all major API calls on the Pager will immediately return Pager.errCode.
39345**
39346** The ERROR state indicates that the contents of the pager-cache
39347** cannot be trusted. This state can be cleared by completely discarding
39348** the contents of the pager-cache. If a transaction was active when
39349** the persistent error occurred, then the rollback journal may need
39350** to be replayed to restore the contents of the database file (as if
39351** it were a hot-journal).
39352*/
39353static int pager_error(Pager *pPager, int rc){
39354  int rc2 = rc & 0xff;
39355  assert( rc==SQLITE_OK || !MEMDB );
39356  assert(
39357       pPager->errCode==SQLITE_FULL ||
39358       pPager->errCode==SQLITE_OK ||
39359       (pPager->errCode & 0xff)==SQLITE_IOERR
39360  );
39361  if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
39362    pPager->errCode = rc;
39363    pPager->eState = PAGER_ERROR;
39364  }
39365  return rc;
39366}
39367
39368/*
39369** This routine ends a transaction. A transaction is usually ended by
39370** either a COMMIT or a ROLLBACK operation. This routine may be called
39371** after rollback of a hot-journal, or if an error occurs while opening
39372** the journal file or writing the very first journal-header of a
39373** database transaction.
39374**
39375** This routine is never called in PAGER_ERROR state. If it is called
39376** in PAGER_NONE or PAGER_SHARED state and the lock held is less
39377** exclusive than a RESERVED lock, it is a no-op.
39378**
39379** Otherwise, any active savepoints are released.
39380**
39381** If the journal file is open, then it is "finalized". Once a journal
39382** file has been finalized it is not possible to use it to roll back a
39383** transaction. Nor will it be considered to be a hot-journal by this
39384** or any other database connection. Exactly how a journal is finalized
39385** depends on whether or not the pager is running in exclusive mode and
39386** the current journal-mode (Pager.journalMode value), as follows:
39387**
39388**   journalMode==MEMORY
39389**     Journal file descriptor is simply closed. This destroys an
39390**     in-memory journal.
39391**
39392**   journalMode==TRUNCATE
39393**     Journal file is truncated to zero bytes in size.
39394**
39395**   journalMode==PERSIST
39396**     The first 28 bytes of the journal file are zeroed. This invalidates
39397**     the first journal header in the file, and hence the entire journal
39398**     file. An invalid journal file cannot be rolled back.
39399**
39400**   journalMode==DELETE
39401**     The journal file is closed and deleted using sqlite3OsDelete().
39402**
39403**     If the pager is running in exclusive mode, this method of finalizing
39404**     the journal file is never used. Instead, if the journalMode is
39405**     DELETE and the pager is in exclusive mode, the method described under
39406**     journalMode==PERSIST is used instead.
39407**
39408** After the journal is finalized, the pager moves to PAGER_READER state.
39409** If running in non-exclusive rollback mode, the lock on the file is
39410** downgraded to a SHARED_LOCK.
39411**
39412** SQLITE_OK is returned if no error occurs. If an error occurs during
39413** any of the IO operations to finalize the journal file or unlock the
39414** database then the IO error code is returned to the user. If the
39415** operation to finalize the journal file fails, then the code still
39416** tries to unlock the database file if not in exclusive mode. If the
39417** unlock operation fails as well, then the first error code related
39418** to the first error encountered (the journal finalization one) is
39419** returned.
39420*/
39421static int pager_end_transaction(Pager *pPager, int hasMaster){
39422  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
39423  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
39424
39425  /* Do nothing if the pager does not have an open write transaction
39426  ** or at least a RESERVED lock. This function may be called when there
39427  ** is no write-transaction active but a RESERVED or greater lock is
39428  ** held under two circumstances:
39429  **
39430  **   1. After a successful hot-journal rollback, it is called with
39431  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
39432  **
39433  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
39434  **      lock switches back to locking_mode=normal and then executes a
39435  **      read-transaction, this function is called with eState==PAGER_READER
39436  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
39437  */
39438  assert( assert_pager_state(pPager) );
39439  assert( pPager->eState!=PAGER_ERROR );
39440  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
39441    return SQLITE_OK;
39442  }
39443
39444  releaseAllSavepoints(pPager);
39445  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
39446  if( isOpen(pPager->jfd) ){
39447    assert( !pagerUseWal(pPager) );
39448
39449    /* Finalize the journal file. */
39450    if( sqlite3IsMemJournal(pPager->jfd) ){
39451      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
39452      sqlite3OsClose(pPager->jfd);
39453    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
39454      if( pPager->journalOff==0 ){
39455        rc = SQLITE_OK;
39456      }else{
39457        rc = sqlite3OsTruncate(pPager->jfd, 0);
39458      }
39459      pPager->journalOff = 0;
39460    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
39461      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
39462    ){
39463      rc = zeroJournalHdr(pPager, hasMaster);
39464      pPager->journalOff = 0;
39465    }else{
39466      /* This branch may be executed with Pager.journalMode==MEMORY if
39467      ** a hot-journal was just rolled back. In this case the journal
39468      ** file should be closed and deleted. If this connection writes to
39469      ** the database file, it will do so using an in-memory journal.
39470      */
39471      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
39472           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
39473           || pPager->journalMode==PAGER_JOURNALMODE_WAL
39474      );
39475      sqlite3OsClose(pPager->jfd);
39476      if( !pPager->tempFile ){
39477        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
39478      }
39479    }
39480  }
39481
39482#ifdef SQLITE_CHECK_PAGES
39483  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
39484  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
39485    PgHdr *p = pager_lookup(pPager, 1);
39486    if( p ){
39487      p->pageHash = 0;
39488      sqlite3PagerUnref(p);
39489    }
39490  }
39491#endif
39492
39493  sqlite3BitvecDestroy(pPager->pInJournal);
39494  pPager->pInJournal = 0;
39495  pPager->nRec = 0;
39496  sqlite3PcacheCleanAll(pPager->pPCache);
39497  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
39498
39499  if( pagerUseWal(pPager) ){
39500    /* Drop the WAL write-lock, if any. Also, if the connection was in
39501    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
39502    ** lock held on the database file.
39503    */
39504    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
39505    assert( rc2==SQLITE_OK );
39506  }
39507  if( !pPager->exclusiveMode
39508   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
39509  ){
39510    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
39511    pPager->changeCountDone = 0;
39512  }
39513  pPager->eState = PAGER_READER;
39514  pPager->setMaster = 0;
39515
39516  return (rc==SQLITE_OK?rc2:rc);
39517}
39518
39519/*
39520** Execute a rollback if a transaction is active and unlock the
39521** database file.
39522**
39523** If the pager has already entered the ERROR state, do not attempt
39524** the rollback at this time. Instead, pager_unlock() is called. The
39525** call to pager_unlock() will discard all in-memory pages, unlock
39526** the database file and move the pager back to OPEN state. If this
39527** means that there is a hot-journal left in the file-system, the next
39528** connection to obtain a shared lock on the pager (which may be this one)
39529** will roll it back.
39530**
39531** If the pager has not already entered the ERROR state, but an IO or
39532** malloc error occurs during a rollback, then this will itself cause
39533** the pager to enter the ERROR state. Which will be cleared by the
39534** call to pager_unlock(), as described above.
39535*/
39536static void pagerUnlockAndRollback(Pager *pPager){
39537  if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
39538    assert( assert_pager_state(pPager) );
39539    if( pPager->eState>=PAGER_WRITER_LOCKED ){
39540      sqlite3BeginBenignMalloc();
39541      sqlite3PagerRollback(pPager);
39542      sqlite3EndBenignMalloc();
39543    }else if( !pPager->exclusiveMode ){
39544      assert( pPager->eState==PAGER_READER );
39545      pager_end_transaction(pPager, 0);
39546    }
39547  }
39548  pager_unlock(pPager);
39549}
39550
39551/*
39552** Parameter aData must point to a buffer of pPager->pageSize bytes
39553** of data. Compute and return a checksum based ont the contents of the
39554** page of data and the current value of pPager->cksumInit.
39555**
39556** This is not a real checksum. It is really just the sum of the
39557** random initial value (pPager->cksumInit) and every 200th byte
39558** of the page data, starting with byte offset (pPager->pageSize%200).
39559** Each byte is interpreted as an 8-bit unsigned integer.
39560**
39561** Changing the formula used to compute this checksum results in an
39562** incompatible journal file format.
39563**
39564** If journal corruption occurs due to a power failure, the most likely
39565** scenario is that one end or the other of the record will be changed.
39566** It is much less likely that the two ends of the journal record will be
39567** correct and the middle be corrupt.  Thus, this "checksum" scheme,
39568** though fast and simple, catches the mostly likely kind of corruption.
39569*/
39570static u32 pager_cksum(Pager *pPager, const u8 *aData){
39571  u32 cksum = pPager->cksumInit;         /* Checksum value to return */
39572  int i = pPager->pageSize-200;          /* Loop counter */
39573  while( i>0 ){
39574    cksum += aData[i];
39575    i -= 200;
39576  }
39577  return cksum;
39578}
39579
39580/*
39581** Report the current page size and number of reserved bytes back
39582** to the codec.
39583*/
39584#ifdef SQLITE_HAS_CODEC
39585static void pagerReportSize(Pager *pPager){
39586  if( pPager->xCodecSizeChng ){
39587    pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
39588                           (int)pPager->nReserve);
39589  }
39590}
39591#else
39592# define pagerReportSize(X)     /* No-op if we do not support a codec */
39593#endif
39594
39595/*
39596** Read a single page from either the journal file (if isMainJrnl==1) or
39597** from the sub-journal (if isMainJrnl==0) and playback that page.
39598** The page begins at offset *pOffset into the file. The *pOffset
39599** value is increased to the start of the next page in the journal.
39600**
39601** The main rollback journal uses checksums - the statement journal does
39602** not.
39603**
39604** If the page number of the page record read from the (sub-)journal file
39605** is greater than the current value of Pager.dbSize, then playback is
39606** skipped and SQLITE_OK is returned.
39607**
39608** If pDone is not NULL, then it is a record of pages that have already
39609** been played back.  If the page at *pOffset has already been played back
39610** (if the corresponding pDone bit is set) then skip the playback.
39611** Make sure the pDone bit corresponding to the *pOffset page is set
39612** prior to returning.
39613**
39614** If the page record is successfully read from the (sub-)journal file
39615** and played back, then SQLITE_OK is returned. If an IO error occurs
39616** while reading the record from the (sub-)journal file or while writing
39617** to the database file, then the IO error code is returned. If data
39618** is successfully read from the (sub-)journal file but appears to be
39619** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
39620** two circumstances:
39621**
39622**   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
39623**   * If the record is being rolled back from the main journal file
39624**     and the checksum field does not match the record content.
39625**
39626** Neither of these two scenarios are possible during a savepoint rollback.
39627**
39628** If this is a savepoint rollback, then memory may have to be dynamically
39629** allocated by this function. If this is the case and an allocation fails,
39630** SQLITE_NOMEM is returned.
39631*/
39632static int pager_playback_one_page(
39633  Pager *pPager,                /* The pager being played back */
39634  i64 *pOffset,                 /* Offset of record to playback */
39635  Bitvec *pDone,                /* Bitvec of pages already played back */
39636  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
39637  int isSavepnt                 /* True for a savepoint rollback */
39638){
39639  int rc;
39640  PgHdr *pPg;                   /* An existing page in the cache */
39641  Pgno pgno;                    /* The page number of a page in journal */
39642  u32 cksum;                    /* Checksum used for sanity checking */
39643  char *aData;                  /* Temporary storage for the page */
39644  sqlite3_file *jfd;            /* The file descriptor for the journal file */
39645  int isSynced;                 /* True if journal page is synced */
39646
39647  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
39648  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
39649  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
39650  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
39651
39652  aData = pPager->pTmpSpace;
39653  assert( aData );         /* Temp storage must have already been allocated */
39654  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
39655
39656  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
39657  ** or savepoint rollback done at the request of the caller) or this is
39658  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
39659  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
39660  ** only reads from the main journal, not the sub-journal.
39661  */
39662  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
39663       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
39664  );
39665  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
39666
39667  /* Read the page number and page data from the journal or sub-journal
39668  ** file. Return an error code to the caller if an IO error occurs.
39669  */
39670  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
39671  rc = read32bits(jfd, *pOffset, &pgno);
39672  if( rc!=SQLITE_OK ) return rc;
39673  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
39674  if( rc!=SQLITE_OK ) return rc;
39675  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
39676
39677  /* Sanity checking on the page.  This is more important that I originally
39678  ** thought.  If a power failure occurs while the journal is being written,
39679  ** it could cause invalid data to be written into the journal.  We need to
39680  ** detect this invalid data (with high probability) and ignore it.
39681  */
39682  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
39683    assert( !isSavepnt );
39684    return SQLITE_DONE;
39685  }
39686  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
39687    return SQLITE_OK;
39688  }
39689  if( isMainJrnl ){
39690    rc = read32bits(jfd, (*pOffset)-4, &cksum);
39691    if( rc ) return rc;
39692    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
39693      return SQLITE_DONE;
39694    }
39695  }
39696
39697  /* If this page has already been played by before during the current
39698  ** rollback, then don't bother to play it back again.
39699  */
39700  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
39701    return rc;
39702  }
39703
39704  /* When playing back page 1, restore the nReserve setting
39705  */
39706  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
39707    pPager->nReserve = ((u8*)aData)[20];
39708    pagerReportSize(pPager);
39709  }
39710
39711  /* If the pager is in CACHEMOD state, then there must be a copy of this
39712  ** page in the pager cache. In this case just update the pager cache,
39713  ** not the database file. The page is left marked dirty in this case.
39714  **
39715  ** An exception to the above rule: If the database is in no-sync mode
39716  ** and a page is moved during an incremental vacuum then the page may
39717  ** not be in the pager cache. Later: if a malloc() or IO error occurs
39718  ** during a Movepage() call, then the page may not be in the cache
39719  ** either. So the condition described in the above paragraph is not
39720  ** assert()able.
39721  **
39722  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
39723  ** pager cache if it exists and the main file. The page is then marked
39724  ** not dirty. Since this code is only executed in PAGER_OPEN state for
39725  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
39726  ** if the pager is in OPEN state.
39727  **
39728  ** Ticket #1171:  The statement journal might contain page content that is
39729  ** different from the page content at the start of the transaction.
39730  ** This occurs when a page is changed prior to the start of a statement
39731  ** then changed again within the statement.  When rolling back such a
39732  ** statement we must not write to the original database unless we know
39733  ** for certain that original page contents are synced into the main rollback
39734  ** journal.  Otherwise, a power loss might leave modified data in the
39735  ** database file without an entry in the rollback journal that can
39736  ** restore the database to its original form.  Two conditions must be
39737  ** met before writing to the database files. (1) the database must be
39738  ** locked.  (2) we know that the original page content is fully synced
39739  ** in the main journal either because the page is not in cache or else
39740  ** the page is marked as needSync==0.
39741  **
39742  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
39743  ** is possible to fail a statement on a database that does not yet exist.
39744  ** Do not attempt to write if database file has never been opened.
39745  */
39746  if( pagerUseWal(pPager) ){
39747    pPg = 0;
39748  }else{
39749    pPg = pager_lookup(pPager, pgno);
39750  }
39751  assert( pPg || !MEMDB );
39752  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
39753  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
39754           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
39755           (isMainJrnl?"main-journal":"sub-journal")
39756  ));
39757  if( isMainJrnl ){
39758    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
39759  }else{
39760    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
39761  }
39762  if( isOpen(pPager->fd)
39763   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
39764   && isSynced
39765  ){
39766    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
39767    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
39768    assert( !pagerUseWal(pPager) );
39769    rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
39770    if( pgno>pPager->dbFileSize ){
39771      pPager->dbFileSize = pgno;
39772    }
39773    if( pPager->pBackup ){
39774      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
39775      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
39776      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
39777    }
39778  }else if( !isMainJrnl && pPg==0 ){
39779    /* If this is a rollback of a savepoint and data was not written to
39780    ** the database and the page is not in-memory, there is a potential
39781    ** problem. When the page is next fetched by the b-tree layer, it
39782    ** will be read from the database file, which may or may not be
39783    ** current.
39784    **
39785    ** There are a couple of different ways this can happen. All are quite
39786    ** obscure. When running in synchronous mode, this can only happen
39787    ** if the page is on the free-list at the start of the transaction, then
39788    ** populated, then moved using sqlite3PagerMovepage().
39789    **
39790    ** The solution is to add an in-memory page to the cache containing
39791    ** the data just read from the sub-journal. Mark the page as dirty
39792    ** and if the pager requires a journal-sync, then mark the page as
39793    ** requiring a journal-sync before it is written.
39794    */
39795    assert( isSavepnt );
39796    assert( pPager->doNotSpill==0 );
39797    pPager->doNotSpill++;
39798    rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
39799    assert( pPager->doNotSpill==1 );
39800    pPager->doNotSpill--;
39801    if( rc!=SQLITE_OK ) return rc;
39802    pPg->flags &= ~PGHDR_NEED_READ;
39803    sqlite3PcacheMakeDirty(pPg);
39804  }
39805  if( pPg ){
39806    /* No page should ever be explicitly rolled back that is in use, except
39807    ** for page 1 which is held in use in order to keep the lock on the
39808    ** database active. However such a page may be rolled back as a result
39809    ** of an internal error resulting in an automatic call to
39810    ** sqlite3PagerRollback().
39811    */
39812    void *pData;
39813    pData = pPg->pData;
39814    memcpy(pData, (u8*)aData, pPager->pageSize);
39815    pPager->xReiniter(pPg);
39816    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
39817      /* If the contents of this page were just restored from the main
39818      ** journal file, then its content must be as they were when the
39819      ** transaction was first opened. In this case we can mark the page
39820      ** as clean, since there will be no need to write it out to the
39821      ** database.
39822      **
39823      ** There is one exception to this rule. If the page is being rolled
39824      ** back as part of a savepoint (or statement) rollback from an
39825      ** unsynced portion of the main journal file, then it is not safe
39826      ** to mark the page as clean. This is because marking the page as
39827      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
39828      ** already in the journal file (recorded in Pager.pInJournal) and
39829      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
39830      ** again within this transaction, it will be marked as dirty but
39831      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
39832      ** be written out into the database file before its journal file
39833      ** segment is synced. If a crash occurs during or following this,
39834      ** database corruption may ensue.
39835      */
39836      assert( !pagerUseWal(pPager) );
39837      sqlite3PcacheMakeClean(pPg);
39838    }
39839    pager_set_pagehash(pPg);
39840
39841    /* If this was page 1, then restore the value of Pager.dbFileVers.
39842    ** Do this before any decoding. */
39843    if( pgno==1 ){
39844      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
39845    }
39846
39847    /* Decode the page just read from disk */
39848    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
39849    sqlite3PcacheRelease(pPg);
39850  }
39851  return rc;
39852}
39853
39854/*
39855** Parameter zMaster is the name of a master journal file. A single journal
39856** file that referred to the master journal file has just been rolled back.
39857** This routine checks if it is possible to delete the master journal file,
39858** and does so if it is.
39859**
39860** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
39861** available for use within this function.
39862**
39863** When a master journal file is created, it is populated with the names
39864** of all of its child journals, one after another, formatted as utf-8
39865** encoded text. The end of each child journal file is marked with a
39866** nul-terminator byte (0x00). i.e. the entire contents of a master journal
39867** file for a transaction involving two databases might be:
39868**
39869**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
39870**
39871** A master journal file may only be deleted once all of its child
39872** journals have been rolled back.
39873**
39874** This function reads the contents of the master-journal file into
39875** memory and loops through each of the child journal names. For
39876** each child journal, it checks if:
39877**
39878**   * if the child journal exists, and if so
39879**   * if the child journal contains a reference to master journal
39880**     file zMaster
39881**
39882** If a child journal can be found that matches both of the criteria
39883** above, this function returns without doing anything. Otherwise, if
39884** no such child journal can be found, file zMaster is deleted from
39885** the file-system using sqlite3OsDelete().
39886**
39887** If an IO error within this function, an error code is returned. This
39888** function allocates memory by calling sqlite3Malloc(). If an allocation
39889** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
39890** occur, SQLITE_OK is returned.
39891**
39892** TODO: This function allocates a single block of memory to load
39893** the entire contents of the master journal file. This could be
39894** a couple of kilobytes or so - potentially larger than the page
39895** size.
39896*/
39897static int pager_delmaster(Pager *pPager, const char *zMaster){
39898  sqlite3_vfs *pVfs = pPager->pVfs;
39899  int rc;                   /* Return code */
39900  sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
39901  sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
39902  char *zMasterJournal = 0; /* Contents of master journal file */
39903  i64 nMasterJournal;       /* Size of master journal file */
39904  char *zJournal;           /* Pointer to one journal within MJ file */
39905  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
39906  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
39907
39908  /* Allocate space for both the pJournal and pMaster file descriptors.
39909  ** If successful, open the master journal file for reading.
39910  */
39911  pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
39912  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
39913  if( !pMaster ){
39914    rc = SQLITE_NOMEM;
39915  }else{
39916    const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
39917    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
39918  }
39919  if( rc!=SQLITE_OK ) goto delmaster_out;
39920
39921  /* Load the entire master journal file into space obtained from
39922  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
39923  ** sufficient space (in zMasterPtr) to hold the names of master
39924  ** journal files extracted from regular rollback-journals.
39925  */
39926  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
39927  if( rc!=SQLITE_OK ) goto delmaster_out;
39928  nMasterPtr = pVfs->mxPathname+1;
39929  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
39930  if( !zMasterJournal ){
39931    rc = SQLITE_NOMEM;
39932    goto delmaster_out;
39933  }
39934  zMasterPtr = &zMasterJournal[nMasterJournal+1];
39935  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
39936  if( rc!=SQLITE_OK ) goto delmaster_out;
39937  zMasterJournal[nMasterJournal] = 0;
39938
39939  zJournal = zMasterJournal;
39940  while( (zJournal-zMasterJournal)<nMasterJournal ){
39941    int exists;
39942    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
39943    if( rc!=SQLITE_OK ){
39944      goto delmaster_out;
39945    }
39946    if( exists ){
39947      /* One of the journals pointed to by the master journal exists.
39948      ** Open it and check if it points at the master journal. If
39949      ** so, return without deleting the master journal file.
39950      */
39951      int c;
39952      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
39953      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
39954      if( rc!=SQLITE_OK ){
39955        goto delmaster_out;
39956      }
39957
39958      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
39959      sqlite3OsClose(pJournal);
39960      if( rc!=SQLITE_OK ){
39961        goto delmaster_out;
39962      }
39963
39964      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
39965      if( c ){
39966        /* We have a match. Do not delete the master journal file. */
39967        goto delmaster_out;
39968      }
39969    }
39970    zJournal += (sqlite3Strlen30(zJournal)+1);
39971  }
39972
39973  sqlite3OsClose(pMaster);
39974  rc = sqlite3OsDelete(pVfs, zMaster, 0);
39975
39976delmaster_out:
39977  sqlite3_free(zMasterJournal);
39978  if( pMaster ){
39979    sqlite3OsClose(pMaster);
39980    assert( !isOpen(pJournal) );
39981    sqlite3_free(pMaster);
39982  }
39983  return rc;
39984}
39985
39986
39987/*
39988** This function is used to change the actual size of the database
39989** file in the file-system. This only happens when committing a transaction,
39990** or rolling back a transaction (including rolling back a hot-journal).
39991**
39992** If the main database file is not open, or the pager is not in either
39993** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
39994** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
39995** If the file on disk is currently larger than nPage pages, then use the VFS
39996** xTruncate() method to truncate it.
39997**
39998** Or, it might might be the case that the file on disk is smaller than
39999** nPage pages. Some operating system implementations can get confused if
40000** you try to truncate a file to some size that is larger than it
40001** currently is, so detect this case and write a single zero byte to
40002** the end of the new file instead.
40003**
40004** If successful, return SQLITE_OK. If an IO error occurs while modifying
40005** the database file, return the error code to the caller.
40006*/
40007static int pager_truncate(Pager *pPager, Pgno nPage){
40008  int rc = SQLITE_OK;
40009  assert( pPager->eState!=PAGER_ERROR );
40010  assert( pPager->eState!=PAGER_READER );
40011
40012  if( isOpen(pPager->fd)
40013   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40014  ){
40015    i64 currentSize, newSize;
40016    int szPage = pPager->pageSize;
40017    assert( pPager->eLock==EXCLUSIVE_LOCK );
40018    /* TODO: Is it safe to use Pager.dbFileSize here? */
40019    rc = sqlite3OsFileSize(pPager->fd, &currentSize);
40020    newSize = szPage*(i64)nPage;
40021    if( rc==SQLITE_OK && currentSize!=newSize ){
40022      if( currentSize>newSize ){
40023        rc = sqlite3OsTruncate(pPager->fd, newSize);
40024      }else{
40025        char *pTmp = pPager->pTmpSpace;
40026        memset(pTmp, 0, szPage);
40027        testcase( (newSize-szPage) <  currentSize );
40028        testcase( (newSize-szPage) == currentSize );
40029        testcase( (newSize-szPage) >  currentSize );
40030        rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
40031      }
40032      if( rc==SQLITE_OK ){
40033        pPager->dbFileSize = nPage;
40034      }
40035    }
40036  }
40037  return rc;
40038}
40039
40040/*
40041** Set the value of the Pager.sectorSize variable for the given
40042** pager based on the value returned by the xSectorSize method
40043** of the open database file. The sector size will be used used
40044** to determine the size and alignment of journal header and
40045** master journal pointers within created journal files.
40046**
40047** For temporary files the effective sector size is always 512 bytes.
40048**
40049** Otherwise, for non-temporary files, the effective sector size is
40050** the value returned by the xSectorSize() method rounded up to 32 if
40051** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
40052** is greater than MAX_SECTOR_SIZE.
40053*/
40054static void setSectorSize(Pager *pPager){
40055  assert( isOpen(pPager->fd) || pPager->tempFile );
40056
40057  if( !pPager->tempFile ){
40058    /* Sector size doesn't matter for temporary files. Also, the file
40059    ** may not have been opened yet, in which case the OsSectorSize()
40060    ** call will segfault.
40061    */
40062    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
40063  }
40064  if( pPager->sectorSize<32 ){
40065    pPager->sectorSize = 512;
40066  }
40067  if( pPager->sectorSize>MAX_SECTOR_SIZE ){
40068    assert( MAX_SECTOR_SIZE>=512 );
40069    pPager->sectorSize = MAX_SECTOR_SIZE;
40070  }
40071}
40072
40073/*
40074** Playback the journal and thus restore the database file to
40075** the state it was in before we started making changes.
40076**
40077** The journal file format is as follows:
40078**
40079**  (1)  8 byte prefix.  A copy of aJournalMagic[].
40080**  (2)  4 byte big-endian integer which is the number of valid page records
40081**       in the journal.  If this value is 0xffffffff, then compute the
40082**       number of page records from the journal size.
40083**  (3)  4 byte big-endian integer which is the initial value for the
40084**       sanity checksum.
40085**  (4)  4 byte integer which is the number of pages to truncate the
40086**       database to during a rollback.
40087**  (5)  4 byte big-endian integer which is the sector size.  The header
40088**       is this many bytes in size.
40089**  (6)  4 byte big-endian integer which is the page size.
40090**  (7)  zero padding out to the next sector size.
40091**  (8)  Zero or more pages instances, each as follows:
40092**        +  4 byte page number.
40093**        +  pPager->pageSize bytes of data.
40094**        +  4 byte checksum
40095**
40096** When we speak of the journal header, we mean the first 7 items above.
40097** Each entry in the journal is an instance of the 8th item.
40098**
40099** Call the value from the second bullet "nRec".  nRec is the number of
40100** valid page entries in the journal.  In most cases, you can compute the
40101** value of nRec from the size of the journal file.  But if a power
40102** failure occurred while the journal was being written, it could be the
40103** case that the size of the journal file had already been increased but
40104** the extra entries had not yet made it safely to disk.  In such a case,
40105** the value of nRec computed from the file size would be too large.  For
40106** that reason, we always use the nRec value in the header.
40107**
40108** If the nRec value is 0xffffffff it means that nRec should be computed
40109** from the file size.  This value is used when the user selects the
40110** no-sync option for the journal.  A power failure could lead to corruption
40111** in this case.  But for things like temporary table (which will be
40112** deleted when the power is restored) we don't care.
40113**
40114** If the file opened as the journal file is not a well-formed
40115** journal file then all pages up to the first corrupted page are rolled
40116** back (or no pages if the journal header is corrupted). The journal file
40117** is then deleted and SQLITE_OK returned, just as if no corruption had
40118** been encountered.
40119**
40120** If an I/O or malloc() error occurs, the journal-file is not deleted
40121** and an error code is returned.
40122**
40123** The isHot parameter indicates that we are trying to rollback a journal
40124** that might be a hot journal.  Or, it could be that the journal is
40125** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
40126** If the journal really is hot, reset the pager cache prior rolling
40127** back any content.  If the journal is merely persistent, no reset is
40128** needed.
40129*/
40130static int pager_playback(Pager *pPager, int isHot){
40131  sqlite3_vfs *pVfs = pPager->pVfs;
40132  i64 szJ;                 /* Size of the journal file in bytes */
40133  u32 nRec;                /* Number of Records in the journal */
40134  u32 u;                   /* Unsigned loop counter */
40135  Pgno mxPg = 0;           /* Size of the original file in pages */
40136  int rc;                  /* Result code of a subroutine */
40137  int res = 1;             /* Value returned by sqlite3OsAccess() */
40138  char *zMaster = 0;       /* Name of master journal file if any */
40139  int needPagerReset;      /* True to reset page prior to first page rollback */
40140
40141  /* Figure out how many records are in the journal.  Abort early if
40142  ** the journal is empty.
40143  */
40144  assert( isOpen(pPager->jfd) );
40145  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
40146  if( rc!=SQLITE_OK ){
40147    goto end_playback;
40148  }
40149
40150  /* Read the master journal name from the journal, if it is present.
40151  ** If a master journal file name is specified, but the file is not
40152  ** present on disk, then the journal is not hot and does not need to be
40153  ** played back.
40154  **
40155  ** TODO: Technically the following is an error because it assumes that
40156  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
40157  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
40158  **  mxPathname is 512, which is the same as the minimum allowable value
40159  ** for pageSize.
40160  */
40161  zMaster = pPager->pTmpSpace;
40162  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
40163  if( rc==SQLITE_OK && zMaster[0] ){
40164    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
40165  }
40166  zMaster = 0;
40167  if( rc!=SQLITE_OK || !res ){
40168    goto end_playback;
40169  }
40170  pPager->journalOff = 0;
40171  needPagerReset = isHot;
40172
40173  /* This loop terminates either when a readJournalHdr() or
40174  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
40175  ** occurs.
40176  */
40177  while( 1 ){
40178    /* Read the next journal header from the journal file.  If there are
40179    ** not enough bytes left in the journal file for a complete header, or
40180    ** it is corrupted, then a process must have failed while writing it.
40181    ** This indicates nothing more needs to be rolled back.
40182    */
40183    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
40184    if( rc!=SQLITE_OK ){
40185      if( rc==SQLITE_DONE ){
40186        rc = SQLITE_OK;
40187      }
40188      goto end_playback;
40189    }
40190
40191    /* If nRec is 0xffffffff, then this journal was created by a process
40192    ** working in no-sync mode. This means that the rest of the journal
40193    ** file consists of pages, there are no more journal headers. Compute
40194    ** the value of nRec based on this assumption.
40195    */
40196    if( nRec==0xffffffff ){
40197      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
40198      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
40199    }
40200
40201    /* If nRec is 0 and this rollback is of a transaction created by this
40202    ** process and if this is the final header in the journal, then it means
40203    ** that this part of the journal was being filled but has not yet been
40204    ** synced to disk.  Compute the number of pages based on the remaining
40205    ** size of the file.
40206    **
40207    ** The third term of the test was added to fix ticket #2565.
40208    ** When rolling back a hot journal, nRec==0 always means that the next
40209    ** chunk of the journal contains zero pages to be rolled back.  But
40210    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
40211    ** the journal, it means that the journal might contain additional
40212    ** pages that need to be rolled back and that the number of pages
40213    ** should be computed based on the journal file size.
40214    */
40215    if( nRec==0 && !isHot &&
40216        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
40217      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
40218    }
40219
40220    /* If this is the first header read from the journal, truncate the
40221    ** database file back to its original size.
40222    */
40223    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
40224      rc = pager_truncate(pPager, mxPg);
40225      if( rc!=SQLITE_OK ){
40226        goto end_playback;
40227      }
40228      pPager->dbSize = mxPg;
40229    }
40230
40231    /* Copy original pages out of the journal and back into the
40232    ** database file and/or page cache.
40233    */
40234    for(u=0; u<nRec; u++){
40235      if( needPagerReset ){
40236        pager_reset(pPager);
40237        needPagerReset = 0;
40238      }
40239      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40240      if( rc!=SQLITE_OK ){
40241        if( rc==SQLITE_DONE ){
40242          rc = SQLITE_OK;
40243          pPager->journalOff = szJ;
40244          break;
40245        }else if( rc==SQLITE_IOERR_SHORT_READ ){
40246          /* If the journal has been truncated, simply stop reading and
40247          ** processing the journal. This might happen if the journal was
40248          ** not completely written and synced prior to a crash.  In that
40249          ** case, the database should have never been written in the
40250          ** first place so it is OK to simply abandon the rollback. */
40251          rc = SQLITE_OK;
40252          goto end_playback;
40253        }else{
40254          /* If we are unable to rollback, quit and return the error
40255          ** code.  This will cause the pager to enter the error state
40256          ** so that no further harm will be done.  Perhaps the next
40257          ** process to come along will be able to rollback the database.
40258          */
40259          goto end_playback;
40260        }
40261      }
40262    }
40263  }
40264  /*NOTREACHED*/
40265  assert( 0 );
40266
40267end_playback:
40268  /* Following a rollback, the database file should be back in its original
40269  ** state prior to the start of the transaction, so invoke the
40270  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
40271  ** assertion that the transaction counter was modified.
40272  */
40273  assert(
40274    pPager->fd->pMethods==0 ||
40275    sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
40276  );
40277
40278  /* If this playback is happening automatically as a result of an IO or
40279  ** malloc error that occurred after the change-counter was updated but
40280  ** before the transaction was committed, then the change-counter
40281  ** modification may just have been reverted. If this happens in exclusive
40282  ** mode, then subsequent transactions performed by the connection will not
40283  ** update the change-counter at all. This may lead to cache inconsistency
40284  ** problems for other processes at some point in the future. So, just
40285  ** in case this has happened, clear the changeCountDone flag now.
40286  */
40287  pPager->changeCountDone = pPager->tempFile;
40288
40289  if( rc==SQLITE_OK ){
40290    zMaster = pPager->pTmpSpace;
40291    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
40292    testcase( rc!=SQLITE_OK );
40293  }
40294  if( rc==SQLITE_OK
40295   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40296  ){
40297    rc = sqlite3PagerSync(pPager);
40298  }
40299  if( rc==SQLITE_OK ){
40300    rc = pager_end_transaction(pPager, zMaster[0]!='\0');
40301    testcase( rc!=SQLITE_OK );
40302  }
40303  if( rc==SQLITE_OK && zMaster[0] && res ){
40304    /* If there was a master journal and this routine will return success,
40305    ** see if it is possible to delete the master journal.
40306    */
40307    rc = pager_delmaster(pPager, zMaster);
40308    testcase( rc!=SQLITE_OK );
40309  }
40310
40311  /* The Pager.sectorSize variable may have been updated while rolling
40312  ** back a journal created by a process with a different sector size
40313  ** value. Reset it to the correct value for this process.
40314  */
40315  setSectorSize(pPager);
40316  return rc;
40317}
40318
40319
40320/*
40321** Read the content for page pPg out of the database file and into
40322** pPg->pData. A shared lock or greater must be held on the database
40323** file before this function is called.
40324**
40325** If page 1 is read, then the value of Pager.dbFileVers[] is set to
40326** the value read from the database file.
40327**
40328** If an IO error occurs, then the IO error is returned to the caller.
40329** Otherwise, SQLITE_OK is returned.
40330*/
40331static int readDbPage(PgHdr *pPg){
40332  Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
40333  Pgno pgno = pPg->pgno;       /* Page number to read */
40334  int rc = SQLITE_OK;          /* Return code */
40335  int isInWal = 0;             /* True if page is in log file */
40336  int pgsz = pPager->pageSize; /* Number of bytes to read */
40337
40338  assert( pPager->eState>=PAGER_READER && !MEMDB );
40339  assert( isOpen(pPager->fd) );
40340
40341  if( NEVER(!isOpen(pPager->fd)) ){
40342    assert( pPager->tempFile );
40343    memset(pPg->pData, 0, pPager->pageSize);
40344    return SQLITE_OK;
40345  }
40346
40347  if( pagerUseWal(pPager) ){
40348    /* Try to pull the page from the write-ahead log. */
40349    rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
40350  }
40351  if( rc==SQLITE_OK && !isInWal ){
40352    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
40353    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
40354    if( rc==SQLITE_IOERR_SHORT_READ ){
40355      rc = SQLITE_OK;
40356    }
40357  }
40358
40359  if( pgno==1 ){
40360    if( rc ){
40361      /* If the read is unsuccessful, set the dbFileVers[] to something
40362      ** that will never be a valid file version.  dbFileVers[] is a copy
40363      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
40364      ** zero or the size of the database in page. Bytes 32..35 and 35..39
40365      ** should be page numbers which are never 0xffffffff.  So filling
40366      ** pPager->dbFileVers[] with all 0xff bytes should suffice.
40367      **
40368      ** For an encrypted database, the situation is more complex:  bytes
40369      ** 24..39 of the database are white noise.  But the probability of
40370      ** white noising equaling 16 bytes of 0xff is vanishingly small so
40371      ** we should still be ok.
40372      */
40373      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
40374    }else{
40375      u8 *dbFileVers = &((u8*)pPg->pData)[24];
40376      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
40377    }
40378  }
40379  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
40380
40381  PAGER_INCR(sqlite3_pager_readdb_count);
40382  PAGER_INCR(pPager->nRead);
40383  IOTRACE(("PGIN %p %d\n", pPager, pgno));
40384  PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
40385               PAGERID(pPager), pgno, pager_pagehash(pPg)));
40386
40387  return rc;
40388}
40389
40390/*
40391** Update the value of the change-counter at offsets 24 and 92 in
40392** the header and the sqlite version number at offset 96.
40393**
40394** This is an unconditional update.  See also the pager_incr_changecounter()
40395** routine which only updates the change-counter if the update is actually
40396** needed, as determined by the pPager->changeCountDone state variable.
40397*/
40398static void pager_write_changecounter(PgHdr *pPg){
40399  u32 change_counter;
40400
40401  /* Increment the value just read and write it back to byte 24. */
40402  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
40403  put32bits(((char*)pPg->pData)+24, change_counter);
40404
40405  /* Also store the SQLite version number in bytes 96..99 and in
40406  ** bytes 92..95 store the change counter for which the version number
40407  ** is valid. */
40408  put32bits(((char*)pPg->pData)+92, change_counter);
40409  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
40410}
40411
40412#ifndef SQLITE_OMIT_WAL
40413/*
40414** This function is invoked once for each page that has already been
40415** written into the log file when a WAL transaction is rolled back.
40416** Parameter iPg is the page number of said page. The pCtx argument
40417** is actually a pointer to the Pager structure.
40418**
40419** If page iPg is present in the cache, and has no outstanding references,
40420** it is discarded. Otherwise, if there are one or more outstanding
40421** references, the page content is reloaded from the database. If the
40422** attempt to reload content from the database is required and fails,
40423** return an SQLite error code. Otherwise, SQLITE_OK.
40424*/
40425static int pagerUndoCallback(void *pCtx, Pgno iPg){
40426  int rc = SQLITE_OK;
40427  Pager *pPager = (Pager *)pCtx;
40428  PgHdr *pPg;
40429
40430  pPg = sqlite3PagerLookup(pPager, iPg);
40431  if( pPg ){
40432    if( sqlite3PcachePageRefcount(pPg)==1 ){
40433      sqlite3PcacheDrop(pPg);
40434    }else{
40435      rc = readDbPage(pPg);
40436      if( rc==SQLITE_OK ){
40437        pPager->xReiniter(pPg);
40438      }
40439      sqlite3PagerUnref(pPg);
40440    }
40441  }
40442
40443  /* Normally, if a transaction is rolled back, any backup processes are
40444  ** updated as data is copied out of the rollback journal and into the
40445  ** database. This is not generally possible with a WAL database, as
40446  ** rollback involves simply truncating the log file. Therefore, if one
40447  ** or more frames have already been written to the log (and therefore
40448  ** also copied into the backup databases) as part of this transaction,
40449  ** the backups must be restarted.
40450  */
40451  sqlite3BackupRestart(pPager->pBackup);
40452
40453  return rc;
40454}
40455
40456/*
40457** This function is called to rollback a transaction on a WAL database.
40458*/
40459static int pagerRollbackWal(Pager *pPager){
40460  int rc;                         /* Return Code */
40461  PgHdr *pList;                   /* List of dirty pages to revert */
40462
40463  /* For all pages in the cache that are currently dirty or have already
40464  ** been written (but not committed) to the log file, do one of the
40465  ** following:
40466  **
40467  **   + Discard the cached page (if refcount==0), or
40468  **   + Reload page content from the database (if refcount>0).
40469  */
40470  pPager->dbSize = pPager->dbOrigSize;
40471  rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
40472  pList = sqlite3PcacheDirtyList(pPager->pPCache);
40473  while( pList && rc==SQLITE_OK ){
40474    PgHdr *pNext = pList->pDirty;
40475    rc = pagerUndoCallback((void *)pPager, pList->pgno);
40476    pList = pNext;
40477  }
40478
40479  return rc;
40480}
40481
40482/*
40483** This function is a wrapper around sqlite3WalFrames(). As well as logging
40484** the contents of the list of pages headed by pList (connected by pDirty),
40485** this function notifies any active backup processes that the pages have
40486** changed.
40487**
40488** The list of pages passed into this routine is always sorted by page number.
40489** Hence, if page 1 appears anywhere on the list, it will be the first page.
40490*/
40491static int pagerWalFrames(
40492  Pager *pPager,                  /* Pager object */
40493  PgHdr *pList,                   /* List of frames to log */
40494  Pgno nTruncate,                 /* Database size after this commit */
40495  int isCommit,                   /* True if this is a commit */
40496  int syncFlags                   /* Flags to pass to OsSync() (or 0) */
40497){
40498  int rc;                         /* Return code */
40499#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
40500  PgHdr *p;                       /* For looping over pages */
40501#endif
40502
40503  assert( pPager->pWal );
40504#ifdef SQLITE_DEBUG
40505  /* Verify that the page list is in accending order */
40506  for(p=pList; p && p->pDirty; p=p->pDirty){
40507    assert( p->pgno < p->pDirty->pgno );
40508  }
40509#endif
40510
40511  if( isCommit ){
40512    /* If a WAL transaction is being committed, there is no point in writing
40513    ** any pages with page numbers greater than nTruncate into the WAL file.
40514    ** They will never be read by any client. So remove them from the pDirty
40515    ** list here. */
40516    PgHdr *p;
40517    PgHdr **ppNext = &pList;
40518    for(p=pList; (*ppNext = p); p=p->pDirty){
40519      if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
40520    }
40521    assert( pList );
40522  }
40523
40524  if( pList->pgno==1 ) pager_write_changecounter(pList);
40525  rc = sqlite3WalFrames(pPager->pWal,
40526      pPager->pageSize, pList, nTruncate, isCommit, syncFlags
40527  );
40528  if( rc==SQLITE_OK && pPager->pBackup ){
40529    PgHdr *p;
40530    for(p=pList; p; p=p->pDirty){
40531      sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
40532    }
40533  }
40534
40535#ifdef SQLITE_CHECK_PAGES
40536  pList = sqlite3PcacheDirtyList(pPager->pPCache);
40537  for(p=pList; p; p=p->pDirty){
40538    pager_set_pagehash(p);
40539  }
40540#endif
40541
40542  return rc;
40543}
40544
40545/*
40546** Begin a read transaction on the WAL.
40547**
40548** This routine used to be called "pagerOpenSnapshot()" because it essentially
40549** makes a snapshot of the database at the current point in time and preserves
40550** that snapshot for use by the reader in spite of concurrently changes by
40551** other writers or checkpointers.
40552*/
40553static int pagerBeginReadTransaction(Pager *pPager){
40554  int rc;                         /* Return code */
40555  int changed = 0;                /* True if cache must be reset */
40556
40557  assert( pagerUseWal(pPager) );
40558  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
40559
40560  /* sqlite3WalEndReadTransaction() was not called for the previous
40561  ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
40562  ** are in locking_mode=NORMAL and EndRead() was previously called,
40563  ** the duplicate call is harmless.
40564  */
40565  sqlite3WalEndReadTransaction(pPager->pWal);
40566
40567  rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
40568  if( rc!=SQLITE_OK || changed ){
40569    pager_reset(pPager);
40570  }
40571
40572  return rc;
40573}
40574#endif
40575
40576/*
40577** This function is called as part of the transition from PAGER_OPEN
40578** to PAGER_READER state to determine the size of the database file
40579** in pages (assuming the page size currently stored in Pager.pageSize).
40580**
40581** If no error occurs, SQLITE_OK is returned and the size of the database
40582** in pages is stored in *pnPage. Otherwise, an error code (perhaps
40583** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
40584*/
40585static int pagerPagecount(Pager *pPager, Pgno *pnPage){
40586  Pgno nPage;                     /* Value to return via *pnPage */
40587
40588  /* Query the WAL sub-system for the database size. The WalDbsize()
40589  ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
40590  ** if the database size is not available. The database size is not
40591  ** available from the WAL sub-system if the log file is empty or
40592  ** contains no valid committed transactions.
40593  */
40594  assert( pPager->eState==PAGER_OPEN );
40595  assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
40596  nPage = sqlite3WalDbsize(pPager->pWal);
40597
40598  /* If the database size was not available from the WAL sub-system,
40599  ** determine it based on the size of the database file. If the size
40600  ** of the database file is not an integer multiple of the page-size,
40601  ** round down to the nearest page. Except, any file larger than 0
40602  ** bytes in size is considered to contain at least one page.
40603  */
40604  if( nPage==0 ){
40605    i64 n = 0;                    /* Size of db file in bytes */
40606    assert( isOpen(pPager->fd) || pPager->tempFile );
40607    if( isOpen(pPager->fd) ){
40608      int rc = sqlite3OsFileSize(pPager->fd, &n);
40609      if( rc!=SQLITE_OK ){
40610        return rc;
40611      }
40612    }
40613    nPage = (Pgno)(n / pPager->pageSize);
40614    if( nPage==0 && n>0 ){
40615      nPage = 1;
40616    }
40617  }
40618
40619  /* If the current number of pages in the file is greater than the
40620  ** configured maximum pager number, increase the allowed limit so
40621  ** that the file can be read.
40622  */
40623  if( nPage>pPager->mxPgno ){
40624    pPager->mxPgno = (Pgno)nPage;
40625  }
40626
40627  *pnPage = nPage;
40628  return SQLITE_OK;
40629}
40630
40631#ifndef SQLITE_OMIT_WAL
40632/*
40633** Check if the *-wal file that corresponds to the database opened by pPager
40634** exists if the database is not empy, or verify that the *-wal file does
40635** not exist (by deleting it) if the database file is empty.
40636**
40637** If the database is not empty and the *-wal file exists, open the pager
40638** in WAL mode.  If the database is empty or if no *-wal file exists and
40639** if no error occurs, make sure Pager.journalMode is not set to
40640** PAGER_JOURNALMODE_WAL.
40641**
40642** Return SQLITE_OK or an error code.
40643**
40644** The caller must hold a SHARED lock on the database file to call this
40645** function. Because an EXCLUSIVE lock on the db file is required to delete
40646** a WAL on a none-empty database, this ensures there is no race condition
40647** between the xAccess() below and an xDelete() being executed by some
40648** other connection.
40649*/
40650static int pagerOpenWalIfPresent(Pager *pPager){
40651  int rc = SQLITE_OK;
40652  assert( pPager->eState==PAGER_OPEN );
40653  assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
40654
40655  if( !pPager->tempFile ){
40656    int isWal;                    /* True if WAL file exists */
40657    Pgno nPage;                   /* Size of the database file */
40658
40659    rc = pagerPagecount(pPager, &nPage);
40660    if( rc ) return rc;
40661    if( nPage==0 ){
40662      rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
40663      isWal = 0;
40664    }else{
40665      rc = sqlite3OsAccess(
40666          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
40667      );
40668    }
40669    if( rc==SQLITE_OK ){
40670      if( isWal ){
40671        testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
40672        rc = sqlite3PagerOpenWal(pPager, 0);
40673      }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
40674        pPager->journalMode = PAGER_JOURNALMODE_DELETE;
40675      }
40676    }
40677  }
40678  return rc;
40679}
40680#endif
40681
40682/*
40683** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
40684** the entire master journal file. The case pSavepoint==NULL occurs when
40685** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
40686** savepoint.
40687**
40688** When pSavepoint is not NULL (meaning a non-transaction savepoint is
40689** being rolled back), then the rollback consists of up to three stages,
40690** performed in the order specified:
40691**
40692**   * Pages are played back from the main journal starting at byte
40693**     offset PagerSavepoint.iOffset and continuing to
40694**     PagerSavepoint.iHdrOffset, or to the end of the main journal
40695**     file if PagerSavepoint.iHdrOffset is zero.
40696**
40697**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
40698**     back starting from the journal header immediately following
40699**     PagerSavepoint.iHdrOffset to the end of the main journal file.
40700**
40701**   * Pages are then played back from the sub-journal file, starting
40702**     with the PagerSavepoint.iSubRec and continuing to the end of
40703**     the journal file.
40704**
40705** Throughout the rollback process, each time a page is rolled back, the
40706** corresponding bit is set in a bitvec structure (variable pDone in the
40707** implementation below). This is used to ensure that a page is only
40708** rolled back the first time it is encountered in either journal.
40709**
40710** If pSavepoint is NULL, then pages are only played back from the main
40711** journal file. There is no need for a bitvec in this case.
40712**
40713** In either case, before playback commences the Pager.dbSize variable
40714** is reset to the value that it held at the start of the savepoint
40715** (or transaction). No page with a page-number greater than this value
40716** is played back. If one is encountered it is simply skipped.
40717*/
40718static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
40719  i64 szJ;                 /* Effective size of the main journal */
40720  i64 iHdrOff;             /* End of first segment of main-journal records */
40721  int rc = SQLITE_OK;      /* Return code */
40722  Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
40723
40724  assert( pPager->eState!=PAGER_ERROR );
40725  assert( pPager->eState>=PAGER_WRITER_LOCKED );
40726
40727  /* Allocate a bitvec to use to store the set of pages rolled back */
40728  if( pSavepoint ){
40729    pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
40730    if( !pDone ){
40731      return SQLITE_NOMEM;
40732    }
40733  }
40734
40735  /* Set the database size back to the value it was before the savepoint
40736  ** being reverted was opened.
40737  */
40738  pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
40739  pPager->changeCountDone = pPager->tempFile;
40740
40741  if( !pSavepoint && pagerUseWal(pPager) ){
40742    return pagerRollbackWal(pPager);
40743  }
40744
40745  /* Use pPager->journalOff as the effective size of the main rollback
40746  ** journal.  The actual file might be larger than this in
40747  ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
40748  ** past pPager->journalOff is off-limits to us.
40749  */
40750  szJ = pPager->journalOff;
40751  assert( pagerUseWal(pPager)==0 || szJ==0 );
40752
40753  /* Begin by rolling back records from the main journal starting at
40754  ** PagerSavepoint.iOffset and continuing to the next journal header.
40755  ** There might be records in the main journal that have a page number
40756  ** greater than the current database size (pPager->dbSize) but those
40757  ** will be skipped automatically.  Pages are added to pDone as they
40758  ** are played back.
40759  */
40760  if( pSavepoint && !pagerUseWal(pPager) ){
40761    iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
40762    pPager->journalOff = pSavepoint->iOffset;
40763    while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
40764      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
40765    }
40766    assert( rc!=SQLITE_DONE );
40767  }else{
40768    pPager->journalOff = 0;
40769  }
40770
40771  /* Continue rolling back records out of the main journal starting at
40772  ** the first journal header seen and continuing until the effective end
40773  ** of the main journal file.  Continue to skip out-of-range pages and
40774  ** continue adding pages rolled back to pDone.
40775  */
40776  while( rc==SQLITE_OK && pPager->journalOff<szJ ){
40777    u32 ii;            /* Loop counter */
40778    u32 nJRec = 0;     /* Number of Journal Records */
40779    u32 dummy;
40780    rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
40781    assert( rc!=SQLITE_DONE );
40782
40783    /*
40784    ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
40785    ** test is related to ticket #2565.  See the discussion in the
40786    ** pager_playback() function for additional information.
40787    */
40788    if( nJRec==0
40789     && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
40790    ){
40791      nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
40792    }
40793    for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
40794      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
40795    }
40796    assert( rc!=SQLITE_DONE );
40797  }
40798  assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
40799
40800  /* Finally,  rollback pages from the sub-journal.  Page that were
40801  ** previously rolled back out of the main journal (and are hence in pDone)
40802  ** will be skipped.  Out-of-range pages are also skipped.
40803  */
40804  if( pSavepoint ){
40805    u32 ii;            /* Loop counter */
40806    i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
40807
40808    if( pagerUseWal(pPager) ){
40809      rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
40810    }
40811    for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
40812      assert( offset==ii*(4+pPager->pageSize) );
40813      rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
40814    }
40815    assert( rc!=SQLITE_DONE );
40816  }
40817
40818  sqlite3BitvecDestroy(pDone);
40819  if( rc==SQLITE_OK ){
40820    pPager->journalOff = szJ;
40821  }
40822
40823  return rc;
40824}
40825
40826/*
40827** Change the maximum number of in-memory pages that are allowed.
40828*/
40829SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
40830  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
40831}
40832
40833/*
40834** Adjust the robustness of the database to damage due to OS crashes
40835** or power failures by changing the number of syncs()s when writing
40836** the rollback journal.  There are three levels:
40837**
40838**    OFF       sqlite3OsSync() is never called.  This is the default
40839**              for temporary and transient files.
40840**
40841**    NORMAL    The journal is synced once before writes begin on the
40842**              database.  This is normally adequate protection, but
40843**              it is theoretically possible, though very unlikely,
40844**              that an inopertune power failure could leave the journal
40845**              in a state which would cause damage to the database
40846**              when it is rolled back.
40847**
40848**    FULL      The journal is synced twice before writes begin on the
40849**              database (with some additional information - the nRec field
40850**              of the journal header - being written in between the two
40851**              syncs).  If we assume that writing a
40852**              single disk sector is atomic, then this mode provides
40853**              assurance that the journal will not be corrupted to the
40854**              point of causing damage to the database during rollback.
40855**
40856** The above is for a rollback-journal mode.  For WAL mode, OFF continues
40857** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
40858** prior to the start of checkpoint and that the database file is synced
40859** at the conclusion of the checkpoint if the entire content of the WAL
40860** was written back into the database.  But no sync operations occur for
40861** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
40862** file is synced following each commit operation, in addition to the
40863** syncs associated with NORMAL.
40864**
40865** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
40866** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
40867** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
40868** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
40869** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
40870** synchronous=FULL versus synchronous=NORMAL setting determines when
40871** the xSync primitive is called and is relevant to all platforms.
40872**
40873** Numeric values associated with these states are OFF==1, NORMAL=2,
40874** and FULL=3.
40875*/
40876#ifndef SQLITE_OMIT_PAGER_PRAGMAS
40877SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
40878  Pager *pPager,        /* The pager to set safety level for */
40879  int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
40880  int bFullFsync,       /* PRAGMA fullfsync */
40881  int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
40882){
40883  assert( level>=1 && level<=3 );
40884  pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
40885  pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
40886  if( pPager->noSync ){
40887    pPager->syncFlags = 0;
40888    pPager->ckptSyncFlags = 0;
40889  }else if( bFullFsync ){
40890    pPager->syncFlags = SQLITE_SYNC_FULL;
40891    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
40892  }else if( bCkptFullFsync ){
40893    pPager->syncFlags = SQLITE_SYNC_NORMAL;
40894    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
40895  }else{
40896    pPager->syncFlags = SQLITE_SYNC_NORMAL;
40897    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
40898  }
40899}
40900#endif
40901
40902/*
40903** The following global variable is incremented whenever the library
40904** attempts to open a temporary file.  This information is used for
40905** testing and analysis only.
40906*/
40907#ifdef SQLITE_TEST
40908SQLITE_API int sqlite3_opentemp_count = 0;
40909#endif
40910
40911/*
40912** Open a temporary file.
40913**
40914** Write the file descriptor into *pFile. Return SQLITE_OK on success
40915** or some other error code if we fail. The OS will automatically
40916** delete the temporary file when it is closed.
40917**
40918** The flags passed to the VFS layer xOpen() call are those specified
40919** by parameter vfsFlags ORed with the following:
40920**
40921**     SQLITE_OPEN_READWRITE
40922**     SQLITE_OPEN_CREATE
40923**     SQLITE_OPEN_EXCLUSIVE
40924**     SQLITE_OPEN_DELETEONCLOSE
40925*/
40926static int pagerOpentemp(
40927  Pager *pPager,        /* The pager object */
40928  sqlite3_file *pFile,  /* Write the file descriptor here */
40929  int vfsFlags          /* Flags passed through to the VFS */
40930){
40931  int rc;               /* Return code */
40932
40933#ifdef SQLITE_TEST
40934  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
40935#endif
40936
40937  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
40938            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
40939  rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
40940  assert( rc!=SQLITE_OK || isOpen(pFile) );
40941  return rc;
40942}
40943
40944/*
40945** Set the busy handler function.
40946**
40947** The pager invokes the busy-handler if sqlite3OsLock() returns
40948** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
40949** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
40950** lock. It does *not* invoke the busy handler when upgrading from
40951** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
40952** (which occurs during hot-journal rollback). Summary:
40953**
40954**   Transition                        | Invokes xBusyHandler
40955**   --------------------------------------------------------
40956**   NO_LOCK       -> SHARED_LOCK      | Yes
40957**   SHARED_LOCK   -> RESERVED_LOCK    | No
40958**   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
40959**   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
40960**
40961** If the busy-handler callback returns non-zero, the lock is
40962** retried. If it returns zero, then the SQLITE_BUSY error is
40963** returned to the caller of the pager API function.
40964*/
40965SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
40966  Pager *pPager,                       /* Pager object */
40967  int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
40968  void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
40969){
40970  pPager->xBusyHandler = xBusyHandler;
40971  pPager->pBusyHandlerArg = pBusyHandlerArg;
40972}
40973
40974/*
40975** Change the page size used by the Pager object. The new page size
40976** is passed in *pPageSize.
40977**
40978** If the pager is in the error state when this function is called, it
40979** is a no-op. The value returned is the error state error code (i.e.
40980** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
40981**
40982** Otherwise, if all of the following are true:
40983**
40984**   * the new page size (value of *pPageSize) is valid (a power
40985**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
40986**
40987**   * there are no outstanding page references, and
40988**
40989**   * the database is either not an in-memory database or it is
40990**     an in-memory database that currently consists of zero pages.
40991**
40992** then the pager object page size is set to *pPageSize.
40993**
40994** If the page size is changed, then this function uses sqlite3PagerMalloc()
40995** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
40996** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
40997** In all other cases, SQLITE_OK is returned.
40998**
40999** If the page size is not changed, either because one of the enumerated
41000** conditions above is not true, the pager was in error state when this
41001** function was called, or because the memory allocation attempt failed,
41002** then *pPageSize is set to the old, retained page size before returning.
41003*/
41004SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
41005  int rc = SQLITE_OK;
41006
41007  /* It is not possible to do a full assert_pager_state() here, as this
41008  ** function may be called from within PagerOpen(), before the state
41009  ** of the Pager object is internally consistent.
41010  **
41011  ** At one point this function returned an error if the pager was in
41012  ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
41013  ** there is at least one outstanding page reference, this function
41014  ** is a no-op for that case anyhow.
41015  */
41016
41017  u32 pageSize = *pPageSize;
41018  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
41019  if( (pPager->memDb==0 || pPager->dbSize==0)
41020   && sqlite3PcacheRefCount(pPager->pPCache)==0
41021   && pageSize && pageSize!=(u32)pPager->pageSize
41022  ){
41023    char *pNew = NULL;             /* New temp space */
41024    i64 nByte = 0;
41025
41026    if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
41027      rc = sqlite3OsFileSize(pPager->fd, &nByte);
41028    }
41029    if( rc==SQLITE_OK ){
41030      pNew = (char *)sqlite3PageMalloc(pageSize);
41031      if( !pNew ) rc = SQLITE_NOMEM;
41032    }
41033
41034    if( rc==SQLITE_OK ){
41035      pager_reset(pPager);
41036      pPager->dbSize = (Pgno)(nByte/pageSize);
41037      pPager->pageSize = pageSize;
41038      sqlite3PageFree(pPager->pTmpSpace);
41039      pPager->pTmpSpace = pNew;
41040      sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
41041    }
41042  }
41043
41044  *pPageSize = pPager->pageSize;
41045  if( rc==SQLITE_OK ){
41046    if( nReserve<0 ) nReserve = pPager->nReserve;
41047    assert( nReserve>=0 && nReserve<1000 );
41048    pPager->nReserve = (i16)nReserve;
41049    pagerReportSize(pPager);
41050  }
41051  return rc;
41052}
41053
41054/*
41055** Return a pointer to the "temporary page" buffer held internally
41056** by the pager.  This is a buffer that is big enough to hold the
41057** entire content of a database page.  This buffer is used internally
41058** during rollback and will be overwritten whenever a rollback
41059** occurs.  But other modules are free to use it too, as long as
41060** no rollbacks are happening.
41061*/
41062SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
41063  return pPager->pTmpSpace;
41064}
41065
41066/*
41067** Attempt to set the maximum database page count if mxPage is positive.
41068** Make no changes if mxPage is zero or negative.  And never reduce the
41069** maximum page count below the current size of the database.
41070**
41071** Regardless of mxPage, return the current maximum page count.
41072*/
41073SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
41074  if( mxPage>0 ){
41075    pPager->mxPgno = mxPage;
41076  }
41077  assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
41078  assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
41079  return pPager->mxPgno;
41080}
41081
41082/*
41083** The following set of routines are used to disable the simulated
41084** I/O error mechanism.  These routines are used to avoid simulated
41085** errors in places where we do not care about errors.
41086**
41087** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
41088** and generate no code.
41089*/
41090#ifdef SQLITE_TEST
41091SQLITE_API extern int sqlite3_io_error_pending;
41092SQLITE_API extern int sqlite3_io_error_hit;
41093static int saved_cnt;
41094void disable_simulated_io_errors(void){
41095  saved_cnt = sqlite3_io_error_pending;
41096  sqlite3_io_error_pending = -1;
41097}
41098void enable_simulated_io_errors(void){
41099  sqlite3_io_error_pending = saved_cnt;
41100}
41101#else
41102# define disable_simulated_io_errors()
41103# define enable_simulated_io_errors()
41104#endif
41105
41106/*
41107** Read the first N bytes from the beginning of the file into memory
41108** that pDest points to.
41109**
41110** If the pager was opened on a transient file (zFilename==""), or
41111** opened on a file less than N bytes in size, the output buffer is
41112** zeroed and SQLITE_OK returned. The rationale for this is that this
41113** function is used to read database headers, and a new transient or
41114** zero sized database has a header than consists entirely of zeroes.
41115**
41116** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
41117** the error code is returned to the caller and the contents of the
41118** output buffer undefined.
41119*/
41120SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
41121  int rc = SQLITE_OK;
41122  memset(pDest, 0, N);
41123  assert( isOpen(pPager->fd) || pPager->tempFile );
41124
41125  /* This routine is only called by btree immediately after creating
41126  ** the Pager object.  There has not been an opportunity to transition
41127  ** to WAL mode yet.
41128  */
41129  assert( !pagerUseWal(pPager) );
41130
41131  if( isOpen(pPager->fd) ){
41132    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
41133    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
41134    if( rc==SQLITE_IOERR_SHORT_READ ){
41135      rc = SQLITE_OK;
41136    }
41137  }
41138  return rc;
41139}
41140
41141/*
41142** This function may only be called when a read-transaction is open on
41143** the pager. It returns the total number of pages in the database.
41144**
41145** However, if the file is between 1 and <page-size> bytes in size, then
41146** this is considered a 1 page file.
41147*/
41148SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
41149  assert( pPager->eState>=PAGER_READER );
41150  assert( pPager->eState!=PAGER_WRITER_FINISHED );
41151  *pnPage = (int)pPager->dbSize;
41152}
41153
41154
41155/*
41156** Try to obtain a lock of type locktype on the database file. If
41157** a similar or greater lock is already held, this function is a no-op
41158** (returning SQLITE_OK immediately).
41159**
41160** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
41161** the busy callback if the lock is currently not available. Repeat
41162** until the busy callback returns false or until the attempt to
41163** obtain the lock succeeds.
41164**
41165** Return SQLITE_OK on success and an error code if we cannot obtain
41166** the lock. If the lock is obtained successfully, set the Pager.state
41167** variable to locktype before returning.
41168*/
41169static int pager_wait_on_lock(Pager *pPager, int locktype){
41170  int rc;                              /* Return code */
41171
41172  /* Check that this is either a no-op (because the requested lock is
41173  ** already held, or one of the transistions that the busy-handler
41174  ** may be invoked during, according to the comment above
41175  ** sqlite3PagerSetBusyhandler().
41176  */
41177  assert( (pPager->eLock>=locktype)
41178       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
41179       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
41180  );
41181
41182  do {
41183    rc = pagerLockDb(pPager, locktype);
41184  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
41185  return rc;
41186}
41187
41188/*
41189** Function assertTruncateConstraint(pPager) checks that one of the
41190** following is true for all dirty pages currently in the page-cache:
41191**
41192**   a) The page number is less than or equal to the size of the
41193**      current database image, in pages, OR
41194**
41195**   b) if the page content were written at this time, it would not
41196**      be necessary to write the current content out to the sub-journal
41197**      (as determined by function subjRequiresPage()).
41198**
41199** If the condition asserted by this function were not true, and the
41200** dirty page were to be discarded from the cache via the pagerStress()
41201** routine, pagerStress() would not write the current page content to
41202** the database file. If a savepoint transaction were rolled back after
41203** this happened, the correct behaviour would be to restore the current
41204** content of the page. However, since this content is not present in either
41205** the database file or the portion of the rollback journal and
41206** sub-journal rolled back the content could not be restored and the
41207** database image would become corrupt. It is therefore fortunate that
41208** this circumstance cannot arise.
41209*/
41210#if defined(SQLITE_DEBUG)
41211static void assertTruncateConstraintCb(PgHdr *pPg){
41212  assert( pPg->flags&PGHDR_DIRTY );
41213  assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
41214}
41215static void assertTruncateConstraint(Pager *pPager){
41216  sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
41217}
41218#else
41219# define assertTruncateConstraint(pPager)
41220#endif
41221
41222/*
41223** Truncate the in-memory database file image to nPage pages. This
41224** function does not actually modify the database file on disk. It
41225** just sets the internal state of the pager object so that the
41226** truncation will be done when the current transaction is committed.
41227*/
41228SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
41229  assert( pPager->dbSize>=nPage );
41230  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
41231  pPager->dbSize = nPage;
41232  assertTruncateConstraint(pPager);
41233}
41234
41235
41236/*
41237** This function is called before attempting a hot-journal rollback. It
41238** syncs the journal file to disk, then sets pPager->journalHdr to the
41239** size of the journal file so that the pager_playback() routine knows
41240** that the entire journal file has been synced.
41241**
41242** Syncing a hot-journal to disk before attempting to roll it back ensures
41243** that if a power-failure occurs during the rollback, the process that
41244** attempts rollback following system recovery sees the same journal
41245** content as this process.
41246**
41247** If everything goes as planned, SQLITE_OK is returned. Otherwise,
41248** an SQLite error code.
41249*/
41250static int pagerSyncHotJournal(Pager *pPager){
41251  int rc = SQLITE_OK;
41252  if( !pPager->noSync ){
41253    rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
41254  }
41255  if( rc==SQLITE_OK ){
41256    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
41257  }
41258  return rc;
41259}
41260
41261/*
41262** Shutdown the page cache.  Free all memory and close all files.
41263**
41264** If a transaction was in progress when this routine is called, that
41265** transaction is rolled back.  All outstanding pages are invalidated
41266** and their memory is freed.  Any attempt to use a page associated
41267** with this page cache after this function returns will likely
41268** result in a coredump.
41269**
41270** This function always succeeds. If a transaction is active an attempt
41271** is made to roll it back. If an error occurs during the rollback
41272** a hot journal may be left in the filesystem but no error is returned
41273** to the caller.
41274*/
41275SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
41276  u8 *pTmp = (u8 *)pPager->pTmpSpace;
41277
41278  assert( assert_pager_state(pPager) );
41279  disable_simulated_io_errors();
41280  sqlite3BeginBenignMalloc();
41281  /* pPager->errCode = 0; */
41282  pPager->exclusiveMode = 0;
41283#ifndef SQLITE_OMIT_WAL
41284  sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
41285  pPager->pWal = 0;
41286#endif
41287  pager_reset(pPager);
41288  if( MEMDB ){
41289    pager_unlock(pPager);
41290  }else{
41291    /* If it is open, sync the journal file before calling UnlockAndRollback.
41292    ** If this is not done, then an unsynced portion of the open journal
41293    ** file may be played back into the database. If a power failure occurs
41294    ** while this is happening, the database could become corrupt.
41295    **
41296    ** If an error occurs while trying to sync the journal, shift the pager
41297    ** into the ERROR state. This causes UnlockAndRollback to unlock the
41298    ** database and close the journal file without attempting to roll it
41299    ** back or finalize it. The next database user will have to do hot-journal
41300    ** rollback before accessing the database file.
41301    */
41302    if( isOpen(pPager->jfd) ){
41303      pager_error(pPager, pagerSyncHotJournal(pPager));
41304    }
41305    pagerUnlockAndRollback(pPager);
41306  }
41307  sqlite3EndBenignMalloc();
41308  enable_simulated_io_errors();
41309  PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
41310  IOTRACE(("CLOSE %p\n", pPager))
41311  sqlite3OsClose(pPager->jfd);
41312  sqlite3OsClose(pPager->fd);
41313  sqlite3PageFree(pTmp);
41314  sqlite3PcacheClose(pPager->pPCache);
41315
41316#ifdef SQLITE_HAS_CODEC
41317  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
41318#endif
41319
41320  assert( !pPager->aSavepoint && !pPager->pInJournal );
41321  assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
41322
41323  sqlite3_free(pPager);
41324  return SQLITE_OK;
41325}
41326
41327#if !defined(NDEBUG) || defined(SQLITE_TEST)
41328/*
41329** Return the page number for page pPg.
41330*/
41331SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
41332  return pPg->pgno;
41333}
41334#endif
41335
41336/*
41337** Increment the reference count for page pPg.
41338*/
41339SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
41340  sqlite3PcacheRef(pPg);
41341}
41342
41343/*
41344** Sync the journal. In other words, make sure all the pages that have
41345** been written to the journal have actually reached the surface of the
41346** disk and can be restored in the event of a hot-journal rollback.
41347**
41348** If the Pager.noSync flag is set, then this function is a no-op.
41349** Otherwise, the actions required depend on the journal-mode and the
41350** device characteristics of the the file-system, as follows:
41351**
41352**   * If the journal file is an in-memory journal file, no action need
41353**     be taken.
41354**
41355**   * Otherwise, if the device does not support the SAFE_APPEND property,
41356**     then the nRec field of the most recently written journal header
41357**     is updated to contain the number of journal records that have
41358**     been written following it. If the pager is operating in full-sync
41359**     mode, then the journal file is synced before this field is updated.
41360**
41361**   * If the device does not support the SEQUENTIAL property, then
41362**     journal file is synced.
41363**
41364** Or, in pseudo-code:
41365**
41366**   if( NOT <in-memory journal> ){
41367**     if( NOT SAFE_APPEND ){
41368**       if( <full-sync mode> ) xSync(<journal file>);
41369**       <update nRec field>
41370**     }
41371**     if( NOT SEQUENTIAL ) xSync(<journal file>);
41372**   }
41373**
41374** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
41375** page currently held in memory before returning SQLITE_OK. If an IO
41376** error is encountered, then the IO error code is returned to the caller.
41377*/
41378static int syncJournal(Pager *pPager, int newHdr){
41379  int rc;                         /* Return code */
41380
41381  assert( pPager->eState==PAGER_WRITER_CACHEMOD
41382       || pPager->eState==PAGER_WRITER_DBMOD
41383  );
41384  assert( assert_pager_state(pPager) );
41385  assert( !pagerUseWal(pPager) );
41386
41387  rc = sqlite3PagerExclusiveLock(pPager);
41388  if( rc!=SQLITE_OK ) return rc;
41389
41390  if( !pPager->noSync ){
41391    assert( !pPager->tempFile );
41392    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
41393      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
41394      assert( isOpen(pPager->jfd) );
41395
41396      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
41397        /* This block deals with an obscure problem. If the last connection
41398        ** that wrote to this database was operating in persistent-journal
41399        ** mode, then the journal file may at this point actually be larger
41400        ** than Pager.journalOff bytes. If the next thing in the journal
41401        ** file happens to be a journal-header (written as part of the
41402        ** previous connection's transaction), and a crash or power-failure
41403        ** occurs after nRec is updated but before this connection writes
41404        ** anything else to the journal file (or commits/rolls back its
41405        ** transaction), then SQLite may become confused when doing the
41406        ** hot-journal rollback following recovery. It may roll back all
41407        ** of this connections data, then proceed to rolling back the old,
41408        ** out-of-date data that follows it. Database corruption.
41409        **
41410        ** To work around this, if the journal file does appear to contain
41411        ** a valid header following Pager.journalOff, then write a 0x00
41412        ** byte to the start of it to prevent it from being recognized.
41413        **
41414        ** Variable iNextHdrOffset is set to the offset at which this
41415        ** problematic header will occur, if it exists. aMagic is used
41416        ** as a temporary buffer to inspect the first couple of bytes of
41417        ** the potential journal header.
41418        */
41419        i64 iNextHdrOffset;
41420        u8 aMagic[8];
41421        u8 zHeader[sizeof(aJournalMagic)+4];
41422
41423        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
41424        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
41425
41426        iNextHdrOffset = journalHdrOffset(pPager);
41427        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
41428        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
41429          static const u8 zerobyte = 0;
41430          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
41431        }
41432        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
41433          return rc;
41434        }
41435
41436        /* Write the nRec value into the journal file header. If in
41437        ** full-synchronous mode, sync the journal first. This ensures that
41438        ** all data has really hit the disk before nRec is updated to mark
41439        ** it as a candidate for rollback.
41440        **
41441        ** This is not required if the persistent media supports the
41442        ** SAFE_APPEND property. Because in this case it is not possible
41443        ** for garbage data to be appended to the file, the nRec field
41444        ** is populated with 0xFFFFFFFF when the journal header is written
41445        ** and never needs to be updated.
41446        */
41447        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
41448          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
41449          IOTRACE(("JSYNC %p\n", pPager))
41450          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
41451          if( rc!=SQLITE_OK ) return rc;
41452        }
41453        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
41454        rc = sqlite3OsWrite(
41455            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
41456        );
41457        if( rc!=SQLITE_OK ) return rc;
41458      }
41459      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
41460        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
41461        IOTRACE(("JSYNC %p\n", pPager))
41462        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
41463          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
41464        );
41465        if( rc!=SQLITE_OK ) return rc;
41466      }
41467
41468      pPager->journalHdr = pPager->journalOff;
41469      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
41470        pPager->nRec = 0;
41471        rc = writeJournalHdr(pPager);
41472        if( rc!=SQLITE_OK ) return rc;
41473      }
41474    }else{
41475      pPager->journalHdr = pPager->journalOff;
41476    }
41477  }
41478
41479  /* Unless the pager is in noSync mode, the journal file was just
41480  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
41481  ** all pages.
41482  */
41483  sqlite3PcacheClearSyncFlags(pPager->pPCache);
41484  pPager->eState = PAGER_WRITER_DBMOD;
41485  assert( assert_pager_state(pPager) );
41486  return SQLITE_OK;
41487}
41488
41489/*
41490** The argument is the first in a linked list of dirty pages connected
41491** by the PgHdr.pDirty pointer. This function writes each one of the
41492** in-memory pages in the list to the database file. The argument may
41493** be NULL, representing an empty list. In this case this function is
41494** a no-op.
41495**
41496** The pager must hold at least a RESERVED lock when this function
41497** is called. Before writing anything to the database file, this lock
41498** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
41499** SQLITE_BUSY is returned and no data is written to the database file.
41500**
41501** If the pager is a temp-file pager and the actual file-system file
41502** is not yet open, it is created and opened before any data is
41503** written out.
41504**
41505** Once the lock has been upgraded and, if necessary, the file opened,
41506** the pages are written out to the database file in list order. Writing
41507** a page is skipped if it meets either of the following criteria:
41508**
41509**   * The page number is greater than Pager.dbSize, or
41510**   * The PGHDR_DONT_WRITE flag is set on the page.
41511**
41512** If writing out a page causes the database file to grow, Pager.dbFileSize
41513** is updated accordingly. If page 1 is written out, then the value cached
41514** in Pager.dbFileVers[] is updated to match the new value stored in
41515** the database file.
41516**
41517** If everything is successful, SQLITE_OK is returned. If an IO error
41518** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
41519** be obtained, SQLITE_BUSY is returned.
41520*/
41521static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
41522  int rc = SQLITE_OK;                  /* Return code */
41523
41524  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
41525  assert( !pagerUseWal(pPager) );
41526  assert( pPager->eState==PAGER_WRITER_DBMOD );
41527  assert( pPager->eLock==EXCLUSIVE_LOCK );
41528
41529  /* If the file is a temp-file has not yet been opened, open it now. It
41530  ** is not possible for rc to be other than SQLITE_OK if this branch
41531  ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
41532  */
41533  if( !isOpen(pPager->fd) ){
41534    assert( pPager->tempFile && rc==SQLITE_OK );
41535    rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
41536  }
41537
41538  /* Before the first write, give the VFS a hint of what the final
41539  ** file size will be.
41540  */
41541  assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
41542  if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
41543    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
41544    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
41545    pPager->dbHintSize = pPager->dbSize;
41546  }
41547
41548  while( rc==SQLITE_OK && pList ){
41549    Pgno pgno = pList->pgno;
41550
41551    /* If there are dirty pages in the page cache with page numbers greater
41552    ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
41553    ** make the file smaller (presumably by auto-vacuum code). Do not write
41554    ** any such pages to the file.
41555    **
41556    ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
41557    ** set (set by sqlite3PagerDontWrite()).
41558    */
41559    if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
41560      i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
41561      char *pData;                                   /* Data to write */
41562
41563      assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
41564      if( pList->pgno==1 ) pager_write_changecounter(pList);
41565
41566      /* Encode the database */
41567      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
41568
41569      /* Write out the page data. */
41570      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
41571
41572      /* If page 1 was just written, update Pager.dbFileVers to match
41573      ** the value now stored in the database file. If writing this
41574      ** page caused the database file to grow, update dbFileSize.
41575      */
41576      if( pgno==1 ){
41577        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
41578      }
41579      if( pgno>pPager->dbFileSize ){
41580        pPager->dbFileSize = pgno;
41581      }
41582
41583      /* Update any backup objects copying the contents of this pager. */
41584      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
41585
41586      PAGERTRACE(("STORE %d page %d hash(%08x)\n",
41587                   PAGERID(pPager), pgno, pager_pagehash(pList)));
41588      IOTRACE(("PGOUT %p %d\n", pPager, pgno));
41589      PAGER_INCR(sqlite3_pager_writedb_count);
41590      PAGER_INCR(pPager->nWrite);
41591    }else{
41592      PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
41593    }
41594    pager_set_pagehash(pList);
41595    pList = pList->pDirty;
41596  }
41597
41598  return rc;
41599}
41600
41601/*
41602** Ensure that the sub-journal file is open. If it is already open, this
41603** function is a no-op.
41604**
41605** SQLITE_OK is returned if everything goes according to plan. An
41606** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
41607** fails.
41608*/
41609static int openSubJournal(Pager *pPager){
41610  int rc = SQLITE_OK;
41611  if( !isOpen(pPager->sjfd) ){
41612    if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
41613      sqlite3MemJournalOpen(pPager->sjfd);
41614    }else{
41615      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
41616    }
41617  }
41618  return rc;
41619}
41620
41621/*
41622** Append a record of the current state of page pPg to the sub-journal.
41623** It is the callers responsibility to use subjRequiresPage() to check
41624** that it is really required before calling this function.
41625**
41626** If successful, set the bit corresponding to pPg->pgno in the bitvecs
41627** for all open savepoints before returning.
41628**
41629** This function returns SQLITE_OK if everything is successful, an IO
41630** error code if the attempt to write to the sub-journal fails, or
41631** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
41632** bitvec.
41633*/
41634static int subjournalPage(PgHdr *pPg){
41635  int rc = SQLITE_OK;
41636  Pager *pPager = pPg->pPager;
41637  if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
41638
41639    /* Open the sub-journal, if it has not already been opened */
41640    assert( pPager->useJournal );
41641    assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
41642    assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
41643    assert( pagerUseWal(pPager)
41644         || pageInJournal(pPg)
41645         || pPg->pgno>pPager->dbOrigSize
41646    );
41647    rc = openSubJournal(pPager);
41648
41649    /* If the sub-journal was opened successfully (or was already open),
41650    ** write the journal record into the file.  */
41651    if( rc==SQLITE_OK ){
41652      void *pData = pPg->pData;
41653      i64 offset = pPager->nSubRec*(4+pPager->pageSize);
41654      char *pData2;
41655
41656      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
41657      PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
41658      rc = write32bits(pPager->sjfd, offset, pPg->pgno);
41659      if( rc==SQLITE_OK ){
41660        rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
41661      }
41662    }
41663  }
41664  if( rc==SQLITE_OK ){
41665    pPager->nSubRec++;
41666    assert( pPager->nSavepoint>0 );
41667    rc = addToSavepointBitvecs(pPager, pPg->pgno);
41668  }
41669  return rc;
41670}
41671
41672/*
41673** This function is called by the pcache layer when it has reached some
41674** soft memory limit. The first argument is a pointer to a Pager object
41675** (cast as a void*). The pager is always 'purgeable' (not an in-memory
41676** database). The second argument is a reference to a page that is
41677** currently dirty but has no outstanding references. The page
41678** is always associated with the Pager object passed as the first
41679** argument.
41680**
41681** The job of this function is to make pPg clean by writing its contents
41682** out to the database file, if possible. This may involve syncing the
41683** journal file.
41684**
41685** If successful, sqlite3PcacheMakeClean() is called on the page and
41686** SQLITE_OK returned. If an IO error occurs while trying to make the
41687** page clean, the IO error code is returned. If the page cannot be
41688** made clean for some other reason, but no error occurs, then SQLITE_OK
41689** is returned by sqlite3PcacheMakeClean() is not called.
41690*/
41691static int pagerStress(void *p, PgHdr *pPg){
41692  Pager *pPager = (Pager *)p;
41693  int rc = SQLITE_OK;
41694
41695  assert( pPg->pPager==pPager );
41696  assert( pPg->flags&PGHDR_DIRTY );
41697
41698  /* The doNotSyncSpill flag is set during times when doing a sync of
41699  ** journal (and adding a new header) is not allowed.  This occurs
41700  ** during calls to sqlite3PagerWrite() while trying to journal multiple
41701  ** pages belonging to the same sector.
41702  **
41703  ** The doNotSpill flag inhibits all cache spilling regardless of whether
41704  ** or not a sync is required.  This is set during a rollback.
41705  **
41706  ** Spilling is also prohibited when in an error state since that could
41707  ** lead to database corruption.   In the current implementaton it
41708  ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41709  ** while in the error state, hence it is impossible for this routine to
41710  ** be called in the error state.  Nevertheless, we include a NEVER()
41711  ** test for the error state as a safeguard against future changes.
41712  */
41713  if( NEVER(pPager->errCode) ) return SQLITE_OK;
41714  if( pPager->doNotSpill ) return SQLITE_OK;
41715  if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
41716    return SQLITE_OK;
41717  }
41718
41719  pPg->pDirty = 0;
41720  if( pagerUseWal(pPager) ){
41721    /* Write a single frame for this page to the log. */
41722    if( subjRequiresPage(pPg) ){
41723      rc = subjournalPage(pPg);
41724    }
41725    if( rc==SQLITE_OK ){
41726      rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
41727    }
41728  }else{
41729
41730    /* Sync the journal file if required. */
41731    if( pPg->flags&PGHDR_NEED_SYNC
41732     || pPager->eState==PAGER_WRITER_CACHEMOD
41733    ){
41734      rc = syncJournal(pPager, 1);
41735    }
41736
41737    /* If the page number of this page is larger than the current size of
41738    ** the database image, it may need to be written to the sub-journal.
41739    ** This is because the call to pager_write_pagelist() below will not
41740    ** actually write data to the file in this case.
41741    **
41742    ** Consider the following sequence of events:
41743    **
41744    **   BEGIN;
41745    **     <journal page X>
41746    **     <modify page X>
41747    **     SAVEPOINT sp;
41748    **       <shrink database file to Y pages>
41749    **       pagerStress(page X)
41750    **     ROLLBACK TO sp;
41751    **
41752    ** If (X>Y), then when pagerStress is called page X will not be written
41753    ** out to the database file, but will be dropped from the cache. Then,
41754    ** following the "ROLLBACK TO sp" statement, reading page X will read
41755    ** data from the database file. This will be the copy of page X as it
41756    ** was when the transaction started, not as it was when "SAVEPOINT sp"
41757    ** was executed.
41758    **
41759    ** The solution is to write the current data for page X into the
41760    ** sub-journal file now (if it is not already there), so that it will
41761    ** be restored to its current value when the "ROLLBACK TO sp" is
41762    ** executed.
41763    */
41764    if( NEVER(
41765        rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
41766    ) ){
41767      rc = subjournalPage(pPg);
41768    }
41769
41770    /* Write the contents of the page out to the database file. */
41771    if( rc==SQLITE_OK ){
41772      assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
41773      rc = pager_write_pagelist(pPager, pPg);
41774    }
41775  }
41776
41777  /* Mark the page as clean. */
41778  if( rc==SQLITE_OK ){
41779    PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
41780    sqlite3PcacheMakeClean(pPg);
41781  }
41782
41783  return pager_error(pPager, rc);
41784}
41785
41786
41787/*
41788** Allocate and initialize a new Pager object and put a pointer to it
41789** in *ppPager. The pager should eventually be freed by passing it
41790** to sqlite3PagerClose().
41791**
41792** The zFilename argument is the path to the database file to open.
41793** If zFilename is NULL then a randomly-named temporary file is created
41794** and used as the file to be cached. Temporary files are be deleted
41795** automatically when they are closed. If zFilename is ":memory:" then
41796** all information is held in cache. It is never written to disk.
41797** This can be used to implement an in-memory database.
41798**
41799** The nExtra parameter specifies the number of bytes of space allocated
41800** along with each page reference. This space is available to the user
41801** via the sqlite3PagerGetExtra() API.
41802**
41803** The flags argument is used to specify properties that affect the
41804** operation of the pager. It should be passed some bitwise combination
41805** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
41806**
41807** The vfsFlags parameter is a bitmask to pass to the flags parameter
41808** of the xOpen() method of the supplied VFS when opening files.
41809**
41810** If the pager object is allocated and the specified file opened
41811** successfully, SQLITE_OK is returned and *ppPager set to point to
41812** the new pager object. If an error occurs, *ppPager is set to NULL
41813** and error code returned. This function may return SQLITE_NOMEM
41814** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
41815** various SQLITE_IO_XXX errors.
41816*/
41817SQLITE_PRIVATE int sqlite3PagerOpen(
41818  sqlite3_vfs *pVfs,       /* The virtual file system to use */
41819  Pager **ppPager,         /* OUT: Return the Pager structure here */
41820  const char *zFilename,   /* Name of the database file to open */
41821  int nExtra,              /* Extra bytes append to each in-memory page */
41822  int flags,               /* flags controlling this file */
41823  int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
41824  void (*xReinit)(DbPage*) /* Function to reinitialize pages */
41825){
41826  u8 *pPtr;
41827  Pager *pPager = 0;       /* Pager object to allocate and return */
41828  int rc = SQLITE_OK;      /* Return code */
41829  int tempFile = 0;        /* True for temp files (incl. in-memory files) */
41830  int memDb = 0;           /* True if this is an in-memory file */
41831  int readOnly = 0;        /* True if this is a read-only file */
41832  int journalFileSize;     /* Bytes to allocate for each journal fd */
41833  char *zPathname = 0;     /* Full path to database file */
41834  int nPathname = 0;       /* Number of bytes in zPathname */
41835  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
41836  int noReadlock = (flags & PAGER_NO_READLOCK)!=0;  /* True to omit read-lock */
41837  int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
41838  u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
41839  const char *zUri = 0;    /* URI args to copy */
41840  int nUri = 0;            /* Number of bytes of URI args at *zUri */
41841
41842  /* Figure out how much space is required for each journal file-handle
41843  ** (there are two of them, the main journal and the sub-journal). This
41844  ** is the maximum space required for an in-memory journal file handle
41845  ** and a regular journal file-handle. Note that a "regular journal-handle"
41846  ** may be a wrapper capable of caching the first portion of the journal
41847  ** file in memory to implement the atomic-write optimization (see
41848  ** source file journal.c).
41849  */
41850  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
41851    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
41852  }else{
41853    journalFileSize = ROUND8(sqlite3MemJournalSize());
41854  }
41855
41856  /* Set the output variable to NULL in case an error occurs. */
41857  *ppPager = 0;
41858
41859#ifndef SQLITE_OMIT_MEMORYDB
41860  if( flags & PAGER_MEMORY ){
41861    memDb = 1;
41862    zFilename = 0;
41863  }
41864#endif
41865
41866  /* Compute and store the full pathname in an allocated buffer pointed
41867  ** to by zPathname, length nPathname. Or, if this is a temporary file,
41868  ** leave both nPathname and zPathname set to 0.
41869  */
41870  if( zFilename && zFilename[0] ){
41871    const char *z;
41872    nPathname = pVfs->mxPathname+1;
41873    zPathname = sqlite3Malloc(nPathname*2);
41874    if( zPathname==0 ){
41875      return SQLITE_NOMEM;
41876    }
41877    zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
41878    rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
41879    nPathname = sqlite3Strlen30(zPathname);
41880    z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
41881    while( *z ){
41882      z += sqlite3Strlen30(z)+1;
41883      z += sqlite3Strlen30(z)+1;
41884    }
41885    nUri = &z[1] - zUri;
41886    if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
41887      /* This branch is taken when the journal path required by
41888      ** the database being opened will be more than pVfs->mxPathname
41889      ** bytes in length. This means the database cannot be opened,
41890      ** as it will not be possible to open the journal file or even
41891      ** check for a hot-journal before reading.
41892      */
41893      rc = SQLITE_CANTOPEN_BKPT;
41894    }
41895    if( rc!=SQLITE_OK ){
41896      sqlite3_free(zPathname);
41897      return rc;
41898    }
41899  }
41900
41901  /* Allocate memory for the Pager structure, PCache object, the
41902  ** three file descriptors, the database file name and the journal
41903  ** file name. The layout in memory is as follows:
41904  **
41905  **     Pager object                    (sizeof(Pager) bytes)
41906  **     PCache object                   (sqlite3PcacheSize() bytes)
41907  **     Database file handle            (pVfs->szOsFile bytes)
41908  **     Sub-journal file handle         (journalFileSize bytes)
41909  **     Main journal file handle        (journalFileSize bytes)
41910  **     Database file name              (nPathname+1 bytes)
41911  **     Journal file name               (nPathname+8+1 bytes)
41912  */
41913  pPtr = (u8 *)sqlite3MallocZero(
41914    ROUND8(sizeof(*pPager)) +      /* Pager structure */
41915    ROUND8(pcacheSize) +           /* PCache object */
41916    ROUND8(pVfs->szOsFile) +       /* The main db file */
41917    journalFileSize * 2 +          /* The two journal files */
41918    nPathname + 1 + nUri +         /* zFilename */
41919    nPathname + 8 + 1              /* zJournal */
41920#ifndef SQLITE_OMIT_WAL
41921    + nPathname + 4 + 1              /* zWal */
41922#endif
41923  );
41924  assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
41925  if( !pPtr ){
41926    sqlite3_free(zPathname);
41927    return SQLITE_NOMEM;
41928  }
41929  pPager =              (Pager*)(pPtr);
41930  pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
41931  pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
41932  pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
41933  pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
41934  pPager->zFilename =    (char*)(pPtr += journalFileSize);
41935  assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
41936
41937  /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
41938  if( zPathname ){
41939    assert( nPathname>0 );
41940    pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
41941    memcpy(pPager->zFilename, zPathname, nPathname);
41942    memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
41943    memcpy(pPager->zJournal, zPathname, nPathname);
41944    memcpy(&pPager->zJournal[nPathname], "-journal", 8);
41945    sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
41946#ifndef SQLITE_OMIT_WAL
41947    pPager->zWal = &pPager->zJournal[nPathname+8+1];
41948    memcpy(pPager->zWal, zPathname, nPathname);
41949    memcpy(&pPager->zWal[nPathname], "-wal", 4);
41950    sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
41951#endif
41952    sqlite3_free(zPathname);
41953  }
41954  pPager->pVfs = pVfs;
41955  pPager->vfsFlags = vfsFlags;
41956
41957  /* Open the pager file.
41958  */
41959  if( zFilename && zFilename[0] ){
41960    int fout = 0;                    /* VFS flags returned by xOpen() */
41961    rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
41962    assert( !memDb );
41963    readOnly = (fout&SQLITE_OPEN_READONLY);
41964
41965    /* If the file was successfully opened for read/write access,
41966    ** choose a default page size in case we have to create the
41967    ** database file. The default page size is the maximum of:
41968    **
41969    **    + SQLITE_DEFAULT_PAGE_SIZE,
41970    **    + The value returned by sqlite3OsSectorSize()
41971    **    + The largest page size that can be written atomically.
41972    */
41973    if( rc==SQLITE_OK && !readOnly ){
41974      setSectorSize(pPager);
41975      assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
41976      if( szPageDflt<pPager->sectorSize ){
41977        if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
41978          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
41979        }else{
41980          szPageDflt = (u32)pPager->sectorSize;
41981        }
41982      }
41983#ifdef SQLITE_ENABLE_ATOMIC_WRITE
41984      {
41985        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
41986        int ii;
41987        assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
41988        assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
41989        assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
41990        for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
41991          if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
41992            szPageDflt = ii;
41993          }
41994        }
41995      }
41996#endif
41997    }
41998  }else{
41999    /* If a temporary file is requested, it is not opened immediately.
42000    ** In this case we accept the default page size and delay actually
42001    ** opening the file until the first call to OsWrite().
42002    **
42003    ** This branch is also run for an in-memory database. An in-memory
42004    ** database is the same as a temp-file that is never written out to
42005    ** disk and uses an in-memory rollback journal.
42006    */
42007    tempFile = 1;
42008    pPager->eState = PAGER_READER;
42009    pPager->eLock = EXCLUSIVE_LOCK;
42010    readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
42011  }
42012
42013  /* The following call to PagerSetPagesize() serves to set the value of
42014  ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
42015  */
42016  if( rc==SQLITE_OK ){
42017    assert( pPager->memDb==0 );
42018    rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
42019    testcase( rc!=SQLITE_OK );
42020  }
42021
42022  /* If an error occurred in either of the blocks above, free the
42023  ** Pager structure and close the file.
42024  */
42025  if( rc!=SQLITE_OK ){
42026    assert( !pPager->pTmpSpace );
42027    sqlite3OsClose(pPager->fd);
42028    sqlite3_free(pPager);
42029    return rc;
42030  }
42031
42032  /* Initialize the PCache object. */
42033  assert( nExtra<1000 );
42034  nExtra = ROUND8(nExtra);
42035  sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
42036                    !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
42037
42038  PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
42039  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
42040
42041  pPager->useJournal = (u8)useJournal;
42042  pPager->noReadlock = (noReadlock && readOnly) ?1:0;
42043  /* pPager->stmtOpen = 0; */
42044  /* pPager->stmtInUse = 0; */
42045  /* pPager->nRef = 0; */
42046  /* pPager->stmtSize = 0; */
42047  /* pPager->stmtJSize = 0; */
42048  /* pPager->nPage = 0; */
42049  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
42050  /* pPager->state = PAGER_UNLOCK; */
42051#if 0
42052  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
42053#endif
42054  /* pPager->errMask = 0; */
42055  pPager->tempFile = (u8)tempFile;
42056  assert( tempFile==PAGER_LOCKINGMODE_NORMAL
42057          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
42058  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
42059  pPager->exclusiveMode = (u8)tempFile;
42060  pPager->changeCountDone = pPager->tempFile;
42061  pPager->memDb = (u8)memDb;
42062  pPager->readOnly = (u8)readOnly;
42063  assert( useJournal || pPager->tempFile );
42064  pPager->noSync = pPager->tempFile;
42065  pPager->fullSync = pPager->noSync ?0:1;
42066  pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL;
42067  pPager->ckptSyncFlags = pPager->syncFlags;
42068  /* pPager->pFirst = 0; */
42069  /* pPager->pFirstSynced = 0; */
42070  /* pPager->pLast = 0; */
42071  pPager->nExtra = (u16)nExtra;
42072  pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
42073  assert( isOpen(pPager->fd) || tempFile );
42074  setSectorSize(pPager);
42075  if( !useJournal ){
42076    pPager->journalMode = PAGER_JOURNALMODE_OFF;
42077  }else if( memDb ){
42078    pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
42079  }
42080  /* pPager->xBusyHandler = 0; */
42081  /* pPager->pBusyHandlerArg = 0; */
42082  pPager->xReiniter = xReinit;
42083  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
42084
42085  *ppPager = pPager;
42086  return SQLITE_OK;
42087}
42088
42089
42090
42091/*
42092** This function is called after transitioning from PAGER_UNLOCK to
42093** PAGER_SHARED state. It tests if there is a hot journal present in
42094** the file-system for the given pager. A hot journal is one that
42095** needs to be played back. According to this function, a hot-journal
42096** file exists if the following criteria are met:
42097**
42098**   * The journal file exists in the file system, and
42099**   * No process holds a RESERVED or greater lock on the database file, and
42100**   * The database file itself is greater than 0 bytes in size, and
42101**   * The first byte of the journal file exists and is not 0x00.
42102**
42103** If the current size of the database file is 0 but a journal file
42104** exists, that is probably an old journal left over from a prior
42105** database with the same name. In this case the journal file is
42106** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
42107** is returned.
42108**
42109** This routine does not check if there is a master journal filename
42110** at the end of the file. If there is, and that master journal file
42111** does not exist, then the journal file is not really hot. In this
42112** case this routine will return a false-positive. The pager_playback()
42113** routine will discover that the journal file is not really hot and
42114** will not roll it back.
42115**
42116** If a hot-journal file is found to exist, *pExists is set to 1 and
42117** SQLITE_OK returned. If no hot-journal file is present, *pExists is
42118** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
42119** to determine whether or not a hot-journal file exists, the IO error
42120** code is returned and the value of *pExists is undefined.
42121*/
42122static int hasHotJournal(Pager *pPager, int *pExists){
42123  sqlite3_vfs * const pVfs = pPager->pVfs;
42124  int rc = SQLITE_OK;           /* Return code */
42125  int exists = 1;               /* True if a journal file is present */
42126  int jrnlOpen = !!isOpen(pPager->jfd);
42127
42128  assert( pPager->useJournal );
42129  assert( isOpen(pPager->fd) );
42130  assert( pPager->eState==PAGER_OPEN );
42131
42132  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
42133    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
42134  ));
42135
42136  *pExists = 0;
42137  if( !jrnlOpen ){
42138    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
42139  }
42140  if( rc==SQLITE_OK && exists ){
42141    int locked = 0;             /* True if some process holds a RESERVED lock */
42142
42143    /* Race condition here:  Another process might have been holding the
42144    ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
42145    ** call above, but then delete the journal and drop the lock before
42146    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
42147    ** is the case, this routine might think there is a hot journal when
42148    ** in fact there is none.  This results in a false-positive which will
42149    ** be dealt with by the playback routine.  Ticket #3883.
42150    */
42151    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
42152    if( rc==SQLITE_OK && !locked ){
42153      Pgno nPage;                 /* Number of pages in database file */
42154
42155      /* Check the size of the database file. If it consists of 0 pages,
42156      ** then delete the journal file. See the header comment above for
42157      ** the reasoning here.  Delete the obsolete journal file under
42158      ** a RESERVED lock to avoid race conditions and to avoid violating
42159      ** [H33020].
42160      */
42161      rc = pagerPagecount(pPager, &nPage);
42162      if( rc==SQLITE_OK ){
42163        if( nPage==0 ){
42164          sqlite3BeginBenignMalloc();
42165          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
42166            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
42167            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
42168          }
42169          sqlite3EndBenignMalloc();
42170        }else{
42171          /* The journal file exists and no other connection has a reserved
42172          ** or greater lock on the database file. Now check that there is
42173          ** at least one non-zero bytes at the start of the journal file.
42174          ** If there is, then we consider this journal to be hot. If not,
42175          ** it can be ignored.
42176          */
42177          if( !jrnlOpen ){
42178            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
42179            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
42180          }
42181          if( rc==SQLITE_OK ){
42182            u8 first = 0;
42183            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
42184            if( rc==SQLITE_IOERR_SHORT_READ ){
42185              rc = SQLITE_OK;
42186            }
42187            if( !jrnlOpen ){
42188              sqlite3OsClose(pPager->jfd);
42189            }
42190            *pExists = (first!=0);
42191          }else if( rc==SQLITE_CANTOPEN ){
42192            /* If we cannot open the rollback journal file in order to see if
42193            ** its has a zero header, that might be due to an I/O error, or
42194            ** it might be due to the race condition described above and in
42195            ** ticket #3883.  Either way, assume that the journal is hot.
42196            ** This might be a false positive.  But if it is, then the
42197            ** automatic journal playback and recovery mechanism will deal
42198            ** with it under an EXCLUSIVE lock where we do not need to
42199            ** worry so much with race conditions.
42200            */
42201            *pExists = 1;
42202            rc = SQLITE_OK;
42203          }
42204        }
42205      }
42206    }
42207  }
42208
42209  return rc;
42210}
42211
42212/*
42213** This function is called to obtain a shared lock on the database file.
42214** It is illegal to call sqlite3PagerAcquire() until after this function
42215** has been successfully called. If a shared-lock is already held when
42216** this function is called, it is a no-op.
42217**
42218** The following operations are also performed by this function.
42219**
42220**   1) If the pager is currently in PAGER_OPEN state (no lock held
42221**      on the database file), then an attempt is made to obtain a
42222**      SHARED lock on the database file. Immediately after obtaining
42223**      the SHARED lock, the file-system is checked for a hot-journal,
42224**      which is played back if present. Following any hot-journal
42225**      rollback, the contents of the cache are validated by checking
42226**      the 'change-counter' field of the database file header and
42227**      discarded if they are found to be invalid.
42228**
42229**   2) If the pager is running in exclusive-mode, and there are currently
42230**      no outstanding references to any pages, and is in the error state,
42231**      then an attempt is made to clear the error state by discarding
42232**      the contents of the page cache and rolling back any open journal
42233**      file.
42234**
42235** If everything is successful, SQLITE_OK is returned. If an IO error
42236** occurs while locking the database, checking for a hot-journal file or
42237** rolling back a journal file, the IO error code is returned.
42238*/
42239SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
42240  int rc = SQLITE_OK;                /* Return code */
42241
42242  /* This routine is only called from b-tree and only when there are no
42243  ** outstanding pages. This implies that the pager state should either
42244  ** be OPEN or READER. READER is only possible if the pager is or was in
42245  ** exclusive access mode.
42246  */
42247  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
42248  assert( assert_pager_state(pPager) );
42249  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
42250  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
42251
42252  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
42253    int bHotJournal = 1;          /* True if there exists a hot journal-file */
42254
42255    assert( !MEMDB );
42256    assert( pPager->noReadlock==0 || pPager->readOnly );
42257
42258    if( pPager->noReadlock==0 ){
42259      rc = pager_wait_on_lock(pPager, SHARED_LOCK);
42260      if( rc!=SQLITE_OK ){
42261        assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
42262        goto failed;
42263      }
42264    }
42265
42266    /* If a journal file exists, and there is no RESERVED lock on the
42267    ** database file, then it either needs to be played back or deleted.
42268    */
42269    if( pPager->eLock<=SHARED_LOCK ){
42270      rc = hasHotJournal(pPager, &bHotJournal);
42271    }
42272    if( rc!=SQLITE_OK ){
42273      goto failed;
42274    }
42275    if( bHotJournal ){
42276      /* Get an EXCLUSIVE lock on the database file. At this point it is
42277      ** important that a RESERVED lock is not obtained on the way to the
42278      ** EXCLUSIVE lock. If it were, another process might open the
42279      ** database file, detect the RESERVED lock, and conclude that the
42280      ** database is safe to read while this process is still rolling the
42281      ** hot-journal back.
42282      **
42283      ** Because the intermediate RESERVED lock is not requested, any
42284      ** other process attempting to access the database file will get to
42285      ** this point in the code and fail to obtain its own EXCLUSIVE lock
42286      ** on the database file.
42287      **
42288      ** Unless the pager is in locking_mode=exclusive mode, the lock is
42289      ** downgraded to SHARED_LOCK before this function returns.
42290      */
42291      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
42292      if( rc!=SQLITE_OK ){
42293        goto failed;
42294      }
42295
42296      /* If it is not already open and the file exists on disk, open the
42297      ** journal for read/write access. Write access is required because
42298      ** in exclusive-access mode the file descriptor will be kept open
42299      ** and possibly used for a transaction later on. Also, write-access
42300      ** is usually required to finalize the journal in journal_mode=persist
42301      ** mode (and also for journal_mode=truncate on some systems).
42302      **
42303      ** If the journal does not exist, it usually means that some
42304      ** other connection managed to get in and roll it back before
42305      ** this connection obtained the exclusive lock above. Or, it
42306      ** may mean that the pager was in the error-state when this
42307      ** function was called and the journal file does not exist.
42308      */
42309      if( !isOpen(pPager->jfd) ){
42310        sqlite3_vfs * const pVfs = pPager->pVfs;
42311        int bExists;              /* True if journal file exists */
42312        rc = sqlite3OsAccess(
42313            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
42314        if( rc==SQLITE_OK && bExists ){
42315          int fout = 0;
42316          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
42317          assert( !pPager->tempFile );
42318          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
42319          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
42320          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
42321            rc = SQLITE_CANTOPEN_BKPT;
42322            sqlite3OsClose(pPager->jfd);
42323          }
42324        }
42325      }
42326
42327      /* Playback and delete the journal.  Drop the database write
42328      ** lock and reacquire the read lock. Purge the cache before
42329      ** playing back the hot-journal so that we don't end up with
42330      ** an inconsistent cache.  Sync the hot journal before playing
42331      ** it back since the process that crashed and left the hot journal
42332      ** probably did not sync it and we are required to always sync
42333      ** the journal before playing it back.
42334      */
42335      if( isOpen(pPager->jfd) ){
42336        assert( rc==SQLITE_OK );
42337        rc = pagerSyncHotJournal(pPager);
42338        if( rc==SQLITE_OK ){
42339          rc = pager_playback(pPager, 1);
42340          pPager->eState = PAGER_OPEN;
42341        }
42342      }else if( !pPager->exclusiveMode ){
42343        pagerUnlockDb(pPager, SHARED_LOCK);
42344      }
42345
42346      if( rc!=SQLITE_OK ){
42347        /* This branch is taken if an error occurs while trying to open
42348        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
42349        ** pager_unlock() routine will be called before returning to unlock
42350        ** the file. If the unlock attempt fails, then Pager.eLock must be
42351        ** set to UNKNOWN_LOCK (see the comment above the #define for
42352        ** UNKNOWN_LOCK above for an explanation).
42353        **
42354        ** In order to get pager_unlock() to do this, set Pager.eState to
42355        ** PAGER_ERROR now. This is not actually counted as a transition
42356        ** to ERROR state in the state diagram at the top of this file,
42357        ** since we know that the same call to pager_unlock() will very
42358        ** shortly transition the pager object to the OPEN state. Calling
42359        ** assert_pager_state() would fail now, as it should not be possible
42360        ** to be in ERROR state when there are zero outstanding page
42361        ** references.
42362        */
42363        pager_error(pPager, rc);
42364        goto failed;
42365      }
42366
42367      assert( pPager->eState==PAGER_OPEN );
42368      assert( (pPager->eLock==SHARED_LOCK)
42369           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
42370      );
42371    }
42372
42373    if( !pPager->tempFile
42374     && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
42375    ){
42376      /* The shared-lock has just been acquired on the database file
42377      ** and there are already pages in the cache (from a previous
42378      ** read or write transaction).  Check to see if the database
42379      ** has been modified.  If the database has changed, flush the
42380      ** cache.
42381      **
42382      ** Database changes is detected by looking at 15 bytes beginning
42383      ** at offset 24 into the file.  The first 4 of these 16 bytes are
42384      ** a 32-bit counter that is incremented with each change.  The
42385      ** other bytes change randomly with each file change when
42386      ** a codec is in use.
42387      **
42388      ** There is a vanishingly small chance that a change will not be
42389      ** detected.  The chance of an undetected change is so small that
42390      ** it can be neglected.
42391      */
42392      Pgno nPage = 0;
42393      char dbFileVers[sizeof(pPager->dbFileVers)];
42394
42395      rc = pagerPagecount(pPager, &nPage);
42396      if( rc ) goto failed;
42397
42398      if( nPage>0 ){
42399        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
42400        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
42401        if( rc!=SQLITE_OK ){
42402          goto failed;
42403        }
42404      }else{
42405        memset(dbFileVers, 0, sizeof(dbFileVers));
42406      }
42407
42408      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
42409        pager_reset(pPager);
42410      }
42411    }
42412
42413    /* If there is a WAL file in the file-system, open this database in WAL
42414    ** mode. Otherwise, the following function call is a no-op.
42415    */
42416    rc = pagerOpenWalIfPresent(pPager);
42417#ifndef SQLITE_OMIT_WAL
42418    assert( pPager->pWal==0 || rc==SQLITE_OK );
42419#endif
42420  }
42421
42422  if( pagerUseWal(pPager) ){
42423    assert( rc==SQLITE_OK );
42424    rc = pagerBeginReadTransaction(pPager);
42425  }
42426
42427  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
42428    rc = pagerPagecount(pPager, &pPager->dbSize);
42429  }
42430
42431 failed:
42432  if( rc!=SQLITE_OK ){
42433    assert( !MEMDB );
42434    pager_unlock(pPager);
42435    assert( pPager->eState==PAGER_OPEN );
42436  }else{
42437    pPager->eState = PAGER_READER;
42438  }
42439  return rc;
42440}
42441
42442/*
42443** If the reference count has reached zero, rollback any active
42444** transaction and unlock the pager.
42445**
42446** Except, in locking_mode=EXCLUSIVE when there is nothing to in
42447** the rollback journal, the unlock is not performed and there is
42448** nothing to rollback, so this routine is a no-op.
42449*/
42450static void pagerUnlockIfUnused(Pager *pPager){
42451  if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
42452    pagerUnlockAndRollback(pPager);
42453  }
42454}
42455
42456/*
42457** Acquire a reference to page number pgno in pager pPager (a page
42458** reference has type DbPage*). If the requested reference is
42459** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
42460**
42461** If the requested page is already in the cache, it is returned.
42462** Otherwise, a new page object is allocated and populated with data
42463** read from the database file. In some cases, the pcache module may
42464** choose not to allocate a new page object and may reuse an existing
42465** object with no outstanding references.
42466**
42467** The extra data appended to a page is always initialized to zeros the
42468** first time a page is loaded into memory. If the page requested is
42469** already in the cache when this function is called, then the extra
42470** data is left as it was when the page object was last used.
42471**
42472** If the database image is smaller than the requested page or if a
42473** non-zero value is passed as the noContent parameter and the
42474** requested page is not already stored in the cache, then no
42475** actual disk read occurs. In this case the memory image of the
42476** page is initialized to all zeros.
42477**
42478** If noContent is true, it means that we do not care about the contents
42479** of the page. This occurs in two seperate scenarios:
42480**
42481**   a) When reading a free-list leaf page from the database, and
42482**
42483**   b) When a savepoint is being rolled back and we need to load
42484**      a new page into the cache to be filled with the data read
42485**      from the savepoint journal.
42486**
42487** If noContent is true, then the data returned is zeroed instead of
42488** being read from the database. Additionally, the bits corresponding
42489** to pgno in Pager.pInJournal (bitvec of pages already written to the
42490** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
42491** savepoints are set. This means if the page is made writable at any
42492** point in the future, using a call to sqlite3PagerWrite(), its contents
42493** will not be journaled. This saves IO.
42494**
42495** The acquisition might fail for several reasons.  In all cases,
42496** an appropriate error code is returned and *ppPage is set to NULL.
42497**
42498** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
42499** to find a page in the in-memory cache first.  If the page is not already
42500** in memory, this routine goes to disk to read it in whereas Lookup()
42501** just returns 0.  This routine acquires a read-lock the first time it
42502** has to go to disk, and could also playback an old journal if necessary.
42503** Since Lookup() never goes to disk, it never has to deal with locks
42504** or journal files.
42505*/
42506SQLITE_PRIVATE int sqlite3PagerAcquire(
42507  Pager *pPager,      /* The pager open on the database file */
42508  Pgno pgno,          /* Page number to fetch */
42509  DbPage **ppPage,    /* Write a pointer to the page here */
42510  int noContent       /* Do not bother reading content from disk if true */
42511){
42512  int rc;
42513  PgHdr *pPg;
42514
42515  assert( pPager->eState>=PAGER_READER );
42516  assert( assert_pager_state(pPager) );
42517
42518  if( pgno==0 ){
42519    return SQLITE_CORRUPT_BKPT;
42520  }
42521
42522  /* If the pager is in the error state, return an error immediately.
42523  ** Otherwise, request the page from the PCache layer. */
42524  if( pPager->errCode!=SQLITE_OK ){
42525    rc = pPager->errCode;
42526  }else{
42527    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
42528  }
42529
42530  if( rc!=SQLITE_OK ){
42531    /* Either the call to sqlite3PcacheFetch() returned an error or the
42532    ** pager was already in the error-state when this function was called.
42533    ** Set pPg to 0 and jump to the exception handler.  */
42534    pPg = 0;
42535    goto pager_acquire_err;
42536  }
42537  assert( (*ppPage)->pgno==pgno );
42538  assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
42539
42540  if( (*ppPage)->pPager && !noContent ){
42541    /* In this case the pcache already contains an initialized copy of
42542    ** the page. Return without further ado.  */
42543    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
42544    PAGER_INCR(pPager->nHit);
42545    return SQLITE_OK;
42546
42547  }else{
42548    /* The pager cache has created a new page. Its content needs to
42549    ** be initialized.  */
42550
42551    PAGER_INCR(pPager->nMiss);
42552    pPg = *ppPage;
42553    pPg->pPager = pPager;
42554
42555    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
42556    ** number greater than this, or the unused locking-page, is requested. */
42557    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
42558      rc = SQLITE_CORRUPT_BKPT;
42559      goto pager_acquire_err;
42560    }
42561
42562    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
42563      if( pgno>pPager->mxPgno ){
42564        rc = SQLITE_FULL;
42565        goto pager_acquire_err;
42566      }
42567      if( noContent ){
42568        /* Failure to set the bits in the InJournal bit-vectors is benign.
42569        ** It merely means that we might do some extra work to journal a
42570        ** page that does not need to be journaled.  Nevertheless, be sure
42571        ** to test the case where a malloc error occurs while trying to set
42572        ** a bit in a bit vector.
42573        */
42574        sqlite3BeginBenignMalloc();
42575        if( pgno<=pPager->dbOrigSize ){
42576          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
42577          testcase( rc==SQLITE_NOMEM );
42578        }
42579        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
42580        testcase( rc==SQLITE_NOMEM );
42581        sqlite3EndBenignMalloc();
42582      }
42583      memset(pPg->pData, 0, pPager->pageSize);
42584      IOTRACE(("ZERO %p %d\n", pPager, pgno));
42585    }else{
42586      assert( pPg->pPager==pPager );
42587      rc = readDbPage(pPg);
42588      if( rc!=SQLITE_OK ){
42589        goto pager_acquire_err;
42590      }
42591    }
42592    pager_set_pagehash(pPg);
42593  }
42594
42595  return SQLITE_OK;
42596
42597pager_acquire_err:
42598  assert( rc!=SQLITE_OK );
42599  if( pPg ){
42600    sqlite3PcacheDrop(pPg);
42601  }
42602  pagerUnlockIfUnused(pPager);
42603
42604  *ppPage = 0;
42605  return rc;
42606}
42607
42608/*
42609** Acquire a page if it is already in the in-memory cache.  Do
42610** not read the page from disk.  Return a pointer to the page,
42611** or 0 if the page is not in cache.
42612**
42613** See also sqlite3PagerGet().  The difference between this routine
42614** and sqlite3PagerGet() is that _get() will go to the disk and read
42615** in the page if the page is not already in cache.  This routine
42616** returns NULL if the page is not in cache or if a disk I/O error
42617** has ever happened.
42618*/
42619SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
42620  PgHdr *pPg = 0;
42621  assert( pPager!=0 );
42622  assert( pgno!=0 );
42623  assert( pPager->pPCache!=0 );
42624  assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
42625  sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
42626  return pPg;
42627}
42628
42629/*
42630** Release a page reference.
42631**
42632** If the number of references to the page drop to zero, then the
42633** page is added to the LRU list.  When all references to all pages
42634** are released, a rollback occurs and the lock on the database is
42635** removed.
42636*/
42637SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
42638  if( pPg ){
42639    Pager *pPager = pPg->pPager;
42640    sqlite3PcacheRelease(pPg);
42641    pagerUnlockIfUnused(pPager);
42642  }
42643}
42644
42645/*
42646** This function is called at the start of every write transaction.
42647** There must already be a RESERVED or EXCLUSIVE lock on the database
42648** file when this routine is called.
42649**
42650** Open the journal file for pager pPager and write a journal header
42651** to the start of it. If there are active savepoints, open the sub-journal
42652** as well. This function is only used when the journal file is being
42653** opened to write a rollback log for a transaction. It is not used
42654** when opening a hot journal file to roll it back.
42655**
42656** If the journal file is already open (as it may be in exclusive mode),
42657** then this function just writes a journal header to the start of the
42658** already open file.
42659**
42660** Whether or not the journal file is opened by this function, the
42661** Pager.pInJournal bitvec structure is allocated.
42662**
42663** Return SQLITE_OK if everything is successful. Otherwise, return
42664** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
42665** an IO error code if opening or writing the journal file fails.
42666*/
42667static int pager_open_journal(Pager *pPager){
42668  int rc = SQLITE_OK;                        /* Return code */
42669  sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
42670
42671  assert( pPager->eState==PAGER_WRITER_LOCKED );
42672  assert( assert_pager_state(pPager) );
42673  assert( pPager->pInJournal==0 );
42674
42675  /* If already in the error state, this function is a no-op.  But on
42676  ** the other hand, this routine is never called if we are already in
42677  ** an error state. */
42678  if( NEVER(pPager->errCode) ) return pPager->errCode;
42679
42680  if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
42681    pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
42682    if( pPager->pInJournal==0 ){
42683      return SQLITE_NOMEM;
42684    }
42685
42686    /* Open the journal file if it is not already open. */
42687    if( !isOpen(pPager->jfd) ){
42688      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
42689        sqlite3MemJournalOpen(pPager->jfd);
42690      }else{
42691        const int flags =                   /* VFS flags to open journal file */
42692          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
42693          (pPager->tempFile ?
42694            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
42695            (SQLITE_OPEN_MAIN_JOURNAL)
42696          );
42697  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
42698        rc = sqlite3JournalOpen(
42699            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
42700        );
42701  #else
42702        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
42703  #endif
42704      }
42705      assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
42706    }
42707
42708
42709    /* Write the first journal header to the journal file and open
42710    ** the sub-journal if necessary.
42711    */
42712    if( rc==SQLITE_OK ){
42713      /* TODO: Check if all of these are really required. */
42714      pPager->nRec = 0;
42715      pPager->journalOff = 0;
42716      pPager->setMaster = 0;
42717      pPager->journalHdr = 0;
42718      rc = writeJournalHdr(pPager);
42719    }
42720  }
42721
42722  if( rc!=SQLITE_OK ){
42723    sqlite3BitvecDestroy(pPager->pInJournal);
42724    pPager->pInJournal = 0;
42725  }else{
42726    assert( pPager->eState==PAGER_WRITER_LOCKED );
42727    pPager->eState = PAGER_WRITER_CACHEMOD;
42728  }
42729
42730  return rc;
42731}
42732
42733/*
42734** Begin a write-transaction on the specified pager object. If a
42735** write-transaction has already been opened, this function is a no-op.
42736**
42737** If the exFlag argument is false, then acquire at least a RESERVED
42738** lock on the database file. If exFlag is true, then acquire at least
42739** an EXCLUSIVE lock. If such a lock is already held, no locking
42740** functions need be called.
42741**
42742** If the subjInMemory argument is non-zero, then any sub-journal opened
42743** within this transaction will be opened as an in-memory file. This
42744** has no effect if the sub-journal is already opened (as it may be when
42745** running in exclusive mode) or if the transaction does not require a
42746** sub-journal. If the subjInMemory argument is zero, then any required
42747** sub-journal is implemented in-memory if pPager is an in-memory database,
42748** or using a temporary file otherwise.
42749*/
42750SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
42751  int rc = SQLITE_OK;
42752
42753  if( pPager->errCode ) return pPager->errCode;
42754  assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
42755  pPager->subjInMemory = (u8)subjInMemory;
42756
42757  if( ALWAYS(pPager->eState==PAGER_READER) ){
42758    assert( pPager->pInJournal==0 );
42759
42760    if( pagerUseWal(pPager) ){
42761      /* If the pager is configured to use locking_mode=exclusive, and an
42762      ** exclusive lock on the database is not already held, obtain it now.
42763      */
42764      if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
42765        rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
42766        if( rc!=SQLITE_OK ){
42767          return rc;
42768        }
42769        sqlite3WalExclusiveMode(pPager->pWal, 1);
42770      }
42771
42772      /* Grab the write lock on the log file. If successful, upgrade to
42773      ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
42774      ** The busy-handler is not invoked if another connection already
42775      ** holds the write-lock. If possible, the upper layer will call it.
42776      */
42777      rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
42778    }else{
42779      /* Obtain a RESERVED lock on the database file. If the exFlag parameter
42780      ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
42781      ** busy-handler callback can be used when upgrading to the EXCLUSIVE
42782      ** lock, but not when obtaining the RESERVED lock.
42783      */
42784      rc = pagerLockDb(pPager, RESERVED_LOCK);
42785      if( rc==SQLITE_OK && exFlag ){
42786        rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
42787      }
42788    }
42789
42790    if( rc==SQLITE_OK ){
42791      /* Change to WRITER_LOCKED state.
42792      **
42793      ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
42794      ** when it has an open transaction, but never to DBMOD or FINISHED.
42795      ** This is because in those states the code to roll back savepoint
42796      ** transactions may copy data from the sub-journal into the database
42797      ** file as well as into the page cache. Which would be incorrect in
42798      ** WAL mode.
42799      */
42800      pPager->eState = PAGER_WRITER_LOCKED;
42801      pPager->dbHintSize = pPager->dbSize;
42802      pPager->dbFileSize = pPager->dbSize;
42803      pPager->dbOrigSize = pPager->dbSize;
42804      pPager->journalOff = 0;
42805    }
42806
42807    assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
42808    assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
42809    assert( assert_pager_state(pPager) );
42810  }
42811
42812  PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
42813  return rc;
42814}
42815
42816/*
42817** Mark a single data page as writeable. The page is written into the
42818** main journal or sub-journal as required. If the page is written into
42819** one of the journals, the corresponding bit is set in the
42820** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
42821** of any open savepoints as appropriate.
42822*/
42823static int pager_write(PgHdr *pPg){
42824  void *pData = pPg->pData;
42825  Pager *pPager = pPg->pPager;
42826  int rc = SQLITE_OK;
42827
42828  /* This routine is not called unless a write-transaction has already
42829  ** been started. The journal file may or may not be open at this point.
42830  ** It is never called in the ERROR state.
42831  */
42832  assert( pPager->eState==PAGER_WRITER_LOCKED
42833       || pPager->eState==PAGER_WRITER_CACHEMOD
42834       || pPager->eState==PAGER_WRITER_DBMOD
42835  );
42836  assert( assert_pager_state(pPager) );
42837
42838  /* If an error has been previously detected, report the same error
42839  ** again. This should not happen, but the check provides robustness. */
42840  if( NEVER(pPager->errCode) )  return pPager->errCode;
42841
42842  /* Higher-level routines never call this function if database is not
42843  ** writable.  But check anyway, just for robustness. */
42844  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
42845
42846  CHECK_PAGE(pPg);
42847
42848  /* The journal file needs to be opened. Higher level routines have already
42849  ** obtained the necessary locks to begin the write-transaction, but the
42850  ** rollback journal might not yet be open. Open it now if this is the case.
42851  **
42852  ** This is done before calling sqlite3PcacheMakeDirty() on the page.
42853  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
42854  ** an error might occur and the pager would end up in WRITER_LOCKED state
42855  ** with pages marked as dirty in the cache.
42856  */
42857  if( pPager->eState==PAGER_WRITER_LOCKED ){
42858    rc = pager_open_journal(pPager);
42859    if( rc!=SQLITE_OK ) return rc;
42860  }
42861  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
42862  assert( assert_pager_state(pPager) );
42863
42864  /* Mark the page as dirty.  If the page has already been written
42865  ** to the journal then we can return right away.
42866  */
42867  sqlite3PcacheMakeDirty(pPg);
42868  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
42869    assert( !pagerUseWal(pPager) );
42870  }else{
42871
42872    /* The transaction journal now exists and we have a RESERVED or an
42873    ** EXCLUSIVE lock on the main database file.  Write the current page to
42874    ** the transaction journal if it is not there already.
42875    */
42876    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
42877      assert( pagerUseWal(pPager)==0 );
42878      if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
42879        u32 cksum;
42880        char *pData2;
42881        i64 iOff = pPager->journalOff;
42882
42883        /* We should never write to the journal file the page that
42884        ** contains the database locks.  The following assert verifies
42885        ** that we do not. */
42886        assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
42887
42888        assert( pPager->journalHdr<=pPager->journalOff );
42889        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
42890        cksum = pager_cksum(pPager, (u8*)pData2);
42891
42892        /* Even if an IO or diskfull error occurs while journalling the
42893        ** page in the block above, set the need-sync flag for the page.
42894        ** Otherwise, when the transaction is rolled back, the logic in
42895        ** playback_one_page() will think that the page needs to be restored
42896        ** in the database file. And if an IO error occurs while doing so,
42897        ** then corruption may follow.
42898        */
42899        pPg->flags |= PGHDR_NEED_SYNC;
42900
42901        rc = write32bits(pPager->jfd, iOff, pPg->pgno);
42902        if( rc!=SQLITE_OK ) return rc;
42903        rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
42904        if( rc!=SQLITE_OK ) return rc;
42905        rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
42906        if( rc!=SQLITE_OK ) return rc;
42907
42908        IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
42909                 pPager->journalOff, pPager->pageSize));
42910        PAGER_INCR(sqlite3_pager_writej_count);
42911        PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
42912             PAGERID(pPager), pPg->pgno,
42913             ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
42914
42915        pPager->journalOff += 8 + pPager->pageSize;
42916        pPager->nRec++;
42917        assert( pPager->pInJournal!=0 );
42918        rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
42919        testcase( rc==SQLITE_NOMEM );
42920        assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
42921        rc |= addToSavepointBitvecs(pPager, pPg->pgno);
42922        if( rc!=SQLITE_OK ){
42923          assert( rc==SQLITE_NOMEM );
42924          return rc;
42925        }
42926      }else{
42927        if( pPager->eState!=PAGER_WRITER_DBMOD ){
42928          pPg->flags |= PGHDR_NEED_SYNC;
42929        }
42930        PAGERTRACE(("APPEND %d page %d needSync=%d\n",
42931                PAGERID(pPager), pPg->pgno,
42932               ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
42933      }
42934    }
42935
42936    /* If the statement journal is open and the page is not in it,
42937    ** then write the current page to the statement journal.  Note that
42938    ** the statement journal format differs from the standard journal format
42939    ** in that it omits the checksums and the header.
42940    */
42941    if( subjRequiresPage(pPg) ){
42942      rc = subjournalPage(pPg);
42943    }
42944  }
42945
42946  /* Update the database size and return.
42947  */
42948  if( pPager->dbSize<pPg->pgno ){
42949    pPager->dbSize = pPg->pgno;
42950  }
42951  return rc;
42952}
42953
42954/*
42955** Mark a data page as writeable. This routine must be called before
42956** making changes to a page. The caller must check the return value
42957** of this function and be careful not to change any page data unless
42958** this routine returns SQLITE_OK.
42959**
42960** The difference between this function and pager_write() is that this
42961** function also deals with the special case where 2 or more pages
42962** fit on a single disk sector. In this case all co-resident pages
42963** must have been written to the journal file before returning.
42964**
42965** If an error occurs, SQLITE_NOMEM or an IO error code is returned
42966** as appropriate. Otherwise, SQLITE_OK.
42967*/
42968SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
42969  int rc = SQLITE_OK;
42970
42971  PgHdr *pPg = pDbPage;
42972  Pager *pPager = pPg->pPager;
42973  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
42974
42975  assert( pPager->eState>=PAGER_WRITER_LOCKED );
42976  assert( pPager->eState!=PAGER_ERROR );
42977  assert( assert_pager_state(pPager) );
42978
42979  if( nPagePerSector>1 ){
42980    Pgno nPageCount;          /* Total number of pages in database file */
42981    Pgno pg1;                 /* First page of the sector pPg is located on. */
42982    int nPage = 0;            /* Number of pages starting at pg1 to journal */
42983    int ii;                   /* Loop counter */
42984    int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
42985
42986    /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
42987    ** a journal header to be written between the pages journaled by
42988    ** this function.
42989    */
42990    assert( !MEMDB );
42991    assert( pPager->doNotSyncSpill==0 );
42992    pPager->doNotSyncSpill++;
42993
42994    /* This trick assumes that both the page-size and sector-size are
42995    ** an integer power of 2. It sets variable pg1 to the identifier
42996    ** of the first page of the sector pPg is located on.
42997    */
42998    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
42999
43000    nPageCount = pPager->dbSize;
43001    if( pPg->pgno>nPageCount ){
43002      nPage = (pPg->pgno - pg1)+1;
43003    }else if( (pg1+nPagePerSector-1)>nPageCount ){
43004      nPage = nPageCount+1-pg1;
43005    }else{
43006      nPage = nPagePerSector;
43007    }
43008    assert(nPage>0);
43009    assert(pg1<=pPg->pgno);
43010    assert((pg1+nPage)>pPg->pgno);
43011
43012    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
43013      Pgno pg = pg1+ii;
43014      PgHdr *pPage;
43015      if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
43016        if( pg!=PAGER_MJ_PGNO(pPager) ){
43017          rc = sqlite3PagerGet(pPager, pg, &pPage);
43018          if( rc==SQLITE_OK ){
43019            rc = pager_write(pPage);
43020            if( pPage->flags&PGHDR_NEED_SYNC ){
43021              needSync = 1;
43022            }
43023            sqlite3PagerUnref(pPage);
43024          }
43025        }
43026      }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
43027        if( pPage->flags&PGHDR_NEED_SYNC ){
43028          needSync = 1;
43029        }
43030        sqlite3PagerUnref(pPage);
43031      }
43032    }
43033
43034    /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
43035    ** starting at pg1, then it needs to be set for all of them. Because
43036    ** writing to any of these nPage pages may damage the others, the
43037    ** journal file must contain sync()ed copies of all of them
43038    ** before any of them can be written out to the database file.
43039    */
43040    if( rc==SQLITE_OK && needSync ){
43041      assert( !MEMDB );
43042      for(ii=0; ii<nPage; ii++){
43043        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
43044        if( pPage ){
43045          pPage->flags |= PGHDR_NEED_SYNC;
43046          sqlite3PagerUnref(pPage);
43047        }
43048      }
43049    }
43050
43051    assert( pPager->doNotSyncSpill==1 );
43052    pPager->doNotSyncSpill--;
43053  }else{
43054    rc = pager_write(pDbPage);
43055  }
43056  return rc;
43057}
43058
43059/*
43060** Return TRUE if the page given in the argument was previously passed
43061** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
43062** to change the content of the page.
43063*/
43064#ifndef NDEBUG
43065SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
43066  return pPg->flags&PGHDR_DIRTY;
43067}
43068#endif
43069
43070/*
43071** A call to this routine tells the pager that it is not necessary to
43072** write the information on page pPg back to the disk, even though
43073** that page might be marked as dirty.  This happens, for example, when
43074** the page has been added as a leaf of the freelist and so its
43075** content no longer matters.
43076**
43077** The overlying software layer calls this routine when all of the data
43078** on the given page is unused. The pager marks the page as clean so
43079** that it does not get written to disk.
43080**
43081** Tests show that this optimization can quadruple the speed of large
43082** DELETE operations.
43083*/
43084SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
43085  Pager *pPager = pPg->pPager;
43086  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
43087    PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
43088    IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
43089    pPg->flags |= PGHDR_DONT_WRITE;
43090    pager_set_pagehash(pPg);
43091  }
43092}
43093
43094/*
43095** This routine is called to increment the value of the database file
43096** change-counter, stored as a 4-byte big-endian integer starting at
43097** byte offset 24 of the pager file.  The secondary change counter at
43098** 92 is also updated, as is the SQLite version number at offset 96.
43099**
43100** But this only happens if the pPager->changeCountDone flag is false.
43101** To avoid excess churning of page 1, the update only happens once.
43102** See also the pager_write_changecounter() routine that does an
43103** unconditional update of the change counters.
43104**
43105** If the isDirectMode flag is zero, then this is done by calling
43106** sqlite3PagerWrite() on page 1, then modifying the contents of the
43107** page data. In this case the file will be updated when the current
43108** transaction is committed.
43109**
43110** The isDirectMode flag may only be non-zero if the library was compiled
43111** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
43112** if isDirect is non-zero, then the database file is updated directly
43113** by writing an updated version of page 1 using a call to the
43114** sqlite3OsWrite() function.
43115*/
43116static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
43117  int rc = SQLITE_OK;
43118
43119  assert( pPager->eState==PAGER_WRITER_CACHEMOD
43120       || pPager->eState==PAGER_WRITER_DBMOD
43121  );
43122  assert( assert_pager_state(pPager) );
43123
43124  /* Declare and initialize constant integer 'isDirect'. If the
43125  ** atomic-write optimization is enabled in this build, then isDirect
43126  ** is initialized to the value passed as the isDirectMode parameter
43127  ** to this function. Otherwise, it is always set to zero.
43128  **
43129  ** The idea is that if the atomic-write optimization is not
43130  ** enabled at compile time, the compiler can omit the tests of
43131  ** 'isDirect' below, as well as the block enclosed in the
43132  ** "if( isDirect )" condition.
43133  */
43134#ifndef SQLITE_ENABLE_ATOMIC_WRITE
43135# define DIRECT_MODE 0
43136  assert( isDirectMode==0 );
43137  UNUSED_PARAMETER(isDirectMode);
43138#else
43139# define DIRECT_MODE isDirectMode
43140#endif
43141
43142  if( !pPager->changeCountDone && pPager->dbSize>0 ){
43143    PgHdr *pPgHdr;                /* Reference to page 1 */
43144
43145    assert( !pPager->tempFile && isOpen(pPager->fd) );
43146
43147    /* Open page 1 of the file for writing. */
43148    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
43149    assert( pPgHdr==0 || rc==SQLITE_OK );
43150
43151    /* If page one was fetched successfully, and this function is not
43152    ** operating in direct-mode, make page 1 writable.  When not in
43153    ** direct mode, page 1 is always held in cache and hence the PagerGet()
43154    ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
43155    */
43156    if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
43157      rc = sqlite3PagerWrite(pPgHdr);
43158    }
43159
43160    if( rc==SQLITE_OK ){
43161      /* Actually do the update of the change counter */
43162      pager_write_changecounter(pPgHdr);
43163
43164      /* If running in direct mode, write the contents of page 1 to the file. */
43165      if( DIRECT_MODE ){
43166        const void *zBuf;
43167        assert( pPager->dbFileSize>0 );
43168        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
43169        if( rc==SQLITE_OK ){
43170          rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
43171        }
43172        if( rc==SQLITE_OK ){
43173          pPager->changeCountDone = 1;
43174        }
43175      }else{
43176        pPager->changeCountDone = 1;
43177      }
43178    }
43179
43180    /* Release the page reference. */
43181    sqlite3PagerUnref(pPgHdr);
43182  }
43183  return rc;
43184}
43185
43186/*
43187** Sync the database file to disk. This is a no-op for in-memory databases
43188** or pages with the Pager.noSync flag set.
43189**
43190** If successful, or if called on a pager for which it is a no-op, this
43191** function returns SQLITE_OK. Otherwise, an IO error code is returned.
43192*/
43193SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
43194  int rc = SQLITE_OK;
43195  if( !pPager->noSync ){
43196    assert( !MEMDB );
43197    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
43198  }else if( isOpen(pPager->fd) ){
43199    assert( !MEMDB );
43200    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
43201  }
43202  return rc;
43203}
43204
43205/*
43206** This function may only be called while a write-transaction is active in
43207** rollback. If the connection is in WAL mode, this call is a no-op.
43208** Otherwise, if the connection does not already have an EXCLUSIVE lock on
43209** the database file, an attempt is made to obtain one.
43210**
43211** If the EXCLUSIVE lock is already held or the attempt to obtain it is
43212** successful, or the connection is in WAL mode, SQLITE_OK is returned.
43213** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
43214** returned.
43215*/
43216SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
43217  int rc = SQLITE_OK;
43218  assert( pPager->eState==PAGER_WRITER_CACHEMOD
43219       || pPager->eState==PAGER_WRITER_DBMOD
43220       || pPager->eState==PAGER_WRITER_LOCKED
43221  );
43222  assert( assert_pager_state(pPager) );
43223  if( 0==pagerUseWal(pPager) ){
43224    rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
43225  }
43226  return rc;
43227}
43228
43229/*
43230** Sync the database file for the pager pPager. zMaster points to the name
43231** of a master journal file that should be written into the individual
43232** journal file. zMaster may be NULL, which is interpreted as no master
43233** journal (a single database transaction).
43234**
43235** This routine ensures that:
43236**
43237**   * The database file change-counter is updated,
43238**   * the journal is synced (unless the atomic-write optimization is used),
43239**   * all dirty pages are written to the database file,
43240**   * the database file is truncated (if required), and
43241**   * the database file synced.
43242**
43243** The only thing that remains to commit the transaction is to finalize
43244** (delete, truncate or zero the first part of) the journal file (or
43245** delete the master journal file if specified).
43246**
43247** Note that if zMaster==NULL, this does not overwrite a previous value
43248** passed to an sqlite3PagerCommitPhaseOne() call.
43249**
43250** If the final parameter - noSync - is true, then the database file itself
43251** is not synced. The caller must call sqlite3PagerSync() directly to
43252** sync the database file before calling CommitPhaseTwo() to delete the
43253** journal file in this case.
43254*/
43255SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
43256  Pager *pPager,                  /* Pager object */
43257  const char *zMaster,            /* If not NULL, the master journal name */
43258  int noSync                      /* True to omit the xSync on the db file */
43259){
43260  int rc = SQLITE_OK;             /* Return code */
43261
43262  assert( pPager->eState==PAGER_WRITER_LOCKED
43263       || pPager->eState==PAGER_WRITER_CACHEMOD
43264       || pPager->eState==PAGER_WRITER_DBMOD
43265       || pPager->eState==PAGER_ERROR
43266  );
43267  assert( assert_pager_state(pPager) );
43268
43269  /* If a prior error occurred, report that error again. */
43270  if( NEVER(pPager->errCode) ) return pPager->errCode;
43271
43272  PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
43273      pPager->zFilename, zMaster, pPager->dbSize));
43274
43275  /* If no database changes have been made, return early. */
43276  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
43277
43278  if( MEMDB ){
43279    /* If this is an in-memory db, or no pages have been written to, or this
43280    ** function has already been called, it is mostly a no-op.  However, any
43281    ** backup in progress needs to be restarted.
43282    */
43283    sqlite3BackupRestart(pPager->pBackup);
43284  }else{
43285    if( pagerUseWal(pPager) ){
43286      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
43287      PgHdr *pPageOne = 0;
43288      if( pList==0 ){
43289        /* Must have at least one page for the WAL commit flag.
43290        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
43291        rc = sqlite3PagerGet(pPager, 1, &pPageOne);
43292        pList = pPageOne;
43293        pList->pDirty = 0;
43294      }
43295      assert( rc==SQLITE_OK );
43296      if( ALWAYS(pList) ){
43297        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
43298            (pPager->fullSync ? pPager->syncFlags : 0)
43299        );
43300      }
43301      sqlite3PagerUnref(pPageOne);
43302      if( rc==SQLITE_OK ){
43303        sqlite3PcacheCleanAll(pPager->pPCache);
43304      }
43305    }else{
43306      /* The following block updates the change-counter. Exactly how it
43307      ** does this depends on whether or not the atomic-update optimization
43308      ** was enabled at compile time, and if this transaction meets the
43309      ** runtime criteria to use the operation:
43310      **
43311      **    * The file-system supports the atomic-write property for
43312      **      blocks of size page-size, and
43313      **    * This commit is not part of a multi-file transaction, and
43314      **    * Exactly one page has been modified and store in the journal file.
43315      **
43316      ** If the optimization was not enabled at compile time, then the
43317      ** pager_incr_changecounter() function is called to update the change
43318      ** counter in 'indirect-mode'. If the optimization is compiled in but
43319      ** is not applicable to this transaction, call sqlite3JournalCreate()
43320      ** to make sure the journal file has actually been created, then call
43321      ** pager_incr_changecounter() to update the change-counter in indirect
43322      ** mode.
43323      **
43324      ** Otherwise, if the optimization is both enabled and applicable,
43325      ** then call pager_incr_changecounter() to update the change-counter
43326      ** in 'direct' mode. In this case the journal file will never be
43327      ** created for this transaction.
43328      */
43329  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
43330      PgHdr *pPg;
43331      assert( isOpen(pPager->jfd)
43332           || pPager->journalMode==PAGER_JOURNALMODE_OFF
43333           || pPager->journalMode==PAGER_JOURNALMODE_WAL
43334      );
43335      if( !zMaster && isOpen(pPager->jfd)
43336       && pPager->journalOff==jrnlBufferSize(pPager)
43337       && pPager->dbSize>=pPager->dbOrigSize
43338       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
43339      ){
43340        /* Update the db file change counter via the direct-write method. The
43341        ** following call will modify the in-memory representation of page 1
43342        ** to include the updated change counter and then write page 1
43343        ** directly to the database file. Because of the atomic-write
43344        ** property of the host file-system, this is safe.
43345        */
43346        rc = pager_incr_changecounter(pPager, 1);
43347      }else{
43348        rc = sqlite3JournalCreate(pPager->jfd);
43349        if( rc==SQLITE_OK ){
43350          rc = pager_incr_changecounter(pPager, 0);
43351        }
43352      }
43353  #else
43354      rc = pager_incr_changecounter(pPager, 0);
43355  #endif
43356      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43357
43358      /* If this transaction has made the database smaller, then all pages
43359      ** being discarded by the truncation must be written to the journal
43360      ** file. This can only happen in auto-vacuum mode.
43361      **
43362      ** Before reading the pages with page numbers larger than the
43363      ** current value of Pager.dbSize, set dbSize back to the value
43364      ** that it took at the start of the transaction. Otherwise, the
43365      ** calls to sqlite3PagerGet() return zeroed pages instead of
43366      ** reading data from the database file.
43367      */
43368  #ifndef SQLITE_OMIT_AUTOVACUUM
43369      if( pPager->dbSize<pPager->dbOrigSize
43370       && pPager->journalMode!=PAGER_JOURNALMODE_OFF
43371      ){
43372        Pgno i;                                   /* Iterator variable */
43373        const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
43374        const Pgno dbSize = pPager->dbSize;       /* Database image size */
43375        pPager->dbSize = pPager->dbOrigSize;
43376        for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
43377          if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
43378            PgHdr *pPage;             /* Page to journal */
43379            rc = sqlite3PagerGet(pPager, i, &pPage);
43380            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43381            rc = sqlite3PagerWrite(pPage);
43382            sqlite3PagerUnref(pPage);
43383            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43384          }
43385        }
43386        pPager->dbSize = dbSize;
43387      }
43388  #endif
43389
43390      /* Write the master journal name into the journal file. If a master
43391      ** journal file name has already been written to the journal file,
43392      ** or if zMaster is NULL (no master journal), then this call is a no-op.
43393      */
43394      rc = writeMasterJournal(pPager, zMaster);
43395      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43396
43397      /* Sync the journal file and write all dirty pages to the database.
43398      ** If the atomic-update optimization is being used, this sync will not
43399      ** create the journal file or perform any real IO.
43400      **
43401      ** Because the change-counter page was just modified, unless the
43402      ** atomic-update optimization is used it is almost certain that the
43403      ** journal requires a sync here. However, in locking_mode=exclusive
43404      ** on a system under memory pressure it is just possible that this is
43405      ** not the case. In this case it is likely enough that the redundant
43406      ** xSync() call will be changed to a no-op by the OS anyhow.
43407      */
43408      rc = syncJournal(pPager, 0);
43409      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43410
43411      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
43412      if( rc!=SQLITE_OK ){
43413        assert( rc!=SQLITE_IOERR_BLOCKED );
43414        goto commit_phase_one_exit;
43415      }
43416      sqlite3PcacheCleanAll(pPager->pPCache);
43417
43418      /* If the file on disk is not the same size as the database image,
43419      ** then use pager_truncate to grow or shrink the file here.
43420      */
43421      if( pPager->dbSize!=pPager->dbFileSize ){
43422        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
43423        assert( pPager->eState==PAGER_WRITER_DBMOD );
43424        rc = pager_truncate(pPager, nNew);
43425        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
43426      }
43427
43428      /* Finally, sync the database file. */
43429      if( !noSync ){
43430        rc = sqlite3PagerSync(pPager);
43431      }
43432      IOTRACE(("DBSYNC %p\n", pPager))
43433    }
43434  }
43435
43436commit_phase_one_exit:
43437  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
43438    pPager->eState = PAGER_WRITER_FINISHED;
43439  }
43440  return rc;
43441}
43442
43443
43444/*
43445** When this function is called, the database file has been completely
43446** updated to reflect the changes made by the current transaction and
43447** synced to disk. The journal file still exists in the file-system
43448** though, and if a failure occurs at this point it will eventually
43449** be used as a hot-journal and the current transaction rolled back.
43450**
43451** This function finalizes the journal file, either by deleting,
43452** truncating or partially zeroing it, so that it cannot be used
43453** for hot-journal rollback. Once this is done the transaction is
43454** irrevocably committed.
43455**
43456** If an error occurs, an IO error code is returned and the pager
43457** moves into the error state. Otherwise, SQLITE_OK is returned.
43458*/
43459SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
43460  int rc = SQLITE_OK;                  /* Return code */
43461
43462  /* This routine should not be called if a prior error has occurred.
43463  ** But if (due to a coding error elsewhere in the system) it does get
43464  ** called, just return the same error code without doing anything. */
43465  if( NEVER(pPager->errCode) ) return pPager->errCode;
43466
43467  assert( pPager->eState==PAGER_WRITER_LOCKED
43468       || pPager->eState==PAGER_WRITER_FINISHED
43469       || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
43470  );
43471  assert( assert_pager_state(pPager) );
43472
43473  /* An optimization. If the database was not actually modified during
43474  ** this transaction, the pager is running in exclusive-mode and is
43475  ** using persistent journals, then this function is a no-op.
43476  **
43477  ** The start of the journal file currently contains a single journal
43478  ** header with the nRec field set to 0. If such a journal is used as
43479  ** a hot-journal during hot-journal rollback, 0 changes will be made
43480  ** to the database file. So there is no need to zero the journal
43481  ** header. Since the pager is in exclusive mode, there is no need
43482  ** to drop any locks either.
43483  */
43484  if( pPager->eState==PAGER_WRITER_LOCKED
43485   && pPager->exclusiveMode
43486   && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
43487  ){
43488    assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
43489    pPager->eState = PAGER_READER;
43490    return SQLITE_OK;
43491  }
43492
43493  PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
43494  rc = pager_end_transaction(pPager, pPager->setMaster);
43495  return pager_error(pPager, rc);
43496}
43497
43498/*
43499** If a write transaction is open, then all changes made within the
43500** transaction are reverted and the current write-transaction is closed.
43501** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
43502** state if an error occurs.
43503**
43504** If the pager is already in PAGER_ERROR state when this function is called,
43505** it returns Pager.errCode immediately. No work is performed in this case.
43506**
43507** Otherwise, in rollback mode, this function performs two functions:
43508**
43509**   1) It rolls back the journal file, restoring all database file and
43510**      in-memory cache pages to the state they were in when the transaction
43511**      was opened, and
43512**
43513**   2) It finalizes the journal file, so that it is not used for hot
43514**      rollback at any point in the future.
43515**
43516** Finalization of the journal file (task 2) is only performed if the
43517** rollback is successful.
43518**
43519** In WAL mode, all cache-entries containing data modified within the
43520** current transaction are either expelled from the cache or reverted to
43521** their pre-transaction state by re-reading data from the database or
43522** WAL files. The WAL transaction is then closed.
43523*/
43524SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
43525  int rc = SQLITE_OK;                  /* Return code */
43526  PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
43527
43528  /* PagerRollback() is a no-op if called in READER or OPEN state. If
43529  ** the pager is already in the ERROR state, the rollback is not
43530  ** attempted here. Instead, the error code is returned to the caller.
43531  */
43532  assert( assert_pager_state(pPager) );
43533  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
43534  if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
43535
43536  if( pagerUseWal(pPager) ){
43537    int rc2;
43538    rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
43539    rc2 = pager_end_transaction(pPager, pPager->setMaster);
43540    if( rc==SQLITE_OK ) rc = rc2;
43541  }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
43542    int eState = pPager->eState;
43543    rc = pager_end_transaction(pPager, 0);
43544    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
43545      /* This can happen using journal_mode=off. Move the pager to the error
43546      ** state to indicate that the contents of the cache may not be trusted.
43547      ** Any active readers will get SQLITE_ABORT.
43548      */
43549      pPager->errCode = SQLITE_ABORT;
43550      pPager->eState = PAGER_ERROR;
43551      return rc;
43552    }
43553  }else{
43554    rc = pager_playback(pPager, 0);
43555  }
43556
43557  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
43558  assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
43559
43560  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
43561  ** cache. So call pager_error() on the way out to make any error persistent.
43562  */
43563  return pager_error(pPager, rc);
43564}
43565
43566/*
43567** Return TRUE if the database file is opened read-only.  Return FALSE
43568** if the database is (in theory) writable.
43569*/
43570SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
43571  return pPager->readOnly;
43572}
43573
43574/*
43575** Return the number of references to the pager.
43576*/
43577SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
43578  return sqlite3PcacheRefCount(pPager->pPCache);
43579}
43580
43581/*
43582** Return the approximate number of bytes of memory currently
43583** used by the pager and its associated cache.
43584*/
43585SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
43586  int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
43587                                     + 5*sizeof(void*);
43588  return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
43589           + sqlite3MallocSize(pPager)
43590           + pPager->pageSize;
43591}
43592
43593/*
43594** Return the number of references to the specified page.
43595*/
43596SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
43597  return sqlite3PcachePageRefcount(pPage);
43598}
43599
43600#ifdef SQLITE_TEST
43601/*
43602** This routine is used for testing and analysis only.
43603*/
43604SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
43605  static int a[11];
43606  a[0] = sqlite3PcacheRefCount(pPager->pPCache);
43607  a[1] = sqlite3PcachePagecount(pPager->pPCache);
43608  a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
43609  a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
43610  a[4] = pPager->eState;
43611  a[5] = pPager->errCode;
43612  a[6] = pPager->nHit;
43613  a[7] = pPager->nMiss;
43614  a[8] = 0;  /* Used to be pPager->nOvfl */
43615  a[9] = pPager->nRead;
43616  a[10] = pPager->nWrite;
43617  return a;
43618}
43619#endif
43620
43621/*
43622** Return true if this is an in-memory pager.
43623*/
43624SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
43625  return MEMDB;
43626}
43627
43628/*
43629** Check that there are at least nSavepoint savepoints open. If there are
43630** currently less than nSavepoints open, then open one or more savepoints
43631** to make up the difference. If the number of savepoints is already
43632** equal to nSavepoint, then this function is a no-op.
43633**
43634** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
43635** occurs while opening the sub-journal file, then an IO error code is
43636** returned. Otherwise, SQLITE_OK.
43637*/
43638SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
43639  int rc = SQLITE_OK;                       /* Return code */
43640  int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
43641
43642  assert( pPager->eState>=PAGER_WRITER_LOCKED );
43643  assert( assert_pager_state(pPager) );
43644
43645  if( nSavepoint>nCurrent && pPager->useJournal ){
43646    int ii;                                 /* Iterator variable */
43647    PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
43648
43649    /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
43650    ** if the allocation fails. Otherwise, zero the new portion in case a
43651    ** malloc failure occurs while populating it in the for(...) loop below.
43652    */
43653    aNew = (PagerSavepoint *)sqlite3Realloc(
43654        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
43655    );
43656    if( !aNew ){
43657      return SQLITE_NOMEM;
43658    }
43659    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
43660    pPager->aSavepoint = aNew;
43661
43662    /* Populate the PagerSavepoint structures just allocated. */
43663    for(ii=nCurrent; ii<nSavepoint; ii++){
43664      aNew[ii].nOrig = pPager->dbSize;
43665      if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
43666        aNew[ii].iOffset = pPager->journalOff;
43667      }else{
43668        aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
43669      }
43670      aNew[ii].iSubRec = pPager->nSubRec;
43671      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
43672      if( !aNew[ii].pInSavepoint ){
43673        return SQLITE_NOMEM;
43674      }
43675      if( pagerUseWal(pPager) ){
43676        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
43677      }
43678      pPager->nSavepoint = ii+1;
43679    }
43680    assert( pPager->nSavepoint==nSavepoint );
43681    assertTruncateConstraint(pPager);
43682  }
43683
43684  return rc;
43685}
43686
43687/*
43688** This function is called to rollback or release (commit) a savepoint.
43689** The savepoint to release or rollback need not be the most recently
43690** created savepoint.
43691**
43692** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
43693** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
43694** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
43695** that have occurred since the specified savepoint was created.
43696**
43697** The savepoint to rollback or release is identified by parameter
43698** iSavepoint. A value of 0 means to operate on the outermost savepoint
43699** (the first created). A value of (Pager.nSavepoint-1) means operate
43700** on the most recently created savepoint. If iSavepoint is greater than
43701** (Pager.nSavepoint-1), then this function is a no-op.
43702**
43703** If a negative value is passed to this function, then the current
43704** transaction is rolled back. This is different to calling
43705** sqlite3PagerRollback() because this function does not terminate
43706** the transaction or unlock the database, it just restores the
43707** contents of the database to its original state.
43708**
43709** In any case, all savepoints with an index greater than iSavepoint
43710** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
43711** then savepoint iSavepoint is also destroyed.
43712**
43713** This function may return SQLITE_NOMEM if a memory allocation fails,
43714** or an IO error code if an IO error occurs while rolling back a
43715** savepoint. If no errors occur, SQLITE_OK is returned.
43716*/
43717SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
43718  int rc = pPager->errCode;       /* Return code */
43719
43720  assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
43721  assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
43722
43723  if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
43724    int ii;            /* Iterator variable */
43725    int nNew;          /* Number of remaining savepoints after this op. */
43726
43727    /* Figure out how many savepoints will still be active after this
43728    ** operation. Store this value in nNew. Then free resources associated
43729    ** with any savepoints that are destroyed by this operation.
43730    */
43731    nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
43732    for(ii=nNew; ii<pPager->nSavepoint; ii++){
43733      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
43734    }
43735    pPager->nSavepoint = nNew;
43736
43737    /* If this is a release of the outermost savepoint, truncate
43738    ** the sub-journal to zero bytes in size. */
43739    if( op==SAVEPOINT_RELEASE ){
43740      if( nNew==0 && isOpen(pPager->sjfd) ){
43741        /* Only truncate if it is an in-memory sub-journal. */
43742        if( sqlite3IsMemJournal(pPager->sjfd) ){
43743          rc = sqlite3OsTruncate(pPager->sjfd, 0);
43744          assert( rc==SQLITE_OK );
43745        }
43746        pPager->nSubRec = 0;
43747      }
43748    }
43749    /* Else this is a rollback operation, playback the specified savepoint.
43750    ** If this is a temp-file, it is possible that the journal file has
43751    ** not yet been opened. In this case there have been no changes to
43752    ** the database file, so the playback operation can be skipped.
43753    */
43754    else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
43755      PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
43756      rc = pagerPlaybackSavepoint(pPager, pSavepoint);
43757      assert(rc!=SQLITE_DONE);
43758    }
43759  }
43760
43761  return rc;
43762}
43763
43764/*
43765** Return the full pathname of the database file.
43766*/
43767SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
43768  return pPager->zFilename;
43769}
43770
43771/*
43772** Return the VFS structure for the pager.
43773*/
43774SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
43775  return pPager->pVfs;
43776}
43777
43778/*
43779** Return the file handle for the database file associated
43780** with the pager.  This might return NULL if the file has
43781** not yet been opened.
43782*/
43783SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
43784  return pPager->fd;
43785}
43786
43787/*
43788** Return the full pathname of the journal file.
43789*/
43790SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
43791  return pPager->zJournal;
43792}
43793
43794/*
43795** Return true if fsync() calls are disabled for this pager.  Return FALSE
43796** if fsync()s are executed normally.
43797*/
43798SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
43799  return pPager->noSync;
43800}
43801
43802#ifdef SQLITE_HAS_CODEC
43803/*
43804** Set or retrieve the codec for this pager
43805*/
43806SQLITE_PRIVATE void sqlite3PagerSetCodec(
43807  Pager *pPager,
43808  void *(*xCodec)(void*,void*,Pgno,int),
43809  void (*xCodecSizeChng)(void*,int,int),
43810  void (*xCodecFree)(void*),
43811  void *pCodec
43812){
43813  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
43814  pPager->xCodec = pPager->memDb ? 0 : xCodec;
43815  pPager->xCodecSizeChng = xCodecSizeChng;
43816  pPager->xCodecFree = xCodecFree;
43817  pPager->pCodec = pCodec;
43818  pagerReportSize(pPager);
43819}
43820SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
43821  return pPager->pCodec;
43822}
43823#endif
43824
43825#ifndef SQLITE_OMIT_AUTOVACUUM
43826/*
43827** Move the page pPg to location pgno in the file.
43828**
43829** There must be no references to the page previously located at
43830** pgno (which we call pPgOld) though that page is allowed to be
43831** in cache.  If the page previously located at pgno is not already
43832** in the rollback journal, it is not put there by by this routine.
43833**
43834** References to the page pPg remain valid. Updating any
43835** meta-data associated with pPg (i.e. data stored in the nExtra bytes
43836** allocated along with the page) is the responsibility of the caller.
43837**
43838** A transaction must be active when this routine is called. It used to be
43839** required that a statement transaction was not active, but this restriction
43840** has been removed (CREATE INDEX needs to move a page when a statement
43841** transaction is active).
43842**
43843** If the fourth argument, isCommit, is non-zero, then this page is being
43844** moved as part of a database reorganization just before the transaction
43845** is being committed. In this case, it is guaranteed that the database page
43846** pPg refers to will not be written to again within this transaction.
43847**
43848** This function may return SQLITE_NOMEM or an IO error code if an error
43849** occurs. Otherwise, it returns SQLITE_OK.
43850*/
43851SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
43852  PgHdr *pPgOld;               /* The page being overwritten. */
43853  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
43854  int rc;                      /* Return code */
43855  Pgno origPgno;               /* The original page number */
43856
43857  assert( pPg->nRef>0 );
43858  assert( pPager->eState==PAGER_WRITER_CACHEMOD
43859       || pPager->eState==PAGER_WRITER_DBMOD
43860  );
43861  assert( assert_pager_state(pPager) );
43862
43863  /* In order to be able to rollback, an in-memory database must journal
43864  ** the page we are moving from.
43865  */
43866  if( MEMDB ){
43867    rc = sqlite3PagerWrite(pPg);
43868    if( rc ) return rc;
43869  }
43870
43871  /* If the page being moved is dirty and has not been saved by the latest
43872  ** savepoint, then save the current contents of the page into the
43873  ** sub-journal now. This is required to handle the following scenario:
43874  **
43875  **   BEGIN;
43876  **     <journal page X, then modify it in memory>
43877  **     SAVEPOINT one;
43878  **       <Move page X to location Y>
43879  **     ROLLBACK TO one;
43880  **
43881  ** If page X were not written to the sub-journal here, it would not
43882  ** be possible to restore its contents when the "ROLLBACK TO one"
43883  ** statement were is processed.
43884  **
43885  ** subjournalPage() may need to allocate space to store pPg->pgno into
43886  ** one or more savepoint bitvecs. This is the reason this function
43887  ** may return SQLITE_NOMEM.
43888  */
43889  if( pPg->flags&PGHDR_DIRTY
43890   && subjRequiresPage(pPg)
43891   && SQLITE_OK!=(rc = subjournalPage(pPg))
43892  ){
43893    return rc;
43894  }
43895
43896  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
43897      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
43898  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
43899
43900  /* If the journal needs to be sync()ed before page pPg->pgno can
43901  ** be written to, store pPg->pgno in local variable needSyncPgno.
43902  **
43903  ** If the isCommit flag is set, there is no need to remember that
43904  ** the journal needs to be sync()ed before database page pPg->pgno
43905  ** can be written to. The caller has already promised not to write to it.
43906  */
43907  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
43908    needSyncPgno = pPg->pgno;
43909    assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
43910    assert( pPg->flags&PGHDR_DIRTY );
43911  }
43912
43913  /* If the cache contains a page with page-number pgno, remove it
43914  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
43915  ** page pgno before the 'move' operation, it needs to be retained
43916  ** for the page moved there.
43917  */
43918  pPg->flags &= ~PGHDR_NEED_SYNC;
43919  pPgOld = pager_lookup(pPager, pgno);
43920  assert( !pPgOld || pPgOld->nRef==1 );
43921  if( pPgOld ){
43922    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
43923    if( MEMDB ){
43924      /* Do not discard pages from an in-memory database since we might
43925      ** need to rollback later.  Just move the page out of the way. */
43926      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
43927    }else{
43928      sqlite3PcacheDrop(pPgOld);
43929    }
43930  }
43931
43932  origPgno = pPg->pgno;
43933  sqlite3PcacheMove(pPg, pgno);
43934  sqlite3PcacheMakeDirty(pPg);
43935
43936  /* For an in-memory database, make sure the original page continues
43937  ** to exist, in case the transaction needs to roll back.  Use pPgOld
43938  ** as the original page since it has already been allocated.
43939  */
43940  if( MEMDB ){
43941    assert( pPgOld );
43942    sqlite3PcacheMove(pPgOld, origPgno);
43943    sqlite3PagerUnref(pPgOld);
43944  }
43945
43946  if( needSyncPgno ){
43947    /* If needSyncPgno is non-zero, then the journal file needs to be
43948    ** sync()ed before any data is written to database file page needSyncPgno.
43949    ** Currently, no such page exists in the page-cache and the
43950    ** "is journaled" bitvec flag has been set. This needs to be remedied by
43951    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
43952    ** flag.
43953    **
43954    ** If the attempt to load the page into the page-cache fails, (due
43955    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
43956    ** array. Otherwise, if the page is loaded and written again in
43957    ** this transaction, it may be written to the database file before
43958    ** it is synced into the journal file. This way, it may end up in
43959    ** the journal file twice, but that is not a problem.
43960    */
43961    PgHdr *pPgHdr;
43962    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
43963    if( rc!=SQLITE_OK ){
43964      if( needSyncPgno<=pPager->dbOrigSize ){
43965        assert( pPager->pTmpSpace!=0 );
43966        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
43967      }
43968      return rc;
43969    }
43970    pPgHdr->flags |= PGHDR_NEED_SYNC;
43971    sqlite3PcacheMakeDirty(pPgHdr);
43972    sqlite3PagerUnref(pPgHdr);
43973  }
43974
43975  return SQLITE_OK;
43976}
43977#endif
43978
43979/*
43980** Return a pointer to the data for the specified page.
43981*/
43982SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
43983  assert( pPg->nRef>0 || pPg->pPager->memDb );
43984  return pPg->pData;
43985}
43986
43987/*
43988** Return a pointer to the Pager.nExtra bytes of "extra" space
43989** allocated along with the specified page.
43990*/
43991SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
43992  return pPg->pExtra;
43993}
43994
43995/*
43996** Get/set the locking-mode for this pager. Parameter eMode must be one
43997** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
43998** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
43999** the locking-mode is set to the value specified.
44000**
44001** The returned value is either PAGER_LOCKINGMODE_NORMAL or
44002** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
44003** locking-mode.
44004*/
44005SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
44006  assert( eMode==PAGER_LOCKINGMODE_QUERY
44007            || eMode==PAGER_LOCKINGMODE_NORMAL
44008            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
44009  assert( PAGER_LOCKINGMODE_QUERY<0 );
44010  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
44011  assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
44012  if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
44013    pPager->exclusiveMode = (u8)eMode;
44014  }
44015  return (int)pPager->exclusiveMode;
44016}
44017
44018/*
44019** Set the journal-mode for this pager. Parameter eMode must be one of:
44020**
44021**    PAGER_JOURNALMODE_DELETE
44022**    PAGER_JOURNALMODE_TRUNCATE
44023**    PAGER_JOURNALMODE_PERSIST
44024**    PAGER_JOURNALMODE_OFF
44025**    PAGER_JOURNALMODE_MEMORY
44026**    PAGER_JOURNALMODE_WAL
44027**
44028** The journalmode is set to the value specified if the change is allowed.
44029** The change may be disallowed for the following reasons:
44030**
44031**   *  An in-memory database can only have its journal_mode set to _OFF
44032**      or _MEMORY.
44033**
44034**   *  Temporary databases cannot have _WAL journalmode.
44035**
44036** The returned indicate the current (possibly updated) journal-mode.
44037*/
44038SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
44039  u8 eOld = pPager->journalMode;    /* Prior journalmode */
44040
44041#ifdef SQLITE_DEBUG
44042  /* The print_pager_state() routine is intended to be used by the debugger
44043  ** only.  We invoke it once here to suppress a compiler warning. */
44044  print_pager_state(pPager);
44045#endif
44046
44047
44048  /* The eMode parameter is always valid */
44049  assert(      eMode==PAGER_JOURNALMODE_DELETE
44050            || eMode==PAGER_JOURNALMODE_TRUNCATE
44051            || eMode==PAGER_JOURNALMODE_PERSIST
44052            || eMode==PAGER_JOURNALMODE_OFF
44053            || eMode==PAGER_JOURNALMODE_WAL
44054            || eMode==PAGER_JOURNALMODE_MEMORY );
44055
44056  /* This routine is only called from the OP_JournalMode opcode, and
44057  ** the logic there will never allow a temporary file to be changed
44058  ** to WAL mode.
44059  */
44060  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
44061
44062  /* Do allow the journalmode of an in-memory database to be set to
44063  ** anything other than MEMORY or OFF
44064  */
44065  if( MEMDB ){
44066    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
44067    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
44068      eMode = eOld;
44069    }
44070  }
44071
44072  if( eMode!=eOld ){
44073
44074    /* Change the journal mode. */
44075    assert( pPager->eState!=PAGER_ERROR );
44076    pPager->journalMode = (u8)eMode;
44077
44078    /* When transistioning from TRUNCATE or PERSIST to any other journal
44079    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
44080    ** delete the journal file.
44081    */
44082    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
44083    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
44084    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
44085    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
44086    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
44087    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
44088
44089    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
44090    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
44091
44092      /* In this case we would like to delete the journal file. If it is
44093      ** not possible, then that is not a problem. Deleting the journal file
44094      ** here is an optimization only.
44095      **
44096      ** Before deleting the journal file, obtain a RESERVED lock on the
44097      ** database file. This ensures that the journal file is not deleted
44098      ** while it is in use by some other client.
44099      */
44100      sqlite3OsClose(pPager->jfd);
44101      if( pPager->eLock>=RESERVED_LOCK ){
44102        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
44103      }else{
44104        int rc = SQLITE_OK;
44105        int state = pPager->eState;
44106        assert( state==PAGER_OPEN || state==PAGER_READER );
44107        if( state==PAGER_OPEN ){
44108          rc = sqlite3PagerSharedLock(pPager);
44109        }
44110        if( pPager->eState==PAGER_READER ){
44111          assert( rc==SQLITE_OK );
44112          rc = pagerLockDb(pPager, RESERVED_LOCK);
44113        }
44114        if( rc==SQLITE_OK ){
44115          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
44116        }
44117        if( rc==SQLITE_OK && state==PAGER_READER ){
44118          pagerUnlockDb(pPager, SHARED_LOCK);
44119        }else if( state==PAGER_OPEN ){
44120          pager_unlock(pPager);
44121        }
44122        assert( state==pPager->eState );
44123      }
44124    }
44125  }
44126
44127  /* Return the new journal mode */
44128  return (int)pPager->journalMode;
44129}
44130
44131/*
44132** Return the current journal mode.
44133*/
44134SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
44135  return (int)pPager->journalMode;
44136}
44137
44138/*
44139** Return TRUE if the pager is in a state where it is OK to change the
44140** journalmode.  Journalmode changes can only happen when the database
44141** is unmodified.
44142*/
44143SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
44144  assert( assert_pager_state(pPager) );
44145  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
44146  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
44147  return 1;
44148}
44149
44150/*
44151** Get/set the size-limit used for persistent journal files.
44152**
44153** Setting the size limit to -1 means no limit is enforced.
44154** An attempt to set a limit smaller than -1 is a no-op.
44155*/
44156SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
44157  if( iLimit>=-1 ){
44158    pPager->journalSizeLimit = iLimit;
44159    sqlite3WalLimit(pPager->pWal, iLimit);
44160  }
44161  return pPager->journalSizeLimit;
44162}
44163
44164/*
44165** Return a pointer to the pPager->pBackup variable. The backup module
44166** in backup.c maintains the content of this variable. This module
44167** uses it opaquely as an argument to sqlite3BackupRestart() and
44168** sqlite3BackupUpdate() only.
44169*/
44170SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
44171  return &pPager->pBackup;
44172}
44173
44174#ifndef SQLITE_OMIT_WAL
44175/*
44176** This function is called when the user invokes "PRAGMA wal_checkpoint",
44177** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
44178** or wal_blocking_checkpoint() API functions.
44179**
44180** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
44181*/
44182SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
44183  int rc = SQLITE_OK;
44184  if( pPager->pWal ){
44185    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
44186        pPager->xBusyHandler, pPager->pBusyHandlerArg,
44187        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
44188        pnLog, pnCkpt
44189    );
44190  }
44191  return rc;
44192}
44193
44194SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
44195  return sqlite3WalCallback(pPager->pWal);
44196}
44197
44198/*
44199** Return true if the underlying VFS for the given pager supports the
44200** primitives necessary for write-ahead logging.
44201*/
44202SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
44203  const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
44204  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
44205}
44206
44207/*
44208** Attempt to take an exclusive lock on the database file. If a PENDING lock
44209** is obtained instead, immediately release it.
44210*/
44211static int pagerExclusiveLock(Pager *pPager){
44212  int rc;                         /* Return code */
44213
44214  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
44215  rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
44216  if( rc!=SQLITE_OK ){
44217    /* If the attempt to grab the exclusive lock failed, release the
44218    ** pending lock that may have been obtained instead.  */
44219    pagerUnlockDb(pPager, SHARED_LOCK);
44220  }
44221
44222  return rc;
44223}
44224
44225/*
44226** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
44227** exclusive-locking mode when this function is called, take an EXCLUSIVE
44228** lock on the database file and use heap-memory to store the wal-index
44229** in. Otherwise, use the normal shared-memory.
44230*/
44231static int pagerOpenWal(Pager *pPager){
44232  int rc = SQLITE_OK;
44233
44234  assert( pPager->pWal==0 && pPager->tempFile==0 );
44235  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager->noReadlock);
44236
44237  /* If the pager is already in exclusive-mode, the WAL module will use
44238  ** heap-memory for the wal-index instead of the VFS shared-memory
44239  ** implementation. Take the exclusive lock now, before opening the WAL
44240  ** file, to make sure this is safe.
44241  */
44242  if( pPager->exclusiveMode ){
44243    rc = pagerExclusiveLock(pPager);
44244  }
44245
44246  /* Open the connection to the log file. If this operation fails,
44247  ** (e.g. due to malloc() failure), return an error code.
44248  */
44249  if( rc==SQLITE_OK ){
44250    rc = sqlite3WalOpen(pPager->pVfs,
44251        pPager->fd, pPager->zWal, pPager->exclusiveMode,
44252        pPager->journalSizeLimit, &pPager->pWal
44253    );
44254  }
44255
44256  return rc;
44257}
44258
44259
44260/*
44261** The caller must be holding a SHARED lock on the database file to call
44262** this function.
44263**
44264** If the pager passed as the first argument is open on a real database
44265** file (not a temp file or an in-memory database), and the WAL file
44266** is not already open, make an attempt to open it now. If successful,
44267** return SQLITE_OK. If an error occurs or the VFS used by the pager does
44268** not support the xShmXXX() methods, return an error code. *pbOpen is
44269** not modified in either case.
44270**
44271** If the pager is open on a temp-file (or in-memory database), or if
44272** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
44273** without doing anything.
44274*/
44275SQLITE_PRIVATE int sqlite3PagerOpenWal(
44276  Pager *pPager,                  /* Pager object */
44277  int *pbOpen                     /* OUT: Set to true if call is a no-op */
44278){
44279  int rc = SQLITE_OK;             /* Return code */
44280
44281  assert( assert_pager_state(pPager) );
44282  assert( pPager->eState==PAGER_OPEN   || pbOpen );
44283  assert( pPager->eState==PAGER_READER || !pbOpen );
44284  assert( pbOpen==0 || *pbOpen==0 );
44285  assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
44286
44287  if( !pPager->tempFile && !pPager->pWal ){
44288    if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
44289
44290    /* Close any rollback journal previously open */
44291    sqlite3OsClose(pPager->jfd);
44292
44293    rc = pagerOpenWal(pPager);
44294    if( rc==SQLITE_OK ){
44295      pPager->journalMode = PAGER_JOURNALMODE_WAL;
44296      pPager->eState = PAGER_OPEN;
44297    }
44298  }else{
44299    *pbOpen = 1;
44300  }
44301
44302  return rc;
44303}
44304
44305/*
44306** This function is called to close the connection to the log file prior
44307** to switching from WAL to rollback mode.
44308**
44309** Before closing the log file, this function attempts to take an
44310** EXCLUSIVE lock on the database file. If this cannot be obtained, an
44311** error (SQLITE_BUSY) is returned and the log connection is not closed.
44312** If successful, the EXCLUSIVE lock is not released before returning.
44313*/
44314SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
44315  int rc = SQLITE_OK;
44316
44317  assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
44318
44319  /* If the log file is not already open, but does exist in the file-system,
44320  ** it may need to be checkpointed before the connection can switch to
44321  ** rollback mode. Open it now so this can happen.
44322  */
44323  if( !pPager->pWal ){
44324    int logexists = 0;
44325    rc = pagerLockDb(pPager, SHARED_LOCK);
44326    if( rc==SQLITE_OK ){
44327      rc = sqlite3OsAccess(
44328          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
44329      );
44330    }
44331    if( rc==SQLITE_OK && logexists ){
44332      rc = pagerOpenWal(pPager);
44333    }
44334  }
44335
44336  /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
44337  ** the database file, the log and log-summary files will be deleted.
44338  */
44339  if( rc==SQLITE_OK && pPager->pWal ){
44340    rc = pagerExclusiveLock(pPager);
44341    if( rc==SQLITE_OK ){
44342      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
44343                           pPager->pageSize, (u8*)pPager->pTmpSpace);
44344      pPager->pWal = 0;
44345    }
44346  }
44347  return rc;
44348}
44349
44350#ifdef SQLITE_HAS_CODEC
44351/*
44352** This function is called by the wal module when writing page content
44353** into the log file.
44354**
44355** This function returns a pointer to a buffer containing the encrypted
44356** page content. If a malloc fails, this function may return NULL.
44357*/
44358SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
44359  void *aData = 0;
44360  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
44361  return aData;
44362}
44363#endif /* SQLITE_HAS_CODEC */
44364
44365#endif /* !SQLITE_OMIT_WAL */
44366
44367#endif /* SQLITE_OMIT_DISKIO */
44368
44369/************** End of pager.c ***********************************************/
44370/************** Begin file wal.c *********************************************/
44371/*
44372** 2010 February 1
44373**
44374** The author disclaims copyright to this source code.  In place of
44375** a legal notice, here is a blessing:
44376**
44377**    May you do good and not evil.
44378**    May you find forgiveness for yourself and forgive others.
44379**    May you share freely, never taking more than you give.
44380**
44381*************************************************************************
44382**
44383** This file contains the implementation of a write-ahead log (WAL) used in
44384** "journal_mode=WAL" mode.
44385**
44386** WRITE-AHEAD LOG (WAL) FILE FORMAT
44387**
44388** A WAL file consists of a header followed by zero or more "frames".
44389** Each frame records the revised content of a single page from the
44390** database file.  All changes to the database are recorded by writing
44391** frames into the WAL.  Transactions commit when a frame is written that
44392** contains a commit marker.  A single WAL can and usually does record
44393** multiple transactions.  Periodically, the content of the WAL is
44394** transferred back into the database file in an operation called a
44395** "checkpoint".
44396**
44397** A single WAL file can be used multiple times.  In other words, the
44398** WAL can fill up with frames and then be checkpointed and then new
44399** frames can overwrite the old ones.  A WAL always grows from beginning
44400** toward the end.  Checksums and counters attached to each frame are
44401** used to determine which frames within the WAL are valid and which
44402** are leftovers from prior checkpoints.
44403**
44404** The WAL header is 32 bytes in size and consists of the following eight
44405** big-endian 32-bit unsigned integer values:
44406**
44407**     0: Magic number.  0x377f0682 or 0x377f0683
44408**     4: File format version.  Currently 3007000
44409**     8: Database page size.  Example: 1024
44410**    12: Checkpoint sequence number
44411**    16: Salt-1, random integer incremented with each checkpoint
44412**    20: Salt-2, a different random integer changing with each ckpt
44413**    24: Checksum-1 (first part of checksum for first 24 bytes of header).
44414**    28: Checksum-2 (second part of checksum for first 24 bytes of header).
44415**
44416** Immediately following the wal-header are zero or more frames. Each
44417** frame consists of a 24-byte frame-header followed by a <page-size> bytes
44418** of page data. The frame-header is six big-endian 32-bit unsigned
44419** integer values, as follows:
44420**
44421**     0: Page number.
44422**     4: For commit records, the size of the database image in pages
44423**        after the commit. For all other records, zero.
44424**     8: Salt-1 (copied from the header)
44425**    12: Salt-2 (copied from the header)
44426**    16: Checksum-1.
44427**    20: Checksum-2.
44428**
44429** A frame is considered valid if and only if the following conditions are
44430** true:
44431**
44432**    (1) The salt-1 and salt-2 values in the frame-header match
44433**        salt values in the wal-header
44434**
44435**    (2) The checksum values in the final 8 bytes of the frame-header
44436**        exactly match the checksum computed consecutively on the
44437**        WAL header and the first 8 bytes and the content of all frames
44438**        up to and including the current frame.
44439**
44440** The checksum is computed using 32-bit big-endian integers if the
44441** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
44442** is computed using little-endian if the magic number is 0x377f0682.
44443** The checksum values are always stored in the frame header in a
44444** big-endian format regardless of which byte order is used to compute
44445** the checksum.  The checksum is computed by interpreting the input as
44446** an even number of unsigned 32-bit integers: x[0] through x[N].  The
44447** algorithm used for the checksum is as follows:
44448**
44449**   for i from 0 to n-1 step 2:
44450**     s0 += x[i] + s1;
44451**     s1 += x[i+1] + s0;
44452**   endfor
44453**
44454** Note that s0 and s1 are both weighted checksums using fibonacci weights
44455** in reverse order (the largest fibonacci weight occurs on the first element
44456** of the sequence being summed.)  The s1 value spans all 32-bit
44457** terms of the sequence whereas s0 omits the final term.
44458**
44459** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
44460** WAL is transferred into the database, then the database is VFS.xSync-ed.
44461** The VFS.xSync operations serve as write barriers - all writes launched
44462** before the xSync must complete before any write that launches after the
44463** xSync begins.
44464**
44465** After each checkpoint, the salt-1 value is incremented and the salt-2
44466** value is randomized.  This prevents old and new frames in the WAL from
44467** being considered valid at the same time and being checkpointing together
44468** following a crash.
44469**
44470** READER ALGORITHM
44471**
44472** To read a page from the database (call it page number P), a reader
44473** first checks the WAL to see if it contains page P.  If so, then the
44474** last valid instance of page P that is a followed by a commit frame
44475** or is a commit frame itself becomes the value read.  If the WAL
44476** contains no copies of page P that are valid and which are a commit
44477** frame or are followed by a commit frame, then page P is read from
44478** the database file.
44479**
44480** To start a read transaction, the reader records the index of the last
44481** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
44482** for all subsequent read operations.  New transactions can be appended
44483** to the WAL, but as long as the reader uses its original mxFrame value
44484** and ignores the newly appended content, it will see a consistent snapshot
44485** of the database from a single point in time.  This technique allows
44486** multiple concurrent readers to view different versions of the database
44487** content simultaneously.
44488**
44489** The reader algorithm in the previous paragraphs works correctly, but
44490** because frames for page P can appear anywhere within the WAL, the
44491** reader has to scan the entire WAL looking for page P frames.  If the
44492** WAL is large (multiple megabytes is typical) that scan can be slow,
44493** and read performance suffers.  To overcome this problem, a separate
44494** data structure called the wal-index is maintained to expedite the
44495** search for frames of a particular page.
44496**
44497** WAL-INDEX FORMAT
44498**
44499** Conceptually, the wal-index is shared memory, though VFS implementations
44500** might choose to implement the wal-index using a mmapped file.  Because
44501** the wal-index is shared memory, SQLite does not support journal_mode=WAL
44502** on a network filesystem.  All users of the database must be able to
44503** share memory.
44504**
44505** The wal-index is transient.  After a crash, the wal-index can (and should
44506** be) reconstructed from the original WAL file.  In fact, the VFS is required
44507** to either truncate or zero the header of the wal-index when the last
44508** connection to it closes.  Because the wal-index is transient, it can
44509** use an architecture-specific format; it does not have to be cross-platform.
44510** Hence, unlike the database and WAL file formats which store all values
44511** as big endian, the wal-index can store multi-byte values in the native
44512** byte order of the host computer.
44513**
44514** The purpose of the wal-index is to answer this question quickly:  Given
44515** a page number P, return the index of the last frame for page P in the WAL,
44516** or return NULL if there are no frames for page P in the WAL.
44517**
44518** The wal-index consists of a header region, followed by an one or
44519** more index blocks.
44520**
44521** The wal-index header contains the total number of frames within the WAL
44522** in the the mxFrame field.
44523**
44524** Each index block except for the first contains information on
44525** HASHTABLE_NPAGE frames. The first index block contains information on
44526** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
44527** HASHTABLE_NPAGE are selected so that together the wal-index header and
44528** first index block are the same size as all other index blocks in the
44529** wal-index.
44530**
44531** Each index block contains two sections, a page-mapping that contains the
44532** database page number associated with each wal frame, and a hash-table
44533** that allows readers to query an index block for a specific page number.
44534** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
44535** for the first index block) 32-bit page numbers. The first entry in the
44536** first index-block contains the database page number corresponding to the
44537** first frame in the WAL file. The first entry in the second index block
44538** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
44539** the log, and so on.
44540**
44541** The last index block in a wal-index usually contains less than the full
44542** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
44543** depending on the contents of the WAL file. This does not change the
44544** allocated size of the page-mapping array - the page-mapping array merely
44545** contains unused entries.
44546**
44547** Even without using the hash table, the last frame for page P
44548** can be found by scanning the page-mapping sections of each index block
44549** starting with the last index block and moving toward the first, and
44550** within each index block, starting at the end and moving toward the
44551** beginning.  The first entry that equals P corresponds to the frame
44552** holding the content for that page.
44553**
44554** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
44555** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
44556** hash table for each page number in the mapping section, so the hash
44557** table is never more than half full.  The expected number of collisions
44558** prior to finding a match is 1.  Each entry of the hash table is an
44559** 1-based index of an entry in the mapping section of the same
44560** index block.   Let K be the 1-based index of the largest entry in
44561** the mapping section.  (For index blocks other than the last, K will
44562** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
44563** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
44564** contain a value of 0.
44565**
44566** To look for page P in the hash table, first compute a hash iKey on
44567** P as follows:
44568**
44569**      iKey = (P * 383) % HASHTABLE_NSLOT
44570**
44571** Then start scanning entries of the hash table, starting with iKey
44572** (wrapping around to the beginning when the end of the hash table is
44573** reached) until an unused hash slot is found. Let the first unused slot
44574** be at index iUnused.  (iUnused might be less than iKey if there was
44575** wrap-around.) Because the hash table is never more than half full,
44576** the search is guaranteed to eventually hit an unused entry.  Let
44577** iMax be the value between iKey and iUnused, closest to iUnused,
44578** where aHash[iMax]==P.  If there is no iMax entry (if there exists
44579** no hash slot such that aHash[i]==p) then page P is not in the
44580** current index block.  Otherwise the iMax-th mapping entry of the
44581** current index block corresponds to the last entry that references
44582** page P.
44583**
44584** A hash search begins with the last index block and moves toward the
44585** first index block, looking for entries corresponding to page P.  On
44586** average, only two or three slots in each index block need to be
44587** examined in order to either find the last entry for page P, or to
44588** establish that no such entry exists in the block.  Each index block
44589** holds over 4000 entries.  So two or three index blocks are sufficient
44590** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
44591** comparisons (on average) suffice to either locate a frame in the
44592** WAL or to establish that the frame does not exist in the WAL.  This
44593** is much faster than scanning the entire 10MB WAL.
44594**
44595** Note that entries are added in order of increasing K.  Hence, one
44596** reader might be using some value K0 and a second reader that started
44597** at a later time (after additional transactions were added to the WAL
44598** and to the wal-index) might be using a different value K1, where K1>K0.
44599** Both readers can use the same hash table and mapping section to get
44600** the correct result.  There may be entries in the hash table with
44601** K>K0 but to the first reader, those entries will appear to be unused
44602** slots in the hash table and so the first reader will get an answer as
44603** if no values greater than K0 had ever been inserted into the hash table
44604** in the first place - which is what reader one wants.  Meanwhile, the
44605** second reader using K1 will see additional values that were inserted
44606** later, which is exactly what reader two wants.
44607**
44608** When a rollback occurs, the value of K is decreased. Hash table entries
44609** that correspond to frames greater than the new K value are removed
44610** from the hash table at this point.
44611*/
44612#ifndef SQLITE_OMIT_WAL
44613
44614
44615/*
44616** Trace output macros
44617*/
44618#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
44619SQLITE_PRIVATE int sqlite3WalTrace = 0;
44620# define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
44621#else
44622# define WALTRACE(X)
44623#endif
44624
44625/*
44626** The maximum (and only) versions of the wal and wal-index formats
44627** that may be interpreted by this version of SQLite.
44628**
44629** If a client begins recovering a WAL file and finds that (a) the checksum
44630** values in the wal-header are correct and (b) the version field is not
44631** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
44632**
44633** Similarly, if a client successfully reads a wal-index header (i.e. the
44634** checksum test is successful) and finds that the version field is not
44635** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
44636** returns SQLITE_CANTOPEN.
44637*/
44638#define WAL_MAX_VERSION      3007000
44639#define WALINDEX_MAX_VERSION 3007000
44640
44641/*
44642** Indices of various locking bytes.   WAL_NREADER is the number
44643** of available reader locks and should be at least 3.
44644*/
44645#define WAL_WRITE_LOCK         0
44646#define WAL_ALL_BUT_WRITE      1
44647#define WAL_CKPT_LOCK          1
44648#define WAL_RECOVER_LOCK       2
44649#define WAL_READ_LOCK(I)       (3+(I))
44650#define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
44651
44652
44653/* Object declarations */
44654typedef struct WalIndexHdr WalIndexHdr;
44655typedef struct WalIterator WalIterator;
44656typedef struct WalCkptInfo WalCkptInfo;
44657
44658
44659/*
44660** The following object holds a copy of the wal-index header content.
44661**
44662** The actual header in the wal-index consists of two copies of this
44663** object.
44664**
44665** The szPage value can be any power of 2 between 512 and 32768, inclusive.
44666** Or it can be 1 to represent a 65536-byte page.  The latter case was
44667** added in 3.7.1 when support for 64K pages was added.
44668*/
44669struct WalIndexHdr {
44670  u32 iVersion;                   /* Wal-index version */
44671  u32 unused;                     /* Unused (padding) field */
44672  u32 iChange;                    /* Counter incremented each transaction */
44673  u8 isInit;                      /* 1 when initialized */
44674  u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
44675  u16 szPage;                     /* Database page size in bytes. 1==64K */
44676  u32 mxFrame;                    /* Index of last valid frame in the WAL */
44677  u32 nPage;                      /* Size of database in pages */
44678  u32 aFrameCksum[2];             /* Checksum of last frame in log */
44679  u32 aSalt[2];                   /* Two salt values copied from WAL header */
44680  u32 aCksum[2];                  /* Checksum over all prior fields */
44681};
44682
44683/*
44684** A copy of the following object occurs in the wal-index immediately
44685** following the second copy of the WalIndexHdr.  This object stores
44686** information used by checkpoint.
44687**
44688** nBackfill is the number of frames in the WAL that have been written
44689** back into the database. (We call the act of moving content from WAL to
44690** database "backfilling".)  The nBackfill number is never greater than
44691** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
44692** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
44693** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
44694** mxFrame back to zero when the WAL is reset.
44695**
44696** There is one entry in aReadMark[] for each reader lock.  If a reader
44697** holds read-lock K, then the value in aReadMark[K] is no greater than
44698** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
44699** for any aReadMark[] means that entry is unused.  aReadMark[0] is
44700** a special case; its value is never used and it exists as a place-holder
44701** to avoid having to offset aReadMark[] indexs by one.  Readers holding
44702** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
44703** directly from the database.
44704**
44705** The value of aReadMark[K] may only be changed by a thread that
44706** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
44707** aReadMark[K] cannot changed while there is a reader is using that mark
44708** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
44709**
44710** The checkpointer may only transfer frames from WAL to database where
44711** the frame numbers are less than or equal to every aReadMark[] that is
44712** in use (that is, every aReadMark[j] for which there is a corresponding
44713** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
44714** largest value and will increase an unused aReadMark[] to mxFrame if there
44715** is not already an aReadMark[] equal to mxFrame.  The exception to the
44716** previous sentence is when nBackfill equals mxFrame (meaning that everything
44717** in the WAL has been backfilled into the database) then new readers
44718** will choose aReadMark[0] which has value 0 and hence such reader will
44719** get all their all content directly from the database file and ignore
44720** the WAL.
44721**
44722** Writers normally append new frames to the end of the WAL.  However,
44723** if nBackfill equals mxFrame (meaning that all WAL content has been
44724** written back into the database) and if no readers are using the WAL
44725** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
44726** the writer will first "reset" the WAL back to the beginning and start
44727** writing new content beginning at frame 1.
44728**
44729** We assume that 32-bit loads are atomic and so no locks are needed in
44730** order to read from any aReadMark[] entries.
44731*/
44732struct WalCkptInfo {
44733  u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
44734  u32 aReadMark[WAL_NREADER];     /* Reader marks */
44735};
44736#define READMARK_NOT_USED  0xffffffff
44737
44738
44739/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
44740** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
44741** only support mandatory file-locks, we do not read or write data
44742** from the region of the file on which locks are applied.
44743*/
44744#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
44745#define WALINDEX_LOCK_RESERVED 16
44746#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
44747
44748/* Size of header before each frame in wal */
44749#define WAL_FRAME_HDRSIZE 24
44750
44751/* Size of write ahead log header, including checksum. */
44752/* #define WAL_HDRSIZE 24 */
44753#define WAL_HDRSIZE 32
44754
44755/* WAL magic value. Either this value, or the same value with the least
44756** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
44757** big-endian format in the first 4 bytes of a WAL file.
44758**
44759** If the LSB is set, then the checksums for each frame within the WAL
44760** file are calculated by treating all data as an array of 32-bit
44761** big-endian words. Otherwise, they are calculated by interpreting
44762** all data as 32-bit little-endian words.
44763*/
44764#define WAL_MAGIC 0x377f0682
44765
44766/*
44767** Return the offset of frame iFrame in the write-ahead log file,
44768** assuming a database page size of szPage bytes. The offset returned
44769** is to the start of the write-ahead log frame-header.
44770*/
44771#define walFrameOffset(iFrame, szPage) (                               \
44772  WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
44773)
44774
44775/*
44776** An open write-ahead log file is represented by an instance of the
44777** following object.
44778*/
44779struct Wal {
44780  sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
44781  sqlite3_file *pDbFd;       /* File handle for the database file */
44782  sqlite3_file *pWalFd;      /* File handle for WAL file */
44783  u32 iCallback;             /* Value to pass to log callback (or 0) */
44784  i64 mxWalSize;             /* Truncate WAL to this size upon reset */
44785  int nWiData;               /* Size of array apWiData */
44786  volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
44787  u32 szPage;                /* Database page size */
44788  i16 readLock;              /* Which read lock is being held.  -1 for none */
44789  u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
44790  u8 writeLock;              /* True if in a write transaction */
44791  u8 ckptLock;               /* True if holding a checkpoint lock */
44792  u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
44793  WalIndexHdr hdr;           /* Wal-index header for current transaction */
44794  const char *zWalName;      /* Name of WAL file */
44795  u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
44796#ifdef SQLITE_DEBUG
44797  u8 lockError;              /* True if a locking error has occurred */
44798#endif
44799};
44800
44801/*
44802** Candidate values for Wal.exclusiveMode.
44803*/
44804#define WAL_NORMAL_MODE     0
44805#define WAL_EXCLUSIVE_MODE  1
44806#define WAL_HEAPMEMORY_MODE 2
44807
44808/*
44809** Possible values for WAL.readOnly
44810*/
44811#define WAL_RDWR        0    /* Normal read/write connection */
44812#define WAL_RDONLY      1    /* The WAL file is readonly */
44813#define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
44814
44815/*
44816** Each page of the wal-index mapping contains a hash-table made up of
44817** an array of HASHTABLE_NSLOT elements of the following type.
44818*/
44819typedef u16 ht_slot;
44820
44821/*
44822** This structure is used to implement an iterator that loops through
44823** all frames in the WAL in database page order. Where two or more frames
44824** correspond to the same database page, the iterator visits only the
44825** frame most recently written to the WAL (in other words, the frame with
44826** the largest index).
44827**
44828** The internals of this structure are only accessed by:
44829**
44830**   walIteratorInit() - Create a new iterator,
44831**   walIteratorNext() - Step an iterator,
44832**   walIteratorFree() - Free an iterator.
44833**
44834** This functionality is used by the checkpoint code (see walCheckpoint()).
44835*/
44836struct WalIterator {
44837  int iPrior;                     /* Last result returned from the iterator */
44838  int nSegment;                   /* Number of entries in aSegment[] */
44839  struct WalSegment {
44840    int iNext;                    /* Next slot in aIndex[] not yet returned */
44841    ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
44842    u32 *aPgno;                   /* Array of page numbers. */
44843    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
44844    int iZero;                    /* Frame number associated with aPgno[0] */
44845  } aSegment[1];                  /* One for every 32KB page in the wal-index */
44846};
44847
44848/*
44849** Define the parameters of the hash tables in the wal-index file. There
44850** is a hash-table following every HASHTABLE_NPAGE page numbers in the
44851** wal-index.
44852**
44853** Changing any of these constants will alter the wal-index format and
44854** create incompatibilities.
44855*/
44856#define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
44857#define HASHTABLE_HASH_1     383                  /* Should be prime */
44858#define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
44859
44860/*
44861** The block of page numbers associated with the first hash-table in a
44862** wal-index is smaller than usual. This is so that there is a complete
44863** hash-table on each aligned 32KB page of the wal-index.
44864*/
44865#define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
44866
44867/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
44868#define WALINDEX_PGSZ   (                                         \
44869    sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
44870)
44871
44872/*
44873** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
44874** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
44875** numbered from zero.
44876**
44877** If this call is successful, *ppPage is set to point to the wal-index
44878** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
44879** then an SQLite error code is returned and *ppPage is set to 0.
44880*/
44881static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
44882  int rc = SQLITE_OK;
44883
44884  /* Enlarge the pWal->apWiData[] array if required */
44885  if( pWal->nWiData<=iPage ){
44886    int nByte = sizeof(u32*)*(iPage+1);
44887    volatile u32 **apNew;
44888    apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
44889    if( !apNew ){
44890      *ppPage = 0;
44891      return SQLITE_NOMEM;
44892    }
44893    memset((void*)&apNew[pWal->nWiData], 0,
44894           sizeof(u32*)*(iPage+1-pWal->nWiData));
44895    pWal->apWiData = apNew;
44896    pWal->nWiData = iPage+1;
44897  }
44898
44899  /* Request a pointer to the required page from the VFS */
44900  if( pWal->apWiData[iPage]==0 ){
44901    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
44902      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
44903      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
44904    }else{
44905      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
44906          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
44907      );
44908      if( rc==SQLITE_READONLY ){
44909        pWal->readOnly |= WAL_SHM_RDONLY;
44910        rc = SQLITE_OK;
44911      }
44912    }
44913  }
44914
44915  *ppPage = pWal->apWiData[iPage];
44916  assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
44917  return rc;
44918}
44919
44920/*
44921** Return a pointer to the WalCkptInfo structure in the wal-index.
44922*/
44923static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
44924  assert( pWal->nWiData>0 && pWal->apWiData[0] );
44925  return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
44926}
44927
44928/*
44929** Return a pointer to the WalIndexHdr structure in the wal-index.
44930*/
44931static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
44932  assert( pWal->nWiData>0 && pWal->apWiData[0] );
44933  return (volatile WalIndexHdr*)pWal->apWiData[0];
44934}
44935
44936/*
44937** The argument to this macro must be of type u32. On a little-endian
44938** architecture, it returns the u32 value that results from interpreting
44939** the 4 bytes as a big-endian value. On a big-endian architecture, it
44940** returns the value that would be produced by intepreting the 4 bytes
44941** of the input value as a little-endian integer.
44942*/
44943#define BYTESWAP32(x) ( \
44944    (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
44945  + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
44946)
44947
44948/*
44949** Generate or extend an 8 byte checksum based on the data in
44950** array aByte[] and the initial values of aIn[0] and aIn[1] (or
44951** initial values of 0 and 0 if aIn==NULL).
44952**
44953** The checksum is written back into aOut[] before returning.
44954**
44955** nByte must be a positive multiple of 8.
44956*/
44957static void walChecksumBytes(
44958  int nativeCksum, /* True for native byte-order, false for non-native */
44959  u8 *a,           /* Content to be checksummed */
44960  int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
44961  const u32 *aIn,  /* Initial checksum value input */
44962  u32 *aOut        /* OUT: Final checksum value output */
44963){
44964  u32 s1, s2;
44965  u32 *aData = (u32 *)a;
44966  u32 *aEnd = (u32 *)&a[nByte];
44967
44968  if( aIn ){
44969    s1 = aIn[0];
44970    s2 = aIn[1];
44971  }else{
44972    s1 = s2 = 0;
44973  }
44974
44975  assert( nByte>=8 );
44976  assert( (nByte&0x00000007)==0 );
44977
44978  if( nativeCksum ){
44979    do {
44980      s1 += *aData++ + s2;
44981      s2 += *aData++ + s1;
44982    }while( aData<aEnd );
44983  }else{
44984    do {
44985      s1 += BYTESWAP32(aData[0]) + s2;
44986      s2 += BYTESWAP32(aData[1]) + s1;
44987      aData += 2;
44988    }while( aData<aEnd );
44989  }
44990
44991  aOut[0] = s1;
44992  aOut[1] = s2;
44993}
44994
44995static void walShmBarrier(Wal *pWal){
44996  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
44997    sqlite3OsShmBarrier(pWal->pDbFd);
44998  }
44999}
45000
45001/*
45002** Write the header information in pWal->hdr into the wal-index.
45003**
45004** The checksum on pWal->hdr is updated before it is written.
45005*/
45006static void walIndexWriteHdr(Wal *pWal){
45007  volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
45008  const int nCksum = offsetof(WalIndexHdr, aCksum);
45009
45010  assert( pWal->writeLock );
45011  pWal->hdr.isInit = 1;
45012  pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
45013  walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
45014  memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
45015  walShmBarrier(pWal);
45016  memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
45017}
45018
45019/*
45020** This function encodes a single frame header and writes it to a buffer
45021** supplied by the caller. A frame-header is made up of a series of
45022** 4-byte big-endian integers, as follows:
45023**
45024**     0: Page number.
45025**     4: For commit records, the size of the database image in pages
45026**        after the commit. For all other records, zero.
45027**     8: Salt-1 (copied from the wal-header)
45028**    12: Salt-2 (copied from the wal-header)
45029**    16: Checksum-1.
45030**    20: Checksum-2.
45031*/
45032static void walEncodeFrame(
45033  Wal *pWal,                      /* The write-ahead log */
45034  u32 iPage,                      /* Database page number for frame */
45035  u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
45036  u8 *aData,                      /* Pointer to page data */
45037  u8 *aFrame                      /* OUT: Write encoded frame here */
45038){
45039  int nativeCksum;                /* True for native byte-order checksums */
45040  u32 *aCksum = pWal->hdr.aFrameCksum;
45041  assert( WAL_FRAME_HDRSIZE==24 );
45042  sqlite3Put4byte(&aFrame[0], iPage);
45043  sqlite3Put4byte(&aFrame[4], nTruncate);
45044  memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
45045
45046  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
45047  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
45048  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
45049
45050  sqlite3Put4byte(&aFrame[16], aCksum[0]);
45051  sqlite3Put4byte(&aFrame[20], aCksum[1]);
45052}
45053
45054/*
45055** Check to see if the frame with header in aFrame[] and content
45056** in aData[] is valid.  If it is a valid frame, fill *piPage and
45057** *pnTruncate and return true.  Return if the frame is not valid.
45058*/
45059static int walDecodeFrame(
45060  Wal *pWal,                      /* The write-ahead log */
45061  u32 *piPage,                    /* OUT: Database page number for frame */
45062  u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
45063  u8 *aData,                      /* Pointer to page data (for checksum) */
45064  u8 *aFrame                      /* Frame data */
45065){
45066  int nativeCksum;                /* True for native byte-order checksums */
45067  u32 *aCksum = pWal->hdr.aFrameCksum;
45068  u32 pgno;                       /* Page number of the frame */
45069  assert( WAL_FRAME_HDRSIZE==24 );
45070
45071  /* A frame is only valid if the salt values in the frame-header
45072  ** match the salt values in the wal-header.
45073  */
45074  if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
45075    return 0;
45076  }
45077
45078  /* A frame is only valid if the page number is creater than zero.
45079  */
45080  pgno = sqlite3Get4byte(&aFrame[0]);
45081  if( pgno==0 ){
45082    return 0;
45083  }
45084
45085  /* A frame is only valid if a checksum of the WAL header,
45086  ** all prior frams, the first 16 bytes of this frame-header,
45087  ** and the frame-data matches the checksum in the last 8
45088  ** bytes of this frame-header.
45089  */
45090  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
45091  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
45092  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
45093  if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
45094   || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
45095  ){
45096    /* Checksum failed. */
45097    return 0;
45098  }
45099
45100  /* If we reach this point, the frame is valid.  Return the page number
45101  ** and the new database size.
45102  */
45103  *piPage = pgno;
45104  *pnTruncate = sqlite3Get4byte(&aFrame[4]);
45105  return 1;
45106}
45107
45108
45109#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
45110/*
45111** Names of locks.  This routine is used to provide debugging output and is not
45112** a part of an ordinary build.
45113*/
45114static const char *walLockName(int lockIdx){
45115  if( lockIdx==WAL_WRITE_LOCK ){
45116    return "WRITE-LOCK";
45117  }else if( lockIdx==WAL_CKPT_LOCK ){
45118    return "CKPT-LOCK";
45119  }else if( lockIdx==WAL_RECOVER_LOCK ){
45120    return "RECOVER-LOCK";
45121  }else{
45122    static char zName[15];
45123    sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
45124                     lockIdx-WAL_READ_LOCK(0));
45125    return zName;
45126  }
45127}
45128#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
45129
45130
45131/*
45132** Set or release locks on the WAL.  Locks are either shared or exclusive.
45133** A lock cannot be moved directly between shared and exclusive - it must go
45134** through the unlocked state first.
45135**
45136** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
45137*/
45138static int walLockShared(Wal *pWal, int lockIdx){
45139  int rc;
45140  if( pWal->exclusiveMode ) return SQLITE_OK;
45141  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
45142                        SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
45143  WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
45144            walLockName(lockIdx), rc ? "failed" : "ok"));
45145  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
45146  return rc;
45147}
45148static void walUnlockShared(Wal *pWal, int lockIdx){
45149  if( pWal->exclusiveMode ) return;
45150  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
45151                         SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
45152  WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
45153}
45154static int walLockExclusive(Wal *pWal, int lockIdx, int n){
45155  int rc;
45156  if( pWal->exclusiveMode ) return SQLITE_OK;
45157  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
45158                        SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
45159  WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
45160            walLockName(lockIdx), n, rc ? "failed" : "ok"));
45161  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
45162  return rc;
45163}
45164static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
45165  if( pWal->exclusiveMode ) return;
45166  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
45167                         SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
45168  WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
45169             walLockName(lockIdx), n));
45170}
45171
45172/*
45173** Compute a hash on a page number.  The resulting hash value must land
45174** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
45175** the hash to the next value in the event of a collision.
45176*/
45177static int walHash(u32 iPage){
45178  assert( iPage>0 );
45179  assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
45180  return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
45181}
45182static int walNextHash(int iPriorHash){
45183  return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
45184}
45185
45186/*
45187** Return pointers to the hash table and page number array stored on
45188** page iHash of the wal-index. The wal-index is broken into 32KB pages
45189** numbered starting from 0.
45190**
45191** Set output variable *paHash to point to the start of the hash table
45192** in the wal-index file. Set *piZero to one less than the frame
45193** number of the first frame indexed by this hash table. If a
45194** slot in the hash table is set to N, it refers to frame number
45195** (*piZero+N) in the log.
45196**
45197** Finally, set *paPgno so that *paPgno[1] is the page number of the
45198** first frame indexed by the hash table, frame (*piZero+1).
45199*/
45200static int walHashGet(
45201  Wal *pWal,                      /* WAL handle */
45202  int iHash,                      /* Find the iHash'th table */
45203  volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
45204  volatile u32 **paPgno,          /* OUT: Pointer to page number array */
45205  u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
45206){
45207  int rc;                         /* Return code */
45208  volatile u32 *aPgno;
45209
45210  rc = walIndexPage(pWal, iHash, &aPgno);
45211  assert( rc==SQLITE_OK || iHash>0 );
45212
45213  if( rc==SQLITE_OK ){
45214    u32 iZero;
45215    volatile ht_slot *aHash;
45216
45217    aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
45218    if( iHash==0 ){
45219      aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
45220      iZero = 0;
45221    }else{
45222      iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
45223    }
45224
45225    *paPgno = &aPgno[-1];
45226    *paHash = aHash;
45227    *piZero = iZero;
45228  }
45229  return rc;
45230}
45231
45232/*
45233** Return the number of the wal-index page that contains the hash-table
45234** and page-number array that contain entries corresponding to WAL frame
45235** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
45236** are numbered starting from 0.
45237*/
45238static int walFramePage(u32 iFrame){
45239  int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
45240  assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
45241       && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
45242       && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
45243       && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
45244       && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
45245  );
45246  return iHash;
45247}
45248
45249/*
45250** Return the page number associated with frame iFrame in this WAL.
45251*/
45252static u32 walFramePgno(Wal *pWal, u32 iFrame){
45253  int iHash = walFramePage(iFrame);
45254  if( iHash==0 ){
45255    return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
45256  }
45257  return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
45258}
45259
45260/*
45261** Remove entries from the hash table that point to WAL slots greater
45262** than pWal->hdr.mxFrame.
45263**
45264** This function is called whenever pWal->hdr.mxFrame is decreased due
45265** to a rollback or savepoint.
45266**
45267** At most only the hash table containing pWal->hdr.mxFrame needs to be
45268** updated.  Any later hash tables will be automatically cleared when
45269** pWal->hdr.mxFrame advances to the point where those hash tables are
45270** actually needed.
45271*/
45272static void walCleanupHash(Wal *pWal){
45273  volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
45274  volatile u32 *aPgno = 0;        /* Page number array for hash table */
45275  u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
45276  int iLimit = 0;                 /* Zero values greater than this */
45277  int nByte;                      /* Number of bytes to zero in aPgno[] */
45278  int i;                          /* Used to iterate through aHash[] */
45279
45280  assert( pWal->writeLock );
45281  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
45282  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
45283  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
45284
45285  if( pWal->hdr.mxFrame==0 ) return;
45286
45287  /* Obtain pointers to the hash-table and page-number array containing
45288  ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
45289  ** that the page said hash-table and array reside on is already mapped.
45290  */
45291  assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
45292  assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
45293  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
45294
45295  /* Zero all hash-table entries that correspond to frame numbers greater
45296  ** than pWal->hdr.mxFrame.
45297  */
45298  iLimit = pWal->hdr.mxFrame - iZero;
45299  assert( iLimit>0 );
45300  for(i=0; i<HASHTABLE_NSLOT; i++){
45301    if( aHash[i]>iLimit ){
45302      aHash[i] = 0;
45303    }
45304  }
45305
45306  /* Zero the entries in the aPgno array that correspond to frames with
45307  ** frame numbers greater than pWal->hdr.mxFrame.
45308  */
45309  nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
45310  memset((void *)&aPgno[iLimit+1], 0, nByte);
45311
45312#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
45313  /* Verify that the every entry in the mapping region is still reachable
45314  ** via the hash table even after the cleanup.
45315  */
45316  if( iLimit ){
45317    int i;           /* Loop counter */
45318    int iKey;        /* Hash key */
45319    for(i=1; i<=iLimit; i++){
45320      for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
45321        if( aHash[iKey]==i ) break;
45322      }
45323      assert( aHash[iKey]==i );
45324    }
45325  }
45326#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
45327}
45328
45329
45330/*
45331** Set an entry in the wal-index that will map database page number
45332** pPage into WAL frame iFrame.
45333*/
45334static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
45335  int rc;                         /* Return code */
45336  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
45337  volatile u32 *aPgno = 0;        /* Page number array */
45338  volatile ht_slot *aHash = 0;    /* Hash table */
45339
45340  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
45341
45342  /* Assuming the wal-index file was successfully mapped, populate the
45343  ** page number array and hash table entry.
45344  */
45345  if( rc==SQLITE_OK ){
45346    int iKey;                     /* Hash table key */
45347    int idx;                      /* Value to write to hash-table slot */
45348    int nCollide;                 /* Number of hash collisions */
45349
45350    idx = iFrame - iZero;
45351    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
45352
45353    /* If this is the first entry to be added to this hash-table, zero the
45354    ** entire hash table and aPgno[] array before proceding.
45355    */
45356    if( idx==1 ){
45357      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
45358      memset((void*)&aPgno[1], 0, nByte);
45359    }
45360
45361    /* If the entry in aPgno[] is already set, then the previous writer
45362    ** must have exited unexpectedly in the middle of a transaction (after
45363    ** writing one or more dirty pages to the WAL to free up memory).
45364    ** Remove the remnants of that writers uncommitted transaction from
45365    ** the hash-table before writing any new entries.
45366    */
45367    if( aPgno[idx] ){
45368      walCleanupHash(pWal);
45369      assert( !aPgno[idx] );
45370    }
45371
45372    /* Write the aPgno[] array entry and the hash-table slot. */
45373    nCollide = idx;
45374    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
45375      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
45376    }
45377    aPgno[idx] = iPage;
45378    aHash[iKey] = (ht_slot)idx;
45379
45380#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
45381    /* Verify that the number of entries in the hash table exactly equals
45382    ** the number of entries in the mapping region.
45383    */
45384    {
45385      int i;           /* Loop counter */
45386      int nEntry = 0;  /* Number of entries in the hash table */
45387      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
45388      assert( nEntry==idx );
45389    }
45390
45391    /* Verify that the every entry in the mapping region is reachable
45392    ** via the hash table.  This turns out to be a really, really expensive
45393    ** thing to check, so only do this occasionally - not on every
45394    ** iteration.
45395    */
45396    if( (idx&0x3ff)==0 ){
45397      int i;           /* Loop counter */
45398      for(i=1; i<=idx; i++){
45399        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
45400          if( aHash[iKey]==i ) break;
45401        }
45402        assert( aHash[iKey]==i );
45403      }
45404    }
45405#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
45406  }
45407
45408
45409  return rc;
45410}
45411
45412
45413/*
45414** Recover the wal-index by reading the write-ahead log file.
45415**
45416** This routine first tries to establish an exclusive lock on the
45417** wal-index to prevent other threads/processes from doing anything
45418** with the WAL or wal-index while recovery is running.  The
45419** WAL_RECOVER_LOCK is also held so that other threads will know
45420** that this thread is running recovery.  If unable to establish
45421** the necessary locks, this routine returns SQLITE_BUSY.
45422*/
45423static int walIndexRecover(Wal *pWal){
45424  int rc;                         /* Return Code */
45425  i64 nSize;                      /* Size of log file */
45426  u32 aFrameCksum[2] = {0, 0};
45427  int iLock;                      /* Lock offset to lock for checkpoint */
45428  int nLock;                      /* Number of locks to hold */
45429
45430  /* Obtain an exclusive lock on all byte in the locking range not already
45431  ** locked by the caller. The caller is guaranteed to have locked the
45432  ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
45433  ** If successful, the same bytes that are locked here are unlocked before
45434  ** this function returns.
45435  */
45436  assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
45437  assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
45438  assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
45439  assert( pWal->writeLock );
45440  iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
45441  nLock = SQLITE_SHM_NLOCK - iLock;
45442  rc = walLockExclusive(pWal, iLock, nLock);
45443  if( rc ){
45444    return rc;
45445  }
45446  WALTRACE(("WAL%p: recovery begin...\n", pWal));
45447
45448  memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
45449
45450  rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
45451  if( rc!=SQLITE_OK ){
45452    goto recovery_error;
45453  }
45454
45455  if( nSize>WAL_HDRSIZE ){
45456    u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
45457    u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
45458    int szFrame;                  /* Number of bytes in buffer aFrame[] */
45459    u8 *aData;                    /* Pointer to data part of aFrame buffer */
45460    int iFrame;                   /* Index of last frame read */
45461    i64 iOffset;                  /* Next offset to read from log file */
45462    int szPage;                   /* Page size according to the log */
45463    u32 magic;                    /* Magic value read from WAL header */
45464    u32 version;                  /* Magic value read from WAL header */
45465
45466    /* Read in the WAL header. */
45467    rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
45468    if( rc!=SQLITE_OK ){
45469      goto recovery_error;
45470    }
45471
45472    /* If the database page size is not a power of two, or is greater than
45473    ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
45474    ** data. Similarly, if the 'magic' value is invalid, ignore the whole
45475    ** WAL file.
45476    */
45477    magic = sqlite3Get4byte(&aBuf[0]);
45478    szPage = sqlite3Get4byte(&aBuf[8]);
45479    if( (magic&0xFFFFFFFE)!=WAL_MAGIC
45480     || szPage&(szPage-1)
45481     || szPage>SQLITE_MAX_PAGE_SIZE
45482     || szPage<512
45483    ){
45484      goto finished;
45485    }
45486    pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
45487    pWal->szPage = szPage;
45488    pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
45489    memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
45490
45491    /* Verify that the WAL header checksum is correct */
45492    walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
45493        aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
45494    );
45495    if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
45496     || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
45497    ){
45498      goto finished;
45499    }
45500
45501    /* Verify that the version number on the WAL format is one that
45502    ** are able to understand */
45503    version = sqlite3Get4byte(&aBuf[4]);
45504    if( version!=WAL_MAX_VERSION ){
45505      rc = SQLITE_CANTOPEN_BKPT;
45506      goto finished;
45507    }
45508
45509    /* Malloc a buffer to read frames into. */
45510    szFrame = szPage + WAL_FRAME_HDRSIZE;
45511    aFrame = (u8 *)sqlite3_malloc(szFrame);
45512    if( !aFrame ){
45513      rc = SQLITE_NOMEM;
45514      goto recovery_error;
45515    }
45516    aData = &aFrame[WAL_FRAME_HDRSIZE];
45517
45518    /* Read all frames from the log file. */
45519    iFrame = 0;
45520    for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
45521      u32 pgno;                   /* Database page number for frame */
45522      u32 nTruncate;              /* dbsize field from frame header */
45523      int isValid;                /* True if this frame is valid */
45524
45525      /* Read and decode the next log frame. */
45526      rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
45527      if( rc!=SQLITE_OK ) break;
45528      isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
45529      if( !isValid ) break;
45530      rc = walIndexAppend(pWal, ++iFrame, pgno);
45531      if( rc!=SQLITE_OK ) break;
45532
45533      /* If nTruncate is non-zero, this is a commit record. */
45534      if( nTruncate ){
45535        pWal->hdr.mxFrame = iFrame;
45536        pWal->hdr.nPage = nTruncate;
45537        pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
45538        testcase( szPage<=32768 );
45539        testcase( szPage>=65536 );
45540        aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
45541        aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
45542      }
45543    }
45544
45545    sqlite3_free(aFrame);
45546  }
45547
45548finished:
45549  if( rc==SQLITE_OK ){
45550    volatile WalCkptInfo *pInfo;
45551    int i;
45552    pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
45553    pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
45554    walIndexWriteHdr(pWal);
45555
45556    /* Reset the checkpoint-header. This is safe because this thread is
45557    ** currently holding locks that exclude all other readers, writers and
45558    ** checkpointers.
45559    */
45560    pInfo = walCkptInfo(pWal);
45561    pInfo->nBackfill = 0;
45562    pInfo->aReadMark[0] = 0;
45563    for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
45564
45565    /* If more than one frame was recovered from the log file, report an
45566    ** event via sqlite3_log(). This is to help with identifying performance
45567    ** problems caused by applications routinely shutting down without
45568    ** checkpointing the log file.
45569    */
45570    if( pWal->hdr.nPage ){
45571      sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
45572          pWal->hdr.nPage, pWal->zWalName
45573      );
45574    }
45575  }
45576
45577recovery_error:
45578  WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
45579  walUnlockExclusive(pWal, iLock, nLock);
45580  return rc;
45581}
45582
45583/*
45584** Close an open wal-index.
45585*/
45586static void walIndexClose(Wal *pWal, int isDelete){
45587  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
45588    int i;
45589    for(i=0; i<pWal->nWiData; i++){
45590      sqlite3_free((void *)pWal->apWiData[i]);
45591      pWal->apWiData[i] = 0;
45592    }
45593  }else{
45594    sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
45595  }
45596}
45597
45598/*
45599** Open a connection to the WAL file zWalName. The database file must
45600** already be opened on connection pDbFd. The buffer that zWalName points
45601** to must remain valid for the lifetime of the returned Wal* handle.
45602**
45603** A SHARED lock should be held on the database file when this function
45604** is called. The purpose of this SHARED lock is to prevent any other
45605** client from unlinking the WAL or wal-index file. If another process
45606** were to do this just after this client opened one of these files, the
45607** system would be badly broken.
45608**
45609** If the log file is successfully opened, SQLITE_OK is returned and
45610** *ppWal is set to point to a new WAL handle. If an error occurs,
45611** an SQLite error code is returned and *ppWal is left unmodified.
45612*/
45613SQLITE_PRIVATE int sqlite3WalOpen(
45614  sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
45615  sqlite3_file *pDbFd,            /* The open database file */
45616  const char *zWalName,           /* Name of the WAL file */
45617  int bNoShm,                     /* True to run in heap-memory mode */
45618  i64 mxWalSize,                  /* Truncate WAL to this size on reset */
45619  Wal **ppWal                     /* OUT: Allocated Wal handle */
45620){
45621  int rc;                         /* Return Code */
45622  Wal *pRet;                      /* Object to allocate and return */
45623  int flags;                      /* Flags passed to OsOpen() */
45624
45625  assert( zWalName && zWalName[0] );
45626  assert( pDbFd );
45627
45628  /* In the amalgamation, the os_unix.c and os_win.c source files come before
45629  ** this source file.  Verify that the #defines of the locking byte offsets
45630  ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
45631  */
45632#ifdef WIN_SHM_BASE
45633  assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
45634#endif
45635#ifdef UNIX_SHM_BASE
45636  assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
45637#endif
45638
45639
45640  /* Allocate an instance of struct Wal to return. */
45641  *ppWal = 0;
45642  pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
45643  if( !pRet ){
45644    return SQLITE_NOMEM;
45645  }
45646
45647  pRet->pVfs = pVfs;
45648  pRet->pWalFd = (sqlite3_file *)&pRet[1];
45649  pRet->pDbFd = pDbFd;
45650  pRet->readLock = -1;
45651  pRet->mxWalSize = mxWalSize;
45652  pRet->zWalName = zWalName;
45653  pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
45654
45655  /* Open file handle on the write-ahead log file. */
45656  flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
45657  rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
45658  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
45659    pRet->readOnly = WAL_RDONLY;
45660  }
45661
45662  if( rc!=SQLITE_OK ){
45663    walIndexClose(pRet, 0);
45664    sqlite3OsClose(pRet->pWalFd);
45665    sqlite3_free(pRet);
45666  }else{
45667    *ppWal = pRet;
45668    WALTRACE(("WAL%d: opened\n", pRet));
45669  }
45670  return rc;
45671}
45672
45673/*
45674** Change the size to which the WAL file is trucated on each reset.
45675*/
45676SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
45677  if( pWal ) pWal->mxWalSize = iLimit;
45678}
45679
45680/*
45681** Find the smallest page number out of all pages held in the WAL that
45682** has not been returned by any prior invocation of this method on the
45683** same WalIterator object.   Write into *piFrame the frame index where
45684** that page was last written into the WAL.  Write into *piPage the page
45685** number.
45686**
45687** Return 0 on success.  If there are no pages in the WAL with a page
45688** number larger than *piPage, then return 1.
45689*/
45690static int walIteratorNext(
45691  WalIterator *p,               /* Iterator */
45692  u32 *piPage,                  /* OUT: The page number of the next page */
45693  u32 *piFrame                  /* OUT: Wal frame index of next page */
45694){
45695  u32 iMin;                     /* Result pgno must be greater than iMin */
45696  u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
45697  int i;                        /* For looping through segments */
45698
45699  iMin = p->iPrior;
45700  assert( iMin<0xffffffff );
45701  for(i=p->nSegment-1; i>=0; i--){
45702    struct WalSegment *pSegment = &p->aSegment[i];
45703    while( pSegment->iNext<pSegment->nEntry ){
45704      u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
45705      if( iPg>iMin ){
45706        if( iPg<iRet ){
45707          iRet = iPg;
45708          *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
45709        }
45710        break;
45711      }
45712      pSegment->iNext++;
45713    }
45714  }
45715
45716  *piPage = p->iPrior = iRet;
45717  return (iRet==0xFFFFFFFF);
45718}
45719
45720/*
45721** This function merges two sorted lists into a single sorted list.
45722**
45723** aLeft[] and aRight[] are arrays of indices.  The sort key is
45724** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
45725** is guaranteed for all J<K:
45726**
45727**        aContent[aLeft[J]] < aContent[aLeft[K]]
45728**        aContent[aRight[J]] < aContent[aRight[K]]
45729**
45730** This routine overwrites aRight[] with a new (probably longer) sequence
45731** of indices such that the aRight[] contains every index that appears in
45732** either aLeft[] or the old aRight[] and such that the second condition
45733** above is still met.
45734**
45735** The aContent[aLeft[X]] values will be unique for all X.  And the
45736** aContent[aRight[X]] values will be unique too.  But there might be
45737** one or more combinations of X and Y such that
45738**
45739**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
45740**
45741** When that happens, omit the aLeft[X] and use the aRight[Y] index.
45742*/
45743static void walMerge(
45744  const u32 *aContent,            /* Pages in wal - keys for the sort */
45745  ht_slot *aLeft,                 /* IN: Left hand input list */
45746  int nLeft,                      /* IN: Elements in array *paLeft */
45747  ht_slot **paRight,              /* IN/OUT: Right hand input list */
45748  int *pnRight,                   /* IN/OUT: Elements in *paRight */
45749  ht_slot *aTmp                   /* Temporary buffer */
45750){
45751  int iLeft = 0;                  /* Current index in aLeft */
45752  int iRight = 0;                 /* Current index in aRight */
45753  int iOut = 0;                   /* Current index in output buffer */
45754  int nRight = *pnRight;
45755  ht_slot *aRight = *paRight;
45756
45757  assert( nLeft>0 && nRight>0 );
45758  while( iRight<nRight || iLeft<nLeft ){
45759    ht_slot logpage;
45760    Pgno dbpage;
45761
45762    if( (iLeft<nLeft)
45763     && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
45764    ){
45765      logpage = aLeft[iLeft++];
45766    }else{
45767      logpage = aRight[iRight++];
45768    }
45769    dbpage = aContent[logpage];
45770
45771    aTmp[iOut++] = logpage;
45772    if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
45773
45774    assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
45775    assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
45776  }
45777
45778  *paRight = aLeft;
45779  *pnRight = iOut;
45780  memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
45781}
45782
45783/*
45784** Sort the elements in list aList using aContent[] as the sort key.
45785** Remove elements with duplicate keys, preferring to keep the
45786** larger aList[] values.
45787**
45788** The aList[] entries are indices into aContent[].  The values in
45789** aList[] are to be sorted so that for all J<K:
45790**
45791**      aContent[aList[J]] < aContent[aList[K]]
45792**
45793** For any X and Y such that
45794**
45795**      aContent[aList[X]] == aContent[aList[Y]]
45796**
45797** Keep the larger of the two values aList[X] and aList[Y] and discard
45798** the smaller.
45799*/
45800static void walMergesort(
45801  const u32 *aContent,            /* Pages in wal */
45802  ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
45803  ht_slot *aList,                 /* IN/OUT: List to sort */
45804  int *pnList                     /* IN/OUT: Number of elements in aList[] */
45805){
45806  struct Sublist {
45807    int nList;                    /* Number of elements in aList */
45808    ht_slot *aList;               /* Pointer to sub-list content */
45809  };
45810
45811  const int nList = *pnList;      /* Size of input list */
45812  int nMerge = 0;                 /* Number of elements in list aMerge */
45813  ht_slot *aMerge = 0;            /* List to be merged */
45814  int iList;                      /* Index into input list */
45815  int iSub = 0;                   /* Index into aSub array */
45816  struct Sublist aSub[13];        /* Array of sub-lists */
45817
45818  memset(aSub, 0, sizeof(aSub));
45819  assert( nList<=HASHTABLE_NPAGE && nList>0 );
45820  assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
45821
45822  for(iList=0; iList<nList; iList++){
45823    nMerge = 1;
45824    aMerge = &aList[iList];
45825    for(iSub=0; iList & (1<<iSub); iSub++){
45826      struct Sublist *p = &aSub[iSub];
45827      assert( p->aList && p->nList<=(1<<iSub) );
45828      assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
45829      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
45830    }
45831    aSub[iSub].aList = aMerge;
45832    aSub[iSub].nList = nMerge;
45833  }
45834
45835  for(iSub++; iSub<ArraySize(aSub); iSub++){
45836    if( nList & (1<<iSub) ){
45837      struct Sublist *p = &aSub[iSub];
45838      assert( p->nList<=(1<<iSub) );
45839      assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
45840      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
45841    }
45842  }
45843  assert( aMerge==aList );
45844  *pnList = nMerge;
45845
45846#ifdef SQLITE_DEBUG
45847  {
45848    int i;
45849    for(i=1; i<*pnList; i++){
45850      assert( aContent[aList[i]] > aContent[aList[i-1]] );
45851    }
45852  }
45853#endif
45854}
45855
45856/*
45857** Free an iterator allocated by walIteratorInit().
45858*/
45859static void walIteratorFree(WalIterator *p){
45860  sqlite3ScratchFree(p);
45861}
45862
45863/*
45864** Construct a WalInterator object that can be used to loop over all
45865** pages in the WAL in ascending order. The caller must hold the checkpoint
45866** lock.
45867**
45868** On success, make *pp point to the newly allocated WalInterator object
45869** return SQLITE_OK. Otherwise, return an error code. If this routine
45870** returns an error, the value of *pp is undefined.
45871**
45872** The calling routine should invoke walIteratorFree() to destroy the
45873** WalIterator object when it has finished with it.
45874*/
45875static int walIteratorInit(Wal *pWal, WalIterator **pp){
45876  WalIterator *p;                 /* Return value */
45877  int nSegment;                   /* Number of segments to merge */
45878  u32 iLast;                      /* Last frame in log */
45879  int nByte;                      /* Number of bytes to allocate */
45880  int i;                          /* Iterator variable */
45881  ht_slot *aTmp;                  /* Temp space used by merge-sort */
45882  int rc = SQLITE_OK;             /* Return Code */
45883
45884  /* This routine only runs while holding the checkpoint lock. And
45885  ** it only runs if there is actually content in the log (mxFrame>0).
45886  */
45887  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
45888  iLast = pWal->hdr.mxFrame;
45889
45890  /* Allocate space for the WalIterator object. */
45891  nSegment = walFramePage(iLast) + 1;
45892  nByte = sizeof(WalIterator)
45893        + (nSegment-1)*sizeof(struct WalSegment)
45894        + iLast*sizeof(ht_slot);
45895  p = (WalIterator *)sqlite3ScratchMalloc(nByte);
45896  if( !p ){
45897    return SQLITE_NOMEM;
45898  }
45899  memset(p, 0, nByte);
45900  p->nSegment = nSegment;
45901
45902  /* Allocate temporary space used by the merge-sort routine. This block
45903  ** of memory will be freed before this function returns.
45904  */
45905  aTmp = (ht_slot *)sqlite3ScratchMalloc(
45906      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
45907  );
45908  if( !aTmp ){
45909    rc = SQLITE_NOMEM;
45910  }
45911
45912  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
45913    volatile ht_slot *aHash;
45914    u32 iZero;
45915    volatile u32 *aPgno;
45916
45917    rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
45918    if( rc==SQLITE_OK ){
45919      int j;                      /* Counter variable */
45920      int nEntry;                 /* Number of entries in this segment */
45921      ht_slot *aIndex;            /* Sorted index for this segment */
45922
45923      aPgno++;
45924      if( (i+1)==nSegment ){
45925        nEntry = (int)(iLast - iZero);
45926      }else{
45927        nEntry = (int)((u32*)aHash - (u32*)aPgno);
45928      }
45929      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
45930      iZero++;
45931
45932      for(j=0; j<nEntry; j++){
45933        aIndex[j] = (ht_slot)j;
45934      }
45935      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
45936      p->aSegment[i].iZero = iZero;
45937      p->aSegment[i].nEntry = nEntry;
45938      p->aSegment[i].aIndex = aIndex;
45939      p->aSegment[i].aPgno = (u32 *)aPgno;
45940    }
45941  }
45942  sqlite3ScratchFree(aTmp);
45943
45944  if( rc!=SQLITE_OK ){
45945    walIteratorFree(p);
45946  }
45947  *pp = p;
45948  return rc;
45949}
45950
45951/*
45952** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
45953** n. If the attempt fails and parameter xBusy is not NULL, then it is a
45954** busy-handler function. Invoke it and retry the lock until either the
45955** lock is successfully obtained or the busy-handler returns 0.
45956*/
45957static int walBusyLock(
45958  Wal *pWal,                      /* WAL connection */
45959  int (*xBusy)(void*),            /* Function to call when busy */
45960  void *pBusyArg,                 /* Context argument for xBusyHandler */
45961  int lockIdx,                    /* Offset of first byte to lock */
45962  int n                           /* Number of bytes to lock */
45963){
45964  int rc;
45965  do {
45966    rc = walLockExclusive(pWal, lockIdx, n);
45967  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
45968  return rc;
45969}
45970
45971/*
45972** The cache of the wal-index header must be valid to call this function.
45973** Return the page-size in bytes used by the database.
45974*/
45975static int walPagesize(Wal *pWal){
45976  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
45977}
45978
45979/*
45980** Copy as much content as we can from the WAL back into the database file
45981** in response to an sqlite3_wal_checkpoint() request or the equivalent.
45982**
45983** The amount of information copies from WAL to database might be limited
45984** by active readers.  This routine will never overwrite a database page
45985** that a concurrent reader might be using.
45986**
45987** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
45988** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
45989** checkpoints are always run by a background thread or background
45990** process, foreground threads will never block on a lengthy fsync call.
45991**
45992** Fsync is called on the WAL before writing content out of the WAL and
45993** into the database.  This ensures that if the new content is persistent
45994** in the WAL and can be recovered following a power-loss or hard reset.
45995**
45996** Fsync is also called on the database file if (and only if) the entire
45997** WAL content is copied into the database file.  This second fsync makes
45998** it safe to delete the WAL since the new content will persist in the
45999** database file.
46000**
46001** This routine uses and updates the nBackfill field of the wal-index header.
46002** This is the only routine tha will increase the value of nBackfill.
46003** (A WAL reset or recovery will revert nBackfill to zero, but not increase
46004** its value.)
46005**
46006** The caller must be holding sufficient locks to ensure that no other
46007** checkpoint is running (in any other thread or process) at the same
46008** time.
46009*/
46010static int walCheckpoint(
46011  Wal *pWal,                      /* Wal connection */
46012  int eMode,                      /* One of PASSIVE, FULL or RESTART */
46013  int (*xBusyCall)(void*),        /* Function to call when busy */
46014  void *pBusyArg,                 /* Context argument for xBusyHandler */
46015  int sync_flags,                 /* Flags for OsSync() (or 0) */
46016  u8 *zBuf                        /* Temporary buffer to use */
46017){
46018  int rc;                         /* Return code */
46019  int szPage;                     /* Database page-size */
46020  WalIterator *pIter = 0;         /* Wal iterator context */
46021  u32 iDbpage = 0;                /* Next database page to write */
46022  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
46023  u32 mxSafeFrame;                /* Max frame that can be backfilled */
46024  u32 mxPage;                     /* Max database page to write */
46025  int i;                          /* Loop counter */
46026  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
46027  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
46028
46029  szPage = walPagesize(pWal);
46030  testcase( szPage<=32768 );
46031  testcase( szPage>=65536 );
46032  pInfo = walCkptInfo(pWal);
46033  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
46034
46035  /* Allocate the iterator */
46036  rc = walIteratorInit(pWal, &pIter);
46037  if( rc!=SQLITE_OK ){
46038    return rc;
46039  }
46040  assert( pIter );
46041
46042  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
46043
46044  /* Compute in mxSafeFrame the index of the last frame of the WAL that is
46045  ** safe to write into the database.  Frames beyond mxSafeFrame might
46046  ** overwrite database pages that are in use by active readers and thus
46047  ** cannot be backfilled from the WAL.
46048  */
46049  mxSafeFrame = pWal->hdr.mxFrame;
46050  mxPage = pWal->hdr.nPage;
46051  for(i=1; i<WAL_NREADER; i++){
46052    u32 y = pInfo->aReadMark[i];
46053    if( mxSafeFrame>y ){
46054      assert( y<=pWal->hdr.mxFrame );
46055      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
46056      if( rc==SQLITE_OK ){
46057        pInfo->aReadMark[i] = READMARK_NOT_USED;
46058        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
46059      }else if( rc==SQLITE_BUSY ){
46060        mxSafeFrame = y;
46061        xBusy = 0;
46062      }else{
46063        goto walcheckpoint_out;
46064      }
46065    }
46066  }
46067
46068  if( pInfo->nBackfill<mxSafeFrame
46069   && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
46070  ){
46071    i64 nSize;                    /* Current size of database file */
46072    u32 nBackfill = pInfo->nBackfill;
46073
46074    /* Sync the WAL to disk */
46075    if( sync_flags ){
46076      rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
46077    }
46078
46079    /* If the database file may grow as a result of this checkpoint, hint
46080    ** about the eventual size of the db file to the VFS layer.
46081    */
46082    if( rc==SQLITE_OK ){
46083      i64 nReq = ((i64)mxPage * szPage);
46084      rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
46085      if( rc==SQLITE_OK && nSize<nReq ){
46086        sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
46087      }
46088    }
46089
46090    /* Iterate through the contents of the WAL, copying data to the db file. */
46091    while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
46092      i64 iOffset;
46093      assert( walFramePgno(pWal, iFrame)==iDbpage );
46094      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
46095      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
46096      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
46097      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
46098      if( rc!=SQLITE_OK ) break;
46099      iOffset = (iDbpage-1)*(i64)szPage;
46100      testcase( IS_BIG_INT(iOffset) );
46101      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
46102      if( rc!=SQLITE_OK ) break;
46103    }
46104
46105    /* If work was actually accomplished... */
46106    if( rc==SQLITE_OK ){
46107      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
46108        i64 szDb = pWal->hdr.nPage*(i64)szPage;
46109        testcase( IS_BIG_INT(szDb) );
46110        rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
46111        if( rc==SQLITE_OK && sync_flags ){
46112          rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
46113        }
46114      }
46115      if( rc==SQLITE_OK ){
46116        pInfo->nBackfill = mxSafeFrame;
46117      }
46118    }
46119
46120    /* Release the reader lock held while backfilling */
46121    walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
46122  }
46123
46124  if( rc==SQLITE_BUSY ){
46125    /* Reset the return code so as not to report a checkpoint failure
46126    ** just because there are active readers.  */
46127    rc = SQLITE_OK;
46128  }
46129
46130  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
46131  ** file has been copied into the database file, then block until all
46132  ** readers have finished using the wal file. This ensures that the next
46133  ** process to write to the database restarts the wal file.
46134  */
46135  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
46136    assert( pWal->writeLock );
46137    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
46138      rc = SQLITE_BUSY;
46139    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
46140      assert( mxSafeFrame==pWal->hdr.mxFrame );
46141      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
46142      if( rc==SQLITE_OK ){
46143        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
46144      }
46145    }
46146  }
46147
46148 walcheckpoint_out:
46149  walIteratorFree(pIter);
46150  return rc;
46151}
46152
46153/*
46154** Close a connection to a log file.
46155*/
46156SQLITE_PRIVATE int sqlite3WalClose(
46157  Wal *pWal,                      /* Wal to close */
46158  int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
46159  int nBuf,
46160  u8 *zBuf                        /* Buffer of at least nBuf bytes */
46161){
46162  int rc = SQLITE_OK;
46163  if( pWal ){
46164    int isDelete = 0;             /* True to unlink wal and wal-index files */
46165
46166    /* If an EXCLUSIVE lock can be obtained on the database file (using the
46167    ** ordinary, rollback-mode locking methods, this guarantees that the
46168    ** connection associated with this log file is the only connection to
46169    ** the database. In this case checkpoint the database and unlink both
46170    ** the wal and wal-index files.
46171    **
46172    ** The EXCLUSIVE lock is not released before returning.
46173    */
46174    rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
46175    if( rc==SQLITE_OK ){
46176      int bPersistWal = -1;
46177      if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
46178        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
46179      }
46180      rc = sqlite3WalCheckpoint(
46181          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
46182      );
46183      sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersistWal);
46184      if( rc==SQLITE_OK && bPersistWal!=1 ){
46185        isDelete = 1;
46186      }
46187    }
46188
46189    walIndexClose(pWal, isDelete);
46190    sqlite3OsClose(pWal->pWalFd);
46191    if( isDelete ){
46192      sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
46193    }
46194    WALTRACE(("WAL%p: closed\n", pWal));
46195    sqlite3_free((void *)pWal->apWiData);
46196    sqlite3_free(pWal);
46197  }
46198  return rc;
46199}
46200
46201/*
46202** Try to read the wal-index header.  Return 0 on success and 1 if
46203** there is a problem.
46204**
46205** The wal-index is in shared memory.  Another thread or process might
46206** be writing the header at the same time this procedure is trying to
46207** read it, which might result in inconsistency.  A dirty read is detected
46208** by verifying that both copies of the header are the same and also by
46209** a checksum on the header.
46210**
46211** If and only if the read is consistent and the header is different from
46212** pWal->hdr, then pWal->hdr is updated to the content of the new header
46213** and *pChanged is set to 1.
46214**
46215** If the checksum cannot be verified return non-zero. If the header
46216** is read successfully and the checksum verified, return zero.
46217*/
46218static int walIndexTryHdr(Wal *pWal, int *pChanged){
46219  u32 aCksum[2];                  /* Checksum on the header content */
46220  WalIndexHdr h1, h2;             /* Two copies of the header content */
46221  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
46222
46223  /* The first page of the wal-index must be mapped at this point. */
46224  assert( pWal->nWiData>0 && pWal->apWiData[0] );
46225
46226  /* Read the header. This might happen concurrently with a write to the
46227  ** same area of shared memory on a different CPU in a SMP,
46228  ** meaning it is possible that an inconsistent snapshot is read
46229  ** from the file. If this happens, return non-zero.
46230  **
46231  ** There are two copies of the header at the beginning of the wal-index.
46232  ** When reading, read [0] first then [1].  Writes are in the reverse order.
46233  ** Memory barriers are used to prevent the compiler or the hardware from
46234  ** reordering the reads and writes.
46235  */
46236  aHdr = walIndexHdr(pWal);
46237  memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
46238  walShmBarrier(pWal);
46239  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
46240
46241  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
46242    return 1;   /* Dirty read */
46243  }
46244  if( h1.isInit==0 ){
46245    return 1;   /* Malformed header - probably all zeros */
46246  }
46247  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
46248  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
46249    return 1;   /* Checksum does not match */
46250  }
46251
46252  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
46253    *pChanged = 1;
46254    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
46255    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
46256    testcase( pWal->szPage<=32768 );
46257    testcase( pWal->szPage>=65536 );
46258  }
46259
46260  /* The header was successfully read. Return zero. */
46261  return 0;
46262}
46263
46264/*
46265** Read the wal-index header from the wal-index and into pWal->hdr.
46266** If the wal-header appears to be corrupt, try to reconstruct the
46267** wal-index from the WAL before returning.
46268**
46269** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
46270** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
46271** to 0.
46272**
46273** If the wal-index header is successfully read, return SQLITE_OK.
46274** Otherwise an SQLite error code.
46275*/
46276static int walIndexReadHdr(Wal *pWal, int *pChanged){
46277  int rc;                         /* Return code */
46278  int badHdr;                     /* True if a header read failed */
46279  volatile u32 *page0;            /* Chunk of wal-index containing header */
46280
46281  /* Ensure that page 0 of the wal-index (the page that contains the
46282  ** wal-index header) is mapped. Return early if an error occurs here.
46283  */
46284  assert( pChanged );
46285  rc = walIndexPage(pWal, 0, &page0);
46286  if( rc!=SQLITE_OK ){
46287    return rc;
46288  };
46289  assert( page0 || pWal->writeLock==0 );
46290
46291  /* If the first page of the wal-index has been mapped, try to read the
46292  ** wal-index header immediately, without holding any lock. This usually
46293  ** works, but may fail if the wal-index header is corrupt or currently
46294  ** being modified by another thread or process.
46295  */
46296  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
46297
46298  /* If the first attempt failed, it might have been due to a race
46299  ** with a writer.  So get a WRITE lock and try again.
46300  */
46301  assert( badHdr==0 || pWal->writeLock==0 );
46302  if( badHdr ){
46303    if( pWal->readOnly & WAL_SHM_RDONLY ){
46304      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
46305        walUnlockShared(pWal, WAL_WRITE_LOCK);
46306        rc = SQLITE_READONLY_RECOVERY;
46307      }
46308    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
46309      pWal->writeLock = 1;
46310      if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
46311        badHdr = walIndexTryHdr(pWal, pChanged);
46312        if( badHdr ){
46313          /* If the wal-index header is still malformed even while holding
46314          ** a WRITE lock, it can only mean that the header is corrupted and
46315          ** needs to be reconstructed.  So run recovery to do exactly that.
46316          */
46317          rc = walIndexRecover(pWal);
46318          *pChanged = 1;
46319        }
46320      }
46321      pWal->writeLock = 0;
46322      walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46323    }
46324  }
46325
46326  /* If the header is read successfully, check the version number to make
46327  ** sure the wal-index was not constructed with some future format that
46328  ** this version of SQLite cannot understand.
46329  */
46330  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
46331    rc = SQLITE_CANTOPEN_BKPT;
46332  }
46333
46334  return rc;
46335}
46336
46337/*
46338** This is the value that walTryBeginRead returns when it needs to
46339** be retried.
46340*/
46341#define WAL_RETRY  (-1)
46342
46343/*
46344** Attempt to start a read transaction.  This might fail due to a race or
46345** other transient condition.  When that happens, it returns WAL_RETRY to
46346** indicate to the caller that it is safe to retry immediately.
46347**
46348** On success return SQLITE_OK.  On a permanent failure (such an
46349** I/O error or an SQLITE_BUSY because another process is running
46350** recovery) return a positive error code.
46351**
46352** The useWal parameter is true to force the use of the WAL and disable
46353** the case where the WAL is bypassed because it has been completely
46354** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
46355** to make a copy of the wal-index header into pWal->hdr.  If the
46356** wal-index header has changed, *pChanged is set to 1 (as an indication
46357** to the caller that the local paget cache is obsolete and needs to be
46358** flushed.)  When useWal==1, the wal-index header is assumed to already
46359** be loaded and the pChanged parameter is unused.
46360**
46361** The caller must set the cnt parameter to the number of prior calls to
46362** this routine during the current read attempt that returned WAL_RETRY.
46363** This routine will start taking more aggressive measures to clear the
46364** race conditions after multiple WAL_RETRY returns, and after an excessive
46365** number of errors will ultimately return SQLITE_PROTOCOL.  The
46366** SQLITE_PROTOCOL return indicates that some other process has gone rogue
46367** and is not honoring the locking protocol.  There is a vanishingly small
46368** chance that SQLITE_PROTOCOL could be returned because of a run of really
46369** bad luck when there is lots of contention for the wal-index, but that
46370** possibility is so small that it can be safely neglected, we believe.
46371**
46372** On success, this routine obtains a read lock on
46373** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
46374** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
46375** that means the Wal does not hold any read lock.  The reader must not
46376** access any database page that is modified by a WAL frame up to and
46377** including frame number aReadMark[pWal->readLock].  The reader will
46378** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
46379** Or if pWal->readLock==0, then the reader will ignore the WAL
46380** completely and get all content directly from the database file.
46381** If the useWal parameter is 1 then the WAL will never be ignored and
46382** this routine will always set pWal->readLock>0 on success.
46383** When the read transaction is completed, the caller must release the
46384** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
46385**
46386** This routine uses the nBackfill and aReadMark[] fields of the header
46387** to select a particular WAL_READ_LOCK() that strives to let the
46388** checkpoint process do as much work as possible.  This routine might
46389** update values of the aReadMark[] array in the header, but if it does
46390** so it takes care to hold an exclusive lock on the corresponding
46391** WAL_READ_LOCK() while changing values.
46392*/
46393static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
46394  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
46395  u32 mxReadMark;                 /* Largest aReadMark[] value */
46396  int mxI;                        /* Index of largest aReadMark[] value */
46397  int i;                          /* Loop counter */
46398  int rc = SQLITE_OK;             /* Return code  */
46399
46400  assert( pWal->readLock<0 );     /* Not currently locked */
46401
46402  /* Take steps to avoid spinning forever if there is a protocol error.
46403  **
46404  ** Circumstances that cause a RETRY should only last for the briefest
46405  ** instances of time.  No I/O or other system calls are done while the
46406  ** locks are held, so the locks should not be held for very long. But
46407  ** if we are unlucky, another process that is holding a lock might get
46408  ** paged out or take a page-fault that is time-consuming to resolve,
46409  ** during the few nanoseconds that it is holding the lock.  In that case,
46410  ** it might take longer than normal for the lock to free.
46411  **
46412  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
46413  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
46414  ** is more of a scheduler yield than an actual delay.  But on the 10th
46415  ** an subsequent retries, the delays start becoming longer and longer,
46416  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
46417  ** The total delay time before giving up is less than 1 second.
46418  */
46419  if( cnt>5 ){
46420    int nDelay = 1;                      /* Pause time in microseconds */
46421    if( cnt>100 ){
46422      VVA_ONLY( pWal->lockError = 1; )
46423      return SQLITE_PROTOCOL;
46424    }
46425    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
46426    sqlite3OsSleep(pWal->pVfs, nDelay);
46427  }
46428
46429  if( !useWal ){
46430    rc = walIndexReadHdr(pWal, pChanged);
46431    if( rc==SQLITE_BUSY ){
46432      /* If there is not a recovery running in another thread or process
46433      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
46434      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
46435      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
46436      ** would be technically correct.  But the race is benign since with
46437      ** WAL_RETRY this routine will be called again and will probably be
46438      ** right on the second iteration.
46439      */
46440      if( pWal->apWiData[0]==0 ){
46441        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
46442        ** We assume this is a transient condition, so return WAL_RETRY. The
46443        ** xShmMap() implementation used by the default unix and win32 VFS
46444        ** modules may return SQLITE_BUSY due to a race condition in the
46445        ** code that determines whether or not the shared-memory region
46446        ** must be zeroed before the requested page is returned.
46447        */
46448        rc = WAL_RETRY;
46449      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
46450        walUnlockShared(pWal, WAL_RECOVER_LOCK);
46451        rc = WAL_RETRY;
46452      }else if( rc==SQLITE_BUSY ){
46453        rc = SQLITE_BUSY_RECOVERY;
46454      }
46455    }
46456    if( rc!=SQLITE_OK ){
46457      return rc;
46458    }
46459  }
46460
46461  pInfo = walCkptInfo(pWal);
46462  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
46463    /* The WAL has been completely backfilled (or it is empty).
46464    ** and can be safely ignored.
46465    */
46466    rc = walLockShared(pWal, WAL_READ_LOCK(0));
46467    walShmBarrier(pWal);
46468    if( rc==SQLITE_OK ){
46469      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
46470        /* It is not safe to allow the reader to continue here if frames
46471        ** may have been appended to the log before READ_LOCK(0) was obtained.
46472        ** When holding READ_LOCK(0), the reader ignores the entire log file,
46473        ** which implies that the database file contains a trustworthy
46474        ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
46475        ** happening, this is usually correct.
46476        **
46477        ** However, if frames have been appended to the log (or if the log
46478        ** is wrapped and written for that matter) before the READ_LOCK(0)
46479        ** is obtained, that is not necessarily true. A checkpointer may
46480        ** have started to backfill the appended frames but crashed before
46481        ** it finished. Leaving a corrupt image in the database file.
46482        */
46483        walUnlockShared(pWal, WAL_READ_LOCK(0));
46484        return WAL_RETRY;
46485      }
46486      pWal->readLock = 0;
46487      return SQLITE_OK;
46488    }else if( rc!=SQLITE_BUSY ){
46489      return rc;
46490    }
46491  }
46492
46493  /* If we get this far, it means that the reader will want to use
46494  ** the WAL to get at content from recent commits.  The job now is
46495  ** to select one of the aReadMark[] entries that is closest to
46496  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
46497  */
46498  mxReadMark = 0;
46499  mxI = 0;
46500  for(i=1; i<WAL_NREADER; i++){
46501    u32 thisMark = pInfo->aReadMark[i];
46502    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
46503      assert( thisMark!=READMARK_NOT_USED );
46504      mxReadMark = thisMark;
46505      mxI = i;
46506    }
46507  }
46508  /* There was once an "if" here. The extra "{" is to preserve indentation. */
46509  {
46510    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
46511     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
46512    ){
46513      for(i=1; i<WAL_NREADER; i++){
46514        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
46515        if( rc==SQLITE_OK ){
46516          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
46517          mxI = i;
46518          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
46519          break;
46520        }else if( rc!=SQLITE_BUSY ){
46521          return rc;
46522        }
46523      }
46524    }
46525    if( mxI==0 ){
46526      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
46527      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
46528    }
46529
46530    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
46531    if( rc ){
46532      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
46533    }
46534    /* Now that the read-lock has been obtained, check that neither the
46535    ** value in the aReadMark[] array or the contents of the wal-index
46536    ** header have changed.
46537    **
46538    ** It is necessary to check that the wal-index header did not change
46539    ** between the time it was read and when the shared-lock was obtained
46540    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
46541    ** that the log file may have been wrapped by a writer, or that frames
46542    ** that occur later in the log than pWal->hdr.mxFrame may have been
46543    ** copied into the database by a checkpointer. If either of these things
46544    ** happened, then reading the database with the current value of
46545    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
46546    ** instead.
46547    **
46548    ** This does not guarantee that the copy of the wal-index header is up to
46549    ** date before proceeding. That would not be possible without somehow
46550    ** blocking writers. It only guarantees that a dangerous checkpoint or
46551    ** log-wrap (either of which would require an exclusive lock on
46552    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
46553    */
46554    walShmBarrier(pWal);
46555    if( pInfo->aReadMark[mxI]!=mxReadMark
46556     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
46557    ){
46558      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
46559      return WAL_RETRY;
46560    }else{
46561      assert( mxReadMark<=pWal->hdr.mxFrame );
46562      pWal->readLock = (i16)mxI;
46563    }
46564  }
46565  return rc;
46566}
46567
46568/*
46569** Begin a read transaction on the database.
46570**
46571** This routine used to be called sqlite3OpenSnapshot() and with good reason:
46572** it takes a snapshot of the state of the WAL and wal-index for the current
46573** instant in time.  The current thread will continue to use this snapshot.
46574** Other threads might append new content to the WAL and wal-index but
46575** that extra content is ignored by the current thread.
46576**
46577** If the database contents have changes since the previous read
46578** transaction, then *pChanged is set to 1 before returning.  The
46579** Pager layer will use this to know that is cache is stale and
46580** needs to be flushed.
46581*/
46582SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
46583  int rc;                         /* Return code */
46584  int cnt = 0;                    /* Number of TryBeginRead attempts */
46585
46586  do{
46587    rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
46588  }while( rc==WAL_RETRY );
46589  testcase( (rc&0xff)==SQLITE_BUSY );
46590  testcase( (rc&0xff)==SQLITE_IOERR );
46591  testcase( rc==SQLITE_PROTOCOL );
46592  testcase( rc==SQLITE_OK );
46593  return rc;
46594}
46595
46596/*
46597** Finish with a read transaction.  All this does is release the
46598** read-lock.
46599*/
46600SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
46601  sqlite3WalEndWriteTransaction(pWal);
46602  if( pWal->readLock>=0 ){
46603    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
46604    pWal->readLock = -1;
46605  }
46606}
46607
46608/*
46609** Read a page from the WAL, if it is present in the WAL and if the
46610** current read transaction is configured to use the WAL.
46611**
46612** The *pInWal is set to 1 if the requested page is in the WAL and
46613** has been loaded.  Or *pInWal is set to 0 if the page was not in
46614** the WAL and needs to be read out of the database.
46615*/
46616SQLITE_PRIVATE int sqlite3WalRead(
46617  Wal *pWal,                      /* WAL handle */
46618  Pgno pgno,                      /* Database page number to read data for */
46619  int *pInWal,                    /* OUT: True if data is read from WAL */
46620  int nOut,                       /* Size of buffer pOut in bytes */
46621  u8 *pOut                        /* Buffer to write page data to */
46622){
46623  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
46624  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
46625  int iHash;                      /* Used to loop through N hash tables */
46626
46627  /* This routine is only be called from within a read transaction. */
46628  assert( pWal->readLock>=0 || pWal->lockError );
46629
46630  /* If the "last page" field of the wal-index header snapshot is 0, then
46631  ** no data will be read from the wal under any circumstances. Return early
46632  ** in this case as an optimization.  Likewise, if pWal->readLock==0,
46633  ** then the WAL is ignored by the reader so return early, as if the
46634  ** WAL were empty.
46635  */
46636  if( iLast==0 || pWal->readLock==0 ){
46637    *pInWal = 0;
46638    return SQLITE_OK;
46639  }
46640
46641  /* Search the hash table or tables for an entry matching page number
46642  ** pgno. Each iteration of the following for() loop searches one
46643  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
46644  **
46645  ** This code might run concurrently to the code in walIndexAppend()
46646  ** that adds entries to the wal-index (and possibly to this hash
46647  ** table). This means the value just read from the hash
46648  ** slot (aHash[iKey]) may have been added before or after the
46649  ** current read transaction was opened. Values added after the
46650  ** read transaction was opened may have been written incorrectly -
46651  ** i.e. these slots may contain garbage data. However, we assume
46652  ** that any slots written before the current read transaction was
46653  ** opened remain unmodified.
46654  **
46655  ** For the reasons above, the if(...) condition featured in the inner
46656  ** loop of the following block is more stringent that would be required
46657  ** if we had exclusive access to the hash-table:
46658  **
46659  **   (aPgno[iFrame]==pgno):
46660  **     This condition filters out normal hash-table collisions.
46661  **
46662  **   (iFrame<=iLast):
46663  **     This condition filters out entries that were added to the hash
46664  **     table after the current read-transaction had started.
46665  */
46666  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
46667    volatile ht_slot *aHash;      /* Pointer to hash table */
46668    volatile u32 *aPgno;          /* Pointer to array of page numbers */
46669    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
46670    int iKey;                     /* Hash slot index */
46671    int nCollide;                 /* Number of hash collisions remaining */
46672    int rc;                       /* Error code */
46673
46674    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
46675    if( rc!=SQLITE_OK ){
46676      return rc;
46677    }
46678    nCollide = HASHTABLE_NSLOT;
46679    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
46680      u32 iFrame = aHash[iKey] + iZero;
46681      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
46682        assert( iFrame>iRead );
46683        iRead = iFrame;
46684      }
46685      if( (nCollide--)==0 ){
46686        return SQLITE_CORRUPT_BKPT;
46687      }
46688    }
46689  }
46690
46691#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
46692  /* If expensive assert() statements are available, do a linear search
46693  ** of the wal-index file content. Make sure the results agree with the
46694  ** result obtained using the hash indexes above.  */
46695  {
46696    u32 iRead2 = 0;
46697    u32 iTest;
46698    for(iTest=iLast; iTest>0; iTest--){
46699      if( walFramePgno(pWal, iTest)==pgno ){
46700        iRead2 = iTest;
46701        break;
46702      }
46703    }
46704    assert( iRead==iRead2 );
46705  }
46706#endif
46707
46708  /* If iRead is non-zero, then it is the log frame number that contains the
46709  ** required page. Read and return data from the log file.
46710  */
46711  if( iRead ){
46712    int sz;
46713    i64 iOffset;
46714    sz = pWal->hdr.szPage;
46715    sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
46716    testcase( sz<=32768 );
46717    testcase( sz>=65536 );
46718    iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
46719    *pInWal = 1;
46720    /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46721    return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
46722  }
46723
46724  *pInWal = 0;
46725  return SQLITE_OK;
46726}
46727
46728
46729/*
46730** Return the size of the database in pages (or zero, if unknown).
46731*/
46732SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
46733  if( pWal && ALWAYS(pWal->readLock>=0) ){
46734    return pWal->hdr.nPage;
46735  }
46736  return 0;
46737}
46738
46739
46740/*
46741** This function starts a write transaction on the WAL.
46742**
46743** A read transaction must have already been started by a prior call
46744** to sqlite3WalBeginReadTransaction().
46745**
46746** If another thread or process has written into the database since
46747** the read transaction was started, then it is not possible for this
46748** thread to write as doing so would cause a fork.  So this routine
46749** returns SQLITE_BUSY in that case and no write transaction is started.
46750**
46751** There can only be a single writer active at a time.
46752*/
46753SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
46754  int rc;
46755
46756  /* Cannot start a write transaction without first holding a read
46757  ** transaction. */
46758  assert( pWal->readLock>=0 );
46759
46760  if( pWal->readOnly ){
46761    return SQLITE_READONLY;
46762  }
46763
46764  /* Only one writer allowed at a time.  Get the write lock.  Return
46765  ** SQLITE_BUSY if unable.
46766  */
46767  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
46768  if( rc ){
46769    return rc;
46770  }
46771  pWal->writeLock = 1;
46772
46773  /* If another connection has written to the database file since the
46774  ** time the read transaction on this connection was started, then
46775  ** the write is disallowed.
46776  */
46777  if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
46778    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46779    pWal->writeLock = 0;
46780    rc = SQLITE_BUSY;
46781  }
46782
46783  return rc;
46784}
46785
46786/*
46787** End a write transaction.  The commit has already been done.  This
46788** routine merely releases the lock.
46789*/
46790SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
46791  if( pWal->writeLock ){
46792    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46793    pWal->writeLock = 0;
46794  }
46795  return SQLITE_OK;
46796}
46797
46798/*
46799** If any data has been written (but not committed) to the log file, this
46800** function moves the write-pointer back to the start of the transaction.
46801**
46802** Additionally, the callback function is invoked for each frame written
46803** to the WAL since the start of the transaction. If the callback returns
46804** other than SQLITE_OK, it is not invoked again and the error code is
46805** returned to the caller.
46806**
46807** Otherwise, if the callback function does not return an error, this
46808** function returns SQLITE_OK.
46809*/
46810SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
46811  int rc = SQLITE_OK;
46812  if( ALWAYS(pWal->writeLock) ){
46813    Pgno iMax = pWal->hdr.mxFrame;
46814    Pgno iFrame;
46815
46816    /* Restore the clients cache of the wal-index header to the state it
46817    ** was in before the client began writing to the database.
46818    */
46819    memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
46820
46821    for(iFrame=pWal->hdr.mxFrame+1;
46822        ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
46823        iFrame++
46824    ){
46825      /* This call cannot fail. Unless the page for which the page number
46826      ** is passed as the second argument is (a) in the cache and
46827      ** (b) has an outstanding reference, then xUndo is either a no-op
46828      ** (if (a) is false) or simply expels the page from the cache (if (b)
46829      ** is false).
46830      **
46831      ** If the upper layer is doing a rollback, it is guaranteed that there
46832      ** are no outstanding references to any page other than page 1. And
46833      ** page 1 is never written to the log until the transaction is
46834      ** committed. As a result, the call to xUndo may not fail.
46835      */
46836      assert( walFramePgno(pWal, iFrame)!=1 );
46837      rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
46838    }
46839    walCleanupHash(pWal);
46840  }
46841  assert( rc==SQLITE_OK );
46842  return rc;
46843}
46844
46845/*
46846** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
46847** values. This function populates the array with values required to
46848** "rollback" the write position of the WAL handle back to the current
46849** point in the event of a savepoint rollback (via WalSavepointUndo()).
46850*/
46851SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
46852  assert( pWal->writeLock );
46853  aWalData[0] = pWal->hdr.mxFrame;
46854  aWalData[1] = pWal->hdr.aFrameCksum[0];
46855  aWalData[2] = pWal->hdr.aFrameCksum[1];
46856  aWalData[3] = pWal->nCkpt;
46857}
46858
46859/*
46860** Move the write position of the WAL back to the point identified by
46861** the values in the aWalData[] array. aWalData must point to an array
46862** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
46863** by a call to WalSavepoint().
46864*/
46865SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
46866  int rc = SQLITE_OK;
46867
46868  assert( pWal->writeLock );
46869  assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
46870
46871  if( aWalData[3]!=pWal->nCkpt ){
46872    /* This savepoint was opened immediately after the write-transaction
46873    ** was started. Right after that, the writer decided to wrap around
46874    ** to the start of the log. Update the savepoint values to match.
46875    */
46876    aWalData[0] = 0;
46877    aWalData[3] = pWal->nCkpt;
46878  }
46879
46880  if( aWalData[0]<pWal->hdr.mxFrame ){
46881    pWal->hdr.mxFrame = aWalData[0];
46882    pWal->hdr.aFrameCksum[0] = aWalData[1];
46883    pWal->hdr.aFrameCksum[1] = aWalData[2];
46884    walCleanupHash(pWal);
46885  }
46886
46887  return rc;
46888}
46889
46890/*
46891** This function is called just before writing a set of frames to the log
46892** file (see sqlite3WalFrames()). It checks to see if, instead of appending
46893** to the current log file, it is possible to overwrite the start of the
46894** existing log file with the new frames (i.e. "reset" the log). If so,
46895** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
46896** unchanged.
46897**
46898** SQLITE_OK is returned if no error is encountered (regardless of whether
46899** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
46900** if an error occurs.
46901*/
46902static int walRestartLog(Wal *pWal){
46903  int rc = SQLITE_OK;
46904  int cnt;
46905
46906  if( pWal->readLock==0 ){
46907    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
46908    assert( pInfo->nBackfill==pWal->hdr.mxFrame );
46909    if( pInfo->nBackfill>0 ){
46910      u32 salt1;
46911      sqlite3_randomness(4, &salt1);
46912      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
46913      if( rc==SQLITE_OK ){
46914        /* If all readers are using WAL_READ_LOCK(0) (in other words if no
46915        ** readers are currently using the WAL), then the transactions
46916        ** frames will overwrite the start of the existing log. Update the
46917        ** wal-index header to reflect this.
46918        **
46919        ** In theory it would be Ok to update the cache of the header only
46920        ** at this point. But updating the actual wal-index header is also
46921        ** safe and means there is no special case for sqlite3WalUndo()
46922        ** to handle if this transaction is rolled back.
46923        */
46924        int i;                    /* Loop counter */
46925        u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
46926
46927        /* Limit the size of WAL file if the journal_size_limit PRAGMA is
46928        ** set to a non-negative value.  Log errors encountered
46929        ** during the truncation attempt. */
46930        if( pWal->mxWalSize>=0 ){
46931          i64 sz;
46932          int rx;
46933          sqlite3BeginBenignMalloc();
46934          rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46935          if( rx==SQLITE_OK && (sz > pWal->mxWalSize) ){
46936            rx = sqlite3OsTruncate(pWal->pWalFd, pWal->mxWalSize);
46937          }
46938          sqlite3EndBenignMalloc();
46939          if( rx ){
46940            sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46941          }
46942        }
46943
46944        pWal->nCkpt++;
46945        pWal->hdr.mxFrame = 0;
46946        sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
46947        aSalt[1] = salt1;
46948        walIndexWriteHdr(pWal);
46949        pInfo->nBackfill = 0;
46950        for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
46951        assert( pInfo->aReadMark[0]==0 );
46952        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
46953      }else if( rc!=SQLITE_BUSY ){
46954        return rc;
46955      }
46956    }
46957    walUnlockShared(pWal, WAL_READ_LOCK(0));
46958    pWal->readLock = -1;
46959    cnt = 0;
46960    do{
46961      int notUsed;
46962      rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
46963    }while( rc==WAL_RETRY );
46964    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
46965    testcase( (rc&0xff)==SQLITE_IOERR );
46966    testcase( rc==SQLITE_PROTOCOL );
46967    testcase( rc==SQLITE_OK );
46968  }
46969  return rc;
46970}
46971
46972/*
46973** Write a set of frames to the log. The caller must hold the write-lock
46974** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
46975*/
46976SQLITE_PRIVATE int sqlite3WalFrames(
46977  Wal *pWal,                      /* Wal handle to write to */
46978  int szPage,                     /* Database page-size in bytes */
46979  PgHdr *pList,                   /* List of dirty pages to write */
46980  Pgno nTruncate,                 /* Database size after this commit */
46981  int isCommit,                   /* True if this is a commit */
46982  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
46983){
46984  int rc;                         /* Used to catch return codes */
46985  u32 iFrame;                     /* Next frame address */
46986  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
46987  PgHdr *p;                       /* Iterator to run through pList with. */
46988  PgHdr *pLast = 0;               /* Last frame in list */
46989  int nLast = 0;                  /* Number of extra copies of last page */
46990
46991  assert( pList );
46992  assert( pWal->writeLock );
46993
46994#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
46995  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
46996    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
46997              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
46998  }
46999#endif
47000
47001  /* See if it is possible to write these frames into the start of the
47002  ** log file, instead of appending to it at pWal->hdr.mxFrame.
47003  */
47004  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
47005    return rc;
47006  }
47007
47008  /* If this is the first frame written into the log, write the WAL
47009  ** header to the start of the WAL file. See comments at the top of
47010  ** this source file for a description of the WAL header format.
47011  */
47012  iFrame = pWal->hdr.mxFrame;
47013  if( iFrame==0 ){
47014    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
47015    u32 aCksum[2];                /* Checksum for wal-header */
47016
47017    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
47018    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
47019    sqlite3Put4byte(&aWalHdr[8], szPage);
47020    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
47021    sqlite3_randomness(8, pWal->hdr.aSalt);
47022    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
47023    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
47024    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
47025    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
47026
47027    pWal->szPage = szPage;
47028    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
47029    pWal->hdr.aFrameCksum[0] = aCksum[0];
47030    pWal->hdr.aFrameCksum[1] = aCksum[1];
47031
47032    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
47033    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
47034    if( rc!=SQLITE_OK ){
47035      return rc;
47036    }
47037  }
47038  assert( (int)pWal->szPage==szPage );
47039
47040  /* Write the log file. */
47041  for(p=pList; p; p=p->pDirty){
47042    u32 nDbsize;                  /* Db-size field for frame header */
47043    i64 iOffset;                  /* Write offset in log file */
47044    void *pData;
47045
47046    iOffset = walFrameOffset(++iFrame, szPage);
47047    /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
47048
47049    /* Populate and write the frame header */
47050    nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
47051#if defined(SQLITE_HAS_CODEC)
47052    if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
47053#else
47054    pData = p->pData;
47055#endif
47056    walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
47057    rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
47058    if( rc!=SQLITE_OK ){
47059      return rc;
47060    }
47061
47062    /* Write the page data */
47063    rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
47064    if( rc!=SQLITE_OK ){
47065      return rc;
47066    }
47067    pLast = p;
47068  }
47069
47070  /* Sync the log file if the 'isSync' flag was specified. */
47071  if( sync_flags ){
47072    i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
47073    i64 iOffset = walFrameOffset(iFrame+1, szPage);
47074
47075    assert( isCommit );
47076    assert( iSegment>0 );
47077
47078    iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
47079    while( iOffset<iSegment ){
47080      void *pData;
47081#if defined(SQLITE_HAS_CODEC)
47082      if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
47083#else
47084      pData = pLast->pData;
47085#endif
47086      walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
47087      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
47088      rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
47089      if( rc!=SQLITE_OK ){
47090        return rc;
47091      }
47092      iOffset += WAL_FRAME_HDRSIZE;
47093      rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
47094      if( rc!=SQLITE_OK ){
47095        return rc;
47096      }
47097      nLast++;
47098      iOffset += szPage;
47099    }
47100
47101    rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
47102  }
47103
47104  /* Append data to the wal-index. It is not necessary to lock the
47105  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
47106  ** guarantees that there are no other writers, and no data that may
47107  ** be in use by existing readers is being overwritten.
47108  */
47109  iFrame = pWal->hdr.mxFrame;
47110  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
47111    iFrame++;
47112    rc = walIndexAppend(pWal, iFrame, p->pgno);
47113  }
47114  while( nLast>0 && rc==SQLITE_OK ){
47115    iFrame++;
47116    nLast--;
47117    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
47118  }
47119
47120  if( rc==SQLITE_OK ){
47121    /* Update the private copy of the header. */
47122    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
47123    testcase( szPage<=32768 );
47124    testcase( szPage>=65536 );
47125    pWal->hdr.mxFrame = iFrame;
47126    if( isCommit ){
47127      pWal->hdr.iChange++;
47128      pWal->hdr.nPage = nTruncate;
47129    }
47130    /* If this is a commit, update the wal-index header too. */
47131    if( isCommit ){
47132      walIndexWriteHdr(pWal);
47133      pWal->iCallback = iFrame;
47134    }
47135  }
47136
47137  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
47138  return rc;
47139}
47140
47141/*
47142** This routine is called to implement sqlite3_wal_checkpoint() and
47143** related interfaces.
47144**
47145** Obtain a CHECKPOINT lock and then backfill as much information as
47146** we can from WAL into the database.
47147**
47148** If parameter xBusy is not NULL, it is a pointer to a busy-handler
47149** callback. In this case this function runs a blocking checkpoint.
47150*/
47151SQLITE_PRIVATE int sqlite3WalCheckpoint(
47152  Wal *pWal,                      /* Wal connection */
47153  int eMode,                      /* PASSIVE, FULL or RESTART */
47154  int (*xBusy)(void*),            /* Function to call when busy */
47155  void *pBusyArg,                 /* Context argument for xBusyHandler */
47156  int sync_flags,                 /* Flags to sync db file with (or 0) */
47157  int nBuf,                       /* Size of temporary buffer */
47158  u8 *zBuf,                       /* Temporary buffer to use */
47159  int *pnLog,                     /* OUT: Number of frames in WAL */
47160  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
47161){
47162  int rc;                         /* Return code */
47163  int isChanged = 0;              /* True if a new wal-index header is loaded */
47164  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
47165
47166  assert( pWal->ckptLock==0 );
47167  assert( pWal->writeLock==0 );
47168
47169  if( pWal->readOnly ) return SQLITE_READONLY;
47170  WALTRACE(("WAL%p: checkpoint begins\n", pWal));
47171  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
47172  if( rc ){
47173    /* Usually this is SQLITE_BUSY meaning that another thread or process
47174    ** is already running a checkpoint, or maybe a recovery.  But it might
47175    ** also be SQLITE_IOERR. */
47176    return rc;
47177  }
47178  pWal->ckptLock = 1;
47179
47180  /* If this is a blocking-checkpoint, then obtain the write-lock as well
47181  ** to prevent any writers from running while the checkpoint is underway.
47182  ** This has to be done before the call to walIndexReadHdr() below.
47183  **
47184  ** If the writer lock cannot be obtained, then a passive checkpoint is
47185  ** run instead. Since the checkpointer is not holding the writer lock,
47186  ** there is no point in blocking waiting for any readers. Assuming no
47187  ** other error occurs, this function will return SQLITE_BUSY to the caller.
47188  */
47189  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
47190    rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
47191    if( rc==SQLITE_OK ){
47192      pWal->writeLock = 1;
47193    }else if( rc==SQLITE_BUSY ){
47194      eMode2 = SQLITE_CHECKPOINT_PASSIVE;
47195      rc = SQLITE_OK;
47196    }
47197  }
47198
47199  /* Read the wal-index header. */
47200  if( rc==SQLITE_OK ){
47201    rc = walIndexReadHdr(pWal, &isChanged);
47202  }
47203
47204  /* Copy data from the log to the database file. */
47205  if( rc==SQLITE_OK ){
47206    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
47207      rc = SQLITE_CORRUPT_BKPT;
47208    }else{
47209      rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
47210    }
47211
47212    /* If no error occurred, set the output variables. */
47213    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
47214      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
47215      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
47216    }
47217  }
47218
47219  if( isChanged ){
47220    /* If a new wal-index header was loaded before the checkpoint was
47221    ** performed, then the pager-cache associated with pWal is now
47222    ** out of date. So zero the cached wal-index header to ensure that
47223    ** next time the pager opens a snapshot on this database it knows that
47224    ** the cache needs to be reset.
47225    */
47226    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
47227  }
47228
47229  /* Release the locks. */
47230  sqlite3WalEndWriteTransaction(pWal);
47231  walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
47232  pWal->ckptLock = 0;
47233  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
47234  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
47235}
47236
47237/* Return the value to pass to a sqlite3_wal_hook callback, the
47238** number of frames in the WAL at the point of the last commit since
47239** sqlite3WalCallback() was called.  If no commits have occurred since
47240** the last call, then return 0.
47241*/
47242SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
47243  u32 ret = 0;
47244  if( pWal ){
47245    ret = pWal->iCallback;
47246    pWal->iCallback = 0;
47247  }
47248  return (int)ret;
47249}
47250
47251/*
47252** This function is called to change the WAL subsystem into or out
47253** of locking_mode=EXCLUSIVE.
47254**
47255** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
47256** into locking_mode=NORMAL.  This means that we must acquire a lock
47257** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
47258** or if the acquisition of the lock fails, then return 0.  If the
47259** transition out of exclusive-mode is successful, return 1.  This
47260** operation must occur while the pager is still holding the exclusive
47261** lock on the main database file.
47262**
47263** If op is one, then change from locking_mode=NORMAL into
47264** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
47265** be released.  Return 1 if the transition is made and 0 if the
47266** WAL is already in exclusive-locking mode - meaning that this
47267** routine is a no-op.  The pager must already hold the exclusive lock
47268** on the main database file before invoking this operation.
47269**
47270** If op is negative, then do a dry-run of the op==1 case but do
47271** not actually change anything. The pager uses this to see if it
47272** should acquire the database exclusive lock prior to invoking
47273** the op==1 case.
47274*/
47275SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
47276  int rc;
47277  assert( pWal->writeLock==0 );
47278  assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
47279
47280  /* pWal->readLock is usually set, but might be -1 if there was a
47281  ** prior error while attempting to acquire are read-lock. This cannot
47282  ** happen if the connection is actually in exclusive mode (as no xShmLock
47283  ** locks are taken in this case). Nor should the pager attempt to
47284  ** upgrade to exclusive-mode following such an error.
47285  */
47286  assert( pWal->readLock>=0 || pWal->lockError );
47287  assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
47288
47289  if( op==0 ){
47290    if( pWal->exclusiveMode ){
47291      pWal->exclusiveMode = 0;
47292      if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
47293        pWal->exclusiveMode = 1;
47294      }
47295      rc = pWal->exclusiveMode==0;
47296    }else{
47297      /* Already in locking_mode=NORMAL */
47298      rc = 0;
47299    }
47300  }else if( op>0 ){
47301    assert( pWal->exclusiveMode==0 );
47302    assert( pWal->readLock>=0 );
47303    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
47304    pWal->exclusiveMode = 1;
47305    rc = 1;
47306  }else{
47307    rc = pWal->exclusiveMode==0;
47308  }
47309  return rc;
47310}
47311
47312/*
47313** Return true if the argument is non-NULL and the WAL module is using
47314** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
47315** WAL module is using shared-memory, return false.
47316*/
47317SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
47318  return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
47319}
47320
47321#endif /* #ifndef SQLITE_OMIT_WAL */
47322
47323/************** End of wal.c *************************************************/
47324/************** Begin file btmutex.c *****************************************/
47325/*
47326** 2007 August 27
47327**
47328** The author disclaims copyright to this source code.  In place of
47329** a legal notice, here is a blessing:
47330**
47331**    May you do good and not evil.
47332**    May you find forgiveness for yourself and forgive others.
47333**    May you share freely, never taking more than you give.
47334**
47335*************************************************************************
47336**
47337** This file contains code used to implement mutexes on Btree objects.
47338** This code really belongs in btree.c.  But btree.c is getting too
47339** big and we want to break it down some.  This packaged seemed like
47340** a good breakout.
47341*/
47342/************** Include btreeInt.h in the middle of btmutex.c ****************/
47343/************** Begin file btreeInt.h ****************************************/
47344/*
47345** 2004 April 6
47346**
47347** The author disclaims copyright to this source code.  In place of
47348** a legal notice, here is a blessing:
47349**
47350**    May you do good and not evil.
47351**    May you find forgiveness for yourself and forgive others.
47352**    May you share freely, never taking more than you give.
47353**
47354*************************************************************************
47355** This file implements a external (disk-based) database using BTrees.
47356** For a detailed discussion of BTrees, refer to
47357**
47358**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
47359**     "Sorting And Searching", pages 473-480. Addison-Wesley
47360**     Publishing Company, Reading, Massachusetts.
47361**
47362** The basic idea is that each page of the file contains N database
47363** entries and N+1 pointers to subpages.
47364**
47365**   ----------------------------------------------------------------
47366**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
47367**   ----------------------------------------------------------------
47368**
47369** All of the keys on the page that Ptr(0) points to have values less
47370** than Key(0).  All of the keys on page Ptr(1) and its subpages have
47371** values greater than Key(0) and less than Key(1).  All of the keys
47372** on Ptr(N) and its subpages have values greater than Key(N-1).  And
47373** so forth.
47374**
47375** Finding a particular key requires reading O(log(M)) pages from the
47376** disk where M is the number of entries in the tree.
47377**
47378** In this implementation, a single file can hold one or more separate
47379** BTrees.  Each BTree is identified by the index of its root page.  The
47380** key and data for any entry are combined to form the "payload".  A
47381** fixed amount of payload can be carried directly on the database
47382** page.  If the payload is larger than the preset amount then surplus
47383** bytes are stored on overflow pages.  The payload for an entry
47384** and the preceding pointer are combined to form a "Cell".  Each
47385** page has a small header which contains the Ptr(N) pointer and other
47386** information such as the size of key and data.
47387**
47388** FORMAT DETAILS
47389**
47390** The file is divided into pages.  The first page is called page 1,
47391** the second is page 2, and so forth.  A page number of zero indicates
47392** "no such page".  The page size can be any power of 2 between 512 and 65536.
47393** Each page can be either a btree page, a freelist page, an overflow
47394** page, or a pointer-map page.
47395**
47396** The first page is always a btree page.  The first 100 bytes of the first
47397** page contain a special header (the "file header") that describes the file.
47398** The format of the file header is as follows:
47399**
47400**   OFFSET   SIZE    DESCRIPTION
47401**      0      16     Header string: "SQLite format 3\000"
47402**     16       2     Page size in bytes.
47403**     18       1     File format write version
47404**     19       1     File format read version
47405**     20       1     Bytes of unused space at the end of each page
47406**     21       1     Max embedded payload fraction
47407**     22       1     Min embedded payload fraction
47408**     23       1     Min leaf payload fraction
47409**     24       4     File change counter
47410**     28       4     Reserved for future use
47411**     32       4     First freelist page
47412**     36       4     Number of freelist pages in the file
47413**     40      60     15 4-byte meta values passed to higher layers
47414**
47415**     40       4     Schema cookie
47416**     44       4     File format of schema layer
47417**     48       4     Size of page cache
47418**     52       4     Largest root-page (auto/incr_vacuum)
47419**     56       4     1=UTF-8 2=UTF16le 3=UTF16be
47420**     60       4     User version
47421**     64       4     Incremental vacuum mode
47422**     68       4     unused
47423**     72       4     unused
47424**     76       4     unused
47425**
47426** All of the integer values are big-endian (most significant byte first).
47427**
47428** The file change counter is incremented when the database is changed
47429** This counter allows other processes to know when the file has changed
47430** and thus when they need to flush their cache.
47431**
47432** The max embedded payload fraction is the amount of the total usable
47433** space in a page that can be consumed by a single cell for standard
47434** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
47435** is to limit the maximum cell size so that at least 4 cells will fit
47436** on one page.  Thus the default max embedded payload fraction is 64.
47437**
47438** If the payload for a cell is larger than the max payload, then extra
47439** payload is spilled to overflow pages.  Once an overflow page is allocated,
47440** as many bytes as possible are moved into the overflow pages without letting
47441** the cell size drop below the min embedded payload fraction.
47442**
47443** The min leaf payload fraction is like the min embedded payload fraction
47444** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
47445** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
47446** not specified in the header.
47447**
47448** Each btree pages is divided into three sections:  The header, the
47449** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
47450** file header that occurs before the page header.
47451**
47452**      |----------------|
47453**      | file header    |   100 bytes.  Page 1 only.
47454**      |----------------|
47455**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
47456**      |----------------|
47457**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
47458**      | array          |   |  Grows downward
47459**      |                |   v
47460**      |----------------|
47461**      | unallocated    |
47462**      | space          |
47463**      |----------------|   ^  Grows upwards
47464**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
47465**      | area           |   |  and free space fragments.
47466**      |----------------|
47467**
47468** The page headers looks like this:
47469**
47470**   OFFSET   SIZE     DESCRIPTION
47471**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
47472**      1       2      byte offset to the first freeblock
47473**      3       2      number of cells on this page
47474**      5       2      first byte of the cell content area
47475**      7       1      number of fragmented free bytes
47476**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
47477**
47478** The flags define the format of this btree page.  The leaf flag means that
47479** this page has no children.  The zerodata flag means that this page carries
47480** only keys and no data.  The intkey flag means that the key is a integer
47481** which is stored in the key size entry of the cell header rather than in
47482** the payload area.
47483**
47484** The cell pointer array begins on the first byte after the page header.
47485** The cell pointer array contains zero or more 2-byte numbers which are
47486** offsets from the beginning of the page to the cell content in the cell
47487** content area.  The cell pointers occur in sorted order.  The system strives
47488** to keep free space after the last cell pointer so that new cells can
47489** be easily added without having to defragment the page.
47490**
47491** Cell content is stored at the very end of the page and grows toward the
47492** beginning of the page.
47493**
47494** Unused space within the cell content area is collected into a linked list of
47495** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
47496** to the first freeblock is given in the header.  Freeblocks occur in
47497** increasing order.  Because a freeblock must be at least 4 bytes in size,
47498** any group of 3 or fewer unused bytes in the cell content area cannot
47499** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
47500** a fragment.  The total number of bytes in all fragments is recorded.
47501** in the page header at offset 7.
47502**
47503**    SIZE    DESCRIPTION
47504**      2     Byte offset of the next freeblock
47505**      2     Bytes in this freeblock
47506**
47507** Cells are of variable length.  Cells are stored in the cell content area at
47508** the end of the page.  Pointers to the cells are in the cell pointer array
47509** that immediately follows the page header.  Cells is not necessarily
47510** contiguous or in order, but cell pointers are contiguous and in order.
47511**
47512** Cell content makes use of variable length integers.  A variable
47513** length integer is 1 to 9 bytes where the lower 7 bits of each
47514** byte are used.  The integer consists of all bytes that have bit 8 set and
47515** the first byte with bit 8 clear.  The most significant byte of the integer
47516** appears first.  A variable-length integer may not be more than 9 bytes long.
47517** As a special case, all 8 bytes of the 9th byte are used as data.  This
47518** allows a 64-bit integer to be encoded in 9 bytes.
47519**
47520**    0x00                      becomes  0x00000000
47521**    0x7f                      becomes  0x0000007f
47522**    0x81 0x00                 becomes  0x00000080
47523**    0x82 0x00                 becomes  0x00000100
47524**    0x80 0x7f                 becomes  0x0000007f
47525**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
47526**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
47527**
47528** Variable length integers are used for rowids and to hold the number of
47529** bytes of key and data in a btree cell.
47530**
47531** The content of a cell looks like this:
47532**
47533**    SIZE    DESCRIPTION
47534**      4     Page number of the left child. Omitted if leaf flag is set.
47535**     var    Number of bytes of data. Omitted if the zerodata flag is set.
47536**     var    Number of bytes of key. Or the key itself if intkey flag is set.
47537**      *     Payload
47538**      4     First page of the overflow chain.  Omitted if no overflow
47539**
47540** Overflow pages form a linked list.  Each page except the last is completely
47541** filled with data (pagesize - 4 bytes).  The last page can have as little
47542** as 1 byte of data.
47543**
47544**    SIZE    DESCRIPTION
47545**      4     Page number of next overflow page
47546**      *     Data
47547**
47548** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
47549** file header points to the first in a linked list of trunk page.  Each trunk
47550** page points to multiple leaf pages.  The content of a leaf page is
47551** unspecified.  A trunk page looks like this:
47552**
47553**    SIZE    DESCRIPTION
47554**      4     Page number of next trunk page
47555**      4     Number of leaf pointers on this page
47556**      *     zero or more pages numbers of leaves
47557*/
47558
47559
47560/* The following value is the maximum cell size assuming a maximum page
47561** size give above.
47562*/
47563#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
47564
47565/* The maximum number of cells on a single page of the database.  This
47566** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
47567** plus 2 bytes for the index to the cell in the page header).  Such
47568** small cells will be rare, but they are possible.
47569*/
47570#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
47571
47572/* Forward declarations */
47573typedef struct MemPage MemPage;
47574typedef struct BtLock BtLock;
47575
47576/*
47577** This is a magic string that appears at the beginning of every
47578** SQLite database in order to identify the file as a real database.
47579**
47580** You can change this value at compile-time by specifying a
47581** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
47582** header must be exactly 16 bytes including the zero-terminator so
47583** the string itself should be 15 characters long.  If you change
47584** the header, then your custom library will not be able to read
47585** databases generated by the standard tools and the standard tools
47586** will not be able to read databases created by your custom library.
47587*/
47588#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
47589#  define SQLITE_FILE_HEADER "SQLite format 3"
47590#endif
47591
47592/*
47593** Page type flags.  An ORed combination of these flags appear as the
47594** first byte of on-disk image of every BTree page.
47595*/
47596#define PTF_INTKEY    0x01
47597#define PTF_ZERODATA  0x02
47598#define PTF_LEAFDATA  0x04
47599#define PTF_LEAF      0x08
47600
47601/*
47602** As each page of the file is loaded into memory, an instance of the following
47603** structure is appended and initialized to zero.  This structure stores
47604** information about the page that is decoded from the raw file page.
47605**
47606** The pParent field points back to the parent page.  This allows us to
47607** walk up the BTree from any leaf to the root.  Care must be taken to
47608** unref() the parent page pointer when this page is no longer referenced.
47609** The pageDestructor() routine handles that chore.
47610**
47611** Access to all fields of this structure is controlled by the mutex
47612** stored in MemPage.pBt->mutex.
47613*/
47614struct MemPage {
47615  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
47616  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
47617  u8 intKey;           /* True if intkey flag is set */
47618  u8 leaf;             /* True if leaf flag is set */
47619  u8 hasData;          /* True if this page stores data */
47620  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
47621  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
47622  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
47623  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
47624  u16 cellOffset;      /* Index in aData of first cell pointer */
47625  u16 nFree;           /* Number of free bytes on the page */
47626  u16 nCell;           /* Number of cells on this page, local and ovfl */
47627  u16 maskPage;        /* Mask for page offset */
47628  struct _OvflCell {   /* Cells that will not fit on aData[] */
47629    u8 *pCell;          /* Pointers to the body of the overflow cell */
47630    u16 idx;            /* Insert this cell before idx-th non-overflow cell */
47631  } aOvfl[5];
47632  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
47633  u8 *aData;           /* Pointer to disk image of the page data */
47634  DbPage *pDbPage;     /* Pager page handle */
47635  Pgno pgno;           /* Page number for this page */
47636};
47637
47638/*
47639** The in-memory image of a disk page has the auxiliary information appended
47640** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
47641** that extra information.
47642*/
47643#define EXTRA_SIZE sizeof(MemPage)
47644
47645/*
47646** A linked list of the following structures is stored at BtShared.pLock.
47647** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
47648** is opened on the table with root page BtShared.iTable. Locks are removed
47649** from this list when a transaction is committed or rolled back, or when
47650** a btree handle is closed.
47651*/
47652struct BtLock {
47653  Btree *pBtree;        /* Btree handle holding this lock */
47654  Pgno iTable;          /* Root page of table */
47655  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
47656  BtLock *pNext;        /* Next in BtShared.pLock list */
47657};
47658
47659/* Candidate values for BtLock.eLock */
47660#define READ_LOCK     1
47661#define WRITE_LOCK    2
47662
47663/* A Btree handle
47664**
47665** A database connection contains a pointer to an instance of
47666** this object for every database file that it has open.  This structure
47667** is opaque to the database connection.  The database connection cannot
47668** see the internals of this structure and only deals with pointers to
47669** this structure.
47670**
47671** For some database files, the same underlying database cache might be
47672** shared between multiple connections.  In that case, each connection
47673** has it own instance of this object.  But each instance of this object
47674** points to the same BtShared object.  The database cache and the
47675** schema associated with the database file are all contained within
47676** the BtShared object.
47677**
47678** All fields in this structure are accessed under sqlite3.mutex.
47679** The pBt pointer itself may not be changed while there exists cursors
47680** in the referenced BtShared that point back to this Btree since those
47681** cursors have to go through this Btree to find their BtShared and
47682** they often do so without holding sqlite3.mutex.
47683*/
47684struct Btree {
47685  sqlite3 *db;       /* The database connection holding this btree */
47686  BtShared *pBt;     /* Sharable content of this btree */
47687  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
47688  u8 sharable;       /* True if we can share pBt with another db */
47689  u8 locked;         /* True if db currently has pBt locked */
47690  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
47691  int nBackup;       /* Number of backup operations reading this btree */
47692  Btree *pNext;      /* List of other sharable Btrees from the same db */
47693  Btree *pPrev;      /* Back pointer of the same list */
47694#ifndef SQLITE_OMIT_SHARED_CACHE
47695  BtLock lock;       /* Object used to lock page 1 */
47696#endif
47697};
47698
47699/*
47700** Btree.inTrans may take one of the following values.
47701**
47702** If the shared-data extension is enabled, there may be multiple users
47703** of the Btree structure. At most one of these may open a write transaction,
47704** but any number may have active read transactions.
47705*/
47706#define TRANS_NONE  0
47707#define TRANS_READ  1
47708#define TRANS_WRITE 2
47709
47710/*
47711** An instance of this object represents a single database file.
47712**
47713** A single database file can be in use as the same time by two
47714** or more database connections.  When two or more connections are
47715** sharing the same database file, each connection has it own
47716** private Btree object for the file and each of those Btrees points
47717** to this one BtShared object.  BtShared.nRef is the number of
47718** connections currently sharing this database file.
47719**
47720** Fields in this structure are accessed under the BtShared.mutex
47721** mutex, except for nRef and pNext which are accessed under the
47722** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
47723** may not be modified once it is initially set as long as nRef>0.
47724** The pSchema field may be set once under BtShared.mutex and
47725** thereafter is unchanged as long as nRef>0.
47726**
47727** isPending:
47728**
47729**   If a BtShared client fails to obtain a write-lock on a database
47730**   table (because there exists one or more read-locks on the table),
47731**   the shared-cache enters 'pending-lock' state and isPending is
47732**   set to true.
47733**
47734**   The shared-cache leaves the 'pending lock' state when either of
47735**   the following occur:
47736**
47737**     1) The current writer (BtShared.pWriter) concludes its transaction, OR
47738**     2) The number of locks held by other connections drops to zero.
47739**
47740**   while in the 'pending-lock' state, no connection may start a new
47741**   transaction.
47742**
47743**   This feature is included to help prevent writer-starvation.
47744*/
47745struct BtShared {
47746  Pager *pPager;        /* The page cache */
47747  sqlite3 *db;          /* Database connection currently using this Btree */
47748  BtCursor *pCursor;    /* A list of all open cursors */
47749  MemPage *pPage1;      /* First page of the database */
47750  u8 readOnly;          /* True if the underlying file is readonly */
47751  u8 pageSizeFixed;     /* True if the page size can no longer be changed */
47752  u8 secureDelete;      /* True if secure_delete is enabled */
47753  u8 initiallyEmpty;    /* Database is empty at start of transaction */
47754  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
47755#ifndef SQLITE_OMIT_AUTOVACUUM
47756  u8 autoVacuum;        /* True if auto-vacuum is enabled */
47757  u8 incrVacuum;        /* True if incr-vacuum is enabled */
47758#endif
47759  u8 inTransaction;     /* Transaction state */
47760  u8 doNotUseWAL;       /* If true, do not open write-ahead-log file */
47761  u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
47762  u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
47763  u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
47764  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
47765  u32 pageSize;         /* Total number of bytes on a page */
47766  u32 usableSize;       /* Number of usable bytes on each page */
47767  int nTransaction;     /* Number of open transactions (read + write) */
47768  u32 nPage;            /* Number of pages in the database */
47769  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
47770  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
47771  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
47772  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
47773#ifndef SQLITE_OMIT_SHARED_CACHE
47774  int nRef;             /* Number of references to this structure */
47775  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
47776  BtLock *pLock;        /* List of locks held on this shared-btree struct */
47777  Btree *pWriter;       /* Btree with currently open write transaction */
47778  u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
47779  u8 isPending;         /* If waiting for read-locks to clear */
47780#endif
47781  u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
47782};
47783
47784/*
47785** An instance of the following structure is used to hold information
47786** about a cell.  The parseCellPtr() function fills in this structure
47787** based on information extract from the raw disk page.
47788*/
47789typedef struct CellInfo CellInfo;
47790struct CellInfo {
47791  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
47792  u8 *pCell;     /* Pointer to the start of cell content */
47793  u32 nData;     /* Number of bytes of data */
47794  u32 nPayload;  /* Total amount of payload */
47795  u16 nHeader;   /* Size of the cell content header in bytes */
47796  u16 nLocal;    /* Amount of payload held locally */
47797  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
47798  u16 nSize;     /* Size of the cell content on the main b-tree page */
47799};
47800
47801/*
47802** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
47803** this will be declared corrupt. This value is calculated based on a
47804** maximum database size of 2^31 pages a minimum fanout of 2 for a
47805** root-node and 3 for all other internal nodes.
47806**
47807** If a tree that appears to be taller than this is encountered, it is
47808** assumed that the database is corrupt.
47809*/
47810#define BTCURSOR_MAX_DEPTH 20
47811
47812/*
47813** A cursor is a pointer to a particular entry within a particular
47814** b-tree within a database file.
47815**
47816** The entry is identified by its MemPage and the index in
47817** MemPage.aCell[] of the entry.
47818**
47819** A single database file can shared by two more database connections,
47820** but cursors cannot be shared.  Each cursor is associated with a
47821** particular database connection identified BtCursor.pBtree.db.
47822**
47823** Fields in this structure are accessed under the BtShared.mutex
47824** found at self->pBt->mutex.
47825*/
47826struct BtCursor {
47827  Btree *pBtree;            /* The Btree to which this cursor belongs */
47828  BtShared *pBt;            /* The BtShared this cursor points to */
47829  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
47830  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
47831  Pgno pgnoRoot;            /* The root page of this tree */
47832  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
47833  CellInfo info;            /* A parse of the cell we are pointing at */
47834  i64 nKey;        /* Size of pKey, or last integer key */
47835  void *pKey;      /* Saved key that was cursor's last known position */
47836  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
47837  u8 wrFlag;                /* True if writable */
47838  u8 atLast;                /* Cursor pointing to the last entry */
47839  u8 validNKey;             /* True if info.nKey is valid */
47840  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
47841#ifndef SQLITE_OMIT_INCRBLOB
47842  Pgno *aOverflow;          /* Cache of overflow page locations */
47843  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
47844#endif
47845  i16 iPage;                            /* Index of current page in apPage */
47846  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
47847  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
47848};
47849
47850/*
47851** Potential values for BtCursor.eState.
47852**
47853** CURSOR_VALID:
47854**   Cursor points to a valid entry. getPayload() etc. may be called.
47855**
47856** CURSOR_INVALID:
47857**   Cursor does not point to a valid entry. This can happen (for example)
47858**   because the table is empty or because BtreeCursorFirst() has not been
47859**   called.
47860**
47861** CURSOR_REQUIRESEEK:
47862**   The table that this cursor was opened on still exists, but has been
47863**   modified since the cursor was last used. The cursor position is saved
47864**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
47865**   this state, restoreCursorPosition() can be called to attempt to
47866**   seek the cursor to the saved position.
47867**
47868** CURSOR_FAULT:
47869**   A unrecoverable error (an I/O error or a malloc failure) has occurred
47870**   on a different connection that shares the BtShared cache with this
47871**   cursor.  The error has left the cache in an inconsistent state.
47872**   Do nothing else with this cursor.  Any attempt to use the cursor
47873**   should return the error code stored in BtCursor.skip
47874*/
47875#define CURSOR_INVALID           0
47876#define CURSOR_VALID             1
47877#define CURSOR_REQUIRESEEK       2
47878#define CURSOR_FAULT             3
47879
47880/*
47881** The database page the PENDING_BYTE occupies. This page is never used.
47882*/
47883# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
47884
47885/*
47886** These macros define the location of the pointer-map entry for a
47887** database page. The first argument to each is the number of usable
47888** bytes on each page of the database (often 1024). The second is the
47889** page number to look up in the pointer map.
47890**
47891** PTRMAP_PAGENO returns the database page number of the pointer-map
47892** page that stores the required pointer. PTRMAP_PTROFFSET returns
47893** the offset of the requested map entry.
47894**
47895** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
47896** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
47897** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
47898** this test.
47899*/
47900#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
47901#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
47902#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
47903
47904/*
47905** The pointer map is a lookup table that identifies the parent page for
47906** each child page in the database file.  The parent page is the page that
47907** contains a pointer to the child.  Every page in the database contains
47908** 0 or 1 parent pages.  (In this context 'database page' refers
47909** to any page that is not part of the pointer map itself.)  Each pointer map
47910** entry consists of a single byte 'type' and a 4 byte parent page number.
47911** The PTRMAP_XXX identifiers below are the valid types.
47912**
47913** The purpose of the pointer map is to facility moving pages from one
47914** position in the file to another as part of autovacuum.  When a page
47915** is moved, the pointer in its parent must be updated to point to the
47916** new location.  The pointer map is used to locate the parent page quickly.
47917**
47918** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
47919**                  used in this case.
47920**
47921** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
47922**                  is not used in this case.
47923**
47924** PTRMAP_OVERFLOW1: The database page is the first page in a list of
47925**                   overflow pages. The page number identifies the page that
47926**                   contains the cell with a pointer to this overflow page.
47927**
47928** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
47929**                   overflow pages. The page-number identifies the previous
47930**                   page in the overflow page list.
47931**
47932** PTRMAP_BTREE: The database page is a non-root btree page. The page number
47933**               identifies the parent page in the btree.
47934*/
47935#define PTRMAP_ROOTPAGE 1
47936#define PTRMAP_FREEPAGE 2
47937#define PTRMAP_OVERFLOW1 3
47938#define PTRMAP_OVERFLOW2 4
47939#define PTRMAP_BTREE 5
47940
47941/* A bunch of assert() statements to check the transaction state variables
47942** of handle p (type Btree*) are internally consistent.
47943*/
47944#define btreeIntegrity(p) \
47945  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
47946  assert( p->pBt->inTransaction>=p->inTrans );
47947
47948
47949/*
47950** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
47951** if the database supports auto-vacuum or not. Because it is used
47952** within an expression that is an argument to another macro
47953** (sqliteMallocRaw), it is not possible to use conditional compilation.
47954** So, this macro is defined instead.
47955*/
47956#ifndef SQLITE_OMIT_AUTOVACUUM
47957#define ISAUTOVACUUM (pBt->autoVacuum)
47958#else
47959#define ISAUTOVACUUM 0
47960#endif
47961
47962
47963/*
47964** This structure is passed around through all the sanity checking routines
47965** in order to keep track of some global state information.
47966*/
47967typedef struct IntegrityCk IntegrityCk;
47968struct IntegrityCk {
47969  BtShared *pBt;    /* The tree being checked out */
47970  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
47971  Pgno nPage;       /* Number of pages in the database */
47972  int *anRef;       /* Number of times each page is referenced */
47973  int mxErr;        /* Stop accumulating errors when this reaches zero */
47974  int nErr;         /* Number of messages written to zErrMsg so far */
47975  int mallocFailed; /* A memory allocation error has occurred */
47976  StrAccum errMsg;  /* Accumulate the error message text here */
47977};
47978
47979/*
47980** Read or write a two- and four-byte big-endian integer values.
47981*/
47982#define get2byte(x)   ((x)[0]<<8 | (x)[1])
47983#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
47984#define get4byte sqlite3Get4byte
47985#define put4byte sqlite3Put4byte
47986
47987/************** End of btreeInt.h ********************************************/
47988/************** Continuing where we left off in btmutex.c ********************/
47989#ifndef SQLITE_OMIT_SHARED_CACHE
47990#if SQLITE_THREADSAFE
47991
47992/*
47993** Obtain the BtShared mutex associated with B-Tree handle p. Also,
47994** set BtShared.db to the database handle associated with p and the
47995** p->locked boolean to true.
47996*/
47997static void lockBtreeMutex(Btree *p){
47998  assert( p->locked==0 );
47999  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
48000  assert( sqlite3_mutex_held(p->db->mutex) );
48001
48002  sqlite3_mutex_enter(p->pBt->mutex);
48003  p->pBt->db = p->db;
48004  p->locked = 1;
48005}
48006
48007/*
48008** Release the BtShared mutex associated with B-Tree handle p and
48009** clear the p->locked boolean.
48010*/
48011static void unlockBtreeMutex(Btree *p){
48012  BtShared *pBt = p->pBt;
48013  assert( p->locked==1 );
48014  assert( sqlite3_mutex_held(pBt->mutex) );
48015  assert( sqlite3_mutex_held(p->db->mutex) );
48016  assert( p->db==pBt->db );
48017
48018  sqlite3_mutex_leave(pBt->mutex);
48019  p->locked = 0;
48020}
48021
48022/*
48023** Enter a mutex on the given BTree object.
48024**
48025** If the object is not sharable, then no mutex is ever required
48026** and this routine is a no-op.  The underlying mutex is non-recursive.
48027** But we keep a reference count in Btree.wantToLock so the behavior
48028** of this interface is recursive.
48029**
48030** To avoid deadlocks, multiple Btrees are locked in the same order
48031** by all database connections.  The p->pNext is a list of other
48032** Btrees belonging to the same database connection as the p Btree
48033** which need to be locked after p.  If we cannot get a lock on
48034** p, then first unlock all of the others on p->pNext, then wait
48035** for the lock to become available on p, then relock all of the
48036** subsequent Btrees that desire a lock.
48037*/
48038SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
48039  Btree *pLater;
48040
48041  /* Some basic sanity checking on the Btree.  The list of Btrees
48042  ** connected by pNext and pPrev should be in sorted order by
48043  ** Btree.pBt value. All elements of the list should belong to
48044  ** the same connection. Only shared Btrees are on the list. */
48045  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
48046  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
48047  assert( p->pNext==0 || p->pNext->db==p->db );
48048  assert( p->pPrev==0 || p->pPrev->db==p->db );
48049  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
48050
48051  /* Check for locking consistency */
48052  assert( !p->locked || p->wantToLock>0 );
48053  assert( p->sharable || p->wantToLock==0 );
48054
48055  /* We should already hold a lock on the database connection */
48056  assert( sqlite3_mutex_held(p->db->mutex) );
48057
48058  /* Unless the database is sharable and unlocked, then BtShared.db
48059  ** should already be set correctly. */
48060  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
48061
48062  if( !p->sharable ) return;
48063  p->wantToLock++;
48064  if( p->locked ) return;
48065
48066  /* In most cases, we should be able to acquire the lock we
48067  ** want without having to go throught the ascending lock
48068  ** procedure that follows.  Just be sure not to block.
48069  */
48070  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
48071    p->pBt->db = p->db;
48072    p->locked = 1;
48073    return;
48074  }
48075
48076  /* To avoid deadlock, first release all locks with a larger
48077  ** BtShared address.  Then acquire our lock.  Then reacquire
48078  ** the other BtShared locks that we used to hold in ascending
48079  ** order.
48080  */
48081  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
48082    assert( pLater->sharable );
48083    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
48084    assert( !pLater->locked || pLater->wantToLock>0 );
48085    if( pLater->locked ){
48086      unlockBtreeMutex(pLater);
48087    }
48088  }
48089  lockBtreeMutex(p);
48090  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
48091    if( pLater->wantToLock ){
48092      lockBtreeMutex(pLater);
48093    }
48094  }
48095}
48096
48097/*
48098** Exit the recursive mutex on a Btree.
48099*/
48100SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
48101  if( p->sharable ){
48102    assert( p->wantToLock>0 );
48103    p->wantToLock--;
48104    if( p->wantToLock==0 ){
48105      unlockBtreeMutex(p);
48106    }
48107  }
48108}
48109
48110#ifndef NDEBUG
48111/*
48112** Return true if the BtShared mutex is held on the btree, or if the
48113** B-Tree is not marked as sharable.
48114**
48115** This routine is used only from within assert() statements.
48116*/
48117SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
48118  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
48119  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
48120  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
48121  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
48122
48123  return (p->sharable==0 || p->locked);
48124}
48125#endif
48126
48127
48128#ifndef SQLITE_OMIT_INCRBLOB
48129/*
48130** Enter and leave a mutex on a Btree given a cursor owned by that
48131** Btree.  These entry points are used by incremental I/O and can be
48132** omitted if that module is not used.
48133*/
48134SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
48135  sqlite3BtreeEnter(pCur->pBtree);
48136}
48137SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
48138  sqlite3BtreeLeave(pCur->pBtree);
48139}
48140#endif /* SQLITE_OMIT_INCRBLOB */
48141
48142
48143/*
48144** Enter the mutex on every Btree associated with a database
48145** connection.  This is needed (for example) prior to parsing
48146** a statement since we will be comparing table and column names
48147** against all schemas and we do not want those schemas being
48148** reset out from under us.
48149**
48150** There is a corresponding leave-all procedures.
48151**
48152** Enter the mutexes in accending order by BtShared pointer address
48153** to avoid the possibility of deadlock when two threads with
48154** two or more btrees in common both try to lock all their btrees
48155** at the same instant.
48156*/
48157SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
48158  int i;
48159  Btree *p;
48160  assert( sqlite3_mutex_held(db->mutex) );
48161  for(i=0; i<db->nDb; i++){
48162    p = db->aDb[i].pBt;
48163    if( p ) sqlite3BtreeEnter(p);
48164  }
48165}
48166SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
48167  int i;
48168  Btree *p;
48169  assert( sqlite3_mutex_held(db->mutex) );
48170  for(i=0; i<db->nDb; i++){
48171    p = db->aDb[i].pBt;
48172    if( p ) sqlite3BtreeLeave(p);
48173  }
48174}
48175
48176/*
48177** Return true if a particular Btree requires a lock.  Return FALSE if
48178** no lock is ever required since it is not sharable.
48179*/
48180SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
48181  return p->sharable;
48182}
48183
48184#ifndef NDEBUG
48185/*
48186** Return true if the current thread holds the database connection
48187** mutex and all required BtShared mutexes.
48188**
48189** This routine is used inside assert() statements only.
48190*/
48191SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
48192  int i;
48193  if( !sqlite3_mutex_held(db->mutex) ){
48194    return 0;
48195  }
48196  for(i=0; i<db->nDb; i++){
48197    Btree *p;
48198    p = db->aDb[i].pBt;
48199    if( p && p->sharable &&
48200         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
48201      return 0;
48202    }
48203  }
48204  return 1;
48205}
48206#endif /* NDEBUG */
48207
48208#ifndef NDEBUG
48209/*
48210** Return true if the correct mutexes are held for accessing the
48211** db->aDb[iDb].pSchema structure.  The mutexes required for schema
48212** access are:
48213**
48214**   (1) The mutex on db
48215**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
48216**
48217** If pSchema is not NULL, then iDb is computed from pSchema and
48218** db using sqlite3SchemaToIndex().
48219*/
48220SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
48221  Btree *p;
48222  assert( db!=0 );
48223  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
48224  assert( iDb>=0 && iDb<db->nDb );
48225  if( !sqlite3_mutex_held(db->mutex) ) return 0;
48226  if( iDb==1 ) return 1;
48227  p = db->aDb[iDb].pBt;
48228  assert( p!=0 );
48229  return p->sharable==0 || p->locked==1;
48230}
48231#endif /* NDEBUG */
48232
48233#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
48234/*
48235** The following are special cases for mutex enter routines for use
48236** in single threaded applications that use shared cache.  Except for
48237** these two routines, all mutex operations are no-ops in that case and
48238** are null #defines in btree.h.
48239**
48240** If shared cache is disabled, then all btree mutex routines, including
48241** the ones below, are no-ops and are null #defines in btree.h.
48242*/
48243
48244SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
48245  p->pBt->db = p->db;
48246}
48247SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
48248  int i;
48249  for(i=0; i<db->nDb; i++){
48250    Btree *p = db->aDb[i].pBt;
48251    if( p ){
48252      p->pBt->db = p->db;
48253    }
48254  }
48255}
48256#endif /* if SQLITE_THREADSAFE */
48257#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
48258
48259/************** End of btmutex.c *********************************************/
48260/************** Begin file btree.c *******************************************/
48261/*
48262** 2004 April 6
48263**
48264** The author disclaims copyright to this source code.  In place of
48265** a legal notice, here is a blessing:
48266**
48267**    May you do good and not evil.
48268**    May you find forgiveness for yourself and forgive others.
48269**    May you share freely, never taking more than you give.
48270**
48271*************************************************************************
48272** This file implements a external (disk-based) database using BTrees.
48273** See the header comment on "btreeInt.h" for additional information.
48274** Including a description of file format and an overview of operation.
48275*/
48276
48277/*
48278** The header string that appears at the beginning of every
48279** SQLite database.
48280*/
48281static const char zMagicHeader[] = SQLITE_FILE_HEADER;
48282
48283/*
48284** Set this global variable to 1 to enable tracing using the TRACE
48285** macro.
48286*/
48287#if 0
48288int sqlite3BtreeTrace=1;  /* True to enable tracing */
48289# define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
48290#else
48291# define TRACE(X)
48292#endif
48293
48294/*
48295** Extract a 2-byte big-endian integer from an array of unsigned bytes.
48296** But if the value is zero, make it 65536.
48297**
48298** This routine is used to extract the "offset to cell content area" value
48299** from the header of a btree page.  If the page size is 65536 and the page
48300** is empty, the offset should be 65536, but the 2-byte value stores zero.
48301** This routine makes the necessary adjustment to 65536.
48302*/
48303#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
48304
48305#ifndef SQLITE_OMIT_SHARED_CACHE
48306/*
48307** A list of BtShared objects that are eligible for participation
48308** in shared cache.  This variable has file scope during normal builds,
48309** but the test harness needs to access it so we make it global for
48310** test builds.
48311**
48312** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
48313*/
48314#ifdef SQLITE_TEST
48315SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
48316#else
48317static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
48318#endif
48319#endif /* SQLITE_OMIT_SHARED_CACHE */
48320
48321#ifndef SQLITE_OMIT_SHARED_CACHE
48322/*
48323** Enable or disable the shared pager and schema features.
48324**
48325** This routine has no effect on existing database connections.
48326** The shared cache setting effects only future calls to
48327** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
48328*/
48329SQLITE_API int sqlite3_enable_shared_cache(int enable){
48330  sqlite3GlobalConfig.sharedCacheEnabled = enable;
48331  return SQLITE_OK;
48332}
48333#endif
48334
48335
48336
48337#ifdef SQLITE_OMIT_SHARED_CACHE
48338  /*
48339  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
48340  ** and clearAllSharedCacheTableLocks()
48341  ** manipulate entries in the BtShared.pLock linked list used to store
48342  ** shared-cache table level locks. If the library is compiled with the
48343  ** shared-cache feature disabled, then there is only ever one user
48344  ** of each BtShared structure and so this locking is not necessary.
48345  ** So define the lock related functions as no-ops.
48346  */
48347  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
48348  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
48349  #define clearAllSharedCacheTableLocks(a)
48350  #define downgradeAllSharedCacheTableLocks(a)
48351  #define hasSharedCacheTableLock(a,b,c,d) 1
48352  #define hasReadConflicts(a, b) 0
48353#endif
48354
48355#ifndef SQLITE_OMIT_SHARED_CACHE
48356
48357#ifdef SQLITE_DEBUG
48358/*
48359**** This function is only used as part of an assert() statement. ***
48360**
48361** Check to see if pBtree holds the required locks to read or write to the
48362** table with root page iRoot.   Return 1 if it does and 0 if not.
48363**
48364** For example, when writing to a table with root-page iRoot via
48365** Btree connection pBtree:
48366**
48367**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
48368**
48369** When writing to an index that resides in a sharable database, the
48370** caller should have first obtained a lock specifying the root page of
48371** the corresponding table. This makes things a bit more complicated,
48372** as this module treats each table as a separate structure. To determine
48373** the table corresponding to the index being written, this
48374** function has to search through the database schema.
48375**
48376** Instead of a lock on the table/index rooted at page iRoot, the caller may
48377** hold a write-lock on the schema table (root page 1). This is also
48378** acceptable.
48379*/
48380static int hasSharedCacheTableLock(
48381  Btree *pBtree,         /* Handle that must hold lock */
48382  Pgno iRoot,            /* Root page of b-tree */
48383  int isIndex,           /* True if iRoot is the root of an index b-tree */
48384  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
48385){
48386  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
48387  Pgno iTab = 0;
48388  BtLock *pLock;
48389
48390  /* If this database is not shareable, or if the client is reading
48391  ** and has the read-uncommitted flag set, then no lock is required.
48392  ** Return true immediately.
48393  */
48394  if( (pBtree->sharable==0)
48395   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
48396  ){
48397    return 1;
48398  }
48399
48400  /* If the client is reading  or writing an index and the schema is
48401  ** not loaded, then it is too difficult to actually check to see if
48402  ** the correct locks are held.  So do not bother - just return true.
48403  ** This case does not come up very often anyhow.
48404  */
48405  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
48406    return 1;
48407  }
48408
48409  /* Figure out the root-page that the lock should be held on. For table
48410  ** b-trees, this is just the root page of the b-tree being read or
48411  ** written. For index b-trees, it is the root page of the associated
48412  ** table.  */
48413  if( isIndex ){
48414    HashElem *p;
48415    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
48416      Index *pIdx = (Index *)sqliteHashData(p);
48417      if( pIdx->tnum==(int)iRoot ){
48418        iTab = pIdx->pTable->tnum;
48419      }
48420    }
48421  }else{
48422    iTab = iRoot;
48423  }
48424
48425  /* Search for the required lock. Either a write-lock on root-page iTab, a
48426  ** write-lock on the schema table, or (if the client is reading) a
48427  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
48428  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
48429    if( pLock->pBtree==pBtree
48430     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
48431     && pLock->eLock>=eLockType
48432    ){
48433      return 1;
48434    }
48435  }
48436
48437  /* Failed to find the required lock. */
48438  return 0;
48439}
48440#endif /* SQLITE_DEBUG */
48441
48442#ifdef SQLITE_DEBUG
48443/*
48444**** This function may be used as part of assert() statements only. ****
48445**
48446** Return true if it would be illegal for pBtree to write into the
48447** table or index rooted at iRoot because other shared connections are
48448** simultaneously reading that same table or index.
48449**
48450** It is illegal for pBtree to write if some other Btree object that
48451** shares the same BtShared object is currently reading or writing
48452** the iRoot table.  Except, if the other Btree object has the
48453** read-uncommitted flag set, then it is OK for the other object to
48454** have a read cursor.
48455**
48456** For example, before writing to any part of the table or index
48457** rooted at page iRoot, one should call:
48458**
48459**    assert( !hasReadConflicts(pBtree, iRoot) );
48460*/
48461static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
48462  BtCursor *p;
48463  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
48464    if( p->pgnoRoot==iRoot
48465     && p->pBtree!=pBtree
48466     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
48467    ){
48468      return 1;
48469    }
48470  }
48471  return 0;
48472}
48473#endif    /* #ifdef SQLITE_DEBUG */
48474
48475/*
48476** Query to see if Btree handle p may obtain a lock of type eLock
48477** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
48478** SQLITE_OK if the lock may be obtained (by calling
48479** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
48480*/
48481static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
48482  BtShared *pBt = p->pBt;
48483  BtLock *pIter;
48484
48485  assert( sqlite3BtreeHoldsMutex(p) );
48486  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
48487  assert( p->db!=0 );
48488  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
48489
48490  /* If requesting a write-lock, then the Btree must have an open write
48491  ** transaction on this file. And, obviously, for this to be so there
48492  ** must be an open write transaction on the file itself.
48493  */
48494  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
48495  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
48496
48497  /* This routine is a no-op if the shared-cache is not enabled */
48498  if( !p->sharable ){
48499    return SQLITE_OK;
48500  }
48501
48502  /* If some other connection is holding an exclusive lock, the
48503  ** requested lock may not be obtained.
48504  */
48505  if( pBt->pWriter!=p && pBt->isExclusive ){
48506    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
48507    return SQLITE_LOCKED_SHAREDCACHE;
48508  }
48509
48510  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
48511    /* The condition (pIter->eLock!=eLock) in the following if(...)
48512    ** statement is a simplification of:
48513    **
48514    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
48515    **
48516    ** since we know that if eLock==WRITE_LOCK, then no other connection
48517    ** may hold a WRITE_LOCK on any table in this file (since there can
48518    ** only be a single writer).
48519    */
48520    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
48521    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
48522    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
48523      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
48524      if( eLock==WRITE_LOCK ){
48525        assert( p==pBt->pWriter );
48526        pBt->isPending = 1;
48527      }
48528      return SQLITE_LOCKED_SHAREDCACHE;
48529    }
48530  }
48531  return SQLITE_OK;
48532}
48533#endif /* !SQLITE_OMIT_SHARED_CACHE */
48534
48535#ifndef SQLITE_OMIT_SHARED_CACHE
48536/*
48537** Add a lock on the table with root-page iTable to the shared-btree used
48538** by Btree handle p. Parameter eLock must be either READ_LOCK or
48539** WRITE_LOCK.
48540**
48541** This function assumes the following:
48542**
48543**   (a) The specified Btree object p is connected to a sharable
48544**       database (one with the BtShared.sharable flag set), and
48545**
48546**   (b) No other Btree objects hold a lock that conflicts
48547**       with the requested lock (i.e. querySharedCacheTableLock() has
48548**       already been called and returned SQLITE_OK).
48549**
48550** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
48551** is returned if a malloc attempt fails.
48552*/
48553static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
48554  BtShared *pBt = p->pBt;
48555  BtLock *pLock = 0;
48556  BtLock *pIter;
48557
48558  assert( sqlite3BtreeHoldsMutex(p) );
48559  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
48560  assert( p->db!=0 );
48561
48562  /* A connection with the read-uncommitted flag set will never try to
48563  ** obtain a read-lock using this function. The only read-lock obtained
48564  ** by a connection in read-uncommitted mode is on the sqlite_master
48565  ** table, and that lock is obtained in BtreeBeginTrans().  */
48566  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
48567
48568  /* This function should only be called on a sharable b-tree after it
48569  ** has been determined that no other b-tree holds a conflicting lock.  */
48570  assert( p->sharable );
48571  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
48572
48573  /* First search the list for an existing lock on this table. */
48574  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
48575    if( pIter->iTable==iTable && pIter->pBtree==p ){
48576      pLock = pIter;
48577      break;
48578    }
48579  }
48580
48581  /* If the above search did not find a BtLock struct associating Btree p
48582  ** with table iTable, allocate one and link it into the list.
48583  */
48584  if( !pLock ){
48585    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
48586    if( !pLock ){
48587      return SQLITE_NOMEM;
48588    }
48589    pLock->iTable = iTable;
48590    pLock->pBtree = p;
48591    pLock->pNext = pBt->pLock;
48592    pBt->pLock = pLock;
48593  }
48594
48595  /* Set the BtLock.eLock variable to the maximum of the current lock
48596  ** and the requested lock. This means if a write-lock was already held
48597  ** and a read-lock requested, we don't incorrectly downgrade the lock.
48598  */
48599  assert( WRITE_LOCK>READ_LOCK );
48600  if( eLock>pLock->eLock ){
48601    pLock->eLock = eLock;
48602  }
48603
48604  return SQLITE_OK;
48605}
48606#endif /* !SQLITE_OMIT_SHARED_CACHE */
48607
48608#ifndef SQLITE_OMIT_SHARED_CACHE
48609/*
48610** Release all the table locks (locks obtained via calls to
48611** the setSharedCacheTableLock() procedure) held by Btree object p.
48612**
48613** This function assumes that Btree p has an open read or write
48614** transaction. If it does not, then the BtShared.isPending variable
48615** may be incorrectly cleared.
48616*/
48617static void clearAllSharedCacheTableLocks(Btree *p){
48618  BtShared *pBt = p->pBt;
48619  BtLock **ppIter = &pBt->pLock;
48620
48621  assert( sqlite3BtreeHoldsMutex(p) );
48622  assert( p->sharable || 0==*ppIter );
48623  assert( p->inTrans>0 );
48624
48625  while( *ppIter ){
48626    BtLock *pLock = *ppIter;
48627    assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
48628    assert( pLock->pBtree->inTrans>=pLock->eLock );
48629    if( pLock->pBtree==p ){
48630      *ppIter = pLock->pNext;
48631      assert( pLock->iTable!=1 || pLock==&p->lock );
48632      if( pLock->iTable!=1 ){
48633        sqlite3_free(pLock);
48634      }
48635    }else{
48636      ppIter = &pLock->pNext;
48637    }
48638  }
48639
48640  assert( pBt->isPending==0 || pBt->pWriter );
48641  if( pBt->pWriter==p ){
48642    pBt->pWriter = 0;
48643    pBt->isExclusive = 0;
48644    pBt->isPending = 0;
48645  }else if( pBt->nTransaction==2 ){
48646    /* This function is called when Btree p is concluding its
48647    ** transaction. If there currently exists a writer, and p is not
48648    ** that writer, then the number of locks held by connections other
48649    ** than the writer must be about to drop to zero. In this case
48650    ** set the isPending flag to 0.
48651    **
48652    ** If there is not currently a writer, then BtShared.isPending must
48653    ** be zero already. So this next line is harmless in that case.
48654    */
48655    pBt->isPending = 0;
48656  }
48657}
48658
48659/*
48660** This function changes all write-locks held by Btree p into read-locks.
48661*/
48662static void downgradeAllSharedCacheTableLocks(Btree *p){
48663  BtShared *pBt = p->pBt;
48664  if( pBt->pWriter==p ){
48665    BtLock *pLock;
48666    pBt->pWriter = 0;
48667    pBt->isExclusive = 0;
48668    pBt->isPending = 0;
48669    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
48670      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
48671      pLock->eLock = READ_LOCK;
48672    }
48673  }
48674}
48675
48676#endif /* SQLITE_OMIT_SHARED_CACHE */
48677
48678static void releasePage(MemPage *pPage);  /* Forward reference */
48679
48680/*
48681***** This routine is used inside of assert() only ****
48682**
48683** Verify that the cursor holds the mutex on its BtShared
48684*/
48685#ifdef SQLITE_DEBUG
48686static int cursorHoldsMutex(BtCursor *p){
48687  return sqlite3_mutex_held(p->pBt->mutex);
48688}
48689#endif
48690
48691
48692#ifndef SQLITE_OMIT_INCRBLOB
48693/*
48694** Invalidate the overflow page-list cache for cursor pCur, if any.
48695*/
48696static void invalidateOverflowCache(BtCursor *pCur){
48697  assert( cursorHoldsMutex(pCur) );
48698  sqlite3_free(pCur->aOverflow);
48699  pCur->aOverflow = 0;
48700}
48701
48702/*
48703** Invalidate the overflow page-list cache for all cursors opened
48704** on the shared btree structure pBt.
48705*/
48706static void invalidateAllOverflowCache(BtShared *pBt){
48707  BtCursor *p;
48708  assert( sqlite3_mutex_held(pBt->mutex) );
48709  for(p=pBt->pCursor; p; p=p->pNext){
48710    invalidateOverflowCache(p);
48711  }
48712}
48713
48714/*
48715** This function is called before modifying the contents of a table
48716** to invalidate any incrblob cursors that are open on the
48717** row or one of the rows being modified.
48718**
48719** If argument isClearTable is true, then the entire contents of the
48720** table is about to be deleted. In this case invalidate all incrblob
48721** cursors open on any row within the table with root-page pgnoRoot.
48722**
48723** Otherwise, if argument isClearTable is false, then the row with
48724** rowid iRow is being replaced or deleted. In this case invalidate
48725** only those incrblob cursors open on that specific row.
48726*/
48727static void invalidateIncrblobCursors(
48728  Btree *pBtree,          /* The database file to check */
48729  i64 iRow,               /* The rowid that might be changing */
48730  int isClearTable        /* True if all rows are being deleted */
48731){
48732  BtCursor *p;
48733  BtShared *pBt = pBtree->pBt;
48734  assert( sqlite3BtreeHoldsMutex(pBtree) );
48735  for(p=pBt->pCursor; p; p=p->pNext){
48736    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
48737      p->eState = CURSOR_INVALID;
48738    }
48739  }
48740}
48741
48742#else
48743  /* Stub functions when INCRBLOB is omitted */
48744  #define invalidateOverflowCache(x)
48745  #define invalidateAllOverflowCache(x)
48746  #define invalidateIncrblobCursors(x,y,z)
48747#endif /* SQLITE_OMIT_INCRBLOB */
48748
48749/*
48750** Set bit pgno of the BtShared.pHasContent bitvec. This is called
48751** when a page that previously contained data becomes a free-list leaf
48752** page.
48753**
48754** The BtShared.pHasContent bitvec exists to work around an obscure
48755** bug caused by the interaction of two useful IO optimizations surrounding
48756** free-list leaf pages:
48757**
48758**   1) When all data is deleted from a page and the page becomes
48759**      a free-list leaf page, the page is not written to the database
48760**      (as free-list leaf pages contain no meaningful data). Sometimes
48761**      such a page is not even journalled (as it will not be modified,
48762**      why bother journalling it?).
48763**
48764**   2) When a free-list leaf page is reused, its content is not read
48765**      from the database or written to the journal file (why should it
48766**      be, if it is not at all meaningful?).
48767**
48768** By themselves, these optimizations work fine and provide a handy
48769** performance boost to bulk delete or insert operations. However, if
48770** a page is moved to the free-list and then reused within the same
48771** transaction, a problem comes up. If the page is not journalled when
48772** it is moved to the free-list and it is also not journalled when it
48773** is extracted from the free-list and reused, then the original data
48774** may be lost. In the event of a rollback, it may not be possible
48775** to restore the database to its original configuration.
48776**
48777** The solution is the BtShared.pHasContent bitvec. Whenever a page is
48778** moved to become a free-list leaf page, the corresponding bit is
48779** set in the bitvec. Whenever a leaf page is extracted from the free-list,
48780** optimization 2 above is omitted if the corresponding bit is already
48781** set in BtShared.pHasContent. The contents of the bitvec are cleared
48782** at the end of every transaction.
48783*/
48784static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
48785  int rc = SQLITE_OK;
48786  if( !pBt->pHasContent ){
48787    assert( pgno<=pBt->nPage );
48788    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
48789    if( !pBt->pHasContent ){
48790      rc = SQLITE_NOMEM;
48791    }
48792  }
48793  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
48794    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
48795  }
48796  return rc;
48797}
48798
48799/*
48800** Query the BtShared.pHasContent vector.
48801**
48802** This function is called when a free-list leaf page is removed from the
48803** free-list for reuse. It returns false if it is safe to retrieve the
48804** page from the pager layer with the 'no-content' flag set. True otherwise.
48805*/
48806static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
48807  Bitvec *p = pBt->pHasContent;
48808  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
48809}
48810
48811/*
48812** Clear (destroy) the BtShared.pHasContent bitvec. This should be
48813** invoked at the conclusion of each write-transaction.
48814*/
48815static void btreeClearHasContent(BtShared *pBt){
48816  sqlite3BitvecDestroy(pBt->pHasContent);
48817  pBt->pHasContent = 0;
48818}
48819
48820/*
48821** Save the current cursor position in the variables BtCursor.nKey
48822** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
48823**
48824** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
48825** prior to calling this routine.
48826*/
48827static int saveCursorPosition(BtCursor *pCur){
48828  int rc;
48829
48830  assert( CURSOR_VALID==pCur->eState );
48831  assert( 0==pCur->pKey );
48832  assert( cursorHoldsMutex(pCur) );
48833
48834  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
48835  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
48836
48837  /* If this is an intKey table, then the above call to BtreeKeySize()
48838  ** stores the integer key in pCur->nKey. In this case this value is
48839  ** all that is required. Otherwise, if pCur is not open on an intKey
48840  ** table, then malloc space for and store the pCur->nKey bytes of key
48841  ** data.
48842  */
48843  if( 0==pCur->apPage[0]->intKey ){
48844    void *pKey = sqlite3Malloc( (int)pCur->nKey );
48845    if( pKey ){
48846      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
48847      if( rc==SQLITE_OK ){
48848        pCur->pKey = pKey;
48849      }else{
48850        sqlite3_free(pKey);
48851      }
48852    }else{
48853      rc = SQLITE_NOMEM;
48854    }
48855  }
48856  assert( !pCur->apPage[0]->intKey || !pCur->pKey );
48857
48858  if( rc==SQLITE_OK ){
48859    int i;
48860    for(i=0; i<=pCur->iPage; i++){
48861      releasePage(pCur->apPage[i]);
48862      pCur->apPage[i] = 0;
48863    }
48864    pCur->iPage = -1;
48865    pCur->eState = CURSOR_REQUIRESEEK;
48866  }
48867
48868  invalidateOverflowCache(pCur);
48869  return rc;
48870}
48871
48872/*
48873** Save the positions of all cursors (except pExcept) that are open on
48874** the table  with root-page iRoot. Usually, this is called just before cursor
48875** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
48876*/
48877static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
48878  BtCursor *p;
48879  assert( sqlite3_mutex_held(pBt->mutex) );
48880  assert( pExcept==0 || pExcept->pBt==pBt );
48881  for(p=pBt->pCursor; p; p=p->pNext){
48882    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
48883        p->eState==CURSOR_VALID ){
48884      int rc = saveCursorPosition(p);
48885      if( SQLITE_OK!=rc ){
48886        return rc;
48887      }
48888    }
48889  }
48890  return SQLITE_OK;
48891}
48892
48893/*
48894** Clear the current cursor position.
48895*/
48896SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
48897  assert( cursorHoldsMutex(pCur) );
48898  sqlite3_free(pCur->pKey);
48899  pCur->pKey = 0;
48900  pCur->eState = CURSOR_INVALID;
48901}
48902
48903/*
48904** In this version of BtreeMoveto, pKey is a packed index record
48905** such as is generated by the OP_MakeRecord opcode.  Unpack the
48906** record and then call BtreeMovetoUnpacked() to do the work.
48907*/
48908static int btreeMoveto(
48909  BtCursor *pCur,     /* Cursor open on the btree to be searched */
48910  const void *pKey,   /* Packed key if the btree is an index */
48911  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
48912  int bias,           /* Bias search to the high end */
48913  int *pRes           /* Write search results here */
48914){
48915  int rc;                    /* Status code */
48916  UnpackedRecord *pIdxKey;   /* Unpacked index key */
48917  char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
48918  char *pFree = 0;
48919
48920  if( pKey ){
48921    assert( nKey==(i64)(int)nKey );
48922    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
48923        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
48924    );
48925    if( pIdxKey==0 ) return SQLITE_NOMEM;
48926    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
48927  }else{
48928    pIdxKey = 0;
48929  }
48930  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
48931  if( pFree ){
48932    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
48933  }
48934  return rc;
48935}
48936
48937/*
48938** Restore the cursor to the position it was in (or as close to as possible)
48939** when saveCursorPosition() was called. Note that this call deletes the
48940** saved position info stored by saveCursorPosition(), so there can be
48941** at most one effective restoreCursorPosition() call after each
48942** saveCursorPosition().
48943*/
48944static int btreeRestoreCursorPosition(BtCursor *pCur){
48945  int rc;
48946  assert( cursorHoldsMutex(pCur) );
48947  assert( pCur->eState>=CURSOR_REQUIRESEEK );
48948  if( pCur->eState==CURSOR_FAULT ){
48949    return pCur->skipNext;
48950  }
48951  pCur->eState = CURSOR_INVALID;
48952  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
48953  if( rc==SQLITE_OK ){
48954    sqlite3_free(pCur->pKey);
48955    pCur->pKey = 0;
48956    assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
48957  }
48958  return rc;
48959}
48960
48961#define restoreCursorPosition(p) \
48962  (p->eState>=CURSOR_REQUIRESEEK ? \
48963         btreeRestoreCursorPosition(p) : \
48964         SQLITE_OK)
48965
48966/*
48967** Determine whether or not a cursor has moved from the position it
48968** was last placed at.  Cursors can move when the row they are pointing
48969** at is deleted out from under them.
48970**
48971** This routine returns an error code if something goes wrong.  The
48972** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
48973*/
48974SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
48975  int rc;
48976
48977  rc = restoreCursorPosition(pCur);
48978  if( rc ){
48979    *pHasMoved = 1;
48980    return rc;
48981  }
48982  if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
48983    *pHasMoved = 1;
48984  }else{
48985    *pHasMoved = 0;
48986  }
48987  return SQLITE_OK;
48988}
48989
48990#ifndef SQLITE_OMIT_AUTOVACUUM
48991/*
48992** Given a page number of a regular database page, return the page
48993** number for the pointer-map page that contains the entry for the
48994** input page number.
48995**
48996** Return 0 (not a valid page) for pgno==1 since there is
48997** no pointer map associated with page 1.  The integrity_check logic
48998** requires that ptrmapPageno(*,1)!=1.
48999*/
49000static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
49001  int nPagesPerMapPage;
49002  Pgno iPtrMap, ret;
49003  assert( sqlite3_mutex_held(pBt->mutex) );
49004  if( pgno<2 ) return 0;
49005  nPagesPerMapPage = (pBt->usableSize/5)+1;
49006  iPtrMap = (pgno-2)/nPagesPerMapPage;
49007  ret = (iPtrMap*nPagesPerMapPage) + 2;
49008  if( ret==PENDING_BYTE_PAGE(pBt) ){
49009    ret++;
49010  }
49011  return ret;
49012}
49013
49014/*
49015** Write an entry into the pointer map.
49016**
49017** This routine updates the pointer map entry for page number 'key'
49018** so that it maps to type 'eType' and parent page number 'pgno'.
49019**
49020** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
49021** a no-op.  If an error occurs, the appropriate error code is written
49022** into *pRC.
49023*/
49024static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
49025  DbPage *pDbPage;  /* The pointer map page */
49026  u8 *pPtrmap;      /* The pointer map data */
49027  Pgno iPtrmap;     /* The pointer map page number */
49028  int offset;       /* Offset in pointer map page */
49029  int rc;           /* Return code from subfunctions */
49030
49031  if( *pRC ) return;
49032
49033  assert( sqlite3_mutex_held(pBt->mutex) );
49034  /* The master-journal page number must never be used as a pointer map page */
49035  assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
49036
49037  assert( pBt->autoVacuum );
49038  if( key==0 ){
49039    *pRC = SQLITE_CORRUPT_BKPT;
49040    return;
49041  }
49042  iPtrmap = PTRMAP_PAGENO(pBt, key);
49043  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
49044  if( rc!=SQLITE_OK ){
49045    *pRC = rc;
49046    return;
49047  }
49048  offset = PTRMAP_PTROFFSET(iPtrmap, key);
49049  if( offset<0 ){
49050    *pRC = SQLITE_CORRUPT_BKPT;
49051    goto ptrmap_exit;
49052  }
49053  assert( offset <= (int)pBt->usableSize-5 );
49054  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
49055
49056  if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
49057    TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
49058    *pRC= rc = sqlite3PagerWrite(pDbPage);
49059    if( rc==SQLITE_OK ){
49060      pPtrmap[offset] = eType;
49061      put4byte(&pPtrmap[offset+1], parent);
49062    }
49063  }
49064
49065ptrmap_exit:
49066  sqlite3PagerUnref(pDbPage);
49067}
49068
49069/*
49070** Read an entry from the pointer map.
49071**
49072** This routine retrieves the pointer map entry for page 'key', writing
49073** the type and parent page number to *pEType and *pPgno respectively.
49074** An error code is returned if something goes wrong, otherwise SQLITE_OK.
49075*/
49076static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
49077  DbPage *pDbPage;   /* The pointer map page */
49078  int iPtrmap;       /* Pointer map page index */
49079  u8 *pPtrmap;       /* Pointer map page data */
49080  int offset;        /* Offset of entry in pointer map */
49081  int rc;
49082
49083  assert( sqlite3_mutex_held(pBt->mutex) );
49084
49085  iPtrmap = PTRMAP_PAGENO(pBt, key);
49086  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
49087  if( rc!=0 ){
49088    return rc;
49089  }
49090  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
49091
49092  offset = PTRMAP_PTROFFSET(iPtrmap, key);
49093  if( offset<0 ){
49094    sqlite3PagerUnref(pDbPage);
49095    return SQLITE_CORRUPT_BKPT;
49096  }
49097  assert( offset <= (int)pBt->usableSize-5 );
49098  assert( pEType!=0 );
49099  *pEType = pPtrmap[offset];
49100  if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
49101
49102  sqlite3PagerUnref(pDbPage);
49103  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
49104  return SQLITE_OK;
49105}
49106
49107#else /* if defined SQLITE_OMIT_AUTOVACUUM */
49108  #define ptrmapPut(w,x,y,z,rc)
49109  #define ptrmapGet(w,x,y,z) SQLITE_OK
49110  #define ptrmapPutOvflPtr(x, y, rc)
49111#endif
49112
49113/*
49114** Given a btree page and a cell index (0 means the first cell on
49115** the page, 1 means the second cell, and so forth) return a pointer
49116** to the cell content.
49117**
49118** This routine works only for pages that do not contain overflow cells.
49119*/
49120#define findCell(P,I) \
49121  ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
49122#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
49123
49124
49125/*
49126** This a more complex version of findCell() that works for
49127** pages that do contain overflow cells.
49128*/
49129static u8 *findOverflowCell(MemPage *pPage, int iCell){
49130  int i;
49131  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49132  for(i=pPage->nOverflow-1; i>=0; i--){
49133    int k;
49134    struct _OvflCell *pOvfl;
49135    pOvfl = &pPage->aOvfl[i];
49136    k = pOvfl->idx;
49137    if( k<=iCell ){
49138      if( k==iCell ){
49139        return pOvfl->pCell;
49140      }
49141      iCell--;
49142    }
49143  }
49144  return findCell(pPage, iCell);
49145}
49146
49147/*
49148** Parse a cell content block and fill in the CellInfo structure.  There
49149** are two versions of this function.  btreeParseCell() takes a
49150** cell index as the second argument and btreeParseCellPtr()
49151** takes a pointer to the body of the cell as its second argument.
49152**
49153** Within this file, the parseCell() macro can be called instead of
49154** btreeParseCellPtr(). Using some compilers, this will be faster.
49155*/
49156static void btreeParseCellPtr(
49157  MemPage *pPage,         /* Page containing the cell */
49158  u8 *pCell,              /* Pointer to the cell text. */
49159  CellInfo *pInfo         /* Fill in this structure */
49160){
49161  u16 n;                  /* Number bytes in cell content header */
49162  u32 nPayload;           /* Number of bytes of cell payload */
49163
49164  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49165
49166  pInfo->pCell = pCell;
49167  assert( pPage->leaf==0 || pPage->leaf==1 );
49168  n = pPage->childPtrSize;
49169  assert( n==4-4*pPage->leaf );
49170  if( pPage->intKey ){
49171    if( pPage->hasData ){
49172      n += getVarint32(&pCell[n], nPayload);
49173    }else{
49174      nPayload = 0;
49175    }
49176    n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
49177    pInfo->nData = nPayload;
49178  }else{
49179    pInfo->nData = 0;
49180    n += getVarint32(&pCell[n], nPayload);
49181    pInfo->nKey = nPayload;
49182  }
49183  pInfo->nPayload = nPayload;
49184  pInfo->nHeader = n;
49185  testcase( nPayload==pPage->maxLocal );
49186  testcase( nPayload==pPage->maxLocal+1 );
49187  if( likely(nPayload<=pPage->maxLocal) ){
49188    /* This is the (easy) common case where the entire payload fits
49189    ** on the local page.  No overflow is required.
49190    */
49191    if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
49192    pInfo->nLocal = (u16)nPayload;
49193    pInfo->iOverflow = 0;
49194  }else{
49195    /* If the payload will not fit completely on the local page, we have
49196    ** to decide how much to store locally and how much to spill onto
49197    ** overflow pages.  The strategy is to minimize the amount of unused
49198    ** space on overflow pages while keeping the amount of local storage
49199    ** in between minLocal and maxLocal.
49200    **
49201    ** Warning:  changing the way overflow payload is distributed in any
49202    ** way will result in an incompatible file format.
49203    */
49204    int minLocal;  /* Minimum amount of payload held locally */
49205    int maxLocal;  /* Maximum amount of payload held locally */
49206    int surplus;   /* Overflow payload available for local storage */
49207
49208    minLocal = pPage->minLocal;
49209    maxLocal = pPage->maxLocal;
49210    surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
49211    testcase( surplus==maxLocal );
49212    testcase( surplus==maxLocal+1 );
49213    if( surplus <= maxLocal ){
49214      pInfo->nLocal = (u16)surplus;
49215    }else{
49216      pInfo->nLocal = (u16)minLocal;
49217    }
49218    pInfo->iOverflow = (u16)(pInfo->nLocal + n);
49219    pInfo->nSize = pInfo->iOverflow + 4;
49220  }
49221}
49222#define parseCell(pPage, iCell, pInfo) \
49223  btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
49224static void btreeParseCell(
49225  MemPage *pPage,         /* Page containing the cell */
49226  int iCell,              /* The cell index.  First cell is 0 */
49227  CellInfo *pInfo         /* Fill in this structure */
49228){
49229  parseCell(pPage, iCell, pInfo);
49230}
49231
49232/*
49233** Compute the total number of bytes that a Cell needs in the cell
49234** data area of the btree-page.  The return number includes the cell
49235** data header and the local payload, but not any overflow page or
49236** the space used by the cell pointer.
49237*/
49238static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
49239  u8 *pIter = &pCell[pPage->childPtrSize];
49240  u32 nSize;
49241
49242#ifdef SQLITE_DEBUG
49243  /* The value returned by this function should always be the same as
49244  ** the (CellInfo.nSize) value found by doing a full parse of the
49245  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
49246  ** this function verifies that this invariant is not violated. */
49247  CellInfo debuginfo;
49248  btreeParseCellPtr(pPage, pCell, &debuginfo);
49249#endif
49250
49251  if( pPage->intKey ){
49252    u8 *pEnd;
49253    if( pPage->hasData ){
49254      pIter += getVarint32(pIter, nSize);
49255    }else{
49256      nSize = 0;
49257    }
49258
49259    /* pIter now points at the 64-bit integer key value, a variable length
49260    ** integer. The following block moves pIter to point at the first byte
49261    ** past the end of the key value. */
49262    pEnd = &pIter[9];
49263    while( (*pIter++)&0x80 && pIter<pEnd );
49264  }else{
49265    pIter += getVarint32(pIter, nSize);
49266  }
49267
49268  testcase( nSize==pPage->maxLocal );
49269  testcase( nSize==pPage->maxLocal+1 );
49270  if( nSize>pPage->maxLocal ){
49271    int minLocal = pPage->minLocal;
49272    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
49273    testcase( nSize==pPage->maxLocal );
49274    testcase( nSize==pPage->maxLocal+1 );
49275    if( nSize>pPage->maxLocal ){
49276      nSize = minLocal;
49277    }
49278    nSize += 4;
49279  }
49280  nSize += (u32)(pIter - pCell);
49281
49282  /* The minimum size of any cell is 4 bytes. */
49283  if( nSize<4 ){
49284    nSize = 4;
49285  }
49286
49287  assert( nSize==debuginfo.nSize );
49288  return (u16)nSize;
49289}
49290
49291#ifdef SQLITE_DEBUG
49292/* This variation on cellSizePtr() is used inside of assert() statements
49293** only. */
49294static u16 cellSize(MemPage *pPage, int iCell){
49295  return cellSizePtr(pPage, findCell(pPage, iCell));
49296}
49297#endif
49298
49299#ifndef SQLITE_OMIT_AUTOVACUUM
49300/*
49301** If the cell pCell, part of page pPage contains a pointer
49302** to an overflow page, insert an entry into the pointer-map
49303** for the overflow page.
49304*/
49305static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
49306  CellInfo info;
49307  if( *pRC ) return;
49308  assert( pCell!=0 );
49309  btreeParseCellPtr(pPage, pCell, &info);
49310  assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
49311  if( info.iOverflow ){
49312    Pgno ovfl = get4byte(&pCell[info.iOverflow]);
49313    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
49314  }
49315}
49316#endif
49317
49318
49319/*
49320** Defragment the page given.  All Cells are moved to the
49321** end of the page and all free space is collected into one
49322** big FreeBlk that occurs in between the header and cell
49323** pointer array and the cell content area.
49324*/
49325static int defragmentPage(MemPage *pPage){
49326  int i;                     /* Loop counter */
49327  int pc;                    /* Address of a i-th cell */
49328  int hdr;                   /* Offset to the page header */
49329  int size;                  /* Size of a cell */
49330  int usableSize;            /* Number of usable bytes on a page */
49331  int cellOffset;            /* Offset to the cell pointer array */
49332  int cbrk;                  /* Offset to the cell content area */
49333  int nCell;                 /* Number of cells on the page */
49334  unsigned char *data;       /* The page data */
49335  unsigned char *temp;       /* Temp area for cell content */
49336  int iCellFirst;            /* First allowable cell index */
49337  int iCellLast;             /* Last possible cell index */
49338
49339
49340  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49341  assert( pPage->pBt!=0 );
49342  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
49343  assert( pPage->nOverflow==0 );
49344  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49345  temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
49346  data = pPage->aData;
49347  hdr = pPage->hdrOffset;
49348  cellOffset = pPage->cellOffset;
49349  nCell = pPage->nCell;
49350  assert( nCell==get2byte(&data[hdr+3]) );
49351  usableSize = pPage->pBt->usableSize;
49352  cbrk = get2byte(&data[hdr+5]);
49353  memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
49354  cbrk = usableSize;
49355  iCellFirst = cellOffset + 2*nCell;
49356  iCellLast = usableSize - 4;
49357  for(i=0; i<nCell; i++){
49358    u8 *pAddr;     /* The i-th cell pointer */
49359    pAddr = &data[cellOffset + i*2];
49360    pc = get2byte(pAddr);
49361    testcase( pc==iCellFirst );
49362    testcase( pc==iCellLast );
49363#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
49364    /* These conditions have already been verified in btreeInitPage()
49365    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
49366    */
49367    if( pc<iCellFirst || pc>iCellLast ){
49368      return SQLITE_CORRUPT_BKPT;
49369    }
49370#endif
49371    assert( pc>=iCellFirst && pc<=iCellLast );
49372    size = cellSizePtr(pPage, &temp[pc]);
49373    cbrk -= size;
49374#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
49375    if( cbrk<iCellFirst ){
49376      return SQLITE_CORRUPT_BKPT;
49377    }
49378#else
49379    if( cbrk<iCellFirst || pc+size>usableSize ){
49380      return SQLITE_CORRUPT_BKPT;
49381    }
49382#endif
49383    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
49384    testcase( cbrk+size==usableSize );
49385    testcase( pc+size==usableSize );
49386    memcpy(&data[cbrk], &temp[pc], size);
49387    put2byte(pAddr, cbrk);
49388  }
49389  assert( cbrk>=iCellFirst );
49390  put2byte(&data[hdr+5], cbrk);
49391  data[hdr+1] = 0;
49392  data[hdr+2] = 0;
49393  data[hdr+7] = 0;
49394  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
49395  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49396  if( cbrk-iCellFirst!=pPage->nFree ){
49397    return SQLITE_CORRUPT_BKPT;
49398  }
49399  return SQLITE_OK;
49400}
49401
49402/*
49403** Allocate nByte bytes of space from within the B-Tree page passed
49404** as the first argument. Write into *pIdx the index into pPage->aData[]
49405** of the first byte of allocated space. Return either SQLITE_OK or
49406** an error code (usually SQLITE_CORRUPT).
49407**
49408** The caller guarantees that there is sufficient space to make the
49409** allocation.  This routine might need to defragment in order to bring
49410** all the space together, however.  This routine will avoid using
49411** the first two bytes past the cell pointer area since presumably this
49412** allocation is being made in order to insert a new cell, so we will
49413** also end up needing a new cell pointer.
49414*/
49415static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
49416  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
49417  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
49418  int nFrag;                           /* Number of fragmented bytes on pPage */
49419  int top;                             /* First byte of cell content area */
49420  int gap;        /* First byte of gap between cell pointers and cell content */
49421  int rc;         /* Integer return code */
49422  int usableSize; /* Usable size of the page */
49423
49424  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49425  assert( pPage->pBt );
49426  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49427  assert( nByte>=0 );  /* Minimum cell size is 4 */
49428  assert( pPage->nFree>=nByte );
49429  assert( pPage->nOverflow==0 );
49430  usableSize = pPage->pBt->usableSize;
49431  assert( nByte < usableSize-8 );
49432
49433  nFrag = data[hdr+7];
49434  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
49435  gap = pPage->cellOffset + 2*pPage->nCell;
49436  top = get2byteNotZero(&data[hdr+5]);
49437  if( gap>top ) return SQLITE_CORRUPT_BKPT;
49438  testcase( gap+2==top );
49439  testcase( gap+1==top );
49440  testcase( gap==top );
49441
49442  if( nFrag>=60 ){
49443    /* Always defragment highly fragmented pages */
49444    rc = defragmentPage(pPage);
49445    if( rc ) return rc;
49446    top = get2byteNotZero(&data[hdr+5]);
49447  }else if( gap+2<=top ){
49448    /* Search the freelist looking for a free slot big enough to satisfy
49449    ** the request. The allocation is made from the first free slot in
49450    ** the list that is large enough to accomadate it.
49451    */
49452    int pc, addr;
49453    for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
49454      int size;            /* Size of the free slot */
49455      if( pc>usableSize-4 || pc<addr+4 ){
49456        return SQLITE_CORRUPT_BKPT;
49457      }
49458      size = get2byte(&data[pc+2]);
49459      if( size>=nByte ){
49460        int x = size - nByte;
49461        testcase( x==4 );
49462        testcase( x==3 );
49463        if( x<4 ){
49464          /* Remove the slot from the free-list. Update the number of
49465          ** fragmented bytes within the page. */
49466          memcpy(&data[addr], &data[pc], 2);
49467          data[hdr+7] = (u8)(nFrag + x);
49468        }else if( size+pc > usableSize ){
49469          return SQLITE_CORRUPT_BKPT;
49470        }else{
49471          /* The slot remains on the free-list. Reduce its size to account
49472          ** for the portion used by the new allocation. */
49473          put2byte(&data[pc+2], x);
49474        }
49475        *pIdx = pc + x;
49476        return SQLITE_OK;
49477      }
49478    }
49479  }
49480
49481  /* Check to make sure there is enough space in the gap to satisfy
49482  ** the allocation.  If not, defragment.
49483  */
49484  testcase( gap+2+nByte==top );
49485  if( gap+2+nByte>top ){
49486    rc = defragmentPage(pPage);
49487    if( rc ) return rc;
49488    top = get2byteNotZero(&data[hdr+5]);
49489    assert( gap+nByte<=top );
49490  }
49491
49492
49493  /* Allocate memory from the gap in between the cell pointer array
49494  ** and the cell content area.  The btreeInitPage() call has already
49495  ** validated the freelist.  Given that the freelist is valid, there
49496  ** is no way that the allocation can extend off the end of the page.
49497  ** The assert() below verifies the previous sentence.
49498  */
49499  top -= nByte;
49500  put2byte(&data[hdr+5], top);
49501  assert( top+nByte <= (int)pPage->pBt->usableSize );
49502  *pIdx = top;
49503  return SQLITE_OK;
49504}
49505
49506/*
49507** Return a section of the pPage->aData to the freelist.
49508** The first byte of the new free block is pPage->aDisk[start]
49509** and the size of the block is "size" bytes.
49510**
49511** Most of the effort here is involved in coalesing adjacent
49512** free blocks into a single big free block.
49513*/
49514static int freeSpace(MemPage *pPage, int start, int size){
49515  int addr, pbegin, hdr;
49516  int iLast;                        /* Largest possible freeblock offset */
49517  unsigned char *data = pPage->aData;
49518
49519  assert( pPage->pBt!=0 );
49520  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49521  assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
49522  assert( (start + size) <= (int)pPage->pBt->usableSize );
49523  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49524  assert( size>=0 );   /* Minimum cell size is 4 */
49525
49526  if( pPage->pBt->secureDelete ){
49527    /* Overwrite deleted information with zeros when the secure_delete
49528    ** option is enabled */
49529    memset(&data[start], 0, size);
49530  }
49531
49532  /* Add the space back into the linked list of freeblocks.  Note that
49533  ** even though the freeblock list was checked by btreeInitPage(),
49534  ** btreeInitPage() did not detect overlapping cells or
49535  ** freeblocks that overlapped cells.   Nor does it detect when the
49536  ** cell content area exceeds the value in the page header.  If these
49537  ** situations arise, then subsequent insert operations might corrupt
49538  ** the freelist.  So we do need to check for corruption while scanning
49539  ** the freelist.
49540  */
49541  hdr = pPage->hdrOffset;
49542  addr = hdr + 1;
49543  iLast = pPage->pBt->usableSize - 4;
49544  assert( start<=iLast );
49545  while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
49546    if( pbegin<addr+4 ){
49547      return SQLITE_CORRUPT_BKPT;
49548    }
49549    addr = pbegin;
49550  }
49551  if( pbegin>iLast ){
49552    return SQLITE_CORRUPT_BKPT;
49553  }
49554  assert( pbegin>addr || pbegin==0 );
49555  put2byte(&data[addr], start);
49556  put2byte(&data[start], pbegin);
49557  put2byte(&data[start+2], size);
49558  pPage->nFree = pPage->nFree + (u16)size;
49559
49560  /* Coalesce adjacent free blocks */
49561  addr = hdr + 1;
49562  while( (pbegin = get2byte(&data[addr]))>0 ){
49563    int pnext, psize, x;
49564    assert( pbegin>addr );
49565    assert( pbegin <= (int)pPage->pBt->usableSize-4 );
49566    pnext = get2byte(&data[pbegin]);
49567    psize = get2byte(&data[pbegin+2]);
49568    if( pbegin + psize + 3 >= pnext && pnext>0 ){
49569      int frag = pnext - (pbegin+psize);
49570      if( (frag<0) || (frag>(int)data[hdr+7]) ){
49571        return SQLITE_CORRUPT_BKPT;
49572      }
49573      data[hdr+7] -= (u8)frag;
49574      x = get2byte(&data[pnext]);
49575      put2byte(&data[pbegin], x);
49576      x = pnext + get2byte(&data[pnext+2]) - pbegin;
49577      put2byte(&data[pbegin+2], x);
49578    }else{
49579      addr = pbegin;
49580    }
49581  }
49582
49583  /* If the cell content area begins with a freeblock, remove it. */
49584  if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
49585    int top;
49586    pbegin = get2byte(&data[hdr+1]);
49587    memcpy(&data[hdr+1], &data[pbegin], 2);
49588    top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
49589    put2byte(&data[hdr+5], top);
49590  }
49591  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49592  return SQLITE_OK;
49593}
49594
49595/*
49596** Decode the flags byte (the first byte of the header) for a page
49597** and initialize fields of the MemPage structure accordingly.
49598**
49599** Only the following combinations are supported.  Anything different
49600** indicates a corrupt database files:
49601**
49602**         PTF_ZERODATA
49603**         PTF_ZERODATA | PTF_LEAF
49604**         PTF_LEAFDATA | PTF_INTKEY
49605**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
49606*/
49607static int decodeFlags(MemPage *pPage, int flagByte){
49608  BtShared *pBt;     /* A copy of pPage->pBt */
49609
49610  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
49611  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49612  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
49613  flagByte &= ~PTF_LEAF;
49614  pPage->childPtrSize = 4-4*pPage->leaf;
49615  pBt = pPage->pBt;
49616  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
49617    pPage->intKey = 1;
49618    pPage->hasData = pPage->leaf;
49619    pPage->maxLocal = pBt->maxLeaf;
49620    pPage->minLocal = pBt->minLeaf;
49621  }else if( flagByte==PTF_ZERODATA ){
49622    pPage->intKey = 0;
49623    pPage->hasData = 0;
49624    pPage->maxLocal = pBt->maxLocal;
49625    pPage->minLocal = pBt->minLocal;
49626  }else{
49627    return SQLITE_CORRUPT_BKPT;
49628  }
49629  return SQLITE_OK;
49630}
49631
49632/*
49633** Initialize the auxiliary information for a disk block.
49634**
49635** Return SQLITE_OK on success.  If we see that the page does
49636** not contain a well-formed database page, then return
49637** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
49638** guarantee that the page is well-formed.  It only shows that
49639** we failed to detect any corruption.
49640*/
49641static int btreeInitPage(MemPage *pPage){
49642
49643  assert( pPage->pBt!=0 );
49644  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49645  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
49646  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
49647  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
49648
49649  if( !pPage->isInit ){
49650    u16 pc;            /* Address of a freeblock within pPage->aData[] */
49651    u8 hdr;            /* Offset to beginning of page header */
49652    u8 *data;          /* Equal to pPage->aData */
49653    BtShared *pBt;        /* The main btree structure */
49654    int usableSize;    /* Amount of usable space on each page */
49655    u16 cellOffset;    /* Offset from start of page to first cell pointer */
49656    int nFree;         /* Number of unused bytes on the page */
49657    int top;           /* First byte of the cell content area */
49658    int iCellFirst;    /* First allowable cell or freeblock offset */
49659    int iCellLast;     /* Last possible cell or freeblock offset */
49660
49661    pBt = pPage->pBt;
49662
49663    hdr = pPage->hdrOffset;
49664    data = pPage->aData;
49665    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
49666    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
49667    pPage->maskPage = (u16)(pBt->pageSize - 1);
49668    pPage->nOverflow = 0;
49669    usableSize = pBt->usableSize;
49670    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
49671    top = get2byteNotZero(&data[hdr+5]);
49672    pPage->nCell = get2byte(&data[hdr+3]);
49673    if( pPage->nCell>MX_CELL(pBt) ){
49674      /* To many cells for a single page.  The page must be corrupt */
49675      return SQLITE_CORRUPT_BKPT;
49676    }
49677    testcase( pPage->nCell==MX_CELL(pBt) );
49678
49679    /* A malformed database page might cause us to read past the end
49680    ** of page when parsing a cell.
49681    **
49682    ** The following block of code checks early to see if a cell extends
49683    ** past the end of a page boundary and causes SQLITE_CORRUPT to be
49684    ** returned if it does.
49685    */
49686    iCellFirst = cellOffset + 2*pPage->nCell;
49687    iCellLast = usableSize - 4;
49688#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
49689    {
49690      int i;            /* Index into the cell pointer array */
49691      int sz;           /* Size of a cell */
49692
49693      if( !pPage->leaf ) iCellLast--;
49694      for(i=0; i<pPage->nCell; i++){
49695        pc = get2byte(&data[cellOffset+i*2]);
49696        testcase( pc==iCellFirst );
49697        testcase( pc==iCellLast );
49698        if( pc<iCellFirst || pc>iCellLast ){
49699          return SQLITE_CORRUPT_BKPT;
49700        }
49701        sz = cellSizePtr(pPage, &data[pc]);
49702        testcase( pc+sz==usableSize );
49703        if( pc+sz>usableSize ){
49704          return SQLITE_CORRUPT_BKPT;
49705        }
49706      }
49707      if( !pPage->leaf ) iCellLast++;
49708    }
49709#endif
49710
49711    /* Compute the total free space on the page */
49712    pc = get2byte(&data[hdr+1]);
49713    nFree = data[hdr+7] + top;
49714    while( pc>0 ){
49715      u16 next, size;
49716      if( pc<iCellFirst || pc>iCellLast ){
49717        /* Start of free block is off the page */
49718        return SQLITE_CORRUPT_BKPT;
49719      }
49720      next = get2byte(&data[pc]);
49721      size = get2byte(&data[pc+2]);
49722      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
49723        /* Free blocks must be in ascending order. And the last byte of
49724	** the free-block must lie on the database page.  */
49725        return SQLITE_CORRUPT_BKPT;
49726      }
49727      nFree = nFree + size;
49728      pc = next;
49729    }
49730
49731    /* At this point, nFree contains the sum of the offset to the start
49732    ** of the cell-content area plus the number of free bytes within
49733    ** the cell-content area. If this is greater than the usable-size
49734    ** of the page, then the page must be corrupted. This check also
49735    ** serves to verify that the offset to the start of the cell-content
49736    ** area, according to the page header, lies within the page.
49737    */
49738    if( nFree>usableSize ){
49739      return SQLITE_CORRUPT_BKPT;
49740    }
49741    pPage->nFree = (u16)(nFree - iCellFirst);
49742    pPage->isInit = 1;
49743  }
49744  return SQLITE_OK;
49745}
49746
49747/*
49748** Set up a raw page so that it looks like a database page holding
49749** no entries.
49750*/
49751static void zeroPage(MemPage *pPage, int flags){
49752  unsigned char *data = pPage->aData;
49753  BtShared *pBt = pPage->pBt;
49754  u8 hdr = pPage->hdrOffset;
49755  u16 first;
49756
49757  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
49758  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
49759  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
49760  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49761  assert( sqlite3_mutex_held(pBt->mutex) );
49762  if( pBt->secureDelete ){
49763    memset(&data[hdr], 0, pBt->usableSize - hdr);
49764  }
49765  data[hdr] = (char)flags;
49766  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
49767  memset(&data[hdr+1], 0, 4);
49768  data[hdr+7] = 0;
49769  put2byte(&data[hdr+5], pBt->usableSize);
49770  pPage->nFree = (u16)(pBt->usableSize - first);
49771  decodeFlags(pPage, flags);
49772  pPage->hdrOffset = hdr;
49773  pPage->cellOffset = first;
49774  pPage->nOverflow = 0;
49775  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
49776  pPage->maskPage = (u16)(pBt->pageSize - 1);
49777  pPage->nCell = 0;
49778  pPage->isInit = 1;
49779}
49780
49781
49782/*
49783** Convert a DbPage obtained from the pager into a MemPage used by
49784** the btree layer.
49785*/
49786static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
49787  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
49788  pPage->aData = sqlite3PagerGetData(pDbPage);
49789  pPage->pDbPage = pDbPage;
49790  pPage->pBt = pBt;
49791  pPage->pgno = pgno;
49792  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
49793  return pPage;
49794}
49795
49796/*
49797** Get a page from the pager.  Initialize the MemPage.pBt and
49798** MemPage.aData elements if needed.
49799**
49800** If the noContent flag is set, it means that we do not care about
49801** the content of the page at this time.  So do not go to the disk
49802** to fetch the content.  Just fill in the content with zeros for now.
49803** If in the future we call sqlite3PagerWrite() on this page, that
49804** means we have started to be concerned about content and the disk
49805** read should occur at that point.
49806*/
49807static int btreeGetPage(
49808  BtShared *pBt,       /* The btree */
49809  Pgno pgno,           /* Number of the page to fetch */
49810  MemPage **ppPage,    /* Return the page in this parameter */
49811  int noContent        /* Do not load page content if true */
49812){
49813  int rc;
49814  DbPage *pDbPage;
49815
49816  assert( sqlite3_mutex_held(pBt->mutex) );
49817  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
49818  if( rc ) return rc;
49819  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
49820  return SQLITE_OK;
49821}
49822
49823/*
49824** Retrieve a page from the pager cache. If the requested page is not
49825** already in the pager cache return NULL. Initialize the MemPage.pBt and
49826** MemPage.aData elements if needed.
49827*/
49828static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
49829  DbPage *pDbPage;
49830  assert( sqlite3_mutex_held(pBt->mutex) );
49831  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
49832  if( pDbPage ){
49833    return btreePageFromDbPage(pDbPage, pgno, pBt);
49834  }
49835  return 0;
49836}
49837
49838/*
49839** Return the size of the database file in pages. If there is any kind of
49840** error, return ((unsigned int)-1).
49841*/
49842static Pgno btreePagecount(BtShared *pBt){
49843  return pBt->nPage;
49844}
49845SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
49846  assert( sqlite3BtreeHoldsMutex(p) );
49847  assert( ((p->pBt->nPage)&0x8000000)==0 );
49848  return (int)btreePagecount(p->pBt);
49849}
49850
49851/*
49852** Get a page from the pager and initialize it.  This routine is just a
49853** convenience wrapper around separate calls to btreeGetPage() and
49854** btreeInitPage().
49855**
49856** If an error occurs, then the value *ppPage is set to is undefined. It
49857** may remain unchanged, or it may be set to an invalid value.
49858*/
49859static int getAndInitPage(
49860  BtShared *pBt,          /* The database file */
49861  Pgno pgno,           /* Number of the page to get */
49862  MemPage **ppPage     /* Write the page pointer here */
49863){
49864  int rc;
49865  assert( sqlite3_mutex_held(pBt->mutex) );
49866
49867  if( pgno>btreePagecount(pBt) ){
49868    rc = SQLITE_CORRUPT_BKPT;
49869  }else{
49870    rc = btreeGetPage(pBt, pgno, ppPage, 0);
49871    if( rc==SQLITE_OK ){
49872      rc = btreeInitPage(*ppPage);
49873      if( rc!=SQLITE_OK ){
49874        releasePage(*ppPage);
49875      }
49876    }
49877  }
49878
49879  testcase( pgno==0 );
49880  assert( pgno!=0 || rc==SQLITE_CORRUPT );
49881  return rc;
49882}
49883
49884/*
49885** Release a MemPage.  This should be called once for each prior
49886** call to btreeGetPage.
49887*/
49888static void releasePage(MemPage *pPage){
49889  if( pPage ){
49890    assert( pPage->aData );
49891    assert( pPage->pBt );
49892    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
49893    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
49894    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49895    sqlite3PagerUnref(pPage->pDbPage);
49896  }
49897}
49898
49899/*
49900** During a rollback, when the pager reloads information into the cache
49901** so that the cache is restored to its original state at the start of
49902** the transaction, for each page restored this routine is called.
49903**
49904** This routine needs to reset the extra data section at the end of the
49905** page to agree with the restored data.
49906*/
49907static void pageReinit(DbPage *pData){
49908  MemPage *pPage;
49909  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
49910  assert( sqlite3PagerPageRefcount(pData)>0 );
49911  if( pPage->isInit ){
49912    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49913    pPage->isInit = 0;
49914    if( sqlite3PagerPageRefcount(pData)>1 ){
49915      /* pPage might not be a btree page;  it might be an overflow page
49916      ** or ptrmap page or a free page.  In those cases, the following
49917      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
49918      ** But no harm is done by this.  And it is very important that
49919      ** btreeInitPage() be called on every btree page so we make
49920      ** the call for every page that comes in for re-initing. */
49921      btreeInitPage(pPage);
49922    }
49923  }
49924}
49925
49926/*
49927** Invoke the busy handler for a btree.
49928*/
49929static int btreeInvokeBusyHandler(void *pArg){
49930  BtShared *pBt = (BtShared*)pArg;
49931  assert( pBt->db );
49932  assert( sqlite3_mutex_held(pBt->db->mutex) );
49933  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
49934}
49935
49936/*
49937** Open a database file.
49938**
49939** zFilename is the name of the database file.  If zFilename is NULL
49940** then an ephemeral database is created.  The ephemeral database might
49941** be exclusively in memory, or it might use a disk-based memory cache.
49942** Either way, the ephemeral database will be automatically deleted
49943** when sqlite3BtreeClose() is called.
49944**
49945** If zFilename is ":memory:" then an in-memory database is created
49946** that is automatically destroyed when it is closed.
49947**
49948** The "flags" parameter is a bitmask that might contain bits
49949** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK.  The BTREE_NO_READLOCK
49950** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
49951** These flags are passed through into sqlite3PagerOpen() and must
49952** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
49953**
49954** If the database is already opened in the same database connection
49955** and we are in shared cache mode, then the open will fail with an
49956** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
49957** objects in the same database connection since doing so will lead
49958** to problems with locking.
49959*/
49960SQLITE_PRIVATE int sqlite3BtreeOpen(
49961  sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
49962  const char *zFilename,  /* Name of the file containing the BTree database */
49963  sqlite3 *db,            /* Associated database handle */
49964  Btree **ppBtree,        /* Pointer to new Btree object written here */
49965  int flags,              /* Options */
49966  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
49967){
49968  BtShared *pBt = 0;             /* Shared part of btree structure */
49969  Btree *p;                      /* Handle to return */
49970  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
49971  int rc = SQLITE_OK;            /* Result code from this function */
49972  u8 nReserve;                   /* Byte of unused space on each page */
49973  unsigned char zDbHeader[100];  /* Database header content */
49974
49975  /* True if opening an ephemeral, temporary database */
49976  const int isTempDb = zFilename==0 || zFilename[0]==0;
49977
49978  /* Set the variable isMemdb to true for an in-memory database, or
49979  ** false for a file-based database.
49980  */
49981#ifdef SQLITE_OMIT_MEMORYDB
49982  const int isMemdb = 0;
49983#else
49984  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
49985                       || (isTempDb && sqlite3TempInMemory(db));
49986#endif
49987
49988  assert( db!=0 );
49989  assert( pVfs!=0 );
49990  assert( sqlite3_mutex_held(db->mutex) );
49991  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
49992
49993  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
49994  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
49995
49996  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
49997  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
49998
49999  if( db->flags & SQLITE_NoReadlock ){
50000    flags |= BTREE_NO_READLOCK;
50001  }
50002  if( isMemdb ){
50003    flags |= BTREE_MEMORY;
50004  }
50005  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
50006    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
50007  }
50008  p = sqlite3MallocZero(sizeof(Btree));
50009  if( !p ){
50010    return SQLITE_NOMEM;
50011  }
50012  p->inTrans = TRANS_NONE;
50013  p->db = db;
50014#ifndef SQLITE_OMIT_SHARED_CACHE
50015  p->lock.pBtree = p;
50016  p->lock.iTable = 1;
50017#endif
50018
50019#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
50020  /*
50021  ** If this Btree is a candidate for shared cache, try to find an
50022  ** existing BtShared object that we can share with
50023  */
50024  if( isMemdb==0 && isTempDb==0 ){
50025    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
50026      int nFullPathname = pVfs->mxPathname+1;
50027      char *zFullPathname = sqlite3Malloc(nFullPathname);
50028      sqlite3_mutex *mutexShared;
50029      p->sharable = 1;
50030      if( !zFullPathname ){
50031        sqlite3_free(p);
50032        return SQLITE_NOMEM;
50033      }
50034      sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
50035      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
50036      sqlite3_mutex_enter(mutexOpen);
50037      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50038      sqlite3_mutex_enter(mutexShared);
50039      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
50040        assert( pBt->nRef>0 );
50041        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
50042                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
50043          int iDb;
50044          for(iDb=db->nDb-1; iDb>=0; iDb--){
50045            Btree *pExisting = db->aDb[iDb].pBt;
50046            if( pExisting && pExisting->pBt==pBt ){
50047              sqlite3_mutex_leave(mutexShared);
50048              sqlite3_mutex_leave(mutexOpen);
50049              sqlite3_free(zFullPathname);
50050              sqlite3_free(p);
50051              return SQLITE_CONSTRAINT;
50052            }
50053          }
50054          p->pBt = pBt;
50055          pBt->nRef++;
50056          break;
50057        }
50058      }
50059      sqlite3_mutex_leave(mutexShared);
50060      sqlite3_free(zFullPathname);
50061    }
50062#ifdef SQLITE_DEBUG
50063    else{
50064      /* In debug mode, we mark all persistent databases as sharable
50065      ** even when they are not.  This exercises the locking code and
50066      ** gives more opportunity for asserts(sqlite3_mutex_held())
50067      ** statements to find locking problems.
50068      */
50069      p->sharable = 1;
50070    }
50071#endif
50072  }
50073#endif
50074  if( pBt==0 ){
50075    /*
50076    ** The following asserts make sure that structures used by the btree are
50077    ** the right size.  This is to guard against size changes that result
50078    ** when compiling on a different architecture.
50079    */
50080    assert( sizeof(i64)==8 || sizeof(i64)==4 );
50081    assert( sizeof(u64)==8 || sizeof(u64)==4 );
50082    assert( sizeof(u32)==4 );
50083    assert( sizeof(u16)==2 );
50084    assert( sizeof(Pgno)==4 );
50085
50086    pBt = sqlite3MallocZero( sizeof(*pBt) );
50087    if( pBt==0 ){
50088      rc = SQLITE_NOMEM;
50089      goto btree_open_out;
50090    }
50091    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
50092                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
50093    if( rc==SQLITE_OK ){
50094      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
50095    }
50096    if( rc!=SQLITE_OK ){
50097      goto btree_open_out;
50098    }
50099    pBt->openFlags = (u8)flags;
50100    pBt->db = db;
50101    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
50102    p->pBt = pBt;
50103
50104    pBt->pCursor = 0;
50105    pBt->pPage1 = 0;
50106    pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
50107#ifdef SQLITE_SECURE_DELETE
50108    pBt->secureDelete = 1;
50109#endif
50110    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
50111    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
50112         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
50113      pBt->pageSize = 0;
50114#ifndef SQLITE_OMIT_AUTOVACUUM
50115      /* If the magic name ":memory:" will create an in-memory database, then
50116      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
50117      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
50118      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
50119      ** regular file-name. In this case the auto-vacuum applies as per normal.
50120      */
50121      if( zFilename && !isMemdb ){
50122        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
50123        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
50124      }
50125#endif
50126      nReserve = 0;
50127    }else{
50128      nReserve = zDbHeader[20];
50129      pBt->pageSizeFixed = 1;
50130#ifndef SQLITE_OMIT_AUTOVACUUM
50131      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
50132      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
50133#endif
50134    }
50135    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
50136    if( rc ) goto btree_open_out;
50137    pBt->usableSize = pBt->pageSize - nReserve;
50138    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
50139
50140#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
50141    /* Add the new BtShared object to the linked list sharable BtShareds.
50142    */
50143    if( p->sharable ){
50144      sqlite3_mutex *mutexShared;
50145      pBt->nRef = 1;
50146      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50147      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
50148        pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
50149        if( pBt->mutex==0 ){
50150          rc = SQLITE_NOMEM;
50151          db->mallocFailed = 0;
50152          goto btree_open_out;
50153        }
50154      }
50155      sqlite3_mutex_enter(mutexShared);
50156      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
50157      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
50158      sqlite3_mutex_leave(mutexShared);
50159    }
50160#endif
50161  }
50162
50163#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
50164  /* If the new Btree uses a sharable pBtShared, then link the new
50165  ** Btree into the list of all sharable Btrees for the same connection.
50166  ** The list is kept in ascending order by pBt address.
50167  */
50168  if( p->sharable ){
50169    int i;
50170    Btree *pSib;
50171    for(i=0; i<db->nDb; i++){
50172      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
50173        while( pSib->pPrev ){ pSib = pSib->pPrev; }
50174        if( p->pBt<pSib->pBt ){
50175          p->pNext = pSib;
50176          p->pPrev = 0;
50177          pSib->pPrev = p;
50178        }else{
50179          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
50180            pSib = pSib->pNext;
50181          }
50182          p->pNext = pSib->pNext;
50183          p->pPrev = pSib;
50184          if( p->pNext ){
50185            p->pNext->pPrev = p;
50186          }
50187          pSib->pNext = p;
50188        }
50189        break;
50190      }
50191    }
50192  }
50193#endif
50194  *ppBtree = p;
50195
50196btree_open_out:
50197  if( rc!=SQLITE_OK ){
50198    if( pBt && pBt->pPager ){
50199      sqlite3PagerClose(pBt->pPager);
50200    }
50201    sqlite3_free(pBt);
50202    sqlite3_free(p);
50203    *ppBtree = 0;
50204  }else{
50205    /* If the B-Tree was successfully opened, set the pager-cache size to the
50206    ** default value. Except, when opening on an existing shared pager-cache,
50207    ** do not change the pager-cache size.
50208    */
50209    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
50210      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
50211    }
50212  }
50213  if( mutexOpen ){
50214    assert( sqlite3_mutex_held(mutexOpen) );
50215    sqlite3_mutex_leave(mutexOpen);
50216  }
50217  return rc;
50218}
50219
50220/*
50221** Decrement the BtShared.nRef counter.  When it reaches zero,
50222** remove the BtShared structure from the sharing list.  Return
50223** true if the BtShared.nRef counter reaches zero and return
50224** false if it is still positive.
50225*/
50226static int removeFromSharingList(BtShared *pBt){
50227#ifndef SQLITE_OMIT_SHARED_CACHE
50228  sqlite3_mutex *pMaster;
50229  BtShared *pList;
50230  int removed = 0;
50231
50232  assert( sqlite3_mutex_notheld(pBt->mutex) );
50233  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50234  sqlite3_mutex_enter(pMaster);
50235  pBt->nRef--;
50236  if( pBt->nRef<=0 ){
50237    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
50238      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
50239    }else{
50240      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
50241      while( ALWAYS(pList) && pList->pNext!=pBt ){
50242        pList=pList->pNext;
50243      }
50244      if( ALWAYS(pList) ){
50245        pList->pNext = pBt->pNext;
50246      }
50247    }
50248    if( SQLITE_THREADSAFE ){
50249      sqlite3_mutex_free(pBt->mutex);
50250    }
50251    removed = 1;
50252  }
50253  sqlite3_mutex_leave(pMaster);
50254  return removed;
50255#else
50256  return 1;
50257#endif
50258}
50259
50260/*
50261** Make sure pBt->pTmpSpace points to an allocation of
50262** MX_CELL_SIZE(pBt) bytes.
50263*/
50264static void allocateTempSpace(BtShared *pBt){
50265  if( !pBt->pTmpSpace ){
50266    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
50267  }
50268}
50269
50270/*
50271** Free the pBt->pTmpSpace allocation
50272*/
50273static void freeTempSpace(BtShared *pBt){
50274  sqlite3PageFree( pBt->pTmpSpace);
50275  pBt->pTmpSpace = 0;
50276}
50277
50278/*
50279** Close an open database and invalidate all cursors.
50280*/
50281SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
50282  BtShared *pBt = p->pBt;
50283  BtCursor *pCur;
50284
50285  /* Close all cursors opened via this handle.  */
50286  assert( sqlite3_mutex_held(p->db->mutex) );
50287  sqlite3BtreeEnter(p);
50288  pCur = pBt->pCursor;
50289  while( pCur ){
50290    BtCursor *pTmp = pCur;
50291    pCur = pCur->pNext;
50292    if( pTmp->pBtree==p ){
50293      sqlite3BtreeCloseCursor(pTmp);
50294    }
50295  }
50296
50297  /* Rollback any active transaction and free the handle structure.
50298  ** The call to sqlite3BtreeRollback() drops any table-locks held by
50299  ** this handle.
50300  */
50301  sqlite3BtreeRollback(p);
50302  sqlite3BtreeLeave(p);
50303
50304  /* If there are still other outstanding references to the shared-btree
50305  ** structure, return now. The remainder of this procedure cleans
50306  ** up the shared-btree.
50307  */
50308  assert( p->wantToLock==0 && p->locked==0 );
50309  if( !p->sharable || removeFromSharingList(pBt) ){
50310    /* The pBt is no longer on the sharing list, so we can access
50311    ** it without having to hold the mutex.
50312    **
50313    ** Clean out and delete the BtShared object.
50314    */
50315    assert( !pBt->pCursor );
50316    sqlite3PagerClose(pBt->pPager);
50317    if( pBt->xFreeSchema && pBt->pSchema ){
50318      pBt->xFreeSchema(pBt->pSchema);
50319    }
50320    sqlite3DbFree(0, pBt->pSchema);
50321    freeTempSpace(pBt);
50322    sqlite3_free(pBt);
50323  }
50324
50325#ifndef SQLITE_OMIT_SHARED_CACHE
50326  assert( p->wantToLock==0 );
50327  assert( p->locked==0 );
50328  if( p->pPrev ) p->pPrev->pNext = p->pNext;
50329  if( p->pNext ) p->pNext->pPrev = p->pPrev;
50330#endif
50331
50332  sqlite3_free(p);
50333  return SQLITE_OK;
50334}
50335
50336/*
50337** Change the limit on the number of pages allowed in the cache.
50338**
50339** The maximum number of cache pages is set to the absolute
50340** value of mxPage.  If mxPage is negative, the pager will
50341** operate asynchronously - it will not stop to do fsync()s
50342** to insure data is written to the disk surface before
50343** continuing.  Transactions still work if synchronous is off,
50344** and the database cannot be corrupted if this program
50345** crashes.  But if the operating system crashes or there is
50346** an abrupt power failure when synchronous is off, the database
50347** could be left in an inconsistent and unrecoverable state.
50348** Synchronous is on by default so database corruption is not
50349** normally a worry.
50350*/
50351SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
50352  BtShared *pBt = p->pBt;
50353  assert( sqlite3_mutex_held(p->db->mutex) );
50354  sqlite3BtreeEnter(p);
50355  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
50356  sqlite3BtreeLeave(p);
50357  return SQLITE_OK;
50358}
50359
50360/*
50361** Change the way data is synced to disk in order to increase or decrease
50362** how well the database resists damage due to OS crashes and power
50363** failures.  Level 1 is the same as asynchronous (no syncs() occur and
50364** there is a high probability of damage)  Level 2 is the default.  There
50365** is a very low but non-zero probability of damage.  Level 3 reduces the
50366** probability of damage to near zero but with a write performance reduction.
50367*/
50368#ifndef SQLITE_OMIT_PAGER_PRAGMAS
50369SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
50370  Btree *p,              /* The btree to set the safety level on */
50371  int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
50372  int fullSync,          /* PRAGMA fullfsync. */
50373  int ckptFullSync       /* PRAGMA checkpoint_fullfync */
50374){
50375  BtShared *pBt = p->pBt;
50376  assert( sqlite3_mutex_held(p->db->mutex) );
50377  assert( level>=1 && level<=3 );
50378  sqlite3BtreeEnter(p);
50379  sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
50380  sqlite3BtreeLeave(p);
50381  return SQLITE_OK;
50382}
50383#endif
50384
50385/*
50386** Return TRUE if the given btree is set to safety level 1.  In other
50387** words, return TRUE if no sync() occurs on the disk files.
50388*/
50389SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
50390  BtShared *pBt = p->pBt;
50391  int rc;
50392  assert( sqlite3_mutex_held(p->db->mutex) );
50393  sqlite3BtreeEnter(p);
50394  assert( pBt && pBt->pPager );
50395  rc = sqlite3PagerNosync(pBt->pPager);
50396  sqlite3BtreeLeave(p);
50397  return rc;
50398}
50399
50400/*
50401** Change the default pages size and the number of reserved bytes per page.
50402** Or, if the page size has already been fixed, return SQLITE_READONLY
50403** without changing anything.
50404**
50405** The page size must be a power of 2 between 512 and 65536.  If the page
50406** size supplied does not meet this constraint then the page size is not
50407** changed.
50408**
50409** Page sizes are constrained to be a power of two so that the region
50410** of the database file used for locking (beginning at PENDING_BYTE,
50411** the first byte past the 1GB boundary, 0x40000000) needs to occur
50412** at the beginning of a page.
50413**
50414** If parameter nReserve is less than zero, then the number of reserved
50415** bytes per page is left unchanged.
50416**
50417** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
50418** and autovacuum mode can no longer be changed.
50419*/
50420SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
50421  int rc = SQLITE_OK;
50422  BtShared *pBt = p->pBt;
50423  assert( nReserve>=-1 && nReserve<=255 );
50424  sqlite3BtreeEnter(p);
50425  if( pBt->pageSizeFixed ){
50426    sqlite3BtreeLeave(p);
50427    return SQLITE_READONLY;
50428  }
50429  if( nReserve<0 ){
50430    nReserve = pBt->pageSize - pBt->usableSize;
50431  }
50432  assert( nReserve>=0 && nReserve<=255 );
50433  if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
50434        ((pageSize-1)&pageSize)==0 ){
50435    assert( (pageSize & 7)==0 );
50436    assert( !pBt->pPage1 && !pBt->pCursor );
50437    pBt->pageSize = (u32)pageSize;
50438    freeTempSpace(pBt);
50439  }
50440  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
50441  pBt->usableSize = pBt->pageSize - (u16)nReserve;
50442  if( iFix ) pBt->pageSizeFixed = 1;
50443  sqlite3BtreeLeave(p);
50444  return rc;
50445}
50446
50447/*
50448** Return the currently defined page size
50449*/
50450SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
50451  return p->pBt->pageSize;
50452}
50453
50454#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
50455/*
50456** Return the number of bytes of space at the end of every page that
50457** are intentually left unused.  This is the "reserved" space that is
50458** sometimes used by extensions.
50459*/
50460SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
50461  int n;
50462  sqlite3BtreeEnter(p);
50463  n = p->pBt->pageSize - p->pBt->usableSize;
50464  sqlite3BtreeLeave(p);
50465  return n;
50466}
50467
50468/*
50469** Set the maximum page count for a database if mxPage is positive.
50470** No changes are made if mxPage is 0 or negative.
50471** Regardless of the value of mxPage, return the maximum page count.
50472*/
50473SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
50474  int n;
50475  sqlite3BtreeEnter(p);
50476  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
50477  sqlite3BtreeLeave(p);
50478  return n;
50479}
50480
50481/*
50482** Set the secureDelete flag if newFlag is 0 or 1.  If newFlag is -1,
50483** then make no changes.  Always return the value of the secureDelete
50484** setting after the change.
50485*/
50486SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
50487  int b;
50488  if( p==0 ) return 0;
50489  sqlite3BtreeEnter(p);
50490  if( newFlag>=0 ){
50491    p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
50492  }
50493  b = p->pBt->secureDelete;
50494  sqlite3BtreeLeave(p);
50495  return b;
50496}
50497#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
50498
50499/*
50500** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
50501** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
50502** is disabled. The default value for the auto-vacuum property is
50503** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
50504*/
50505SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
50506#ifdef SQLITE_OMIT_AUTOVACUUM
50507  return SQLITE_READONLY;
50508#else
50509  BtShared *pBt = p->pBt;
50510  int rc = SQLITE_OK;
50511  u8 av = (u8)autoVacuum;
50512
50513  sqlite3BtreeEnter(p);
50514  if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
50515    rc = SQLITE_READONLY;
50516  }else{
50517    pBt->autoVacuum = av ?1:0;
50518    pBt->incrVacuum = av==2 ?1:0;
50519  }
50520  sqlite3BtreeLeave(p);
50521  return rc;
50522#endif
50523}
50524
50525/*
50526** Return the value of the 'auto-vacuum' property. If auto-vacuum is
50527** enabled 1 is returned. Otherwise 0.
50528*/
50529SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
50530#ifdef SQLITE_OMIT_AUTOVACUUM
50531  return BTREE_AUTOVACUUM_NONE;
50532#else
50533  int rc;
50534  sqlite3BtreeEnter(p);
50535  rc = (
50536    (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
50537    (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
50538    BTREE_AUTOVACUUM_INCR
50539  );
50540  sqlite3BtreeLeave(p);
50541  return rc;
50542#endif
50543}
50544
50545
50546/*
50547** Get a reference to pPage1 of the database file.  This will
50548** also acquire a readlock on that file.
50549**
50550** SQLITE_OK is returned on success.  If the file is not a
50551** well-formed database file, then SQLITE_CORRUPT is returned.
50552** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
50553** is returned if we run out of memory.
50554*/
50555static int lockBtree(BtShared *pBt){
50556  int rc;              /* Result code from subfunctions */
50557  MemPage *pPage1;     /* Page 1 of the database file */
50558  int nPage;           /* Number of pages in the database */
50559  int nPageFile = 0;   /* Number of pages in the database file */
50560  int nPageHeader;     /* Number of pages in the database according to hdr */
50561
50562  assert( sqlite3_mutex_held(pBt->mutex) );
50563  assert( pBt->pPage1==0 );
50564  rc = sqlite3PagerSharedLock(pBt->pPager);
50565  if( rc!=SQLITE_OK ) return rc;
50566  rc = btreeGetPage(pBt, 1, &pPage1, 0);
50567  if( rc!=SQLITE_OK ) return rc;
50568
50569  /* Do some checking to help insure the file we opened really is
50570  ** a valid database file.
50571  */
50572  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
50573  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
50574  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
50575    nPage = nPageFile;
50576  }
50577  if( nPage>0 ){
50578    u32 pageSize;
50579    u32 usableSize;
50580    u8 *page1 = pPage1->aData;
50581    rc = SQLITE_NOTADB;
50582    if( memcmp(page1, zMagicHeader, 16)!=0 ){
50583      goto page1_init_failed;
50584    }
50585
50586#ifdef SQLITE_OMIT_WAL
50587    if( page1[18]>1 ){
50588      pBt->readOnly = 1;
50589    }
50590    if( page1[19]>1 ){
50591      goto page1_init_failed;
50592    }
50593#else
50594    if( page1[18]>2 ){
50595      pBt->readOnly = 1;
50596    }
50597    if( page1[19]>2 ){
50598      goto page1_init_failed;
50599    }
50600
50601    /* If the write version is set to 2, this database should be accessed
50602    ** in WAL mode. If the log is not already open, open it now. Then
50603    ** return SQLITE_OK and return without populating BtShared.pPage1.
50604    ** The caller detects this and calls this function again. This is
50605    ** required as the version of page 1 currently in the page1 buffer
50606    ** may not be the latest version - there may be a newer one in the log
50607    ** file.
50608    */
50609    if( page1[19]==2 && pBt->doNotUseWAL==0 ){
50610      int isOpen = 0;
50611      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
50612      if( rc!=SQLITE_OK ){
50613        goto page1_init_failed;
50614      }else if( isOpen==0 ){
50615        releasePage(pPage1);
50616        return SQLITE_OK;
50617      }
50618      rc = SQLITE_NOTADB;
50619    }
50620#endif
50621
50622    /* The maximum embedded fraction must be exactly 25%.  And the minimum
50623    ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
50624    ** The original design allowed these amounts to vary, but as of
50625    ** version 3.6.0, we require them to be fixed.
50626    */
50627    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
50628      goto page1_init_failed;
50629    }
50630    pageSize = (page1[16]<<8) | (page1[17]<<16);
50631    if( ((pageSize-1)&pageSize)!=0
50632     || pageSize>SQLITE_MAX_PAGE_SIZE
50633     || pageSize<=256
50634    ){
50635      goto page1_init_failed;
50636    }
50637    assert( (pageSize & 7)==0 );
50638    usableSize = pageSize - page1[20];
50639    if( (u32)pageSize!=pBt->pageSize ){
50640      /* After reading the first page of the database assuming a page size
50641      ** of BtShared.pageSize, we have discovered that the page-size is
50642      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
50643      ** zero and return SQLITE_OK. The caller will call this function
50644      ** again with the correct page-size.
50645      */
50646      releasePage(pPage1);
50647      pBt->usableSize = usableSize;
50648      pBt->pageSize = pageSize;
50649      freeTempSpace(pBt);
50650      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
50651                                   pageSize-usableSize);
50652      return rc;
50653    }
50654    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
50655      rc = SQLITE_CORRUPT_BKPT;
50656      goto page1_init_failed;
50657    }
50658    if( usableSize<480 ){
50659      goto page1_init_failed;
50660    }
50661    pBt->pageSize = pageSize;
50662    pBt->usableSize = usableSize;
50663#ifndef SQLITE_OMIT_AUTOVACUUM
50664    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
50665    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
50666#endif
50667  }
50668
50669  /* maxLocal is the maximum amount of payload to store locally for
50670  ** a cell.  Make sure it is small enough so that at least minFanout
50671  ** cells can will fit on one page.  We assume a 10-byte page header.
50672  ** Besides the payload, the cell must store:
50673  **     2-byte pointer to the cell
50674  **     4-byte child pointer
50675  **     9-byte nKey value
50676  **     4-byte nData value
50677  **     4-byte overflow page pointer
50678  ** So a cell consists of a 2-byte pointer, a header which is as much as
50679  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
50680  ** page pointer.
50681  */
50682  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
50683  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
50684  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
50685  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
50686  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
50687  pBt->pPage1 = pPage1;
50688  pBt->nPage = nPage;
50689  return SQLITE_OK;
50690
50691page1_init_failed:
50692  releasePage(pPage1);
50693  pBt->pPage1 = 0;
50694  return rc;
50695}
50696
50697/*
50698** If there are no outstanding cursors and we are not in the middle
50699** of a transaction but there is a read lock on the database, then
50700** this routine unrefs the first page of the database file which
50701** has the effect of releasing the read lock.
50702**
50703** If there is a transaction in progress, this routine is a no-op.
50704*/
50705static void unlockBtreeIfUnused(BtShared *pBt){
50706  assert( sqlite3_mutex_held(pBt->mutex) );
50707  assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
50708  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
50709    assert( pBt->pPage1->aData );
50710    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
50711    assert( pBt->pPage1->aData );
50712    releasePage(pBt->pPage1);
50713    pBt->pPage1 = 0;
50714  }
50715}
50716
50717/*
50718** If pBt points to an empty file then convert that empty file
50719** into a new empty database by initializing the first page of
50720** the database.
50721*/
50722static int newDatabase(BtShared *pBt){
50723  MemPage *pP1;
50724  unsigned char *data;
50725  int rc;
50726
50727  assert( sqlite3_mutex_held(pBt->mutex) );
50728  if( pBt->nPage>0 ){
50729    return SQLITE_OK;
50730  }
50731  pP1 = pBt->pPage1;
50732  assert( pP1!=0 );
50733  data = pP1->aData;
50734  rc = sqlite3PagerWrite(pP1->pDbPage);
50735  if( rc ) return rc;
50736  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
50737  assert( sizeof(zMagicHeader)==16 );
50738  data[16] = (u8)((pBt->pageSize>>8)&0xff);
50739  data[17] = (u8)((pBt->pageSize>>16)&0xff);
50740  data[18] = 1;
50741  data[19] = 1;
50742  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
50743  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
50744  data[21] = 64;
50745  data[22] = 32;
50746  data[23] = 32;
50747  memset(&data[24], 0, 100-24);
50748  zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
50749  pBt->pageSizeFixed = 1;
50750#ifndef SQLITE_OMIT_AUTOVACUUM
50751  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
50752  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
50753  put4byte(&data[36 + 4*4], pBt->autoVacuum);
50754  put4byte(&data[36 + 7*4], pBt->incrVacuum);
50755#endif
50756  pBt->nPage = 1;
50757  data[31] = 1;
50758  return SQLITE_OK;
50759}
50760
50761/*
50762** Attempt to start a new transaction. A write-transaction
50763** is started if the second argument is nonzero, otherwise a read-
50764** transaction.  If the second argument is 2 or more and exclusive
50765** transaction is started, meaning that no other process is allowed
50766** to access the database.  A preexisting transaction may not be
50767** upgraded to exclusive by calling this routine a second time - the
50768** exclusivity flag only works for a new transaction.
50769**
50770** A write-transaction must be started before attempting any
50771** changes to the database.  None of the following routines
50772** will work unless a transaction is started first:
50773**
50774**      sqlite3BtreeCreateTable()
50775**      sqlite3BtreeCreateIndex()
50776**      sqlite3BtreeClearTable()
50777**      sqlite3BtreeDropTable()
50778**      sqlite3BtreeInsert()
50779**      sqlite3BtreeDelete()
50780**      sqlite3BtreeUpdateMeta()
50781**
50782** If an initial attempt to acquire the lock fails because of lock contention
50783** and the database was previously unlocked, then invoke the busy handler
50784** if there is one.  But if there was previously a read-lock, do not
50785** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
50786** returned when there is already a read-lock in order to avoid a deadlock.
50787**
50788** Suppose there are two processes A and B.  A has a read lock and B has
50789** a reserved lock.  B tries to promote to exclusive but is blocked because
50790** of A's read lock.  A tries to promote to reserved but is blocked by B.
50791** One or the other of the two processes must give way or there can be
50792** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
50793** when A already has a read lock, we encourage A to give up and let B
50794** proceed.
50795*/
50796SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
50797  sqlite3 *pBlock = 0;
50798  BtShared *pBt = p->pBt;
50799  int rc = SQLITE_OK;
50800
50801  sqlite3BtreeEnter(p);
50802  btreeIntegrity(p);
50803
50804  /* If the btree is already in a write-transaction, or it
50805  ** is already in a read-transaction and a read-transaction
50806  ** is requested, this is a no-op.
50807  */
50808  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
50809    goto trans_begun;
50810  }
50811
50812  /* Write transactions are not possible on a read-only database */
50813  if( pBt->readOnly && wrflag ){
50814    rc = SQLITE_READONLY;
50815    goto trans_begun;
50816  }
50817
50818#ifndef SQLITE_OMIT_SHARED_CACHE
50819  /* If another database handle has already opened a write transaction
50820  ** on this shared-btree structure and a second write transaction is
50821  ** requested, return SQLITE_LOCKED.
50822  */
50823  if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
50824    pBlock = pBt->pWriter->db;
50825  }else if( wrflag>1 ){
50826    BtLock *pIter;
50827    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
50828      if( pIter->pBtree!=p ){
50829        pBlock = pIter->pBtree->db;
50830        break;
50831      }
50832    }
50833  }
50834  if( pBlock ){
50835    sqlite3ConnectionBlocked(p->db, pBlock);
50836    rc = SQLITE_LOCKED_SHAREDCACHE;
50837    goto trans_begun;
50838  }
50839#endif
50840
50841  /* Any read-only or read-write transaction implies a read-lock on
50842  ** page 1. So if some other shared-cache client already has a write-lock
50843  ** on page 1, the transaction cannot be opened. */
50844  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
50845  if( SQLITE_OK!=rc ) goto trans_begun;
50846
50847  pBt->initiallyEmpty = (u8)(pBt->nPage==0);
50848  do {
50849    /* Call lockBtree() until either pBt->pPage1 is populated or
50850    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
50851    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
50852    ** reading page 1 it discovers that the page-size of the database
50853    ** file is not pBt->pageSize. In this case lockBtree() will update
50854    ** pBt->pageSize to the page-size of the file on disk.
50855    */
50856    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
50857
50858    if( rc==SQLITE_OK && wrflag ){
50859      if( pBt->readOnly ){
50860        rc = SQLITE_READONLY;
50861      }else{
50862        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
50863        if( rc==SQLITE_OK ){
50864          rc = newDatabase(pBt);
50865        }
50866      }
50867    }
50868
50869    if( rc!=SQLITE_OK ){
50870      unlockBtreeIfUnused(pBt);
50871    }
50872  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
50873          btreeInvokeBusyHandler(pBt) );
50874
50875  if( rc==SQLITE_OK ){
50876    if( p->inTrans==TRANS_NONE ){
50877      pBt->nTransaction++;
50878#ifndef SQLITE_OMIT_SHARED_CACHE
50879      if( p->sharable ){
50880	assert( p->lock.pBtree==p && p->lock.iTable==1 );
50881        p->lock.eLock = READ_LOCK;
50882        p->lock.pNext = pBt->pLock;
50883        pBt->pLock = &p->lock;
50884      }
50885#endif
50886    }
50887    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
50888    if( p->inTrans>pBt->inTransaction ){
50889      pBt->inTransaction = p->inTrans;
50890    }
50891    if( wrflag ){
50892      MemPage *pPage1 = pBt->pPage1;
50893#ifndef SQLITE_OMIT_SHARED_CACHE
50894      assert( !pBt->pWriter );
50895      pBt->pWriter = p;
50896      pBt->isExclusive = (u8)(wrflag>1);
50897#endif
50898
50899      /* If the db-size header field is incorrect (as it may be if an old
50900      ** client has been writing the database file), update it now. Doing
50901      ** this sooner rather than later means the database size can safely
50902      ** re-read the database size from page 1 if a savepoint or transaction
50903      ** rollback occurs within the transaction.
50904      */
50905      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
50906        rc = sqlite3PagerWrite(pPage1->pDbPage);
50907        if( rc==SQLITE_OK ){
50908          put4byte(&pPage1->aData[28], pBt->nPage);
50909        }
50910      }
50911    }
50912  }
50913
50914
50915trans_begun:
50916  if( rc==SQLITE_OK && wrflag ){
50917    /* This call makes sure that the pager has the correct number of
50918    ** open savepoints. If the second parameter is greater than 0 and
50919    ** the sub-journal is not already open, then it will be opened here.
50920    */
50921    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
50922  }
50923
50924  btreeIntegrity(p);
50925  sqlite3BtreeLeave(p);
50926  return rc;
50927}
50928
50929#ifndef SQLITE_OMIT_AUTOVACUUM
50930
50931/*
50932** Set the pointer-map entries for all children of page pPage. Also, if
50933** pPage contains cells that point to overflow pages, set the pointer
50934** map entries for the overflow pages as well.
50935*/
50936static int setChildPtrmaps(MemPage *pPage){
50937  int i;                             /* Counter variable */
50938  int nCell;                         /* Number of cells in page pPage */
50939  int rc;                            /* Return code */
50940  BtShared *pBt = pPage->pBt;
50941  u8 isInitOrig = pPage->isInit;
50942  Pgno pgno = pPage->pgno;
50943
50944  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50945  rc = btreeInitPage(pPage);
50946  if( rc!=SQLITE_OK ){
50947    goto set_child_ptrmaps_out;
50948  }
50949  nCell = pPage->nCell;
50950
50951  for(i=0; i<nCell; i++){
50952    u8 *pCell = findCell(pPage, i);
50953
50954    ptrmapPutOvflPtr(pPage, pCell, &rc);
50955
50956    if( !pPage->leaf ){
50957      Pgno childPgno = get4byte(pCell);
50958      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
50959    }
50960  }
50961
50962  if( !pPage->leaf ){
50963    Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
50964    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
50965  }
50966
50967set_child_ptrmaps_out:
50968  pPage->isInit = isInitOrig;
50969  return rc;
50970}
50971
50972/*
50973** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
50974** that it points to iTo. Parameter eType describes the type of pointer to
50975** be modified, as  follows:
50976**
50977** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
50978**                   page of pPage.
50979**
50980** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
50981**                   page pointed to by one of the cells on pPage.
50982**
50983** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
50984**                   overflow page in the list.
50985*/
50986static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
50987  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50988  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
50989  if( eType==PTRMAP_OVERFLOW2 ){
50990    /* The pointer is always the first 4 bytes of the page in this case.  */
50991    if( get4byte(pPage->aData)!=iFrom ){
50992      return SQLITE_CORRUPT_BKPT;
50993    }
50994    put4byte(pPage->aData, iTo);
50995  }else{
50996    u8 isInitOrig = pPage->isInit;
50997    int i;
50998    int nCell;
50999
51000    btreeInitPage(pPage);
51001    nCell = pPage->nCell;
51002
51003    for(i=0; i<nCell; i++){
51004      u8 *pCell = findCell(pPage, i);
51005      if( eType==PTRMAP_OVERFLOW1 ){
51006        CellInfo info;
51007        btreeParseCellPtr(pPage, pCell, &info);
51008        if( info.iOverflow
51009         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
51010         && iFrom==get4byte(&pCell[info.iOverflow])
51011        ){
51012          put4byte(&pCell[info.iOverflow], iTo);
51013          break;
51014        }
51015      }else{
51016        if( get4byte(pCell)==iFrom ){
51017          put4byte(pCell, iTo);
51018          break;
51019        }
51020      }
51021    }
51022
51023    if( i==nCell ){
51024      if( eType!=PTRMAP_BTREE ||
51025          get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
51026        return SQLITE_CORRUPT_BKPT;
51027      }
51028      put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
51029    }
51030
51031    pPage->isInit = isInitOrig;
51032  }
51033  return SQLITE_OK;
51034}
51035
51036
51037/*
51038** Move the open database page pDbPage to location iFreePage in the
51039** database. The pDbPage reference remains valid.
51040**
51041** The isCommit flag indicates that there is no need to remember that
51042** the journal needs to be sync()ed before database page pDbPage->pgno
51043** can be written to. The caller has already promised not to write to that
51044** page.
51045*/
51046static int relocatePage(
51047  BtShared *pBt,           /* Btree */
51048  MemPage *pDbPage,        /* Open page to move */
51049  u8 eType,                /* Pointer map 'type' entry for pDbPage */
51050  Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
51051  Pgno iFreePage,          /* The location to move pDbPage to */
51052  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
51053){
51054  MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
51055  Pgno iDbPage = pDbPage->pgno;
51056  Pager *pPager = pBt->pPager;
51057  int rc;
51058
51059  assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
51060      eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
51061  assert( sqlite3_mutex_held(pBt->mutex) );
51062  assert( pDbPage->pBt==pBt );
51063
51064  /* Move page iDbPage from its current location to page number iFreePage */
51065  TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
51066      iDbPage, iFreePage, iPtrPage, eType));
51067  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
51068  if( rc!=SQLITE_OK ){
51069    return rc;
51070  }
51071  pDbPage->pgno = iFreePage;
51072
51073  /* If pDbPage was a btree-page, then it may have child pages and/or cells
51074  ** that point to overflow pages. The pointer map entries for all these
51075  ** pages need to be changed.
51076  **
51077  ** If pDbPage is an overflow page, then the first 4 bytes may store a
51078  ** pointer to a subsequent overflow page. If this is the case, then
51079  ** the pointer map needs to be updated for the subsequent overflow page.
51080  */
51081  if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
51082    rc = setChildPtrmaps(pDbPage);
51083    if( rc!=SQLITE_OK ){
51084      return rc;
51085    }
51086  }else{
51087    Pgno nextOvfl = get4byte(pDbPage->aData);
51088    if( nextOvfl!=0 ){
51089      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
51090      if( rc!=SQLITE_OK ){
51091        return rc;
51092      }
51093    }
51094  }
51095
51096  /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
51097  ** that it points at iFreePage. Also fix the pointer map entry for
51098  ** iPtrPage.
51099  */
51100  if( eType!=PTRMAP_ROOTPAGE ){
51101    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
51102    if( rc!=SQLITE_OK ){
51103      return rc;
51104    }
51105    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
51106    if( rc!=SQLITE_OK ){
51107      releasePage(pPtrPage);
51108      return rc;
51109    }
51110    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
51111    releasePage(pPtrPage);
51112    if( rc==SQLITE_OK ){
51113      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
51114    }
51115  }
51116  return rc;
51117}
51118
51119/* Forward declaration required by incrVacuumStep(). */
51120static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
51121
51122/*
51123** Perform a single step of an incremental-vacuum. If successful,
51124** return SQLITE_OK. If there is no work to do (and therefore no
51125** point in calling this function again), return SQLITE_DONE.
51126**
51127** More specificly, this function attempts to re-organize the
51128** database so that the last page of the file currently in use
51129** is no longer in use.
51130**
51131** If the nFin parameter is non-zero, this function assumes
51132** that the caller will keep calling incrVacuumStep() until
51133** it returns SQLITE_DONE or an error, and that nFin is the
51134** number of pages the database file will contain after this
51135** process is complete.  If nFin is zero, it is assumed that
51136** incrVacuumStep() will be called a finite amount of times
51137** which may or may not empty the freelist.  A full autovacuum
51138** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
51139*/
51140static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
51141  Pgno nFreeList;           /* Number of pages still on the free-list */
51142  int rc;
51143
51144  assert( sqlite3_mutex_held(pBt->mutex) );
51145  assert( iLastPg>nFin );
51146
51147  if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
51148    u8 eType;
51149    Pgno iPtrPage;
51150
51151    nFreeList = get4byte(&pBt->pPage1->aData[36]);
51152    if( nFreeList==0 ){
51153      return SQLITE_DONE;
51154    }
51155
51156    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
51157    if( rc!=SQLITE_OK ){
51158      return rc;
51159    }
51160    if( eType==PTRMAP_ROOTPAGE ){
51161      return SQLITE_CORRUPT_BKPT;
51162    }
51163
51164    if( eType==PTRMAP_FREEPAGE ){
51165      if( nFin==0 ){
51166        /* Remove the page from the files free-list. This is not required
51167        ** if nFin is non-zero. In that case, the free-list will be
51168        ** truncated to zero after this function returns, so it doesn't
51169        ** matter if it still contains some garbage entries.
51170        */
51171        Pgno iFreePg;
51172        MemPage *pFreePg;
51173        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
51174        if( rc!=SQLITE_OK ){
51175          return rc;
51176        }
51177        assert( iFreePg==iLastPg );
51178        releasePage(pFreePg);
51179      }
51180    } else {
51181      Pgno iFreePg;             /* Index of free page to move pLastPg to */
51182      MemPage *pLastPg;
51183
51184      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
51185      if( rc!=SQLITE_OK ){
51186        return rc;
51187      }
51188
51189      /* If nFin is zero, this loop runs exactly once and page pLastPg
51190      ** is swapped with the first free page pulled off the free list.
51191      **
51192      ** On the other hand, if nFin is greater than zero, then keep
51193      ** looping until a free-page located within the first nFin pages
51194      ** of the file is found.
51195      */
51196      do {
51197        MemPage *pFreePg;
51198        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
51199        if( rc!=SQLITE_OK ){
51200          releasePage(pLastPg);
51201          return rc;
51202        }
51203        releasePage(pFreePg);
51204      }while( nFin!=0 && iFreePg>nFin );
51205      assert( iFreePg<iLastPg );
51206
51207      rc = sqlite3PagerWrite(pLastPg->pDbPage);
51208      if( rc==SQLITE_OK ){
51209        rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
51210      }
51211      releasePage(pLastPg);
51212      if( rc!=SQLITE_OK ){
51213        return rc;
51214      }
51215    }
51216  }
51217
51218  if( nFin==0 ){
51219    iLastPg--;
51220    while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
51221      if( PTRMAP_ISPAGE(pBt, iLastPg) ){
51222        MemPage *pPg;
51223        rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
51224        if( rc!=SQLITE_OK ){
51225          return rc;
51226        }
51227        rc = sqlite3PagerWrite(pPg->pDbPage);
51228        releasePage(pPg);
51229        if( rc!=SQLITE_OK ){
51230          return rc;
51231        }
51232      }
51233      iLastPg--;
51234    }
51235    sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
51236    pBt->nPage = iLastPg;
51237  }
51238  return SQLITE_OK;
51239}
51240
51241/*
51242** A write-transaction must be opened before calling this function.
51243** It performs a single unit of work towards an incremental vacuum.
51244**
51245** If the incremental vacuum is finished after this function has run,
51246** SQLITE_DONE is returned. If it is not finished, but no error occurred,
51247** SQLITE_OK is returned. Otherwise an SQLite error code.
51248*/
51249SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
51250  int rc;
51251  BtShared *pBt = p->pBt;
51252
51253  sqlite3BtreeEnter(p);
51254  assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
51255  if( !pBt->autoVacuum ){
51256    rc = SQLITE_DONE;
51257  }else{
51258    invalidateAllOverflowCache(pBt);
51259    rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
51260    if( rc==SQLITE_OK ){
51261      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51262      put4byte(&pBt->pPage1->aData[28], pBt->nPage);
51263    }
51264  }
51265  sqlite3BtreeLeave(p);
51266  return rc;
51267}
51268
51269/*
51270** This routine is called prior to sqlite3PagerCommit when a transaction
51271** is commited for an auto-vacuum database.
51272**
51273** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
51274** the database file should be truncated to during the commit process.
51275** i.e. the database has been reorganized so that only the first *pnTrunc
51276** pages are in use.
51277*/
51278static int autoVacuumCommit(BtShared *pBt){
51279  int rc = SQLITE_OK;
51280  Pager *pPager = pBt->pPager;
51281  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
51282
51283  assert( sqlite3_mutex_held(pBt->mutex) );
51284  invalidateAllOverflowCache(pBt);
51285  assert(pBt->autoVacuum);
51286  if( !pBt->incrVacuum ){
51287    Pgno nFin;         /* Number of pages in database after autovacuuming */
51288    Pgno nFree;        /* Number of pages on the freelist initially */
51289    Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
51290    Pgno iFree;        /* The next page to be freed */
51291    int nEntry;        /* Number of entries on one ptrmap page */
51292    Pgno nOrig;        /* Database size before freeing */
51293
51294    nOrig = btreePagecount(pBt);
51295    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
51296      /* It is not possible to create a database for which the final page
51297      ** is either a pointer-map page or the pending-byte page. If one
51298      ** is encountered, this indicates corruption.
51299      */
51300      return SQLITE_CORRUPT_BKPT;
51301    }
51302
51303    nFree = get4byte(&pBt->pPage1->aData[36]);
51304    nEntry = pBt->usableSize/5;
51305    nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
51306    nFin = nOrig - nFree - nPtrmap;
51307    if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
51308      nFin--;
51309    }
51310    while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
51311      nFin--;
51312    }
51313    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
51314
51315    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
51316      rc = incrVacuumStep(pBt, nFin, iFree);
51317    }
51318    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
51319      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51320      put4byte(&pBt->pPage1->aData[32], 0);
51321      put4byte(&pBt->pPage1->aData[36], 0);
51322      put4byte(&pBt->pPage1->aData[28], nFin);
51323      sqlite3PagerTruncateImage(pBt->pPager, nFin);
51324      pBt->nPage = nFin;
51325    }
51326    if( rc!=SQLITE_OK ){
51327      sqlite3PagerRollback(pPager);
51328    }
51329  }
51330
51331  assert( nRef==sqlite3PagerRefcount(pPager) );
51332  return rc;
51333}
51334
51335#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
51336# define setChildPtrmaps(x) SQLITE_OK
51337#endif
51338
51339/*
51340** This routine does the first phase of a two-phase commit.  This routine
51341** causes a rollback journal to be created (if it does not already exist)
51342** and populated with enough information so that if a power loss occurs
51343** the database can be restored to its original state by playing back
51344** the journal.  Then the contents of the journal are flushed out to
51345** the disk.  After the journal is safely on oxide, the changes to the
51346** database are written into the database file and flushed to oxide.
51347** At the end of this call, the rollback journal still exists on the
51348** disk and we are still holding all locks, so the transaction has not
51349** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
51350** commit process.
51351**
51352** This call is a no-op if no write-transaction is currently active on pBt.
51353**
51354** Otherwise, sync the database file for the btree pBt. zMaster points to
51355** the name of a master journal file that should be written into the
51356** individual journal file, or is NULL, indicating no master journal file
51357** (single database transaction).
51358**
51359** When this is called, the master journal should already have been
51360** created, populated with this journal pointer and synced to disk.
51361**
51362** Once this is routine has returned, the only thing required to commit
51363** the write-transaction for this database file is to delete the journal.
51364*/
51365SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
51366  int rc = SQLITE_OK;
51367  if( p->inTrans==TRANS_WRITE ){
51368    BtShared *pBt = p->pBt;
51369    sqlite3BtreeEnter(p);
51370#ifndef SQLITE_OMIT_AUTOVACUUM
51371    if( pBt->autoVacuum ){
51372      rc = autoVacuumCommit(pBt);
51373      if( rc!=SQLITE_OK ){
51374        sqlite3BtreeLeave(p);
51375        return rc;
51376      }
51377    }
51378#endif
51379    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
51380    sqlite3BtreeLeave(p);
51381  }
51382  return rc;
51383}
51384
51385/*
51386** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
51387** at the conclusion of a transaction.
51388*/
51389static void btreeEndTransaction(Btree *p){
51390  BtShared *pBt = p->pBt;
51391  assert( sqlite3BtreeHoldsMutex(p) );
51392
51393  btreeClearHasContent(pBt);
51394  if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
51395    /* If there are other active statements that belong to this database
51396    ** handle, downgrade to a read-only transaction. The other statements
51397    ** may still be reading from the database.  */
51398    downgradeAllSharedCacheTableLocks(p);
51399    p->inTrans = TRANS_READ;
51400  }else{
51401    /* If the handle had any kind of transaction open, decrement the
51402    ** transaction count of the shared btree. If the transaction count
51403    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
51404    ** call below will unlock the pager.  */
51405    if( p->inTrans!=TRANS_NONE ){
51406      clearAllSharedCacheTableLocks(p);
51407      pBt->nTransaction--;
51408      if( 0==pBt->nTransaction ){
51409        pBt->inTransaction = TRANS_NONE;
51410      }
51411    }
51412
51413    /* Set the current transaction state to TRANS_NONE and unlock the
51414    ** pager if this call closed the only read or write transaction.  */
51415    p->inTrans = TRANS_NONE;
51416    unlockBtreeIfUnused(pBt);
51417  }
51418
51419  btreeIntegrity(p);
51420}
51421
51422/*
51423** Commit the transaction currently in progress.
51424**
51425** This routine implements the second phase of a 2-phase commit.  The
51426** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
51427** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
51428** routine did all the work of writing information out to disk and flushing the
51429** contents so that they are written onto the disk platter.  All this
51430** routine has to do is delete or truncate or zero the header in the
51431** the rollback journal (which causes the transaction to commit) and
51432** drop locks.
51433**
51434** Normally, if an error occurs while the pager layer is attempting to
51435** finalize the underlying journal file, this function returns an error and
51436** the upper layer will attempt a rollback. However, if the second argument
51437** is non-zero then this b-tree transaction is part of a multi-file
51438** transaction. In this case, the transaction has already been committed
51439** (by deleting a master journal file) and the caller will ignore this
51440** functions return code. So, even if an error occurs in the pager layer,
51441** reset the b-tree objects internal state to indicate that the write
51442** transaction has been closed. This is quite safe, as the pager will have
51443** transitioned to the error state.
51444**
51445** This will release the write lock on the database file.  If there
51446** are no active cursors, it also releases the read lock.
51447*/
51448SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
51449
51450  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
51451  sqlite3BtreeEnter(p);
51452  btreeIntegrity(p);
51453
51454  /* If the handle has a write-transaction open, commit the shared-btrees
51455  ** transaction and set the shared state to TRANS_READ.
51456  */
51457  if( p->inTrans==TRANS_WRITE ){
51458    int rc;
51459    BtShared *pBt = p->pBt;
51460    assert( pBt->inTransaction==TRANS_WRITE );
51461    assert( pBt->nTransaction>0 );
51462    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
51463    if( rc!=SQLITE_OK && bCleanup==0 ){
51464      sqlite3BtreeLeave(p);
51465      return rc;
51466    }
51467    pBt->inTransaction = TRANS_READ;
51468  }
51469
51470  btreeEndTransaction(p);
51471  sqlite3BtreeLeave(p);
51472  return SQLITE_OK;
51473}
51474
51475/*
51476** Do both phases of a commit.
51477*/
51478SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
51479  int rc;
51480  sqlite3BtreeEnter(p);
51481  rc = sqlite3BtreeCommitPhaseOne(p, 0);
51482  if( rc==SQLITE_OK ){
51483    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
51484  }
51485  sqlite3BtreeLeave(p);
51486  return rc;
51487}
51488
51489#ifndef NDEBUG
51490/*
51491** Return the number of write-cursors open on this handle. This is for use
51492** in assert() expressions, so it is only compiled if NDEBUG is not
51493** defined.
51494**
51495** For the purposes of this routine, a write-cursor is any cursor that
51496** is capable of writing to the databse.  That means the cursor was
51497** originally opened for writing and the cursor has not be disabled
51498** by having its state changed to CURSOR_FAULT.
51499*/
51500static int countWriteCursors(BtShared *pBt){
51501  BtCursor *pCur;
51502  int r = 0;
51503  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
51504    if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
51505  }
51506  return r;
51507}
51508#endif
51509
51510/*
51511** This routine sets the state to CURSOR_FAULT and the error
51512** code to errCode for every cursor on BtShared that pBtree
51513** references.
51514**
51515** Every cursor is tripped, including cursors that belong
51516** to other database connections that happen to be sharing
51517** the cache with pBtree.
51518**
51519** This routine gets called when a rollback occurs.
51520** All cursors using the same cache must be tripped
51521** to prevent them from trying to use the btree after
51522** the rollback.  The rollback may have deleted tables
51523** or moved root pages, so it is not sufficient to
51524** save the state of the cursor.  The cursor must be
51525** invalidated.
51526*/
51527SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
51528  BtCursor *p;
51529  sqlite3BtreeEnter(pBtree);
51530  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
51531    int i;
51532    sqlite3BtreeClearCursor(p);
51533    p->eState = CURSOR_FAULT;
51534    p->skipNext = errCode;
51535    for(i=0; i<=p->iPage; i++){
51536      releasePage(p->apPage[i]);
51537      p->apPage[i] = 0;
51538    }
51539  }
51540  sqlite3BtreeLeave(pBtree);
51541}
51542
51543/*
51544** Rollback the transaction in progress.  All cursors will be
51545** invalided by this operation.  Any attempt to use a cursor
51546** that was open at the beginning of this operation will result
51547** in an error.
51548**
51549** This will release the write lock on the database file.  If there
51550** are no active cursors, it also releases the read lock.
51551*/
51552SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
51553  int rc;
51554  BtShared *pBt = p->pBt;
51555  MemPage *pPage1;
51556
51557  sqlite3BtreeEnter(p);
51558  rc = saveAllCursors(pBt, 0, 0);
51559#ifndef SQLITE_OMIT_SHARED_CACHE
51560  if( rc!=SQLITE_OK ){
51561    /* This is a horrible situation. An IO or malloc() error occurred whilst
51562    ** trying to save cursor positions. If this is an automatic rollback (as
51563    ** the result of a constraint, malloc() failure or IO error) then
51564    ** the cache may be internally inconsistent (not contain valid trees) so
51565    ** we cannot simply return the error to the caller. Instead, abort
51566    ** all queries that may be using any of the cursors that failed to save.
51567    */
51568    sqlite3BtreeTripAllCursors(p, rc);
51569  }
51570#endif
51571  btreeIntegrity(p);
51572
51573  if( p->inTrans==TRANS_WRITE ){
51574    int rc2;
51575
51576    assert( TRANS_WRITE==pBt->inTransaction );
51577    rc2 = sqlite3PagerRollback(pBt->pPager);
51578    if( rc2!=SQLITE_OK ){
51579      rc = rc2;
51580    }
51581
51582    /* The rollback may have destroyed the pPage1->aData value.  So
51583    ** call btreeGetPage() on page 1 again to make
51584    ** sure pPage1->aData is set correctly. */
51585    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
51586      int nPage = get4byte(28+(u8*)pPage1->aData);
51587      testcase( nPage==0 );
51588      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
51589      testcase( pBt->nPage!=nPage );
51590      pBt->nPage = nPage;
51591      releasePage(pPage1);
51592    }
51593    assert( countWriteCursors(pBt)==0 );
51594    pBt->inTransaction = TRANS_READ;
51595  }
51596
51597  btreeEndTransaction(p);
51598  sqlite3BtreeLeave(p);
51599  return rc;
51600}
51601
51602/*
51603** Start a statement subtransaction. The subtransaction can can be rolled
51604** back independently of the main transaction. You must start a transaction
51605** before starting a subtransaction. The subtransaction is ended automatically
51606** if the main transaction commits or rolls back.
51607**
51608** Statement subtransactions are used around individual SQL statements
51609** that are contained within a BEGIN...COMMIT block.  If a constraint
51610** error occurs within the statement, the effect of that one statement
51611** can be rolled back without having to rollback the entire transaction.
51612**
51613** A statement sub-transaction is implemented as an anonymous savepoint. The
51614** value passed as the second parameter is the total number of savepoints,
51615** including the new anonymous savepoint, open on the B-Tree. i.e. if there
51616** are no active savepoints and no other statement-transactions open,
51617** iStatement is 1. This anonymous savepoint can be released or rolled back
51618** using the sqlite3BtreeSavepoint() function.
51619*/
51620SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
51621  int rc;
51622  BtShared *pBt = p->pBt;
51623  sqlite3BtreeEnter(p);
51624  assert( p->inTrans==TRANS_WRITE );
51625  assert( pBt->readOnly==0 );
51626  assert( iStatement>0 );
51627  assert( iStatement>p->db->nSavepoint );
51628  assert( pBt->inTransaction==TRANS_WRITE );
51629  /* At the pager level, a statement transaction is a savepoint with
51630  ** an index greater than all savepoints created explicitly using
51631  ** SQL statements. It is illegal to open, release or rollback any
51632  ** such savepoints while the statement transaction savepoint is active.
51633  */
51634  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
51635  sqlite3BtreeLeave(p);
51636  return rc;
51637}
51638
51639/*
51640** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
51641** or SAVEPOINT_RELEASE. This function either releases or rolls back the
51642** savepoint identified by parameter iSavepoint, depending on the value
51643** of op.
51644**
51645** Normally, iSavepoint is greater than or equal to zero. However, if op is
51646** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
51647** contents of the entire transaction are rolled back. This is different
51648** from a normal transaction rollback, as no locks are released and the
51649** transaction remains open.
51650*/
51651SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
51652  int rc = SQLITE_OK;
51653  if( p && p->inTrans==TRANS_WRITE ){
51654    BtShared *pBt = p->pBt;
51655    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
51656    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
51657    sqlite3BtreeEnter(p);
51658    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
51659    if( rc==SQLITE_OK ){
51660      if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
51661      rc = newDatabase(pBt);
51662      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
51663
51664      /* The database size was written into the offset 28 of the header
51665      ** when the transaction started, so we know that the value at offset
51666      ** 28 is nonzero. */
51667      assert( pBt->nPage>0 );
51668    }
51669    sqlite3BtreeLeave(p);
51670  }
51671  return rc;
51672}
51673
51674/*
51675** Create a new cursor for the BTree whose root is on the page
51676** iTable. If a read-only cursor is requested, it is assumed that
51677** the caller already has at least a read-only transaction open
51678** on the database already. If a write-cursor is requested, then
51679** the caller is assumed to have an open write transaction.
51680**
51681** If wrFlag==0, then the cursor can only be used for reading.
51682** If wrFlag==1, then the cursor can be used for reading or for
51683** writing if other conditions for writing are also met.  These
51684** are the conditions that must be met in order for writing to
51685** be allowed:
51686**
51687** 1:  The cursor must have been opened with wrFlag==1
51688**
51689** 2:  Other database connections that share the same pager cache
51690**     but which are not in the READ_UNCOMMITTED state may not have
51691**     cursors open with wrFlag==0 on the same table.  Otherwise
51692**     the changes made by this write cursor would be visible to
51693**     the read cursors in the other database connection.
51694**
51695** 3:  The database must be writable (not on read-only media)
51696**
51697** 4:  There must be an active transaction.
51698**
51699** No checking is done to make sure that page iTable really is the
51700** root page of a b-tree.  If it is not, then the cursor acquired
51701** will not work correctly.
51702**
51703** It is assumed that the sqlite3BtreeCursorZero() has been called
51704** on pCur to initialize the memory space prior to invoking this routine.
51705*/
51706static int btreeCursor(
51707  Btree *p,                              /* The btree */
51708  int iTable,                            /* Root page of table to open */
51709  int wrFlag,                            /* 1 to write. 0 read-only */
51710  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
51711  BtCursor *pCur                         /* Space for new cursor */
51712){
51713  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
51714
51715  assert( sqlite3BtreeHoldsMutex(p) );
51716  assert( wrFlag==0 || wrFlag==1 );
51717
51718  /* The following assert statements verify that if this is a sharable
51719  ** b-tree database, the connection is holding the required table locks,
51720  ** and that no other connection has any open cursor that conflicts with
51721  ** this lock.  */
51722  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
51723  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
51724
51725  /* Assert that the caller has opened the required transaction. */
51726  assert( p->inTrans>TRANS_NONE );
51727  assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
51728  assert( pBt->pPage1 && pBt->pPage1->aData );
51729
51730  if( NEVER(wrFlag && pBt->readOnly) ){
51731    return SQLITE_READONLY;
51732  }
51733  if( iTable==1 && btreePagecount(pBt)==0 ){
51734    assert( wrFlag==0 );
51735    iTable = 0;
51736  }
51737
51738  /* Now that no other errors can occur, finish filling in the BtCursor
51739  ** variables and link the cursor into the BtShared list.  */
51740  pCur->pgnoRoot = (Pgno)iTable;
51741  pCur->iPage = -1;
51742  pCur->pKeyInfo = pKeyInfo;
51743  pCur->pBtree = p;
51744  pCur->pBt = pBt;
51745  pCur->wrFlag = (u8)wrFlag;
51746  pCur->pNext = pBt->pCursor;
51747  if( pCur->pNext ){
51748    pCur->pNext->pPrev = pCur;
51749  }
51750  pBt->pCursor = pCur;
51751  pCur->eState = CURSOR_INVALID;
51752  pCur->cachedRowid = 0;
51753  return SQLITE_OK;
51754}
51755SQLITE_PRIVATE int sqlite3BtreeCursor(
51756  Btree *p,                                   /* The btree */
51757  int iTable,                                 /* Root page of table to open */
51758  int wrFlag,                                 /* 1 to write. 0 read-only */
51759  struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
51760  BtCursor *pCur                              /* Write new cursor here */
51761){
51762  int rc;
51763  sqlite3BtreeEnter(p);
51764  rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
51765  sqlite3BtreeLeave(p);
51766  return rc;
51767}
51768
51769/*
51770** Return the size of a BtCursor object in bytes.
51771**
51772** This interfaces is needed so that users of cursors can preallocate
51773** sufficient storage to hold a cursor.  The BtCursor object is opaque
51774** to users so they cannot do the sizeof() themselves - they must call
51775** this routine.
51776*/
51777SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
51778  return ROUND8(sizeof(BtCursor));
51779}
51780
51781/*
51782** Initialize memory that will be converted into a BtCursor object.
51783**
51784** The simple approach here would be to memset() the entire object
51785** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
51786** do not need to be zeroed and they are large, so we can save a lot
51787** of run-time by skipping the initialization of those elements.
51788*/
51789SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
51790  memset(p, 0, offsetof(BtCursor, iPage));
51791}
51792
51793/*
51794** Set the cached rowid value of every cursor in the same database file
51795** as pCur and having the same root page number as pCur.  The value is
51796** set to iRowid.
51797**
51798** Only positive rowid values are considered valid for this cache.
51799** The cache is initialized to zero, indicating an invalid cache.
51800** A btree will work fine with zero or negative rowids.  We just cannot
51801** cache zero or negative rowids, which means tables that use zero or
51802** negative rowids might run a little slower.  But in practice, zero
51803** or negative rowids are very uncommon so this should not be a problem.
51804*/
51805SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
51806  BtCursor *p;
51807  for(p=pCur->pBt->pCursor; p; p=p->pNext){
51808    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
51809  }
51810  assert( pCur->cachedRowid==iRowid );
51811}
51812
51813/*
51814** Return the cached rowid for the given cursor.  A negative or zero
51815** return value indicates that the rowid cache is invalid and should be
51816** ignored.  If the rowid cache has never before been set, then a
51817** zero is returned.
51818*/
51819SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
51820  return pCur->cachedRowid;
51821}
51822
51823/*
51824** Close a cursor.  The read lock on the database file is released
51825** when the last cursor is closed.
51826*/
51827SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
51828  Btree *pBtree = pCur->pBtree;
51829  if( pBtree ){
51830    int i;
51831    BtShared *pBt = pCur->pBt;
51832    sqlite3BtreeEnter(pBtree);
51833    sqlite3BtreeClearCursor(pCur);
51834    if( pCur->pPrev ){
51835      pCur->pPrev->pNext = pCur->pNext;
51836    }else{
51837      pBt->pCursor = pCur->pNext;
51838    }
51839    if( pCur->pNext ){
51840      pCur->pNext->pPrev = pCur->pPrev;
51841    }
51842    for(i=0; i<=pCur->iPage; i++){
51843      releasePage(pCur->apPage[i]);
51844    }
51845    unlockBtreeIfUnused(pBt);
51846    invalidateOverflowCache(pCur);
51847    /* sqlite3_free(pCur); */
51848    sqlite3BtreeLeave(pBtree);
51849  }
51850  return SQLITE_OK;
51851}
51852
51853/*
51854** Make sure the BtCursor* given in the argument has a valid
51855** BtCursor.info structure.  If it is not already valid, call
51856** btreeParseCell() to fill it in.
51857**
51858** BtCursor.info is a cache of the information in the current cell.
51859** Using this cache reduces the number of calls to btreeParseCell().
51860**
51861** 2007-06-25:  There is a bug in some versions of MSVC that cause the
51862** compiler to crash when getCellInfo() is implemented as a macro.
51863** But there is a measureable speed advantage to using the macro on gcc
51864** (when less compiler optimizations like -Os or -O0 are used and the
51865** compiler is not doing agressive inlining.)  So we use a real function
51866** for MSVC and a macro for everything else.  Ticket #2457.
51867*/
51868#ifndef NDEBUG
51869  static void assertCellInfo(BtCursor *pCur){
51870    CellInfo info;
51871    int iPage = pCur->iPage;
51872    memset(&info, 0, sizeof(info));
51873    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
51874    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
51875  }
51876#else
51877  #define assertCellInfo(x)
51878#endif
51879#ifdef _MSC_VER
51880  /* Use a real function in MSVC to work around bugs in that compiler. */
51881  static void getCellInfo(BtCursor *pCur){
51882    if( pCur->info.nSize==0 ){
51883      int iPage = pCur->iPage;
51884      btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
51885      pCur->validNKey = 1;
51886    }else{
51887      assertCellInfo(pCur);
51888    }
51889  }
51890#else /* if not _MSC_VER */
51891  /* Use a macro in all other compilers so that the function is inlined */
51892#define getCellInfo(pCur)                                                      \
51893  if( pCur->info.nSize==0 ){                                                   \
51894    int iPage = pCur->iPage;                                                   \
51895    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
51896    pCur->validNKey = 1;                                                       \
51897  }else{                                                                       \
51898    assertCellInfo(pCur);                                                      \
51899  }
51900#endif /* _MSC_VER */
51901
51902#ifndef NDEBUG  /* The next routine used only within assert() statements */
51903/*
51904** Return true if the given BtCursor is valid.  A valid cursor is one
51905** that is currently pointing to a row in a (non-empty) table.
51906** This is a verification routine is used only within assert() statements.
51907*/
51908SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
51909  return pCur && pCur->eState==CURSOR_VALID;
51910}
51911#endif /* NDEBUG */
51912
51913/*
51914** Set *pSize to the size of the buffer needed to hold the value of
51915** the key for the current entry.  If the cursor is not pointing
51916** to a valid entry, *pSize is set to 0.
51917**
51918** For a table with the INTKEY flag set, this routine returns the key
51919** itself, not the number of bytes in the key.
51920**
51921** The caller must position the cursor prior to invoking this routine.
51922**
51923** This routine cannot fail.  It always returns SQLITE_OK.
51924*/
51925SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
51926  assert( cursorHoldsMutex(pCur) );
51927  assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
51928  if( pCur->eState!=CURSOR_VALID ){
51929    *pSize = 0;
51930  }else{
51931    getCellInfo(pCur);
51932    *pSize = pCur->info.nKey;
51933  }
51934  return SQLITE_OK;
51935}
51936
51937/*
51938** Set *pSize to the number of bytes of data in the entry the
51939** cursor currently points to.
51940**
51941** The caller must guarantee that the cursor is pointing to a non-NULL
51942** valid entry.  In other words, the calling procedure must guarantee
51943** that the cursor has Cursor.eState==CURSOR_VALID.
51944**
51945** Failure is not possible.  This function always returns SQLITE_OK.
51946** It might just as well be a procedure (returning void) but we continue
51947** to return an integer result code for historical reasons.
51948*/
51949SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
51950  assert( cursorHoldsMutex(pCur) );
51951  assert( pCur->eState==CURSOR_VALID );
51952  getCellInfo(pCur);
51953  *pSize = pCur->info.nData;
51954  return SQLITE_OK;
51955}
51956
51957/*
51958** Given the page number of an overflow page in the database (parameter
51959** ovfl), this function finds the page number of the next page in the
51960** linked list of overflow pages. If possible, it uses the auto-vacuum
51961** pointer-map data instead of reading the content of page ovfl to do so.
51962**
51963** If an error occurs an SQLite error code is returned. Otherwise:
51964**
51965** The page number of the next overflow page in the linked list is
51966** written to *pPgnoNext. If page ovfl is the last page in its linked
51967** list, *pPgnoNext is set to zero.
51968**
51969** If ppPage is not NULL, and a reference to the MemPage object corresponding
51970** to page number pOvfl was obtained, then *ppPage is set to point to that
51971** reference. It is the responsibility of the caller to call releasePage()
51972** on *ppPage to free the reference. In no reference was obtained (because
51973** the pointer-map was used to obtain the value for *pPgnoNext), then
51974** *ppPage is set to zero.
51975*/
51976static int getOverflowPage(
51977  BtShared *pBt,               /* The database file */
51978  Pgno ovfl,                   /* Current overflow page number */
51979  MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
51980  Pgno *pPgnoNext              /* OUT: Next overflow page number */
51981){
51982  Pgno next = 0;
51983  MemPage *pPage = 0;
51984  int rc = SQLITE_OK;
51985
51986  assert( sqlite3_mutex_held(pBt->mutex) );
51987  assert(pPgnoNext);
51988
51989#ifndef SQLITE_OMIT_AUTOVACUUM
51990  /* Try to find the next page in the overflow list using the
51991  ** autovacuum pointer-map pages. Guess that the next page in
51992  ** the overflow list is page number (ovfl+1). If that guess turns
51993  ** out to be wrong, fall back to loading the data of page
51994  ** number ovfl to determine the next page number.
51995  */
51996  if( pBt->autoVacuum ){
51997    Pgno pgno;
51998    Pgno iGuess = ovfl+1;
51999    u8 eType;
52000
52001    while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
52002      iGuess++;
52003    }
52004
52005    if( iGuess<=btreePagecount(pBt) ){
52006      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
52007      if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
52008        next = iGuess;
52009        rc = SQLITE_DONE;
52010      }
52011    }
52012  }
52013#endif
52014
52015  assert( next==0 || rc==SQLITE_DONE );
52016  if( rc==SQLITE_OK ){
52017    rc = btreeGetPage(pBt, ovfl, &pPage, 0);
52018    assert( rc==SQLITE_OK || pPage==0 );
52019    if( rc==SQLITE_OK ){
52020      next = get4byte(pPage->aData);
52021    }
52022  }
52023
52024  *pPgnoNext = next;
52025  if( ppPage ){
52026    *ppPage = pPage;
52027  }else{
52028    releasePage(pPage);
52029  }
52030  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
52031}
52032
52033/*
52034** Copy data from a buffer to a page, or from a page to a buffer.
52035**
52036** pPayload is a pointer to data stored on database page pDbPage.
52037** If argument eOp is false, then nByte bytes of data are copied
52038** from pPayload to the buffer pointed at by pBuf. If eOp is true,
52039** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
52040** of data are copied from the buffer pBuf to pPayload.
52041**
52042** SQLITE_OK is returned on success, otherwise an error code.
52043*/
52044static int copyPayload(
52045  void *pPayload,           /* Pointer to page data */
52046  void *pBuf,               /* Pointer to buffer */
52047  int nByte,                /* Number of bytes to copy */
52048  int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
52049  DbPage *pDbPage           /* Page containing pPayload */
52050){
52051  if( eOp ){
52052    /* Copy data from buffer to page (a write operation) */
52053    int rc = sqlite3PagerWrite(pDbPage);
52054    if( rc!=SQLITE_OK ){
52055      return rc;
52056    }
52057    memcpy(pPayload, pBuf, nByte);
52058  }else{
52059    /* Copy data from page to buffer (a read operation) */
52060    memcpy(pBuf, pPayload, nByte);
52061  }
52062  return SQLITE_OK;
52063}
52064
52065/*
52066** This function is used to read or overwrite payload information
52067** for the entry that the pCur cursor is pointing to. If the eOp
52068** parameter is 0, this is a read operation (data copied into
52069** buffer pBuf). If it is non-zero, a write (data copied from
52070** buffer pBuf).
52071**
52072** A total of "amt" bytes are read or written beginning at "offset".
52073** Data is read to or from the buffer pBuf.
52074**
52075** The content being read or written might appear on the main page
52076** or be scattered out on multiple overflow pages.
52077**
52078** If the BtCursor.isIncrblobHandle flag is set, and the current
52079** cursor entry uses one or more overflow pages, this function
52080** allocates space for and lazily popluates the overflow page-list
52081** cache array (BtCursor.aOverflow). Subsequent calls use this
52082** cache to make seeking to the supplied offset more efficient.
52083**
52084** Once an overflow page-list cache has been allocated, it may be
52085** invalidated if some other cursor writes to the same table, or if
52086** the cursor is moved to a different row. Additionally, in auto-vacuum
52087** mode, the following events may invalidate an overflow page-list cache.
52088**
52089**   * An incremental vacuum,
52090**   * A commit in auto_vacuum="full" mode,
52091**   * Creating a table (may require moving an overflow page).
52092*/
52093static int accessPayload(
52094  BtCursor *pCur,      /* Cursor pointing to entry to read from */
52095  u32 offset,          /* Begin reading this far into payload */
52096  u32 amt,             /* Read this many bytes */
52097  unsigned char *pBuf, /* Write the bytes into this buffer */
52098  int eOp              /* zero to read. non-zero to write. */
52099){
52100  unsigned char *aPayload;
52101  int rc = SQLITE_OK;
52102  u32 nKey;
52103  int iIdx = 0;
52104  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
52105  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
52106
52107  assert( pPage );
52108  assert( pCur->eState==CURSOR_VALID );
52109  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
52110  assert( cursorHoldsMutex(pCur) );
52111
52112  getCellInfo(pCur);
52113  aPayload = pCur->info.pCell + pCur->info.nHeader;
52114  nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
52115
52116  if( NEVER(offset+amt > nKey+pCur->info.nData)
52117   || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
52118  ){
52119    /* Trying to read or write past the end of the data is an error */
52120    return SQLITE_CORRUPT_BKPT;
52121  }
52122
52123  /* Check if data must be read/written to/from the btree page itself. */
52124  if( offset<pCur->info.nLocal ){
52125    int a = amt;
52126    if( a+offset>pCur->info.nLocal ){
52127      a = pCur->info.nLocal - offset;
52128    }
52129    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
52130    offset = 0;
52131    pBuf += a;
52132    amt -= a;
52133  }else{
52134    offset -= pCur->info.nLocal;
52135  }
52136
52137  if( rc==SQLITE_OK && amt>0 ){
52138    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
52139    Pgno nextPage;
52140
52141    nextPage = get4byte(&aPayload[pCur->info.nLocal]);
52142
52143#ifndef SQLITE_OMIT_INCRBLOB
52144    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
52145    ** has not been allocated, allocate it now. The array is sized at
52146    ** one entry for each overflow page in the overflow chain. The
52147    ** page number of the first overflow page is stored in aOverflow[0],
52148    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
52149    ** (the cache is lazily populated).
52150    */
52151    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
52152      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
52153      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
52154      /* nOvfl is always positive.  If it were zero, fetchPayload would have
52155      ** been used instead of this routine. */
52156      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
52157        rc = SQLITE_NOMEM;
52158      }
52159    }
52160
52161    /* If the overflow page-list cache has been allocated and the
52162    ** entry for the first required overflow page is valid, skip
52163    ** directly to it.
52164    */
52165    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
52166      iIdx = (offset/ovflSize);
52167      nextPage = pCur->aOverflow[iIdx];
52168      offset = (offset%ovflSize);
52169    }
52170#endif
52171
52172    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
52173
52174#ifndef SQLITE_OMIT_INCRBLOB
52175      /* If required, populate the overflow page-list cache. */
52176      if( pCur->aOverflow ){
52177        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
52178        pCur->aOverflow[iIdx] = nextPage;
52179      }
52180#endif
52181
52182      if( offset>=ovflSize ){
52183        /* The only reason to read this page is to obtain the page
52184        ** number for the next page in the overflow chain. The page
52185        ** data is not required. So first try to lookup the overflow
52186        ** page-list cache, if any, then fall back to the getOverflowPage()
52187        ** function.
52188        */
52189#ifndef SQLITE_OMIT_INCRBLOB
52190        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
52191          nextPage = pCur->aOverflow[iIdx+1];
52192        } else
52193#endif
52194          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
52195        offset -= ovflSize;
52196      }else{
52197        /* Need to read this page properly. It contains some of the
52198        ** range of data that is being read (eOp==0) or written (eOp!=0).
52199        */
52200        DbPage *pDbPage;
52201        int a = amt;
52202        rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
52203        if( rc==SQLITE_OK ){
52204          aPayload = sqlite3PagerGetData(pDbPage);
52205          nextPage = get4byte(aPayload);
52206          if( a + offset > ovflSize ){
52207            a = ovflSize - offset;
52208          }
52209          rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52210          sqlite3PagerUnref(pDbPage);
52211          offset = 0;
52212          amt -= a;
52213          pBuf += a;
52214        }
52215      }
52216    }
52217  }
52218
52219  if( rc==SQLITE_OK && amt>0 ){
52220    return SQLITE_CORRUPT_BKPT;
52221  }
52222  return rc;
52223}
52224
52225/*
52226** Read part of the key associated with cursor pCur.  Exactly
52227** "amt" bytes will be transfered into pBuf[].  The transfer
52228** begins at "offset".
52229**
52230** The caller must ensure that pCur is pointing to a valid row
52231** in the table.
52232**
52233** Return SQLITE_OK on success or an error code if anything goes
52234** wrong.  An error is returned if "offset+amt" is larger than
52235** the available payload.
52236*/
52237SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
52238  assert( cursorHoldsMutex(pCur) );
52239  assert( pCur->eState==CURSOR_VALID );
52240  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
52241  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
52242  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
52243}
52244
52245/*
52246** Read part of the data associated with cursor pCur.  Exactly
52247** "amt" bytes will be transfered into pBuf[].  The transfer
52248** begins at "offset".
52249**
52250** Return SQLITE_OK on success or an error code if anything goes
52251** wrong.  An error is returned if "offset+amt" is larger than
52252** the available payload.
52253*/
52254SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
52255  int rc;
52256
52257#ifndef SQLITE_OMIT_INCRBLOB
52258  if ( pCur->eState==CURSOR_INVALID ){
52259    return SQLITE_ABORT;
52260  }
52261#endif
52262
52263  assert( cursorHoldsMutex(pCur) );
52264  rc = restoreCursorPosition(pCur);
52265  if( rc==SQLITE_OK ){
52266    assert( pCur->eState==CURSOR_VALID );
52267    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
52268    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
52269    rc = accessPayload(pCur, offset, amt, pBuf, 0);
52270  }
52271  return rc;
52272}
52273
52274/*
52275** Return a pointer to payload information from the entry that the
52276** pCur cursor is pointing to.  The pointer is to the beginning of
52277** the key if skipKey==0 and it points to the beginning of data if
52278** skipKey==1.  The number of bytes of available key/data is written
52279** into *pAmt.  If *pAmt==0, then the value returned will not be
52280** a valid pointer.
52281**
52282** This routine is an optimization.  It is common for the entire key
52283** and data to fit on the local page and for there to be no overflow
52284** pages.  When that is so, this routine can be used to access the
52285** key and data without making a copy.  If the key and/or data spills
52286** onto overflow pages, then accessPayload() must be used to reassemble
52287** the key/data and copy it into a preallocated buffer.
52288**
52289** The pointer returned by this routine looks directly into the cached
52290** page of the database.  The data might change or move the next time
52291** any btree routine is called.
52292*/
52293static const unsigned char *fetchPayload(
52294  BtCursor *pCur,      /* Cursor pointing to entry to read from */
52295  int *pAmt,           /* Write the number of available bytes here */
52296  int skipKey          /* read beginning at data if this is true */
52297){
52298  unsigned char *aPayload;
52299  MemPage *pPage;
52300  u32 nKey;
52301  u32 nLocal;
52302
52303  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
52304  assert( pCur->eState==CURSOR_VALID );
52305  assert( cursorHoldsMutex(pCur) );
52306  pPage = pCur->apPage[pCur->iPage];
52307  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
52308  if( NEVER(pCur->info.nSize==0) ){
52309    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
52310                   &pCur->info);
52311  }
52312  aPayload = pCur->info.pCell;
52313  aPayload += pCur->info.nHeader;
52314  if( pPage->intKey ){
52315    nKey = 0;
52316  }else{
52317    nKey = (int)pCur->info.nKey;
52318  }
52319  if( skipKey ){
52320    aPayload += nKey;
52321    nLocal = pCur->info.nLocal - nKey;
52322  }else{
52323    nLocal = pCur->info.nLocal;
52324    assert( nLocal<=nKey );
52325  }
52326  *pAmt = nLocal;
52327  return aPayload;
52328}
52329
52330
52331/*
52332** For the entry that cursor pCur is point to, return as
52333** many bytes of the key or data as are available on the local
52334** b-tree page.  Write the number of available bytes into *pAmt.
52335**
52336** The pointer returned is ephemeral.  The key/data may move
52337** or be destroyed on the next call to any Btree routine,
52338** including calls from other threads against the same cache.
52339** Hence, a mutex on the BtShared should be held prior to calling
52340** this routine.
52341**
52342** These routines is used to get quick access to key and data
52343** in the common case where no overflow pages are used.
52344*/
52345SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
52346  const void *p = 0;
52347  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52348  assert( cursorHoldsMutex(pCur) );
52349  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
52350    p = (const void*)fetchPayload(pCur, pAmt, 0);
52351  }
52352  return p;
52353}
52354SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
52355  const void *p = 0;
52356  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52357  assert( cursorHoldsMutex(pCur) );
52358  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
52359    p = (const void*)fetchPayload(pCur, pAmt, 1);
52360  }
52361  return p;
52362}
52363
52364
52365/*
52366** Move the cursor down to a new child page.  The newPgno argument is the
52367** page number of the child page to move to.
52368**
52369** This function returns SQLITE_CORRUPT if the page-header flags field of
52370** the new child page does not match the flags field of the parent (i.e.
52371** if an intkey page appears to be the parent of a non-intkey page, or
52372** vice-versa).
52373*/
52374static int moveToChild(BtCursor *pCur, u32 newPgno){
52375  int rc;
52376  int i = pCur->iPage;
52377  MemPage *pNewPage;
52378  BtShared *pBt = pCur->pBt;
52379
52380  assert( cursorHoldsMutex(pCur) );
52381  assert( pCur->eState==CURSOR_VALID );
52382  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
52383  if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
52384    return SQLITE_CORRUPT_BKPT;
52385  }
52386  rc = getAndInitPage(pBt, newPgno, &pNewPage);
52387  if( rc ) return rc;
52388  pCur->apPage[i+1] = pNewPage;
52389  pCur->aiIdx[i+1] = 0;
52390  pCur->iPage++;
52391
52392  pCur->info.nSize = 0;
52393  pCur->validNKey = 0;
52394  if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
52395    return SQLITE_CORRUPT_BKPT;
52396  }
52397  return SQLITE_OK;
52398}
52399
52400#ifndef NDEBUG
52401/*
52402** Page pParent is an internal (non-leaf) tree page. This function
52403** asserts that page number iChild is the left-child if the iIdx'th
52404** cell in page pParent. Or, if iIdx is equal to the total number of
52405** cells in pParent, that page number iChild is the right-child of
52406** the page.
52407*/
52408static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
52409  assert( iIdx<=pParent->nCell );
52410  if( iIdx==pParent->nCell ){
52411    assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
52412  }else{
52413    assert( get4byte(findCell(pParent, iIdx))==iChild );
52414  }
52415}
52416#else
52417#  define assertParentIndex(x,y,z)
52418#endif
52419
52420/*
52421** Move the cursor up to the parent page.
52422**
52423** pCur->idx is set to the cell index that contains the pointer
52424** to the page we are coming from.  If we are coming from the
52425** right-most child page then pCur->idx is set to one more than
52426** the largest cell index.
52427*/
52428static void moveToParent(BtCursor *pCur){
52429  assert( cursorHoldsMutex(pCur) );
52430  assert( pCur->eState==CURSOR_VALID );
52431  assert( pCur->iPage>0 );
52432  assert( pCur->apPage[pCur->iPage] );
52433  assertParentIndex(
52434    pCur->apPage[pCur->iPage-1],
52435    pCur->aiIdx[pCur->iPage-1],
52436    pCur->apPage[pCur->iPage]->pgno
52437  );
52438  releasePage(pCur->apPage[pCur->iPage]);
52439  pCur->iPage--;
52440  pCur->info.nSize = 0;
52441  pCur->validNKey = 0;
52442}
52443
52444/*
52445** Move the cursor to point to the root page of its b-tree structure.
52446**
52447** If the table has a virtual root page, then the cursor is moved to point
52448** to the virtual root page instead of the actual root page. A table has a
52449** virtual root page when the actual root page contains no cells and a
52450** single child page. This can only happen with the table rooted at page 1.
52451**
52452** If the b-tree structure is empty, the cursor state is set to
52453** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
52454** cell located on the root (or virtual root) page and the cursor state
52455** is set to CURSOR_VALID.
52456**
52457** If this function returns successfully, it may be assumed that the
52458** page-header flags indicate that the [virtual] root-page is the expected
52459** kind of b-tree page (i.e. if when opening the cursor the caller did not
52460** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
52461** indicating a table b-tree, or if the caller did specify a KeyInfo
52462** structure the flags byte is set to 0x02 or 0x0A, indicating an index
52463** b-tree).
52464*/
52465static int moveToRoot(BtCursor *pCur){
52466  MemPage *pRoot;
52467  int rc = SQLITE_OK;
52468  Btree *p = pCur->pBtree;
52469  BtShared *pBt = p->pBt;
52470
52471  assert( cursorHoldsMutex(pCur) );
52472  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
52473  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
52474  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
52475  if( pCur->eState>=CURSOR_REQUIRESEEK ){
52476    if( pCur->eState==CURSOR_FAULT ){
52477      assert( pCur->skipNext!=SQLITE_OK );
52478      return pCur->skipNext;
52479    }
52480    sqlite3BtreeClearCursor(pCur);
52481  }
52482
52483  if( pCur->iPage>=0 ){
52484    int i;
52485    for(i=1; i<=pCur->iPage; i++){
52486      releasePage(pCur->apPage[i]);
52487    }
52488    pCur->iPage = 0;
52489  }else if( pCur->pgnoRoot==0 ){
52490    pCur->eState = CURSOR_INVALID;
52491    return SQLITE_OK;
52492  }else{
52493    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
52494    if( rc!=SQLITE_OK ){
52495      pCur->eState = CURSOR_INVALID;
52496      return rc;
52497    }
52498    pCur->iPage = 0;
52499
52500    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
52501    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
52502    ** NULL, the caller expects a table b-tree. If this is not the case,
52503    ** return an SQLITE_CORRUPT error.  */
52504    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
52505    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
52506      return SQLITE_CORRUPT_BKPT;
52507    }
52508  }
52509
52510  /* Assert that the root page is of the correct type. This must be the
52511  ** case as the call to this function that loaded the root-page (either
52512  ** this call or a previous invocation) would have detected corruption
52513  ** if the assumption were not true, and it is not possible for the flags
52514  ** byte to have been modified while this cursor is holding a reference
52515  ** to the page.  */
52516  pRoot = pCur->apPage[0];
52517  assert( pRoot->pgno==pCur->pgnoRoot );
52518  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
52519
52520  pCur->aiIdx[0] = 0;
52521  pCur->info.nSize = 0;
52522  pCur->atLast = 0;
52523  pCur->validNKey = 0;
52524
52525  if( pRoot->nCell==0 && !pRoot->leaf ){
52526    Pgno subpage;
52527    if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
52528    subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
52529    pCur->eState = CURSOR_VALID;
52530    rc = moveToChild(pCur, subpage);
52531  }else{
52532    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
52533  }
52534  return rc;
52535}
52536
52537/*
52538** Move the cursor down to the left-most leaf entry beneath the
52539** entry to which it is currently pointing.
52540**
52541** The left-most leaf is the one with the smallest key - the first
52542** in ascending order.
52543*/
52544static int moveToLeftmost(BtCursor *pCur){
52545  Pgno pgno;
52546  int rc = SQLITE_OK;
52547  MemPage *pPage;
52548
52549  assert( cursorHoldsMutex(pCur) );
52550  assert( pCur->eState==CURSOR_VALID );
52551  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
52552    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
52553    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
52554    rc = moveToChild(pCur, pgno);
52555  }
52556  return rc;
52557}
52558
52559/*
52560** Move the cursor down to the right-most leaf entry beneath the
52561** page to which it is currently pointing.  Notice the difference
52562** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
52563** finds the left-most entry beneath the *entry* whereas moveToRightmost()
52564** finds the right-most entry beneath the *page*.
52565**
52566** The right-most entry is the one with the largest key - the last
52567** key in ascending order.
52568*/
52569static int moveToRightmost(BtCursor *pCur){
52570  Pgno pgno;
52571  int rc = SQLITE_OK;
52572  MemPage *pPage = 0;
52573
52574  assert( cursorHoldsMutex(pCur) );
52575  assert( pCur->eState==CURSOR_VALID );
52576  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
52577    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
52578    pCur->aiIdx[pCur->iPage] = pPage->nCell;
52579    rc = moveToChild(pCur, pgno);
52580  }
52581  if( rc==SQLITE_OK ){
52582    pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
52583    pCur->info.nSize = 0;
52584    pCur->validNKey = 0;
52585  }
52586  return rc;
52587}
52588
52589/* Move the cursor to the first entry in the table.  Return SQLITE_OK
52590** on success.  Set *pRes to 0 if the cursor actually points to something
52591** or set *pRes to 1 if the table is empty.
52592*/
52593SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
52594  int rc;
52595
52596  assert( cursorHoldsMutex(pCur) );
52597  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52598  rc = moveToRoot(pCur);
52599  if( rc==SQLITE_OK ){
52600    if( pCur->eState==CURSOR_INVALID ){
52601      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52602      *pRes = 1;
52603    }else{
52604      assert( pCur->apPage[pCur->iPage]->nCell>0 );
52605      *pRes = 0;
52606      rc = moveToLeftmost(pCur);
52607    }
52608  }
52609  return rc;
52610}
52611
52612/* Move the cursor to the last entry in the table.  Return SQLITE_OK
52613** on success.  Set *pRes to 0 if the cursor actually points to something
52614** or set *pRes to 1 if the table is empty.
52615*/
52616SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
52617  int rc;
52618
52619  assert( cursorHoldsMutex(pCur) );
52620  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52621
52622  /* If the cursor already points to the last entry, this is a no-op. */
52623  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
52624#ifdef SQLITE_DEBUG
52625    /* This block serves to assert() that the cursor really does point
52626    ** to the last entry in the b-tree. */
52627    int ii;
52628    for(ii=0; ii<pCur->iPage; ii++){
52629      assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
52630    }
52631    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
52632    assert( pCur->apPage[pCur->iPage]->leaf );
52633#endif
52634    return SQLITE_OK;
52635  }
52636
52637  rc = moveToRoot(pCur);
52638  if( rc==SQLITE_OK ){
52639    if( CURSOR_INVALID==pCur->eState ){
52640      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52641      *pRes = 1;
52642    }else{
52643      assert( pCur->eState==CURSOR_VALID );
52644      *pRes = 0;
52645      rc = moveToRightmost(pCur);
52646      pCur->atLast = rc==SQLITE_OK ?1:0;
52647    }
52648  }
52649  return rc;
52650}
52651
52652/* Move the cursor so that it points to an entry near the key
52653** specified by pIdxKey or intKey.   Return a success code.
52654**
52655** For INTKEY tables, the intKey parameter is used.  pIdxKey
52656** must be NULL.  For index tables, pIdxKey is used and intKey
52657** is ignored.
52658**
52659** If an exact match is not found, then the cursor is always
52660** left pointing at a leaf page which would hold the entry if it
52661** were present.  The cursor might point to an entry that comes
52662** before or after the key.
52663**
52664** An integer is written into *pRes which is the result of
52665** comparing the key with the entry to which the cursor is
52666** pointing.  The meaning of the integer written into
52667** *pRes is as follows:
52668**
52669**     *pRes<0      The cursor is left pointing at an entry that
52670**                  is smaller than intKey/pIdxKey or if the table is empty
52671**                  and the cursor is therefore left point to nothing.
52672**
52673**     *pRes==0     The cursor is left pointing at an entry that
52674**                  exactly matches intKey/pIdxKey.
52675**
52676**     *pRes>0      The cursor is left pointing at an entry that
52677**                  is larger than intKey/pIdxKey.
52678**
52679*/
52680SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
52681  BtCursor *pCur,          /* The cursor to be moved */
52682  UnpackedRecord *pIdxKey, /* Unpacked index key */
52683  i64 intKey,              /* The table key */
52684  int biasRight,           /* If true, bias the search to the high end */
52685  int *pRes                /* Write search results here */
52686){
52687  int rc;
52688
52689  assert( cursorHoldsMutex(pCur) );
52690  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52691  assert( pRes );
52692  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
52693
52694  /* If the cursor is already positioned at the point we are trying
52695  ** to move to, then just return without doing any work */
52696  if( pCur->eState==CURSOR_VALID && pCur->validNKey
52697   && pCur->apPage[0]->intKey
52698  ){
52699    if( pCur->info.nKey==intKey ){
52700      *pRes = 0;
52701      return SQLITE_OK;
52702    }
52703    if( pCur->atLast && pCur->info.nKey<intKey ){
52704      *pRes = -1;
52705      return SQLITE_OK;
52706    }
52707  }
52708
52709  rc = moveToRoot(pCur);
52710  if( rc ){
52711    return rc;
52712  }
52713  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
52714  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
52715  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
52716  if( pCur->eState==CURSOR_INVALID ){
52717    *pRes = -1;
52718    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52719    return SQLITE_OK;
52720  }
52721  assert( pCur->apPage[0]->intKey || pIdxKey );
52722  for(;;){
52723    int lwr, upr, idx;
52724    Pgno chldPg;
52725    MemPage *pPage = pCur->apPage[pCur->iPage];
52726    int c;
52727
52728    /* pPage->nCell must be greater than zero. If this is the root-page
52729    ** the cursor would have been INVALID above and this for(;;) loop
52730    ** not run. If this is not the root-page, then the moveToChild() routine
52731    ** would have already detected db corruption. Similarly, pPage must
52732    ** be the right kind (index or table) of b-tree page. Otherwise
52733    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
52734    assert( pPage->nCell>0 );
52735    assert( pPage->intKey==(pIdxKey==0) );
52736    lwr = 0;
52737    upr = pPage->nCell-1;
52738    if( biasRight ){
52739      pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
52740    }else{
52741      pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
52742    }
52743    for(;;){
52744      u8 *pCell;                          /* Pointer to current cell in pPage */
52745
52746      assert( idx==pCur->aiIdx[pCur->iPage] );
52747      pCur->info.nSize = 0;
52748      pCell = findCell(pPage, idx) + pPage->childPtrSize;
52749      if( pPage->intKey ){
52750        i64 nCellKey;
52751        if( pPage->hasData ){
52752          u32 dummy;
52753          pCell += getVarint32(pCell, dummy);
52754        }
52755        getVarint(pCell, (u64*)&nCellKey);
52756        if( nCellKey==intKey ){
52757          c = 0;
52758        }else if( nCellKey<intKey ){
52759          c = -1;
52760        }else{
52761          assert( nCellKey>intKey );
52762          c = +1;
52763        }
52764        pCur->validNKey = 1;
52765        pCur->info.nKey = nCellKey;
52766      }else{
52767        /* The maximum supported page-size is 65536 bytes. This means that
52768        ** the maximum number of record bytes stored on an index B-Tree
52769        ** page is less than 16384 bytes and may be stored as a 2-byte
52770        ** varint. This information is used to attempt to avoid parsing
52771        ** the entire cell by checking for the cases where the record is
52772        ** stored entirely within the b-tree page by inspecting the first
52773        ** 2 bytes of the cell.
52774        */
52775        int nCell = pCell[0];
52776        if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
52777          /* This branch runs if the record-size field of the cell is a
52778          ** single byte varint and the record fits entirely on the main
52779          ** b-tree page.  */
52780          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
52781        }else if( !(pCell[1] & 0x80)
52782          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
52783        ){
52784          /* The record-size field is a 2 byte varint and the record
52785          ** fits entirely on the main b-tree page.  */
52786          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
52787        }else{
52788          /* The record flows over onto one or more overflow pages. In
52789          ** this case the whole cell needs to be parsed, a buffer allocated
52790          ** and accessPayload() used to retrieve the record into the
52791          ** buffer before VdbeRecordCompare() can be called. */
52792          void *pCellKey;
52793          u8 * const pCellBody = pCell - pPage->childPtrSize;
52794          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
52795          nCell = (int)pCur->info.nKey;
52796          pCellKey = sqlite3Malloc( nCell );
52797          if( pCellKey==0 ){
52798            rc = SQLITE_NOMEM;
52799            goto moveto_finish;
52800          }
52801          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
52802          if( rc ){
52803            sqlite3_free(pCellKey);
52804            goto moveto_finish;
52805          }
52806          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
52807          sqlite3_free(pCellKey);
52808        }
52809      }
52810      if( c==0 ){
52811        if( pPage->intKey && !pPage->leaf ){
52812          lwr = idx;
52813          upr = lwr - 1;
52814          break;
52815        }else{
52816          *pRes = 0;
52817          rc = SQLITE_OK;
52818          goto moveto_finish;
52819        }
52820      }
52821      if( c<0 ){
52822        lwr = idx+1;
52823      }else{
52824        upr = idx-1;
52825      }
52826      if( lwr>upr ){
52827        break;
52828      }
52829      pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
52830    }
52831    assert( lwr==upr+1 );
52832    assert( pPage->isInit );
52833    if( pPage->leaf ){
52834      chldPg = 0;
52835    }else if( lwr>=pPage->nCell ){
52836      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
52837    }else{
52838      chldPg = get4byte(findCell(pPage, lwr));
52839    }
52840    if( chldPg==0 ){
52841      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
52842      *pRes = c;
52843      rc = SQLITE_OK;
52844      goto moveto_finish;
52845    }
52846    pCur->aiIdx[pCur->iPage] = (u16)lwr;
52847    pCur->info.nSize = 0;
52848    pCur->validNKey = 0;
52849    rc = moveToChild(pCur, chldPg);
52850    if( rc ) goto moveto_finish;
52851  }
52852moveto_finish:
52853  return rc;
52854}
52855
52856
52857/*
52858** Return TRUE if the cursor is not pointing at an entry of the table.
52859**
52860** TRUE will be returned after a call to sqlite3BtreeNext() moves
52861** past the last entry in the table or sqlite3BtreePrev() moves past
52862** the first entry.  TRUE is also returned if the table is empty.
52863*/
52864SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
52865  /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
52866  ** have been deleted? This API will need to change to return an error code
52867  ** as well as the boolean result value.
52868  */
52869  return (CURSOR_VALID!=pCur->eState);
52870}
52871
52872/*
52873** Advance the cursor to the next entry in the database.  If
52874** successful then set *pRes=0.  If the cursor
52875** was already pointing to the last entry in the database before
52876** this routine was called, then set *pRes=1.
52877*/
52878SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
52879  int rc;
52880  int idx;
52881  MemPage *pPage;
52882
52883  assert( cursorHoldsMutex(pCur) );
52884  rc = restoreCursorPosition(pCur);
52885  if( rc!=SQLITE_OK ){
52886    return rc;
52887  }
52888  assert( pRes!=0 );
52889  if( CURSOR_INVALID==pCur->eState ){
52890    *pRes = 1;
52891    return SQLITE_OK;
52892  }
52893  if( pCur->skipNext>0 ){
52894    pCur->skipNext = 0;
52895    *pRes = 0;
52896    return SQLITE_OK;
52897  }
52898  pCur->skipNext = 0;
52899
52900  pPage = pCur->apPage[pCur->iPage];
52901  idx = ++pCur->aiIdx[pCur->iPage];
52902  assert( pPage->isInit );
52903  assert( idx<=pPage->nCell );
52904
52905  pCur->info.nSize = 0;
52906  pCur->validNKey = 0;
52907  if( idx>=pPage->nCell ){
52908    if( !pPage->leaf ){
52909      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
52910      if( rc ) return rc;
52911      rc = moveToLeftmost(pCur);
52912      *pRes = 0;
52913      return rc;
52914    }
52915    do{
52916      if( pCur->iPage==0 ){
52917        *pRes = 1;
52918        pCur->eState = CURSOR_INVALID;
52919        return SQLITE_OK;
52920      }
52921      moveToParent(pCur);
52922      pPage = pCur->apPage[pCur->iPage];
52923    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
52924    *pRes = 0;
52925    if( pPage->intKey ){
52926      rc = sqlite3BtreeNext(pCur, pRes);
52927    }else{
52928      rc = SQLITE_OK;
52929    }
52930    return rc;
52931  }
52932  *pRes = 0;
52933  if( pPage->leaf ){
52934    return SQLITE_OK;
52935  }
52936  rc = moveToLeftmost(pCur);
52937  return rc;
52938}
52939
52940
52941/*
52942** Step the cursor to the back to the previous entry in the database.  If
52943** successful then set *pRes=0.  If the cursor
52944** was already pointing to the first entry in the database before
52945** this routine was called, then set *pRes=1.
52946*/
52947SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
52948  int rc;
52949  MemPage *pPage;
52950
52951  assert( cursorHoldsMutex(pCur) );
52952  rc = restoreCursorPosition(pCur);
52953  if( rc!=SQLITE_OK ){
52954    return rc;
52955  }
52956  pCur->atLast = 0;
52957  if( CURSOR_INVALID==pCur->eState ){
52958    *pRes = 1;
52959    return SQLITE_OK;
52960  }
52961  if( pCur->skipNext<0 ){
52962    pCur->skipNext = 0;
52963    *pRes = 0;
52964    return SQLITE_OK;
52965  }
52966  pCur->skipNext = 0;
52967
52968  pPage = pCur->apPage[pCur->iPage];
52969  assert( pPage->isInit );
52970  if( !pPage->leaf ){
52971    int idx = pCur->aiIdx[pCur->iPage];
52972    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
52973    if( rc ){
52974      return rc;
52975    }
52976    rc = moveToRightmost(pCur);
52977  }else{
52978    while( pCur->aiIdx[pCur->iPage]==0 ){
52979      if( pCur->iPage==0 ){
52980        pCur->eState = CURSOR_INVALID;
52981        *pRes = 1;
52982        return SQLITE_OK;
52983      }
52984      moveToParent(pCur);
52985    }
52986    pCur->info.nSize = 0;
52987    pCur->validNKey = 0;
52988
52989    pCur->aiIdx[pCur->iPage]--;
52990    pPage = pCur->apPage[pCur->iPage];
52991    if( pPage->intKey && !pPage->leaf ){
52992      rc = sqlite3BtreePrevious(pCur, pRes);
52993    }else{
52994      rc = SQLITE_OK;
52995    }
52996  }
52997  *pRes = 0;
52998  return rc;
52999}
53000
53001/*
53002** Allocate a new page from the database file.
53003**
53004** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
53005** has already been called on the new page.)  The new page has also
53006** been referenced and the calling routine is responsible for calling
53007** sqlite3PagerUnref() on the new page when it is done.
53008**
53009** SQLITE_OK is returned on success.  Any other return value indicates
53010** an error.  *ppPage and *pPgno are undefined in the event of an error.
53011** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
53012**
53013** If the "nearby" parameter is not 0, then a (feeble) effort is made to
53014** locate a page close to the page number "nearby".  This can be used in an
53015** attempt to keep related pages close to each other in the database file,
53016** which in turn can make database access faster.
53017**
53018** If the "exact" parameter is not 0, and the page-number nearby exists
53019** anywhere on the free-list, then it is guarenteed to be returned. This
53020** is only used by auto-vacuum databases when allocating a new table.
53021*/
53022static int allocateBtreePage(
53023  BtShared *pBt,
53024  MemPage **ppPage,
53025  Pgno *pPgno,
53026  Pgno nearby,
53027  u8 exact
53028){
53029  MemPage *pPage1;
53030  int rc;
53031  u32 n;     /* Number of pages on the freelist */
53032  u32 k;     /* Number of leaves on the trunk of the freelist */
53033  MemPage *pTrunk = 0;
53034  MemPage *pPrevTrunk = 0;
53035  Pgno mxPage;     /* Total size of the database file */
53036
53037  assert( sqlite3_mutex_held(pBt->mutex) );
53038  pPage1 = pBt->pPage1;
53039  mxPage = btreePagecount(pBt);
53040  n = get4byte(&pPage1->aData[36]);
53041  testcase( n==mxPage-1 );
53042  if( n>=mxPage ){
53043    return SQLITE_CORRUPT_BKPT;
53044  }
53045  if( n>0 ){
53046    /* There are pages on the freelist.  Reuse one of those pages. */
53047    Pgno iTrunk;
53048    u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
53049
53050    /* If the 'exact' parameter was true and a query of the pointer-map
53051    ** shows that the page 'nearby' is somewhere on the free-list, then
53052    ** the entire-list will be searched for that page.
53053    */
53054#ifndef SQLITE_OMIT_AUTOVACUUM
53055    if( exact && nearby<=mxPage ){
53056      u8 eType;
53057      assert( nearby>0 );
53058      assert( pBt->autoVacuum );
53059      rc = ptrmapGet(pBt, nearby, &eType, 0);
53060      if( rc ) return rc;
53061      if( eType==PTRMAP_FREEPAGE ){
53062        searchList = 1;
53063      }
53064      *pPgno = nearby;
53065    }
53066#endif
53067
53068    /* Decrement the free-list count by 1. Set iTrunk to the index of the
53069    ** first free-list trunk page. iPrevTrunk is initially 1.
53070    */
53071    rc = sqlite3PagerWrite(pPage1->pDbPage);
53072    if( rc ) return rc;
53073    put4byte(&pPage1->aData[36], n-1);
53074
53075    /* The code within this loop is run only once if the 'searchList' variable
53076    ** is not true. Otherwise, it runs once for each trunk-page on the
53077    ** free-list until the page 'nearby' is located.
53078    */
53079    do {
53080      pPrevTrunk = pTrunk;
53081      if( pPrevTrunk ){
53082        iTrunk = get4byte(&pPrevTrunk->aData[0]);
53083      }else{
53084        iTrunk = get4byte(&pPage1->aData[32]);
53085      }
53086      testcase( iTrunk==mxPage );
53087      if( iTrunk>mxPage ){
53088        rc = SQLITE_CORRUPT_BKPT;
53089      }else{
53090        rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
53091      }
53092      if( rc ){
53093        pTrunk = 0;
53094        goto end_allocate_page;
53095      }
53096
53097      k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
53098      if( k==0 && !searchList ){
53099        /* The trunk has no leaves and the list is not being searched.
53100        ** So extract the trunk page itself and use it as the newly
53101        ** allocated page */
53102        assert( pPrevTrunk==0 );
53103        rc = sqlite3PagerWrite(pTrunk->pDbPage);
53104        if( rc ){
53105          goto end_allocate_page;
53106        }
53107        *pPgno = iTrunk;
53108        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
53109        *ppPage = pTrunk;
53110        pTrunk = 0;
53111        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
53112      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
53113        /* Value of k is out of range.  Database corruption */
53114        rc = SQLITE_CORRUPT_BKPT;
53115        goto end_allocate_page;
53116#ifndef SQLITE_OMIT_AUTOVACUUM
53117      }else if( searchList && nearby==iTrunk ){
53118        /* The list is being searched and this trunk page is the page
53119        ** to allocate, regardless of whether it has leaves.
53120        */
53121        assert( *pPgno==iTrunk );
53122        *ppPage = pTrunk;
53123        searchList = 0;
53124        rc = sqlite3PagerWrite(pTrunk->pDbPage);
53125        if( rc ){
53126          goto end_allocate_page;
53127        }
53128        if( k==0 ){
53129          if( !pPrevTrunk ){
53130            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
53131          }else{
53132            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
53133            if( rc!=SQLITE_OK ){
53134              goto end_allocate_page;
53135            }
53136            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
53137          }
53138        }else{
53139          /* The trunk page is required by the caller but it contains
53140          ** pointers to free-list leaves. The first leaf becomes a trunk
53141          ** page in this case.
53142          */
53143          MemPage *pNewTrunk;
53144          Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
53145          if( iNewTrunk>mxPage ){
53146            rc = SQLITE_CORRUPT_BKPT;
53147            goto end_allocate_page;
53148          }
53149          testcase( iNewTrunk==mxPage );
53150          rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
53151          if( rc!=SQLITE_OK ){
53152            goto end_allocate_page;
53153          }
53154          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
53155          if( rc!=SQLITE_OK ){
53156            releasePage(pNewTrunk);
53157            goto end_allocate_page;
53158          }
53159          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
53160          put4byte(&pNewTrunk->aData[4], k-1);
53161          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
53162          releasePage(pNewTrunk);
53163          if( !pPrevTrunk ){
53164            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
53165            put4byte(&pPage1->aData[32], iNewTrunk);
53166          }else{
53167            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
53168            if( rc ){
53169              goto end_allocate_page;
53170            }
53171            put4byte(&pPrevTrunk->aData[0], iNewTrunk);
53172          }
53173        }
53174        pTrunk = 0;
53175        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
53176#endif
53177      }else if( k>0 ){
53178        /* Extract a leaf from the trunk */
53179        u32 closest;
53180        Pgno iPage;
53181        unsigned char *aData = pTrunk->aData;
53182        if( nearby>0 ){
53183          u32 i;
53184          int dist;
53185          closest = 0;
53186          dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
53187          for(i=1; i<k; i++){
53188            int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
53189            if( d2<dist ){
53190              closest = i;
53191              dist = d2;
53192            }
53193          }
53194        }else{
53195          closest = 0;
53196        }
53197
53198        iPage = get4byte(&aData[8+closest*4]);
53199        testcase( iPage==mxPage );
53200        if( iPage>mxPage ){
53201          rc = SQLITE_CORRUPT_BKPT;
53202          goto end_allocate_page;
53203        }
53204        testcase( iPage==mxPage );
53205        if( !searchList || iPage==nearby ){
53206          int noContent;
53207          *pPgno = iPage;
53208          TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
53209                 ": %d more free pages\n",
53210                 *pPgno, closest+1, k, pTrunk->pgno, n-1));
53211          rc = sqlite3PagerWrite(pTrunk->pDbPage);
53212          if( rc ) goto end_allocate_page;
53213          if( closest<k-1 ){
53214            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
53215          }
53216          put4byte(&aData[4], k-1);
53217          noContent = !btreeGetHasContent(pBt, *pPgno);
53218          rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
53219          if( rc==SQLITE_OK ){
53220            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
53221            if( rc!=SQLITE_OK ){
53222              releasePage(*ppPage);
53223            }
53224          }
53225          searchList = 0;
53226        }
53227      }
53228      releasePage(pPrevTrunk);
53229      pPrevTrunk = 0;
53230    }while( searchList );
53231  }else{
53232    /* There are no pages on the freelist, so create a new page at the
53233    ** end of the file */
53234    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
53235    if( rc ) return rc;
53236    pBt->nPage++;
53237    if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
53238
53239#ifndef SQLITE_OMIT_AUTOVACUUM
53240    if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
53241      /* If *pPgno refers to a pointer-map page, allocate two new pages
53242      ** at the end of the file instead of one. The first allocated page
53243      ** becomes a new pointer-map page, the second is used by the caller.
53244      */
53245      MemPage *pPg = 0;
53246      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
53247      assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
53248      rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
53249      if( rc==SQLITE_OK ){
53250        rc = sqlite3PagerWrite(pPg->pDbPage);
53251        releasePage(pPg);
53252      }
53253      if( rc ) return rc;
53254      pBt->nPage++;
53255      if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
53256    }
53257#endif
53258    put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
53259    *pPgno = pBt->nPage;
53260
53261    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
53262    rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
53263    if( rc ) return rc;
53264    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
53265    if( rc!=SQLITE_OK ){
53266      releasePage(*ppPage);
53267    }
53268    TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
53269  }
53270
53271  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
53272
53273end_allocate_page:
53274  releasePage(pTrunk);
53275  releasePage(pPrevTrunk);
53276  if( rc==SQLITE_OK ){
53277    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
53278      releasePage(*ppPage);
53279      return SQLITE_CORRUPT_BKPT;
53280    }
53281    (*ppPage)->isInit = 0;
53282  }else{
53283    *ppPage = 0;
53284  }
53285  assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
53286  return rc;
53287}
53288
53289/*
53290** This function is used to add page iPage to the database file free-list.
53291** It is assumed that the page is not already a part of the free-list.
53292**
53293** The value passed as the second argument to this function is optional.
53294** If the caller happens to have a pointer to the MemPage object
53295** corresponding to page iPage handy, it may pass it as the second value.
53296** Otherwise, it may pass NULL.
53297**
53298** If a pointer to a MemPage object is passed as the second argument,
53299** its reference count is not altered by this function.
53300*/
53301static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
53302  MemPage *pTrunk = 0;                /* Free-list trunk page */
53303  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
53304  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
53305  MemPage *pPage;                     /* Page being freed. May be NULL. */
53306  int rc;                             /* Return Code */
53307  int nFree;                          /* Initial number of pages on free-list */
53308
53309  assert( sqlite3_mutex_held(pBt->mutex) );
53310  assert( iPage>1 );
53311  assert( !pMemPage || pMemPage->pgno==iPage );
53312
53313  if( pMemPage ){
53314    pPage = pMemPage;
53315    sqlite3PagerRef(pPage->pDbPage);
53316  }else{
53317    pPage = btreePageLookup(pBt, iPage);
53318  }
53319
53320  /* Increment the free page count on pPage1 */
53321  rc = sqlite3PagerWrite(pPage1->pDbPage);
53322  if( rc ) goto freepage_out;
53323  nFree = get4byte(&pPage1->aData[36]);
53324  put4byte(&pPage1->aData[36], nFree+1);
53325
53326  if( pBt->secureDelete ){
53327    /* If the secure_delete option is enabled, then
53328    ** always fully overwrite deleted information with zeros.
53329    */
53330    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
53331     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
53332    ){
53333      goto freepage_out;
53334    }
53335    memset(pPage->aData, 0, pPage->pBt->pageSize);
53336  }
53337
53338  /* If the database supports auto-vacuum, write an entry in the pointer-map
53339  ** to indicate that the page is free.
53340  */
53341  if( ISAUTOVACUUM ){
53342    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
53343    if( rc ) goto freepage_out;
53344  }
53345
53346  /* Now manipulate the actual database free-list structure. There are two
53347  ** possibilities. If the free-list is currently empty, or if the first
53348  ** trunk page in the free-list is full, then this page will become a
53349  ** new free-list trunk page. Otherwise, it will become a leaf of the
53350  ** first trunk page in the current free-list. This block tests if it
53351  ** is possible to add the page as a new free-list leaf.
53352  */
53353  if( nFree!=0 ){
53354    u32 nLeaf;                /* Initial number of leaf cells on trunk page */
53355
53356    iTrunk = get4byte(&pPage1->aData[32]);
53357    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
53358    if( rc!=SQLITE_OK ){
53359      goto freepage_out;
53360    }
53361
53362    nLeaf = get4byte(&pTrunk->aData[4]);
53363    assert( pBt->usableSize>32 );
53364    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
53365      rc = SQLITE_CORRUPT_BKPT;
53366      goto freepage_out;
53367    }
53368    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
53369      /* In this case there is room on the trunk page to insert the page
53370      ** being freed as a new leaf.
53371      **
53372      ** Note that the trunk page is not really full until it contains
53373      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
53374      ** coded.  But due to a coding error in versions of SQLite prior to
53375      ** 3.6.0, databases with freelist trunk pages holding more than
53376      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
53377      ** to maintain backwards compatibility with older versions of SQLite,
53378      ** we will continue to restrict the number of entries to usableSize/4 - 8
53379      ** for now.  At some point in the future (once everyone has upgraded
53380      ** to 3.6.0 or later) we should consider fixing the conditional above
53381      ** to read "usableSize/4-2" instead of "usableSize/4-8".
53382      */
53383      rc = sqlite3PagerWrite(pTrunk->pDbPage);
53384      if( rc==SQLITE_OK ){
53385        put4byte(&pTrunk->aData[4], nLeaf+1);
53386        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
53387        if( pPage && !pBt->secureDelete ){
53388          sqlite3PagerDontWrite(pPage->pDbPage);
53389        }
53390        rc = btreeSetHasContent(pBt, iPage);
53391      }
53392      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
53393      goto freepage_out;
53394    }
53395  }
53396
53397  /* If control flows to this point, then it was not possible to add the
53398  ** the page being freed as a leaf page of the first trunk in the free-list.
53399  ** Possibly because the free-list is empty, or possibly because the
53400  ** first trunk in the free-list is full. Either way, the page being freed
53401  ** will become the new first trunk page in the free-list.
53402  */
53403  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
53404    goto freepage_out;
53405  }
53406  rc = sqlite3PagerWrite(pPage->pDbPage);
53407  if( rc!=SQLITE_OK ){
53408    goto freepage_out;
53409  }
53410  put4byte(pPage->aData, iTrunk);
53411  put4byte(&pPage->aData[4], 0);
53412  put4byte(&pPage1->aData[32], iPage);
53413  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
53414
53415freepage_out:
53416  if( pPage ){
53417    pPage->isInit = 0;
53418  }
53419  releasePage(pPage);
53420  releasePage(pTrunk);
53421  return rc;
53422}
53423static void freePage(MemPage *pPage, int *pRC){
53424  if( (*pRC)==SQLITE_OK ){
53425    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
53426  }
53427}
53428
53429/*
53430** Free any overflow pages associated with the given Cell.
53431*/
53432static int clearCell(MemPage *pPage, unsigned char *pCell){
53433  BtShared *pBt = pPage->pBt;
53434  CellInfo info;
53435  Pgno ovflPgno;
53436  int rc;
53437  int nOvfl;
53438  u32 ovflPageSize;
53439
53440  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53441  btreeParseCellPtr(pPage, pCell, &info);
53442  if( info.iOverflow==0 ){
53443    return SQLITE_OK;  /* No overflow pages. Return without doing anything */
53444  }
53445  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53446    return SQLITE_CORRUPT;  /* Cell extends past end of page */
53447  }
53448  ovflPgno = get4byte(&pCell[info.iOverflow]);
53449  assert( pBt->usableSize > 4 );
53450  ovflPageSize = pBt->usableSize - 4;
53451  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
53452  assert( ovflPgno==0 || nOvfl>0 );
53453  while( nOvfl-- ){
53454    Pgno iNext = 0;
53455    MemPage *pOvfl = 0;
53456    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
53457      /* 0 is not a legal page number and page 1 cannot be an
53458      ** overflow page. Therefore if ovflPgno<2 or past the end of the
53459      ** file the database must be corrupt. */
53460      return SQLITE_CORRUPT_BKPT;
53461    }
53462    if( nOvfl ){
53463      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
53464      if( rc ) return rc;
53465    }
53466
53467    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
53468     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
53469    ){
53470      /* There is no reason any cursor should have an outstanding reference
53471      ** to an overflow page belonging to a cell that is being deleted/updated.
53472      ** So if there exists more than one reference to this page, then it
53473      ** must not really be an overflow page and the database must be corrupt.
53474      ** It is helpful to detect this before calling freePage2(), as
53475      ** freePage2() may zero the page contents if secure-delete mode is
53476      ** enabled. If this 'overflow' page happens to be a page that the
53477      ** caller is iterating through or using in some other way, this
53478      ** can be problematic.
53479      */
53480      rc = SQLITE_CORRUPT_BKPT;
53481    }else{
53482      rc = freePage2(pBt, pOvfl, ovflPgno);
53483    }
53484
53485    if( pOvfl ){
53486      sqlite3PagerUnref(pOvfl->pDbPage);
53487    }
53488    if( rc ) return rc;
53489    ovflPgno = iNext;
53490  }
53491  return SQLITE_OK;
53492}
53493
53494/*
53495** Create the byte sequence used to represent a cell on page pPage
53496** and write that byte sequence into pCell[].  Overflow pages are
53497** allocated and filled in as necessary.  The calling procedure
53498** is responsible for making sure sufficient space has been allocated
53499** for pCell[].
53500**
53501** Note that pCell does not necessary need to point to the pPage->aData
53502** area.  pCell might point to some temporary storage.  The cell will
53503** be constructed in this temporary area then copied into pPage->aData
53504** later.
53505*/
53506static int fillInCell(
53507  MemPage *pPage,                /* The page that contains the cell */
53508  unsigned char *pCell,          /* Complete text of the cell */
53509  const void *pKey, i64 nKey,    /* The key */
53510  const void *pData,int nData,   /* The data */
53511  int nZero,                     /* Extra zero bytes to append to pData */
53512  int *pnSize                    /* Write cell size here */
53513){
53514  int nPayload;
53515  const u8 *pSrc;
53516  int nSrc, n, rc;
53517  int spaceLeft;
53518  MemPage *pOvfl = 0;
53519  MemPage *pToRelease = 0;
53520  unsigned char *pPrior;
53521  unsigned char *pPayload;
53522  BtShared *pBt = pPage->pBt;
53523  Pgno pgnoOvfl = 0;
53524  int nHeader;
53525  CellInfo info;
53526
53527  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53528
53529  /* pPage is not necessarily writeable since pCell might be auxiliary
53530  ** buffer space that is separate from the pPage buffer area */
53531  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
53532            || sqlite3PagerIswriteable(pPage->pDbPage) );
53533
53534  /* Fill in the header. */
53535  nHeader = 0;
53536  if( !pPage->leaf ){
53537    nHeader += 4;
53538  }
53539  if( pPage->hasData ){
53540    nHeader += putVarint(&pCell[nHeader], nData+nZero);
53541  }else{
53542    nData = nZero = 0;
53543  }
53544  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
53545  btreeParseCellPtr(pPage, pCell, &info);
53546  assert( info.nHeader==nHeader );
53547  assert( info.nKey==nKey );
53548  assert( info.nData==(u32)(nData+nZero) );
53549
53550  /* Fill in the payload */
53551  nPayload = nData + nZero;
53552  if( pPage->intKey ){
53553    pSrc = pData;
53554    nSrc = nData;
53555    nData = 0;
53556  }else{
53557    if( NEVER(nKey>0x7fffffff || pKey==0) ){
53558      return SQLITE_CORRUPT_BKPT;
53559    }
53560    nPayload += (int)nKey;
53561    pSrc = pKey;
53562    nSrc = (int)nKey;
53563  }
53564  *pnSize = info.nSize;
53565  spaceLeft = info.nLocal;
53566  pPayload = &pCell[nHeader];
53567  pPrior = &pCell[info.iOverflow];
53568
53569  while( nPayload>0 ){
53570    if( spaceLeft==0 ){
53571#ifndef SQLITE_OMIT_AUTOVACUUM
53572      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
53573      if( pBt->autoVacuum ){
53574        do{
53575          pgnoOvfl++;
53576        } while(
53577          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
53578        );
53579      }
53580#endif
53581      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
53582#ifndef SQLITE_OMIT_AUTOVACUUM
53583      /* If the database supports auto-vacuum, and the second or subsequent
53584      ** overflow page is being allocated, add an entry to the pointer-map
53585      ** for that page now.
53586      **
53587      ** If this is the first overflow page, then write a partial entry
53588      ** to the pointer-map. If we write nothing to this pointer-map slot,
53589      ** then the optimistic overflow chain processing in clearCell()
53590      ** may misinterpret the uninitialised values and delete the
53591      ** wrong pages from the database.
53592      */
53593      if( pBt->autoVacuum && rc==SQLITE_OK ){
53594        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
53595        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
53596        if( rc ){
53597          releasePage(pOvfl);
53598        }
53599      }
53600#endif
53601      if( rc ){
53602        releasePage(pToRelease);
53603        return rc;
53604      }
53605
53606      /* If pToRelease is not zero than pPrior points into the data area
53607      ** of pToRelease.  Make sure pToRelease is still writeable. */
53608      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
53609
53610      /* If pPrior is part of the data area of pPage, then make sure pPage
53611      ** is still writeable */
53612      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
53613            || sqlite3PagerIswriteable(pPage->pDbPage) );
53614
53615      put4byte(pPrior, pgnoOvfl);
53616      releasePage(pToRelease);
53617      pToRelease = pOvfl;
53618      pPrior = pOvfl->aData;
53619      put4byte(pPrior, 0);
53620      pPayload = &pOvfl->aData[4];
53621      spaceLeft = pBt->usableSize - 4;
53622    }
53623    n = nPayload;
53624    if( n>spaceLeft ) n = spaceLeft;
53625
53626    /* If pToRelease is not zero than pPayload points into the data area
53627    ** of pToRelease.  Make sure pToRelease is still writeable. */
53628    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
53629
53630    /* If pPayload is part of the data area of pPage, then make sure pPage
53631    ** is still writeable */
53632    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
53633            || sqlite3PagerIswriteable(pPage->pDbPage) );
53634
53635    if( nSrc>0 ){
53636      if( n>nSrc ) n = nSrc;
53637      assert( pSrc );
53638      memcpy(pPayload, pSrc, n);
53639    }else{
53640      memset(pPayload, 0, n);
53641    }
53642    nPayload -= n;
53643    pPayload += n;
53644    pSrc += n;
53645    nSrc -= n;
53646    spaceLeft -= n;
53647    if( nSrc==0 ){
53648      nSrc = nData;
53649      pSrc = pData;
53650    }
53651  }
53652  releasePage(pToRelease);
53653  return SQLITE_OK;
53654}
53655
53656/*
53657** Remove the i-th cell from pPage.  This routine effects pPage only.
53658** The cell content is not freed or deallocated.  It is assumed that
53659** the cell content has been copied someplace else.  This routine just
53660** removes the reference to the cell from pPage.
53661**
53662** "sz" must be the number of bytes in the cell.
53663*/
53664static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
53665  u32 pc;         /* Offset to cell content of cell being deleted */
53666  u8 *data;       /* pPage->aData */
53667  u8 *ptr;        /* Used to move bytes around within data[] */
53668  u8 *endPtr;     /* End of loop */
53669  int rc;         /* The return code */
53670  int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
53671
53672  if( *pRC ) return;
53673
53674  assert( idx>=0 && idx<pPage->nCell );
53675  assert( sz==cellSize(pPage, idx) );
53676  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53677  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53678  data = pPage->aData;
53679  ptr = &data[pPage->cellOffset + 2*idx];
53680  pc = get2byte(ptr);
53681  hdr = pPage->hdrOffset;
53682  testcase( pc==get2byte(&data[hdr+5]) );
53683  testcase( pc+sz==pPage->pBt->usableSize );
53684  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
53685    *pRC = SQLITE_CORRUPT_BKPT;
53686    return;
53687  }
53688  rc = freeSpace(pPage, pc, sz);
53689  if( rc ){
53690    *pRC = rc;
53691    return;
53692  }
53693  endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
53694  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
53695  while( ptr<endPtr ){
53696    *(u16*)ptr = *(u16*)&ptr[2];
53697    ptr += 2;
53698  }
53699  pPage->nCell--;
53700  put2byte(&data[hdr+3], pPage->nCell);
53701  pPage->nFree += 2;
53702}
53703
53704/*
53705** Insert a new cell on pPage at cell index "i".  pCell points to the
53706** content of the cell.
53707**
53708** If the cell content will fit on the page, then put it there.  If it
53709** will not fit, then make a copy of the cell content into pTemp if
53710** pTemp is not null.  Regardless of pTemp, allocate a new entry
53711** in pPage->aOvfl[] and make it point to the cell content (either
53712** in pTemp or the original pCell) and also record its index.
53713** Allocating a new entry in pPage->aCell[] implies that
53714** pPage->nOverflow is incremented.
53715**
53716** If nSkip is non-zero, then do not copy the first nSkip bytes of the
53717** cell. The caller will overwrite them after this function returns. If
53718** nSkip is non-zero, then pCell may not point to an invalid memory location
53719** (but pCell+nSkip is always valid).
53720*/
53721static void insertCell(
53722  MemPage *pPage,   /* Page into which we are copying */
53723  int i,            /* New cell becomes the i-th cell of the page */
53724  u8 *pCell,        /* Content of the new cell */
53725  int sz,           /* Bytes of content in pCell */
53726  u8 *pTemp,        /* Temp storage space for pCell, if needed */
53727  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
53728  int *pRC          /* Read and write return code from here */
53729){
53730  int idx = 0;      /* Where to write new cell content in data[] */
53731  int j;            /* Loop counter */
53732  int end;          /* First byte past the last cell pointer in data[] */
53733  int ins;          /* Index in data[] where new cell pointer is inserted */
53734  int cellOffset;   /* Address of first cell pointer in data[] */
53735  u8 *data;         /* The content of the whole page */
53736  u8 *ptr;          /* Used for moving information around in data[] */
53737  u8 *endPtr;       /* End of the loop */
53738
53739  int nSkip = (iChild ? 4 : 0);
53740
53741  if( *pRC ) return;
53742
53743  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
53744  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
53745  assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
53746  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53747  /* The cell should normally be sized correctly.  However, when moving a
53748  ** malformed cell from a leaf page to an interior page, if the cell size
53749  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
53750  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
53751  ** the term after the || in the following assert(). */
53752  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
53753  if( pPage->nOverflow || sz+2>pPage->nFree ){
53754    if( pTemp ){
53755      memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
53756      pCell = pTemp;
53757    }
53758    if( iChild ){
53759      put4byte(pCell, iChild);
53760    }
53761    j = pPage->nOverflow++;
53762    assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
53763    pPage->aOvfl[j].pCell = pCell;
53764    pPage->aOvfl[j].idx = (u16)i;
53765  }else{
53766    int rc = sqlite3PagerWrite(pPage->pDbPage);
53767    if( rc!=SQLITE_OK ){
53768      *pRC = rc;
53769      return;
53770    }
53771    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53772    data = pPage->aData;
53773    cellOffset = pPage->cellOffset;
53774    end = cellOffset + 2*pPage->nCell;
53775    ins = cellOffset + 2*i;
53776    rc = allocateSpace(pPage, sz, &idx);
53777    if( rc ){ *pRC = rc; return; }
53778    /* The allocateSpace() routine guarantees the following two properties
53779    ** if it returns success */
53780    assert( idx >= end+2 );
53781    assert( idx+sz <= (int)pPage->pBt->usableSize );
53782    pPage->nCell++;
53783    pPage->nFree -= (u16)(2 + sz);
53784    memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
53785    if( iChild ){
53786      put4byte(&data[idx], iChild);
53787    }
53788    ptr = &data[end];
53789    endPtr = &data[ins];
53790    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
53791    while( ptr>endPtr ){
53792      *(u16*)ptr = *(u16*)&ptr[-2];
53793      ptr -= 2;
53794    }
53795    put2byte(&data[ins], idx);
53796    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
53797#ifndef SQLITE_OMIT_AUTOVACUUM
53798    if( pPage->pBt->autoVacuum ){
53799      /* The cell may contain a pointer to an overflow page. If so, write
53800      ** the entry for the overflow page into the pointer map.
53801      */
53802      ptrmapPutOvflPtr(pPage, pCell, pRC);
53803    }
53804#endif
53805  }
53806}
53807
53808/*
53809** Add a list of cells to a page.  The page should be initially empty.
53810** The cells are guaranteed to fit on the page.
53811*/
53812static void assemblePage(
53813  MemPage *pPage,   /* The page to be assemblied */
53814  int nCell,        /* The number of cells to add to this page */
53815  u8 **apCell,      /* Pointers to cell bodies */
53816  u16 *aSize        /* Sizes of the cells */
53817){
53818  int i;            /* Loop counter */
53819  u8 *pCellptr;     /* Address of next cell pointer */
53820  int cellbody;     /* Address of next cell body */
53821  u8 * const data = pPage->aData;             /* Pointer to data for pPage */
53822  const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
53823  const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
53824
53825  assert( pPage->nOverflow==0 );
53826  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53827  assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
53828            && (int)MX_CELL(pPage->pBt)<=10921);
53829  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53830
53831  /* Check that the page has just been zeroed by zeroPage() */
53832  assert( pPage->nCell==0 );
53833  assert( get2byteNotZero(&data[hdr+5])==nUsable );
53834
53835  pCellptr = &data[pPage->cellOffset + nCell*2];
53836  cellbody = nUsable;
53837  for(i=nCell-1; i>=0; i--){
53838    u16 sz = aSize[i];
53839    pCellptr -= 2;
53840    cellbody -= sz;
53841    put2byte(pCellptr, cellbody);
53842    memcpy(&data[cellbody], apCell[i], sz);
53843  }
53844  put2byte(&data[hdr+3], nCell);
53845  put2byte(&data[hdr+5], cellbody);
53846  pPage->nFree -= (nCell*2 + nUsable - cellbody);
53847  pPage->nCell = (u16)nCell;
53848}
53849
53850/*
53851** The following parameters determine how many adjacent pages get involved
53852** in a balancing operation.  NN is the number of neighbors on either side
53853** of the page that participate in the balancing operation.  NB is the
53854** total number of pages that participate, including the target page and
53855** NN neighbors on either side.
53856**
53857** The minimum value of NN is 1 (of course).  Increasing NN above 1
53858** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
53859** in exchange for a larger degradation in INSERT and UPDATE performance.
53860** The value of NN appears to give the best results overall.
53861*/
53862#define NN 1             /* Number of neighbors on either side of pPage */
53863#define NB (NN*2+1)      /* Total pages involved in the balance */
53864
53865
53866#ifndef SQLITE_OMIT_QUICKBALANCE
53867/*
53868** This version of balance() handles the common special case where
53869** a new entry is being inserted on the extreme right-end of the
53870** tree, in other words, when the new entry will become the largest
53871** entry in the tree.
53872**
53873** Instead of trying to balance the 3 right-most leaf pages, just add
53874** a new page to the right-hand side and put the one new entry in
53875** that page.  This leaves the right side of the tree somewhat
53876** unbalanced.  But odds are that we will be inserting new entries
53877** at the end soon afterwards so the nearly empty page will quickly
53878** fill up.  On average.
53879**
53880** pPage is the leaf page which is the right-most page in the tree.
53881** pParent is its parent.  pPage must have a single overflow entry
53882** which is also the right-most entry on the page.
53883**
53884** The pSpace buffer is used to store a temporary copy of the divider
53885** cell that will be inserted into pParent. Such a cell consists of a 4
53886** byte page number followed by a variable length integer. In other
53887** words, at most 13 bytes. Hence the pSpace buffer must be at
53888** least 13 bytes in size.
53889*/
53890static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
53891  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
53892  MemPage *pNew;                       /* Newly allocated page */
53893  int rc;                              /* Return Code */
53894  Pgno pgnoNew;                        /* Page number of pNew */
53895
53896  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53897  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
53898  assert( pPage->nOverflow==1 );
53899
53900  /* This error condition is now caught prior to reaching this function */
53901  if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
53902
53903  /* Allocate a new page. This page will become the right-sibling of
53904  ** pPage. Make the parent page writable, so that the new divider cell
53905  ** may be inserted. If both these operations are successful, proceed.
53906  */
53907  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
53908
53909  if( rc==SQLITE_OK ){
53910
53911    u8 *pOut = &pSpace[4];
53912    u8 *pCell = pPage->aOvfl[0].pCell;
53913    u16 szCell = cellSizePtr(pPage, pCell);
53914    u8 *pStop;
53915
53916    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
53917    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
53918    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
53919    assemblePage(pNew, 1, &pCell, &szCell);
53920
53921    /* If this is an auto-vacuum database, update the pointer map
53922    ** with entries for the new page, and any pointer from the
53923    ** cell on the page to an overflow page. If either of these
53924    ** operations fails, the return code is set, but the contents
53925    ** of the parent page are still manipulated by thh code below.
53926    ** That is Ok, at this point the parent page is guaranteed to
53927    ** be marked as dirty. Returning an error code will cause a
53928    ** rollback, undoing any changes made to the parent page.
53929    */
53930    if( ISAUTOVACUUM ){
53931      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
53932      if( szCell>pNew->minLocal ){
53933        ptrmapPutOvflPtr(pNew, pCell, &rc);
53934      }
53935    }
53936
53937    /* Create a divider cell to insert into pParent. The divider cell
53938    ** consists of a 4-byte page number (the page number of pPage) and
53939    ** a variable length key value (which must be the same value as the
53940    ** largest key on pPage).
53941    **
53942    ** To find the largest key value on pPage, first find the right-most
53943    ** cell on pPage. The first two fields of this cell are the
53944    ** record-length (a variable length integer at most 32-bits in size)
53945    ** and the key value (a variable length integer, may have any value).
53946    ** The first of the while(...) loops below skips over the record-length
53947    ** field. The second while(...) loop copies the key value from the
53948    ** cell on pPage into the pSpace buffer.
53949    */
53950    pCell = findCell(pPage, pPage->nCell-1);
53951    pStop = &pCell[9];
53952    while( (*(pCell++)&0x80) && pCell<pStop );
53953    pStop = &pCell[9];
53954    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
53955
53956    /* Insert the new divider cell into pParent. */
53957    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
53958               0, pPage->pgno, &rc);
53959
53960    /* Set the right-child pointer of pParent to point to the new page. */
53961    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
53962
53963    /* Release the reference to the new page. */
53964    releasePage(pNew);
53965  }
53966
53967  return rc;
53968}
53969#endif /* SQLITE_OMIT_QUICKBALANCE */
53970
53971#if 0
53972/*
53973** This function does not contribute anything to the operation of SQLite.
53974** it is sometimes activated temporarily while debugging code responsible
53975** for setting pointer-map entries.
53976*/
53977static int ptrmapCheckPages(MemPage **apPage, int nPage){
53978  int i, j;
53979  for(i=0; i<nPage; i++){
53980    Pgno n;
53981    u8 e;
53982    MemPage *pPage = apPage[i];
53983    BtShared *pBt = pPage->pBt;
53984    assert( pPage->isInit );
53985
53986    for(j=0; j<pPage->nCell; j++){
53987      CellInfo info;
53988      u8 *z;
53989
53990      z = findCell(pPage, j);
53991      btreeParseCellPtr(pPage, z, &info);
53992      if( info.iOverflow ){
53993        Pgno ovfl = get4byte(&z[info.iOverflow]);
53994        ptrmapGet(pBt, ovfl, &e, &n);
53995        assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
53996      }
53997      if( !pPage->leaf ){
53998        Pgno child = get4byte(z);
53999        ptrmapGet(pBt, child, &e, &n);
54000        assert( n==pPage->pgno && e==PTRMAP_BTREE );
54001      }
54002    }
54003    if( !pPage->leaf ){
54004      Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
54005      ptrmapGet(pBt, child, &e, &n);
54006      assert( n==pPage->pgno && e==PTRMAP_BTREE );
54007    }
54008  }
54009  return 1;
54010}
54011#endif
54012
54013/*
54014** This function is used to copy the contents of the b-tree node stored
54015** on page pFrom to page pTo. If page pFrom was not a leaf page, then
54016** the pointer-map entries for each child page are updated so that the
54017** parent page stored in the pointer map is page pTo. If pFrom contained
54018** any cells with overflow page pointers, then the corresponding pointer
54019** map entries are also updated so that the parent page is page pTo.
54020**
54021** If pFrom is currently carrying any overflow cells (entries in the
54022** MemPage.aOvfl[] array), they are not copied to pTo.
54023**
54024** Before returning, page pTo is reinitialized using btreeInitPage().
54025**
54026** The performance of this function is not critical. It is only used by
54027** the balance_shallower() and balance_deeper() procedures, neither of
54028** which are called often under normal circumstances.
54029*/
54030static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
54031  if( (*pRC)==SQLITE_OK ){
54032    BtShared * const pBt = pFrom->pBt;
54033    u8 * const aFrom = pFrom->aData;
54034    u8 * const aTo = pTo->aData;
54035    int const iFromHdr = pFrom->hdrOffset;
54036    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
54037    int rc;
54038    int iData;
54039
54040
54041    assert( pFrom->isInit );
54042    assert( pFrom->nFree>=iToHdr );
54043    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
54044
54045    /* Copy the b-tree node content from page pFrom to page pTo. */
54046    iData = get2byte(&aFrom[iFromHdr+5]);
54047    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
54048    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
54049
54050    /* Reinitialize page pTo so that the contents of the MemPage structure
54051    ** match the new data. The initialization of pTo can actually fail under
54052    ** fairly obscure circumstances, even though it is a copy of initialized
54053    ** page pFrom.
54054    */
54055    pTo->isInit = 0;
54056    rc = btreeInitPage(pTo);
54057    if( rc!=SQLITE_OK ){
54058      *pRC = rc;
54059      return;
54060    }
54061
54062    /* If this is an auto-vacuum database, update the pointer-map entries
54063    ** for any b-tree or overflow pages that pTo now contains the pointers to.
54064    */
54065    if( ISAUTOVACUUM ){
54066      *pRC = setChildPtrmaps(pTo);
54067    }
54068  }
54069}
54070
54071/*
54072** This routine redistributes cells on the iParentIdx'th child of pParent
54073** (hereafter "the page") and up to 2 siblings so that all pages have about the
54074** same amount of free space. Usually a single sibling on either side of the
54075** page are used in the balancing, though both siblings might come from one
54076** side if the page is the first or last child of its parent. If the page
54077** has fewer than 2 siblings (something which can only happen if the page
54078** is a root page or a child of a root page) then all available siblings
54079** participate in the balancing.
54080**
54081** The number of siblings of the page might be increased or decreased by
54082** one or two in an effort to keep pages nearly full but not over full.
54083**
54084** Note that when this routine is called, some of the cells on the page
54085** might not actually be stored in MemPage.aData[]. This can happen
54086** if the page is overfull. This routine ensures that all cells allocated
54087** to the page and its siblings fit into MemPage.aData[] before returning.
54088**
54089** In the course of balancing the page and its siblings, cells may be
54090** inserted into or removed from the parent page (pParent). Doing so
54091** may cause the parent page to become overfull or underfull. If this
54092** happens, it is the responsibility of the caller to invoke the correct
54093** balancing routine to fix this problem (see the balance() routine).
54094**
54095** If this routine fails for any reason, it might leave the database
54096** in a corrupted state. So if this routine fails, the database should
54097** be rolled back.
54098**
54099** The third argument to this function, aOvflSpace, is a pointer to a
54100** buffer big enough to hold one page. If while inserting cells into the parent
54101** page (pParent) the parent page becomes overfull, this buffer is
54102** used to store the parent's overflow cells. Because this function inserts
54103** a maximum of four divider cells into the parent page, and the maximum
54104** size of a cell stored within an internal node is always less than 1/4
54105** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
54106** enough for all overflow cells.
54107**
54108** If aOvflSpace is set to a null pointer, this function returns
54109** SQLITE_NOMEM.
54110*/
54111static int balance_nonroot(
54112  MemPage *pParent,               /* Parent page of siblings being balanced */
54113  int iParentIdx,                 /* Index of "the page" in pParent */
54114  u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
54115  int isRoot                      /* True if pParent is a root-page */
54116){
54117  BtShared *pBt;               /* The whole database */
54118  int nCell = 0;               /* Number of cells in apCell[] */
54119  int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
54120  int nNew = 0;                /* Number of pages in apNew[] */
54121  int nOld;                    /* Number of pages in apOld[] */
54122  int i, j, k;                 /* Loop counters */
54123  int nxDiv;                   /* Next divider slot in pParent->aCell[] */
54124  int rc = SQLITE_OK;          /* The return code */
54125  u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
54126  int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
54127  int usableSpace;             /* Bytes in pPage beyond the header */
54128  int pageFlags;               /* Value of pPage->aData[0] */
54129  int subtotal;                /* Subtotal of bytes in cells on one page */
54130  int iSpace1 = 0;             /* First unused byte of aSpace1[] */
54131  int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
54132  int szScratch;               /* Size of scratch memory requested */
54133  MemPage *apOld[NB];          /* pPage and up to two siblings */
54134  MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
54135  MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
54136  u8 *pRight;                  /* Location in parent of right-sibling pointer */
54137  u8 *apDiv[NB-1];             /* Divider cells in pParent */
54138  int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
54139  int szNew[NB+2];             /* Combined size of cells place on i-th page */
54140  u8 **apCell = 0;             /* All cells begin balanced */
54141  u16 *szCell;                 /* Local size of all cells in apCell[] */
54142  u8 *aSpace1;                 /* Space for copies of dividers cells */
54143  Pgno pgno;                   /* Temp var to store a page number in */
54144
54145  pBt = pParent->pBt;
54146  assert( sqlite3_mutex_held(pBt->mutex) );
54147  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
54148
54149#if 0
54150  TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
54151#endif
54152
54153  /* At this point pParent may have at most one overflow cell. And if
54154  ** this overflow cell is present, it must be the cell with
54155  ** index iParentIdx. This scenario comes about when this function
54156  ** is called (indirectly) from sqlite3BtreeDelete().
54157  */
54158  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
54159  assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
54160
54161  if( !aOvflSpace ){
54162    return SQLITE_NOMEM;
54163  }
54164
54165  /* Find the sibling pages to balance. Also locate the cells in pParent
54166  ** that divide the siblings. An attempt is made to find NN siblings on
54167  ** either side of pPage. More siblings are taken from one side, however,
54168  ** if there are fewer than NN siblings on the other side. If pParent
54169  ** has NB or fewer children then all children of pParent are taken.
54170  **
54171  ** This loop also drops the divider cells from the parent page. This
54172  ** way, the remainder of the function does not have to deal with any
54173  ** overflow cells in the parent page, since if any existed they will
54174  ** have already been removed.
54175  */
54176  i = pParent->nOverflow + pParent->nCell;
54177  if( i<2 ){
54178    nxDiv = 0;
54179    nOld = i+1;
54180  }else{
54181    nOld = 3;
54182    if( iParentIdx==0 ){
54183      nxDiv = 0;
54184    }else if( iParentIdx==i ){
54185      nxDiv = i-2;
54186    }else{
54187      nxDiv = iParentIdx-1;
54188    }
54189    i = 2;
54190  }
54191  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
54192    pRight = &pParent->aData[pParent->hdrOffset+8];
54193  }else{
54194    pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
54195  }
54196  pgno = get4byte(pRight);
54197  while( 1 ){
54198    rc = getAndInitPage(pBt, pgno, &apOld[i]);
54199    if( rc ){
54200      memset(apOld, 0, (i+1)*sizeof(MemPage*));
54201      goto balance_cleanup;
54202    }
54203    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
54204    if( (i--)==0 ) break;
54205
54206    if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
54207      apDiv[i] = pParent->aOvfl[0].pCell;
54208      pgno = get4byte(apDiv[i]);
54209      szNew[i] = cellSizePtr(pParent, apDiv[i]);
54210      pParent->nOverflow = 0;
54211    }else{
54212      apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
54213      pgno = get4byte(apDiv[i]);
54214      szNew[i] = cellSizePtr(pParent, apDiv[i]);
54215
54216      /* Drop the cell from the parent page. apDiv[i] still points to
54217      ** the cell within the parent, even though it has been dropped.
54218      ** This is safe because dropping a cell only overwrites the first
54219      ** four bytes of it, and this function does not need the first
54220      ** four bytes of the divider cell. So the pointer is safe to use
54221      ** later on.
54222      **
54223      ** Unless SQLite is compiled in secure-delete mode. In this case,
54224      ** the dropCell() routine will overwrite the entire cell with zeroes.
54225      ** In this case, temporarily copy the cell into the aOvflSpace[]
54226      ** buffer. It will be copied out again as soon as the aSpace[] buffer
54227      ** is allocated.  */
54228      if( pBt->secureDelete ){
54229        int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
54230        if( (iOff+szNew[i])>(int)pBt->usableSize ){
54231          rc = SQLITE_CORRUPT_BKPT;
54232          memset(apOld, 0, (i+1)*sizeof(MemPage*));
54233          goto balance_cleanup;
54234        }else{
54235          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
54236          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
54237        }
54238      }
54239      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
54240    }
54241  }
54242
54243  /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
54244  ** alignment */
54245  nMaxCells = (nMaxCells + 3)&~3;
54246
54247  /*
54248  ** Allocate space for memory structures
54249  */
54250  k = pBt->pageSize + ROUND8(sizeof(MemPage));
54251  szScratch =
54252       nMaxCells*sizeof(u8*)                       /* apCell */
54253     + nMaxCells*sizeof(u16)                       /* szCell */
54254     + pBt->pageSize                               /* aSpace1 */
54255     + k*nOld;                                     /* Page copies (apCopy) */
54256  apCell = sqlite3ScratchMalloc( szScratch );
54257  if( apCell==0 ){
54258    rc = SQLITE_NOMEM;
54259    goto balance_cleanup;
54260  }
54261  szCell = (u16*)&apCell[nMaxCells];
54262  aSpace1 = (u8*)&szCell[nMaxCells];
54263  assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
54264
54265  /*
54266  ** Load pointers to all cells on sibling pages and the divider cells
54267  ** into the local apCell[] array.  Make copies of the divider cells
54268  ** into space obtained from aSpace1[] and remove the the divider Cells
54269  ** from pParent.
54270  **
54271  ** If the siblings are on leaf pages, then the child pointers of the
54272  ** divider cells are stripped from the cells before they are copied
54273  ** into aSpace1[].  In this way, all cells in apCell[] are without
54274  ** child pointers.  If siblings are not leaves, then all cell in
54275  ** apCell[] include child pointers.  Either way, all cells in apCell[]
54276  ** are alike.
54277  **
54278  ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
54279  **       leafData:  1 if pPage holds key+data and pParent holds only keys.
54280  */
54281  leafCorrection = apOld[0]->leaf*4;
54282  leafData = apOld[0]->hasData;
54283  for(i=0; i<nOld; i++){
54284    int limit;
54285
54286    /* Before doing anything else, take a copy of the i'th original sibling
54287    ** The rest of this function will use data from the copies rather
54288    ** that the original pages since the original pages will be in the
54289    ** process of being overwritten.  */
54290    MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
54291    memcpy(pOld, apOld[i], sizeof(MemPage));
54292    pOld->aData = (void*)&pOld[1];
54293    memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
54294
54295    limit = pOld->nCell+pOld->nOverflow;
54296    if( pOld->nOverflow>0 ){
54297      for(j=0; j<limit; j++){
54298        assert( nCell<nMaxCells );
54299        apCell[nCell] = findOverflowCell(pOld, j);
54300        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
54301        nCell++;
54302      }
54303    }else{
54304      u8 *aData = pOld->aData;
54305      u16 maskPage = pOld->maskPage;
54306      u16 cellOffset = pOld->cellOffset;
54307      for(j=0; j<limit; j++){
54308        assert( nCell<nMaxCells );
54309        apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
54310        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
54311        nCell++;
54312      }
54313    }
54314    if( i<nOld-1 && !leafData){
54315      u16 sz = (u16)szNew[i];
54316      u8 *pTemp;
54317      assert( nCell<nMaxCells );
54318      szCell[nCell] = sz;
54319      pTemp = &aSpace1[iSpace1];
54320      iSpace1 += sz;
54321      assert( sz<=pBt->maxLocal+23 );
54322      assert( iSpace1 <= (int)pBt->pageSize );
54323      memcpy(pTemp, apDiv[i], sz);
54324      apCell[nCell] = pTemp+leafCorrection;
54325      assert( leafCorrection==0 || leafCorrection==4 );
54326      szCell[nCell] = szCell[nCell] - leafCorrection;
54327      if( !pOld->leaf ){
54328        assert( leafCorrection==0 );
54329        assert( pOld->hdrOffset==0 );
54330        /* The right pointer of the child page pOld becomes the left
54331        ** pointer of the divider cell */
54332        memcpy(apCell[nCell], &pOld->aData[8], 4);
54333      }else{
54334        assert( leafCorrection==4 );
54335        if( szCell[nCell]<4 ){
54336          /* Do not allow any cells smaller than 4 bytes. */
54337          szCell[nCell] = 4;
54338        }
54339      }
54340      nCell++;
54341    }
54342  }
54343
54344  /*
54345  ** Figure out the number of pages needed to hold all nCell cells.
54346  ** Store this number in "k".  Also compute szNew[] which is the total
54347  ** size of all cells on the i-th page and cntNew[] which is the index
54348  ** in apCell[] of the cell that divides page i from page i+1.
54349  ** cntNew[k] should equal nCell.
54350  **
54351  ** Values computed by this block:
54352  **
54353  **           k: The total number of sibling pages
54354  **    szNew[i]: Spaced used on the i-th sibling page.
54355  **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
54356  **              the right of the i-th sibling page.
54357  ** usableSpace: Number of bytes of space available on each sibling.
54358  **
54359  */
54360  usableSpace = pBt->usableSize - 12 + leafCorrection;
54361  for(subtotal=k=i=0; i<nCell; i++){
54362    assert( i<nMaxCells );
54363    subtotal += szCell[i] + 2;
54364    if( subtotal > usableSpace ){
54365      szNew[k] = subtotal - szCell[i];
54366      cntNew[k] = i;
54367      if( leafData ){ i--; }
54368      subtotal = 0;
54369      k++;
54370      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
54371    }
54372  }
54373  szNew[k] = subtotal;
54374  cntNew[k] = nCell;
54375  k++;
54376
54377  /*
54378  ** The packing computed by the previous block is biased toward the siblings
54379  ** on the left side.  The left siblings are always nearly full, while the
54380  ** right-most sibling might be nearly empty.  This block of code attempts
54381  ** to adjust the packing of siblings to get a better balance.
54382  **
54383  ** This adjustment is more than an optimization.  The packing above might
54384  ** be so out of balance as to be illegal.  For example, the right-most
54385  ** sibling might be completely empty.  This adjustment is not optional.
54386  */
54387  for(i=k-1; i>0; i--){
54388    int szRight = szNew[i];  /* Size of sibling on the right */
54389    int szLeft = szNew[i-1]; /* Size of sibling on the left */
54390    int r;              /* Index of right-most cell in left sibling */
54391    int d;              /* Index of first cell to the left of right sibling */
54392
54393    r = cntNew[i-1] - 1;
54394    d = r + 1 - leafData;
54395    assert( d<nMaxCells );
54396    assert( r<nMaxCells );
54397    while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
54398      szRight += szCell[d] + 2;
54399      szLeft -= szCell[r] + 2;
54400      cntNew[i-1]--;
54401      r = cntNew[i-1] - 1;
54402      d = r + 1 - leafData;
54403    }
54404    szNew[i] = szRight;
54405    szNew[i-1] = szLeft;
54406  }
54407
54408  /* Either we found one or more cells (cntnew[0])>0) or pPage is
54409  ** a virtual root page.  A virtual root page is when the real root
54410  ** page is page 1 and we are the only child of that page.
54411  */
54412  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
54413
54414  TRACE(("BALANCE: old: %d %d %d  ",
54415    apOld[0]->pgno,
54416    nOld>=2 ? apOld[1]->pgno : 0,
54417    nOld>=3 ? apOld[2]->pgno : 0
54418  ));
54419
54420  /*
54421  ** Allocate k new pages.  Reuse old pages where possible.
54422  */
54423  if( apOld[0]->pgno<=1 ){
54424    rc = SQLITE_CORRUPT_BKPT;
54425    goto balance_cleanup;
54426  }
54427  pageFlags = apOld[0]->aData[0];
54428  for(i=0; i<k; i++){
54429    MemPage *pNew;
54430    if( i<nOld ){
54431      pNew = apNew[i] = apOld[i];
54432      apOld[i] = 0;
54433      rc = sqlite3PagerWrite(pNew->pDbPage);
54434      nNew++;
54435      if( rc ) goto balance_cleanup;
54436    }else{
54437      assert( i>0 );
54438      rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
54439      if( rc ) goto balance_cleanup;
54440      apNew[i] = pNew;
54441      nNew++;
54442
54443      /* Set the pointer-map entry for the new sibling page. */
54444      if( ISAUTOVACUUM ){
54445        ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
54446        if( rc!=SQLITE_OK ){
54447          goto balance_cleanup;
54448        }
54449      }
54450    }
54451  }
54452
54453  /* Free any old pages that were not reused as new pages.
54454  */
54455  while( i<nOld ){
54456    freePage(apOld[i], &rc);
54457    if( rc ) goto balance_cleanup;
54458    releasePage(apOld[i]);
54459    apOld[i] = 0;
54460    i++;
54461  }
54462
54463  /*
54464  ** Put the new pages in accending order.  This helps to
54465  ** keep entries in the disk file in order so that a scan
54466  ** of the table is a linear scan through the file.  That
54467  ** in turn helps the operating system to deliver pages
54468  ** from the disk more rapidly.
54469  **
54470  ** An O(n^2) insertion sort algorithm is used, but since
54471  ** n is never more than NB (a small constant), that should
54472  ** not be a problem.
54473  **
54474  ** When NB==3, this one optimization makes the database
54475  ** about 25% faster for large insertions and deletions.
54476  */
54477  for(i=0; i<k-1; i++){
54478    int minV = apNew[i]->pgno;
54479    int minI = i;
54480    for(j=i+1; j<k; j++){
54481      if( apNew[j]->pgno<(unsigned)minV ){
54482        minI = j;
54483        minV = apNew[j]->pgno;
54484      }
54485    }
54486    if( minI>i ){
54487      MemPage *pT;
54488      pT = apNew[i];
54489      apNew[i] = apNew[minI];
54490      apNew[minI] = pT;
54491    }
54492  }
54493  TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
54494    apNew[0]->pgno, szNew[0],
54495    nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
54496    nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
54497    nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
54498    nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
54499
54500  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
54501  put4byte(pRight, apNew[nNew-1]->pgno);
54502
54503  /*
54504  ** Evenly distribute the data in apCell[] across the new pages.
54505  ** Insert divider cells into pParent as necessary.
54506  */
54507  j = 0;
54508  for(i=0; i<nNew; i++){
54509    /* Assemble the new sibling page. */
54510    MemPage *pNew = apNew[i];
54511    assert( j<nMaxCells );
54512    zeroPage(pNew, pageFlags);
54513    assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
54514    assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
54515    assert( pNew->nOverflow==0 );
54516
54517    j = cntNew[i];
54518
54519    /* If the sibling page assembled above was not the right-most sibling,
54520    ** insert a divider cell into the parent page.
54521    */
54522    assert( i<nNew-1 || j==nCell );
54523    if( j<nCell ){
54524      u8 *pCell;
54525      u8 *pTemp;
54526      int sz;
54527
54528      assert( j<nMaxCells );
54529      pCell = apCell[j];
54530      sz = szCell[j] + leafCorrection;
54531      pTemp = &aOvflSpace[iOvflSpace];
54532      if( !pNew->leaf ){
54533        memcpy(&pNew->aData[8], pCell, 4);
54534      }else if( leafData ){
54535        /* If the tree is a leaf-data tree, and the siblings are leaves,
54536        ** then there is no divider cell in apCell[]. Instead, the divider
54537        ** cell consists of the integer key for the right-most cell of
54538        ** the sibling-page assembled above only.
54539        */
54540        CellInfo info;
54541        j--;
54542        btreeParseCellPtr(pNew, apCell[j], &info);
54543        pCell = pTemp;
54544        sz = 4 + putVarint(&pCell[4], info.nKey);
54545        pTemp = 0;
54546      }else{
54547        pCell -= 4;
54548        /* Obscure case for non-leaf-data trees: If the cell at pCell was
54549        ** previously stored on a leaf node, and its reported size was 4
54550        ** bytes, then it may actually be smaller than this
54551        ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
54552        ** any cell). But it is important to pass the correct size to
54553        ** insertCell(), so reparse the cell now.
54554        **
54555        ** Note that this can never happen in an SQLite data file, as all
54556        ** cells are at least 4 bytes. It only happens in b-trees used
54557        ** to evaluate "IN (SELECT ...)" and similar clauses.
54558        */
54559        if( szCell[j]==4 ){
54560          assert(leafCorrection==4);
54561          sz = cellSizePtr(pParent, pCell);
54562        }
54563      }
54564      iOvflSpace += sz;
54565      assert( sz<=pBt->maxLocal+23 );
54566      assert( iOvflSpace <= (int)pBt->pageSize );
54567      insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
54568      if( rc!=SQLITE_OK ) goto balance_cleanup;
54569      assert( sqlite3PagerIswriteable(pParent->pDbPage) );
54570
54571      j++;
54572      nxDiv++;
54573    }
54574  }
54575  assert( j==nCell );
54576  assert( nOld>0 );
54577  assert( nNew>0 );
54578  if( (pageFlags & PTF_LEAF)==0 ){
54579    u8 *zChild = &apCopy[nOld-1]->aData[8];
54580    memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
54581  }
54582
54583  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
54584    /* The root page of the b-tree now contains no cells. The only sibling
54585    ** page is the right-child of the parent. Copy the contents of the
54586    ** child page into the parent, decreasing the overall height of the
54587    ** b-tree structure by one. This is described as the "balance-shallower"
54588    ** sub-algorithm in some documentation.
54589    **
54590    ** If this is an auto-vacuum database, the call to copyNodeContent()
54591    ** sets all pointer-map entries corresponding to database image pages
54592    ** for which the pointer is stored within the content being copied.
54593    **
54594    ** The second assert below verifies that the child page is defragmented
54595    ** (it must be, as it was just reconstructed using assemblePage()). This
54596    ** is important if the parent page happens to be page 1 of the database
54597    ** image.  */
54598    assert( nNew==1 );
54599    assert( apNew[0]->nFree ==
54600        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
54601    );
54602    copyNodeContent(apNew[0], pParent, &rc);
54603    freePage(apNew[0], &rc);
54604  }else if( ISAUTOVACUUM ){
54605    /* Fix the pointer-map entries for all the cells that were shifted around.
54606    ** There are several different types of pointer-map entries that need to
54607    ** be dealt with by this routine. Some of these have been set already, but
54608    ** many have not. The following is a summary:
54609    **
54610    **   1) The entries associated with new sibling pages that were not
54611    **      siblings when this function was called. These have already
54612    **      been set. We don't need to worry about old siblings that were
54613    **      moved to the free-list - the freePage() code has taken care
54614    **      of those.
54615    **
54616    **   2) The pointer-map entries associated with the first overflow
54617    **      page in any overflow chains used by new divider cells. These
54618    **      have also already been taken care of by the insertCell() code.
54619    **
54620    **   3) If the sibling pages are not leaves, then the child pages of
54621    **      cells stored on the sibling pages may need to be updated.
54622    **
54623    **   4) If the sibling pages are not internal intkey nodes, then any
54624    **      overflow pages used by these cells may need to be updated
54625    **      (internal intkey nodes never contain pointers to overflow pages).
54626    **
54627    **   5) If the sibling pages are not leaves, then the pointer-map
54628    **      entries for the right-child pages of each sibling may need
54629    **      to be updated.
54630    **
54631    ** Cases 1 and 2 are dealt with above by other code. The next
54632    ** block deals with cases 3 and 4 and the one after that, case 5. Since
54633    ** setting a pointer map entry is a relatively expensive operation, this
54634    ** code only sets pointer map entries for child or overflow pages that have
54635    ** actually moved between pages.  */
54636    MemPage *pNew = apNew[0];
54637    MemPage *pOld = apCopy[0];
54638    int nOverflow = pOld->nOverflow;
54639    int iNextOld = pOld->nCell + nOverflow;
54640    int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
54641    j = 0;                             /* Current 'old' sibling page */
54642    k = 0;                             /* Current 'new' sibling page */
54643    for(i=0; i<nCell; i++){
54644      int isDivider = 0;
54645      while( i==iNextOld ){
54646        /* Cell i is the cell immediately following the last cell on old
54647        ** sibling page j. If the siblings are not leaf pages of an
54648        ** intkey b-tree, then cell i was a divider cell. */
54649        pOld = apCopy[++j];
54650        iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
54651        if( pOld->nOverflow ){
54652          nOverflow = pOld->nOverflow;
54653          iOverflow = i + !leafData + pOld->aOvfl[0].idx;
54654        }
54655        isDivider = !leafData;
54656      }
54657
54658      assert(nOverflow>0 || iOverflow<i );
54659      assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
54660      assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
54661      if( i==iOverflow ){
54662        isDivider = 1;
54663        if( (--nOverflow)>0 ){
54664          iOverflow++;
54665        }
54666      }
54667
54668      if( i==cntNew[k] ){
54669        /* Cell i is the cell immediately following the last cell on new
54670        ** sibling page k. If the siblings are not leaf pages of an
54671        ** intkey b-tree, then cell i is a divider cell.  */
54672        pNew = apNew[++k];
54673        if( !leafData ) continue;
54674      }
54675      assert( j<nOld );
54676      assert( k<nNew );
54677
54678      /* If the cell was originally divider cell (and is not now) or
54679      ** an overflow cell, or if the cell was located on a different sibling
54680      ** page before the balancing, then the pointer map entries associated
54681      ** with any child or overflow pages need to be updated.  */
54682      if( isDivider || pOld->pgno!=pNew->pgno ){
54683        if( !leafCorrection ){
54684          ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
54685        }
54686        if( szCell[i]>pNew->minLocal ){
54687          ptrmapPutOvflPtr(pNew, apCell[i], &rc);
54688        }
54689      }
54690    }
54691
54692    if( !leafCorrection ){
54693      for(i=0; i<nNew; i++){
54694        u32 key = get4byte(&apNew[i]->aData[8]);
54695        ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
54696      }
54697    }
54698
54699#if 0
54700    /* The ptrmapCheckPages() contains assert() statements that verify that
54701    ** all pointer map pages are set correctly. This is helpful while
54702    ** debugging. This is usually disabled because a corrupt database may
54703    ** cause an assert() statement to fail.  */
54704    ptrmapCheckPages(apNew, nNew);
54705    ptrmapCheckPages(&pParent, 1);
54706#endif
54707  }
54708
54709  assert( pParent->isInit );
54710  TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
54711          nOld, nNew, nCell));
54712
54713  /*
54714  ** Cleanup before returning.
54715  */
54716balance_cleanup:
54717  sqlite3ScratchFree(apCell);
54718  for(i=0; i<nOld; i++){
54719    releasePage(apOld[i]);
54720  }
54721  for(i=0; i<nNew; i++){
54722    releasePage(apNew[i]);
54723  }
54724
54725  return rc;
54726}
54727
54728
54729/*
54730** This function is called when the root page of a b-tree structure is
54731** overfull (has one or more overflow pages).
54732**
54733** A new child page is allocated and the contents of the current root
54734** page, including overflow cells, are copied into the child. The root
54735** page is then overwritten to make it an empty page with the right-child
54736** pointer pointing to the new page.
54737**
54738** Before returning, all pointer-map entries corresponding to pages
54739** that the new child-page now contains pointers to are updated. The
54740** entry corresponding to the new right-child pointer of the root
54741** page is also updated.
54742**
54743** If successful, *ppChild is set to contain a reference to the child
54744** page and SQLITE_OK is returned. In this case the caller is required
54745** to call releasePage() on *ppChild exactly once. If an error occurs,
54746** an error code is returned and *ppChild is set to 0.
54747*/
54748static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
54749  int rc;                        /* Return value from subprocedures */
54750  MemPage *pChild = 0;           /* Pointer to a new child page */
54751  Pgno pgnoChild = 0;            /* Page number of the new child page */
54752  BtShared *pBt = pRoot->pBt;    /* The BTree */
54753
54754  assert( pRoot->nOverflow>0 );
54755  assert( sqlite3_mutex_held(pBt->mutex) );
54756
54757  /* Make pRoot, the root page of the b-tree, writable. Allocate a new
54758  ** page that will become the new right-child of pPage. Copy the contents
54759  ** of the node stored on pRoot into the new child page.
54760  */
54761  rc = sqlite3PagerWrite(pRoot->pDbPage);
54762  if( rc==SQLITE_OK ){
54763    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
54764    copyNodeContent(pRoot, pChild, &rc);
54765    if( ISAUTOVACUUM ){
54766      ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
54767    }
54768  }
54769  if( rc ){
54770    *ppChild = 0;
54771    releasePage(pChild);
54772    return rc;
54773  }
54774  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
54775  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
54776  assert( pChild->nCell==pRoot->nCell );
54777
54778  TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
54779
54780  /* Copy the overflow cells from pRoot to pChild */
54781  memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
54782  pChild->nOverflow = pRoot->nOverflow;
54783
54784  /* Zero the contents of pRoot. Then install pChild as the right-child. */
54785  zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
54786  put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
54787
54788  *ppChild = pChild;
54789  return SQLITE_OK;
54790}
54791
54792/*
54793** The page that pCur currently points to has just been modified in
54794** some way. This function figures out if this modification means the
54795** tree needs to be balanced, and if so calls the appropriate balancing
54796** routine. Balancing routines are:
54797**
54798**   balance_quick()
54799**   balance_deeper()
54800**   balance_nonroot()
54801*/
54802static int balance(BtCursor *pCur){
54803  int rc = SQLITE_OK;
54804  const int nMin = pCur->pBt->usableSize * 2 / 3;
54805  u8 aBalanceQuickSpace[13];
54806  u8 *pFree = 0;
54807
54808  TESTONLY( int balance_quick_called = 0 );
54809  TESTONLY( int balance_deeper_called = 0 );
54810
54811  do {
54812    int iPage = pCur->iPage;
54813    MemPage *pPage = pCur->apPage[iPage];
54814
54815    if( iPage==0 ){
54816      if( pPage->nOverflow ){
54817        /* The root page of the b-tree is overfull. In this case call the
54818        ** balance_deeper() function to create a new child for the root-page
54819        ** and copy the current contents of the root-page to it. The
54820        ** next iteration of the do-loop will balance the child page.
54821        */
54822        assert( (balance_deeper_called++)==0 );
54823        rc = balance_deeper(pPage, &pCur->apPage[1]);
54824        if( rc==SQLITE_OK ){
54825          pCur->iPage = 1;
54826          pCur->aiIdx[0] = 0;
54827          pCur->aiIdx[1] = 0;
54828          assert( pCur->apPage[1]->nOverflow );
54829        }
54830      }else{
54831        break;
54832      }
54833    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
54834      break;
54835    }else{
54836      MemPage * const pParent = pCur->apPage[iPage-1];
54837      int const iIdx = pCur->aiIdx[iPage-1];
54838
54839      rc = sqlite3PagerWrite(pParent->pDbPage);
54840      if( rc==SQLITE_OK ){
54841#ifndef SQLITE_OMIT_QUICKBALANCE
54842        if( pPage->hasData
54843         && pPage->nOverflow==1
54844         && pPage->aOvfl[0].idx==pPage->nCell
54845         && pParent->pgno!=1
54846         && pParent->nCell==iIdx
54847        ){
54848          /* Call balance_quick() to create a new sibling of pPage on which
54849          ** to store the overflow cell. balance_quick() inserts a new cell
54850          ** into pParent, which may cause pParent overflow. If this
54851          ** happens, the next interation of the do-loop will balance pParent
54852          ** use either balance_nonroot() or balance_deeper(). Until this
54853          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
54854          ** buffer.
54855          **
54856          ** The purpose of the following assert() is to check that only a
54857          ** single call to balance_quick() is made for each call to this
54858          ** function. If this were not verified, a subtle bug involving reuse
54859          ** of the aBalanceQuickSpace[] might sneak in.
54860          */
54861          assert( (balance_quick_called++)==0 );
54862          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
54863        }else
54864#endif
54865        {
54866          /* In this case, call balance_nonroot() to redistribute cells
54867          ** between pPage and up to 2 of its sibling pages. This involves
54868          ** modifying the contents of pParent, which may cause pParent to
54869          ** become overfull or underfull. The next iteration of the do-loop
54870          ** will balance the parent page to correct this.
54871          **
54872          ** If the parent page becomes overfull, the overflow cell or cells
54873          ** are stored in the pSpace buffer allocated immediately below.
54874          ** A subsequent iteration of the do-loop will deal with this by
54875          ** calling balance_nonroot() (balance_deeper() may be called first,
54876          ** but it doesn't deal with overflow cells - just moves them to a
54877          ** different page). Once this subsequent call to balance_nonroot()
54878          ** has completed, it is safe to release the pSpace buffer used by
54879          ** the previous call, as the overflow cell data will have been
54880          ** copied either into the body of a database page or into the new
54881          ** pSpace buffer passed to the latter call to balance_nonroot().
54882          */
54883          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
54884          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
54885          if( pFree ){
54886            /* If pFree is not NULL, it points to the pSpace buffer used
54887            ** by a previous call to balance_nonroot(). Its contents are
54888            ** now stored either on real database pages or within the
54889            ** new pSpace buffer, so it may be safely freed here. */
54890            sqlite3PageFree(pFree);
54891          }
54892
54893          /* The pSpace buffer will be freed after the next call to
54894          ** balance_nonroot(), or just before this function returns, whichever
54895          ** comes first. */
54896          pFree = pSpace;
54897        }
54898      }
54899
54900      pPage->nOverflow = 0;
54901
54902      /* The next iteration of the do-loop balances the parent page. */
54903      releasePage(pPage);
54904      pCur->iPage--;
54905    }
54906  }while( rc==SQLITE_OK );
54907
54908  if( pFree ){
54909    sqlite3PageFree(pFree);
54910  }
54911  return rc;
54912}
54913
54914
54915/*
54916** Insert a new record into the BTree.  The key is given by (pKey,nKey)
54917** and the data is given by (pData,nData).  The cursor is used only to
54918** define what table the record should be inserted into.  The cursor
54919** is left pointing at a random location.
54920**
54921** For an INTKEY table, only the nKey value of the key is used.  pKey is
54922** ignored.  For a ZERODATA table, the pData and nData are both ignored.
54923**
54924** If the seekResult parameter is non-zero, then a successful call to
54925** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
54926** been performed. seekResult is the search result returned (a negative
54927** number if pCur points at an entry that is smaller than (pKey, nKey), or
54928** a positive value if pCur points at an etry that is larger than
54929** (pKey, nKey)).
54930**
54931** If the seekResult parameter is non-zero, then the caller guarantees that
54932** cursor pCur is pointing at the existing copy of a row that is to be
54933** overwritten.  If the seekResult parameter is 0, then cursor pCur may
54934** point to any entry or to no entry at all and so this function has to seek
54935** the cursor before the new key can be inserted.
54936*/
54937SQLITE_PRIVATE int sqlite3BtreeInsert(
54938  BtCursor *pCur,                /* Insert data into the table of this cursor */
54939  const void *pKey, i64 nKey,    /* The key of the new record */
54940  const void *pData, int nData,  /* The data of the new record */
54941  int nZero,                     /* Number of extra 0 bytes to append to data */
54942  int appendBias,                /* True if this is likely an append */
54943  int seekResult                 /* Result of prior MovetoUnpacked() call */
54944){
54945  int rc;
54946  int loc = seekResult;          /* -1: before desired location  +1: after */
54947  int szNew = 0;
54948  int idx;
54949  MemPage *pPage;
54950  Btree *p = pCur->pBtree;
54951  BtShared *pBt = p->pBt;
54952  unsigned char *oldCell;
54953  unsigned char *newCell = 0;
54954
54955  if( pCur->eState==CURSOR_FAULT ){
54956    assert( pCur->skipNext!=SQLITE_OK );
54957    return pCur->skipNext;
54958  }
54959
54960  assert( cursorHoldsMutex(pCur) );
54961  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
54962  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
54963
54964  /* Assert that the caller has been consistent. If this cursor was opened
54965  ** expecting an index b-tree, then the caller should be inserting blob
54966  ** keys with no associated data. If the cursor was opened expecting an
54967  ** intkey table, the caller should be inserting integer keys with a
54968  ** blob of associated data.  */
54969  assert( (pKey==0)==(pCur->pKeyInfo==0) );
54970
54971  /* If this is an insert into a table b-tree, invalidate any incrblob
54972  ** cursors open on the row being replaced (assuming this is a replace
54973  ** operation - if it is not, the following is a no-op).  */
54974  if( pCur->pKeyInfo==0 ){
54975    invalidateIncrblobCursors(p, nKey, 0);
54976  }
54977
54978  /* Save the positions of any other cursors open on this table.
54979  **
54980  ** In some cases, the call to btreeMoveto() below is a no-op. For
54981  ** example, when inserting data into a table with auto-generated integer
54982  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
54983  ** integer key to use. It then calls this function to actually insert the
54984  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
54985  ** that the cursor is already where it needs to be and returns without
54986  ** doing any work. To avoid thwarting these optimizations, it is important
54987  ** not to clear the cursor here.
54988  */
54989  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
54990  if( rc ) return rc;
54991  if( !loc ){
54992    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
54993    if( rc ) return rc;
54994  }
54995  assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
54996
54997  pPage = pCur->apPage[pCur->iPage];
54998  assert( pPage->intKey || nKey>=0 );
54999  assert( pPage->leaf || !pPage->intKey );
55000
55001  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
55002          pCur->pgnoRoot, nKey, nData, pPage->pgno,
55003          loc==0 ? "overwrite" : "new entry"));
55004  assert( pPage->isInit );
55005  allocateTempSpace(pBt);
55006  newCell = pBt->pTmpSpace;
55007  if( newCell==0 ) return SQLITE_NOMEM;
55008  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
55009  if( rc ) goto end_insert;
55010  assert( szNew==cellSizePtr(pPage, newCell) );
55011  assert( szNew <= MX_CELL_SIZE(pBt) );
55012  idx = pCur->aiIdx[pCur->iPage];
55013  if( loc==0 ){
55014    u16 szOld;
55015    assert( idx<pPage->nCell );
55016    rc = sqlite3PagerWrite(pPage->pDbPage);
55017    if( rc ){
55018      goto end_insert;
55019    }
55020    oldCell = findCell(pPage, idx);
55021    if( !pPage->leaf ){
55022      memcpy(newCell, oldCell, 4);
55023    }
55024    szOld = cellSizePtr(pPage, oldCell);
55025    rc = clearCell(pPage, oldCell);
55026    dropCell(pPage, idx, szOld, &rc);
55027    if( rc ) goto end_insert;
55028  }else if( loc<0 && pPage->nCell>0 ){
55029    assert( pPage->leaf );
55030    idx = ++pCur->aiIdx[pCur->iPage];
55031  }else{
55032    assert( pPage->leaf );
55033  }
55034  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
55035  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
55036
55037  /* If no error has occured and pPage has an overflow cell, call balance()
55038  ** to redistribute the cells within the tree. Since balance() may move
55039  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
55040  ** variables.
55041  **
55042  ** Previous versions of SQLite called moveToRoot() to move the cursor
55043  ** back to the root page as balance() used to invalidate the contents
55044  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
55045  ** set the cursor state to "invalid". This makes common insert operations
55046  ** slightly faster.
55047  **
55048  ** There is a subtle but important optimization here too. When inserting
55049  ** multiple records into an intkey b-tree using a single cursor (as can
55050  ** happen while processing an "INSERT INTO ... SELECT" statement), it
55051  ** is advantageous to leave the cursor pointing to the last entry in
55052  ** the b-tree if possible. If the cursor is left pointing to the last
55053  ** entry in the table, and the next row inserted has an integer key
55054  ** larger than the largest existing key, it is possible to insert the
55055  ** row without seeking the cursor. This can be a big performance boost.
55056  */
55057  pCur->info.nSize = 0;
55058  pCur->validNKey = 0;
55059  if( rc==SQLITE_OK && pPage->nOverflow ){
55060    rc = balance(pCur);
55061
55062    /* Must make sure nOverflow is reset to zero even if the balance()
55063    ** fails. Internal data structure corruption will result otherwise.
55064    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
55065    ** from trying to save the current position of the cursor.  */
55066    pCur->apPage[pCur->iPage]->nOverflow = 0;
55067    pCur->eState = CURSOR_INVALID;
55068  }
55069  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
55070
55071end_insert:
55072  return rc;
55073}
55074
55075/*
55076** Delete the entry that the cursor is pointing to.  The cursor
55077** is left pointing at a arbitrary location.
55078*/
55079SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
55080  Btree *p = pCur->pBtree;
55081  BtShared *pBt = p->pBt;
55082  int rc;                              /* Return code */
55083  MemPage *pPage;                      /* Page to delete cell from */
55084  unsigned char *pCell;                /* Pointer to cell to delete */
55085  int iCellIdx;                        /* Index of cell to delete */
55086  int iCellDepth;                      /* Depth of node containing pCell */
55087
55088  assert( cursorHoldsMutex(pCur) );
55089  assert( pBt->inTransaction==TRANS_WRITE );
55090  assert( !pBt->readOnly );
55091  assert( pCur->wrFlag );
55092  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
55093  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
55094
55095  if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
55096   || NEVER(pCur->eState!=CURSOR_VALID)
55097  ){
55098    return SQLITE_ERROR;  /* Something has gone awry. */
55099  }
55100
55101  /* If this is a delete operation to remove a row from a table b-tree,
55102  ** invalidate any incrblob cursors open on the row being deleted.  */
55103  if( pCur->pKeyInfo==0 ){
55104    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
55105  }
55106
55107  iCellDepth = pCur->iPage;
55108  iCellIdx = pCur->aiIdx[iCellDepth];
55109  pPage = pCur->apPage[iCellDepth];
55110  pCell = findCell(pPage, iCellIdx);
55111
55112  /* If the page containing the entry to delete is not a leaf page, move
55113  ** the cursor to the largest entry in the tree that is smaller than
55114  ** the entry being deleted. This cell will replace the cell being deleted
55115  ** from the internal node. The 'previous' entry is used for this instead
55116  ** of the 'next' entry, as the previous entry is always a part of the
55117  ** sub-tree headed by the child page of the cell being deleted. This makes
55118  ** balancing the tree following the delete operation easier.  */
55119  if( !pPage->leaf ){
55120    int notUsed;
55121    rc = sqlite3BtreePrevious(pCur, &notUsed);
55122    if( rc ) return rc;
55123  }
55124
55125  /* Save the positions of any other cursors open on this table before
55126  ** making any modifications. Make the page containing the entry to be
55127  ** deleted writable. Then free any overflow pages associated with the
55128  ** entry and finally remove the cell itself from within the page.
55129  */
55130  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
55131  if( rc ) return rc;
55132  rc = sqlite3PagerWrite(pPage->pDbPage);
55133  if( rc ) return rc;
55134  rc = clearCell(pPage, pCell);
55135  dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
55136  if( rc ) return rc;
55137
55138  /* If the cell deleted was not located on a leaf page, then the cursor
55139  ** is currently pointing to the largest entry in the sub-tree headed
55140  ** by the child-page of the cell that was just deleted from an internal
55141  ** node. The cell from the leaf node needs to be moved to the internal
55142  ** node to replace the deleted cell.  */
55143  if( !pPage->leaf ){
55144    MemPage *pLeaf = pCur->apPage[pCur->iPage];
55145    int nCell;
55146    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
55147    unsigned char *pTmp;
55148
55149    pCell = findCell(pLeaf, pLeaf->nCell-1);
55150    nCell = cellSizePtr(pLeaf, pCell);
55151    assert( MX_CELL_SIZE(pBt) >= nCell );
55152
55153    allocateTempSpace(pBt);
55154    pTmp = pBt->pTmpSpace;
55155
55156    rc = sqlite3PagerWrite(pLeaf->pDbPage);
55157    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
55158    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
55159    if( rc ) return rc;
55160  }
55161
55162  /* Balance the tree. If the entry deleted was located on a leaf page,
55163  ** then the cursor still points to that page. In this case the first
55164  ** call to balance() repairs the tree, and the if(...) condition is
55165  ** never true.
55166  **
55167  ** Otherwise, if the entry deleted was on an internal node page, then
55168  ** pCur is pointing to the leaf page from which a cell was removed to
55169  ** replace the cell deleted from the internal node. This is slightly
55170  ** tricky as the leaf node may be underfull, and the internal node may
55171  ** be either under or overfull. In this case run the balancing algorithm
55172  ** on the leaf node first. If the balance proceeds far enough up the
55173  ** tree that we can be sure that any problem in the internal node has
55174  ** been corrected, so be it. Otherwise, after balancing the leaf node,
55175  ** walk the cursor up the tree to the internal node and balance it as
55176  ** well.  */
55177  rc = balance(pCur);
55178  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
55179    while( pCur->iPage>iCellDepth ){
55180      releasePage(pCur->apPage[pCur->iPage--]);
55181    }
55182    rc = balance(pCur);
55183  }
55184
55185  if( rc==SQLITE_OK ){
55186    moveToRoot(pCur);
55187  }
55188  return rc;
55189}
55190
55191/*
55192** Create a new BTree table.  Write into *piTable the page
55193** number for the root page of the new table.
55194**
55195** The type of type is determined by the flags parameter.  Only the
55196** following values of flags are currently in use.  Other values for
55197** flags might not work:
55198**
55199**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
55200**     BTREE_ZERODATA                  Used for SQL indices
55201*/
55202static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
55203  BtShared *pBt = p->pBt;
55204  MemPage *pRoot;
55205  Pgno pgnoRoot;
55206  int rc;
55207  int ptfFlags;          /* Page-type flage for the root page of new table */
55208
55209  assert( sqlite3BtreeHoldsMutex(p) );
55210  assert( pBt->inTransaction==TRANS_WRITE );
55211  assert( !pBt->readOnly );
55212
55213#ifdef SQLITE_OMIT_AUTOVACUUM
55214  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
55215  if( rc ){
55216    return rc;
55217  }
55218#else
55219  if( pBt->autoVacuum ){
55220    Pgno pgnoMove;      /* Move a page here to make room for the root-page */
55221    MemPage *pPageMove; /* The page to move to. */
55222
55223    /* Creating a new table may probably require moving an existing database
55224    ** to make room for the new tables root page. In case this page turns
55225    ** out to be an overflow page, delete all overflow page-map caches
55226    ** held by open cursors.
55227    */
55228    invalidateAllOverflowCache(pBt);
55229
55230    /* Read the value of meta[3] from the database to determine where the
55231    ** root page of the new table should go. meta[3] is the largest root-page
55232    ** created so far, so the new root-page is (meta[3]+1).
55233    */
55234    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
55235    pgnoRoot++;
55236
55237    /* The new root-page may not be allocated on a pointer-map page, or the
55238    ** PENDING_BYTE page.
55239    */
55240    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
55241        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
55242      pgnoRoot++;
55243    }
55244    assert( pgnoRoot>=3 );
55245
55246    /* Allocate a page. The page that currently resides at pgnoRoot will
55247    ** be moved to the allocated page (unless the allocated page happens
55248    ** to reside at pgnoRoot).
55249    */
55250    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
55251    if( rc!=SQLITE_OK ){
55252      return rc;
55253    }
55254
55255    if( pgnoMove!=pgnoRoot ){
55256      /* pgnoRoot is the page that will be used for the root-page of
55257      ** the new table (assuming an error did not occur). But we were
55258      ** allocated pgnoMove. If required (i.e. if it was not allocated
55259      ** by extending the file), the current page at position pgnoMove
55260      ** is already journaled.
55261      */
55262      u8 eType = 0;
55263      Pgno iPtrPage = 0;
55264
55265      releasePage(pPageMove);
55266
55267      /* Move the page currently at pgnoRoot to pgnoMove. */
55268      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
55269      if( rc!=SQLITE_OK ){
55270        return rc;
55271      }
55272      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
55273      if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
55274        rc = SQLITE_CORRUPT_BKPT;
55275      }
55276      if( rc!=SQLITE_OK ){
55277        releasePage(pRoot);
55278        return rc;
55279      }
55280      assert( eType!=PTRMAP_ROOTPAGE );
55281      assert( eType!=PTRMAP_FREEPAGE );
55282      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
55283      releasePage(pRoot);
55284
55285      /* Obtain the page at pgnoRoot */
55286      if( rc!=SQLITE_OK ){
55287        return rc;
55288      }
55289      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
55290      if( rc!=SQLITE_OK ){
55291        return rc;
55292      }
55293      rc = sqlite3PagerWrite(pRoot->pDbPage);
55294      if( rc!=SQLITE_OK ){
55295        releasePage(pRoot);
55296        return rc;
55297      }
55298    }else{
55299      pRoot = pPageMove;
55300    }
55301
55302    /* Update the pointer-map and meta-data with the new root-page number. */
55303    ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
55304    if( rc ){
55305      releasePage(pRoot);
55306      return rc;
55307    }
55308
55309    /* When the new root page was allocated, page 1 was made writable in
55310    ** order either to increase the database filesize, or to decrement the
55311    ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
55312    */
55313    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
55314    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
55315    if( NEVER(rc) ){
55316      releasePage(pRoot);
55317      return rc;
55318    }
55319
55320  }else{
55321    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
55322    if( rc ) return rc;
55323  }
55324#endif
55325  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
55326  if( createTabFlags & BTREE_INTKEY ){
55327    ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
55328  }else{
55329    ptfFlags = PTF_ZERODATA | PTF_LEAF;
55330  }
55331  zeroPage(pRoot, ptfFlags);
55332  sqlite3PagerUnref(pRoot->pDbPage);
55333  assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
55334  *piTable = (int)pgnoRoot;
55335  return SQLITE_OK;
55336}
55337SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
55338  int rc;
55339  sqlite3BtreeEnter(p);
55340  rc = btreeCreateTable(p, piTable, flags);
55341  sqlite3BtreeLeave(p);
55342  return rc;
55343}
55344
55345/*
55346** Erase the given database page and all its children.  Return
55347** the page to the freelist.
55348*/
55349static int clearDatabasePage(
55350  BtShared *pBt,           /* The BTree that contains the table */
55351  Pgno pgno,               /* Page number to clear */
55352  int freePageFlag,        /* Deallocate page if true */
55353  int *pnChange            /* Add number of Cells freed to this counter */
55354){
55355  MemPage *pPage;
55356  int rc;
55357  unsigned char *pCell;
55358  int i;
55359
55360  assert( sqlite3_mutex_held(pBt->mutex) );
55361  if( pgno>btreePagecount(pBt) ){
55362    return SQLITE_CORRUPT_BKPT;
55363  }
55364
55365  rc = getAndInitPage(pBt, pgno, &pPage);
55366  if( rc ) return rc;
55367  for(i=0; i<pPage->nCell; i++){
55368    pCell = findCell(pPage, i);
55369    if( !pPage->leaf ){
55370      rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
55371      if( rc ) goto cleardatabasepage_out;
55372    }
55373    rc = clearCell(pPage, pCell);
55374    if( rc ) goto cleardatabasepage_out;
55375  }
55376  if( !pPage->leaf ){
55377    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
55378    if( rc ) goto cleardatabasepage_out;
55379  }else if( pnChange ){
55380    assert( pPage->intKey );
55381    *pnChange += pPage->nCell;
55382  }
55383  if( freePageFlag ){
55384    freePage(pPage, &rc);
55385  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
55386    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
55387  }
55388
55389cleardatabasepage_out:
55390  releasePage(pPage);
55391  return rc;
55392}
55393
55394/*
55395** Delete all information from a single table in the database.  iTable is
55396** the page number of the root of the table.  After this routine returns,
55397** the root page is empty, but still exists.
55398**
55399** This routine will fail with SQLITE_LOCKED if there are any open
55400** read cursors on the table.  Open write cursors are moved to the
55401** root of the table.
55402**
55403** If pnChange is not NULL, then table iTable must be an intkey table. The
55404** integer value pointed to by pnChange is incremented by the number of
55405** entries in the table.
55406*/
55407SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
55408  int rc;
55409  BtShared *pBt = p->pBt;
55410  sqlite3BtreeEnter(p);
55411  assert( p->inTrans==TRANS_WRITE );
55412
55413  /* Invalidate all incrblob cursors open on table iTable (assuming iTable
55414  ** is the root of a table b-tree - if it is not, the following call is
55415  ** a no-op).  */
55416  invalidateIncrblobCursors(p, 0, 1);
55417
55418  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
55419  if( SQLITE_OK==rc ){
55420    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
55421  }
55422  sqlite3BtreeLeave(p);
55423  return rc;
55424}
55425
55426/*
55427** Erase all information in a table and add the root of the table to
55428** the freelist.  Except, the root of the principle table (the one on
55429** page 1) is never added to the freelist.
55430**
55431** This routine will fail with SQLITE_LOCKED if there are any open
55432** cursors on the table.
55433**
55434** If AUTOVACUUM is enabled and the page at iTable is not the last
55435** root page in the database file, then the last root page
55436** in the database file is moved into the slot formerly occupied by
55437** iTable and that last slot formerly occupied by the last root page
55438** is added to the freelist instead of iTable.  In this say, all
55439** root pages are kept at the beginning of the database file, which
55440** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
55441** page number that used to be the last root page in the file before
55442** the move.  If no page gets moved, *piMoved is set to 0.
55443** The last root page is recorded in meta[3] and the value of
55444** meta[3] is updated by this procedure.
55445*/
55446static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
55447  int rc;
55448  MemPage *pPage = 0;
55449  BtShared *pBt = p->pBt;
55450
55451  assert( sqlite3BtreeHoldsMutex(p) );
55452  assert( p->inTrans==TRANS_WRITE );
55453
55454  /* It is illegal to drop a table if any cursors are open on the
55455  ** database. This is because in auto-vacuum mode the backend may
55456  ** need to move another root-page to fill a gap left by the deleted
55457  ** root page. If an open cursor was using this page a problem would
55458  ** occur.
55459  **
55460  ** This error is caught long before control reaches this point.
55461  */
55462  if( NEVER(pBt->pCursor) ){
55463    sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
55464    return SQLITE_LOCKED_SHAREDCACHE;
55465  }
55466
55467  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
55468  if( rc ) return rc;
55469  rc = sqlite3BtreeClearTable(p, iTable, 0);
55470  if( rc ){
55471    releasePage(pPage);
55472    return rc;
55473  }
55474
55475  *piMoved = 0;
55476
55477  if( iTable>1 ){
55478#ifdef SQLITE_OMIT_AUTOVACUUM
55479    freePage(pPage, &rc);
55480    releasePage(pPage);
55481#else
55482    if( pBt->autoVacuum ){
55483      Pgno maxRootPgno;
55484      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
55485
55486      if( iTable==maxRootPgno ){
55487        /* If the table being dropped is the table with the largest root-page
55488        ** number in the database, put the root page on the free list.
55489        */
55490        freePage(pPage, &rc);
55491        releasePage(pPage);
55492        if( rc!=SQLITE_OK ){
55493          return rc;
55494        }
55495      }else{
55496        /* The table being dropped does not have the largest root-page
55497        ** number in the database. So move the page that does into the
55498        ** gap left by the deleted root-page.
55499        */
55500        MemPage *pMove;
55501        releasePage(pPage);
55502        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
55503        if( rc!=SQLITE_OK ){
55504          return rc;
55505        }
55506        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
55507        releasePage(pMove);
55508        if( rc!=SQLITE_OK ){
55509          return rc;
55510        }
55511        pMove = 0;
55512        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
55513        freePage(pMove, &rc);
55514        releasePage(pMove);
55515        if( rc!=SQLITE_OK ){
55516          return rc;
55517        }
55518        *piMoved = maxRootPgno;
55519      }
55520
55521      /* Set the new 'max-root-page' value in the database header. This
55522      ** is the old value less one, less one more if that happens to
55523      ** be a root-page number, less one again if that is the
55524      ** PENDING_BYTE_PAGE.
55525      */
55526      maxRootPgno--;
55527      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
55528             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
55529        maxRootPgno--;
55530      }
55531      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
55532
55533      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
55534    }else{
55535      freePage(pPage, &rc);
55536      releasePage(pPage);
55537    }
55538#endif
55539  }else{
55540    /* If sqlite3BtreeDropTable was called on page 1.
55541    ** This really never should happen except in a corrupt
55542    ** database.
55543    */
55544    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
55545    releasePage(pPage);
55546  }
55547  return rc;
55548}
55549SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
55550  int rc;
55551  sqlite3BtreeEnter(p);
55552  rc = btreeDropTable(p, iTable, piMoved);
55553  sqlite3BtreeLeave(p);
55554  return rc;
55555}
55556
55557
55558/*
55559** This function may only be called if the b-tree connection already
55560** has a read or write transaction open on the database.
55561**
55562** Read the meta-information out of a database file.  Meta[0]
55563** is the number of free pages currently in the database.  Meta[1]
55564** through meta[15] are available for use by higher layers.  Meta[0]
55565** is read-only, the others are read/write.
55566**
55567** The schema layer numbers meta values differently.  At the schema
55568** layer (and the SetCookie and ReadCookie opcodes) the number of
55569** free pages is not visible.  So Cookie[0] is the same as Meta[1].
55570*/
55571SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
55572  BtShared *pBt = p->pBt;
55573
55574  sqlite3BtreeEnter(p);
55575  assert( p->inTrans>TRANS_NONE );
55576  assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
55577  assert( pBt->pPage1 );
55578  assert( idx>=0 && idx<=15 );
55579
55580  *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
55581
55582  /* If auto-vacuum is disabled in this build and this is an auto-vacuum
55583  ** database, mark the database as read-only.  */
55584#ifdef SQLITE_OMIT_AUTOVACUUM
55585  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
55586#endif
55587
55588  sqlite3BtreeLeave(p);
55589}
55590
55591/*
55592** Write meta-information back into the database.  Meta[0] is
55593** read-only and may not be written.
55594*/
55595SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
55596  BtShared *pBt = p->pBt;
55597  unsigned char *pP1;
55598  int rc;
55599  assert( idx>=1 && idx<=15 );
55600  sqlite3BtreeEnter(p);
55601  assert( p->inTrans==TRANS_WRITE );
55602  assert( pBt->pPage1!=0 );
55603  pP1 = pBt->pPage1->aData;
55604  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
55605  if( rc==SQLITE_OK ){
55606    put4byte(&pP1[36 + idx*4], iMeta);
55607#ifndef SQLITE_OMIT_AUTOVACUUM
55608    if( idx==BTREE_INCR_VACUUM ){
55609      assert( pBt->autoVacuum || iMeta==0 );
55610      assert( iMeta==0 || iMeta==1 );
55611      pBt->incrVacuum = (u8)iMeta;
55612    }
55613#endif
55614  }
55615  sqlite3BtreeLeave(p);
55616  return rc;
55617}
55618
55619#ifndef SQLITE_OMIT_BTREECOUNT
55620/*
55621** The first argument, pCur, is a cursor opened on some b-tree. Count the
55622** number of entries in the b-tree and write the result to *pnEntry.
55623**
55624** SQLITE_OK is returned if the operation is successfully executed.
55625** Otherwise, if an error is encountered (i.e. an IO error or database
55626** corruption) an SQLite error code is returned.
55627*/
55628SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
55629  i64 nEntry = 0;                      /* Value to return in *pnEntry */
55630  int rc;                              /* Return code */
55631
55632  if( pCur->pgnoRoot==0 ){
55633    *pnEntry = 0;
55634    return SQLITE_OK;
55635  }
55636  rc = moveToRoot(pCur);
55637
55638  /* Unless an error occurs, the following loop runs one iteration for each
55639  ** page in the B-Tree structure (not including overflow pages).
55640  */
55641  while( rc==SQLITE_OK ){
55642    int iIdx;                          /* Index of child node in parent */
55643    MemPage *pPage;                    /* Current page of the b-tree */
55644
55645    /* If this is a leaf page or the tree is not an int-key tree, then
55646    ** this page contains countable entries. Increment the entry counter
55647    ** accordingly.
55648    */
55649    pPage = pCur->apPage[pCur->iPage];
55650    if( pPage->leaf || !pPage->intKey ){
55651      nEntry += pPage->nCell;
55652    }
55653
55654    /* pPage is a leaf node. This loop navigates the cursor so that it
55655    ** points to the first interior cell that it points to the parent of
55656    ** the next page in the tree that has not yet been visited. The
55657    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
55658    ** of the page, or to the number of cells in the page if the next page
55659    ** to visit is the right-child of its parent.
55660    **
55661    ** If all pages in the tree have been visited, return SQLITE_OK to the
55662    ** caller.
55663    */
55664    if( pPage->leaf ){
55665      do {
55666        if( pCur->iPage==0 ){
55667          /* All pages of the b-tree have been visited. Return successfully. */
55668          *pnEntry = nEntry;
55669          return SQLITE_OK;
55670        }
55671        moveToParent(pCur);
55672      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
55673
55674      pCur->aiIdx[pCur->iPage]++;
55675      pPage = pCur->apPage[pCur->iPage];
55676    }
55677
55678    /* Descend to the child node of the cell that the cursor currently
55679    ** points at. This is the right-child if (iIdx==pPage->nCell).
55680    */
55681    iIdx = pCur->aiIdx[pCur->iPage];
55682    if( iIdx==pPage->nCell ){
55683      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
55684    }else{
55685      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
55686    }
55687  }
55688
55689  /* An error has occurred. Return an error code. */
55690  return rc;
55691}
55692#endif
55693
55694/*
55695** Return the pager associated with a BTree.  This routine is used for
55696** testing and debugging only.
55697*/
55698SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
55699  return p->pBt->pPager;
55700}
55701
55702#ifndef SQLITE_OMIT_INTEGRITY_CHECK
55703/*
55704** Append a message to the error message string.
55705*/
55706static void checkAppendMsg(
55707  IntegrityCk *pCheck,
55708  char *zMsg1,
55709  const char *zFormat,
55710  ...
55711){
55712  va_list ap;
55713  if( !pCheck->mxErr ) return;
55714  pCheck->mxErr--;
55715  pCheck->nErr++;
55716  va_start(ap, zFormat);
55717  if( pCheck->errMsg.nChar ){
55718    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
55719  }
55720  if( zMsg1 ){
55721    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
55722  }
55723  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
55724  va_end(ap);
55725  if( pCheck->errMsg.mallocFailed ){
55726    pCheck->mallocFailed = 1;
55727  }
55728}
55729#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
55730
55731#ifndef SQLITE_OMIT_INTEGRITY_CHECK
55732/*
55733** Add 1 to the reference count for page iPage.  If this is the second
55734** reference to the page, add an error message to pCheck->zErrMsg.
55735** Return 1 if there are 2 ore more references to the page and 0 if
55736** if this is the first reference to the page.
55737**
55738** Also check that the page number is in bounds.
55739*/
55740static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
55741  if( iPage==0 ) return 1;
55742  if( iPage>pCheck->nPage ){
55743    checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
55744    return 1;
55745  }
55746  if( pCheck->anRef[iPage]==1 ){
55747    checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
55748    return 1;
55749  }
55750  return  (pCheck->anRef[iPage]++)>1;
55751}
55752
55753#ifndef SQLITE_OMIT_AUTOVACUUM
55754/*
55755** Check that the entry in the pointer-map for page iChild maps to
55756** page iParent, pointer type ptrType. If not, append an error message
55757** to pCheck.
55758*/
55759static void checkPtrmap(
55760  IntegrityCk *pCheck,   /* Integrity check context */
55761  Pgno iChild,           /* Child page number */
55762  u8 eType,              /* Expected pointer map type */
55763  Pgno iParent,          /* Expected pointer map parent page number */
55764  char *zContext         /* Context description (used for error msg) */
55765){
55766  int rc;
55767  u8 ePtrmapType;
55768  Pgno iPtrmapParent;
55769
55770  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
55771  if( rc!=SQLITE_OK ){
55772    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
55773    checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
55774    return;
55775  }
55776
55777  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
55778    checkAppendMsg(pCheck, zContext,
55779      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
55780      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
55781  }
55782}
55783#endif
55784
55785/*
55786** Check the integrity of the freelist or of an overflow page list.
55787** Verify that the number of pages on the list is N.
55788*/
55789static void checkList(
55790  IntegrityCk *pCheck,  /* Integrity checking context */
55791  int isFreeList,       /* True for a freelist.  False for overflow page list */
55792  int iPage,            /* Page number for first page in the list */
55793  int N,                /* Expected number of pages in the list */
55794  char *zContext        /* Context for error messages */
55795){
55796  int i;
55797  int expected = N;
55798  int iFirst = iPage;
55799  while( N-- > 0 && pCheck->mxErr ){
55800    DbPage *pOvflPage;
55801    unsigned char *pOvflData;
55802    if( iPage<1 ){
55803      checkAppendMsg(pCheck, zContext,
55804         "%d of %d pages missing from overflow list starting at %d",
55805          N+1, expected, iFirst);
55806      break;
55807    }
55808    if( checkRef(pCheck, iPage, zContext) ) break;
55809    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
55810      checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
55811      break;
55812    }
55813    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
55814    if( isFreeList ){
55815      int n = get4byte(&pOvflData[4]);
55816#ifndef SQLITE_OMIT_AUTOVACUUM
55817      if( pCheck->pBt->autoVacuum ){
55818        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
55819      }
55820#endif
55821      if( n>(int)pCheck->pBt->usableSize/4-2 ){
55822        checkAppendMsg(pCheck, zContext,
55823           "freelist leaf count too big on page %d", iPage);
55824        N--;
55825      }else{
55826        for(i=0; i<n; i++){
55827          Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
55828#ifndef SQLITE_OMIT_AUTOVACUUM
55829          if( pCheck->pBt->autoVacuum ){
55830            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
55831          }
55832#endif
55833          checkRef(pCheck, iFreePage, zContext);
55834        }
55835        N -= n;
55836      }
55837    }
55838#ifndef SQLITE_OMIT_AUTOVACUUM
55839    else{
55840      /* If this database supports auto-vacuum and iPage is not the last
55841      ** page in this overflow list, check that the pointer-map entry for
55842      ** the following page matches iPage.
55843      */
55844      if( pCheck->pBt->autoVacuum && N>0 ){
55845        i = get4byte(pOvflData);
55846        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
55847      }
55848    }
55849#endif
55850    iPage = get4byte(pOvflData);
55851    sqlite3PagerUnref(pOvflPage);
55852  }
55853}
55854#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
55855
55856#ifndef SQLITE_OMIT_INTEGRITY_CHECK
55857/*
55858** Do various sanity checks on a single page of a tree.  Return
55859** the tree depth.  Root pages return 0.  Parents of root pages
55860** return 1, and so forth.
55861**
55862** These checks are done:
55863**
55864**      1.  Make sure that cells and freeblocks do not overlap
55865**          but combine to completely cover the page.
55866**  NO  2.  Make sure cell keys are in order.
55867**  NO  3.  Make sure no key is less than or equal to zLowerBound.
55868**  NO  4.  Make sure no key is greater than or equal to zUpperBound.
55869**      5.  Check the integrity of overflow pages.
55870**      6.  Recursively call checkTreePage on all children.
55871**      7.  Verify that the depth of all children is the same.
55872**      8.  Make sure this page is at least 33% full or else it is
55873**          the root of the tree.
55874*/
55875static int checkTreePage(
55876  IntegrityCk *pCheck,  /* Context for the sanity check */
55877  int iPage,            /* Page number of the page to check */
55878  char *zParentContext, /* Parent context */
55879  i64 *pnParentMinKey,
55880  i64 *pnParentMaxKey
55881){
55882  MemPage *pPage;
55883  int i, rc, depth, d2, pgno, cnt;
55884  int hdr, cellStart;
55885  int nCell;
55886  u8 *data;
55887  BtShared *pBt;
55888  int usableSize;
55889  char zContext[100];
55890  char *hit = 0;
55891  i64 nMinKey = 0;
55892  i64 nMaxKey = 0;
55893
55894  sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
55895
55896  /* Check that the page exists
55897  */
55898  pBt = pCheck->pBt;
55899  usableSize = pBt->usableSize;
55900  if( iPage==0 ) return 0;
55901  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
55902  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
55903    checkAppendMsg(pCheck, zContext,
55904       "unable to get the page. error code=%d", rc);
55905    return 0;
55906  }
55907
55908  /* Clear MemPage.isInit to make sure the corruption detection code in
55909  ** btreeInitPage() is executed.  */
55910  pPage->isInit = 0;
55911  if( (rc = btreeInitPage(pPage))!=0 ){
55912    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
55913    checkAppendMsg(pCheck, zContext,
55914                   "btreeInitPage() returns error code %d", rc);
55915    releasePage(pPage);
55916    return 0;
55917  }
55918
55919  /* Check out all the cells.
55920  */
55921  depth = 0;
55922  for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
55923    u8 *pCell;
55924    u32 sz;
55925    CellInfo info;
55926
55927    /* Check payload overflow pages
55928    */
55929    sqlite3_snprintf(sizeof(zContext), zContext,
55930             "On tree page %d cell %d: ", iPage, i);
55931    pCell = findCell(pPage,i);
55932    btreeParseCellPtr(pPage, pCell, &info);
55933    sz = info.nData;
55934    if( !pPage->intKey ) sz += (int)info.nKey;
55935    /* For intKey pages, check that the keys are in order.
55936    */
55937    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
55938    else{
55939      if( info.nKey <= nMaxKey ){
55940        checkAppendMsg(pCheck, zContext,
55941            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
55942      }
55943      nMaxKey = info.nKey;
55944    }
55945    assert( sz==info.nPayload );
55946    if( (sz>info.nLocal)
55947     && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
55948    ){
55949      int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
55950      Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
55951#ifndef SQLITE_OMIT_AUTOVACUUM
55952      if( pBt->autoVacuum ){
55953        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
55954      }
55955#endif
55956      checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
55957    }
55958
55959    /* Check sanity of left child page.
55960    */
55961    if( !pPage->leaf ){
55962      pgno = get4byte(pCell);
55963#ifndef SQLITE_OMIT_AUTOVACUUM
55964      if( pBt->autoVacuum ){
55965        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
55966      }
55967#endif
55968      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
55969      if( i>0 && d2!=depth ){
55970        checkAppendMsg(pCheck, zContext, "Child page depth differs");
55971      }
55972      depth = d2;
55973    }
55974  }
55975
55976  if( !pPage->leaf ){
55977    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55978    sqlite3_snprintf(sizeof(zContext), zContext,
55979                     "On page %d at right child: ", iPage);
55980#ifndef SQLITE_OMIT_AUTOVACUUM
55981    if( pBt->autoVacuum ){
55982      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
55983    }
55984#endif
55985    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
55986  }
55987
55988  /* For intKey leaf pages, check that the min/max keys are in order
55989  ** with any left/parent/right pages.
55990  */
55991  if( pPage->leaf && pPage->intKey ){
55992    /* if we are a left child page */
55993    if( pnParentMinKey ){
55994      /* if we are the left most child page */
55995      if( !pnParentMaxKey ){
55996        if( nMaxKey > *pnParentMinKey ){
55997          checkAppendMsg(pCheck, zContext,
55998              "Rowid %lld out of order (max larger than parent min of %lld)",
55999              nMaxKey, *pnParentMinKey);
56000        }
56001      }else{
56002        if( nMinKey <= *pnParentMinKey ){
56003          checkAppendMsg(pCheck, zContext,
56004              "Rowid %lld out of order (min less than parent min of %lld)",
56005              nMinKey, *pnParentMinKey);
56006        }
56007        if( nMaxKey > *pnParentMaxKey ){
56008          checkAppendMsg(pCheck, zContext,
56009              "Rowid %lld out of order (max larger than parent max of %lld)",
56010              nMaxKey, *pnParentMaxKey);
56011        }
56012        *pnParentMinKey = nMaxKey;
56013      }
56014    /* else if we're a right child page */
56015    } else if( pnParentMaxKey ){
56016      if( nMinKey <= *pnParentMaxKey ){
56017        checkAppendMsg(pCheck, zContext,
56018            "Rowid %lld out of order (min less than parent max of %lld)",
56019            nMinKey, *pnParentMaxKey);
56020      }
56021    }
56022  }
56023
56024  /* Check for complete coverage of the page
56025  */
56026  data = pPage->aData;
56027  hdr = pPage->hdrOffset;
56028  hit = sqlite3PageMalloc( pBt->pageSize );
56029  if( hit==0 ){
56030    pCheck->mallocFailed = 1;
56031  }else{
56032    int contentOffset = get2byteNotZero(&data[hdr+5]);
56033    assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
56034    memset(hit+contentOffset, 0, usableSize-contentOffset);
56035    memset(hit, 1, contentOffset);
56036    nCell = get2byte(&data[hdr+3]);
56037    cellStart = hdr + 12 - 4*pPage->leaf;
56038    for(i=0; i<nCell; i++){
56039      int pc = get2byte(&data[cellStart+i*2]);
56040      u32 size = 65536;
56041      int j;
56042      if( pc<=usableSize-4 ){
56043        size = cellSizePtr(pPage, &data[pc]);
56044      }
56045      if( (int)(pc+size-1)>=usableSize ){
56046        checkAppendMsg(pCheck, 0,
56047            "Corruption detected in cell %d on page %d",i,iPage);
56048      }else{
56049        for(j=pc+size-1; j>=pc; j--) hit[j]++;
56050      }
56051    }
56052    i = get2byte(&data[hdr+1]);
56053    while( i>0 ){
56054      int size, j;
56055      assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
56056      size = get2byte(&data[i+2]);
56057      assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
56058      for(j=i+size-1; j>=i; j--) hit[j]++;
56059      j = get2byte(&data[i]);
56060      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
56061      assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
56062      i = j;
56063    }
56064    for(i=cnt=0; i<usableSize; i++){
56065      if( hit[i]==0 ){
56066        cnt++;
56067      }else if( hit[i]>1 ){
56068        checkAppendMsg(pCheck, 0,
56069          "Multiple uses for byte %d of page %d", i, iPage);
56070        break;
56071      }
56072    }
56073    if( cnt!=data[hdr+7] ){
56074      checkAppendMsg(pCheck, 0,
56075          "Fragmentation of %d bytes reported as %d on page %d",
56076          cnt, data[hdr+7], iPage);
56077    }
56078  }
56079  sqlite3PageFree(hit);
56080  releasePage(pPage);
56081  return depth+1;
56082}
56083#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
56084
56085#ifndef SQLITE_OMIT_INTEGRITY_CHECK
56086/*
56087** This routine does a complete check of the given BTree file.  aRoot[] is
56088** an array of pages numbers were each page number is the root page of
56089** a table.  nRoot is the number of entries in aRoot.
56090**
56091** A read-only or read-write transaction must be opened before calling
56092** this function.
56093**
56094** Write the number of error seen in *pnErr.  Except for some memory
56095** allocation errors,  an error message held in memory obtained from
56096** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
56097** returned.  If a memory allocation error occurs, NULL is returned.
56098*/
56099SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
56100  Btree *p,     /* The btree to be checked */
56101  int *aRoot,   /* An array of root pages numbers for individual trees */
56102  int nRoot,    /* Number of entries in aRoot[] */
56103  int mxErr,    /* Stop reporting errors after this many */
56104  int *pnErr    /* Write number of errors seen to this variable */
56105){
56106  Pgno i;
56107  int nRef;
56108  IntegrityCk sCheck;
56109  BtShared *pBt = p->pBt;
56110  char zErr[100];
56111
56112  sqlite3BtreeEnter(p);
56113  assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
56114  nRef = sqlite3PagerRefcount(pBt->pPager);
56115  sCheck.pBt = pBt;
56116  sCheck.pPager = pBt->pPager;
56117  sCheck.nPage = btreePagecount(sCheck.pBt);
56118  sCheck.mxErr = mxErr;
56119  sCheck.nErr = 0;
56120  sCheck.mallocFailed = 0;
56121  *pnErr = 0;
56122  if( sCheck.nPage==0 ){
56123    sqlite3BtreeLeave(p);
56124    return 0;
56125  }
56126  sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
56127  if( !sCheck.anRef ){
56128    *pnErr = 1;
56129    sqlite3BtreeLeave(p);
56130    return 0;
56131  }
56132  for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
56133  i = PENDING_BYTE_PAGE(pBt);
56134  if( i<=sCheck.nPage ){
56135    sCheck.anRef[i] = 1;
56136  }
56137  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
56138  sCheck.errMsg.useMalloc = 2;
56139
56140  /* Check the integrity of the freelist
56141  */
56142  checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
56143            get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
56144
56145  /* Check all the tables.
56146  */
56147  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
56148    if( aRoot[i]==0 ) continue;
56149#ifndef SQLITE_OMIT_AUTOVACUUM
56150    if( pBt->autoVacuum && aRoot[i]>1 ){
56151      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
56152    }
56153#endif
56154    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
56155  }
56156
56157  /* Make sure every page in the file is referenced
56158  */
56159  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
56160#ifdef SQLITE_OMIT_AUTOVACUUM
56161    if( sCheck.anRef[i]==0 ){
56162      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
56163    }
56164#else
56165    /* If the database supports auto-vacuum, make sure no tables contain
56166    ** references to pointer-map pages.
56167    */
56168    if( sCheck.anRef[i]==0 &&
56169       (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
56170      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
56171    }
56172    if( sCheck.anRef[i]!=0 &&
56173       (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
56174      checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
56175    }
56176#endif
56177  }
56178
56179  /* Make sure this analysis did not leave any unref() pages.
56180  ** This is an internal consistency check; an integrity check
56181  ** of the integrity check.
56182  */
56183  if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
56184    checkAppendMsg(&sCheck, 0,
56185      "Outstanding page count goes from %d to %d during this analysis",
56186      nRef, sqlite3PagerRefcount(pBt->pPager)
56187    );
56188  }
56189
56190  /* Clean  up and report errors.
56191  */
56192  sqlite3BtreeLeave(p);
56193  sqlite3_free(sCheck.anRef);
56194  if( sCheck.mallocFailed ){
56195    sqlite3StrAccumReset(&sCheck.errMsg);
56196    *pnErr = sCheck.nErr+1;
56197    return 0;
56198  }
56199  *pnErr = sCheck.nErr;
56200  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
56201  return sqlite3StrAccumFinish(&sCheck.errMsg);
56202}
56203#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
56204
56205/*
56206** Return the full pathname of the underlying database file.
56207**
56208** The pager filename is invariant as long as the pager is
56209** open so it is safe to access without the BtShared mutex.
56210*/
56211SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
56212  assert( p->pBt->pPager!=0 );
56213  return sqlite3PagerFilename(p->pBt->pPager);
56214}
56215
56216/*
56217** Return the pathname of the journal file for this database. The return
56218** value of this routine is the same regardless of whether the journal file
56219** has been created or not.
56220**
56221** The pager journal filename is invariant as long as the pager is
56222** open so it is safe to access without the BtShared mutex.
56223*/
56224SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
56225  assert( p->pBt->pPager!=0 );
56226  return sqlite3PagerJournalname(p->pBt->pPager);
56227}
56228
56229/*
56230** Return non-zero if a transaction is active.
56231*/
56232SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
56233  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
56234  return (p && (p->inTrans==TRANS_WRITE));
56235}
56236
56237#ifndef SQLITE_OMIT_WAL
56238/*
56239** Run a checkpoint on the Btree passed as the first argument.
56240**
56241** Return SQLITE_LOCKED if this or any other connection has an open
56242** transaction on the shared-cache the argument Btree is connected to.
56243**
56244** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
56245*/
56246SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
56247  int rc = SQLITE_OK;
56248  if( p ){
56249    BtShared *pBt = p->pBt;
56250    sqlite3BtreeEnter(p);
56251    if( pBt->inTransaction!=TRANS_NONE ){
56252      rc = SQLITE_LOCKED;
56253    }else{
56254      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
56255    }
56256    sqlite3BtreeLeave(p);
56257  }
56258  return rc;
56259}
56260#endif
56261
56262/*
56263** Return non-zero if a read (or write) transaction is active.
56264*/
56265SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
56266  assert( p );
56267  assert( sqlite3_mutex_held(p->db->mutex) );
56268  return p->inTrans!=TRANS_NONE;
56269}
56270
56271SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
56272  assert( p );
56273  assert( sqlite3_mutex_held(p->db->mutex) );
56274  return p->nBackup!=0;
56275}
56276
56277/*
56278** This function returns a pointer to a blob of memory associated with
56279** a single shared-btree. The memory is used by client code for its own
56280** purposes (for example, to store a high-level schema associated with
56281** the shared-btree). The btree layer manages reference counting issues.
56282**
56283** The first time this is called on a shared-btree, nBytes bytes of memory
56284** are allocated, zeroed, and returned to the caller. For each subsequent
56285** call the nBytes parameter is ignored and a pointer to the same blob
56286** of memory returned.
56287**
56288** If the nBytes parameter is 0 and the blob of memory has not yet been
56289** allocated, a null pointer is returned. If the blob has already been
56290** allocated, it is returned as normal.
56291**
56292** Just before the shared-btree is closed, the function passed as the
56293** xFree argument when the memory allocation was made is invoked on the
56294** blob of allocated memory. The xFree function should not call sqlite3_free()
56295** on the memory, the btree layer does that.
56296*/
56297SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
56298  BtShared *pBt = p->pBt;
56299  sqlite3BtreeEnter(p);
56300  if( !pBt->pSchema && nBytes ){
56301    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
56302    pBt->xFreeSchema = xFree;
56303  }
56304  sqlite3BtreeLeave(p);
56305  return pBt->pSchema;
56306}
56307
56308/*
56309** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
56310** btree as the argument handle holds an exclusive lock on the
56311** sqlite_master table. Otherwise SQLITE_OK.
56312*/
56313SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
56314  int rc;
56315  assert( sqlite3_mutex_held(p->db->mutex) );
56316  sqlite3BtreeEnter(p);
56317  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
56318  assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
56319  sqlite3BtreeLeave(p);
56320  return rc;
56321}
56322
56323
56324#ifndef SQLITE_OMIT_SHARED_CACHE
56325/*
56326** Obtain a lock on the table whose root page is iTab.  The
56327** lock is a write lock if isWritelock is true or a read lock
56328** if it is false.
56329*/
56330SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
56331  int rc = SQLITE_OK;
56332  assert( p->inTrans!=TRANS_NONE );
56333  if( p->sharable ){
56334    u8 lockType = READ_LOCK + isWriteLock;
56335    assert( READ_LOCK+1==WRITE_LOCK );
56336    assert( isWriteLock==0 || isWriteLock==1 );
56337
56338    sqlite3BtreeEnter(p);
56339    rc = querySharedCacheTableLock(p, iTab, lockType);
56340    if( rc==SQLITE_OK ){
56341      rc = setSharedCacheTableLock(p, iTab, lockType);
56342    }
56343    sqlite3BtreeLeave(p);
56344  }
56345  return rc;
56346}
56347#endif
56348
56349#ifndef SQLITE_OMIT_INCRBLOB
56350/*
56351** Argument pCsr must be a cursor opened for writing on an
56352** INTKEY table currently pointing at a valid table entry.
56353** This function modifies the data stored as part of that entry.
56354**
56355** Only the data content may only be modified, it is not possible to
56356** change the length of the data stored. If this function is called with
56357** parameters that attempt to write past the end of the existing data,
56358** no modifications are made and SQLITE_CORRUPT is returned.
56359*/
56360SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
56361  int rc;
56362  assert( cursorHoldsMutex(pCsr) );
56363  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
56364  assert( pCsr->isIncrblobHandle );
56365
56366  rc = restoreCursorPosition(pCsr);
56367  if( rc!=SQLITE_OK ){
56368    return rc;
56369  }
56370  assert( pCsr->eState!=CURSOR_REQUIRESEEK );
56371  if( pCsr->eState!=CURSOR_VALID ){
56372    return SQLITE_ABORT;
56373  }
56374
56375  /* Check some assumptions:
56376  **   (a) the cursor is open for writing,
56377  **   (b) there is a read/write transaction open,
56378  **   (c) the connection holds a write-lock on the table (if required),
56379  **   (d) there are no conflicting read-locks, and
56380  **   (e) the cursor points at a valid row of an intKey table.
56381  */
56382  if( !pCsr->wrFlag ){
56383    return SQLITE_READONLY;
56384  }
56385  assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
56386  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
56387  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
56388  assert( pCsr->apPage[pCsr->iPage]->intKey );
56389
56390  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
56391}
56392
56393/*
56394** Set a flag on this cursor to cache the locations of pages from the
56395** overflow list for the current row. This is used by cursors opened
56396** for incremental blob IO only.
56397**
56398** This function sets a flag only. The actual page location cache
56399** (stored in BtCursor.aOverflow[]) is allocated and used by function
56400** accessPayload() (the worker function for sqlite3BtreeData() and
56401** sqlite3BtreePutData()).
56402*/
56403SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
56404  assert( cursorHoldsMutex(pCur) );
56405  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
56406  invalidateOverflowCache(pCur);
56407  pCur->isIncrblobHandle = 1;
56408}
56409#endif
56410
56411/*
56412** Set both the "read version" (single byte at byte offset 18) and
56413** "write version" (single byte at byte offset 19) fields in the database
56414** header to iVersion.
56415*/
56416SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
56417  BtShared *pBt = pBtree->pBt;
56418  int rc;                         /* Return code */
56419
56420  assert( iVersion==1 || iVersion==2 );
56421
56422  /* If setting the version fields to 1, do not automatically open the
56423  ** WAL connection, even if the version fields are currently set to 2.
56424  */
56425  pBt->doNotUseWAL = (u8)(iVersion==1);
56426
56427  rc = sqlite3BtreeBeginTrans(pBtree, 0);
56428  if( rc==SQLITE_OK ){
56429    u8 *aData = pBt->pPage1->aData;
56430    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
56431      rc = sqlite3BtreeBeginTrans(pBtree, 2);
56432      if( rc==SQLITE_OK ){
56433        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
56434        if( rc==SQLITE_OK ){
56435          aData[18] = (u8)iVersion;
56436          aData[19] = (u8)iVersion;
56437        }
56438      }
56439    }
56440  }
56441
56442  pBt->doNotUseWAL = 0;
56443  return rc;
56444}
56445
56446/************** End of btree.c ***********************************************/
56447/************** Begin file backup.c ******************************************/
56448/*
56449** 2009 January 28
56450**
56451** The author disclaims copyright to this source code.  In place of
56452** a legal notice, here is a blessing:
56453**
56454**    May you do good and not evil.
56455**    May you find forgiveness for yourself and forgive others.
56456**    May you share freely, never taking more than you give.
56457**
56458*************************************************************************
56459** This file contains the implementation of the sqlite3_backup_XXX()
56460** API functions and the related features.
56461*/
56462
56463/* Macro to find the minimum of two numeric values.
56464*/
56465#ifndef MIN
56466# define MIN(x,y) ((x)<(y)?(x):(y))
56467#endif
56468
56469/*
56470** Structure allocated for each backup operation.
56471*/
56472struct sqlite3_backup {
56473  sqlite3* pDestDb;        /* Destination database handle */
56474  Btree *pDest;            /* Destination b-tree file */
56475  u32 iDestSchema;         /* Original schema cookie in destination */
56476  int bDestLocked;         /* True once a write-transaction is open on pDest */
56477
56478  Pgno iNext;              /* Page number of the next source page to copy */
56479  sqlite3* pSrcDb;         /* Source database handle */
56480  Btree *pSrc;             /* Source b-tree file */
56481
56482  int rc;                  /* Backup process error code */
56483
56484  /* These two variables are set by every call to backup_step(). They are
56485  ** read by calls to backup_remaining() and backup_pagecount().
56486  */
56487  Pgno nRemaining;         /* Number of pages left to copy */
56488  Pgno nPagecount;         /* Total number of pages to copy */
56489
56490  int isAttached;          /* True once backup has been registered with pager */
56491  sqlite3_backup *pNext;   /* Next backup associated with source pager */
56492};
56493
56494/*
56495** THREAD SAFETY NOTES:
56496**
56497**   Once it has been created using backup_init(), a single sqlite3_backup
56498**   structure may be accessed via two groups of thread-safe entry points:
56499**
56500**     * Via the sqlite3_backup_XXX() API function backup_step() and
56501**       backup_finish(). Both these functions obtain the source database
56502**       handle mutex and the mutex associated with the source BtShared
56503**       structure, in that order.
56504**
56505**     * Via the BackupUpdate() and BackupRestart() functions, which are
56506**       invoked by the pager layer to report various state changes in
56507**       the page cache associated with the source database. The mutex
56508**       associated with the source database BtShared structure will always
56509**       be held when either of these functions are invoked.
56510**
56511**   The other sqlite3_backup_XXX() API functions, backup_remaining() and
56512**   backup_pagecount() are not thread-safe functions. If they are called
56513**   while some other thread is calling backup_step() or backup_finish(),
56514**   the values returned may be invalid. There is no way for a call to
56515**   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
56516**   or backup_pagecount().
56517**
56518**   Depending on the SQLite configuration, the database handles and/or
56519**   the Btree objects may have their own mutexes that require locking.
56520**   Non-sharable Btrees (in-memory databases for example), do not have
56521**   associated mutexes.
56522*/
56523
56524/*
56525** Return a pointer corresponding to database zDb (i.e. "main", "temp")
56526** in connection handle pDb. If such a database cannot be found, return
56527** a NULL pointer and write an error message to pErrorDb.
56528**
56529** If the "temp" database is requested, it may need to be opened by this
56530** function. If an error occurs while doing so, return 0 and write an
56531** error message to pErrorDb.
56532*/
56533static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
56534  int i = sqlite3FindDbName(pDb, zDb);
56535
56536  if( i==1 ){
56537    Parse *pParse;
56538    int rc = 0;
56539    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
56540    if( pParse==0 ){
56541      sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
56542      rc = SQLITE_NOMEM;
56543    }else{
56544      pParse->db = pDb;
56545      if( sqlite3OpenTempDatabase(pParse) ){
56546        sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
56547        rc = SQLITE_ERROR;
56548      }
56549      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
56550      sqlite3StackFree(pErrorDb, pParse);
56551    }
56552    if( rc ){
56553      return 0;
56554    }
56555  }
56556
56557  if( i<0 ){
56558    sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
56559    return 0;
56560  }
56561
56562  return pDb->aDb[i].pBt;
56563}
56564
56565/*
56566** Attempt to set the page size of the destination to match the page size
56567** of the source.
56568*/
56569static int setDestPgsz(sqlite3_backup *p){
56570  int rc;
56571  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
56572  return rc;
56573}
56574
56575/*
56576** Create an sqlite3_backup process to copy the contents of zSrcDb from
56577** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
56578** a pointer to the new sqlite3_backup object.
56579**
56580** If an error occurs, NULL is returned and an error code and error message
56581** stored in database handle pDestDb.
56582*/
56583SQLITE_API sqlite3_backup *sqlite3_backup_init(
56584  sqlite3* pDestDb,                     /* Database to write to */
56585  const char *zDestDb,                  /* Name of database within pDestDb */
56586  sqlite3* pSrcDb,                      /* Database connection to read from */
56587  const char *zSrcDb                    /* Name of database within pSrcDb */
56588){
56589  sqlite3_backup *p;                    /* Value to return */
56590
56591  /* Lock the source database handle. The destination database
56592  ** handle is not locked in this routine, but it is locked in
56593  ** sqlite3_backup_step(). The user is required to ensure that no
56594  ** other thread accesses the destination handle for the duration
56595  ** of the backup operation.  Any attempt to use the destination
56596  ** database connection while a backup is in progress may cause
56597  ** a malfunction or a deadlock.
56598  */
56599  sqlite3_mutex_enter(pSrcDb->mutex);
56600  sqlite3_mutex_enter(pDestDb->mutex);
56601
56602  if( pSrcDb==pDestDb ){
56603    sqlite3Error(
56604        pDestDb, SQLITE_ERROR, "source and destination must be distinct"
56605    );
56606    p = 0;
56607  }else {
56608    /* Allocate space for a new sqlite3_backup object...
56609    ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
56610    ** call to sqlite3_backup_init() and is destroyed by a call to
56611    ** sqlite3_backup_finish(). */
56612    p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
56613    if( !p ){
56614      sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
56615    }
56616  }
56617
56618  /* If the allocation succeeded, populate the new object. */
56619  if( p ){
56620    memset(p, 0, sizeof(sqlite3_backup));
56621    p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
56622    p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
56623    p->pDestDb = pDestDb;
56624    p->pSrcDb = pSrcDb;
56625    p->iNext = 1;
56626    p->isAttached = 0;
56627
56628    if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
56629      /* One (or both) of the named databases did not exist or an OOM
56630      ** error was hit.  The error has already been written into the
56631      ** pDestDb handle.  All that is left to do here is free the
56632      ** sqlite3_backup structure.
56633      */
56634      sqlite3_free(p);
56635      p = 0;
56636    }
56637  }
56638  if( p ){
56639    p->pSrc->nBackup++;
56640  }
56641
56642  sqlite3_mutex_leave(pDestDb->mutex);
56643  sqlite3_mutex_leave(pSrcDb->mutex);
56644  return p;
56645}
56646
56647/*
56648** Argument rc is an SQLite error code. Return true if this error is
56649** considered fatal if encountered during a backup operation. All errors
56650** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
56651*/
56652static int isFatalError(int rc){
56653  return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
56654}
56655
56656/*
56657** Parameter zSrcData points to a buffer containing the data for
56658** page iSrcPg from the source database. Copy this data into the
56659** destination database.
56660*/
56661static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
56662  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
56663  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
56664  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
56665  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
56666  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
56667#ifdef SQLITE_HAS_CODEC
56668  int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
56669  int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
56670#endif
56671
56672  int rc = SQLITE_OK;
56673  i64 iOff;
56674
56675  assert( p->bDestLocked );
56676  assert( !isFatalError(p->rc) );
56677  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
56678  assert( zSrcData );
56679
56680  /* Catch the case where the destination is an in-memory database and the
56681  ** page sizes of the source and destination differ.
56682  */
56683  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
56684    rc = SQLITE_READONLY;
56685  }
56686
56687#ifdef SQLITE_HAS_CODEC
56688  /* Backup is not possible if the page size of the destination is changing
56689  ** and a codec is in use.
56690  */
56691  if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
56692    rc = SQLITE_READONLY;
56693  }
56694
56695  /* Backup is not possible if the number of bytes of reserve space differ
56696  ** between source and destination.  If there is a difference, try to
56697  ** fix the destination to agree with the source.  If that is not possible,
56698  ** then the backup cannot proceed.
56699  */
56700  if( nSrcReserve!=nDestReserve ){
56701    u32 newPgsz = nSrcPgsz;
56702    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
56703    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
56704  }
56705#endif
56706
56707  /* This loop runs once for each destination page spanned by the source
56708  ** page. For each iteration, variable iOff is set to the byte offset
56709  ** of the destination page.
56710  */
56711  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
56712    DbPage *pDestPg = 0;
56713    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
56714    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
56715    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
56716     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
56717    ){
56718      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
56719      u8 *zDestData = sqlite3PagerGetData(pDestPg);
56720      u8 *zOut = &zDestData[iOff%nDestPgsz];
56721
56722      /* Copy the data from the source page into the destination page.
56723      ** Then clear the Btree layer MemPage.isInit flag. Both this module
56724      ** and the pager code use this trick (clearing the first byte
56725      ** of the page 'extra' space to invalidate the Btree layers
56726      ** cached parse of the page). MemPage.isInit is marked
56727      ** "MUST BE FIRST" for this purpose.
56728      */
56729      memcpy(zOut, zIn, nCopy);
56730      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
56731    }
56732    sqlite3PagerUnref(pDestPg);
56733  }
56734
56735  return rc;
56736}
56737
56738/*
56739** If pFile is currently larger than iSize bytes, then truncate it to
56740** exactly iSize bytes. If pFile is not larger than iSize bytes, then
56741** this function is a no-op.
56742**
56743** Return SQLITE_OK if everything is successful, or an SQLite error
56744** code if an error occurs.
56745*/
56746static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
56747  i64 iCurrent;
56748  int rc = sqlite3OsFileSize(pFile, &iCurrent);
56749  if( rc==SQLITE_OK && iCurrent>iSize ){
56750    rc = sqlite3OsTruncate(pFile, iSize);
56751  }
56752  return rc;
56753}
56754
56755/*
56756** Register this backup object with the associated source pager for
56757** callbacks when pages are changed or the cache invalidated.
56758*/
56759static void attachBackupObject(sqlite3_backup *p){
56760  sqlite3_backup **pp;
56761  assert( sqlite3BtreeHoldsMutex(p->pSrc) );
56762  pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
56763  p->pNext = *pp;
56764  *pp = p;
56765  p->isAttached = 1;
56766}
56767
56768/*
56769** Copy nPage pages from the source b-tree to the destination.
56770*/
56771SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
56772  int rc;
56773  int destMode;       /* Destination journal mode */
56774  int pgszSrc = 0;    /* Source page size */
56775  int pgszDest = 0;   /* Destination page size */
56776
56777  sqlite3_mutex_enter(p->pSrcDb->mutex);
56778  sqlite3BtreeEnter(p->pSrc);
56779  if( p->pDestDb ){
56780    sqlite3_mutex_enter(p->pDestDb->mutex);
56781  }
56782
56783  rc = p->rc;
56784  if( !isFatalError(rc) ){
56785    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
56786    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
56787    int ii;                            /* Iterator variable */
56788    int nSrcPage = -1;                 /* Size of source db in pages */
56789    int bCloseTrans = 0;               /* True if src db requires unlocking */
56790
56791    /* If the source pager is currently in a write-transaction, return
56792    ** SQLITE_BUSY immediately.
56793    */
56794    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
56795      rc = SQLITE_BUSY;
56796    }else{
56797      rc = SQLITE_OK;
56798    }
56799
56800    /* Lock the destination database, if it is not locked already. */
56801    if( SQLITE_OK==rc && p->bDestLocked==0
56802     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
56803    ){
56804      p->bDestLocked = 1;
56805      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
56806    }
56807
56808    /* If there is no open read-transaction on the source database, open
56809    ** one now. If a transaction is opened here, then it will be closed
56810    ** before this function exits.
56811    */
56812    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
56813      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
56814      bCloseTrans = 1;
56815    }
56816
56817    /* Do not allow backup if the destination database is in WAL mode
56818    ** and the page sizes are different between source and destination */
56819    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
56820    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
56821    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
56822    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
56823      rc = SQLITE_READONLY;
56824    }
56825
56826    /* Now that there is a read-lock on the source database, query the
56827    ** source pager for the number of pages in the database.
56828    */
56829    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
56830    assert( nSrcPage>=0 );
56831    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
56832      const Pgno iSrcPg = p->iNext;                 /* Source page number */
56833      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
56834        DbPage *pSrcPg;                             /* Source page object */
56835        rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56836        if( rc==SQLITE_OK ){
56837          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
56838          sqlite3PagerUnref(pSrcPg);
56839        }
56840      }
56841      p->iNext++;
56842    }
56843    if( rc==SQLITE_OK ){
56844      p->nPagecount = nSrcPage;
56845      p->nRemaining = nSrcPage+1-p->iNext;
56846      if( p->iNext>(Pgno)nSrcPage ){
56847        rc = SQLITE_DONE;
56848      }else if( !p->isAttached ){
56849        attachBackupObject(p);
56850      }
56851    }
56852
56853    /* Update the schema version field in the destination database. This
56854    ** is to make sure that the schema-version really does change in
56855    ** the case where the source and destination databases have the
56856    ** same schema version.
56857    */
56858    if( rc==SQLITE_DONE ){
56859      rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
56860      if( rc==SQLITE_OK ){
56861        if( p->pDestDb ){
56862          sqlite3ResetInternalSchema(p->pDestDb, -1);
56863        }
56864        if( destMode==PAGER_JOURNALMODE_WAL ){
56865          rc = sqlite3BtreeSetVersion(p->pDest, 2);
56866        }
56867      }
56868      if( rc==SQLITE_OK ){
56869        int nDestTruncate;
56870        /* Set nDestTruncate to the final number of pages in the destination
56871        ** database. The complication here is that the destination page
56872        ** size may be different to the source page size.
56873        **
56874        ** If the source page size is smaller than the destination page size,
56875        ** round up. In this case the call to sqlite3OsTruncate() below will
56876        ** fix the size of the file. However it is important to call
56877        ** sqlite3PagerTruncateImage() here so that any pages in the
56878        ** destination file that lie beyond the nDestTruncate page mark are
56879        ** journalled by PagerCommitPhaseOne() before they are destroyed
56880        ** by the file truncation.
56881        */
56882        assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56883        assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56884        if( pgszSrc<pgszDest ){
56885          int ratio = pgszDest/pgszSrc;
56886          nDestTruncate = (nSrcPage+ratio-1)/ratio;
56887          if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56888            nDestTruncate--;
56889          }
56890        }else{
56891          nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56892        }
56893        sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56894
56895        if( pgszSrc<pgszDest ){
56896          /* If the source page-size is smaller than the destination page-size,
56897          ** two extra things may need to happen:
56898          **
56899          **   * The destination may need to be truncated, and
56900          **
56901          **   * Data stored on the pages immediately following the
56902          **     pending-byte page in the source database may need to be
56903          **     copied into the destination database.
56904          */
56905          const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56906          sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56907          i64 iOff;
56908          i64 iEnd;
56909
56910          assert( pFile );
56911          assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56912                nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56913             && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56914          ));
56915
56916          /* This call ensures that all data required to recreate the original
56917          ** database has been stored in the journal for pDestPager and the
56918          ** journal synced to disk. So at this point we may safely modify
56919          ** the database file in any way, knowing that if a power failure
56920          ** occurs, the original database will be reconstructed from the
56921          ** journal file.  */
56922          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56923
56924          /* Write the extra pages and truncate the database file as required */
56925          iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56926          for(
56927            iOff=PENDING_BYTE+pgszSrc;
56928            rc==SQLITE_OK && iOff<iEnd;
56929            iOff+=pgszSrc
56930          ){
56931            PgHdr *pSrcPg = 0;
56932            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56933            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56934            if( rc==SQLITE_OK ){
56935              u8 *zData = sqlite3PagerGetData(pSrcPg);
56936              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56937            }
56938            sqlite3PagerUnref(pSrcPg);
56939          }
56940          if( rc==SQLITE_OK ){
56941            rc = backupTruncateFile(pFile, iSize);
56942          }
56943
56944          /* Sync the database file to disk. */
56945          if( rc==SQLITE_OK ){
56946            rc = sqlite3PagerSync(pDestPager);
56947          }
56948        }else{
56949          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56950        }
56951
56952        /* Finish committing the transaction to the destination database. */
56953        if( SQLITE_OK==rc
56954         && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56955        ){
56956          rc = SQLITE_DONE;
56957        }
56958      }
56959    }
56960
56961    /* If bCloseTrans is true, then this function opened a read transaction
56962    ** on the source database. Close the read transaction here. There is
56963    ** no need to check the return values of the btree methods here, as
56964    ** "committing" a read-only transaction cannot fail.
56965    */
56966    if( bCloseTrans ){
56967      TESTONLY( int rc2 );
56968      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
56969      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
56970      assert( rc2==SQLITE_OK );
56971    }
56972
56973    if( rc==SQLITE_IOERR_NOMEM ){
56974      rc = SQLITE_NOMEM;
56975    }
56976    p->rc = rc;
56977  }
56978  if( p->pDestDb ){
56979    sqlite3_mutex_leave(p->pDestDb->mutex);
56980  }
56981  sqlite3BtreeLeave(p->pSrc);
56982  sqlite3_mutex_leave(p->pSrcDb->mutex);
56983  return rc;
56984}
56985
56986/*
56987** Release all resources associated with an sqlite3_backup* handle.
56988*/
56989SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
56990  sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
56991  sqlite3_mutex *mutex;                /* Mutex to protect source database */
56992  int rc;                              /* Value to return */
56993
56994  /* Enter the mutexes */
56995  if( p==0 ) return SQLITE_OK;
56996  sqlite3_mutex_enter(p->pSrcDb->mutex);
56997  sqlite3BtreeEnter(p->pSrc);
56998  mutex = p->pSrcDb->mutex;
56999  if( p->pDestDb ){
57000    sqlite3_mutex_enter(p->pDestDb->mutex);
57001  }
57002
57003  /* Detach this backup from the source pager. */
57004  if( p->pDestDb ){
57005    p->pSrc->nBackup--;
57006  }
57007  if( p->isAttached ){
57008    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
57009    while( *pp!=p ){
57010      pp = &(*pp)->pNext;
57011    }
57012    *pp = p->pNext;
57013  }
57014
57015  /* If a transaction is still open on the Btree, roll it back. */
57016  sqlite3BtreeRollback(p->pDest);
57017
57018  /* Set the error code of the destination database handle. */
57019  rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
57020  sqlite3Error(p->pDestDb, rc, 0);
57021
57022  /* Exit the mutexes and free the backup context structure. */
57023  if( p->pDestDb ){
57024    sqlite3_mutex_leave(p->pDestDb->mutex);
57025  }
57026  sqlite3BtreeLeave(p->pSrc);
57027  if( p->pDestDb ){
57028    /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
57029    ** call to sqlite3_backup_init() and is destroyed by a call to
57030    ** sqlite3_backup_finish(). */
57031    sqlite3_free(p);
57032  }
57033  sqlite3_mutex_leave(mutex);
57034  return rc;
57035}
57036
57037/*
57038** Return the number of pages still to be backed up as of the most recent
57039** call to sqlite3_backup_step().
57040*/
57041SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
57042  return p->nRemaining;
57043}
57044
57045/*
57046** Return the total number of pages in the source database as of the most
57047** recent call to sqlite3_backup_step().
57048*/
57049SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
57050  return p->nPagecount;
57051}
57052
57053/*
57054** This function is called after the contents of page iPage of the
57055** source database have been modified. If page iPage has already been
57056** copied into the destination database, then the data written to the
57057** destination is now invalidated. The destination copy of iPage needs
57058** to be updated with the new data before the backup operation is
57059** complete.
57060**
57061** It is assumed that the mutex associated with the BtShared object
57062** corresponding to the source database is held when this function is
57063** called.
57064*/
57065SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
57066  sqlite3_backup *p;                   /* Iterator variable */
57067  for(p=pBackup; p; p=p->pNext){
57068    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
57069    if( !isFatalError(p->rc) && iPage<p->iNext ){
57070      /* The backup process p has already copied page iPage. But now it
57071      ** has been modified by a transaction on the source pager. Copy
57072      ** the new data into the backup.
57073      */
57074      int rc;
57075      assert( p->pDestDb );
57076      sqlite3_mutex_enter(p->pDestDb->mutex);
57077      rc = backupOnePage(p, iPage, aData);
57078      sqlite3_mutex_leave(p->pDestDb->mutex);
57079      assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
57080      if( rc!=SQLITE_OK ){
57081        p->rc = rc;
57082      }
57083    }
57084  }
57085}
57086
57087/*
57088** Restart the backup process. This is called when the pager layer
57089** detects that the database has been modified by an external database
57090** connection. In this case there is no way of knowing which of the
57091** pages that have been copied into the destination database are still
57092** valid and which are not, so the entire process needs to be restarted.
57093**
57094** It is assumed that the mutex associated with the BtShared object
57095** corresponding to the source database is held when this function is
57096** called.
57097*/
57098SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
57099  sqlite3_backup *p;                   /* Iterator variable */
57100  for(p=pBackup; p; p=p->pNext){
57101    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
57102    p->iNext = 1;
57103  }
57104}
57105
57106#ifndef SQLITE_OMIT_VACUUM
57107/*
57108** Copy the complete content of pBtFrom into pBtTo.  A transaction
57109** must be active for both files.
57110**
57111** The size of file pTo may be reduced by this operation. If anything
57112** goes wrong, the transaction on pTo is rolled back. If successful, the
57113** transaction is committed before returning.
57114*/
57115SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
57116  int rc;
57117  sqlite3_backup b;
57118  sqlite3BtreeEnter(pTo);
57119  sqlite3BtreeEnter(pFrom);
57120
57121  /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
57122  ** to 0. This is used by the implementations of sqlite3_backup_step()
57123  ** and sqlite3_backup_finish() to detect that they are being called
57124  ** from this function, not directly by the user.
57125  */
57126  memset(&b, 0, sizeof(b));
57127  b.pSrcDb = pFrom->db;
57128  b.pSrc = pFrom;
57129  b.pDest = pTo;
57130  b.iNext = 1;
57131
57132  /* 0x7FFFFFFF is the hard limit for the number of pages in a database
57133  ** file. By passing this as the number of pages to copy to
57134  ** sqlite3_backup_step(), we can guarantee that the copy finishes
57135  ** within a single call (unless an error occurs). The assert() statement
57136  ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
57137  ** or an error code.
57138  */
57139  sqlite3_backup_step(&b, 0x7FFFFFFF);
57140  assert( b.rc!=SQLITE_OK );
57141  rc = sqlite3_backup_finish(&b);
57142  if( rc==SQLITE_OK ){
57143    pTo->pBt->pageSizeFixed = 0;
57144  }
57145
57146  sqlite3BtreeLeave(pFrom);
57147  sqlite3BtreeLeave(pTo);
57148  return rc;
57149}
57150#endif /* SQLITE_OMIT_VACUUM */
57151
57152/************** End of backup.c **********************************************/
57153/************** Begin file vdbemem.c *****************************************/
57154/*
57155** 2004 May 26
57156**
57157** The author disclaims copyright to this source code.  In place of
57158** a legal notice, here is a blessing:
57159**
57160**    May you do good and not evil.
57161**    May you find forgiveness for yourself and forgive others.
57162**    May you share freely, never taking more than you give.
57163**
57164*************************************************************************
57165**
57166** This file contains code use to manipulate "Mem" structure.  A "Mem"
57167** stores a single value in the VDBE.  Mem is an opaque structure visible
57168** only within the VDBE.  Interface routines refer to a Mem using the
57169** name sqlite_value
57170*/
57171
57172/*
57173** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
57174** P if required.
57175*/
57176#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
57177
57178/*
57179** If pMem is an object with a valid string representation, this routine
57180** ensures the internal encoding for the string representation is
57181** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
57182**
57183** If pMem is not a string object, or the encoding of the string
57184** representation is already stored using the requested encoding, then this
57185** routine is a no-op.
57186**
57187** SQLITE_OK is returned if the conversion is successful (or not required).
57188** SQLITE_NOMEM may be returned if a malloc() fails during conversion
57189** between formats.
57190*/
57191SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
57192  int rc;
57193  assert( (pMem->flags&MEM_RowSet)==0 );
57194  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
57195           || desiredEnc==SQLITE_UTF16BE );
57196  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
57197    return SQLITE_OK;
57198  }
57199  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57200#ifdef SQLITE_OMIT_UTF16
57201  return SQLITE_ERROR;
57202#else
57203
57204  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
57205  ** then the encoding of the value may not have changed.
57206  */
57207  rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
57208  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
57209  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
57210  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
57211  return rc;
57212#endif
57213}
57214
57215/*
57216** Make sure pMem->z points to a writable allocation of at least
57217** n bytes.
57218**
57219** If the memory cell currently contains string or blob data
57220** and the third argument passed to this function is true, the
57221** current content of the cell is preserved. Otherwise, it may
57222** be discarded.
57223**
57224** This function sets the MEM_Dyn flag and clears any xDel callback.
57225** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
57226** not set, Mem.n is zeroed.
57227*/
57228SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
57229  assert( 1 >=
57230    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
57231    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
57232    ((pMem->flags&MEM_Ephem) ? 1 : 0) +
57233    ((pMem->flags&MEM_Static) ? 1 : 0)
57234  );
57235  assert( (pMem->flags&MEM_RowSet)==0 );
57236
57237  if( n<32 ) n = 32;
57238  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
57239    if( preserve && pMem->z==pMem->zMalloc ){
57240      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
57241      preserve = 0;
57242    }else{
57243      sqlite3DbFree(pMem->db, pMem->zMalloc);
57244      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
57245    }
57246  }
57247
57248  if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
57249    memcpy(pMem->zMalloc, pMem->z, pMem->n);
57250  }
57251  if( pMem->flags&MEM_Dyn && pMem->xDel ){
57252    pMem->xDel((void *)(pMem->z));
57253  }
57254
57255  pMem->z = pMem->zMalloc;
57256  if( pMem->z==0 ){
57257    pMem->flags = MEM_Null;
57258  }else{
57259    pMem->flags &= ~(MEM_Ephem|MEM_Static);
57260  }
57261  pMem->xDel = 0;
57262  return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
57263}
57264
57265/*
57266** Make the given Mem object MEM_Dyn.  In other words, make it so
57267** that any TEXT or BLOB content is stored in memory obtained from
57268** malloc().  In this way, we know that the memory is safe to be
57269** overwritten or altered.
57270**
57271** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
57272*/
57273SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
57274  int f;
57275  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57276  assert( (pMem->flags&MEM_RowSet)==0 );
57277  expandBlob(pMem);
57278  f = pMem->flags;
57279  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
57280    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
57281      return SQLITE_NOMEM;
57282    }
57283    pMem->z[pMem->n] = 0;
57284    pMem->z[pMem->n+1] = 0;
57285    pMem->flags |= MEM_Term;
57286#ifdef SQLITE_DEBUG
57287    pMem->pScopyFrom = 0;
57288#endif
57289  }
57290
57291  return SQLITE_OK;
57292}
57293
57294/*
57295** If the given Mem* has a zero-filled tail, turn it into an ordinary
57296** blob stored in dynamically allocated space.
57297*/
57298#ifndef SQLITE_OMIT_INCRBLOB
57299SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
57300  if( pMem->flags & MEM_Zero ){
57301    int nByte;
57302    assert( pMem->flags&MEM_Blob );
57303    assert( (pMem->flags&MEM_RowSet)==0 );
57304    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57305
57306    /* Set nByte to the number of bytes required to store the expanded blob. */
57307    nByte = pMem->n + pMem->u.nZero;
57308    if( nByte<=0 ){
57309      nByte = 1;
57310    }
57311    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
57312      return SQLITE_NOMEM;
57313    }
57314
57315    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
57316    pMem->n += pMem->u.nZero;
57317    pMem->flags &= ~(MEM_Zero|MEM_Term);
57318  }
57319  return SQLITE_OK;
57320}
57321#endif
57322
57323
57324/*
57325** Make sure the given Mem is \u0000 terminated.
57326*/
57327SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
57328  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57329  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
57330    return SQLITE_OK;   /* Nothing to do */
57331  }
57332  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
57333    return SQLITE_NOMEM;
57334  }
57335  pMem->z[pMem->n] = 0;
57336  pMem->z[pMem->n+1] = 0;
57337  pMem->flags |= MEM_Term;
57338  return SQLITE_OK;
57339}
57340
57341/*
57342** Add MEM_Str to the set of representations for the given Mem.  Numbers
57343** are converted using sqlite3_snprintf().  Converting a BLOB to a string
57344** is a no-op.
57345**
57346** Existing representations MEM_Int and MEM_Real are *not* invalidated.
57347**
57348** A MEM_Null value will never be passed to this function. This function is
57349** used for converting values to text for returning to the user (i.e. via
57350** sqlite3_value_text()), or for ensuring that values to be used as btree
57351** keys are strings. In the former case a NULL pointer is returned the
57352** user and the later is an internal programming error.
57353*/
57354SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
57355  int rc = SQLITE_OK;
57356  int fg = pMem->flags;
57357  const int nByte = 32;
57358
57359  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57360  assert( !(fg&MEM_Zero) );
57361  assert( !(fg&(MEM_Str|MEM_Blob)) );
57362  assert( fg&(MEM_Int|MEM_Real) );
57363  assert( (pMem->flags&MEM_RowSet)==0 );
57364  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57365
57366
57367  if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
57368    return SQLITE_NOMEM;
57369  }
57370
57371  /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
57372  ** string representation of the value. Then, if the required encoding
57373  ** is UTF-16le or UTF-16be do a translation.
57374  **
57375  ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
57376  */
57377  if( fg & MEM_Int ){
57378    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
57379  }else{
57380    assert( fg & MEM_Real );
57381    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
57382  }
57383  pMem->n = sqlite3Strlen30(pMem->z);
57384  pMem->enc = SQLITE_UTF8;
57385  pMem->flags |= MEM_Str|MEM_Term;
57386  sqlite3VdbeChangeEncoding(pMem, enc);
57387  return rc;
57388}
57389
57390/*
57391** Memory cell pMem contains the context of an aggregate function.
57392** This routine calls the finalize method for that function.  The
57393** result of the aggregate is stored back into pMem.
57394**
57395** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
57396** otherwise.
57397*/
57398SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
57399  int rc = SQLITE_OK;
57400  if( ALWAYS(pFunc && pFunc->xFinalize) ){
57401    sqlite3_context ctx;
57402    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
57403    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57404    memset(&ctx, 0, sizeof(ctx));
57405    ctx.s.flags = MEM_Null;
57406    ctx.s.db = pMem->db;
57407    ctx.pMem = pMem;
57408    ctx.pFunc = pFunc;
57409    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
57410    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
57411    sqlite3DbFree(pMem->db, pMem->zMalloc);
57412    memcpy(pMem, &ctx.s, sizeof(ctx.s));
57413    rc = ctx.isError;
57414  }
57415  return rc;
57416}
57417
57418/*
57419** If the memory cell contains a string value that must be freed by
57420** invoking an external callback, free it now. Calling this function
57421** does not free any Mem.zMalloc buffer.
57422*/
57423SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
57424  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
57425  if( p->flags&MEM_Agg ){
57426    sqlite3VdbeMemFinalize(p, p->u.pDef);
57427    assert( (p->flags & MEM_Agg)==0 );
57428    sqlite3VdbeMemRelease(p);
57429  }else if( p->flags&MEM_Dyn && p->xDel ){
57430    assert( (p->flags&MEM_RowSet)==0 );
57431    p->xDel((void *)p->z);
57432    p->xDel = 0;
57433  }else if( p->flags&MEM_RowSet ){
57434    sqlite3RowSetClear(p->u.pRowSet);
57435  }else if( p->flags&MEM_Frame ){
57436    sqlite3VdbeMemSetNull(p);
57437  }
57438}
57439
57440/*
57441** Release any memory held by the Mem. This may leave the Mem in an
57442** inconsistent state, for example with (Mem.z==0) and
57443** (Mem.type==SQLITE_TEXT).
57444*/
57445SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
57446  MemReleaseExt(p);
57447  sqlite3DbFree(p->db, p->zMalloc);
57448  p->z = 0;
57449  p->zMalloc = 0;
57450  p->xDel = 0;
57451}
57452
57453/*
57454** Convert a 64-bit IEEE double into a 64-bit signed integer.
57455** If the double is too large, return 0x8000000000000000.
57456**
57457** Most systems appear to do this simply by assigning
57458** variables and without the extra range tests.  But
57459** there are reports that windows throws an expection
57460** if the floating point value is out of range. (See ticket #2880.)
57461** Because we do not completely understand the problem, we will
57462** take the conservative approach and always do range tests
57463** before attempting the conversion.
57464*/
57465static i64 doubleToInt64(double r){
57466#ifdef SQLITE_OMIT_FLOATING_POINT
57467  /* When floating-point is omitted, double and int64 are the same thing */
57468  return r;
57469#else
57470  /*
57471  ** Many compilers we encounter do not define constants for the
57472  ** minimum and maximum 64-bit integers, or they define them
57473  ** inconsistently.  And many do not understand the "LL" notation.
57474  ** So we define our own static constants here using nothing
57475  ** larger than a 32-bit integer constant.
57476  */
57477  static const i64 maxInt = LARGEST_INT64;
57478  static const i64 minInt = SMALLEST_INT64;
57479
57480  if( r<(double)minInt ){
57481    return minInt;
57482  }else if( r>(double)maxInt ){
57483    /* minInt is correct here - not maxInt.  It turns out that assigning
57484    ** a very large positive number to an integer results in a very large
57485    ** negative integer.  This makes no sense, but it is what x86 hardware
57486    ** does so for compatibility we will do the same in software. */
57487    return minInt;
57488  }else{
57489    return (i64)r;
57490  }
57491#endif
57492}
57493
57494/*
57495** Return some kind of integer value which is the best we can do
57496** at representing the value that *pMem describes as an integer.
57497** If pMem is an integer, then the value is exact.  If pMem is
57498** a floating-point then the value returned is the integer part.
57499** If pMem is a string or blob, then we make an attempt to convert
57500** it into a integer and return that.  If pMem represents an
57501** an SQL-NULL value, return 0.
57502**
57503** If pMem represents a string value, its encoding might be changed.
57504*/
57505SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
57506  int flags;
57507  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57508  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57509  flags = pMem->flags;
57510  if( flags & MEM_Int ){
57511    return pMem->u.i;
57512  }else if( flags & MEM_Real ){
57513    return doubleToInt64(pMem->r);
57514  }else if( flags & (MEM_Str|MEM_Blob) ){
57515    i64 value = 0;
57516    assert( pMem->z || pMem->n==0 );
57517    testcase( pMem->z==0 );
57518    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
57519    return value;
57520  }else{
57521    return 0;
57522  }
57523}
57524
57525/*
57526** Return the best representation of pMem that we can get into a
57527** double.  If pMem is already a double or an integer, return its
57528** value.  If it is a string or blob, try to convert it to a double.
57529** If it is a NULL, return 0.0.
57530*/
57531SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
57532  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57533  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57534  if( pMem->flags & MEM_Real ){
57535    return pMem->r;
57536  }else if( pMem->flags & MEM_Int ){
57537    return (double)pMem->u.i;
57538  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
57539    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
57540    double val = (double)0;
57541    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
57542    return val;
57543  }else{
57544    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
57545    return (double)0;
57546  }
57547}
57548
57549/*
57550** The MEM structure is already a MEM_Real.  Try to also make it a
57551** MEM_Int if we can.
57552*/
57553SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
57554  assert( pMem->flags & MEM_Real );
57555  assert( (pMem->flags & MEM_RowSet)==0 );
57556  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57557  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57558
57559  pMem->u.i = doubleToInt64(pMem->r);
57560
57561  /* Only mark the value as an integer if
57562  **
57563  **    (1) the round-trip conversion real->int->real is a no-op, and
57564  **    (2) The integer is neither the largest nor the smallest
57565  **        possible integer (ticket #3922)
57566  **
57567  ** The second and third terms in the following conditional enforces
57568  ** the second condition under the assumption that addition overflow causes
57569  ** values to wrap around.  On x86 hardware, the third term is always
57570  ** true and could be omitted.  But we leave it in because other
57571  ** architectures might behave differently.
57572  */
57573  if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
57574      && ALWAYS(pMem->u.i<LARGEST_INT64) ){
57575    pMem->flags |= MEM_Int;
57576  }
57577}
57578
57579/*
57580** Convert pMem to type integer.  Invalidate any prior representations.
57581*/
57582SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
57583  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57584  assert( (pMem->flags & MEM_RowSet)==0 );
57585  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57586
57587  pMem->u.i = sqlite3VdbeIntValue(pMem);
57588  MemSetTypeFlag(pMem, MEM_Int);
57589  return SQLITE_OK;
57590}
57591
57592/*
57593** Convert pMem so that it is of type MEM_Real.
57594** Invalidate any prior representations.
57595*/
57596SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
57597  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57598  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
57599
57600  pMem->r = sqlite3VdbeRealValue(pMem);
57601  MemSetTypeFlag(pMem, MEM_Real);
57602  return SQLITE_OK;
57603}
57604
57605/*
57606** Convert pMem so that it has types MEM_Real or MEM_Int or both.
57607** Invalidate any prior representations.
57608**
57609** Every effort is made to force the conversion, even if the input
57610** is a string that does not look completely like a number.  Convert
57611** as much of the string as we can and ignore the rest.
57612*/
57613SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
57614  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
57615    assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
57616    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57617    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
57618      MemSetTypeFlag(pMem, MEM_Int);
57619    }else{
57620      pMem->r = sqlite3VdbeRealValue(pMem);
57621      MemSetTypeFlag(pMem, MEM_Real);
57622      sqlite3VdbeIntegerAffinity(pMem);
57623    }
57624  }
57625  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
57626  pMem->flags &= ~(MEM_Str|MEM_Blob);
57627  return SQLITE_OK;
57628}
57629
57630/*
57631** Delete any previous value and set the value stored in *pMem to NULL.
57632*/
57633SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
57634  if( pMem->flags & MEM_Frame ){
57635    VdbeFrame *pFrame = pMem->u.pFrame;
57636    pFrame->pParent = pFrame->v->pDelFrame;
57637    pFrame->v->pDelFrame = pFrame;
57638  }
57639  if( pMem->flags & MEM_RowSet ){
57640    sqlite3RowSetClear(pMem->u.pRowSet);
57641  }
57642  MemSetTypeFlag(pMem, MEM_Null);
57643  pMem->type = SQLITE_NULL;
57644}
57645
57646/*
57647** Delete any previous value and set the value to be a BLOB of length
57648** n containing all zeros.
57649*/
57650SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
57651  sqlite3VdbeMemRelease(pMem);
57652  pMem->flags = MEM_Blob|MEM_Zero;
57653  pMem->type = SQLITE_BLOB;
57654  pMem->n = 0;
57655  if( n<0 ) n = 0;
57656  pMem->u.nZero = n;
57657  pMem->enc = SQLITE_UTF8;
57658
57659#ifdef SQLITE_OMIT_INCRBLOB
57660  sqlite3VdbeMemGrow(pMem, n, 0);
57661  if( pMem->z ){
57662    pMem->n = n;
57663    memset(pMem->z, 0, n);
57664  }
57665#endif
57666}
57667
57668/*
57669** Delete any previous value and set the value stored in *pMem to val,
57670** manifest type INTEGER.
57671*/
57672SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
57673  sqlite3VdbeMemRelease(pMem);
57674  pMem->u.i = val;
57675  pMem->flags = MEM_Int;
57676  pMem->type = SQLITE_INTEGER;
57677}
57678
57679#ifndef SQLITE_OMIT_FLOATING_POINT
57680/*
57681** Delete any previous value and set the value stored in *pMem to val,
57682** manifest type REAL.
57683*/
57684SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
57685  if( sqlite3IsNaN(val) ){
57686    sqlite3VdbeMemSetNull(pMem);
57687  }else{
57688    sqlite3VdbeMemRelease(pMem);
57689    pMem->r = val;
57690    pMem->flags = MEM_Real;
57691    pMem->type = SQLITE_FLOAT;
57692  }
57693}
57694#endif
57695
57696/*
57697** Delete any previous value and set the value of pMem to be an
57698** empty boolean index.
57699*/
57700SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
57701  sqlite3 *db = pMem->db;
57702  assert( db!=0 );
57703  assert( (pMem->flags & MEM_RowSet)==0 );
57704  sqlite3VdbeMemRelease(pMem);
57705  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
57706  if( db->mallocFailed ){
57707    pMem->flags = MEM_Null;
57708  }else{
57709    assert( pMem->zMalloc );
57710    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
57711                                       sqlite3DbMallocSize(db, pMem->zMalloc));
57712    assert( pMem->u.pRowSet!=0 );
57713    pMem->flags = MEM_RowSet;
57714  }
57715}
57716
57717/*
57718** Return true if the Mem object contains a TEXT or BLOB that is
57719** too large - whose size exceeds SQLITE_MAX_LENGTH.
57720*/
57721SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
57722  assert( p->db!=0 );
57723  if( p->flags & (MEM_Str|MEM_Blob) ){
57724    int n = p->n;
57725    if( p->flags & MEM_Zero ){
57726      n += p->u.nZero;
57727    }
57728    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
57729  }
57730  return 0;
57731}
57732
57733#ifdef SQLITE_DEBUG
57734/*
57735** This routine prepares a memory cell for modication by breaking
57736** its link to a shallow copy and by marking any current shallow
57737** copies of this cell as invalid.
57738**
57739** This is used for testing and debugging only - to make sure shallow
57740** copies are not misused.
57741*/
57742SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
57743  int i;
57744  Mem *pX;
57745  for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
57746    if( pX->pScopyFrom==pMem ){
57747      pX->flags |= MEM_Invalid;
57748      pX->pScopyFrom = 0;
57749    }
57750  }
57751  pMem->pScopyFrom = 0;
57752}
57753#endif /* SQLITE_DEBUG */
57754
57755/*
57756** Size of struct Mem not including the Mem.zMalloc member.
57757*/
57758#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
57759
57760/*
57761** Make an shallow copy of pFrom into pTo.  Prior contents of
57762** pTo are freed.  The pFrom->z field is not duplicated.  If
57763** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57764** and flags gets srcType (either MEM_Ephem or MEM_Static).
57765*/
57766SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57767  assert( (pFrom->flags & MEM_RowSet)==0 );
57768  MemReleaseExt(pTo);
57769  memcpy(pTo, pFrom, MEMCELLSIZE);
57770  pTo->xDel = 0;
57771  if( (pFrom->flags&MEM_Static)==0 ){
57772    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57773    assert( srcType==MEM_Ephem || srcType==MEM_Static );
57774    pTo->flags |= srcType;
57775  }
57776}
57777
57778/*
57779** Make a full copy of pFrom into pTo.  Prior contents of pTo are
57780** freed before the copy is made.
57781*/
57782SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57783  int rc = SQLITE_OK;
57784
57785  assert( (pFrom->flags & MEM_RowSet)==0 );
57786  MemReleaseExt(pTo);
57787  memcpy(pTo, pFrom, MEMCELLSIZE);
57788  pTo->flags &= ~MEM_Dyn;
57789
57790  if( pTo->flags&(MEM_Str|MEM_Blob) ){
57791    if( 0==(pFrom->flags&MEM_Static) ){
57792      pTo->flags |= MEM_Ephem;
57793      rc = sqlite3VdbeMemMakeWriteable(pTo);
57794    }
57795  }
57796
57797  return rc;
57798}
57799
57800/*
57801** Transfer the contents of pFrom to pTo. Any existing value in pTo is
57802** freed. If pFrom contains ephemeral data, a copy is made.
57803**
57804** pFrom contains an SQL NULL when this routine returns.
57805*/
57806SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
57807  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
57808  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
57809  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
57810
57811  sqlite3VdbeMemRelease(pTo);
57812  memcpy(pTo, pFrom, sizeof(Mem));
57813  pFrom->flags = MEM_Null;
57814  pFrom->xDel = 0;
57815  pFrom->zMalloc = 0;
57816}
57817
57818/*
57819** Change the value of a Mem to be a string or a BLOB.
57820**
57821** The memory management strategy depends on the value of the xDel
57822** parameter. If the value passed is SQLITE_TRANSIENT, then the
57823** string is copied into a (possibly existing) buffer managed by the
57824** Mem structure. Otherwise, any existing buffer is freed and the
57825** pointer copied.
57826**
57827** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
57828** size limit) then no memory allocation occurs.  If the string can be
57829** stored without allocating memory, then it is.  If a memory allocation
57830** is required to store the string, then value of pMem is unchanged.  In
57831** either case, SQLITE_TOOBIG is returned.
57832*/
57833SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
57834  Mem *pMem,          /* Memory cell to set to string value */
57835  const char *z,      /* String pointer */
57836  int n,              /* Bytes in string, or negative */
57837  u8 enc,             /* Encoding of z.  0 for BLOBs */
57838  void (*xDel)(void*) /* Destructor function */
57839){
57840  int nByte = n;      /* New value for pMem->n */
57841  int iLimit;         /* Maximum allowed string or blob size */
57842  u16 flags = 0;      /* New value for pMem->flags */
57843
57844  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57845  assert( (pMem->flags & MEM_RowSet)==0 );
57846
57847  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
57848  if( !z ){
57849    sqlite3VdbeMemSetNull(pMem);
57850    return SQLITE_OK;
57851  }
57852
57853  if( pMem->db ){
57854    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
57855  }else{
57856    iLimit = SQLITE_MAX_LENGTH;
57857  }
57858  flags = (enc==0?MEM_Blob:MEM_Str);
57859  if( nByte<0 ){
57860    assert( enc!=0 );
57861    if( enc==SQLITE_UTF8 ){
57862      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
57863    }else{
57864      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
57865    }
57866    flags |= MEM_Term;
57867  }
57868
57869  /* The following block sets the new values of Mem.z and Mem.xDel. It
57870  ** also sets a flag in local variable "flags" to indicate the memory
57871  ** management (one of MEM_Dyn or MEM_Static).
57872  */
57873  if( xDel==SQLITE_TRANSIENT ){
57874    int nAlloc = nByte;
57875    if( flags&MEM_Term ){
57876      nAlloc += (enc==SQLITE_UTF8?1:2);
57877    }
57878    if( nByte>iLimit ){
57879      return SQLITE_TOOBIG;
57880    }
57881    if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
57882      return SQLITE_NOMEM;
57883    }
57884    memcpy(pMem->z, z, nAlloc);
57885  }else if( xDel==SQLITE_DYNAMIC ){
57886    sqlite3VdbeMemRelease(pMem);
57887    pMem->zMalloc = pMem->z = (char *)z;
57888    pMem->xDel = 0;
57889  }else{
57890    sqlite3VdbeMemRelease(pMem);
57891    pMem->z = (char *)z;
57892    pMem->xDel = xDel;
57893    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
57894  }
57895
57896  pMem->n = nByte;
57897  pMem->flags = flags;
57898  pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
57899  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
57900
57901#ifndef SQLITE_OMIT_UTF16
57902  if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
57903    return SQLITE_NOMEM;
57904  }
57905#endif
57906
57907  if( nByte>iLimit ){
57908    return SQLITE_TOOBIG;
57909  }
57910
57911  return SQLITE_OK;
57912}
57913
57914/*
57915** Compare the values contained by the two memory cells, returning
57916** negative, zero or positive if pMem1 is less than, equal to, or greater
57917** than pMem2. Sorting order is NULL's first, followed by numbers (integers
57918** and reals) sorted numerically, followed by text ordered by the collating
57919** sequence pColl and finally blob's ordered by memcmp().
57920**
57921** Two NULL values are considered equal by this function.
57922*/
57923SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
57924  int rc;
57925  int f1, f2;
57926  int combined_flags;
57927
57928  f1 = pMem1->flags;
57929  f2 = pMem2->flags;
57930  combined_flags = f1|f2;
57931  assert( (combined_flags & MEM_RowSet)==0 );
57932
57933  /* If one value is NULL, it is less than the other. If both values
57934  ** are NULL, return 0.
57935  */
57936  if( combined_flags&MEM_Null ){
57937    return (f2&MEM_Null) - (f1&MEM_Null);
57938  }
57939
57940  /* If one value is a number and the other is not, the number is less.
57941  ** If both are numbers, compare as reals if one is a real, or as integers
57942  ** if both values are integers.
57943  */
57944  if( combined_flags&(MEM_Int|MEM_Real) ){
57945    if( !(f1&(MEM_Int|MEM_Real)) ){
57946      return 1;
57947    }
57948    if( !(f2&(MEM_Int|MEM_Real)) ){
57949      return -1;
57950    }
57951    if( (f1 & f2 & MEM_Int)==0 ){
57952      double r1, r2;
57953      if( (f1&MEM_Real)==0 ){
57954        r1 = (double)pMem1->u.i;
57955      }else{
57956        r1 = pMem1->r;
57957      }
57958      if( (f2&MEM_Real)==0 ){
57959        r2 = (double)pMem2->u.i;
57960      }else{
57961        r2 = pMem2->r;
57962      }
57963      if( r1<r2 ) return -1;
57964      if( r1>r2 ) return 1;
57965      return 0;
57966    }else{
57967      assert( f1&MEM_Int );
57968      assert( f2&MEM_Int );
57969      if( pMem1->u.i < pMem2->u.i ) return -1;
57970      if( pMem1->u.i > pMem2->u.i ) return 1;
57971      return 0;
57972    }
57973  }
57974
57975  /* If one value is a string and the other is a blob, the string is less.
57976  ** If both are strings, compare using the collating functions.
57977  */
57978  if( combined_flags&MEM_Str ){
57979    if( (f1 & MEM_Str)==0 ){
57980      return 1;
57981    }
57982    if( (f2 & MEM_Str)==0 ){
57983      return -1;
57984    }
57985
57986    assert( pMem1->enc==pMem2->enc );
57987    assert( pMem1->enc==SQLITE_UTF8 ||
57988            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
57989
57990    /* The collation sequence must be defined at this point, even if
57991    ** the user deletes the collation sequence after the vdbe program is
57992    ** compiled (this was not always the case).
57993    */
57994    assert( !pColl || pColl->xCmp );
57995
57996    if( pColl ){
57997      if( pMem1->enc==pColl->enc ){
57998        /* The strings are already in the correct encoding.  Call the
57999        ** comparison function directly */
58000        return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
58001      }else{
58002        const void *v1, *v2;
58003        int n1, n2;
58004        Mem c1;
58005        Mem c2;
58006        memset(&c1, 0, sizeof(c1));
58007        memset(&c2, 0, sizeof(c2));
58008        sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
58009        sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
58010        v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
58011        n1 = v1==0 ? 0 : c1.n;
58012        v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
58013        n2 = v2==0 ? 0 : c2.n;
58014        rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
58015        sqlite3VdbeMemRelease(&c1);
58016        sqlite3VdbeMemRelease(&c2);
58017        return rc;
58018      }
58019    }
58020    /* If a NULL pointer was passed as the collate function, fall through
58021    ** to the blob case and use memcmp().  */
58022  }
58023
58024  /* Both values must be blobs.  Compare using memcmp().  */
58025  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
58026  if( rc==0 ){
58027    rc = pMem1->n - pMem2->n;
58028  }
58029  return rc;
58030}
58031
58032/*
58033** Move data out of a btree key or data field and into a Mem structure.
58034** The data or key is taken from the entry that pCur is currently pointing
58035** to.  offset and amt determine what portion of the data or key to retrieve.
58036** key is true to get the key or false to get data.  The result is written
58037** into the pMem element.
58038**
58039** The pMem structure is assumed to be uninitialized.  Any prior content
58040** is overwritten without being freed.
58041**
58042** If this routine fails for any reason (malloc returns NULL or unable
58043** to read from the disk) then the pMem is left in an inconsistent state.
58044*/
58045SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
58046  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
58047  int offset,       /* Offset from the start of data to return bytes from. */
58048  int amt,          /* Number of bytes to return. */
58049  int key,          /* If true, retrieve from the btree key, not data. */
58050  Mem *pMem         /* OUT: Return data in this Mem structure. */
58051){
58052  char *zData;        /* Data from the btree layer */
58053  int available = 0;  /* Number of bytes available on the local btree page */
58054  int rc = SQLITE_OK; /* Return code */
58055
58056  assert( sqlite3BtreeCursorIsValid(pCur) );
58057
58058  /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
58059  ** that both the BtShared and database handle mutexes are held. */
58060  assert( (pMem->flags & MEM_RowSet)==0 );
58061  if( key ){
58062    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
58063  }else{
58064    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
58065  }
58066  assert( zData!=0 );
58067
58068  if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
58069    sqlite3VdbeMemRelease(pMem);
58070    pMem->z = &zData[offset];
58071    pMem->flags = MEM_Blob|MEM_Ephem;
58072  }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
58073    pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
58074    pMem->enc = 0;
58075    pMem->type = SQLITE_BLOB;
58076    if( key ){
58077      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
58078    }else{
58079      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
58080    }
58081    pMem->z[amt] = 0;
58082    pMem->z[amt+1] = 0;
58083    if( rc!=SQLITE_OK ){
58084      sqlite3VdbeMemRelease(pMem);
58085    }
58086  }
58087  pMem->n = amt;
58088
58089  return rc;
58090}
58091
58092/* This function is only available internally, it is not part of the
58093** external API. It works in a similar way to sqlite3_value_text(),
58094** except the data returned is in the encoding specified by the second
58095** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
58096** SQLITE_UTF8.
58097**
58098** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
58099** If that is the case, then the result must be aligned on an even byte
58100** boundary.
58101*/
58102SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
58103  if( !pVal ) return 0;
58104
58105  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
58106  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
58107  assert( (pVal->flags & MEM_RowSet)==0 );
58108
58109  if( pVal->flags&MEM_Null ){
58110    return 0;
58111  }
58112  assert( (MEM_Blob>>3) == MEM_Str );
58113  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
58114  expandBlob(pVal);
58115  if( pVal->flags&MEM_Str ){
58116    sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
58117    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
58118      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
58119      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
58120        return 0;
58121      }
58122    }
58123    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
58124  }else{
58125    assert( (pVal->flags&MEM_Blob)==0 );
58126    sqlite3VdbeMemStringify(pVal, enc);
58127    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
58128  }
58129  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
58130              || pVal->db->mallocFailed );
58131  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
58132    return pVal->z;
58133  }else{
58134    return 0;
58135  }
58136}
58137
58138/*
58139** Create a new sqlite3_value object.
58140*/
58141SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
58142  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
58143  if( p ){
58144    p->flags = MEM_Null;
58145    p->type = SQLITE_NULL;
58146    p->db = db;
58147  }
58148  return p;
58149}
58150
58151/*
58152** Create a new sqlite3_value object, containing the value of pExpr.
58153**
58154** This only works for very simple expressions that consist of one constant
58155** token (i.e. "5", "5.1", "'a string'"). If the expression can
58156** be converted directly into a value, then the value is allocated and
58157** a pointer written to *ppVal. The caller is responsible for deallocating
58158** the value by passing it to sqlite3ValueFree() later on. If the expression
58159** cannot be converted to a value, then *ppVal is set to NULL.
58160*/
58161SQLITE_PRIVATE int sqlite3ValueFromExpr(
58162  sqlite3 *db,              /* The database connection */
58163  Expr *pExpr,              /* The expression to evaluate */
58164  u8 enc,                   /* Encoding to use */
58165  u8 affinity,              /* Affinity to use */
58166  sqlite3_value **ppVal     /* Write the new value here */
58167){
58168  int op;
58169  char *zVal = 0;
58170  sqlite3_value *pVal = 0;
58171  int negInt = 1;
58172  const char *zNeg = "";
58173
58174  if( !pExpr ){
58175    *ppVal = 0;
58176    return SQLITE_OK;
58177  }
58178  op = pExpr->op;
58179
58180  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
58181  ** The ifdef here is to enable us to achieve 100% branch test coverage even
58182  ** when SQLITE_ENABLE_STAT2 is omitted.
58183  */
58184#ifdef SQLITE_ENABLE_STAT2
58185  if( op==TK_REGISTER ) op = pExpr->op2;
58186#else
58187  if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
58188#endif
58189
58190  /* Handle negative integers in a single step.  This is needed in the
58191  ** case when the value is -9223372036854775808.
58192  */
58193  if( op==TK_UMINUS
58194   && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
58195    pExpr = pExpr->pLeft;
58196    op = pExpr->op;
58197    negInt = -1;
58198    zNeg = "-";
58199  }
58200
58201  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
58202    pVal = sqlite3ValueNew(db);
58203    if( pVal==0 ) goto no_mem;
58204    if( ExprHasProperty(pExpr, EP_IntValue) ){
58205      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
58206    }else{
58207      zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
58208      if( zVal==0 ) goto no_mem;
58209      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
58210      if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
58211    }
58212    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
58213      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
58214    }else{
58215      sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
58216    }
58217    if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
58218    if( enc!=SQLITE_UTF8 ){
58219      sqlite3VdbeChangeEncoding(pVal, enc);
58220    }
58221  }else if( op==TK_UMINUS ) {
58222    /* This branch happens for multiple negative signs.  Ex: -(-5) */
58223    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
58224      sqlite3VdbeMemNumerify(pVal);
58225      if( pVal->u.i==SMALLEST_INT64 ){
58226        pVal->flags &= MEM_Int;
58227        pVal->flags |= MEM_Real;
58228        pVal->r = (double)LARGEST_INT64;
58229      }else{
58230        pVal->u.i = -pVal->u.i;
58231      }
58232      pVal->r = -pVal->r;
58233      sqlite3ValueApplyAffinity(pVal, affinity, enc);
58234    }
58235  }else if( op==TK_NULL ){
58236    pVal = sqlite3ValueNew(db);
58237    if( pVal==0 ) goto no_mem;
58238  }
58239#ifndef SQLITE_OMIT_BLOB_LITERAL
58240  else if( op==TK_BLOB ){
58241    int nVal;
58242    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
58243    assert( pExpr->u.zToken[1]=='\'' );
58244    pVal = sqlite3ValueNew(db);
58245    if( !pVal ) goto no_mem;
58246    zVal = &pExpr->u.zToken[2];
58247    nVal = sqlite3Strlen30(zVal)-1;
58248    assert( zVal[nVal]=='\'' );
58249    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
58250                         0, SQLITE_DYNAMIC);
58251  }
58252#endif
58253
58254  if( pVal ){
58255    sqlite3VdbeMemStoreType(pVal);
58256  }
58257  *ppVal = pVal;
58258  return SQLITE_OK;
58259
58260no_mem:
58261  db->mallocFailed = 1;
58262  sqlite3DbFree(db, zVal);
58263  sqlite3ValueFree(pVal);
58264  *ppVal = 0;
58265  return SQLITE_NOMEM;
58266}
58267
58268/*
58269** Change the string value of an sqlite3_value object
58270*/
58271SQLITE_PRIVATE void sqlite3ValueSetStr(
58272  sqlite3_value *v,     /* Value to be set */
58273  int n,                /* Length of string z */
58274  const void *z,        /* Text of the new string */
58275  u8 enc,               /* Encoding to use */
58276  void (*xDel)(void*)   /* Destructor for the string */
58277){
58278  if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
58279}
58280
58281/*
58282** Free an sqlite3_value object
58283*/
58284SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
58285  if( !v ) return;
58286  sqlite3VdbeMemRelease((Mem *)v);
58287  sqlite3DbFree(((Mem*)v)->db, v);
58288}
58289
58290/*
58291** Return the number of bytes in the sqlite3_value object assuming
58292** that it uses the encoding "enc"
58293*/
58294SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
58295  Mem *p = (Mem*)pVal;
58296  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
58297    if( p->flags & MEM_Zero ){
58298      return p->n + p->u.nZero;
58299    }else{
58300      return p->n;
58301    }
58302  }
58303  return 0;
58304}
58305
58306/************** End of vdbemem.c *********************************************/
58307/************** Begin file vdbeaux.c *****************************************/
58308/*
58309** 2003 September 6
58310**
58311** The author disclaims copyright to this source code.  In place of
58312** a legal notice, here is a blessing:
58313**
58314**    May you do good and not evil.
58315**    May you find forgiveness for yourself and forgive others.
58316**    May you share freely, never taking more than you give.
58317**
58318*************************************************************************
58319** This file contains code used for creating, destroying, and populating
58320** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
58321** to version 2.8.7, all this code was combined into the vdbe.c source file.
58322** But that file was getting too big so this subroutines were split out.
58323*/
58324
58325
58326
58327/*
58328** When debugging the code generator in a symbolic debugger, one can
58329** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
58330** as they are added to the instruction stream.
58331*/
58332#ifdef SQLITE_DEBUG
58333SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
58334#endif
58335
58336
58337/*
58338** Create a new virtual database engine.
58339*/
58340SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
58341  Vdbe *p;
58342  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
58343  if( p==0 ) return 0;
58344  p->db = db;
58345  if( db->pVdbe ){
58346    db->pVdbe->pPrev = p;
58347  }
58348  p->pNext = db->pVdbe;
58349  p->pPrev = 0;
58350  db->pVdbe = p;
58351  p->magic = VDBE_MAGIC_INIT;
58352  return p;
58353}
58354
58355/*
58356** Remember the SQL string for a prepared statement.
58357*/
58358SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
58359  assert( isPrepareV2==1 || isPrepareV2==0 );
58360  if( p==0 ) return;
58361#ifdef SQLITE_OMIT_TRACE
58362  if( !isPrepareV2 ) return;
58363#endif
58364  assert( p->zSql==0 );
58365  p->zSql = sqlite3DbStrNDup(p->db, z, n);
58366  p->isPrepareV2 = (u8)isPrepareV2;
58367}
58368
58369/*
58370** Return the SQL associated with a prepared statement
58371*/
58372SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
58373  Vdbe *p = (Vdbe *)pStmt;
58374  return (p && p->isPrepareV2) ? p->zSql : 0;
58375}
58376
58377/*
58378** Swap all content between two VDBE structures.
58379*/
58380SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
58381  Vdbe tmp, *pTmp;
58382  char *zTmp;
58383  tmp = *pA;
58384  *pA = *pB;
58385  *pB = tmp;
58386  pTmp = pA->pNext;
58387  pA->pNext = pB->pNext;
58388  pB->pNext = pTmp;
58389  pTmp = pA->pPrev;
58390  pA->pPrev = pB->pPrev;
58391  pB->pPrev = pTmp;
58392  zTmp = pA->zSql;
58393  pA->zSql = pB->zSql;
58394  pB->zSql = zTmp;
58395  pB->isPrepareV2 = pA->isPrepareV2;
58396}
58397
58398#ifdef SQLITE_DEBUG
58399/*
58400** Turn tracing on or off
58401*/
58402SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
58403  p->trace = trace;
58404}
58405#endif
58406
58407/*
58408** Resize the Vdbe.aOp array so that it is at least one op larger than
58409** it was.
58410**
58411** If an out-of-memory error occurs while resizing the array, return
58412** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
58413** unchanged (this is so that any opcodes already allocated can be
58414** correctly deallocated along with the rest of the Vdbe).
58415*/
58416static int growOpArray(Vdbe *p){
58417  VdbeOp *pNew;
58418  int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
58419  pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
58420  if( pNew ){
58421    p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
58422    p->aOp = pNew;
58423  }
58424  return (pNew ? SQLITE_OK : SQLITE_NOMEM);
58425}
58426
58427/*
58428** Add a new instruction to the list of instructions current in the
58429** VDBE.  Return the address of the new instruction.
58430**
58431** Parameters:
58432**
58433**    p               Pointer to the VDBE
58434**
58435**    op              The opcode for this instruction
58436**
58437**    p1, p2, p3      Operands
58438**
58439** Use the sqlite3VdbeResolveLabel() function to fix an address and
58440** the sqlite3VdbeChangeP4() function to change the value of the P4
58441** operand.
58442*/
58443SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
58444  int i;
58445  VdbeOp *pOp;
58446
58447  i = p->nOp;
58448  assert( p->magic==VDBE_MAGIC_INIT );
58449  assert( op>0 && op<0xff );
58450  if( p->nOpAlloc<=i ){
58451    if( growOpArray(p) ){
58452      return 1;
58453    }
58454  }
58455  p->nOp++;
58456  pOp = &p->aOp[i];
58457  pOp->opcode = (u8)op;
58458  pOp->p5 = 0;
58459  pOp->p1 = p1;
58460  pOp->p2 = p2;
58461  pOp->p3 = p3;
58462  pOp->p4.p = 0;
58463  pOp->p4type = P4_NOTUSED;
58464#ifdef SQLITE_DEBUG
58465  pOp->zComment = 0;
58466  if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
58467#endif
58468#ifdef VDBE_PROFILE
58469  pOp->cycles = 0;
58470  pOp->cnt = 0;
58471#endif
58472  return i;
58473}
58474SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
58475  return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
58476}
58477SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
58478  return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
58479}
58480SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
58481  return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
58482}
58483
58484
58485/*
58486** Add an opcode that includes the p4 value as a pointer.
58487*/
58488SQLITE_PRIVATE int sqlite3VdbeAddOp4(
58489  Vdbe *p,            /* Add the opcode to this VM */
58490  int op,             /* The new opcode */
58491  int p1,             /* The P1 operand */
58492  int p2,             /* The P2 operand */
58493  int p3,             /* The P3 operand */
58494  const char *zP4,    /* The P4 operand */
58495  int p4type          /* P4 operand type */
58496){
58497  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
58498  sqlite3VdbeChangeP4(p, addr, zP4, p4type);
58499  return addr;
58500}
58501
58502/*
58503** Add an OP_ParseSchema opcode.  This routine is broken out from
58504** sqlite3VdbeAddOp4() since it needs to also local all btrees.
58505**
58506** The zWhere string must have been obtained from sqlite3_malloc().
58507** This routine will take ownership of the allocated memory.
58508*/
58509SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
58510  int j;
58511  int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
58512  sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
58513  for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
58514}
58515
58516/*
58517** Add an opcode that includes the p4 value as an integer.
58518*/
58519SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
58520  Vdbe *p,            /* Add the opcode to this VM */
58521  int op,             /* The new opcode */
58522  int p1,             /* The P1 operand */
58523  int p2,             /* The P2 operand */
58524  int p3,             /* The P3 operand */
58525  int p4              /* The P4 operand as an integer */
58526){
58527  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
58528  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
58529  return addr;
58530}
58531
58532/*
58533** Create a new symbolic label for an instruction that has yet to be
58534** coded.  The symbolic label is really just a negative number.  The
58535** label can be used as the P2 value of an operation.  Later, when
58536** the label is resolved to a specific address, the VDBE will scan
58537** through its operation list and change all values of P2 which match
58538** the label into the resolved address.
58539**
58540** The VDBE knows that a P2 value is a label because labels are
58541** always negative and P2 values are suppose to be non-negative.
58542** Hence, a negative P2 value is a label that has yet to be resolved.
58543**
58544** Zero is returned if a malloc() fails.
58545*/
58546SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
58547  int i;
58548  i = p->nLabel++;
58549  assert( p->magic==VDBE_MAGIC_INIT );
58550  if( i>=p->nLabelAlloc ){
58551    int n = p->nLabelAlloc*2 + 5;
58552    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
58553                                       n*sizeof(p->aLabel[0]));
58554    p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
58555  }
58556  if( p->aLabel ){
58557    p->aLabel[i] = -1;
58558  }
58559  return -1-i;
58560}
58561
58562/*
58563** Resolve label "x" to be the address of the next instruction to
58564** be inserted.  The parameter "x" must have been obtained from
58565** a prior call to sqlite3VdbeMakeLabel().
58566*/
58567SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
58568  int j = -1-x;
58569  assert( p->magic==VDBE_MAGIC_INIT );
58570  assert( j>=0 && j<p->nLabel );
58571  if( p->aLabel ){
58572    p->aLabel[j] = p->nOp;
58573  }
58574}
58575
58576/*
58577** Mark the VDBE as one that can only be run one time.
58578*/
58579SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
58580  p->runOnlyOnce = 1;
58581}
58582
58583#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
58584
58585/*
58586** The following type and function are used to iterate through all opcodes
58587** in a Vdbe main program and each of the sub-programs (triggers) it may
58588** invoke directly or indirectly. It should be used as follows:
58589**
58590**   Op *pOp;
58591**   VdbeOpIter sIter;
58592**
58593**   memset(&sIter, 0, sizeof(sIter));
58594**   sIter.v = v;                            // v is of type Vdbe*
58595**   while( (pOp = opIterNext(&sIter)) ){
58596**     // Do something with pOp
58597**   }
58598**   sqlite3DbFree(v->db, sIter.apSub);
58599**
58600*/
58601typedef struct VdbeOpIter VdbeOpIter;
58602struct VdbeOpIter {
58603  Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
58604  SubProgram **apSub;        /* Array of subprograms */
58605  int nSub;                  /* Number of entries in apSub */
58606  int iAddr;                 /* Address of next instruction to return */
58607  int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
58608};
58609static Op *opIterNext(VdbeOpIter *p){
58610  Vdbe *v = p->v;
58611  Op *pRet = 0;
58612  Op *aOp;
58613  int nOp;
58614
58615  if( p->iSub<=p->nSub ){
58616
58617    if( p->iSub==0 ){
58618      aOp = v->aOp;
58619      nOp = v->nOp;
58620    }else{
58621      aOp = p->apSub[p->iSub-1]->aOp;
58622      nOp = p->apSub[p->iSub-1]->nOp;
58623    }
58624    assert( p->iAddr<nOp );
58625
58626    pRet = &aOp[p->iAddr];
58627    p->iAddr++;
58628    if( p->iAddr==nOp ){
58629      p->iSub++;
58630      p->iAddr = 0;
58631    }
58632
58633    if( pRet->p4type==P4_SUBPROGRAM ){
58634      int nByte = (p->nSub+1)*sizeof(SubProgram*);
58635      int j;
58636      for(j=0; j<p->nSub; j++){
58637        if( p->apSub[j]==pRet->p4.pProgram ) break;
58638      }
58639      if( j==p->nSub ){
58640        p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
58641        if( !p->apSub ){
58642          pRet = 0;
58643        }else{
58644          p->apSub[p->nSub++] = pRet->p4.pProgram;
58645        }
58646      }
58647    }
58648  }
58649
58650  return pRet;
58651}
58652
58653/*
58654** Check if the program stored in the VM associated with pParse may
58655** throw an ABORT exception (causing the statement, but not entire transaction
58656** to be rolled back). This condition is true if the main program or any
58657** sub-programs contains any of the following:
58658**
58659**   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
58660**   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
58661**   *  OP_Destroy
58662**   *  OP_VUpdate
58663**   *  OP_VRename
58664**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
58665**
58666** Then check that the value of Parse.mayAbort is true if an
58667** ABORT may be thrown, or false otherwise. Return true if it does
58668** match, or false otherwise. This function is intended to be used as
58669** part of an assert statement in the compiler. Similar to:
58670**
58671**   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
58672*/
58673SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
58674  int hasAbort = 0;
58675  Op *pOp;
58676  VdbeOpIter sIter;
58677  memset(&sIter, 0, sizeof(sIter));
58678  sIter.v = v;
58679
58680  while( (pOp = opIterNext(&sIter))!=0 ){
58681    int opcode = pOp->opcode;
58682    if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
58683#ifndef SQLITE_OMIT_FOREIGN_KEY
58684     || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
58685#endif
58686     || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
58687      && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
58688    ){
58689      hasAbort = 1;
58690      break;
58691    }
58692  }
58693  sqlite3DbFree(v->db, sIter.apSub);
58694
58695  /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
58696  ** If malloc failed, then the while() loop above may not have iterated
58697  ** through all opcodes and hasAbort may be set incorrectly. Return
58698  ** true for this case to prevent the assert() in the callers frame
58699  ** from failing.  */
58700  return ( v->db->mallocFailed || hasAbort==mayAbort );
58701}
58702#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
58703
58704/*
58705** Loop through the program looking for P2 values that are negative
58706** on jump instructions.  Each such value is a label.  Resolve the
58707** label by setting the P2 value to its correct non-zero value.
58708**
58709** This routine is called once after all opcodes have been inserted.
58710**
58711** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
58712** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
58713** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
58714**
58715** The Op.opflags field is set on all opcodes.
58716*/
58717static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
58718  int i;
58719  int nMaxArgs = *pMaxFuncArgs;
58720  Op *pOp;
58721  int *aLabel = p->aLabel;
58722  p->readOnly = 1;
58723  for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
58724    u8 opcode = pOp->opcode;
58725
58726    pOp->opflags = sqlite3OpcodeProperty[opcode];
58727    if( opcode==OP_Function || opcode==OP_AggStep ){
58728      if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
58729    }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
58730      p->readOnly = 0;
58731#ifndef SQLITE_OMIT_VIRTUALTABLE
58732    }else if( opcode==OP_VUpdate ){
58733      if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
58734    }else if( opcode==OP_VFilter ){
58735      int n;
58736      assert( p->nOp - i >= 3 );
58737      assert( pOp[-1].opcode==OP_Integer );
58738      n = pOp[-1].p1;
58739      if( n>nMaxArgs ) nMaxArgs = n;
58740#endif
58741    }else if( opcode==OP_Next || opcode==OP_SorterNext ){
58742      pOp->p4.xAdvance = sqlite3BtreeNext;
58743      pOp->p4type = P4_ADVANCE;
58744    }else if( opcode==OP_Prev ){
58745      pOp->p4.xAdvance = sqlite3BtreePrevious;
58746      pOp->p4type = P4_ADVANCE;
58747    }
58748
58749    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
58750      assert( -1-pOp->p2<p->nLabel );
58751      pOp->p2 = aLabel[-1-pOp->p2];
58752    }
58753  }
58754  sqlite3DbFree(p->db, p->aLabel);
58755  p->aLabel = 0;
58756
58757  *pMaxFuncArgs = nMaxArgs;
58758}
58759
58760/*
58761** Return the address of the next instruction to be inserted.
58762*/
58763SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
58764  assert( p->magic==VDBE_MAGIC_INIT );
58765  return p->nOp;
58766}
58767
58768/*
58769** This function returns a pointer to the array of opcodes associated with
58770** the Vdbe passed as the first argument. It is the callers responsibility
58771** to arrange for the returned array to be eventually freed using the
58772** vdbeFreeOpArray() function.
58773**
58774** Before returning, *pnOp is set to the number of entries in the returned
58775** array. Also, *pnMaxArg is set to the larger of its current value and
58776** the number of entries in the Vdbe.apArg[] array required to execute the
58777** returned program.
58778*/
58779SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
58780  VdbeOp *aOp = p->aOp;
58781  assert( aOp && !p->db->mallocFailed );
58782
58783  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
58784  assert( p->btreeMask==0 );
58785
58786  resolveP2Values(p, pnMaxArg);
58787  *pnOp = p->nOp;
58788  p->aOp = 0;
58789  return aOp;
58790}
58791
58792/*
58793** Add a whole list of operations to the operation stack.  Return the
58794** address of the first operation added.
58795*/
58796SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
58797  int addr;
58798  assert( p->magic==VDBE_MAGIC_INIT );
58799  if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
58800    return 0;
58801  }
58802  addr = p->nOp;
58803  if( ALWAYS(nOp>0) ){
58804    int i;
58805    VdbeOpList const *pIn = aOp;
58806    for(i=0; i<nOp; i++, pIn++){
58807      int p2 = pIn->p2;
58808      VdbeOp *pOut = &p->aOp[i+addr];
58809      pOut->opcode = pIn->opcode;
58810      pOut->p1 = pIn->p1;
58811      if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
58812        pOut->p2 = addr + ADDR(p2);
58813      }else{
58814        pOut->p2 = p2;
58815      }
58816      pOut->p3 = pIn->p3;
58817      pOut->p4type = P4_NOTUSED;
58818      pOut->p4.p = 0;
58819      pOut->p5 = 0;
58820#ifdef SQLITE_DEBUG
58821      pOut->zComment = 0;
58822      if( sqlite3VdbeAddopTrace ){
58823        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
58824      }
58825#endif
58826    }
58827    p->nOp += nOp;
58828  }
58829  return addr;
58830}
58831
58832/*
58833** Change the value of the P1 operand for a specific instruction.
58834** This routine is useful when a large program is loaded from a
58835** static array using sqlite3VdbeAddOpList but we want to make a
58836** few minor changes to the program.
58837*/
58838SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
58839  assert( p!=0 );
58840  if( ((u32)p->nOp)>addr ){
58841    p->aOp[addr].p1 = val;
58842  }
58843}
58844
58845/*
58846** Change the value of the P2 operand for a specific instruction.
58847** This routine is useful for setting a jump destination.
58848*/
58849SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
58850  assert( p!=0 );
58851  if( ((u32)p->nOp)>addr ){
58852    p->aOp[addr].p2 = val;
58853  }
58854}
58855
58856/*
58857** Change the value of the P3 operand for a specific instruction.
58858*/
58859SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
58860  assert( p!=0 );
58861  if( ((u32)p->nOp)>addr ){
58862    p->aOp[addr].p3 = val;
58863  }
58864}
58865
58866/*
58867** Change the value of the P5 operand for the most recently
58868** added operation.
58869*/
58870SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
58871  assert( p!=0 );
58872  if( p->aOp ){
58873    assert( p->nOp>0 );
58874    p->aOp[p->nOp-1].p5 = val;
58875  }
58876}
58877
58878/*
58879** Change the P2 operand of instruction addr so that it points to
58880** the address of the next instruction to be coded.
58881*/
58882SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58883  assert( addr>=0 );
58884  sqlite3VdbeChangeP2(p, addr, p->nOp);
58885}
58886
58887
58888/*
58889** If the input FuncDef structure is ephemeral, then free it.  If
58890** the FuncDef is not ephermal, then do nothing.
58891*/
58892static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
58893  if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
58894    sqlite3DbFree(db, pDef);
58895  }
58896}
58897
58898static void vdbeFreeOpArray(sqlite3 *, Op *, int);
58899
58900/*
58901** Delete a P4 value if necessary.
58902*/
58903static void freeP4(sqlite3 *db, int p4type, void *p4){
58904  if( p4 ){
58905    assert( db );
58906    switch( p4type ){
58907      case P4_REAL:
58908      case P4_INT64:
58909      case P4_DYNAMIC:
58910      case P4_KEYINFO:
58911      case P4_INTARRAY:
58912      case P4_KEYINFO_HANDOFF: {
58913        sqlite3DbFree(db, p4);
58914        break;
58915      }
58916      case P4_MPRINTF: {
58917        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
58918        break;
58919      }
58920      case P4_VDBEFUNC: {
58921        VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
58922        freeEphemeralFunction(db, pVdbeFunc->pFunc);
58923        if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
58924        sqlite3DbFree(db, pVdbeFunc);
58925        break;
58926      }
58927      case P4_FUNCDEF: {
58928        freeEphemeralFunction(db, (FuncDef*)p4);
58929        break;
58930      }
58931      case P4_MEM: {
58932        if( db->pnBytesFreed==0 ){
58933          sqlite3ValueFree((sqlite3_value*)p4);
58934        }else{
58935          Mem *p = (Mem*)p4;
58936          sqlite3DbFree(db, p->zMalloc);
58937          sqlite3DbFree(db, p);
58938        }
58939        break;
58940      }
58941      case P4_VTAB : {
58942        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
58943        break;
58944      }
58945    }
58946  }
58947}
58948
58949/*
58950** Free the space allocated for aOp and any p4 values allocated for the
58951** opcodes contained within. If aOp is not NULL it is assumed to contain
58952** nOp entries.
58953*/
58954static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
58955  if( aOp ){
58956    Op *pOp;
58957    for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
58958      freeP4(db, pOp->p4type, pOp->p4.p);
58959#ifdef SQLITE_DEBUG
58960      sqlite3DbFree(db, pOp->zComment);
58961#endif
58962    }
58963  }
58964  sqlite3DbFree(db, aOp);
58965}
58966
58967/*
58968** Link the SubProgram object passed as the second argument into the linked
58969** list at Vdbe.pSubProgram. This list is used to delete all sub-program
58970** objects when the VM is no longer required.
58971*/
58972SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
58973  p->pNext = pVdbe->pProgram;
58974  pVdbe->pProgram = p;
58975}
58976
58977/*
58978** Change the opcode at addr into OP_Noop
58979*/
58980SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
58981  if( p->aOp ){
58982    VdbeOp *pOp = &p->aOp[addr];
58983    sqlite3 *db = p->db;
58984    freeP4(db, pOp->p4type, pOp->p4.p);
58985    memset(pOp, 0, sizeof(pOp[0]));
58986    pOp->opcode = OP_Noop;
58987  }
58988}
58989
58990/*
58991** Change the value of the P4 operand for a specific instruction.
58992** This routine is useful when a large program is loaded from a
58993** static array using sqlite3VdbeAddOpList but we want to make a
58994** few minor changes to the program.
58995**
58996** If n>=0 then the P4 operand is dynamic, meaning that a copy of
58997** the string is made into memory obtained from sqlite3_malloc().
58998** A value of n==0 means copy bytes of zP4 up to and including the
58999** first null byte.  If n>0 then copy n+1 bytes of zP4.
59000**
59001** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
59002** A copy is made of the KeyInfo structure into memory obtained from
59003** sqlite3_malloc, to be freed when the Vdbe is finalized.
59004** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
59005** stored in memory that the caller has obtained from sqlite3_malloc. The
59006** caller should not free the allocation, it will be freed when the Vdbe is
59007** finalized.
59008**
59009** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
59010** to a string or structure that is guaranteed to exist for the lifetime of
59011** the Vdbe. In these cases we can just copy the pointer.
59012**
59013** If addr<0 then change P4 on the most recently inserted instruction.
59014*/
59015SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
59016  Op *pOp;
59017  sqlite3 *db;
59018  assert( p!=0 );
59019  db = p->db;
59020  assert( p->magic==VDBE_MAGIC_INIT );
59021  if( p->aOp==0 || db->mallocFailed ){
59022    if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
59023      freeP4(db, n, (void*)*(char**)&zP4);
59024    }
59025    return;
59026  }
59027  assert( p->nOp>0 );
59028  assert( addr<p->nOp );
59029  if( addr<0 ){
59030    addr = p->nOp - 1;
59031  }
59032  pOp = &p->aOp[addr];
59033  freeP4(db, pOp->p4type, pOp->p4.p);
59034  pOp->p4.p = 0;
59035  if( n==P4_INT32 ){
59036    /* Note: this cast is safe, because the origin data point was an int
59037    ** that was cast to a (const char *). */
59038    pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
59039    pOp->p4type = P4_INT32;
59040  }else if( zP4==0 ){
59041    pOp->p4.p = 0;
59042    pOp->p4type = P4_NOTUSED;
59043  }else if( n==P4_KEYINFO ){
59044    KeyInfo *pKeyInfo;
59045    int nField, nByte;
59046
59047    nField = ((KeyInfo*)zP4)->nField;
59048    nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
59049    pKeyInfo = sqlite3DbMallocRaw(0, nByte);
59050    pOp->p4.pKeyInfo = pKeyInfo;
59051    if( pKeyInfo ){
59052      u8 *aSortOrder;
59053      memcpy((char*)pKeyInfo, zP4, nByte - nField);
59054      aSortOrder = pKeyInfo->aSortOrder;
59055      if( aSortOrder ){
59056        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
59057        memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
59058      }
59059      pOp->p4type = P4_KEYINFO;
59060    }else{
59061      p->db->mallocFailed = 1;
59062      pOp->p4type = P4_NOTUSED;
59063    }
59064  }else if( n==P4_KEYINFO_HANDOFF ){
59065    pOp->p4.p = (void*)zP4;
59066    pOp->p4type = P4_KEYINFO;
59067  }else if( n==P4_VTAB ){
59068    pOp->p4.p = (void*)zP4;
59069    pOp->p4type = P4_VTAB;
59070    sqlite3VtabLock((VTable *)zP4);
59071    assert( ((VTable *)zP4)->db==p->db );
59072  }else if( n<0 ){
59073    pOp->p4.p = (void*)zP4;
59074    pOp->p4type = (signed char)n;
59075  }else{
59076    if( n==0 ) n = sqlite3Strlen30(zP4);
59077    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
59078    pOp->p4type = P4_DYNAMIC;
59079  }
59080}
59081
59082#ifndef NDEBUG
59083/*
59084** Change the comment on the the most recently coded instruction.  Or
59085** insert a No-op and add the comment to that new instruction.  This
59086** makes the code easier to read during debugging.  None of this happens
59087** in a production build.
59088*/
59089SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
59090  va_list ap;
59091  if( !p ) return;
59092  assert( p->nOp>0 || p->aOp==0 );
59093  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
59094  if( p->nOp ){
59095    char **pz = &p->aOp[p->nOp-1].zComment;
59096    va_start(ap, zFormat);
59097    sqlite3DbFree(p->db, *pz);
59098    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
59099    va_end(ap);
59100  }
59101}
59102SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
59103  va_list ap;
59104  if( !p ) return;
59105  sqlite3VdbeAddOp0(p, OP_Noop);
59106  assert( p->nOp>0 || p->aOp==0 );
59107  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
59108  if( p->nOp ){
59109    char **pz = &p->aOp[p->nOp-1].zComment;
59110    va_start(ap, zFormat);
59111    sqlite3DbFree(p->db, *pz);
59112    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
59113    va_end(ap);
59114  }
59115}
59116#endif  /* NDEBUG */
59117
59118/*
59119** Return the opcode for a given address.  If the address is -1, then
59120** return the most recently inserted opcode.
59121**
59122** If a memory allocation error has occurred prior to the calling of this
59123** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
59124** is readable but not writable, though it is cast to a writable value.
59125** The return of a dummy opcode allows the call to continue functioning
59126** after a OOM fault without having to check to see if the return from
59127** this routine is a valid pointer.  But because the dummy.opcode is 0,
59128** dummy will never be written to.  This is verified by code inspection and
59129** by running with Valgrind.
59130**
59131** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
59132** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
59133** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
59134** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
59135** having to double-check to make sure that the result is non-negative. But
59136** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
59137** check the value of p->nOp-1 before continuing.
59138*/
59139SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
59140  /* C89 specifies that the constant "dummy" will be initialized to all
59141  ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
59142  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
59143  assert( p->magic==VDBE_MAGIC_INIT );
59144  if( addr<0 ){
59145#ifdef SQLITE_OMIT_TRACE
59146    if( p->nOp==0 ) return (VdbeOp*)&dummy;
59147#endif
59148    addr = p->nOp - 1;
59149  }
59150  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
59151  if( p->db->mallocFailed ){
59152    return (VdbeOp*)&dummy;
59153  }else{
59154    return &p->aOp[addr];
59155  }
59156}
59157
59158#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
59159     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
59160/*
59161** Compute a string that describes the P4 parameter for an opcode.
59162** Use zTemp for any required temporary buffer space.
59163*/
59164static char *displayP4(Op *pOp, char *zTemp, int nTemp){
59165  char *zP4 = zTemp;
59166  assert( nTemp>=20 );
59167  switch( pOp->p4type ){
59168    case P4_KEYINFO_STATIC:
59169    case P4_KEYINFO: {
59170      int i, j;
59171      KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
59172      sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
59173      i = sqlite3Strlen30(zTemp);
59174      for(j=0; j<pKeyInfo->nField; j++){
59175        CollSeq *pColl = pKeyInfo->aColl[j];
59176        if( pColl ){
59177          int n = sqlite3Strlen30(pColl->zName);
59178          if( i+n>nTemp-6 ){
59179            memcpy(&zTemp[i],",...",4);
59180            break;
59181          }
59182          zTemp[i++] = ',';
59183          if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
59184            zTemp[i++] = '-';
59185          }
59186          memcpy(&zTemp[i], pColl->zName,n+1);
59187          i += n;
59188        }else if( i+4<nTemp-6 ){
59189          memcpy(&zTemp[i],",nil",4);
59190          i += 4;
59191        }
59192      }
59193      zTemp[i++] = ')';
59194      zTemp[i] = 0;
59195      assert( i<nTemp );
59196      break;
59197    }
59198    case P4_COLLSEQ: {
59199      CollSeq *pColl = pOp->p4.pColl;
59200      sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
59201      break;
59202    }
59203    case P4_FUNCDEF: {
59204      FuncDef *pDef = pOp->p4.pFunc;
59205      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
59206      break;
59207    }
59208    case P4_INT64: {
59209      sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
59210      break;
59211    }
59212    case P4_INT32: {
59213      sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
59214      break;
59215    }
59216    case P4_REAL: {
59217      sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
59218      break;
59219    }
59220    case P4_MEM: {
59221      Mem *pMem = pOp->p4.pMem;
59222      assert( (pMem->flags & MEM_Null)==0 );
59223      if( pMem->flags & MEM_Str ){
59224        zP4 = pMem->z;
59225      }else if( pMem->flags & MEM_Int ){
59226        sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
59227      }else if( pMem->flags & MEM_Real ){
59228        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
59229      }else{
59230        assert( pMem->flags & MEM_Blob );
59231        zP4 = "(blob)";
59232      }
59233      break;
59234    }
59235#ifndef SQLITE_OMIT_VIRTUALTABLE
59236    case P4_VTAB: {
59237      sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
59238      sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
59239      break;
59240    }
59241#endif
59242    case P4_INTARRAY: {
59243      sqlite3_snprintf(nTemp, zTemp, "intarray");
59244      break;
59245    }
59246    case P4_SUBPROGRAM: {
59247      sqlite3_snprintf(nTemp, zTemp, "program");
59248      break;
59249    }
59250    case P4_ADVANCE: {
59251      zTemp[0] = 0;
59252      break;
59253    }
59254    default: {
59255      zP4 = pOp->p4.z;
59256      if( zP4==0 ){
59257        zP4 = zTemp;
59258        zTemp[0] = 0;
59259      }
59260    }
59261  }
59262  assert( zP4!=0 );
59263  return zP4;
59264}
59265#endif
59266
59267/*
59268** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
59269**
59270** The prepared statements need to know in advance the complete set of
59271** attached databases that they will be using.  A mask of these databases
59272** is maintained in p->btreeMask and is used for locking and other purposes.
59273*/
59274SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
59275  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
59276  assert( i<(int)sizeof(p->btreeMask)*8 );
59277  p->btreeMask |= ((yDbMask)1)<<i;
59278  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
59279    p->lockMask |= ((yDbMask)1)<<i;
59280  }
59281}
59282
59283#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
59284/*
59285** If SQLite is compiled to support shared-cache mode and to be threadsafe,
59286** this routine obtains the mutex associated with each BtShared structure
59287** that may be accessed by the VM passed as an argument. In doing so it also
59288** sets the BtShared.db member of each of the BtShared structures, ensuring
59289** that the correct busy-handler callback is invoked if required.
59290**
59291** If SQLite is not threadsafe but does support shared-cache mode, then
59292** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
59293** of all of BtShared structures accessible via the database handle
59294** associated with the VM.
59295**
59296** If SQLite is not threadsafe and does not support shared-cache mode, this
59297** function is a no-op.
59298**
59299** The p->btreeMask field is a bitmask of all btrees that the prepared
59300** statement p will ever use.  Let N be the number of bits in p->btreeMask
59301** corresponding to btrees that use shared cache.  Then the runtime of
59302** this routine is N*N.  But as N is rarely more than 1, this should not
59303** be a problem.
59304*/
59305SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
59306  int i;
59307  yDbMask mask;
59308  sqlite3 *db;
59309  Db *aDb;
59310  int nDb;
59311  if( p->lockMask==0 ) return;  /* The common case */
59312  db = p->db;
59313  aDb = db->aDb;
59314  nDb = db->nDb;
59315  for(i=0, mask=1; i<nDb; i++, mask += mask){
59316    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
59317      sqlite3BtreeEnter(aDb[i].pBt);
59318    }
59319  }
59320}
59321#endif
59322
59323#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
59324/*
59325** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
59326*/
59327SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
59328  int i;
59329  yDbMask mask;
59330  sqlite3 *db;
59331  Db *aDb;
59332  int nDb;
59333  if( p->lockMask==0 ) return;  /* The common case */
59334  db = p->db;
59335  aDb = db->aDb;
59336  nDb = db->nDb;
59337  for(i=0, mask=1; i<nDb; i++, mask += mask){
59338    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
59339      sqlite3BtreeLeave(aDb[i].pBt);
59340    }
59341  }
59342}
59343#endif
59344
59345#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
59346/*
59347** Print a single opcode.  This routine is used for debugging only.
59348*/
59349SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
59350  char *zP4;
59351  char zPtr[50];
59352  static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
59353  if( pOut==0 ) pOut = stdout;
59354  zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
59355  fprintf(pOut, zFormat1, pc,
59356      sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
59357#ifdef SQLITE_DEBUG
59358      pOp->zComment ? pOp->zComment : ""
59359#else
59360      ""
59361#endif
59362  );
59363  fflush(pOut);
59364}
59365#endif
59366
59367/*
59368** Release an array of N Mem elements
59369*/
59370static void releaseMemArray(Mem *p, int N){
59371  if( p && N ){
59372    Mem *pEnd;
59373    sqlite3 *db = p->db;
59374    u8 malloc_failed = db->mallocFailed;
59375    if( db->pnBytesFreed ){
59376      for(pEnd=&p[N]; p<pEnd; p++){
59377        sqlite3DbFree(db, p->zMalloc);
59378      }
59379      return;
59380    }
59381    for(pEnd=&p[N]; p<pEnd; p++){
59382      assert( (&p[1])==pEnd || p[0].db==p[1].db );
59383
59384      /* This block is really an inlined version of sqlite3VdbeMemRelease()
59385      ** that takes advantage of the fact that the memory cell value is
59386      ** being set to NULL after releasing any dynamic resources.
59387      **
59388      ** The justification for duplicating code is that according to
59389      ** callgrind, this causes a certain test case to hit the CPU 4.7
59390      ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
59391      ** sqlite3MemRelease() were called from here. With -O2, this jumps
59392      ** to 6.6 percent. The test case is inserting 1000 rows into a table
59393      ** with no indexes using a single prepared INSERT statement, bind()
59394      ** and reset(). Inserts are grouped into a transaction.
59395      */
59396      if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
59397        sqlite3VdbeMemRelease(p);
59398      }else if( p->zMalloc ){
59399        sqlite3DbFree(db, p->zMalloc);
59400        p->zMalloc = 0;
59401      }
59402
59403      p->flags = MEM_Null;
59404    }
59405    db->mallocFailed = malloc_failed;
59406  }
59407}
59408
59409/*
59410** Delete a VdbeFrame object and its contents. VdbeFrame objects are
59411** allocated by the OP_Program opcode in sqlite3VdbeExec().
59412*/
59413SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
59414  int i;
59415  Mem *aMem = VdbeFrameMem(p);
59416  VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
59417  for(i=0; i<p->nChildCsr; i++){
59418    sqlite3VdbeFreeCursor(p->v, apCsr[i]);
59419  }
59420  releaseMemArray(aMem, p->nChildMem);
59421  sqlite3DbFree(p->v->db, p);
59422}
59423
59424#ifndef SQLITE_OMIT_EXPLAIN
59425/*
59426** Give a listing of the program in the virtual machine.
59427**
59428** The interface is the same as sqlite3VdbeExec().  But instead of
59429** running the code, it invokes the callback once for each instruction.
59430** This feature is used to implement "EXPLAIN".
59431**
59432** When p->explain==1, each instruction is listed.  When
59433** p->explain==2, only OP_Explain instructions are listed and these
59434** are shown in a different format.  p->explain==2 is used to implement
59435** EXPLAIN QUERY PLAN.
59436**
59437** When p->explain==1, first the main program is listed, then each of
59438** the trigger subprograms are listed one by one.
59439*/
59440SQLITE_PRIVATE int sqlite3VdbeList(
59441  Vdbe *p                   /* The VDBE */
59442){
59443  int nRow;                            /* Stop when row count reaches this */
59444  int nSub = 0;                        /* Number of sub-vdbes seen so far */
59445  SubProgram **apSub = 0;              /* Array of sub-vdbes */
59446  Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
59447  sqlite3 *db = p->db;                 /* The database connection */
59448  int i;                               /* Loop counter */
59449  int rc = SQLITE_OK;                  /* Return code */
59450  Mem *pMem = p->pResultSet = &p->aMem[1];  /* First Mem of result set */
59451
59452  assert( p->explain );
59453  assert( p->magic==VDBE_MAGIC_RUN );
59454  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
59455
59456  /* Even though this opcode does not use dynamic strings for
59457  ** the result, result columns may become dynamic if the user calls
59458  ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
59459  */
59460  releaseMemArray(pMem, 8);
59461
59462  if( p->rc==SQLITE_NOMEM ){
59463    /* This happens if a malloc() inside a call to sqlite3_column_text() or
59464    ** sqlite3_column_text16() failed.  */
59465    db->mallocFailed = 1;
59466    return SQLITE_ERROR;
59467  }
59468
59469  /* When the number of output rows reaches nRow, that means the
59470  ** listing has finished and sqlite3_step() should return SQLITE_DONE.
59471  ** nRow is the sum of the number of rows in the main program, plus
59472  ** the sum of the number of rows in all trigger subprograms encountered
59473  ** so far.  The nRow value will increase as new trigger subprograms are
59474  ** encountered, but p->pc will eventually catch up to nRow.
59475  */
59476  nRow = p->nOp;
59477  if( p->explain==1 ){
59478    /* The first 8 memory cells are used for the result set.  So we will
59479    ** commandeer the 9th cell to use as storage for an array of pointers
59480    ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
59481    ** cells.  */
59482    assert( p->nMem>9 );
59483    pSub = &p->aMem[9];
59484    if( pSub->flags&MEM_Blob ){
59485      /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
59486      ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
59487      nSub = pSub->n/sizeof(Vdbe*);
59488      apSub = (SubProgram **)pSub->z;
59489    }
59490    for(i=0; i<nSub; i++){
59491      nRow += apSub[i]->nOp;
59492    }
59493  }
59494
59495  do{
59496    i = p->pc++;
59497  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
59498  if( i>=nRow ){
59499    p->rc = SQLITE_OK;
59500    rc = SQLITE_DONE;
59501  }else if( db->u1.isInterrupted ){
59502    p->rc = SQLITE_INTERRUPT;
59503    rc = SQLITE_ERROR;
59504    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
59505  }else{
59506    char *z;
59507    Op *pOp;
59508    if( i<p->nOp ){
59509      /* The output line number is small enough that we are still in the
59510      ** main program. */
59511      pOp = &p->aOp[i];
59512    }else{
59513      /* We are currently listing subprograms.  Figure out which one and
59514      ** pick up the appropriate opcode. */
59515      int j;
59516      i -= p->nOp;
59517      for(j=0; i>=apSub[j]->nOp; j++){
59518        i -= apSub[j]->nOp;
59519      }
59520      pOp = &apSub[j]->aOp[i];
59521    }
59522    if( p->explain==1 ){
59523      pMem->flags = MEM_Int;
59524      pMem->type = SQLITE_INTEGER;
59525      pMem->u.i = i;                                /* Program counter */
59526      pMem++;
59527
59528      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
59529      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
59530      assert( pMem->z!=0 );
59531      pMem->n = sqlite3Strlen30(pMem->z);
59532      pMem->type = SQLITE_TEXT;
59533      pMem->enc = SQLITE_UTF8;
59534      pMem++;
59535
59536      /* When an OP_Program opcode is encounter (the only opcode that has
59537      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
59538      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
59539      ** has not already been seen.
59540      */
59541      if( pOp->p4type==P4_SUBPROGRAM ){
59542        int nByte = (nSub+1)*sizeof(SubProgram*);
59543        int j;
59544        for(j=0; j<nSub; j++){
59545          if( apSub[j]==pOp->p4.pProgram ) break;
59546        }
59547        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
59548          apSub = (SubProgram **)pSub->z;
59549          apSub[nSub++] = pOp->p4.pProgram;
59550          pSub->flags |= MEM_Blob;
59551          pSub->n = nSub*sizeof(SubProgram*);
59552        }
59553      }
59554    }
59555
59556    pMem->flags = MEM_Int;
59557    pMem->u.i = pOp->p1;                          /* P1 */
59558    pMem->type = SQLITE_INTEGER;
59559    pMem++;
59560
59561    pMem->flags = MEM_Int;
59562    pMem->u.i = pOp->p2;                          /* P2 */
59563    pMem->type = SQLITE_INTEGER;
59564    pMem++;
59565
59566    pMem->flags = MEM_Int;
59567    pMem->u.i = pOp->p3;                          /* P3 */
59568    pMem->type = SQLITE_INTEGER;
59569    pMem++;
59570
59571    if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
59572      assert( p->db->mallocFailed );
59573      return SQLITE_ERROR;
59574    }
59575    pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
59576    z = displayP4(pOp, pMem->z, 32);
59577    if( z!=pMem->z ){
59578      sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
59579    }else{
59580      assert( pMem->z!=0 );
59581      pMem->n = sqlite3Strlen30(pMem->z);
59582      pMem->enc = SQLITE_UTF8;
59583    }
59584    pMem->type = SQLITE_TEXT;
59585    pMem++;
59586
59587    if( p->explain==1 ){
59588      if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
59589        assert( p->db->mallocFailed );
59590        return SQLITE_ERROR;
59591      }
59592      pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
59593      pMem->n = 2;
59594      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
59595      pMem->type = SQLITE_TEXT;
59596      pMem->enc = SQLITE_UTF8;
59597      pMem++;
59598
59599#ifdef SQLITE_DEBUG
59600      if( pOp->zComment ){
59601        pMem->flags = MEM_Str|MEM_Term;
59602        pMem->z = pOp->zComment;
59603        pMem->n = sqlite3Strlen30(pMem->z);
59604        pMem->enc = SQLITE_UTF8;
59605        pMem->type = SQLITE_TEXT;
59606      }else
59607#endif
59608      {
59609        pMem->flags = MEM_Null;                       /* Comment */
59610        pMem->type = SQLITE_NULL;
59611      }
59612    }
59613
59614    p->nResColumn = 8 - 4*(p->explain-1);
59615    p->rc = SQLITE_OK;
59616    rc = SQLITE_ROW;
59617  }
59618  return rc;
59619}
59620#endif /* SQLITE_OMIT_EXPLAIN */
59621
59622#ifdef SQLITE_DEBUG
59623/*
59624** Print the SQL that was used to generate a VDBE program.
59625*/
59626SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
59627  int nOp = p->nOp;
59628  VdbeOp *pOp;
59629  if( nOp<1 ) return;
59630  pOp = &p->aOp[0];
59631  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
59632    const char *z = pOp->p4.z;
59633    while( sqlite3Isspace(*z) ) z++;
59634    printf("SQL: [%s]\n", z);
59635  }
59636}
59637#endif
59638
59639#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
59640/*
59641** Print an IOTRACE message showing SQL content.
59642*/
59643SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
59644  int nOp = p->nOp;
59645  VdbeOp *pOp;
59646  if( sqlite3IoTrace==0 ) return;
59647  if( nOp<1 ) return;
59648  pOp = &p->aOp[0];
59649  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
59650    int i, j;
59651    char z[1000];
59652    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
59653    for(i=0; sqlite3Isspace(z[i]); i++){}
59654    for(j=0; z[i]; i++){
59655      if( sqlite3Isspace(z[i]) ){
59656        if( z[i-1]!=' ' ){
59657          z[j++] = ' ';
59658        }
59659      }else{
59660        z[j++] = z[i];
59661      }
59662    }
59663    z[j] = 0;
59664    sqlite3IoTrace("SQL %s\n", z);
59665  }
59666}
59667#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
59668
59669/*
59670** Allocate space from a fixed size buffer and return a pointer to
59671** that space.  If insufficient space is available, return NULL.
59672**
59673** The pBuf parameter is the initial value of a pointer which will
59674** receive the new memory.  pBuf is normally NULL.  If pBuf is not
59675** NULL, it means that memory space has already been allocated and that
59676** this routine should not allocate any new memory.  When pBuf is not
59677** NULL simply return pBuf.  Only allocate new memory space when pBuf
59678** is NULL.
59679**
59680** nByte is the number of bytes of space needed.
59681**
59682** *ppFrom points to available space and pEnd points to the end of the
59683** available space.  When space is allocated, *ppFrom is advanced past
59684** the end of the allocated space.
59685**
59686** *pnByte is a counter of the number of bytes of space that have failed
59687** to allocate.  If there is insufficient space in *ppFrom to satisfy the
59688** request, then increment *pnByte by the amount of the request.
59689*/
59690static void *allocSpace(
59691  void *pBuf,          /* Where return pointer will be stored */
59692  int nByte,           /* Number of bytes to allocate */
59693  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
59694  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
59695  int *pnByte          /* If allocation cannot be made, increment *pnByte */
59696){
59697  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
59698  if( pBuf ) return pBuf;
59699  nByte = ROUND8(nByte);
59700  if( &(*ppFrom)[nByte] <= pEnd ){
59701    pBuf = (void*)*ppFrom;
59702    *ppFrom += nByte;
59703  }else{
59704    *pnByte += nByte;
59705  }
59706  return pBuf;
59707}
59708
59709/*
59710** Rewind the VDBE back to the beginning in preparation for
59711** running it.
59712*/
59713SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
59714#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
59715  int i;
59716#endif
59717  assert( p!=0 );
59718  assert( p->magic==VDBE_MAGIC_INIT );
59719
59720  /* There should be at least one opcode.
59721  */
59722  assert( p->nOp>0 );
59723
59724  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
59725  p->magic = VDBE_MAGIC_RUN;
59726
59727#ifdef SQLITE_DEBUG
59728  for(i=1; i<p->nMem; i++){
59729    assert( p->aMem[i].db==p->db );
59730  }
59731#endif
59732  p->pc = -1;
59733  p->rc = SQLITE_OK;
59734  p->errorAction = OE_Abort;
59735  p->magic = VDBE_MAGIC_RUN;
59736  p->nChange = 0;
59737  p->cacheCtr = 1;
59738  p->minWriteFileFormat = 255;
59739  p->iStatement = 0;
59740  p->nFkConstraint = 0;
59741#ifdef VDBE_PROFILE
59742  for(i=0; i<p->nOp; i++){
59743    p->aOp[i].cnt = 0;
59744    p->aOp[i].cycles = 0;
59745  }
59746#endif
59747}
59748
59749/*
59750** Prepare a virtual machine for execution for the first time after
59751** creating the virtual machine.  This involves things such
59752** as allocating stack space and initializing the program counter.
59753** After the VDBE has be prepped, it can be executed by one or more
59754** calls to sqlite3VdbeExec().
59755**
59756** This function may be called exact once on a each virtual machine.
59757** After this routine is called the VM has been "packaged" and is ready
59758** to run.  After this routine is called, futher calls to
59759** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
59760** the Vdbe from the Parse object that helped generate it so that the
59761** the Vdbe becomes an independent entity and the Parse object can be
59762** destroyed.
59763**
59764** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
59765** to its initial state after it has been run.
59766*/
59767SQLITE_PRIVATE void sqlite3VdbeMakeReady(
59768  Vdbe *p,                       /* The VDBE */
59769  Parse *pParse                  /* Parsing context */
59770){
59771  sqlite3 *db;                   /* The database connection */
59772  int nVar;                      /* Number of parameters */
59773  int nMem;                      /* Number of VM memory registers */
59774  int nCursor;                   /* Number of cursors required */
59775  int nArg;                      /* Number of arguments in subprograms */
59776  int n;                         /* Loop counter */
59777  u8 *zCsr;                      /* Memory available for allocation */
59778  u8 *zEnd;                      /* First byte past allocated memory */
59779  int nByte;                     /* How much extra memory is needed */
59780
59781  assert( p!=0 );
59782  assert( p->nOp>0 );
59783  assert( pParse!=0 );
59784  assert( p->magic==VDBE_MAGIC_INIT );
59785  db = p->db;
59786  assert( db->mallocFailed==0 );
59787  nVar = pParse->nVar;
59788  nMem = pParse->nMem;
59789  nCursor = pParse->nTab;
59790  nArg = pParse->nMaxArg;
59791
59792  /* For each cursor required, also allocate a memory cell. Memory
59793  ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
59794  ** the vdbe program. Instead they are used to allocate space for
59795  ** VdbeCursor/BtCursor structures. The blob of memory associated with
59796  ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
59797  ** stores the blob of memory associated with cursor 1, etc.
59798  **
59799  ** See also: allocateCursor().
59800  */
59801  nMem += nCursor;
59802
59803  /* Allocate space for memory registers, SQL variables, VDBE cursors and
59804  ** an array to marshal SQL function arguments in.
59805  */
59806  zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
59807  zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
59808
59809  resolveP2Values(p, &nArg);
59810  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
59811  if( pParse->explain && nMem<10 ){
59812    nMem = 10;
59813  }
59814  memset(zCsr, 0, zEnd-zCsr);
59815  zCsr += (zCsr - (u8*)0)&7;
59816  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
59817  p->expired = 0;
59818
59819  /* Memory for registers, parameters, cursor, etc, is allocated in two
59820  ** passes.  On the first pass, we try to reuse unused space at the
59821  ** end of the opcode array.  If we are unable to satisfy all memory
59822  ** requirements by reusing the opcode array tail, then the second
59823  ** pass will fill in the rest using a fresh allocation.
59824  **
59825  ** This two-pass approach that reuses as much memory as possible from
59826  ** the leftover space at the end of the opcode array can significantly
59827  ** reduce the amount of memory held by a prepared statement.
59828  */
59829  do {
59830    nByte = 0;
59831    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
59832    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
59833    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
59834    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
59835    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
59836                          &zCsr, zEnd, &nByte);
59837    if( nByte ){
59838      p->pFree = sqlite3DbMallocZero(db, nByte);
59839    }
59840    zCsr = p->pFree;
59841    zEnd = &zCsr[nByte];
59842  }while( nByte && !db->mallocFailed );
59843
59844  p->nCursor = (u16)nCursor;
59845  if( p->aVar ){
59846    p->nVar = (ynVar)nVar;
59847    for(n=0; n<nVar; n++){
59848      p->aVar[n].flags = MEM_Null;
59849      p->aVar[n].db = db;
59850    }
59851  }
59852  if( p->azVar ){
59853    p->nzVar = pParse->nzVar;
59854    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
59855    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
59856  }
59857  if( p->aMem ){
59858    p->aMem--;                      /* aMem[] goes from 1..nMem */
59859    p->nMem = nMem;                 /*       not from 0..nMem-1 */
59860    for(n=1; n<=nMem; n++){
59861      p->aMem[n].flags = MEM_Null;
59862      p->aMem[n].db = db;
59863    }
59864  }
59865  p->explain = pParse->explain;
59866  sqlite3VdbeRewind(p);
59867}
59868
59869/*
59870** Close a VDBE cursor and release all the resources that cursor
59871** happens to hold.
59872*/
59873SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59874  if( pCx==0 ){
59875    return;
59876  }
59877  sqlite3VdbeSorterClose(p->db, pCx);
59878  if( pCx->pBt ){
59879    sqlite3BtreeClose(pCx->pBt);
59880    /* The pCx->pCursor will be close automatically, if it exists, by
59881    ** the call above. */
59882  }else if( pCx->pCursor ){
59883    sqlite3BtreeCloseCursor(pCx->pCursor);
59884  }
59885#ifndef SQLITE_OMIT_VIRTUALTABLE
59886  if( pCx->pVtabCursor ){
59887    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
59888    const sqlite3_module *pModule = pCx->pModule;
59889    p->inVtabMethod = 1;
59890    pModule->xClose(pVtabCursor);
59891    p->inVtabMethod = 0;
59892  }
59893#endif
59894}
59895
59896/*
59897** Copy the values stored in the VdbeFrame structure to its Vdbe. This
59898** is used, for example, when a trigger sub-program is halted to restore
59899** control to the main program.
59900*/
59901SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
59902  Vdbe *v = pFrame->v;
59903  v->aOp = pFrame->aOp;
59904  v->nOp = pFrame->nOp;
59905  v->aMem = pFrame->aMem;
59906  v->nMem = pFrame->nMem;
59907  v->apCsr = pFrame->apCsr;
59908  v->nCursor = pFrame->nCursor;
59909  v->db->lastRowid = pFrame->lastRowid;
59910  v->nChange = pFrame->nChange;
59911  return pFrame->pc;
59912}
59913
59914/*
59915** Close all cursors.
59916**
59917** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
59918** cell array. This is necessary as the memory cell array may contain
59919** pointers to VdbeFrame objects, which may in turn contain pointers to
59920** open cursors.
59921*/
59922static void closeAllCursors(Vdbe *p){
59923  if( p->pFrame ){
59924    VdbeFrame *pFrame;
59925    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
59926    sqlite3VdbeFrameRestore(pFrame);
59927  }
59928  p->pFrame = 0;
59929  p->nFrame = 0;
59930
59931  if( p->apCsr ){
59932    int i;
59933    for(i=0; i<p->nCursor; i++){
59934      VdbeCursor *pC = p->apCsr[i];
59935      if( pC ){
59936        sqlite3VdbeFreeCursor(p, pC);
59937        p->apCsr[i] = 0;
59938      }
59939    }
59940  }
59941  if( p->aMem ){
59942    releaseMemArray(&p->aMem[1], p->nMem);
59943  }
59944  while( p->pDelFrame ){
59945    VdbeFrame *pDel = p->pDelFrame;
59946    p->pDelFrame = pDel->pParent;
59947    sqlite3VdbeFrameDelete(pDel);
59948  }
59949}
59950
59951/*
59952** Clean up the VM after execution.
59953**
59954** This routine will automatically close any cursors, lists, and/or
59955** sorters that were left open.  It also deletes the values of
59956** variables in the aVar[] array.
59957*/
59958static void Cleanup(Vdbe *p){
59959  sqlite3 *db = p->db;
59960
59961#ifdef SQLITE_DEBUG
59962  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
59963  ** Vdbe.aMem[] arrays have already been cleaned up.  */
59964  int i;
59965  for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
59966  for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
59967#endif
59968
59969  sqlite3DbFree(db, p->zErrMsg);
59970  p->zErrMsg = 0;
59971  p->pResultSet = 0;
59972}
59973
59974/*
59975** Set the number of result columns that will be returned by this SQL
59976** statement. This is now set at compile time, rather than during
59977** execution of the vdbe program so that sqlite3_column_count() can
59978** be called on an SQL statement before sqlite3_step().
59979*/
59980SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
59981  Mem *pColName;
59982  int n;
59983  sqlite3 *db = p->db;
59984
59985  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
59986  sqlite3DbFree(db, p->aColName);
59987  n = nResColumn*COLNAME_N;
59988  p->nResColumn = (u16)nResColumn;
59989  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
59990  if( p->aColName==0 ) return;
59991  while( n-- > 0 ){
59992    pColName->flags = MEM_Null;
59993    pColName->db = p->db;
59994    pColName++;
59995  }
59996}
59997
59998/*
59999** Set the name of the idx'th column to be returned by the SQL statement.
60000** zName must be a pointer to a nul terminated string.
60001**
60002** This call must be made after a call to sqlite3VdbeSetNumCols().
60003**
60004** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
60005** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
60006** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
60007*/
60008SQLITE_PRIVATE int sqlite3VdbeSetColName(
60009  Vdbe *p,                         /* Vdbe being configured */
60010  int idx,                         /* Index of column zName applies to */
60011  int var,                         /* One of the COLNAME_* constants */
60012  const char *zName,               /* Pointer to buffer containing name */
60013  void (*xDel)(void*)              /* Memory management strategy for zName */
60014){
60015  int rc;
60016  Mem *pColName;
60017  assert( idx<p->nResColumn );
60018  assert( var<COLNAME_N );
60019  if( p->db->mallocFailed ){
60020    assert( !zName || xDel!=SQLITE_DYNAMIC );
60021    return SQLITE_NOMEM;
60022  }
60023  assert( p->aColName!=0 );
60024  pColName = &(p->aColName[idx+var*p->nResColumn]);
60025  rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
60026  assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
60027  return rc;
60028}
60029
60030/*
60031** A read or write transaction may or may not be active on database handle
60032** db. If a transaction is active, commit it. If there is a
60033** write-transaction spanning more than one database file, this routine
60034** takes care of the master journal trickery.
60035*/
60036static int vdbeCommit(sqlite3 *db, Vdbe *p){
60037  int i;
60038  int nTrans = 0;  /* Number of databases with an active write-transaction */
60039  int rc = SQLITE_OK;
60040  int needXcommit = 0;
60041
60042#ifdef SQLITE_OMIT_VIRTUALTABLE
60043  /* With this option, sqlite3VtabSync() is defined to be simply
60044  ** SQLITE_OK so p is not used.
60045  */
60046  UNUSED_PARAMETER(p);
60047#endif
60048
60049  /* Before doing anything else, call the xSync() callback for any
60050  ** virtual module tables written in this transaction. This has to
60051  ** be done before determining whether a master journal file is
60052  ** required, as an xSync() callback may add an attached database
60053  ** to the transaction.
60054  */
60055  rc = sqlite3VtabSync(db, &p->zErrMsg);
60056
60057  /* This loop determines (a) if the commit hook should be invoked and
60058  ** (b) how many database files have open write transactions, not
60059  ** including the temp database. (b) is important because if more than
60060  ** one database file has an open write transaction, a master journal
60061  ** file is required for an atomic commit.
60062  */
60063  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
60064    Btree *pBt = db->aDb[i].pBt;
60065    if( sqlite3BtreeIsInTrans(pBt) ){
60066      needXcommit = 1;
60067      if( i!=1 ) nTrans++;
60068      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
60069    }
60070  }
60071  if( rc!=SQLITE_OK ){
60072    return rc;
60073  }
60074
60075  /* If there are any write-transactions at all, invoke the commit hook */
60076  if( needXcommit && db->xCommitCallback ){
60077    rc = db->xCommitCallback(db->pCommitArg);
60078    if( rc ){
60079      return SQLITE_CONSTRAINT;
60080    }
60081  }
60082
60083  /* The simple case - no more than one database file (not counting the
60084  ** TEMP database) has a transaction active.   There is no need for the
60085  ** master-journal.
60086  **
60087  ** If the return value of sqlite3BtreeGetFilename() is a zero length
60088  ** string, it means the main database is :memory: or a temp file.  In
60089  ** that case we do not support atomic multi-file commits, so use the
60090  ** simple case then too.
60091  */
60092  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
60093   || nTrans<=1
60094  ){
60095    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
60096      Btree *pBt = db->aDb[i].pBt;
60097      if( pBt ){
60098        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
60099      }
60100    }
60101
60102    /* Do the commit only if all databases successfully complete phase 1.
60103    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
60104    ** IO error while deleting or truncating a journal file. It is unlikely,
60105    ** but could happen. In this case abandon processing and return the error.
60106    */
60107    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
60108      Btree *pBt = db->aDb[i].pBt;
60109      if( pBt ){
60110        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
60111      }
60112    }
60113    if( rc==SQLITE_OK ){
60114      sqlite3VtabCommit(db);
60115    }
60116  }
60117
60118  /* The complex case - There is a multi-file write-transaction active.
60119  ** This requires a master journal file to ensure the transaction is
60120  ** committed atomicly.
60121  */
60122#ifndef SQLITE_OMIT_DISKIO
60123  else{
60124    sqlite3_vfs *pVfs = db->pVfs;
60125    int needSync = 0;
60126    char *zMaster = 0;   /* File-name for the master journal */
60127    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
60128    sqlite3_file *pMaster = 0;
60129    i64 offset = 0;
60130    int res;
60131
60132    /* Select a master journal file name */
60133    do {
60134      u32 iRandom;
60135      sqlite3DbFree(db, zMaster);
60136      sqlite3_randomness(sizeof(iRandom), &iRandom);
60137      zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
60138      if( !zMaster ){
60139        return SQLITE_NOMEM;
60140      }
60141      sqlite3FileSuffix3(zMainFile, zMaster);
60142      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
60143    }while( rc==SQLITE_OK && res );
60144    if( rc==SQLITE_OK ){
60145      /* Open the master journal. */
60146      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
60147          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
60148          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
60149      );
60150    }
60151    if( rc!=SQLITE_OK ){
60152      sqlite3DbFree(db, zMaster);
60153      return rc;
60154    }
60155
60156    /* Write the name of each database file in the transaction into the new
60157    ** master journal file. If an error occurs at this point close
60158    ** and delete the master journal file. All the individual journal files
60159    ** still have 'null' as the master journal pointer, so they will roll
60160    ** back independently if a failure occurs.
60161    */
60162    for(i=0; i<db->nDb; i++){
60163      Btree *pBt = db->aDb[i].pBt;
60164      if( sqlite3BtreeIsInTrans(pBt) ){
60165        char const *zFile = sqlite3BtreeGetJournalname(pBt);
60166        if( zFile==0 ){
60167          continue;  /* Ignore TEMP and :memory: databases */
60168        }
60169        assert( zFile[0]!=0 );
60170        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
60171          needSync = 1;
60172        }
60173        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
60174        offset += sqlite3Strlen30(zFile)+1;
60175        if( rc!=SQLITE_OK ){
60176          sqlite3OsCloseFree(pMaster);
60177          sqlite3OsDelete(pVfs, zMaster, 0);
60178          sqlite3DbFree(db, zMaster);
60179          return rc;
60180        }
60181      }
60182    }
60183
60184    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
60185    ** flag is set this is not required.
60186    */
60187    if( needSync
60188     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
60189     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
60190    ){
60191      sqlite3OsCloseFree(pMaster);
60192      sqlite3OsDelete(pVfs, zMaster, 0);
60193      sqlite3DbFree(db, zMaster);
60194      return rc;
60195    }
60196
60197    /* Sync all the db files involved in the transaction. The same call
60198    ** sets the master journal pointer in each individual journal. If
60199    ** an error occurs here, do not delete the master journal file.
60200    **
60201    ** If the error occurs during the first call to
60202    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
60203    ** master journal file will be orphaned. But we cannot delete it,
60204    ** in case the master journal file name was written into the journal
60205    ** file before the failure occurred.
60206    */
60207    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
60208      Btree *pBt = db->aDb[i].pBt;
60209      if( pBt ){
60210        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
60211      }
60212    }
60213    sqlite3OsCloseFree(pMaster);
60214    assert( rc!=SQLITE_BUSY );
60215    if( rc!=SQLITE_OK ){
60216      sqlite3DbFree(db, zMaster);
60217      return rc;
60218    }
60219
60220    /* Delete the master journal file. This commits the transaction. After
60221    ** doing this the directory is synced again before any individual
60222    ** transaction files are deleted.
60223    */
60224    rc = sqlite3OsDelete(pVfs, zMaster, 1);
60225    sqlite3DbFree(db, zMaster);
60226    zMaster = 0;
60227    if( rc ){
60228      return rc;
60229    }
60230
60231    /* All files and directories have already been synced, so the following
60232    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
60233    ** deleting or truncating journals. If something goes wrong while
60234    ** this is happening we don't really care. The integrity of the
60235    ** transaction is already guaranteed, but some stray 'cold' journals
60236    ** may be lying around. Returning an error code won't help matters.
60237    */
60238    disable_simulated_io_errors();
60239    sqlite3BeginBenignMalloc();
60240    for(i=0; i<db->nDb; i++){
60241      Btree *pBt = db->aDb[i].pBt;
60242      if( pBt ){
60243        sqlite3BtreeCommitPhaseTwo(pBt, 1);
60244      }
60245    }
60246    sqlite3EndBenignMalloc();
60247    enable_simulated_io_errors();
60248
60249    sqlite3VtabCommit(db);
60250  }
60251#endif
60252
60253  return rc;
60254}
60255
60256/*
60257** This routine checks that the sqlite3.activeVdbeCnt count variable
60258** matches the number of vdbe's in the list sqlite3.pVdbe that are
60259** currently active. An assertion fails if the two counts do not match.
60260** This is an internal self-check only - it is not an essential processing
60261** step.
60262**
60263** This is a no-op if NDEBUG is defined.
60264*/
60265#ifndef NDEBUG
60266static void checkActiveVdbeCnt(sqlite3 *db){
60267  Vdbe *p;
60268  int cnt = 0;
60269  int nWrite = 0;
60270  p = db->pVdbe;
60271  while( p ){
60272    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
60273      cnt++;
60274      if( p->readOnly==0 ) nWrite++;
60275    }
60276    p = p->pNext;
60277  }
60278  assert( cnt==db->activeVdbeCnt );
60279  assert( nWrite==db->writeVdbeCnt );
60280}
60281#else
60282#define checkActiveVdbeCnt(x)
60283#endif
60284
60285/*
60286** For every Btree that in database connection db which
60287** has been modified, "trip" or invalidate each cursor in
60288** that Btree might have been modified so that the cursor
60289** can never be used again.  This happens when a rollback
60290*** occurs.  We have to trip all the other cursors, even
60291** cursor from other VMs in different database connections,
60292** so that none of them try to use the data at which they
60293** were pointing and which now may have been changed due
60294** to the rollback.
60295**
60296** Remember that a rollback can delete tables complete and
60297** reorder rootpages.  So it is not sufficient just to save
60298** the state of the cursor.  We have to invalidate the cursor
60299** so that it is never used again.
60300*/
60301static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
60302  int i;
60303  for(i=0; i<db->nDb; i++){
60304    Btree *p = db->aDb[i].pBt;
60305    if( p && sqlite3BtreeIsInTrans(p) ){
60306      sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
60307    }
60308  }
60309}
60310
60311/*
60312** If the Vdbe passed as the first argument opened a statement-transaction,
60313** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
60314** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
60315** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
60316** statement transaction is commtted.
60317**
60318** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
60319** Otherwise SQLITE_OK.
60320*/
60321SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
60322  sqlite3 *const db = p->db;
60323  int rc = SQLITE_OK;
60324
60325  /* If p->iStatement is greater than zero, then this Vdbe opened a
60326  ** statement transaction that should be closed here. The only exception
60327  ** is that an IO error may have occured, causing an emergency rollback.
60328  ** In this case (db->nStatement==0), and there is nothing to do.
60329  */
60330  if( db->nStatement && p->iStatement ){
60331    int i;
60332    const int iSavepoint = p->iStatement-1;
60333
60334    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
60335    assert( db->nStatement>0 );
60336    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
60337
60338    for(i=0; i<db->nDb; i++){
60339      int rc2 = SQLITE_OK;
60340      Btree *pBt = db->aDb[i].pBt;
60341      if( pBt ){
60342        if( eOp==SAVEPOINT_ROLLBACK ){
60343          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
60344        }
60345        if( rc2==SQLITE_OK ){
60346          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
60347        }
60348        if( rc==SQLITE_OK ){
60349          rc = rc2;
60350        }
60351      }
60352    }
60353    db->nStatement--;
60354    p->iStatement = 0;
60355
60356    if( rc==SQLITE_OK ){
60357      if( eOp==SAVEPOINT_ROLLBACK ){
60358        rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
60359      }
60360      if( rc==SQLITE_OK ){
60361        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
60362      }
60363    }
60364
60365    /* If the statement transaction is being rolled back, also restore the
60366    ** database handles deferred constraint counter to the value it had when
60367    ** the statement transaction was opened.  */
60368    if( eOp==SAVEPOINT_ROLLBACK ){
60369      db->nDeferredCons = p->nStmtDefCons;
60370    }
60371  }
60372  return rc;
60373}
60374
60375/*
60376** This function is called when a transaction opened by the database
60377** handle associated with the VM passed as an argument is about to be
60378** committed. If there are outstanding deferred foreign key constraint
60379** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
60380**
60381** If there are outstanding FK violations and this function returns
60382** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
60383** an error message to it. Then return SQLITE_ERROR.
60384*/
60385#ifndef SQLITE_OMIT_FOREIGN_KEY
60386SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
60387  sqlite3 *db = p->db;
60388  if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
60389    p->rc = SQLITE_CONSTRAINT;
60390    p->errorAction = OE_Abort;
60391    sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
60392    return SQLITE_ERROR;
60393  }
60394  return SQLITE_OK;
60395}
60396#endif
60397
60398/*
60399** This routine is called the when a VDBE tries to halt.  If the VDBE
60400** has made changes and is in autocommit mode, then commit those
60401** changes.  If a rollback is needed, then do the rollback.
60402**
60403** This routine is the only way to move the state of a VM from
60404** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
60405** call this on a VM that is in the SQLITE_MAGIC_HALT state.
60406**
60407** Return an error code.  If the commit could not complete because of
60408** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
60409** means the close did not happen and needs to be repeated.
60410*/
60411SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
60412  int rc;                         /* Used to store transient return codes */
60413  sqlite3 *db = p->db;
60414
60415  /* This function contains the logic that determines if a statement or
60416  ** transaction will be committed or rolled back as a result of the
60417  ** execution of this virtual machine.
60418  **
60419  ** If any of the following errors occur:
60420  **
60421  **     SQLITE_NOMEM
60422  **     SQLITE_IOERR
60423  **     SQLITE_FULL
60424  **     SQLITE_INTERRUPT
60425  **
60426  ** Then the internal cache might have been left in an inconsistent
60427  ** state.  We need to rollback the statement transaction, if there is
60428  ** one, or the complete transaction if there is no statement transaction.
60429  */
60430
60431  if( p->db->mallocFailed ){
60432    p->rc = SQLITE_NOMEM;
60433  }
60434  closeAllCursors(p);
60435  if( p->magic!=VDBE_MAGIC_RUN ){
60436    return SQLITE_OK;
60437  }
60438  checkActiveVdbeCnt(db);
60439
60440  /* No commit or rollback needed if the program never started */
60441  if( p->pc>=0 ){
60442    int mrc;   /* Primary error code from p->rc */
60443    int eStatementOp = 0;
60444    int isSpecialError;            /* Set to true if a 'special' error */
60445
60446    /* Lock all btrees used by the statement */
60447    sqlite3VdbeEnter(p);
60448
60449    /* Check for one of the special errors */
60450    mrc = p->rc & 0xff;
60451    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
60452    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
60453                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
60454    if( isSpecialError ){
60455      /* If the query was read-only and the error code is SQLITE_INTERRUPT,
60456      ** no rollback is necessary. Otherwise, at least a savepoint
60457      ** transaction must be rolled back to restore the database to a
60458      ** consistent state.
60459      **
60460      ** Even if the statement is read-only, it is important to perform
60461      ** a statement or transaction rollback operation. If the error
60462      ** occured while writing to the journal, sub-journal or database
60463      ** file as part of an effort to free up cache space (see function
60464      ** pagerStress() in pager.c), the rollback is required to restore
60465      ** the pager to a consistent state.
60466      */
60467      if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
60468        if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
60469          eStatementOp = SAVEPOINT_ROLLBACK;
60470        }else{
60471          /* We are forced to roll back the active transaction. Before doing
60472          ** so, abort any other statements this handle currently has active.
60473          */
60474          invalidateCursorsOnModifiedBtrees(db);
60475          sqlite3RollbackAll(db);
60476          sqlite3CloseSavepoints(db);
60477          db->autoCommit = 1;
60478        }
60479      }
60480    }
60481
60482    /* Check for immediate foreign key violations. */
60483    if( p->rc==SQLITE_OK ){
60484      sqlite3VdbeCheckFk(p, 0);
60485    }
60486
60487    /* If the auto-commit flag is set and this is the only active writer
60488    ** VM, then we do either a commit or rollback of the current transaction.
60489    **
60490    ** Note: This block also runs if one of the special errors handled
60491    ** above has occurred.
60492    */
60493    if( !sqlite3VtabInSync(db)
60494     && db->autoCommit
60495     && db->writeVdbeCnt==(p->readOnly==0)
60496    ){
60497      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
60498        rc = sqlite3VdbeCheckFk(p, 1);
60499        if( rc!=SQLITE_OK ){
60500          if( NEVER(p->readOnly) ){
60501            sqlite3VdbeLeave(p);
60502            return SQLITE_ERROR;
60503          }
60504          rc = SQLITE_CONSTRAINT;
60505        }else{
60506          /* The auto-commit flag is true, the vdbe program was successful
60507          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
60508          ** key constraints to hold up the transaction. This means a commit
60509          ** is required. */
60510          rc = vdbeCommit(db, p);
60511        }
60512        if( rc==SQLITE_BUSY && p->readOnly ){
60513          sqlite3VdbeLeave(p);
60514          return SQLITE_BUSY;
60515        }else if( rc!=SQLITE_OK ){
60516          p->rc = rc;
60517          sqlite3RollbackAll(db);
60518        }else{
60519          db->nDeferredCons = 0;
60520          sqlite3CommitInternalChanges(db);
60521        }
60522      }else{
60523        sqlite3RollbackAll(db);
60524      }
60525      db->nStatement = 0;
60526    }else if( eStatementOp==0 ){
60527      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
60528        eStatementOp = SAVEPOINT_RELEASE;
60529      }else if( p->errorAction==OE_Abort ){
60530        eStatementOp = SAVEPOINT_ROLLBACK;
60531      }else{
60532        invalidateCursorsOnModifiedBtrees(db);
60533        sqlite3RollbackAll(db);
60534        sqlite3CloseSavepoints(db);
60535        db->autoCommit = 1;
60536      }
60537    }
60538
60539    /* If eStatementOp is non-zero, then a statement transaction needs to
60540    ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
60541    ** do so. If this operation returns an error, and the current statement
60542    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
60543    ** current statement error code.
60544    */
60545    if( eStatementOp ){
60546      rc = sqlite3VdbeCloseStatement(p, eStatementOp);
60547      if( rc ){
60548        if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
60549          p->rc = rc;
60550          sqlite3DbFree(db, p->zErrMsg);
60551          p->zErrMsg = 0;
60552        }
60553        invalidateCursorsOnModifiedBtrees(db);
60554        sqlite3RollbackAll(db);
60555        sqlite3CloseSavepoints(db);
60556        db->autoCommit = 1;
60557      }
60558    }
60559
60560    /* If this was an INSERT, UPDATE or DELETE and no statement transaction
60561    ** has been rolled back, update the database connection change-counter.
60562    */
60563    if( p->changeCntOn ){
60564      if( eStatementOp!=SAVEPOINT_ROLLBACK ){
60565        sqlite3VdbeSetChanges(db, p->nChange);
60566      }else{
60567        sqlite3VdbeSetChanges(db, 0);
60568      }
60569      p->nChange = 0;
60570    }
60571
60572    /* Rollback or commit any schema changes that occurred. */
60573    if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
60574      sqlite3ResetInternalSchema(db, -1);
60575      db->flags = (db->flags | SQLITE_InternChanges);
60576    }
60577
60578    /* Release the locks */
60579    sqlite3VdbeLeave(p);
60580  }
60581
60582  /* We have successfully halted and closed the VM.  Record this fact. */
60583  if( p->pc>=0 ){
60584    db->activeVdbeCnt--;
60585    if( !p->readOnly ){
60586      db->writeVdbeCnt--;
60587    }
60588    assert( db->activeVdbeCnt>=db->writeVdbeCnt );
60589  }
60590  p->magic = VDBE_MAGIC_HALT;
60591  checkActiveVdbeCnt(db);
60592  if( p->db->mallocFailed ){
60593    p->rc = SQLITE_NOMEM;
60594  }
60595
60596  /* If the auto-commit flag is set to true, then any locks that were held
60597  ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
60598  ** to invoke any required unlock-notify callbacks.
60599  */
60600  if( db->autoCommit ){
60601    sqlite3ConnectionUnlocked(db);
60602  }
60603
60604  assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
60605  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
60606}
60607
60608
60609/*
60610** Each VDBE holds the result of the most recent sqlite3_step() call
60611** in p->rc.  This routine sets that result back to SQLITE_OK.
60612*/
60613SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
60614  p->rc = SQLITE_OK;
60615}
60616
60617/*
60618** Clean up a VDBE after execution but do not delete the VDBE just yet.
60619** Write any error messages into *pzErrMsg.  Return the result code.
60620**
60621** After this routine is run, the VDBE should be ready to be executed
60622** again.
60623**
60624** To look at it another way, this routine resets the state of the
60625** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
60626** VDBE_MAGIC_INIT.
60627*/
60628SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
60629  sqlite3 *db;
60630  db = p->db;
60631
60632  /* If the VM did not run to completion or if it encountered an
60633  ** error, then it might not have been halted properly.  So halt
60634  ** it now.
60635  */
60636  sqlite3VdbeHalt(p);
60637
60638  /* If the VDBE has be run even partially, then transfer the error code
60639  ** and error message from the VDBE into the main database structure.  But
60640  ** if the VDBE has just been set to run but has not actually executed any
60641  ** instructions yet, leave the main database error information unchanged.
60642  */
60643  if( p->pc>=0 ){
60644    if( p->zErrMsg ){
60645      sqlite3BeginBenignMalloc();
60646      sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
60647      sqlite3EndBenignMalloc();
60648      db->errCode = p->rc;
60649      sqlite3DbFree(db, p->zErrMsg);
60650      p->zErrMsg = 0;
60651    }else if( p->rc ){
60652      sqlite3Error(db, p->rc, 0);
60653    }else{
60654      sqlite3Error(db, SQLITE_OK, 0);
60655    }
60656    if( p->runOnlyOnce ) p->expired = 1;
60657  }else if( p->rc && p->expired ){
60658    /* The expired flag was set on the VDBE before the first call
60659    ** to sqlite3_step(). For consistency (since sqlite3_step() was
60660    ** called), set the database error in this case as well.
60661    */
60662    sqlite3Error(db, p->rc, 0);
60663    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
60664    sqlite3DbFree(db, p->zErrMsg);
60665    p->zErrMsg = 0;
60666  }
60667
60668  /* Reclaim all memory used by the VDBE
60669  */
60670  Cleanup(p);
60671
60672  /* Save profiling information from this VDBE run.
60673  */
60674#ifdef VDBE_PROFILE
60675  {
60676    FILE *out = fopen("vdbe_profile.out", "a");
60677    if( out ){
60678      int i;
60679      fprintf(out, "---- ");
60680      for(i=0; i<p->nOp; i++){
60681        fprintf(out, "%02x", p->aOp[i].opcode);
60682      }
60683      fprintf(out, "\n");
60684      for(i=0; i<p->nOp; i++){
60685        fprintf(out, "%6d %10lld %8lld ",
60686           p->aOp[i].cnt,
60687           p->aOp[i].cycles,
60688           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
60689        );
60690        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
60691      }
60692      fclose(out);
60693    }
60694  }
60695#endif
60696  p->magic = VDBE_MAGIC_INIT;
60697  return p->rc & db->errMask;
60698}
60699
60700/*
60701** Clean up and delete a VDBE after execution.  Return an integer which is
60702** the result code.  Write any error message text into *pzErrMsg.
60703*/
60704SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
60705  int rc = SQLITE_OK;
60706  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
60707    rc = sqlite3VdbeReset(p);
60708    assert( (rc & p->db->errMask)==rc );
60709  }
60710  sqlite3VdbeDelete(p);
60711  return rc;
60712}
60713
60714/*
60715** Call the destructor for each auxdata entry in pVdbeFunc for which
60716** the corresponding bit in mask is clear.  Auxdata entries beyond 31
60717** are always destroyed.  To destroy all auxdata entries, call this
60718** routine with mask==0.
60719*/
60720SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
60721  int i;
60722  for(i=0; i<pVdbeFunc->nAux; i++){
60723    struct AuxData *pAux = &pVdbeFunc->apAux[i];
60724    if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
60725      if( pAux->xDelete ){
60726        pAux->xDelete(pAux->pAux);
60727      }
60728      pAux->pAux = 0;
60729    }
60730  }
60731}
60732
60733/*
60734** Free all memory associated with the Vdbe passed as the second argument.
60735** The difference between this function and sqlite3VdbeDelete() is that
60736** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
60737** the database connection.
60738*/
60739SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
60740  SubProgram *pSub, *pNext;
60741  int i;
60742  assert( p->db==0 || p->db==db );
60743  releaseMemArray(p->aVar, p->nVar);
60744  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
60745  for(pSub=p->pProgram; pSub; pSub=pNext){
60746    pNext = pSub->pNext;
60747    vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
60748    sqlite3DbFree(db, pSub);
60749  }
60750  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
60751  vdbeFreeOpArray(db, p->aOp, p->nOp);
60752  sqlite3DbFree(db, p->aLabel);
60753  sqlite3DbFree(db, p->aColName);
60754  sqlite3DbFree(db, p->zSql);
60755  sqlite3DbFree(db, p->pFree);
60756  sqlite3DbFree(db, p);
60757}
60758
60759/*
60760** Delete an entire VDBE.
60761*/
60762SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
60763  sqlite3 *db;
60764
60765  if( NEVER(p==0) ) return;
60766  db = p->db;
60767  if( p->pPrev ){
60768    p->pPrev->pNext = p->pNext;
60769  }else{
60770    assert( db->pVdbe==p );
60771    db->pVdbe = p->pNext;
60772  }
60773  if( p->pNext ){
60774    p->pNext->pPrev = p->pPrev;
60775  }
60776  p->magic = VDBE_MAGIC_DEAD;
60777  p->db = 0;
60778  sqlite3VdbeDeleteObject(db, p);
60779}
60780
60781/*
60782** Make sure the cursor p is ready to read or write the row to which it
60783** was last positioned.  Return an error code if an OOM fault or I/O error
60784** prevents us from positioning the cursor to its correct position.
60785**
60786** If a MoveTo operation is pending on the given cursor, then do that
60787** MoveTo now.  If no move is pending, check to see if the row has been
60788** deleted out from under the cursor and if it has, mark the row as
60789** a NULL row.
60790**
60791** If the cursor is already pointing to the correct row and that row has
60792** not been deleted out from under the cursor, then this routine is a no-op.
60793*/
60794SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
60795  if( p->deferredMoveto ){
60796    int res, rc;
60797#ifdef SQLITE_TEST
60798    extern int sqlite3_search_count;
60799#endif
60800    assert( p->isTable );
60801    rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
60802    if( rc ) return rc;
60803    p->lastRowid = p->movetoTarget;
60804    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
60805    p->rowidIsValid = 1;
60806#ifdef SQLITE_TEST
60807    sqlite3_search_count++;
60808#endif
60809    p->deferredMoveto = 0;
60810    p->cacheStatus = CACHE_STALE;
60811  }else if( ALWAYS(p->pCursor) ){
60812    int hasMoved;
60813    int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
60814    if( rc ) return rc;
60815    if( hasMoved ){
60816      p->cacheStatus = CACHE_STALE;
60817      p->nullRow = 1;
60818    }
60819  }
60820  return SQLITE_OK;
60821}
60822
60823/*
60824** The following functions:
60825**
60826** sqlite3VdbeSerialType()
60827** sqlite3VdbeSerialTypeLen()
60828** sqlite3VdbeSerialLen()
60829** sqlite3VdbeSerialPut()
60830** sqlite3VdbeSerialGet()
60831**
60832** encapsulate the code that serializes values for storage in SQLite
60833** data and index records. Each serialized value consists of a
60834** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
60835** integer, stored as a varint.
60836**
60837** In an SQLite index record, the serial type is stored directly before
60838** the blob of data that it corresponds to. In a table record, all serial
60839** types are stored at the start of the record, and the blobs of data at
60840** the end. Hence these functions allow the caller to handle the
60841** serial-type and data blob seperately.
60842**
60843** The following table describes the various storage classes for data:
60844**
60845**   serial type        bytes of data      type
60846**   --------------     ---------------    ---------------
60847**      0                     0            NULL
60848**      1                     1            signed integer
60849**      2                     2            signed integer
60850**      3                     3            signed integer
60851**      4                     4            signed integer
60852**      5                     6            signed integer
60853**      6                     8            signed integer
60854**      7                     8            IEEE float
60855**      8                     0            Integer constant 0
60856**      9                     0            Integer constant 1
60857**     10,11                               reserved for expansion
60858**    N>=12 and even       (N-12)/2        BLOB
60859**    N>=13 and odd        (N-13)/2        text
60860**
60861** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
60862** of SQLite will not understand those serial types.
60863*/
60864
60865/*
60866** Return the serial-type for the value stored in pMem.
60867*/
60868SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
60869  int flags = pMem->flags;
60870  int n;
60871
60872  if( flags&MEM_Null ){
60873    return 0;
60874  }
60875  if( flags&MEM_Int ){
60876    /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
60877#   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
60878    i64 i = pMem->u.i;
60879    u64 u;
60880    if( file_format>=4 && (i&1)==i ){
60881      return 8+(u32)i;
60882    }
60883    if( i<0 ){
60884      if( i<(-MAX_6BYTE) ) return 6;
60885      /* Previous test prevents:  u = -(-9223372036854775808) */
60886      u = -i;
60887    }else{
60888      u = i;
60889    }
60890    if( u<=127 ) return 1;
60891    if( u<=32767 ) return 2;
60892    if( u<=8388607 ) return 3;
60893    if( u<=2147483647 ) return 4;
60894    if( u<=MAX_6BYTE ) return 5;
60895    return 6;
60896  }
60897  if( flags&MEM_Real ){
60898    return 7;
60899  }
60900  assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
60901  n = pMem->n;
60902  if( flags & MEM_Zero ){
60903    n += pMem->u.nZero;
60904  }
60905  assert( n>=0 );
60906  return ((n*2) + 12 + ((flags&MEM_Str)!=0));
60907}
60908
60909/*
60910** Return the length of the data corresponding to the supplied serial-type.
60911*/
60912SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
60913  if( serial_type>=12 ){
60914    return (serial_type-12)/2;
60915  }else{
60916    static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
60917    return aSize[serial_type];
60918  }
60919}
60920
60921/*
60922** If we are on an architecture with mixed-endian floating
60923** points (ex: ARM7) then swap the lower 4 bytes with the
60924** upper 4 bytes.  Return the result.
60925**
60926** For most architectures, this is a no-op.
60927**
60928** (later):  It is reported to me that the mixed-endian problem
60929** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
60930** that early versions of GCC stored the two words of a 64-bit
60931** float in the wrong order.  And that error has been propagated
60932** ever since.  The blame is not necessarily with GCC, though.
60933** GCC might have just copying the problem from a prior compiler.
60934** I am also told that newer versions of GCC that follow a different
60935** ABI get the byte order right.
60936**
60937** Developers using SQLite on an ARM7 should compile and run their
60938** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
60939** enabled, some asserts below will ensure that the byte order of
60940** floating point values is correct.
60941**
60942** (2007-08-30)  Frank van Vugt has studied this problem closely
60943** and has send his findings to the SQLite developers.  Frank
60944** writes that some Linux kernels offer floating point hardware
60945** emulation that uses only 32-bit mantissas instead of a full
60946** 48-bits as required by the IEEE standard.  (This is the
60947** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
60948** byte swapping becomes very complicated.  To avoid problems,
60949** the necessary byte swapping is carried out using a 64-bit integer
60950** rather than a 64-bit float.  Frank assures us that the code here
60951** works for him.  We, the developers, have no way to independently
60952** verify this, but Frank seems to know what he is talking about
60953** so we trust him.
60954*/
60955#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
60956static u64 floatSwap(u64 in){
60957  union {
60958    u64 r;
60959    u32 i[2];
60960  } u;
60961  u32 t;
60962
60963  u.r = in;
60964  t = u.i[0];
60965  u.i[0] = u.i[1];
60966  u.i[1] = t;
60967  return u.r;
60968}
60969# define swapMixedEndianFloat(X)  X = floatSwap(X)
60970#else
60971# define swapMixedEndianFloat(X)
60972#endif
60973
60974/*
60975** Write the serialized data blob for the value stored in pMem into
60976** buf. It is assumed that the caller has allocated sufficient space.
60977** Return the number of bytes written.
60978**
60979** nBuf is the amount of space left in buf[].  nBuf must always be
60980** large enough to hold the entire field.  Except, if the field is
60981** a blob with a zero-filled tail, then buf[] might be just the right
60982** size to hold everything except for the zero-filled tail.  If buf[]
60983** is only big enough to hold the non-zero prefix, then only write that
60984** prefix into buf[].  But if buf[] is large enough to hold both the
60985** prefix and the tail then write the prefix and set the tail to all
60986** zeros.
60987**
60988** Return the number of bytes actually written into buf[].  The number
60989** of bytes in the zero-filled tail is included in the return value only
60990** if those bytes were zeroed in buf[].
60991*/
60992SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
60993  u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
60994  u32 len;
60995
60996  /* Integer and Real */
60997  if( serial_type<=7 && serial_type>0 ){
60998    u64 v;
60999    u32 i;
61000    if( serial_type==7 ){
61001      assert( sizeof(v)==sizeof(pMem->r) );
61002      memcpy(&v, &pMem->r, sizeof(v));
61003      swapMixedEndianFloat(v);
61004    }else{
61005      v = pMem->u.i;
61006    }
61007    len = i = sqlite3VdbeSerialTypeLen(serial_type);
61008    assert( len<=(u32)nBuf );
61009    while( i-- ){
61010      buf[i] = (u8)(v&0xFF);
61011      v >>= 8;
61012    }
61013    return len;
61014  }
61015
61016  /* String or blob */
61017  if( serial_type>=12 ){
61018    assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
61019             == (int)sqlite3VdbeSerialTypeLen(serial_type) );
61020    assert( pMem->n<=nBuf );
61021    len = pMem->n;
61022    memcpy(buf, pMem->z, len);
61023    if( pMem->flags & MEM_Zero ){
61024      len += pMem->u.nZero;
61025      assert( nBuf>=0 );
61026      if( len > (u32)nBuf ){
61027        len = (u32)nBuf;
61028      }
61029      memset(&buf[pMem->n], 0, len-pMem->n);
61030    }
61031    return len;
61032  }
61033
61034  /* NULL or constants 0 or 1 */
61035  return 0;
61036}
61037
61038/*
61039** Deserialize the data blob pointed to by buf as serial type serial_type
61040** and store the result in pMem.  Return the number of bytes read.
61041*/
61042SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
61043  const unsigned char *buf,     /* Buffer to deserialize from */
61044  u32 serial_type,              /* Serial type to deserialize */
61045  Mem *pMem                     /* Memory cell to write value into */
61046){
61047  switch( serial_type ){
61048    case 10:   /* Reserved for future use */
61049    case 11:   /* Reserved for future use */
61050    case 0: {  /* NULL */
61051      pMem->flags = MEM_Null;
61052      break;
61053    }
61054    case 1: { /* 1-byte signed integer */
61055      pMem->u.i = (signed char)buf[0];
61056      pMem->flags = MEM_Int;
61057      return 1;
61058    }
61059    case 2: { /* 2-byte signed integer */
61060      pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
61061      pMem->flags = MEM_Int;
61062      return 2;
61063    }
61064    case 3: { /* 3-byte signed integer */
61065      pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
61066      pMem->flags = MEM_Int;
61067      return 3;
61068    }
61069    case 4: { /* 4-byte signed integer */
61070      pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
61071      pMem->flags = MEM_Int;
61072      return 4;
61073    }
61074    case 5: { /* 6-byte signed integer */
61075      u64 x = (((signed char)buf[0])<<8) | buf[1];
61076      u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
61077      x = (x<<32) | y;
61078      pMem->u.i = *(i64*)&x;
61079      pMem->flags = MEM_Int;
61080      return 6;
61081    }
61082    case 6:   /* 8-byte signed integer */
61083    case 7: { /* IEEE floating point */
61084      u64 x;
61085      u32 y;
61086#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
61087      /* Verify that integers and floating point values use the same
61088      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
61089      ** defined that 64-bit floating point values really are mixed
61090      ** endian.
61091      */
61092      static const u64 t1 = ((u64)0x3ff00000)<<32;
61093      static const double r1 = 1.0;
61094      u64 t2 = t1;
61095      swapMixedEndianFloat(t2);
61096      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
61097#endif
61098
61099      x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
61100      y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
61101      x = (x<<32) | y;
61102      if( serial_type==6 ){
61103        pMem->u.i = *(i64*)&x;
61104        pMem->flags = MEM_Int;
61105      }else{
61106        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
61107        swapMixedEndianFloat(x);
61108        memcpy(&pMem->r, &x, sizeof(x));
61109        pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
61110      }
61111      return 8;
61112    }
61113    case 8:    /* Integer 0 */
61114    case 9: {  /* Integer 1 */
61115      pMem->u.i = serial_type-8;
61116      pMem->flags = MEM_Int;
61117      return 0;
61118    }
61119    default: {
61120      u32 len = (serial_type-12)/2;
61121      pMem->z = (char *)buf;
61122      pMem->n = len;
61123      pMem->xDel = 0;
61124      if( serial_type&0x01 ){
61125        pMem->flags = MEM_Str | MEM_Ephem;
61126      }else{
61127        pMem->flags = MEM_Blob | MEM_Ephem;
61128      }
61129      return len;
61130    }
61131  }
61132  return 0;
61133}
61134
61135/*
61136** This routine is used to allocate sufficient space for an UnpackedRecord
61137** structure large enough to be used with sqlite3VdbeRecordUnpack() if
61138** the first argument is a pointer to KeyInfo structure pKeyInfo.
61139**
61140** The space is either allocated using sqlite3DbMallocRaw() or from within
61141** the unaligned buffer passed via the second and third arguments (presumably
61142** stack space). If the former, then *ppFree is set to a pointer that should
61143** be eventually freed by the caller using sqlite3DbFree(). Or, if the
61144** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
61145** before returning.
61146**
61147** If an OOM error occurs, NULL is returned.
61148*/
61149SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
61150  KeyInfo *pKeyInfo,              /* Description of the record */
61151  char *pSpace,                   /* Unaligned space available */
61152  int szSpace,                    /* Size of pSpace[] in bytes */
61153  char **ppFree                   /* OUT: Caller should free this pointer */
61154){
61155  UnpackedRecord *p;              /* Unpacked record to return */
61156  int nOff;                       /* Increment pSpace by nOff to align it */
61157  int nByte;                      /* Number of bytes required for *p */
61158
61159  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
61160  ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
61161  ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
61162  */
61163  nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
61164  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
61165  if( nByte>szSpace+nOff ){
61166    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
61167    *ppFree = (char *)p;
61168    if( !p ) return 0;
61169  }else{
61170    p = (UnpackedRecord*)&pSpace[nOff];
61171    *ppFree = 0;
61172  }
61173
61174  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
61175  p->pKeyInfo = pKeyInfo;
61176  p->nField = pKeyInfo->nField + 1;
61177  return p;
61178}
61179
61180/*
61181** Given the nKey-byte encoding of a record in pKey[], populate the
61182** UnpackedRecord structure indicated by the fourth argument with the
61183** contents of the decoded record.
61184*/
61185SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
61186  KeyInfo *pKeyInfo,     /* Information about the record format */
61187  int nKey,              /* Size of the binary record */
61188  const void *pKey,      /* The binary record */
61189  UnpackedRecord *p      /* Populate this structure before returning. */
61190){
61191  const unsigned char *aKey = (const unsigned char *)pKey;
61192  int d;
61193  u32 idx;                        /* Offset in aKey[] to read from */
61194  u16 u;                          /* Unsigned loop counter */
61195  u32 szHdr;
61196  Mem *pMem = p->aMem;
61197
61198  p->flags = 0;
61199  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
61200  idx = getVarint32(aKey, szHdr);
61201  d = szHdr;
61202  u = 0;
61203  while( idx<szHdr && u<p->nField && d<=nKey ){
61204    u32 serial_type;
61205
61206    idx += getVarint32(&aKey[idx], serial_type);
61207    pMem->enc = pKeyInfo->enc;
61208    pMem->db = pKeyInfo->db;
61209    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
61210    pMem->zMalloc = 0;
61211    d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
61212    pMem++;
61213    u++;
61214  }
61215  assert( u<=pKeyInfo->nField + 1 );
61216  p->nField = u;
61217}
61218
61219/*
61220** This function compares the two table rows or index records
61221** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
61222** or positive integer if key1 is less than, equal to or
61223** greater than key2.  The {nKey1, pKey1} key must be a blob
61224** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
61225** key must be a parsed key such as obtained from
61226** sqlite3VdbeParseRecord.
61227**
61228** Key1 and Key2 do not have to contain the same number of fields.
61229** The key with fewer fields is usually compares less than the
61230** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
61231** and the common prefixes are equal, then key1 is less than key2.
61232** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
61233** equal, then the keys are considered to be equal and
61234** the parts beyond the common prefix are ignored.
61235**
61236** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
61237** the header of pKey1 is ignored.  It is assumed that pKey1 is
61238** an index key, and thus ends with a rowid value.  The last byte
61239** of the header will therefore be the serial type of the rowid:
61240** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
61241** The serial type of the final rowid will always be a single byte.
61242** By ignoring this last byte of the header, we force the comparison
61243** to ignore the rowid at the end of key1.
61244*/
61245SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
61246  int nKey1, const void *pKey1, /* Left key */
61247  UnpackedRecord *pPKey2        /* Right key */
61248){
61249  int d1;            /* Offset into aKey[] of next data element */
61250  u32 idx1;          /* Offset into aKey[] of next header element */
61251  u32 szHdr1;        /* Number of bytes in header */
61252  int i = 0;
61253  int nField;
61254  int rc = 0;
61255  const unsigned char *aKey1 = (const unsigned char *)pKey1;
61256  KeyInfo *pKeyInfo;
61257  Mem mem1;
61258
61259  pKeyInfo = pPKey2->pKeyInfo;
61260  mem1.enc = pKeyInfo->enc;
61261  mem1.db = pKeyInfo->db;
61262  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
61263  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
61264
61265  /* Compilers may complain that mem1.u.i is potentially uninitialized.
61266  ** We could initialize it, as shown here, to silence those complaints.
61267  ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
61268  ** the unnecessary initialization has a measurable negative performance
61269  ** impact, since this routine is a very high runner.  And so, we choose
61270  ** to ignore the compiler warnings and leave this variable uninitialized.
61271  */
61272  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
61273
61274  idx1 = getVarint32(aKey1, szHdr1);
61275  d1 = szHdr1;
61276  if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
61277    szHdr1--;
61278  }
61279  nField = pKeyInfo->nField;
61280  while( idx1<szHdr1 && i<pPKey2->nField ){
61281    u32 serial_type1;
61282
61283    /* Read the serial types for the next element in each key. */
61284    idx1 += getVarint32( aKey1+idx1, serial_type1 );
61285    if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
61286
61287    /* Extract the values to be compared.
61288    */
61289    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
61290
61291    /* Do the comparison
61292    */
61293    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
61294                           i<nField ? pKeyInfo->aColl[i] : 0);
61295    if( rc!=0 ){
61296      assert( mem1.zMalloc==0 );  /* See comment below */
61297
61298      /* Invert the result if we are using DESC sort order. */
61299      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
61300        rc = -rc;
61301      }
61302
61303      /* If the PREFIX_SEARCH flag is set and all fields except the final
61304      ** rowid field were equal, then clear the PREFIX_SEARCH flag and set
61305      ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
61306      ** This is used by the OP_IsUnique opcode.
61307      */
61308      if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
61309        assert( idx1==szHdr1 && rc );
61310        assert( mem1.flags & MEM_Int );
61311        pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
61312        pPKey2->rowid = mem1.u.i;
61313      }
61314
61315      return rc;
61316    }
61317    i++;
61318  }
61319
61320  /* No memory allocation is ever used on mem1.  Prove this using
61321  ** the following assert().  If the assert() fails, it indicates a
61322  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
61323  */
61324  assert( mem1.zMalloc==0 );
61325
61326  /* rc==0 here means that one of the keys ran out of fields and
61327  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
61328  ** flag is set, then break the tie by treating key2 as larger.
61329  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
61330  ** are considered to be equal.  Otherwise, the longer key is the
61331  ** larger.  As it happens, the pPKey2 will always be the longer
61332  ** if there is a difference.
61333  */
61334  assert( rc==0 );
61335  if( pPKey2->flags & UNPACKED_INCRKEY ){
61336    rc = -1;
61337  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
61338    /* Leave rc==0 */
61339  }else if( idx1<szHdr1 ){
61340    rc = 1;
61341  }
61342  return rc;
61343}
61344
61345
61346/*
61347** pCur points at an index entry created using the OP_MakeRecord opcode.
61348** Read the rowid (the last field in the record) and store it in *rowid.
61349** Return SQLITE_OK if everything works, or an error code otherwise.
61350**
61351** pCur might be pointing to text obtained from a corrupt database file.
61352** So the content cannot be trusted.  Do appropriate checks on the content.
61353*/
61354SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
61355  i64 nCellKey = 0;
61356  int rc;
61357  u32 szHdr;        /* Size of the header */
61358  u32 typeRowid;    /* Serial type of the rowid */
61359  u32 lenRowid;     /* Size of the rowid */
61360  Mem m, v;
61361
61362  UNUSED_PARAMETER(db);
61363
61364  /* Get the size of the index entry.  Only indices entries of less
61365  ** than 2GiB are support - anything large must be database corruption.
61366  ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
61367  ** this code can safely assume that nCellKey is 32-bits
61368  */
61369  assert( sqlite3BtreeCursorIsValid(pCur) );
61370  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61371  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
61372  assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
61373
61374  /* Read in the complete content of the index entry */
61375  memset(&m, 0, sizeof(m));
61376  rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
61377  if( rc ){
61378    return rc;
61379  }
61380
61381  /* The index entry must begin with a header size */
61382  (void)getVarint32((u8*)m.z, szHdr);
61383  testcase( szHdr==3 );
61384  testcase( szHdr==m.n );
61385  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
61386    goto idx_rowid_corruption;
61387  }
61388
61389  /* The last field of the index should be an integer - the ROWID.
61390  ** Verify that the last entry really is an integer. */
61391  (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
61392  testcase( typeRowid==1 );
61393  testcase( typeRowid==2 );
61394  testcase( typeRowid==3 );
61395  testcase( typeRowid==4 );
61396  testcase( typeRowid==5 );
61397  testcase( typeRowid==6 );
61398  testcase( typeRowid==8 );
61399  testcase( typeRowid==9 );
61400  if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
61401    goto idx_rowid_corruption;
61402  }
61403  lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
61404  testcase( (u32)m.n==szHdr+lenRowid );
61405  if( unlikely((u32)m.n<szHdr+lenRowid) ){
61406    goto idx_rowid_corruption;
61407  }
61408
61409  /* Fetch the integer off the end of the index record */
61410  sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
61411  *rowid = v.u.i;
61412  sqlite3VdbeMemRelease(&m);
61413  return SQLITE_OK;
61414
61415  /* Jump here if database corruption is detected after m has been
61416  ** allocated.  Free the m object and return SQLITE_CORRUPT. */
61417idx_rowid_corruption:
61418  testcase( m.zMalloc!=0 );
61419  sqlite3VdbeMemRelease(&m);
61420  return SQLITE_CORRUPT_BKPT;
61421}
61422
61423/*
61424** Compare the key of the index entry that cursor pC is pointing to against
61425** the key string in pUnpacked.  Write into *pRes a number
61426** that is negative, zero, or positive if pC is less than, equal to,
61427** or greater than pUnpacked.  Return SQLITE_OK on success.
61428**
61429** pUnpacked is either created without a rowid or is truncated so that it
61430** omits the rowid at the end.  The rowid at the end of the index entry
61431** is ignored as well.  Hence, this routine only compares the prefixes
61432** of the keys prior to the final rowid, not the entire key.
61433*/
61434SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
61435  VdbeCursor *pC,             /* The cursor to compare against */
61436  UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
61437  int *res                    /* Write the comparison result here */
61438){
61439  i64 nCellKey = 0;
61440  int rc;
61441  BtCursor *pCur = pC->pCursor;
61442  Mem m;
61443
61444  assert( sqlite3BtreeCursorIsValid(pCur) );
61445  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61446  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
61447  /* nCellKey will always be between 0 and 0xffffffff because of the say
61448  ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
61449  if( nCellKey<=0 || nCellKey>0x7fffffff ){
61450    *res = 0;
61451    return SQLITE_CORRUPT_BKPT;
61452  }
61453  memset(&m, 0, sizeof(m));
61454  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
61455  if( rc ){
61456    return rc;
61457  }
61458  assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
61459  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
61460  sqlite3VdbeMemRelease(&m);
61461  return SQLITE_OK;
61462}
61463
61464/*
61465** This routine sets the value to be returned by subsequent calls to
61466** sqlite3_changes() on the database handle 'db'.
61467*/
61468SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
61469  assert( sqlite3_mutex_held(db->mutex) );
61470  db->nChange = nChange;
61471  db->nTotalChange += nChange;
61472}
61473
61474/*
61475** Set a flag in the vdbe to update the change counter when it is finalised
61476** or reset.
61477*/
61478SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
61479  v->changeCntOn = 1;
61480}
61481
61482/*
61483** Mark every prepared statement associated with a database connection
61484** as expired.
61485**
61486** An expired statement means that recompilation of the statement is
61487** recommend.  Statements expire when things happen that make their
61488** programs obsolete.  Removing user-defined functions or collating
61489** sequences, or changing an authorization function are the types of
61490** things that make prepared statements obsolete.
61491*/
61492SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
61493  Vdbe *p;
61494  for(p = db->pVdbe; p; p=p->pNext){
61495    p->expired = 1;
61496  }
61497}
61498
61499/*
61500** Return the database associated with the Vdbe.
61501*/
61502SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
61503  return v->db;
61504}
61505
61506/*
61507** Return a pointer to an sqlite3_value structure containing the value bound
61508** parameter iVar of VM v. Except, if the value is an SQL NULL, return
61509** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
61510** constants) to the value before returning it.
61511**
61512** The returned value must be freed by the caller using sqlite3ValueFree().
61513*/
61514SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
61515  assert( iVar>0 );
61516  if( v ){
61517    Mem *pMem = &v->aVar[iVar-1];
61518    if( 0==(pMem->flags & MEM_Null) ){
61519      sqlite3_value *pRet = sqlite3ValueNew(v->db);
61520      if( pRet ){
61521        sqlite3VdbeMemCopy((Mem *)pRet, pMem);
61522        sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
61523        sqlite3VdbeMemStoreType((Mem *)pRet);
61524      }
61525      return pRet;
61526    }
61527  }
61528  return 0;
61529}
61530
61531/*
61532** Configure SQL variable iVar so that binding a new value to it signals
61533** to sqlite3_reoptimize() that re-preparing the statement may result
61534** in a better query plan.
61535*/
61536SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
61537  assert( iVar>0 );
61538  if( iVar>32 ){
61539    v->expmask = 0xffffffff;
61540  }else{
61541    v->expmask |= ((u32)1 << (iVar-1));
61542  }
61543}
61544
61545/************** End of vdbeaux.c *********************************************/
61546/************** Begin file vdbeapi.c *****************************************/
61547/*
61548** 2004 May 26
61549**
61550** The author disclaims copyright to this source code.  In place of
61551** a legal notice, here is a blessing:
61552**
61553**    May you do good and not evil.
61554**    May you find forgiveness for yourself and forgive others.
61555**    May you share freely, never taking more than you give.
61556**
61557*************************************************************************
61558**
61559** This file contains code use to implement APIs that are part of the
61560** VDBE.
61561*/
61562
61563#ifndef SQLITE_OMIT_DEPRECATED
61564/*
61565** Return TRUE (non-zero) of the statement supplied as an argument needs
61566** to be recompiled.  A statement needs to be recompiled whenever the
61567** execution environment changes in a way that would alter the program
61568** that sqlite3_prepare() generates.  For example, if new functions or
61569** collating sequences are registered or if an authorizer function is
61570** added or changed.
61571*/
61572SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
61573  Vdbe *p = (Vdbe*)pStmt;
61574  return p==0 || p->expired;
61575}
61576#endif
61577
61578/*
61579** Check on a Vdbe to make sure it has not been finalized.  Log
61580** an error and return true if it has been finalized (or is otherwise
61581** invalid).  Return false if it is ok.
61582*/
61583static int vdbeSafety(Vdbe *p){
61584  if( p->db==0 ){
61585    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
61586    return 1;
61587  }else{
61588    return 0;
61589  }
61590}
61591static int vdbeSafetyNotNull(Vdbe *p){
61592  if( p==0 ){
61593    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
61594    return 1;
61595  }else{
61596    return vdbeSafety(p);
61597  }
61598}
61599
61600/*
61601** The following routine destroys a virtual machine that is created by
61602** the sqlite3_compile() routine. The integer returned is an SQLITE_
61603** success/failure code that describes the result of executing the virtual
61604** machine.
61605**
61606** This routine sets the error code and string returned by
61607** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
61608*/
61609SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
61610  int rc;
61611  if( pStmt==0 ){
61612    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
61613    ** pointer is a harmless no-op. */
61614    rc = SQLITE_OK;
61615  }else{
61616    Vdbe *v = (Vdbe*)pStmt;
61617    sqlite3 *db = v->db;
61618#if SQLITE_THREADSAFE
61619    sqlite3_mutex *mutex;
61620#endif
61621    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
61622#if SQLITE_THREADSAFE
61623    mutex = v->db->mutex;
61624#endif
61625    sqlite3_mutex_enter(mutex);
61626    rc = sqlite3VdbeFinalize(v);
61627    rc = sqlite3ApiExit(db, rc);
61628    sqlite3_mutex_leave(mutex);
61629  }
61630  return rc;
61631}
61632
61633/*
61634** Terminate the current execution of an SQL statement and reset it
61635** back to its starting state so that it can be reused. A success code from
61636** the prior execution is returned.
61637**
61638** This routine sets the error code and string returned by
61639** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
61640*/
61641SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
61642  int rc;
61643  if( pStmt==0 ){
61644    rc = SQLITE_OK;
61645  }else{
61646    Vdbe *v = (Vdbe*)pStmt;
61647    sqlite3_mutex_enter(v->db->mutex);
61648    rc = sqlite3VdbeReset(v);
61649    sqlite3VdbeRewind(v);
61650    assert( (rc & (v->db->errMask))==rc );
61651    rc = sqlite3ApiExit(v->db, rc);
61652    sqlite3_mutex_leave(v->db->mutex);
61653  }
61654  return rc;
61655}
61656
61657/*
61658** Set all the parameters in the compiled SQL statement to NULL.
61659*/
61660SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
61661  int i;
61662  int rc = SQLITE_OK;
61663  Vdbe *p = (Vdbe*)pStmt;
61664#if SQLITE_THREADSAFE
61665  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
61666#endif
61667  sqlite3_mutex_enter(mutex);
61668  for(i=0; i<p->nVar; i++){
61669    sqlite3VdbeMemRelease(&p->aVar[i]);
61670    p->aVar[i].flags = MEM_Null;
61671  }
61672  if( p->isPrepareV2 && p->expmask ){
61673    p->expired = 1;
61674  }
61675  sqlite3_mutex_leave(mutex);
61676  return rc;
61677}
61678
61679
61680/**************************** sqlite3_value_  *******************************
61681** The following routines extract information from a Mem or sqlite3_value
61682** structure.
61683*/
61684SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
61685  Mem *p = (Mem*)pVal;
61686  if( p->flags & (MEM_Blob|MEM_Str) ){
61687    sqlite3VdbeMemExpandBlob(p);
61688    p->flags &= ~MEM_Str;
61689    p->flags |= MEM_Blob;
61690    return p->n ? p->z : 0;
61691  }else{
61692    return sqlite3_value_text(pVal);
61693  }
61694}
61695SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
61696  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
61697}
61698SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
61699  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
61700}
61701SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
61702  return sqlite3VdbeRealValue((Mem*)pVal);
61703}
61704SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
61705  return (int)sqlite3VdbeIntValue((Mem*)pVal);
61706}
61707SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
61708  return sqlite3VdbeIntValue((Mem*)pVal);
61709}
61710SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
61711  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
61712}
61713#ifndef SQLITE_OMIT_UTF16
61714SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
61715  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
61716}
61717SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
61718  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
61719}
61720SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
61721  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
61722}
61723#endif /* SQLITE_OMIT_UTF16 */
61724SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
61725  return pVal->type;
61726}
61727
61728/**************************** sqlite3_result_  *******************************
61729** The following routines are used by user-defined functions to specify
61730** the function result.
61731**
61732** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
61733** result as a string or blob but if the string or blob is too large, it
61734** then sets the error code to SQLITE_TOOBIG
61735*/
61736static void setResultStrOrError(
61737  sqlite3_context *pCtx,  /* Function context */
61738  const char *z,          /* String pointer */
61739  int n,                  /* Bytes in string, or negative */
61740  u8 enc,                 /* Encoding of z.  0 for BLOBs */
61741  void (*xDel)(void*)     /* Destructor function */
61742){
61743  if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
61744    sqlite3_result_error_toobig(pCtx);
61745  }
61746}
61747SQLITE_API void sqlite3_result_blob(
61748  sqlite3_context *pCtx,
61749  const void *z,
61750  int n,
61751  void (*xDel)(void *)
61752){
61753  assert( n>=0 );
61754  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61755  setResultStrOrError(pCtx, z, n, 0, xDel);
61756}
61757SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
61758  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61759  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
61760}
61761SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
61762  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61763  pCtx->isError = SQLITE_ERROR;
61764  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
61765}
61766#ifndef SQLITE_OMIT_UTF16
61767SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
61768  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61769  pCtx->isError = SQLITE_ERROR;
61770  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
61771}
61772#endif
61773SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
61774  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61775  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
61776}
61777SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
61778  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61779  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
61780}
61781SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
61782  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61783  sqlite3VdbeMemSetNull(&pCtx->s);
61784}
61785SQLITE_API void sqlite3_result_text(
61786  sqlite3_context *pCtx,
61787  const char *z,
61788  int n,
61789  void (*xDel)(void *)
61790){
61791  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61792  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
61793}
61794#ifndef SQLITE_OMIT_UTF16
61795SQLITE_API void sqlite3_result_text16(
61796  sqlite3_context *pCtx,
61797  const void *z,
61798  int n,
61799  void (*xDel)(void *)
61800){
61801  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61802  setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
61803}
61804SQLITE_API void sqlite3_result_text16be(
61805  sqlite3_context *pCtx,
61806  const void *z,
61807  int n,
61808  void (*xDel)(void *)
61809){
61810  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61811  setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
61812}
61813SQLITE_API void sqlite3_result_text16le(
61814  sqlite3_context *pCtx,
61815  const void *z,
61816  int n,
61817  void (*xDel)(void *)
61818){
61819  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61820  setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
61821}
61822#endif /* SQLITE_OMIT_UTF16 */
61823SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
61824  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61825  sqlite3VdbeMemCopy(&pCtx->s, pValue);
61826}
61827SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
61828  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61829  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
61830}
61831SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
61832  pCtx->isError = errCode;
61833  if( pCtx->s.flags & MEM_Null ){
61834    sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
61835                         SQLITE_UTF8, SQLITE_STATIC);
61836  }
61837}
61838
61839/* Force an SQLITE_TOOBIG error. */
61840SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
61841  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61842  pCtx->isError = SQLITE_TOOBIG;
61843  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
61844                       SQLITE_UTF8, SQLITE_STATIC);
61845}
61846
61847/* An SQLITE_NOMEM error. */
61848SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
61849  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
61850  sqlite3VdbeMemSetNull(&pCtx->s);
61851  pCtx->isError = SQLITE_NOMEM;
61852  pCtx->s.db->mallocFailed = 1;
61853}
61854
61855/*
61856** This function is called after a transaction has been committed. It
61857** invokes callbacks registered with sqlite3_wal_hook() as required.
61858*/
61859static int doWalCallbacks(sqlite3 *db){
61860  int rc = SQLITE_OK;
61861#ifndef SQLITE_OMIT_WAL
61862  int i;
61863  for(i=0; i<db->nDb; i++){
61864    Btree *pBt = db->aDb[i].pBt;
61865    if( pBt ){
61866      int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
61867      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
61868        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
61869      }
61870    }
61871  }
61872#endif
61873  return rc;
61874}
61875
61876/*
61877** Execute the statement pStmt, either until a row of data is ready, the
61878** statement is completely executed or an error occurs.
61879**
61880** This routine implements the bulk of the logic behind the sqlite_step()
61881** API.  The only thing omitted is the automatic recompile if a
61882** schema change has occurred.  That detail is handled by the
61883** outer sqlite3_step() wrapper procedure.
61884*/
61885static int sqlite3Step(Vdbe *p){
61886  sqlite3 *db;
61887  int rc;
61888
61889  assert(p);
61890  if( p->magic!=VDBE_MAGIC_RUN ){
61891    /* We used to require that sqlite3_reset() be called before retrying
61892    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
61893    ** with version 3.7.0, we changed this so that sqlite3_reset() would
61894    ** be called automatically instead of throwing the SQLITE_MISUSE error.
61895    ** This "automatic-reset" change is not technically an incompatibility,
61896    ** since any application that receives an SQLITE_MISUSE is broken by
61897    ** definition.
61898    **
61899    ** Nevertheless, some published applications that were originally written
61900    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
61901    ** returns, and the so were broken by the automatic-reset change.  As a
61902    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
61903    ** legacy behavior of returning SQLITE_MISUSE for cases where the
61904    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
61905    ** or SQLITE_BUSY error.
61906    */
61907#ifdef SQLITE_OMIT_AUTORESET
61908    if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
61909      sqlite3_reset((sqlite3_stmt*)p);
61910    }else{
61911      return SQLITE_MISUSE_BKPT;
61912    }
61913#else
61914    sqlite3_reset((sqlite3_stmt*)p);
61915#endif
61916  }
61917
61918  /* Check that malloc() has not failed. If it has, return early. */
61919  db = p->db;
61920  if( db->mallocFailed ){
61921    p->rc = SQLITE_NOMEM;
61922    return SQLITE_NOMEM;
61923  }
61924
61925  if( p->pc<=0 && p->expired ){
61926    p->rc = SQLITE_SCHEMA;
61927    rc = SQLITE_ERROR;
61928    goto end_of_step;
61929  }
61930  if( p->pc<0 ){
61931    /* If there are no other statements currently running, then
61932    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
61933    ** from interrupting a statement that has not yet started.
61934    */
61935    if( db->activeVdbeCnt==0 ){
61936      db->u1.isInterrupted = 0;
61937    }
61938
61939    assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
61940
61941#ifndef SQLITE_OMIT_TRACE
61942    if( db->xProfile && !db->init.busy ){
61943      sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
61944    }
61945#endif
61946
61947    db->activeVdbeCnt++;
61948    if( p->readOnly==0 ) db->writeVdbeCnt++;
61949    p->pc = 0;
61950  }
61951#ifndef SQLITE_OMIT_EXPLAIN
61952  if( p->explain ){
61953    rc = sqlite3VdbeList(p);
61954  }else
61955#endif /* SQLITE_OMIT_EXPLAIN */
61956  {
61957    db->vdbeExecCnt++;
61958    rc = sqlite3VdbeExec(p);
61959    db->vdbeExecCnt--;
61960  }
61961
61962#ifndef SQLITE_OMIT_TRACE
61963  /* Invoke the profile callback if there is one
61964  */
61965  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
61966    sqlite3_int64 iNow;
61967    sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
61968    db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
61969  }
61970#endif
61971
61972  if( rc==SQLITE_DONE ){
61973    assert( p->rc==SQLITE_OK );
61974    p->rc = doWalCallbacks(db);
61975    if( p->rc!=SQLITE_OK ){
61976      rc = SQLITE_ERROR;
61977    }
61978  }
61979
61980  db->errCode = rc;
61981  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
61982    p->rc = SQLITE_NOMEM;
61983  }
61984end_of_step:
61985  /* At this point local variable rc holds the value that should be
61986  ** returned if this statement was compiled using the legacy
61987  ** sqlite3_prepare() interface. According to the docs, this can only
61988  ** be one of the values in the first assert() below. Variable p->rc
61989  ** contains the value that would be returned if sqlite3_finalize()
61990  ** were called on statement p.
61991  */
61992  assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
61993       || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
61994  );
61995  assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
61996  if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
61997    /* If this statement was prepared using sqlite3_prepare_v2(), and an
61998    ** error has occured, then return the error code in p->rc to the
61999    ** caller. Set the error code in the database handle to the same value.
62000    */
62001    rc = db->errCode = p->rc;
62002  }
62003  return (rc&db->errMask);
62004}
62005
62006/*
62007** The maximum number of times that a statement will try to reparse
62008** itself before giving up and returning SQLITE_SCHEMA.
62009*/
62010#ifndef SQLITE_MAX_SCHEMA_RETRY
62011# define SQLITE_MAX_SCHEMA_RETRY 5
62012#endif
62013
62014/*
62015** This is the top-level implementation of sqlite3_step().  Call
62016** sqlite3Step() to do most of the work.  If a schema error occurs,
62017** call sqlite3Reprepare() and try again.
62018*/
62019SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
62020  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
62021  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
62022  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
62023  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
62024  sqlite3 *db;             /* The database connection */
62025
62026  if( vdbeSafetyNotNull(v) ){
62027    return SQLITE_MISUSE_BKPT;
62028  }
62029  db = v->db;
62030  sqlite3_mutex_enter(db->mutex);
62031  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
62032         && cnt++ < SQLITE_MAX_SCHEMA_RETRY
62033         && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
62034    sqlite3_reset(pStmt);
62035    assert( v->expired==0 );
62036  }
62037  if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
62038    /* This case occurs after failing to recompile an sql statement.
62039    ** The error message from the SQL compiler has already been loaded
62040    ** into the database handle. This block copies the error message
62041    ** from the database handle into the statement and sets the statement
62042    ** program counter to 0 to ensure that when the statement is
62043    ** finalized or reset the parser error message is available via
62044    ** sqlite3_errmsg() and sqlite3_errcode().
62045    */
62046    const char *zErr = (const char *)sqlite3_value_text(db->pErr);
62047    sqlite3DbFree(db, v->zErrMsg);
62048    if( !db->mallocFailed ){
62049      v->zErrMsg = sqlite3DbStrDup(db, zErr);
62050      v->rc = rc2;
62051    } else {
62052      v->zErrMsg = 0;
62053      v->rc = rc = SQLITE_NOMEM;
62054    }
62055  }
62056  rc = sqlite3ApiExit(db, rc);
62057  sqlite3_mutex_leave(db->mutex);
62058  return rc;
62059}
62060
62061/*
62062** Extract the user data from a sqlite3_context structure and return a
62063** pointer to it.
62064*/
62065SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
62066  assert( p && p->pFunc );
62067  return p->pFunc->pUserData;
62068}
62069
62070/*
62071** Extract the user data from a sqlite3_context structure and return a
62072** pointer to it.
62073**
62074** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
62075** returns a copy of the pointer to the database connection (the 1st
62076** parameter) of the sqlite3_create_function() and
62077** sqlite3_create_function16() routines that originally registered the
62078** application defined function.
62079*/
62080SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
62081  assert( p && p->pFunc );
62082  return p->s.db;
62083}
62084
62085/*
62086** The following is the implementation of an SQL function that always
62087** fails with an error message stating that the function is used in the
62088** wrong context.  The sqlite3_overload_function() API might construct
62089** SQL function that use this routine so that the functions will exist
62090** for name resolution but are actually overloaded by the xFindFunction
62091** method of virtual tables.
62092*/
62093SQLITE_PRIVATE void sqlite3InvalidFunction(
62094  sqlite3_context *context,  /* The function calling context */
62095  int NotUsed,               /* Number of arguments to the function */
62096  sqlite3_value **NotUsed2   /* Value of each argument */
62097){
62098  const char *zName = context->pFunc->zName;
62099  char *zErr;
62100  UNUSED_PARAMETER2(NotUsed, NotUsed2);
62101  zErr = sqlite3_mprintf(
62102      "unable to use function %s in the requested context", zName);
62103  sqlite3_result_error(context, zErr, -1);
62104  sqlite3_free(zErr);
62105}
62106
62107/*
62108** Allocate or return the aggregate context for a user function.  A new
62109** context is allocated on the first call.  Subsequent calls return the
62110** same context that was returned on prior calls.
62111*/
62112SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
62113  Mem *pMem;
62114  assert( p && p->pFunc && p->pFunc->xStep );
62115  assert( sqlite3_mutex_held(p->s.db->mutex) );
62116  pMem = p->pMem;
62117  testcase( nByte<0 );
62118  if( (pMem->flags & MEM_Agg)==0 ){
62119    if( nByte<=0 ){
62120      sqlite3VdbeMemReleaseExternal(pMem);
62121      pMem->flags = MEM_Null;
62122      pMem->z = 0;
62123    }else{
62124      sqlite3VdbeMemGrow(pMem, nByte, 0);
62125      pMem->flags = MEM_Agg;
62126      pMem->u.pDef = p->pFunc;
62127      if( pMem->z ){
62128        memset(pMem->z, 0, nByte);
62129      }
62130    }
62131  }
62132  return (void*)pMem->z;
62133}
62134
62135/*
62136** Return the auxilary data pointer, if any, for the iArg'th argument to
62137** the user-function defined by pCtx.
62138*/
62139SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
62140  VdbeFunc *pVdbeFunc;
62141
62142  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
62143  pVdbeFunc = pCtx->pVdbeFunc;
62144  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
62145    return 0;
62146  }
62147  return pVdbeFunc->apAux[iArg].pAux;
62148}
62149
62150/*
62151** Set the auxilary data pointer and delete function, for the iArg'th
62152** argument to the user-function defined by pCtx. Any previous value is
62153** deleted by calling the delete function specified when it was set.
62154*/
62155SQLITE_API void sqlite3_set_auxdata(
62156  sqlite3_context *pCtx,
62157  int iArg,
62158  void *pAux,
62159  void (*xDelete)(void*)
62160){
62161  struct AuxData *pAuxData;
62162  VdbeFunc *pVdbeFunc;
62163  if( iArg<0 ) goto failed;
62164
62165  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
62166  pVdbeFunc = pCtx->pVdbeFunc;
62167  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
62168    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
62169    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
62170    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
62171    if( !pVdbeFunc ){
62172      goto failed;
62173    }
62174    pCtx->pVdbeFunc = pVdbeFunc;
62175    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
62176    pVdbeFunc->nAux = iArg+1;
62177    pVdbeFunc->pFunc = pCtx->pFunc;
62178  }
62179
62180  pAuxData = &pVdbeFunc->apAux[iArg];
62181  if( pAuxData->pAux && pAuxData->xDelete ){
62182    pAuxData->xDelete(pAuxData->pAux);
62183  }
62184  pAuxData->pAux = pAux;
62185  pAuxData->xDelete = xDelete;
62186  return;
62187
62188failed:
62189  if( xDelete ){
62190    xDelete(pAux);
62191  }
62192}
62193
62194#ifndef SQLITE_OMIT_DEPRECATED
62195/*
62196** Return the number of times the Step function of a aggregate has been
62197** called.
62198**
62199** This function is deprecated.  Do not use it for new code.  It is
62200** provide only to avoid breaking legacy code.  New aggregate function
62201** implementations should keep their own counts within their aggregate
62202** context.
62203*/
62204SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
62205  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
62206  return p->pMem->n;
62207}
62208#endif
62209
62210/*
62211** Return the number of columns in the result set for the statement pStmt.
62212*/
62213SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
62214  Vdbe *pVm = (Vdbe *)pStmt;
62215  return pVm ? pVm->nResColumn : 0;
62216}
62217
62218/*
62219** Return the number of values available from the current row of the
62220** currently executing statement pStmt.
62221*/
62222SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
62223  Vdbe *pVm = (Vdbe *)pStmt;
62224  if( pVm==0 || pVm->pResultSet==0 ) return 0;
62225  return pVm->nResColumn;
62226}
62227
62228
62229/*
62230** Check to see if column iCol of the given statement is valid.  If
62231** it is, return a pointer to the Mem for the value of that column.
62232** If iCol is not valid, return a pointer to a Mem which has a value
62233** of NULL.
62234*/
62235static Mem *columnMem(sqlite3_stmt *pStmt, int i){
62236  Vdbe *pVm;
62237  Mem *pOut;
62238
62239  pVm = (Vdbe *)pStmt;
62240  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
62241    sqlite3_mutex_enter(pVm->db->mutex);
62242    pOut = &pVm->pResultSet[i];
62243  }else{
62244    /* If the value passed as the second argument is out of range, return
62245    ** a pointer to the following static Mem object which contains the
62246    ** value SQL NULL. Even though the Mem structure contains an element
62247    ** of type i64, on certain architecture (x86) with certain compiler
62248    ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
62249    ** instead of an 8-byte one. This all works fine, except that when
62250    ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
62251    ** that a Mem structure is located on an 8-byte boundary. To prevent
62252    ** this assert() from failing, when building with SQLITE_DEBUG defined
62253    ** using gcc, force nullMem to be 8-byte aligned using the magical
62254    ** __attribute__((aligned(8))) macro.  */
62255    static const Mem nullMem
62256#if defined(SQLITE_DEBUG) && defined(__GNUC__)
62257      __attribute__((aligned(8)))
62258#endif
62259      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
62260#ifdef SQLITE_DEBUG
62261         0, 0,  /* pScopyFrom, pFiller */
62262#endif
62263         0, 0 };
62264
62265    if( pVm && ALWAYS(pVm->db) ){
62266      sqlite3_mutex_enter(pVm->db->mutex);
62267      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
62268    }
62269    pOut = (Mem*)&nullMem;
62270  }
62271  return pOut;
62272}
62273
62274/*
62275** This function is called after invoking an sqlite3_value_XXX function on a
62276** column value (i.e. a value returned by evaluating an SQL expression in the
62277** select list of a SELECT statement) that may cause a malloc() failure. If
62278** malloc() has failed, the threads mallocFailed flag is cleared and the result
62279** code of statement pStmt set to SQLITE_NOMEM.
62280**
62281** Specifically, this is called from within:
62282**
62283**     sqlite3_column_int()
62284**     sqlite3_column_int64()
62285**     sqlite3_column_text()
62286**     sqlite3_column_text16()
62287**     sqlite3_column_real()
62288**     sqlite3_column_bytes()
62289**     sqlite3_column_bytes16()
62290**     sqiite3_column_blob()
62291*/
62292static void columnMallocFailure(sqlite3_stmt *pStmt)
62293{
62294  /* If malloc() failed during an encoding conversion within an
62295  ** sqlite3_column_XXX API, then set the return code of the statement to
62296  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
62297  ** and _finalize() will return NOMEM.
62298  */
62299  Vdbe *p = (Vdbe *)pStmt;
62300  if( p ){
62301    p->rc = sqlite3ApiExit(p->db, p->rc);
62302    sqlite3_mutex_leave(p->db->mutex);
62303  }
62304}
62305
62306/**************************** sqlite3_column_  *******************************
62307** The following routines are used to access elements of the current row
62308** in the result set.
62309*/
62310SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
62311  const void *val;
62312  val = sqlite3_value_blob( columnMem(pStmt,i) );
62313  /* Even though there is no encoding conversion, value_blob() might
62314  ** need to call malloc() to expand the result of a zeroblob()
62315  ** expression.
62316  */
62317  columnMallocFailure(pStmt);
62318  return val;
62319}
62320SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
62321  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
62322  columnMallocFailure(pStmt);
62323  return val;
62324}
62325SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
62326  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
62327  columnMallocFailure(pStmt);
62328  return val;
62329}
62330SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
62331  double val = sqlite3_value_double( columnMem(pStmt,i) );
62332  columnMallocFailure(pStmt);
62333  return val;
62334}
62335SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
62336  int val = sqlite3_value_int( columnMem(pStmt,i) );
62337  columnMallocFailure(pStmt);
62338  return val;
62339}
62340SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
62341  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
62342  columnMallocFailure(pStmt);
62343  return val;
62344}
62345SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
62346  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
62347  columnMallocFailure(pStmt);
62348  return val;
62349}
62350SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
62351  Mem *pOut = columnMem(pStmt, i);
62352  if( pOut->flags&MEM_Static ){
62353    pOut->flags &= ~MEM_Static;
62354    pOut->flags |= MEM_Ephem;
62355  }
62356  columnMallocFailure(pStmt);
62357  return (sqlite3_value *)pOut;
62358}
62359#ifndef SQLITE_OMIT_UTF16
62360SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
62361  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
62362  columnMallocFailure(pStmt);
62363  return val;
62364}
62365#endif /* SQLITE_OMIT_UTF16 */
62366SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
62367  int iType = sqlite3_value_type( columnMem(pStmt,i) );
62368  columnMallocFailure(pStmt);
62369  return iType;
62370}
62371
62372/* The following function is experimental and subject to change or
62373** removal */
62374/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
62375**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
62376**}
62377*/
62378
62379/*
62380** Convert the N-th element of pStmt->pColName[] into a string using
62381** xFunc() then return that string.  If N is out of range, return 0.
62382**
62383** There are up to 5 names for each column.  useType determines which
62384** name is returned.  Here are the names:
62385**
62386**    0      The column name as it should be displayed for output
62387**    1      The datatype name for the column
62388**    2      The name of the database that the column derives from
62389**    3      The name of the table that the column derives from
62390**    4      The name of the table column that the result column derives from
62391**
62392** If the result is not a simple column reference (if it is an expression
62393** or a constant) then useTypes 2, 3, and 4 return NULL.
62394*/
62395static const void *columnName(
62396  sqlite3_stmt *pStmt,
62397  int N,
62398  const void *(*xFunc)(Mem*),
62399  int useType
62400){
62401  const void *ret = 0;
62402  Vdbe *p = (Vdbe *)pStmt;
62403  int n;
62404  sqlite3 *db = p->db;
62405
62406  assert( db!=0 );
62407  n = sqlite3_column_count(pStmt);
62408  if( N<n && N>=0 ){
62409    N += useType*n;
62410    sqlite3_mutex_enter(db->mutex);
62411    assert( db->mallocFailed==0 );
62412    ret = xFunc(&p->aColName[N]);
62413     /* A malloc may have failed inside of the xFunc() call. If this
62414    ** is the case, clear the mallocFailed flag and return NULL.
62415    */
62416    if( db->mallocFailed ){
62417      db->mallocFailed = 0;
62418      ret = 0;
62419    }
62420    sqlite3_mutex_leave(db->mutex);
62421  }
62422  return ret;
62423}
62424
62425/*
62426** Return the name of the Nth column of the result set returned by SQL
62427** statement pStmt.
62428*/
62429SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
62430  return columnName(
62431      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
62432}
62433#ifndef SQLITE_OMIT_UTF16
62434SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
62435  return columnName(
62436      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
62437}
62438#endif
62439
62440/*
62441** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
62442** not define OMIT_DECLTYPE.
62443*/
62444#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
62445# error "Must not define both SQLITE_OMIT_DECLTYPE \
62446         and SQLITE_ENABLE_COLUMN_METADATA"
62447#endif
62448
62449#ifndef SQLITE_OMIT_DECLTYPE
62450/*
62451** Return the column declaration type (if applicable) of the 'i'th column
62452** of the result set of SQL statement pStmt.
62453*/
62454SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
62455  return columnName(
62456      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
62457}
62458#ifndef SQLITE_OMIT_UTF16
62459SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
62460  return columnName(
62461      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
62462}
62463#endif /* SQLITE_OMIT_UTF16 */
62464#endif /* SQLITE_OMIT_DECLTYPE */
62465
62466#ifdef SQLITE_ENABLE_COLUMN_METADATA
62467/*
62468** Return the name of the database from which a result column derives.
62469** NULL is returned if the result column is an expression or constant or
62470** anything else which is not an unabiguous reference to a database column.
62471*/
62472SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
62473  return columnName(
62474      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
62475}
62476#ifndef SQLITE_OMIT_UTF16
62477SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
62478  return columnName(
62479      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
62480}
62481#endif /* SQLITE_OMIT_UTF16 */
62482
62483/*
62484** Return the name of the table from which a result column derives.
62485** NULL is returned if the result column is an expression or constant or
62486** anything else which is not an unabiguous reference to a database column.
62487*/
62488SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
62489  return columnName(
62490      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
62491}
62492#ifndef SQLITE_OMIT_UTF16
62493SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
62494  return columnName(
62495      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
62496}
62497#endif /* SQLITE_OMIT_UTF16 */
62498
62499/*
62500** Return the name of the table column from which a result column derives.
62501** NULL is returned if the result column is an expression or constant or
62502** anything else which is not an unabiguous reference to a database column.
62503*/
62504SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
62505  return columnName(
62506      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
62507}
62508#ifndef SQLITE_OMIT_UTF16
62509SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
62510  return columnName(
62511      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
62512}
62513#endif /* SQLITE_OMIT_UTF16 */
62514#endif /* SQLITE_ENABLE_COLUMN_METADATA */
62515
62516
62517/******************************* sqlite3_bind_  ***************************
62518**
62519** Routines used to attach values to wildcards in a compiled SQL statement.
62520*/
62521/*
62522** Unbind the value bound to variable i in virtual machine p. This is the
62523** the same as binding a NULL value to the column. If the "i" parameter is
62524** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
62525**
62526** A successful evaluation of this routine acquires the mutex on p.
62527** the mutex is released if any kind of error occurs.
62528**
62529** The error code stored in database p->db is overwritten with the return
62530** value in any case.
62531*/
62532static int vdbeUnbind(Vdbe *p, int i){
62533  Mem *pVar;
62534  if( vdbeSafetyNotNull(p) ){
62535    return SQLITE_MISUSE_BKPT;
62536  }
62537  sqlite3_mutex_enter(p->db->mutex);
62538  if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
62539    sqlite3Error(p->db, SQLITE_MISUSE, 0);
62540    sqlite3_mutex_leave(p->db->mutex);
62541    sqlite3_log(SQLITE_MISUSE,
62542        "bind on a busy prepared statement: [%s]", p->zSql);
62543    return SQLITE_MISUSE_BKPT;
62544  }
62545  if( i<1 || i>p->nVar ){
62546    sqlite3Error(p->db, SQLITE_RANGE, 0);
62547    sqlite3_mutex_leave(p->db->mutex);
62548    return SQLITE_RANGE;
62549  }
62550  i--;
62551  pVar = &p->aVar[i];
62552  sqlite3VdbeMemRelease(pVar);
62553  pVar->flags = MEM_Null;
62554  sqlite3Error(p->db, SQLITE_OK, 0);
62555
62556  /* If the bit corresponding to this variable in Vdbe.expmask is set, then
62557  ** binding a new value to this variable invalidates the current query plan.
62558  **
62559  ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
62560  ** parameter in the WHERE clause might influence the choice of query plan
62561  ** for a statement, then the statement will be automatically recompiled,
62562  ** as if there had been a schema change, on the first sqlite3_step() call
62563  ** following any change to the bindings of that parameter.
62564  */
62565  if( p->isPrepareV2 &&
62566     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
62567  ){
62568    p->expired = 1;
62569  }
62570  return SQLITE_OK;
62571}
62572
62573/*
62574** Bind a text or BLOB value.
62575*/
62576static int bindText(
62577  sqlite3_stmt *pStmt,   /* The statement to bind against */
62578  int i,                 /* Index of the parameter to bind */
62579  const void *zData,     /* Pointer to the data to be bound */
62580  int nData,             /* Number of bytes of data to be bound */
62581  void (*xDel)(void*),   /* Destructor for the data */
62582  u8 encoding            /* Encoding for the data */
62583){
62584  Vdbe *p = (Vdbe *)pStmt;
62585  Mem *pVar;
62586  int rc;
62587
62588  rc = vdbeUnbind(p, i);
62589  if( rc==SQLITE_OK ){
62590    if( zData!=0 ){
62591      pVar = &p->aVar[i-1];
62592      rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
62593      if( rc==SQLITE_OK && encoding!=0 ){
62594        rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
62595      }
62596      sqlite3Error(p->db, rc, 0);
62597      rc = sqlite3ApiExit(p->db, rc);
62598    }
62599    sqlite3_mutex_leave(p->db->mutex);
62600  }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
62601    xDel((void*)zData);
62602  }
62603  return rc;
62604}
62605
62606
62607/*
62608** Bind a blob value to an SQL statement variable.
62609*/
62610SQLITE_API int sqlite3_bind_blob(
62611  sqlite3_stmt *pStmt,
62612  int i,
62613  const void *zData,
62614  int nData,
62615  void (*xDel)(void*)
62616){
62617  return bindText(pStmt, i, zData, nData, xDel, 0);
62618}
62619SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
62620  int rc;
62621  Vdbe *p = (Vdbe *)pStmt;
62622  rc = vdbeUnbind(p, i);
62623  if( rc==SQLITE_OK ){
62624    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
62625    sqlite3_mutex_leave(p->db->mutex);
62626  }
62627  return rc;
62628}
62629SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
62630  return sqlite3_bind_int64(p, i, (i64)iValue);
62631}
62632SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
62633  int rc;
62634  Vdbe *p = (Vdbe *)pStmt;
62635  rc = vdbeUnbind(p, i);
62636  if( rc==SQLITE_OK ){
62637    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
62638    sqlite3_mutex_leave(p->db->mutex);
62639  }
62640  return rc;
62641}
62642SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
62643  int rc;
62644  Vdbe *p = (Vdbe*)pStmt;
62645  rc = vdbeUnbind(p, i);
62646  if( rc==SQLITE_OK ){
62647    sqlite3_mutex_leave(p->db->mutex);
62648  }
62649  return rc;
62650}
62651SQLITE_API int sqlite3_bind_text(
62652  sqlite3_stmt *pStmt,
62653  int i,
62654  const char *zData,
62655  int nData,
62656  void (*xDel)(void*)
62657){
62658  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
62659}
62660#ifndef SQLITE_OMIT_UTF16
62661SQLITE_API int sqlite3_bind_text16(
62662  sqlite3_stmt *pStmt,
62663  int i,
62664  const void *zData,
62665  int nData,
62666  void (*xDel)(void*)
62667){
62668  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
62669}
62670#endif /* SQLITE_OMIT_UTF16 */
62671SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
62672  int rc;
62673  switch( pValue->type ){
62674    case SQLITE_INTEGER: {
62675      rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
62676      break;
62677    }
62678    case SQLITE_FLOAT: {
62679      rc = sqlite3_bind_double(pStmt, i, pValue->r);
62680      break;
62681    }
62682    case SQLITE_BLOB: {
62683      if( pValue->flags & MEM_Zero ){
62684        rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
62685      }else{
62686        rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
62687      }
62688      break;
62689    }
62690    case SQLITE_TEXT: {
62691      rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
62692                              pValue->enc);
62693      break;
62694    }
62695    default: {
62696      rc = sqlite3_bind_null(pStmt, i);
62697      break;
62698    }
62699  }
62700  return rc;
62701}
62702SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
62703  int rc;
62704  Vdbe *p = (Vdbe *)pStmt;
62705  rc = vdbeUnbind(p, i);
62706  if( rc==SQLITE_OK ){
62707    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
62708    sqlite3_mutex_leave(p->db->mutex);
62709  }
62710  return rc;
62711}
62712
62713/*
62714** Return the number of wildcards that can be potentially bound to.
62715** This routine is added to support DBD::SQLite.
62716*/
62717SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
62718  Vdbe *p = (Vdbe*)pStmt;
62719  return p ? p->nVar : 0;
62720}
62721
62722/*
62723** Return the name of a wildcard parameter.  Return NULL if the index
62724** is out of range or if the wildcard is unnamed.
62725**
62726** The result is always UTF-8.
62727*/
62728SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
62729  Vdbe *p = (Vdbe*)pStmt;
62730  if( p==0 || i<1 || i>p->nzVar ){
62731    return 0;
62732  }
62733  return p->azVar[i-1];
62734}
62735
62736/*
62737** Given a wildcard parameter name, return the index of the variable
62738** with that name.  If there is no variable with the given name,
62739** return 0.
62740*/
62741SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
62742  int i;
62743  if( p==0 ){
62744    return 0;
62745  }
62746  if( zName ){
62747    for(i=0; i<p->nzVar; i++){
62748      const char *z = p->azVar[i];
62749      if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
62750        return i+1;
62751      }
62752    }
62753  }
62754  return 0;
62755}
62756SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
62757  return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
62758}
62759
62760/*
62761** Transfer all bindings from the first statement over to the second.
62762*/
62763SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
62764  Vdbe *pFrom = (Vdbe*)pFromStmt;
62765  Vdbe *pTo = (Vdbe*)pToStmt;
62766  int i;
62767  assert( pTo->db==pFrom->db );
62768  assert( pTo->nVar==pFrom->nVar );
62769  sqlite3_mutex_enter(pTo->db->mutex);
62770  for(i=0; i<pFrom->nVar; i++){
62771    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
62772  }
62773  sqlite3_mutex_leave(pTo->db->mutex);
62774  return SQLITE_OK;
62775}
62776
62777#ifndef SQLITE_OMIT_DEPRECATED
62778/*
62779** Deprecated external interface.  Internal/core SQLite code
62780** should call sqlite3TransferBindings.
62781**
62782** Is is misuse to call this routine with statements from different
62783** database connections.  But as this is a deprecated interface, we
62784** will not bother to check for that condition.
62785**
62786** If the two statements contain a different number of bindings, then
62787** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
62788** SQLITE_OK is returned.
62789*/
62790SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
62791  Vdbe *pFrom = (Vdbe*)pFromStmt;
62792  Vdbe *pTo = (Vdbe*)pToStmt;
62793  if( pFrom->nVar!=pTo->nVar ){
62794    return SQLITE_ERROR;
62795  }
62796  if( pTo->isPrepareV2 && pTo->expmask ){
62797    pTo->expired = 1;
62798  }
62799  if( pFrom->isPrepareV2 && pFrom->expmask ){
62800    pFrom->expired = 1;
62801  }
62802  return sqlite3TransferBindings(pFromStmt, pToStmt);
62803}
62804#endif
62805
62806/*
62807** Return the sqlite3* database handle to which the prepared statement given
62808** in the argument belongs.  This is the same database handle that was
62809** the first argument to the sqlite3_prepare() that was used to create
62810** the statement in the first place.
62811*/
62812SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
62813  return pStmt ? ((Vdbe*)pStmt)->db : 0;
62814}
62815
62816/*
62817** Return true if the prepared statement is guaranteed to not modify the
62818** database.
62819*/
62820SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
62821  return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
62822}
62823
62824/*
62825** Return a pointer to the next prepared statement after pStmt associated
62826** with database connection pDb.  If pStmt is NULL, return the first
62827** prepared statement for the database connection.  Return NULL if there
62828** are no more.
62829*/
62830SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
62831  sqlite3_stmt *pNext;
62832  sqlite3_mutex_enter(pDb->mutex);
62833  if( pStmt==0 ){
62834    pNext = (sqlite3_stmt*)pDb->pVdbe;
62835  }else{
62836    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
62837  }
62838  sqlite3_mutex_leave(pDb->mutex);
62839  return pNext;
62840}
62841
62842/*
62843** Return the value of a status counter for a prepared statement
62844*/
62845SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
62846  Vdbe *pVdbe = (Vdbe*)pStmt;
62847  int v = pVdbe->aCounter[op-1];
62848  if( resetFlag ) pVdbe->aCounter[op-1] = 0;
62849  return v;
62850}
62851
62852/************** End of vdbeapi.c *********************************************/
62853/************** Begin file vdbetrace.c ***************************************/
62854/*
62855** 2009 November 25
62856**
62857** The author disclaims copyright to this source code.  In place of
62858** a legal notice, here is a blessing:
62859**
62860**    May you do good and not evil.
62861**    May you find forgiveness for yourself and forgive others.
62862**    May you share freely, never taking more than you give.
62863**
62864*************************************************************************
62865**
62866** This file contains code used to insert the values of host parameters
62867** (aka "wildcards") into the SQL text output by sqlite3_trace().
62868*/
62869
62870#ifndef SQLITE_OMIT_TRACE
62871
62872/*
62873** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
62874** bytes in this text up to but excluding the first character in
62875** a host parameter.  If the text contains no host parameters, return
62876** the total number of bytes in the text.
62877*/
62878static int findNextHostParameter(const char *zSql, int *pnToken){
62879  int tokenType;
62880  int nTotal = 0;
62881  int n;
62882
62883  *pnToken = 0;
62884  while( zSql[0] ){
62885    n = sqlite3GetToken((u8*)zSql, &tokenType);
62886    assert( n>0 && tokenType!=TK_ILLEGAL );
62887    if( tokenType==TK_VARIABLE ){
62888      *pnToken = n;
62889      break;
62890    }
62891    nTotal += n;
62892    zSql += n;
62893  }
62894  return nTotal;
62895}
62896
62897/*
62898** This function returns a pointer to a nul-terminated string in memory
62899** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
62900** string contains a copy of zRawSql but with host parameters expanded to
62901** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
62902** then the returned string holds a copy of zRawSql with "-- " prepended
62903** to each line of text.
62904**
62905** The calling function is responsible for making sure the memory returned
62906** is eventually freed.
62907**
62908** ALGORITHM:  Scan the input string looking for host parameters in any of
62909** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
62910** string literals, quoted identifier names, and comments.  For text forms,
62911** the host parameter index is found by scanning the perpared
62912** statement for the corresponding OP_Variable opcode.  Once the host
62913** parameter index is known, locate the value in p->aVar[].  Then render
62914** the value as a literal in place of the host parameter name.
62915*/
62916SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
62917  Vdbe *p,                 /* The prepared statement being evaluated */
62918  const char *zRawSql      /* Raw text of the SQL statement */
62919){
62920  sqlite3 *db;             /* The database connection */
62921  int idx = 0;             /* Index of a host parameter */
62922  int nextIndex = 1;       /* Index of next ? host parameter */
62923  int n;                   /* Length of a token prefix */
62924  int nToken;              /* Length of the parameter token */
62925  int i;                   /* Loop counter */
62926  Mem *pVar;               /* Value of a host parameter */
62927  StrAccum out;            /* Accumulate the output here */
62928  char zBase[100];         /* Initial working space */
62929
62930  db = p->db;
62931  sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
62932                      db->aLimit[SQLITE_LIMIT_LENGTH]);
62933  out.db = db;
62934  if( db->vdbeExecCnt>1 ){
62935    while( *zRawSql ){
62936      const char *zStart = zRawSql;
62937      while( *(zRawSql++)!='\n' && *zRawSql );
62938      sqlite3StrAccumAppend(&out, "-- ", 3);
62939      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
62940    }
62941  }else{
62942    while( zRawSql[0] ){
62943      n = findNextHostParameter(zRawSql, &nToken);
62944      assert( n>0 );
62945      sqlite3StrAccumAppend(&out, zRawSql, n);
62946      zRawSql += n;
62947      assert( zRawSql[0] || nToken==0 );
62948      if( nToken==0 ) break;
62949      if( zRawSql[0]=='?' ){
62950        if( nToken>1 ){
62951          assert( sqlite3Isdigit(zRawSql[1]) );
62952          sqlite3GetInt32(&zRawSql[1], &idx);
62953        }else{
62954          idx = nextIndex;
62955        }
62956      }else{
62957        assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
62958        testcase( zRawSql[0]==':' );
62959        testcase( zRawSql[0]=='$' );
62960        testcase( zRawSql[0]=='@' );
62961        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
62962        assert( idx>0 );
62963      }
62964      zRawSql += nToken;
62965      nextIndex = idx + 1;
62966      assert( idx>0 && idx<=p->nVar );
62967      pVar = &p->aVar[idx-1];
62968      if( pVar->flags & MEM_Null ){
62969        sqlite3StrAccumAppend(&out, "NULL", 4);
62970      }else if( pVar->flags & MEM_Int ){
62971        sqlite3XPrintf(&out, "%lld", pVar->u.i);
62972      }else if( pVar->flags & MEM_Real ){
62973        sqlite3XPrintf(&out, "%!.15g", pVar->r);
62974      }else if( pVar->flags & MEM_Str ){
62975#ifndef SQLITE_OMIT_UTF16
62976        u8 enc = ENC(db);
62977        if( enc!=SQLITE_UTF8 ){
62978          Mem utf8;
62979          memset(&utf8, 0, sizeof(utf8));
62980          utf8.db = db;
62981          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
62982          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
62983          sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
62984          sqlite3VdbeMemRelease(&utf8);
62985        }else
62986#endif
62987        {
62988          sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
62989        }
62990      }else if( pVar->flags & MEM_Zero ){
62991        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
62992      }else{
62993        assert( pVar->flags & MEM_Blob );
62994        sqlite3StrAccumAppend(&out, "x'", 2);
62995        for(i=0; i<pVar->n; i++){
62996          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
62997        }
62998        sqlite3StrAccumAppend(&out, "'", 1);
62999      }
63000    }
63001  }
63002  return sqlite3StrAccumFinish(&out);
63003}
63004
63005#endif /* #ifndef SQLITE_OMIT_TRACE */
63006
63007/************** End of vdbetrace.c *******************************************/
63008/************** Begin file vdbe.c ********************************************/
63009/*
63010** 2001 September 15
63011**
63012** The author disclaims copyright to this source code.  In place of
63013** a legal notice, here is a blessing:
63014**
63015**    May you do good and not evil.
63016**    May you find forgiveness for yourself and forgive others.
63017**    May you share freely, never taking more than you give.
63018**
63019*************************************************************************
63020** The code in this file implements execution method of the
63021** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
63022** handles housekeeping details such as creating and deleting
63023** VDBE instances.  This file is solely interested in executing
63024** the VDBE program.
63025**
63026** In the external interface, an "sqlite3_stmt*" is an opaque pointer
63027** to a VDBE.
63028**
63029** The SQL parser generates a program which is then executed by
63030** the VDBE to do the work of the SQL statement.  VDBE programs are
63031** similar in form to assembly language.  The program consists of
63032** a linear sequence of operations.  Each operation has an opcode
63033** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4
63034** is a null-terminated string.  Operand P5 is an unsigned character.
63035** Few opcodes use all 5 operands.
63036**
63037** Computation results are stored on a set of registers numbered beginning
63038** with 1 and going up to Vdbe.nMem.  Each register can store
63039** either an integer, a null-terminated string, a floating point
63040** number, or the SQL "NULL" value.  An implicit conversion from one
63041** type to the other occurs as necessary.
63042**
63043** Most of the code in this file is taken up by the sqlite3VdbeExec()
63044** function which does the work of interpreting a VDBE program.
63045** But other routines are also provided to help in building up
63046** a program instruction by instruction.
63047**
63048** Various scripts scan this source file in order to generate HTML
63049** documentation, headers files, or other derived files.  The formatting
63050** of the code in this file is, therefore, important.  See other comments
63051** in this file for details.  If in doubt, do not deviate from existing
63052** commenting and indentation practices when changing or adding code.
63053*/
63054
63055/*
63056** Invoke this macro on memory cells just prior to changing the
63057** value of the cell.  This macro verifies that shallow copies are
63058** not misused.
63059*/
63060#ifdef SQLITE_DEBUG
63061# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
63062#else
63063# define memAboutToChange(P,M)
63064#endif
63065
63066/*
63067** The following global variable is incremented every time a cursor
63068** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
63069** procedures use this information to make sure that indices are
63070** working correctly.  This variable has no function other than to
63071** help verify the correct operation of the library.
63072*/
63073#ifdef SQLITE_TEST
63074SQLITE_API int sqlite3_search_count = 0;
63075#endif
63076
63077/*
63078** When this global variable is positive, it gets decremented once before
63079** each instruction in the VDBE.  When reaches zero, the u1.isInterrupted
63080** field of the sqlite3 structure is set in order to simulate and interrupt.
63081**
63082** This facility is used for testing purposes only.  It does not function
63083** in an ordinary build.
63084*/
63085#ifdef SQLITE_TEST
63086SQLITE_API int sqlite3_interrupt_count = 0;
63087#endif
63088
63089/*
63090** The next global variable is incremented each type the OP_Sort opcode
63091** is executed.  The test procedures use this information to make sure that
63092** sorting is occurring or not occurring at appropriate times.   This variable
63093** has no function other than to help verify the correct operation of the
63094** library.
63095*/
63096#ifdef SQLITE_TEST
63097SQLITE_API int sqlite3_sort_count = 0;
63098#endif
63099
63100/*
63101** The next global variable records the size of the largest MEM_Blob
63102** or MEM_Str that has been used by a VDBE opcode.  The test procedures
63103** use this information to make sure that the zero-blob functionality
63104** is working correctly.   This variable has no function other than to
63105** help verify the correct operation of the library.
63106*/
63107#ifdef SQLITE_TEST
63108SQLITE_API int sqlite3_max_blobsize = 0;
63109static void updateMaxBlobsize(Mem *p){
63110  if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
63111    sqlite3_max_blobsize = p->n;
63112  }
63113}
63114#endif
63115
63116/*
63117** The next global variable is incremented each type the OP_Found opcode
63118** is executed. This is used to test whether or not the foreign key
63119** operation implemented using OP_FkIsZero is working. This variable
63120** has no function other than to help verify the correct operation of the
63121** library.
63122*/
63123#ifdef SQLITE_TEST
63124SQLITE_API int sqlite3_found_count = 0;
63125#endif
63126
63127/*
63128** Test a register to see if it exceeds the current maximum blob size.
63129** If it does, record the new maximum blob size.
63130*/
63131#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
63132# define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
63133#else
63134# define UPDATE_MAX_BLOBSIZE(P)
63135#endif
63136
63137/*
63138** Convert the given register into a string if it isn't one
63139** already. Return non-zero if a malloc() fails.
63140*/
63141#define Stringify(P, enc) \
63142   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
63143     { goto no_mem; }
63144
63145/*
63146** An ephemeral string value (signified by the MEM_Ephem flag) contains
63147** a pointer to a dynamically allocated string where some other entity
63148** is responsible for deallocating that string.  Because the register
63149** does not control the string, it might be deleted without the register
63150** knowing it.
63151**
63152** This routine converts an ephemeral string into a dynamically allocated
63153** string that the register itself controls.  In other words, it
63154** converts an MEM_Ephem string into an MEM_Dyn string.
63155*/
63156#define Deephemeralize(P) \
63157   if( ((P)->flags&MEM_Ephem)!=0 \
63158       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
63159
63160/*
63161** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
63162** P if required.
63163*/
63164#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
63165
63166/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63167#ifdef SQLITE_OMIT_MERGE_SORT
63168# define isSorter(x) 0
63169#else
63170# define isSorter(x) ((x)->pSorter!=0)
63171#endif
63172
63173/*
63174** Argument pMem points at a register that will be passed to a
63175** user-defined function or returned to the user as the result of a query.
63176** This routine sets the pMem->type variable used by the sqlite3_value_*()
63177** routines.
63178*/
63179SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
63180  int flags = pMem->flags;
63181  if( flags & MEM_Null ){
63182    pMem->type = SQLITE_NULL;
63183  }
63184  else if( flags & MEM_Int ){
63185    pMem->type = SQLITE_INTEGER;
63186  }
63187  else if( flags & MEM_Real ){
63188    pMem->type = SQLITE_FLOAT;
63189  }
63190  else if( flags & MEM_Str ){
63191    pMem->type = SQLITE_TEXT;
63192  }else{
63193    pMem->type = SQLITE_BLOB;
63194  }
63195}
63196
63197/*
63198** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
63199** if we run out of memory.
63200*/
63201static VdbeCursor *allocateCursor(
63202  Vdbe *p,              /* The virtual machine */
63203  int iCur,             /* Index of the new VdbeCursor */
63204  int nField,           /* Number of fields in the table or index */
63205  int iDb,              /* When database the cursor belongs to, or -1 */
63206  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
63207){
63208  /* Find the memory cell that will be used to store the blob of memory
63209  ** required for this VdbeCursor structure. It is convenient to use a
63210  ** vdbe memory cell to manage the memory allocation required for a
63211  ** VdbeCursor structure for the following reasons:
63212  **
63213  **   * Sometimes cursor numbers are used for a couple of different
63214  **     purposes in a vdbe program. The different uses might require
63215  **     different sized allocations. Memory cells provide growable
63216  **     allocations.
63217  **
63218  **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
63219  **     be freed lazily via the sqlite3_release_memory() API. This
63220  **     minimizes the number of malloc calls made by the system.
63221  **
63222  ** Memory cells for cursors are allocated at the top of the address
63223  ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
63224  ** cursor 1 is managed by memory cell (p->nMem-1), etc.
63225  */
63226  Mem *pMem = &p->aMem[p->nMem-iCur];
63227
63228  int nByte;
63229  VdbeCursor *pCx = 0;
63230  nByte =
63231      ROUND8(sizeof(VdbeCursor)) +
63232      (isBtreeCursor?sqlite3BtreeCursorSize():0) +
63233      2*nField*sizeof(u32);
63234
63235  assert( iCur<p->nCursor );
63236  if( p->apCsr[iCur] ){
63237    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
63238    p->apCsr[iCur] = 0;
63239  }
63240  if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
63241    p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
63242    memset(pCx, 0, sizeof(VdbeCursor));
63243    pCx->iDb = iDb;
63244    pCx->nField = nField;
63245    if( nField ){
63246      pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
63247    }
63248    if( isBtreeCursor ){
63249      pCx->pCursor = (BtCursor*)
63250          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
63251      sqlite3BtreeCursorZero(pCx->pCursor);
63252    }
63253  }
63254  return pCx;
63255}
63256
63257/*
63258** Try to convert a value into a numeric representation if we can
63259** do so without loss of information.  In other words, if the string
63260** looks like a number, convert it into a number.  If it does not
63261** look like a number, leave it alone.
63262*/
63263static void applyNumericAffinity(Mem *pRec){
63264  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
63265    double rValue;
63266    i64 iValue;
63267    u8 enc = pRec->enc;
63268    if( (pRec->flags&MEM_Str)==0 ) return;
63269    if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
63270    if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
63271      pRec->u.i = iValue;
63272      pRec->flags |= MEM_Int;
63273    }else{
63274      pRec->r = rValue;
63275      pRec->flags |= MEM_Real;
63276    }
63277  }
63278}
63279
63280/*
63281** Processing is determine by the affinity parameter:
63282**
63283** SQLITE_AFF_INTEGER:
63284** SQLITE_AFF_REAL:
63285** SQLITE_AFF_NUMERIC:
63286**    Try to convert pRec to an integer representation or a
63287**    floating-point representation if an integer representation
63288**    is not possible.  Note that the integer representation is
63289**    always preferred, even if the affinity is REAL, because
63290**    an integer representation is more space efficient on disk.
63291**
63292** SQLITE_AFF_TEXT:
63293**    Convert pRec to a text representation.
63294**
63295** SQLITE_AFF_NONE:
63296**    No-op.  pRec is unchanged.
63297*/
63298static void applyAffinity(
63299  Mem *pRec,          /* The value to apply affinity to */
63300  char affinity,      /* The affinity to be applied */
63301  u8 enc              /* Use this text encoding */
63302){
63303  if( affinity==SQLITE_AFF_TEXT ){
63304    /* Only attempt the conversion to TEXT if there is an integer or real
63305    ** representation (blob and NULL do not get converted) but no string
63306    ** representation.
63307    */
63308    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
63309      sqlite3VdbeMemStringify(pRec, enc);
63310    }
63311    pRec->flags &= ~(MEM_Real|MEM_Int);
63312  }else if( affinity!=SQLITE_AFF_NONE ){
63313    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
63314             || affinity==SQLITE_AFF_NUMERIC );
63315    applyNumericAffinity(pRec);
63316    if( pRec->flags & MEM_Real ){
63317      sqlite3VdbeIntegerAffinity(pRec);
63318    }
63319  }
63320}
63321
63322/*
63323** Try to convert the type of a function argument or a result column
63324** into a numeric representation.  Use either INTEGER or REAL whichever
63325** is appropriate.  But only do the conversion if it is possible without
63326** loss of information and return the revised type of the argument.
63327*/
63328SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
63329  Mem *pMem = (Mem*)pVal;
63330  if( pMem->type==SQLITE_TEXT ){
63331    applyNumericAffinity(pMem);
63332    sqlite3VdbeMemStoreType(pMem);
63333  }
63334  return pMem->type;
63335}
63336
63337/*
63338** Exported version of applyAffinity(). This one works on sqlite3_value*,
63339** not the internal Mem* type.
63340*/
63341SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
63342  sqlite3_value *pVal,
63343  u8 affinity,
63344  u8 enc
63345){
63346  applyAffinity((Mem *)pVal, affinity, enc);
63347}
63348
63349#ifdef SQLITE_DEBUG
63350/*
63351** Write a nice string representation of the contents of cell pMem
63352** into buffer zBuf, length nBuf.
63353*/
63354SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
63355  char *zCsr = zBuf;
63356  int f = pMem->flags;
63357
63358  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
63359
63360  if( f&MEM_Blob ){
63361    int i;
63362    char c;
63363    if( f & MEM_Dyn ){
63364      c = 'z';
63365      assert( (f & (MEM_Static|MEM_Ephem))==0 );
63366    }else if( f & MEM_Static ){
63367      c = 't';
63368      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
63369    }else if( f & MEM_Ephem ){
63370      c = 'e';
63371      assert( (f & (MEM_Static|MEM_Dyn))==0 );
63372    }else{
63373      c = 's';
63374    }
63375
63376    sqlite3_snprintf(100, zCsr, "%c", c);
63377    zCsr += sqlite3Strlen30(zCsr);
63378    sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
63379    zCsr += sqlite3Strlen30(zCsr);
63380    for(i=0; i<16 && i<pMem->n; i++){
63381      sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
63382      zCsr += sqlite3Strlen30(zCsr);
63383    }
63384    for(i=0; i<16 && i<pMem->n; i++){
63385      char z = pMem->z[i];
63386      if( z<32 || z>126 ) *zCsr++ = '.';
63387      else *zCsr++ = z;
63388    }
63389
63390    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
63391    zCsr += sqlite3Strlen30(zCsr);
63392    if( f & MEM_Zero ){
63393      sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
63394      zCsr += sqlite3Strlen30(zCsr);
63395    }
63396    *zCsr = '\0';
63397  }else if( f & MEM_Str ){
63398    int j, k;
63399    zBuf[0] = ' ';
63400    if( f & MEM_Dyn ){
63401      zBuf[1] = 'z';
63402      assert( (f & (MEM_Static|MEM_Ephem))==0 );
63403    }else if( f & MEM_Static ){
63404      zBuf[1] = 't';
63405      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
63406    }else if( f & MEM_Ephem ){
63407      zBuf[1] = 'e';
63408      assert( (f & (MEM_Static|MEM_Dyn))==0 );
63409    }else{
63410      zBuf[1] = 's';
63411    }
63412    k = 2;
63413    sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
63414    k += sqlite3Strlen30(&zBuf[k]);
63415    zBuf[k++] = '[';
63416    for(j=0; j<15 && j<pMem->n; j++){
63417      u8 c = pMem->z[j];
63418      if( c>=0x20 && c<0x7f ){
63419        zBuf[k++] = c;
63420      }else{
63421        zBuf[k++] = '.';
63422      }
63423    }
63424    zBuf[k++] = ']';
63425    sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
63426    k += sqlite3Strlen30(&zBuf[k]);
63427    zBuf[k++] = 0;
63428  }
63429}
63430#endif
63431
63432#ifdef SQLITE_DEBUG
63433/*
63434** Print the value of a register for tracing purposes:
63435*/
63436static void memTracePrint(FILE *out, Mem *p){
63437  if( p->flags & MEM_Null ){
63438    fprintf(out, " NULL");
63439  }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
63440    fprintf(out, " si:%lld", p->u.i);
63441  }else if( p->flags & MEM_Int ){
63442    fprintf(out, " i:%lld", p->u.i);
63443#ifndef SQLITE_OMIT_FLOATING_POINT
63444  }else if( p->flags & MEM_Real ){
63445    fprintf(out, " r:%g", p->r);
63446#endif
63447  }else if( p->flags & MEM_RowSet ){
63448    fprintf(out, " (rowset)");
63449  }else{
63450    char zBuf[200];
63451    sqlite3VdbeMemPrettyPrint(p, zBuf);
63452    fprintf(out, " ");
63453    fprintf(out, "%s", zBuf);
63454  }
63455}
63456static void registerTrace(FILE *out, int iReg, Mem *p){
63457  fprintf(out, "REG[%d] = ", iReg);
63458  memTracePrint(out, p);
63459  fprintf(out, "\n");
63460}
63461#endif
63462
63463#ifdef SQLITE_DEBUG
63464#  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
63465#else
63466#  define REGISTER_TRACE(R,M)
63467#endif
63468
63469
63470#ifdef VDBE_PROFILE
63471
63472/*
63473** hwtime.h contains inline assembler code for implementing
63474** high-performance timing routines.
63475*/
63476/************** Include hwtime.h in the middle of vdbe.c *********************/
63477/************** Begin file hwtime.h ******************************************/
63478/*
63479** 2008 May 27
63480**
63481** The author disclaims copyright to this source code.  In place of
63482** a legal notice, here is a blessing:
63483**
63484**    May you do good and not evil.
63485**    May you find forgiveness for yourself and forgive others.
63486**    May you share freely, never taking more than you give.
63487**
63488******************************************************************************
63489**
63490** This file contains inline asm code for retrieving "high-performance"
63491** counters for x86 class CPUs.
63492*/
63493#ifndef _HWTIME_H_
63494#define _HWTIME_H_
63495
63496/*
63497** The following routine only works on pentium-class (or newer) processors.
63498** It uses the RDTSC opcode to read the cycle count value out of the
63499** processor and returns that value.  This can be used for high-res
63500** profiling.
63501*/
63502#if (defined(__GNUC__) || defined(_MSC_VER)) && \
63503      (defined(i386) || defined(__i386__) || defined(_M_IX86))
63504
63505  #if defined(__GNUC__)
63506
63507  __inline__ sqlite_uint64 sqlite3Hwtime(void){
63508     unsigned int lo, hi;
63509     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
63510     return (sqlite_uint64)hi << 32 | lo;
63511  }
63512
63513  #elif defined(_MSC_VER)
63514
63515  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
63516     __asm {
63517        rdtsc
63518        ret       ; return value at EDX:EAX
63519     }
63520  }
63521
63522  #endif
63523
63524#elif (defined(__GNUC__) && defined(__x86_64__))
63525
63526  __inline__ sqlite_uint64 sqlite3Hwtime(void){
63527      unsigned long val;
63528      __asm__ __volatile__ ("rdtsc" : "=A" (val));
63529      return val;
63530  }
63531
63532#elif (defined(__GNUC__) && defined(__ppc__))
63533
63534  __inline__ sqlite_uint64 sqlite3Hwtime(void){
63535      unsigned long long retval;
63536      unsigned long junk;
63537      __asm__ __volatile__ ("\n\
63538          1:      mftbu   %1\n\
63539                  mftb    %L0\n\
63540                  mftbu   %0\n\
63541                  cmpw    %0,%1\n\
63542                  bne     1b"
63543                  : "=r" (retval), "=r" (junk));
63544      return retval;
63545  }
63546
63547#else
63548
63549  #error Need implementation of sqlite3Hwtime() for your platform.
63550
63551  /*
63552  ** To compile without implementing sqlite3Hwtime() for your platform,
63553  ** you can remove the above #error and use the following
63554  ** stub function.  You will lose timing support for many
63555  ** of the debugging and testing utilities, but it should at
63556  ** least compile and run.
63557  */
63558SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
63559
63560#endif
63561
63562#endif /* !defined(_HWTIME_H_) */
63563
63564/************** End of hwtime.h **********************************************/
63565/************** Continuing where we left off in vdbe.c ***********************/
63566
63567#endif
63568
63569/*
63570** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
63571** sqlite3_interrupt() routine has been called.  If it has been, then
63572** processing of the VDBE program is interrupted.
63573**
63574** This macro added to every instruction that does a jump in order to
63575** implement a loop.  This test used to be on every single instruction,
63576** but that meant we more testing that we needed.  By only testing the
63577** flag on jump instructions, we get a (small) speed improvement.
63578*/
63579#define CHECK_FOR_INTERRUPT \
63580   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
63581
63582
63583#ifndef NDEBUG
63584/*
63585** This function is only called from within an assert() expression. It
63586** checks that the sqlite3.nTransaction variable is correctly set to
63587** the number of non-transaction savepoints currently in the
63588** linked list starting at sqlite3.pSavepoint.
63589**
63590** Usage:
63591**
63592**     assert( checkSavepointCount(db) );
63593*/
63594static int checkSavepointCount(sqlite3 *db){
63595  int n = 0;
63596  Savepoint *p;
63597  for(p=db->pSavepoint; p; p=p->pNext) n++;
63598  assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
63599  return 1;
63600}
63601#endif
63602
63603/*
63604** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
63605** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
63606** in memory obtained from sqlite3DbMalloc).
63607*/
63608static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
63609  sqlite3 *db = p->db;
63610  sqlite3DbFree(db, p->zErrMsg);
63611  p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
63612  sqlite3_free(pVtab->zErrMsg);
63613  pVtab->zErrMsg = 0;
63614}
63615
63616
63617/*
63618** Execute as much of a VDBE program as we can then return.
63619**
63620** sqlite3VdbeMakeReady() must be called before this routine in order to
63621** close the program with a final OP_Halt and to set up the callbacks
63622** and the error message pointer.
63623**
63624** Whenever a row or result data is available, this routine will either
63625** invoke the result callback (if there is one) or return with
63626** SQLITE_ROW.
63627**
63628** If an attempt is made to open a locked database, then this routine
63629** will either invoke the busy callback (if there is one) or it will
63630** return SQLITE_BUSY.
63631**
63632** If an error occurs, an error message is written to memory obtained
63633** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
63634** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
63635**
63636** If the callback ever returns non-zero, then the program exits
63637** immediately.  There will be no error message but the p->rc field is
63638** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
63639**
63640** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
63641** routine to return SQLITE_ERROR.
63642**
63643** Other fatal errors return SQLITE_ERROR.
63644**
63645** After this routine has finished, sqlite3VdbeFinalize() should be
63646** used to clean up the mess that was left behind.
63647*/
63648SQLITE_PRIVATE int sqlite3VdbeExec(
63649  Vdbe *p                    /* The VDBE */
63650){
63651  int pc=0;                  /* The program counter */
63652  Op *aOp = p->aOp;          /* Copy of p->aOp */
63653  Op *pOp;                   /* Current operation */
63654  int rc = SQLITE_OK;        /* Value to return */
63655  sqlite3 *db = p->db;       /* The database */
63656  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
63657  u8 encoding = ENC(db);     /* The database encoding */
63658#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
63659  int checkProgress;         /* True if progress callbacks are enabled */
63660  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
63661#endif
63662  Mem *aMem = p->aMem;       /* Copy of p->aMem */
63663  Mem *pIn1 = 0;             /* 1st input operand */
63664  Mem *pIn2 = 0;             /* 2nd input operand */
63665  Mem *pIn3 = 0;             /* 3rd input operand */
63666  Mem *pOut = 0;             /* Output operand */
63667  int iCompare = 0;          /* Result of last OP_Compare operation */
63668  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
63669  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
63670#ifdef VDBE_PROFILE
63671  u64 start;                 /* CPU clock count at start of opcode */
63672  int origPc;                /* Program counter at start of opcode */
63673#endif
63674  /********************************************************************
63675  ** Automatically generated code
63676  **
63677  ** The following union is automatically generated by the
63678  ** vdbe-compress.tcl script.  The purpose of this union is to
63679  ** reduce the amount of stack space required by this function.
63680  ** See comments in the vdbe-compress.tcl script for details.
63681  */
63682  union vdbeExecUnion {
63683    struct OP_Yield_stack_vars {
63684      int pcDest;
63685    } aa;
63686    struct OP_Variable_stack_vars {
63687      Mem *pVar;       /* Value being transferred */
63688    } ab;
63689    struct OP_Move_stack_vars {
63690      char *zMalloc;   /* Holding variable for allocated memory */
63691      int n;           /* Number of registers left to copy */
63692      int p1;          /* Register to copy from */
63693      int p2;          /* Register to copy to */
63694    } ac;
63695    struct OP_ResultRow_stack_vars {
63696      Mem *pMem;
63697      int i;
63698    } ad;
63699    struct OP_Concat_stack_vars {
63700      i64 nByte;
63701    } ae;
63702    struct OP_Remainder_stack_vars {
63703      int flags;      /* Combined MEM_* flags from both inputs */
63704      i64 iA;         /* Integer value of left operand */
63705      i64 iB;         /* Integer value of right operand */
63706      double rA;      /* Real value of left operand */
63707      double rB;      /* Real value of right operand */
63708    } af;
63709    struct OP_Function_stack_vars {
63710      int i;
63711      Mem *pArg;
63712      sqlite3_context ctx;
63713      sqlite3_value **apVal;
63714      int n;
63715    } ag;
63716    struct OP_ShiftRight_stack_vars {
63717      i64 iA;
63718      u64 uA;
63719      i64 iB;
63720      u8 op;
63721    } ah;
63722    struct OP_Ge_stack_vars {
63723      int res;            /* Result of the comparison of pIn1 against pIn3 */
63724      char affinity;      /* Affinity to use for comparison */
63725      u16 flags1;         /* Copy of initial value of pIn1->flags */
63726      u16 flags3;         /* Copy of initial value of pIn3->flags */
63727    } ai;
63728    struct OP_Compare_stack_vars {
63729      int n;
63730      int i;
63731      int p1;
63732      int p2;
63733      const KeyInfo *pKeyInfo;
63734      int idx;
63735      CollSeq *pColl;    /* Collating sequence to use on this term */
63736      int bRev;          /* True for DESCENDING sort order */
63737    } aj;
63738    struct OP_Or_stack_vars {
63739      int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63740      int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63741    } ak;
63742    struct OP_IfNot_stack_vars {
63743      int c;
63744    } al;
63745    struct OP_Column_stack_vars {
63746      u32 payloadSize;   /* Number of bytes in the record */
63747      i64 payloadSize64; /* Number of bytes in the record */
63748      int p1;            /* P1 value of the opcode */
63749      int p2;            /* column number to retrieve */
63750      VdbeCursor *pC;    /* The VDBE cursor */
63751      char *zRec;        /* Pointer to complete record-data */
63752      BtCursor *pCrsr;   /* The BTree cursor */
63753      u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
63754      u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
63755      int nField;        /* number of fields in the record */
63756      int len;           /* The length of the serialized data for the column */
63757      int i;             /* Loop counter */
63758      char *zData;       /* Part of the record being decoded */
63759      Mem *pDest;        /* Where to write the extracted value */
63760      Mem sMem;          /* For storing the record being decoded */
63761      u8 *zIdx;          /* Index into header */
63762      u8 *zEndHdr;       /* Pointer to first byte after the header */
63763      u32 offset;        /* Offset into the data */
63764      u32 szField;       /* Number of bytes in the content of a field */
63765      int szHdr;         /* Size of the header size field at start of record */
63766      int avail;         /* Number of bytes of available data */
63767      u32 t;             /* A type code from the record header */
63768      Mem *pReg;         /* PseudoTable input register */
63769    } am;
63770    struct OP_Affinity_stack_vars {
63771      const char *zAffinity;   /* The affinity to be applied */
63772      char cAff;               /* A single character of affinity */
63773    } an;
63774    struct OP_MakeRecord_stack_vars {
63775      u8 *zNewRecord;        /* A buffer to hold the data for the new record */
63776      Mem *pRec;             /* The new record */
63777      u64 nData;             /* Number of bytes of data space */
63778      int nHdr;              /* Number of bytes of header space */
63779      i64 nByte;             /* Data space required for this record */
63780      int nZero;             /* Number of zero bytes at the end of the record */
63781      int nVarint;           /* Number of bytes in a varint */
63782      u32 serial_type;       /* Type field */
63783      Mem *pData0;           /* First field to be combined into the record */
63784      Mem *pLast;            /* Last field of the record */
63785      int nField;            /* Number of fields in the record */
63786      char *zAffinity;       /* The affinity string for the record */
63787      int file_format;       /* File format to use for encoding */
63788      int i;                 /* Space used in zNewRecord[] */
63789      int len;               /* Length of a field */
63790    } ao;
63791    struct OP_Count_stack_vars {
63792      i64 nEntry;
63793      BtCursor *pCrsr;
63794    } ap;
63795    struct OP_Savepoint_stack_vars {
63796      int p1;                         /* Value of P1 operand */
63797      char *zName;                    /* Name of savepoint */
63798      int nName;
63799      Savepoint *pNew;
63800      Savepoint *pSavepoint;
63801      Savepoint *pTmp;
63802      int iSavepoint;
63803      int ii;
63804    } aq;
63805    struct OP_AutoCommit_stack_vars {
63806      int desiredAutoCommit;
63807      int iRollback;
63808      int turnOnAC;
63809    } ar;
63810    struct OP_Transaction_stack_vars {
63811      Btree *pBt;
63812    } as;
63813    struct OP_ReadCookie_stack_vars {
63814      int iMeta;
63815      int iDb;
63816      int iCookie;
63817    } at;
63818    struct OP_SetCookie_stack_vars {
63819      Db *pDb;
63820    } au;
63821    struct OP_VerifyCookie_stack_vars {
63822      int iMeta;
63823      int iGen;
63824      Btree *pBt;
63825    } av;
63826    struct OP_OpenWrite_stack_vars {
63827      int nField;
63828      KeyInfo *pKeyInfo;
63829      int p2;
63830      int iDb;
63831      int wrFlag;
63832      Btree *pX;
63833      VdbeCursor *pCur;
63834      Db *pDb;
63835    } aw;
63836    struct OP_OpenEphemeral_stack_vars {
63837      VdbeCursor *pCx;
63838    } ax;
63839    struct OP_SorterOpen_stack_vars {
63840      VdbeCursor *pCx;
63841    } ay;
63842    struct OP_OpenPseudo_stack_vars {
63843      VdbeCursor *pCx;
63844    } az;
63845    struct OP_SeekGt_stack_vars {
63846      int res;
63847      int oc;
63848      VdbeCursor *pC;
63849      UnpackedRecord r;
63850      int nField;
63851      i64 iKey;      /* The rowid we are to seek to */
63852    } ba;
63853    struct OP_Seek_stack_vars {
63854      VdbeCursor *pC;
63855    } bb;
63856    struct OP_Found_stack_vars {
63857      int alreadyExists;
63858      VdbeCursor *pC;
63859      int res;
63860      char *pFree;
63861      UnpackedRecord *pIdxKey;
63862      UnpackedRecord r;
63863      char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63864    } bc;
63865    struct OP_IsUnique_stack_vars {
63866      u16 ii;
63867      VdbeCursor *pCx;
63868      BtCursor *pCrsr;
63869      u16 nField;
63870      Mem *aMx;
63871      UnpackedRecord r;                  /* B-Tree index search key */
63872      i64 R;                             /* Rowid stored in register P3 */
63873    } bd;
63874    struct OP_NotExists_stack_vars {
63875      VdbeCursor *pC;
63876      BtCursor *pCrsr;
63877      int res;
63878      u64 iKey;
63879    } be;
63880    struct OP_NewRowid_stack_vars {
63881      i64 v;                 /* The new rowid */
63882      VdbeCursor *pC;        /* Cursor of table to get the new rowid */
63883      int res;               /* Result of an sqlite3BtreeLast() */
63884      int cnt;               /* Counter to limit the number of searches */
63885      Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
63886      VdbeFrame *pFrame;     /* Root frame of VDBE */
63887    } bf;
63888    struct OP_InsertInt_stack_vars {
63889      Mem *pData;       /* MEM cell holding data for the record to be inserted */
63890      Mem *pKey;        /* MEM cell holding key  for the record */
63891      i64 iKey;         /* The integer ROWID or key for the record to be inserted */
63892      VdbeCursor *pC;   /* Cursor to table into which insert is written */
63893      int nZero;        /* Number of zero-bytes to append */
63894      int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
63895      const char *zDb;  /* database name - used by the update hook */
63896      const char *zTbl; /* Table name - used by the opdate hook */
63897      int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63898    } bg;
63899    struct OP_Delete_stack_vars {
63900      i64 iKey;
63901      VdbeCursor *pC;
63902    } bh;
63903    struct OP_SorterCompare_stack_vars {
63904      VdbeCursor *pC;
63905      int res;
63906    } bi;
63907    struct OP_SorterData_stack_vars {
63908      VdbeCursor *pC;
63909    } bj;
63910    struct OP_RowData_stack_vars {
63911      VdbeCursor *pC;
63912      BtCursor *pCrsr;
63913      u32 n;
63914      i64 n64;
63915    } bk;
63916    struct OP_Rowid_stack_vars {
63917      VdbeCursor *pC;
63918      i64 v;
63919      sqlite3_vtab *pVtab;
63920      const sqlite3_module *pModule;
63921    } bl;
63922    struct OP_NullRow_stack_vars {
63923      VdbeCursor *pC;
63924    } bm;
63925    struct OP_Last_stack_vars {
63926      VdbeCursor *pC;
63927      BtCursor *pCrsr;
63928      int res;
63929    } bn;
63930    struct OP_Rewind_stack_vars {
63931      VdbeCursor *pC;
63932      BtCursor *pCrsr;
63933      int res;
63934    } bo;
63935    struct OP_Next_stack_vars {
63936      VdbeCursor *pC;
63937      int res;
63938    } bp;
63939    struct OP_IdxInsert_stack_vars {
63940      VdbeCursor *pC;
63941      BtCursor *pCrsr;
63942      int nKey;
63943      const char *zKey;
63944    } bq;
63945    struct OP_IdxDelete_stack_vars {
63946      VdbeCursor *pC;
63947      BtCursor *pCrsr;
63948      int res;
63949      UnpackedRecord r;
63950    } br;
63951    struct OP_IdxRowid_stack_vars {
63952      BtCursor *pCrsr;
63953      VdbeCursor *pC;
63954      i64 rowid;
63955    } bs;
63956    struct OP_IdxGE_stack_vars {
63957      VdbeCursor *pC;
63958      int res;
63959      UnpackedRecord r;
63960    } bt;
63961    struct OP_Destroy_stack_vars {
63962      int iMoved;
63963      int iCnt;
63964      Vdbe *pVdbe;
63965      int iDb;
63966    } bu;
63967    struct OP_Clear_stack_vars {
63968      int nChange;
63969    } bv;
63970    struct OP_CreateTable_stack_vars {
63971      int pgno;
63972      int flags;
63973      Db *pDb;
63974    } bw;
63975    struct OP_ParseSchema_stack_vars {
63976      int iDb;
63977      const char *zMaster;
63978      char *zSql;
63979      InitData initData;
63980    } bx;
63981    struct OP_IntegrityCk_stack_vars {
63982      int nRoot;      /* Number of tables to check.  (Number of root pages.) */
63983      int *aRoot;     /* Array of rootpage numbers for tables to be checked */
63984      int j;          /* Loop counter */
63985      int nErr;       /* Number of errors reported */
63986      char *z;        /* Text of the error report */
63987      Mem *pnErr;     /* Register keeping track of errors remaining */
63988    } by;
63989    struct OP_RowSetRead_stack_vars {
63990      i64 val;
63991    } bz;
63992    struct OP_RowSetTest_stack_vars {
63993      int iSet;
63994      int exists;
63995    } ca;
63996    struct OP_Program_stack_vars {
63997      int nMem;               /* Number of memory registers for sub-program */
63998      int nByte;              /* Bytes of runtime space required for sub-program */
63999      Mem *pRt;               /* Register to allocate runtime space */
64000      Mem *pMem;              /* Used to iterate through memory cells */
64001      Mem *pEnd;              /* Last memory cell in new array */
64002      VdbeFrame *pFrame;      /* New vdbe frame to execute in */
64003      SubProgram *pProgram;   /* Sub-program to execute */
64004      void *t;                /* Token identifying trigger */
64005    } cb;
64006    struct OP_Param_stack_vars {
64007      VdbeFrame *pFrame;
64008      Mem *pIn;
64009    } cc;
64010    struct OP_MemMax_stack_vars {
64011      Mem *pIn1;
64012      VdbeFrame *pFrame;
64013    } cd;
64014    struct OP_AggStep_stack_vars {
64015      int n;
64016      int i;
64017      Mem *pMem;
64018      Mem *pRec;
64019      sqlite3_context ctx;
64020      sqlite3_value **apVal;
64021    } ce;
64022    struct OP_AggFinal_stack_vars {
64023      Mem *pMem;
64024    } cf;
64025    struct OP_Checkpoint_stack_vars {
64026      int i;                          /* Loop counter */
64027      int aRes[3];                    /* Results */
64028      Mem *pMem;                      /* Write results here */
64029    } cg;
64030    struct OP_JournalMode_stack_vars {
64031      Btree *pBt;                     /* Btree to change journal mode of */
64032      Pager *pPager;                  /* Pager associated with pBt */
64033      int eNew;                       /* New journal mode */
64034      int eOld;                       /* The old journal mode */
64035      const char *zFilename;          /* Name of database file for pPager */
64036    } ch;
64037    struct OP_IncrVacuum_stack_vars {
64038      Btree *pBt;
64039    } ci;
64040    struct OP_VBegin_stack_vars {
64041      VTable *pVTab;
64042    } cj;
64043    struct OP_VOpen_stack_vars {
64044      VdbeCursor *pCur;
64045      sqlite3_vtab_cursor *pVtabCursor;
64046      sqlite3_vtab *pVtab;
64047      sqlite3_module *pModule;
64048    } ck;
64049    struct OP_VFilter_stack_vars {
64050      int nArg;
64051      int iQuery;
64052      const sqlite3_module *pModule;
64053      Mem *pQuery;
64054      Mem *pArgc;
64055      sqlite3_vtab_cursor *pVtabCursor;
64056      sqlite3_vtab *pVtab;
64057      VdbeCursor *pCur;
64058      int res;
64059      int i;
64060      Mem **apArg;
64061    } cl;
64062    struct OP_VColumn_stack_vars {
64063      sqlite3_vtab *pVtab;
64064      const sqlite3_module *pModule;
64065      Mem *pDest;
64066      sqlite3_context sContext;
64067    } cm;
64068    struct OP_VNext_stack_vars {
64069      sqlite3_vtab *pVtab;
64070      const sqlite3_module *pModule;
64071      int res;
64072      VdbeCursor *pCur;
64073    } cn;
64074    struct OP_VRename_stack_vars {
64075      sqlite3_vtab *pVtab;
64076      Mem *pName;
64077    } co;
64078    struct OP_VUpdate_stack_vars {
64079      sqlite3_vtab *pVtab;
64080      sqlite3_module *pModule;
64081      int nArg;
64082      int i;
64083      sqlite_int64 rowid;
64084      Mem **apArg;
64085      Mem *pX;
64086    } cp;
64087    struct OP_Trace_stack_vars {
64088      char *zTrace;
64089      char *z;
64090    } cq;
64091  } u;
64092  /* End automatically generated code
64093  ********************************************************************/
64094
64095  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
64096  sqlite3VdbeEnter(p);
64097  if( p->rc==SQLITE_NOMEM ){
64098    /* This happens if a malloc() inside a call to sqlite3_column_text() or
64099    ** sqlite3_column_text16() failed.  */
64100    goto no_mem;
64101  }
64102  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
64103  p->rc = SQLITE_OK;
64104  assert( p->explain==0 );
64105  p->pResultSet = 0;
64106  db->busyHandler.nBusy = 0;
64107  CHECK_FOR_INTERRUPT;
64108  sqlite3VdbeIOTraceSql(p);
64109#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
64110  checkProgress = db->xProgress!=0;
64111#endif
64112#ifdef SQLITE_DEBUG
64113  sqlite3BeginBenignMalloc();
64114  if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
64115    int i;
64116    printf("VDBE Program Listing:\n");
64117    sqlite3VdbePrintSql(p);
64118    for(i=0; i<p->nOp; i++){
64119      sqlite3VdbePrintOp(stdout, i, &aOp[i]);
64120    }
64121  }
64122  sqlite3EndBenignMalloc();
64123#endif
64124  for(pc=p->pc; rc==SQLITE_OK; pc++){
64125    assert( pc>=0 && pc<p->nOp );
64126    if( db->mallocFailed ) goto no_mem;
64127#ifdef VDBE_PROFILE
64128    origPc = pc;
64129    start = sqlite3Hwtime();
64130#endif
64131    pOp = &aOp[pc];
64132
64133    /* Only allow tracing if SQLITE_DEBUG is defined.
64134    */
64135#ifdef SQLITE_DEBUG
64136    if( p->trace ){
64137      if( pc==0 ){
64138        printf("VDBE Execution Trace:\n");
64139        sqlite3VdbePrintSql(p);
64140      }
64141      sqlite3VdbePrintOp(p->trace, pc, pOp);
64142    }
64143#endif
64144
64145
64146    /* Check to see if we need to simulate an interrupt.  This only happens
64147    ** if we have a special test build.
64148    */
64149#ifdef SQLITE_TEST
64150    if( sqlite3_interrupt_count>0 ){
64151      sqlite3_interrupt_count--;
64152      if( sqlite3_interrupt_count==0 ){
64153        sqlite3_interrupt(db);
64154      }
64155    }
64156#endif
64157
64158#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
64159    /* Call the progress callback if it is configured and the required number
64160    ** of VDBE ops have been executed (either since this invocation of
64161    ** sqlite3VdbeExec() or since last time the progress callback was called).
64162    ** If the progress callback returns non-zero, exit the virtual machine with
64163    ** a return code SQLITE_ABORT.
64164    */
64165    if( checkProgress ){
64166      if( db->nProgressOps==nProgressOps ){
64167        int prc;
64168        prc = db->xProgress(db->pProgressArg);
64169        if( prc!=0 ){
64170          rc = SQLITE_INTERRUPT;
64171          goto vdbe_error_halt;
64172        }
64173        nProgressOps = 0;
64174      }
64175      nProgressOps++;
64176    }
64177#endif
64178
64179    /* On any opcode with the "out2-prerelase" tag, free any
64180    ** external allocations out of mem[p2] and set mem[p2] to be
64181    ** an undefined integer.  Opcodes will either fill in the integer
64182    ** value or convert mem[p2] to a different type.
64183    */
64184    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
64185    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
64186      assert( pOp->p2>0 );
64187      assert( pOp->p2<=p->nMem );
64188      pOut = &aMem[pOp->p2];
64189      memAboutToChange(p, pOut);
64190      MemReleaseExt(pOut);
64191      pOut->flags = MEM_Int;
64192    }
64193
64194    /* Sanity checking on other operands */
64195#ifdef SQLITE_DEBUG
64196    if( (pOp->opflags & OPFLG_IN1)!=0 ){
64197      assert( pOp->p1>0 );
64198      assert( pOp->p1<=p->nMem );
64199      assert( memIsValid(&aMem[pOp->p1]) );
64200      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
64201    }
64202    if( (pOp->opflags & OPFLG_IN2)!=0 ){
64203      assert( pOp->p2>0 );
64204      assert( pOp->p2<=p->nMem );
64205      assert( memIsValid(&aMem[pOp->p2]) );
64206      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
64207    }
64208    if( (pOp->opflags & OPFLG_IN3)!=0 ){
64209      assert( pOp->p3>0 );
64210      assert( pOp->p3<=p->nMem );
64211      assert( memIsValid(&aMem[pOp->p3]) );
64212      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
64213    }
64214    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
64215      assert( pOp->p2>0 );
64216      assert( pOp->p2<=p->nMem );
64217      memAboutToChange(p, &aMem[pOp->p2]);
64218    }
64219    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
64220      assert( pOp->p3>0 );
64221      assert( pOp->p3<=p->nMem );
64222      memAboutToChange(p, &aMem[pOp->p3]);
64223    }
64224#endif
64225
64226    switch( pOp->opcode ){
64227
64228/*****************************************************************************
64229** What follows is a massive switch statement where each case implements a
64230** separate instruction in the virtual machine.  If we follow the usual
64231** indentation conventions, each case should be indented by 6 spaces.  But
64232** that is a lot of wasted space on the left margin.  So the code within
64233** the switch statement will break with convention and be flush-left. Another
64234** big comment (similar to this one) will mark the point in the code where
64235** we transition back to normal indentation.
64236**
64237** The formatting of each case is important.  The makefile for SQLite
64238** generates two C files "opcodes.h" and "opcodes.c" by scanning this
64239** file looking for lines that begin with "case OP_".  The opcodes.h files
64240** will be filled with #defines that give unique integer values to each
64241** opcode and the opcodes.c file is filled with an array of strings where
64242** each string is the symbolic name for the corresponding opcode.  If the
64243** case statement is followed by a comment of the form "/# same as ... #/"
64244** that comment is used to determine the particular value of the opcode.
64245**
64246** Other keywords in the comment that follows each case are used to
64247** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
64248** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
64249** the mkopcodeh.awk script for additional information.
64250**
64251** Documentation about VDBE opcodes is generated by scanning this file
64252** for lines of that contain "Opcode:".  That line and all subsequent
64253** comment lines are used in the generation of the opcode.html documentation
64254** file.
64255**
64256** SUMMARY:
64257**
64258**     Formatting is important to scripts that scan this file.
64259**     Do not deviate from the formatting style currently in use.
64260**
64261*****************************************************************************/
64262
64263/* Opcode:  Goto * P2 * * *
64264**
64265** An unconditional jump to address P2.
64266** The next instruction executed will be
64267** the one at index P2 from the beginning of
64268** the program.
64269*/
64270case OP_Goto: {             /* jump */
64271  CHECK_FOR_INTERRUPT;
64272  pc = pOp->p2 - 1;
64273  break;
64274}
64275
64276/* Opcode:  Gosub P1 P2 * * *
64277**
64278** Write the current address onto register P1
64279** and then jump to address P2.
64280*/
64281case OP_Gosub: {            /* jump, in1 */
64282  pIn1 = &aMem[pOp->p1];
64283  assert( (pIn1->flags & MEM_Dyn)==0 );
64284  memAboutToChange(p, pIn1);
64285  pIn1->flags = MEM_Int;
64286  pIn1->u.i = pc;
64287  REGISTER_TRACE(pOp->p1, pIn1);
64288  pc = pOp->p2 - 1;
64289  break;
64290}
64291
64292/* Opcode:  Return P1 * * * *
64293**
64294** Jump to the next instruction after the address in register P1.
64295*/
64296case OP_Return: {           /* in1 */
64297  pIn1 = &aMem[pOp->p1];
64298  assert( pIn1->flags & MEM_Int );
64299  pc = (int)pIn1->u.i;
64300  break;
64301}
64302
64303/* Opcode:  Yield P1 * * * *
64304**
64305** Swap the program counter with the value in register P1.
64306*/
64307case OP_Yield: {            /* in1 */
64308#if 0  /* local variables moved into u.aa */
64309  int pcDest;
64310#endif /* local variables moved into u.aa */
64311  pIn1 = &aMem[pOp->p1];
64312  assert( (pIn1->flags & MEM_Dyn)==0 );
64313  pIn1->flags = MEM_Int;
64314  u.aa.pcDest = (int)pIn1->u.i;
64315  pIn1->u.i = pc;
64316  REGISTER_TRACE(pOp->p1, pIn1);
64317  pc = u.aa.pcDest;
64318  break;
64319}
64320
64321/* Opcode:  HaltIfNull  P1 P2 P3 P4 *
64322**
64323** Check the value in register P3.  If it is NULL then Halt using
64324** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
64325** value in register P3 is not NULL, then this routine is a no-op.
64326*/
64327case OP_HaltIfNull: {      /* in3 */
64328  pIn3 = &aMem[pOp->p3];
64329  if( (pIn3->flags & MEM_Null)==0 ) break;
64330  /* Fall through into OP_Halt */
64331}
64332
64333/* Opcode:  Halt P1 P2 * P4 *
64334**
64335** Exit immediately.  All open cursors, etc are closed
64336** automatically.
64337**
64338** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
64339** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
64340** For errors, it can be some other value.  If P1!=0 then P2 will determine
64341** whether or not to rollback the current transaction.  Do not rollback
64342** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
64343** then back out all changes that have occurred during this execution of the
64344** VDBE, but do not rollback the transaction.
64345**
64346** If P4 is not null then it is an error message string.
64347**
64348** There is an implied "Halt 0 0 0" instruction inserted at the very end of
64349** every program.  So a jump past the last instruction of the program
64350** is the same as executing Halt.
64351*/
64352case OP_Halt: {
64353  if( pOp->p1==SQLITE_OK && p->pFrame ){
64354    /* Halt the sub-program. Return control to the parent frame. */
64355    VdbeFrame *pFrame = p->pFrame;
64356    p->pFrame = pFrame->pParent;
64357    p->nFrame--;
64358    sqlite3VdbeSetChanges(db, p->nChange);
64359    pc = sqlite3VdbeFrameRestore(pFrame);
64360    lastRowid = db->lastRowid;
64361    if( pOp->p2==OE_Ignore ){
64362      /* Instruction pc is the OP_Program that invoked the sub-program
64363      ** currently being halted. If the p2 instruction of this OP_Halt
64364      ** instruction is set to OE_Ignore, then the sub-program is throwing
64365      ** an IGNORE exception. In this case jump to the address specified
64366      ** as the p2 of the calling OP_Program.  */
64367      pc = p->aOp[pc].p2-1;
64368    }
64369    aOp = p->aOp;
64370    aMem = p->aMem;
64371    break;
64372  }
64373
64374  p->rc = pOp->p1;
64375  p->errorAction = (u8)pOp->p2;
64376  p->pc = pc;
64377  if( pOp->p4.z ){
64378    assert( p->rc!=SQLITE_OK );
64379    sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
64380    testcase( sqlite3GlobalConfig.xLog!=0 );
64381    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
64382  }else if( p->rc ){
64383    testcase( sqlite3GlobalConfig.xLog!=0 );
64384    sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
64385  }
64386  rc = sqlite3VdbeHalt(p);
64387  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
64388  if( rc==SQLITE_BUSY ){
64389    p->rc = rc = SQLITE_BUSY;
64390  }else{
64391    assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
64392    assert( rc==SQLITE_OK || db->nDeferredCons>0 );
64393    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
64394  }
64395  goto vdbe_return;
64396}
64397
64398/* Opcode: Integer P1 P2 * * *
64399**
64400** The 32-bit integer value P1 is written into register P2.
64401*/
64402case OP_Integer: {         /* out2-prerelease */
64403  pOut->u.i = pOp->p1;
64404  break;
64405}
64406
64407/* Opcode: Int64 * P2 * P4 *
64408**
64409** P4 is a pointer to a 64-bit integer value.
64410** Write that value into register P2.
64411*/
64412case OP_Int64: {           /* out2-prerelease */
64413  assert( pOp->p4.pI64!=0 );
64414  pOut->u.i = *pOp->p4.pI64;
64415  break;
64416}
64417
64418#ifndef SQLITE_OMIT_FLOATING_POINT
64419/* Opcode: Real * P2 * P4 *
64420**
64421** P4 is a pointer to a 64-bit floating point value.
64422** Write that value into register P2.
64423*/
64424case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
64425  pOut->flags = MEM_Real;
64426  assert( !sqlite3IsNaN(*pOp->p4.pReal) );
64427  pOut->r = *pOp->p4.pReal;
64428  break;
64429}
64430#endif
64431
64432/* Opcode: String8 * P2 * P4 *
64433**
64434** P4 points to a nul terminated UTF-8 string. This opcode is transformed
64435** into an OP_String before it is executed for the first time.
64436*/
64437case OP_String8: {         /* same as TK_STRING, out2-prerelease */
64438  assert( pOp->p4.z!=0 );
64439  pOp->opcode = OP_String;
64440  pOp->p1 = sqlite3Strlen30(pOp->p4.z);
64441
64442#ifndef SQLITE_OMIT_UTF16
64443  if( encoding!=SQLITE_UTF8 ){
64444    rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
64445    if( rc==SQLITE_TOOBIG ) goto too_big;
64446    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
64447    assert( pOut->zMalloc==pOut->z );
64448    assert( pOut->flags & MEM_Dyn );
64449    pOut->zMalloc = 0;
64450    pOut->flags |= MEM_Static;
64451    pOut->flags &= ~MEM_Dyn;
64452    if( pOp->p4type==P4_DYNAMIC ){
64453      sqlite3DbFree(db, pOp->p4.z);
64454    }
64455    pOp->p4type = P4_DYNAMIC;
64456    pOp->p4.z = pOut->z;
64457    pOp->p1 = pOut->n;
64458  }
64459#endif
64460  if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64461    goto too_big;
64462  }
64463  /* Fall through to the next case, OP_String */
64464}
64465
64466/* Opcode: String P1 P2 * P4 *
64467**
64468** The string value P4 of length P1 (bytes) is stored in register P2.
64469*/
64470case OP_String: {          /* out2-prerelease */
64471  assert( pOp->p4.z!=0 );
64472  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
64473  pOut->z = pOp->p4.z;
64474  pOut->n = pOp->p1;
64475  pOut->enc = encoding;
64476  UPDATE_MAX_BLOBSIZE(pOut);
64477  break;
64478}
64479
64480/* Opcode: Null * P2 * * *
64481**
64482** Write a NULL into register P2.
64483*/
64484case OP_Null: {           /* out2-prerelease */
64485  pOut->flags = MEM_Null;
64486  break;
64487}
64488
64489
64490/* Opcode: Blob P1 P2 * P4
64491**
64492** P4 points to a blob of data P1 bytes long.  Store this
64493** blob in register P2.
64494*/
64495case OP_Blob: {                /* out2-prerelease */
64496  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
64497  sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
64498  pOut->enc = encoding;
64499  UPDATE_MAX_BLOBSIZE(pOut);
64500  break;
64501}
64502
64503/* Opcode: Variable P1 P2 * P4 *
64504**
64505** Transfer the values of bound parameter P1 into register P2
64506**
64507** If the parameter is named, then its name appears in P4 and P3==1.
64508** The P4 value is used by sqlite3_bind_parameter_name().
64509*/
64510case OP_Variable: {            /* out2-prerelease */
64511#if 0  /* local variables moved into u.ab */
64512  Mem *pVar;       /* Value being transferred */
64513#endif /* local variables moved into u.ab */
64514
64515  assert( pOp->p1>0 && pOp->p1<=p->nVar );
64516  assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
64517  u.ab.pVar = &p->aVar[pOp->p1 - 1];
64518  if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
64519    goto too_big;
64520  }
64521  sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
64522  UPDATE_MAX_BLOBSIZE(pOut);
64523  break;
64524}
64525
64526/* Opcode: Move P1 P2 P3 * *
64527**
64528** Move the values in register P1..P1+P3-1 over into
64529** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
64530** left holding a NULL.  It is an error for register ranges
64531** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
64532*/
64533case OP_Move: {
64534#if 0  /* local variables moved into u.ac */
64535  char *zMalloc;   /* Holding variable for allocated memory */
64536  int n;           /* Number of registers left to copy */
64537  int p1;          /* Register to copy from */
64538  int p2;          /* Register to copy to */
64539#endif /* local variables moved into u.ac */
64540
64541  u.ac.n = pOp->p3;
64542  u.ac.p1 = pOp->p1;
64543  u.ac.p2 = pOp->p2;
64544  assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
64545  assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
64546
64547  pIn1 = &aMem[u.ac.p1];
64548  pOut = &aMem[u.ac.p2];
64549  while( u.ac.n-- ){
64550    assert( pOut<=&aMem[p->nMem] );
64551    assert( pIn1<=&aMem[p->nMem] );
64552    assert( memIsValid(pIn1) );
64553    memAboutToChange(p, pOut);
64554    u.ac.zMalloc = pOut->zMalloc;
64555    pOut->zMalloc = 0;
64556    sqlite3VdbeMemMove(pOut, pIn1);
64557#ifdef SQLITE_DEBUG
64558    if( pOut->pScopyFrom>=&aMem[u.ac.p1] && pOut->pScopyFrom<&aMem[u.ac.p1+pOp->p3] ){
64559      pOut->pScopyFrom += u.ac.p1 - pOp->p2;
64560    }
64561#endif
64562    pIn1->zMalloc = u.ac.zMalloc;
64563    REGISTER_TRACE(u.ac.p2++, pOut);
64564    pIn1++;
64565    pOut++;
64566  }
64567  break;
64568}
64569
64570/* Opcode: Copy P1 P2 * * *
64571**
64572** Make a copy of register P1 into register P2.
64573**
64574** This instruction makes a deep copy of the value.  A duplicate
64575** is made of any string or blob constant.  See also OP_SCopy.
64576*/
64577case OP_Copy: {             /* in1, out2 */
64578  pIn1 = &aMem[pOp->p1];
64579  pOut = &aMem[pOp->p2];
64580  assert( pOut!=pIn1 );
64581  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64582  Deephemeralize(pOut);
64583  REGISTER_TRACE(pOp->p2, pOut);
64584  break;
64585}
64586
64587/* Opcode: SCopy P1 P2 * * *
64588**
64589** Make a shallow copy of register P1 into register P2.
64590**
64591** This instruction makes a shallow copy of the value.  If the value
64592** is a string or blob, then the copy is only a pointer to the
64593** original and hence if the original changes so will the copy.
64594** Worse, if the original is deallocated, the copy becomes invalid.
64595** Thus the program must guarantee that the original will not change
64596** during the lifetime of the copy.  Use OP_Copy to make a complete
64597** copy.
64598*/
64599case OP_SCopy: {            /* in1, out2 */
64600  pIn1 = &aMem[pOp->p1];
64601  pOut = &aMem[pOp->p2];
64602  assert( pOut!=pIn1 );
64603  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64604#ifdef SQLITE_DEBUG
64605  if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
64606#endif
64607  REGISTER_TRACE(pOp->p2, pOut);
64608  break;
64609}
64610
64611/* Opcode: ResultRow P1 P2 * * *
64612**
64613** The registers P1 through P1+P2-1 contain a single row of
64614** results. This opcode causes the sqlite3_step() call to terminate
64615** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
64616** structure to provide access to the top P1 values as the result
64617** row.
64618*/
64619case OP_ResultRow: {
64620#if 0  /* local variables moved into u.ad */
64621  Mem *pMem;
64622  int i;
64623#endif /* local variables moved into u.ad */
64624  assert( p->nResColumn==pOp->p2 );
64625  assert( pOp->p1>0 );
64626  assert( pOp->p1+pOp->p2<=p->nMem+1 );
64627
64628  /* If this statement has violated immediate foreign key constraints, do
64629  ** not return the number of rows modified. And do not RELEASE the statement
64630  ** transaction. It needs to be rolled back.  */
64631  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
64632    assert( db->flags&SQLITE_CountRows );
64633    assert( p->usesStmtJournal );
64634    break;
64635  }
64636
64637  /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
64638  ** DML statements invoke this opcode to return the number of rows
64639  ** modified to the user. This is the only way that a VM that
64640  ** opens a statement transaction may invoke this opcode.
64641  **
64642  ** In case this is such a statement, close any statement transaction
64643  ** opened by this VM before returning control to the user. This is to
64644  ** ensure that statement-transactions are always nested, not overlapping.
64645  ** If the open statement-transaction is not closed here, then the user
64646  ** may step another VM that opens its own statement transaction. This
64647  ** may lead to overlapping statement transactions.
64648  **
64649  ** The statement transaction is never a top-level transaction.  Hence
64650  ** the RELEASE call below can never fail.
64651  */
64652  assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
64653  rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
64654  if( NEVER(rc!=SQLITE_OK) ){
64655    break;
64656  }
64657
64658  /* Invalidate all ephemeral cursor row caches */
64659  p->cacheCtr = (p->cacheCtr + 2)|1;
64660
64661  /* Make sure the results of the current row are \000 terminated
64662  ** and have an assigned type.  The results are de-ephemeralized as
64663  ** as side effect.
64664  */
64665  u.ad.pMem = p->pResultSet = &aMem[pOp->p1];
64666  for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
64667    assert( memIsValid(&u.ad.pMem[u.ad.i]) );
64668    Deephemeralize(&u.ad.pMem[u.ad.i]);
64669    assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0
64670            || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 );
64671    sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
64672    sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]);
64673    REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
64674  }
64675  if( db->mallocFailed ) goto no_mem;
64676
64677  /* Return SQLITE_ROW
64678  */
64679  p->pc = pc + 1;
64680  rc = SQLITE_ROW;
64681  goto vdbe_return;
64682}
64683
64684/* Opcode: Concat P1 P2 P3 * *
64685**
64686** Add the text in register P1 onto the end of the text in
64687** register P2 and store the result in register P3.
64688** If either the P1 or P2 text are NULL then store NULL in P3.
64689**
64690**   P3 = P2 || P1
64691**
64692** It is illegal for P1 and P3 to be the same register. Sometimes,
64693** if P3 is the same register as P2, the implementation is able
64694** to avoid a memcpy().
64695*/
64696case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
64697#if 0  /* local variables moved into u.ae */
64698  i64 nByte;
64699#endif /* local variables moved into u.ae */
64700
64701  pIn1 = &aMem[pOp->p1];
64702  pIn2 = &aMem[pOp->p2];
64703  pOut = &aMem[pOp->p3];
64704  assert( pIn1!=pOut );
64705  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
64706    sqlite3VdbeMemSetNull(pOut);
64707    break;
64708  }
64709  if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
64710  Stringify(pIn1, encoding);
64711  Stringify(pIn2, encoding);
64712  u.ae.nByte = pIn1->n + pIn2->n;
64713  if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64714    goto too_big;
64715  }
64716  MemSetTypeFlag(pOut, MEM_Str);
64717  if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
64718    goto no_mem;
64719  }
64720  if( pOut!=pIn2 ){
64721    memcpy(pOut->z, pIn2->z, pIn2->n);
64722  }
64723  memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64724  pOut->z[u.ae.nByte] = 0;
64725  pOut->z[u.ae.nByte+1] = 0;
64726  pOut->flags |= MEM_Term;
64727  pOut->n = (int)u.ae.nByte;
64728  pOut->enc = encoding;
64729  UPDATE_MAX_BLOBSIZE(pOut);
64730  break;
64731}
64732
64733/* Opcode: Add P1 P2 P3 * *
64734**
64735** Add the value in register P1 to the value in register P2
64736** and store the result in register P3.
64737** If either input is NULL, the result is NULL.
64738*/
64739/* Opcode: Multiply P1 P2 P3 * *
64740**
64741**
64742** Multiply the value in register P1 by the value in register P2
64743** and store the result in register P3.
64744** If either input is NULL, the result is NULL.
64745*/
64746/* Opcode: Subtract P1 P2 P3 * *
64747**
64748** Subtract the value in register P1 from the value in register P2
64749** and store the result in register P3.
64750** If either input is NULL, the result is NULL.
64751*/
64752/* Opcode: Divide P1 P2 P3 * *
64753**
64754** Divide the value in register P1 by the value in register P2
64755** and store the result in register P3 (P3=P2/P1). If the value in
64756** register P1 is zero, then the result is NULL. If either input is
64757** NULL, the result is NULL.
64758*/
64759/* Opcode: Remainder P1 P2 P3 * *
64760**
64761** Compute the remainder after integer division of the value in
64762** register P1 by the value in register P2 and store the result in P3.
64763** If the value in register P2 is zero the result is NULL.
64764** If either operand is NULL, the result is NULL.
64765*/
64766case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
64767case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
64768case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
64769case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
64770case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
64771#if 0  /* local variables moved into u.af */
64772  int flags;      /* Combined MEM_* flags from both inputs */
64773  i64 iA;         /* Integer value of left operand */
64774  i64 iB;         /* Integer value of right operand */
64775  double rA;      /* Real value of left operand */
64776  double rB;      /* Real value of right operand */
64777#endif /* local variables moved into u.af */
64778
64779  pIn1 = &aMem[pOp->p1];
64780  applyNumericAffinity(pIn1);
64781  pIn2 = &aMem[pOp->p2];
64782  applyNumericAffinity(pIn2);
64783  pOut = &aMem[pOp->p3];
64784  u.af.flags = pIn1->flags | pIn2->flags;
64785  if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64786  if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64787    u.af.iA = pIn1->u.i;
64788    u.af.iB = pIn2->u.i;
64789    switch( pOp->opcode ){
64790      case OP_Add:       if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64791      case OP_Subtract:  if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64792      case OP_Multiply:  if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
64793      case OP_Divide: {
64794        if( u.af.iA==0 ) goto arithmetic_result_is_null;
64795        if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
64796        u.af.iB /= u.af.iA;
64797        break;
64798      }
64799      default: {
64800        if( u.af.iA==0 ) goto arithmetic_result_is_null;
64801        if( u.af.iA==-1 ) u.af.iA = 1;
64802        u.af.iB %= u.af.iA;
64803        break;
64804      }
64805    }
64806    pOut->u.i = u.af.iB;
64807    MemSetTypeFlag(pOut, MEM_Int);
64808  }else{
64809fp_math:
64810    u.af.rA = sqlite3VdbeRealValue(pIn1);
64811    u.af.rB = sqlite3VdbeRealValue(pIn2);
64812    switch( pOp->opcode ){
64813      case OP_Add:         u.af.rB += u.af.rA;       break;
64814      case OP_Subtract:    u.af.rB -= u.af.rA;       break;
64815      case OP_Multiply:    u.af.rB *= u.af.rA;       break;
64816      case OP_Divide: {
64817        /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64818        if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
64819        u.af.rB /= u.af.rA;
64820        break;
64821      }
64822      default: {
64823        u.af.iA = (i64)u.af.rA;
64824        u.af.iB = (i64)u.af.rB;
64825        if( u.af.iA==0 ) goto arithmetic_result_is_null;
64826        if( u.af.iA==-1 ) u.af.iA = 1;
64827        u.af.rB = (double)(u.af.iB % u.af.iA);
64828        break;
64829      }
64830    }
64831#ifdef SQLITE_OMIT_FLOATING_POINT
64832    pOut->u.i = u.af.rB;
64833    MemSetTypeFlag(pOut, MEM_Int);
64834#else
64835    if( sqlite3IsNaN(u.af.rB) ){
64836      goto arithmetic_result_is_null;
64837    }
64838    pOut->r = u.af.rB;
64839    MemSetTypeFlag(pOut, MEM_Real);
64840    if( (u.af.flags & MEM_Real)==0 ){
64841      sqlite3VdbeIntegerAffinity(pOut);
64842    }
64843#endif
64844  }
64845  break;
64846
64847arithmetic_result_is_null:
64848  sqlite3VdbeMemSetNull(pOut);
64849  break;
64850}
64851
64852/* Opcode: CollSeq * * P4
64853**
64854** P4 is a pointer to a CollSeq struct. If the next call to a user function
64855** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
64856** be returned. This is used by the built-in min(), max() and nullif()
64857** functions.
64858**
64859** The interface used by the implementation of the aforementioned functions
64860** to retrieve the collation sequence set by this opcode is not available
64861** publicly, only to user functions defined in func.c.
64862*/
64863case OP_CollSeq: {
64864  assert( pOp->p4type==P4_COLLSEQ );
64865  break;
64866}
64867
64868/* Opcode: Function P1 P2 P3 P4 P5
64869**
64870** Invoke a user function (P4 is a pointer to a Function structure that
64871** defines the function) with P5 arguments taken from register P2 and
64872** successors.  The result of the function is stored in register P3.
64873** Register P3 must not be one of the function inputs.
64874**
64875** P1 is a 32-bit bitmask indicating whether or not each argument to the
64876** function was determined to be constant at compile time. If the first
64877** argument was constant then bit 0 of P1 is set. This is used to determine
64878** whether meta data associated with a user function argument using the
64879** sqlite3_set_auxdata() API may be safely retained until the next
64880** invocation of this opcode.
64881**
64882** See also: AggStep and AggFinal
64883*/
64884case OP_Function: {
64885#if 0  /* local variables moved into u.ag */
64886  int i;
64887  Mem *pArg;
64888  sqlite3_context ctx;
64889  sqlite3_value **apVal;
64890  int n;
64891#endif /* local variables moved into u.ag */
64892
64893  u.ag.n = pOp->p5;
64894  u.ag.apVal = p->apArg;
64895  assert( u.ag.apVal || u.ag.n==0 );
64896  assert( pOp->p3>0 && pOp->p3<=p->nMem );
64897  pOut = &aMem[pOp->p3];
64898  memAboutToChange(p, pOut);
64899
64900  assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
64901  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
64902  u.ag.pArg = &aMem[pOp->p2];
64903  for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
64904    assert( memIsValid(u.ag.pArg) );
64905    u.ag.apVal[u.ag.i] = u.ag.pArg;
64906    Deephemeralize(u.ag.pArg);
64907    sqlite3VdbeMemStoreType(u.ag.pArg);
64908    REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg);
64909  }
64910
64911  assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
64912  if( pOp->p4type==P4_FUNCDEF ){
64913    u.ag.ctx.pFunc = pOp->p4.pFunc;
64914    u.ag.ctx.pVdbeFunc = 0;
64915  }else{
64916    u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64917    u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
64918  }
64919
64920  u.ag.ctx.s.flags = MEM_Null;
64921  u.ag.ctx.s.db = db;
64922  u.ag.ctx.s.xDel = 0;
64923  u.ag.ctx.s.zMalloc = 0;
64924
64925  /* The output cell may already have a buffer allocated. Move
64926  ** the pointer to u.ag.ctx.s so in case the user-function can use
64927  ** the already allocated buffer instead of allocating a new one.
64928  */
64929  sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
64930  MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
64931
64932  u.ag.ctx.isError = 0;
64933  if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
64934    assert( pOp>aOp );
64935    assert( pOp[-1].p4type==P4_COLLSEQ );
64936    assert( pOp[-1].opcode==OP_CollSeq );
64937    u.ag.ctx.pColl = pOp[-1].p4.pColl;
64938  }
64939  db->lastRowid = lastRowid;
64940  (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
64941  lastRowid = db->lastRowid;
64942
64943  /* If any auxiliary data functions have been called by this user function,
64944  ** immediately call the destructor for any non-static values.
64945  */
64946  if( u.ag.ctx.pVdbeFunc ){
64947    sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
64948    pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
64949    pOp->p4type = P4_VDBEFUNC;
64950  }
64951
64952  if( db->mallocFailed ){
64953    /* Even though a malloc() has failed, the implementation of the
64954    ** user function may have called an sqlite3_result_XXX() function
64955    ** to return a value. The following call releases any resources
64956    ** associated with such a value.
64957    */
64958    sqlite3VdbeMemRelease(&u.ag.ctx.s);
64959    goto no_mem;
64960  }
64961
64962  /* If the function returned an error, throw an exception */
64963  if( u.ag.ctx.isError ){
64964    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
64965    rc = u.ag.ctx.isError;
64966  }
64967
64968  /* Copy the result of the function into register P3 */
64969  sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
64970  sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
64971  if( sqlite3VdbeMemTooBig(pOut) ){
64972    goto too_big;
64973  }
64974
64975#if 0
64976  /* The app-defined function has done something that as caused this
64977  ** statement to expire.  (Perhaps the function called sqlite3_exec()
64978  ** with a CREATE TABLE statement.)
64979  */
64980  if( p->expired ) rc = SQLITE_ABORT;
64981#endif
64982
64983  REGISTER_TRACE(pOp->p3, pOut);
64984  UPDATE_MAX_BLOBSIZE(pOut);
64985  break;
64986}
64987
64988/* Opcode: BitAnd P1 P2 P3 * *
64989**
64990** Take the bit-wise AND of the values in register P1 and P2 and
64991** store the result in register P3.
64992** If either input is NULL, the result is NULL.
64993*/
64994/* Opcode: BitOr P1 P2 P3 * *
64995**
64996** Take the bit-wise OR of the values in register P1 and P2 and
64997** store the result in register P3.
64998** If either input is NULL, the result is NULL.
64999*/
65000/* Opcode: ShiftLeft P1 P2 P3 * *
65001**
65002** Shift the integer value in register P2 to the left by the
65003** number of bits specified by the integer in register P1.
65004** Store the result in register P3.
65005** If either input is NULL, the result is NULL.
65006*/
65007/* Opcode: ShiftRight P1 P2 P3 * *
65008**
65009** Shift the integer value in register P2 to the right by the
65010** number of bits specified by the integer in register P1.
65011** Store the result in register P3.
65012** If either input is NULL, the result is NULL.
65013*/
65014case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
65015case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
65016case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
65017case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
65018#if 0  /* local variables moved into u.ah */
65019  i64 iA;
65020  u64 uA;
65021  i64 iB;
65022  u8 op;
65023#endif /* local variables moved into u.ah */
65024
65025  pIn1 = &aMem[pOp->p1];
65026  pIn2 = &aMem[pOp->p2];
65027  pOut = &aMem[pOp->p3];
65028  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
65029    sqlite3VdbeMemSetNull(pOut);
65030    break;
65031  }
65032  u.ah.iA = sqlite3VdbeIntValue(pIn2);
65033  u.ah.iB = sqlite3VdbeIntValue(pIn1);
65034  u.ah.op = pOp->opcode;
65035  if( u.ah.op==OP_BitAnd ){
65036    u.ah.iA &= u.ah.iB;
65037  }else if( u.ah.op==OP_BitOr ){
65038    u.ah.iA |= u.ah.iB;
65039  }else if( u.ah.iB!=0 ){
65040    assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
65041
65042    /* If shifting by a negative amount, shift in the other direction */
65043    if( u.ah.iB<0 ){
65044      assert( OP_ShiftRight==OP_ShiftLeft+1 );
65045      u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
65046      u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
65047    }
65048
65049    if( u.ah.iB>=64 ){
65050      u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
65051    }else{
65052      memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
65053      if( u.ah.op==OP_ShiftLeft ){
65054        u.ah.uA <<= u.ah.iB;
65055      }else{
65056        u.ah.uA >>= u.ah.iB;
65057        /* Sign-extend on a right shift of a negative number */
65058        if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
65059      }
65060      memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
65061    }
65062  }
65063  pOut->u.i = u.ah.iA;
65064  MemSetTypeFlag(pOut, MEM_Int);
65065  break;
65066}
65067
65068/* Opcode: AddImm  P1 P2 * * *
65069**
65070** Add the constant P2 to the value in register P1.
65071** The result is always an integer.
65072**
65073** To force any register to be an integer, just add 0.
65074*/
65075case OP_AddImm: {            /* in1 */
65076  pIn1 = &aMem[pOp->p1];
65077  memAboutToChange(p, pIn1);
65078  sqlite3VdbeMemIntegerify(pIn1);
65079  pIn1->u.i += pOp->p2;
65080  break;
65081}
65082
65083/* Opcode: MustBeInt P1 P2 * * *
65084**
65085** Force the value in register P1 to be an integer.  If the value
65086** in P1 is not an integer and cannot be converted into an integer
65087** without data loss, then jump immediately to P2, or if P2==0
65088** raise an SQLITE_MISMATCH exception.
65089*/
65090case OP_MustBeInt: {            /* jump, in1 */
65091  pIn1 = &aMem[pOp->p1];
65092  applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
65093  if( (pIn1->flags & MEM_Int)==0 ){
65094    if( pOp->p2==0 ){
65095      rc = SQLITE_MISMATCH;
65096      goto abort_due_to_error;
65097    }else{
65098      pc = pOp->p2 - 1;
65099    }
65100  }else{
65101    MemSetTypeFlag(pIn1, MEM_Int);
65102  }
65103  break;
65104}
65105
65106#ifndef SQLITE_OMIT_FLOATING_POINT
65107/* Opcode: RealAffinity P1 * * * *
65108**
65109** If register P1 holds an integer convert it to a real value.
65110**
65111** This opcode is used when extracting information from a column that
65112** has REAL affinity.  Such column values may still be stored as
65113** integers, for space efficiency, but after extraction we want them
65114** to have only a real value.
65115*/
65116case OP_RealAffinity: {                  /* in1 */
65117  pIn1 = &aMem[pOp->p1];
65118  if( pIn1->flags & MEM_Int ){
65119    sqlite3VdbeMemRealify(pIn1);
65120  }
65121  break;
65122}
65123#endif
65124
65125#ifndef SQLITE_OMIT_CAST
65126/* Opcode: ToText P1 * * * *
65127**
65128** Force the value in register P1 to be text.
65129** If the value is numeric, convert it to a string using the
65130** equivalent of printf().  Blob values are unchanged and
65131** are afterwards simply interpreted as text.
65132**
65133** A NULL value is not changed by this routine.  It remains NULL.
65134*/
65135case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
65136  pIn1 = &aMem[pOp->p1];
65137  memAboutToChange(p, pIn1);
65138  if( pIn1->flags & MEM_Null ) break;
65139  assert( MEM_Str==(MEM_Blob>>3) );
65140  pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
65141  applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
65142  rc = ExpandBlob(pIn1);
65143  assert( pIn1->flags & MEM_Str || db->mallocFailed );
65144  pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
65145  UPDATE_MAX_BLOBSIZE(pIn1);
65146  break;
65147}
65148
65149/* Opcode: ToBlob P1 * * * *
65150**
65151** Force the value in register P1 to be a BLOB.
65152** If the value is numeric, convert it to a string first.
65153** Strings are simply reinterpreted as blobs with no change
65154** to the underlying data.
65155**
65156** A NULL value is not changed by this routine.  It remains NULL.
65157*/
65158case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
65159  pIn1 = &aMem[pOp->p1];
65160  if( pIn1->flags & MEM_Null ) break;
65161  if( (pIn1->flags & MEM_Blob)==0 ){
65162    applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
65163    assert( pIn1->flags & MEM_Str || db->mallocFailed );
65164    MemSetTypeFlag(pIn1, MEM_Blob);
65165  }else{
65166    pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
65167  }
65168  UPDATE_MAX_BLOBSIZE(pIn1);
65169  break;
65170}
65171
65172/* Opcode: ToNumeric P1 * * * *
65173**
65174** Force the value in register P1 to be numeric (either an
65175** integer or a floating-point number.)
65176** If the value is text or blob, try to convert it to an using the
65177** equivalent of atoi() or atof() and store 0 if no such conversion
65178** is possible.
65179**
65180** A NULL value is not changed by this routine.  It remains NULL.
65181*/
65182case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
65183  pIn1 = &aMem[pOp->p1];
65184  sqlite3VdbeMemNumerify(pIn1);
65185  break;
65186}
65187#endif /* SQLITE_OMIT_CAST */
65188
65189/* Opcode: ToInt P1 * * * *
65190**
65191** Force the value in register P1 to be an integer.  If
65192** The value is currently a real number, drop its fractional part.
65193** If the value is text or blob, try to convert it to an integer using the
65194** equivalent of atoi() and store 0 if no such conversion is possible.
65195**
65196** A NULL value is not changed by this routine.  It remains NULL.
65197*/
65198case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
65199  pIn1 = &aMem[pOp->p1];
65200  if( (pIn1->flags & MEM_Null)==0 ){
65201    sqlite3VdbeMemIntegerify(pIn1);
65202  }
65203  break;
65204}
65205
65206#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
65207/* Opcode: ToReal P1 * * * *
65208**
65209** Force the value in register P1 to be a floating point number.
65210** If The value is currently an integer, convert it.
65211** If the value is text or blob, try to convert it to an integer using the
65212** equivalent of atoi() and store 0.0 if no such conversion is possible.
65213**
65214** A NULL value is not changed by this routine.  It remains NULL.
65215*/
65216case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
65217  pIn1 = &aMem[pOp->p1];
65218  memAboutToChange(p, pIn1);
65219  if( (pIn1->flags & MEM_Null)==0 ){
65220    sqlite3VdbeMemRealify(pIn1);
65221  }
65222  break;
65223}
65224#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
65225
65226/* Opcode: Lt P1 P2 P3 P4 P5
65227**
65228** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
65229** jump to address P2.
65230**
65231** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
65232** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL
65233** bit is clear then fall through if either operand is NULL.
65234**
65235** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
65236** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
65237** to coerce both inputs according to this affinity before the
65238** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
65239** affinity is used. Note that the affinity conversions are stored
65240** back into the input registers P1 and P3.  So this opcode can cause
65241** persistent changes to registers P1 and P3.
65242**
65243** Once any conversions have taken place, and neither value is NULL,
65244** the values are compared. If both values are blobs then memcmp() is
65245** used to determine the results of the comparison.  If both values
65246** are text, then the appropriate collating function specified in
65247** P4 is  used to do the comparison.  If P4 is not specified then
65248** memcmp() is used to compare text string.  If both values are
65249** numeric, then a numeric comparison is used. If the two values
65250** are of different types, then numbers are considered less than
65251** strings and strings are considered less than blobs.
65252**
65253** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
65254** store a boolean result (either 0, or 1, or NULL) in register P2.
65255*/
65256/* Opcode: Ne P1 P2 P3 P4 P5
65257**
65258** This works just like the Lt opcode except that the jump is taken if
65259** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
65260** additional information.
65261**
65262** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
65263** true or false and is never NULL.  If both operands are NULL then the result
65264** of comparison is false.  If either operand is NULL then the result is true.
65265** If neither operand is NULL the result is the same as it would be if
65266** the SQLITE_NULLEQ flag were omitted from P5.
65267*/
65268/* Opcode: Eq P1 P2 P3 P4 P5
65269**
65270** This works just like the Lt opcode except that the jump is taken if
65271** the operands in registers P1 and P3 are equal.
65272** See the Lt opcode for additional information.
65273**
65274** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
65275** true or false and is never NULL.  If both operands are NULL then the result
65276** of comparison is true.  If either operand is NULL then the result is false.
65277** If neither operand is NULL the result is the same as it would be if
65278** the SQLITE_NULLEQ flag were omitted from P5.
65279*/
65280/* Opcode: Le P1 P2 P3 P4 P5
65281**
65282** This works just like the Lt opcode except that the jump is taken if
65283** the content of register P3 is less than or equal to the content of
65284** register P1.  See the Lt opcode for additional information.
65285*/
65286/* Opcode: Gt P1 P2 P3 P4 P5
65287**
65288** This works just like the Lt opcode except that the jump is taken if
65289** the content of register P3 is greater than the content of
65290** register P1.  See the Lt opcode for additional information.
65291*/
65292/* Opcode: Ge P1 P2 P3 P4 P5
65293**
65294** This works just like the Lt opcode except that the jump is taken if
65295** the content of register P3 is greater than or equal to the content of
65296** register P1.  See the Lt opcode for additional information.
65297*/
65298case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
65299case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
65300case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
65301case OP_Le:               /* same as TK_LE, jump, in1, in3 */
65302case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
65303case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
65304#if 0  /* local variables moved into u.ai */
65305  int res;            /* Result of the comparison of pIn1 against pIn3 */
65306  char affinity;      /* Affinity to use for comparison */
65307  u16 flags1;         /* Copy of initial value of pIn1->flags */
65308  u16 flags3;         /* Copy of initial value of pIn3->flags */
65309#endif /* local variables moved into u.ai */
65310
65311  pIn1 = &aMem[pOp->p1];
65312  pIn3 = &aMem[pOp->p3];
65313  u.ai.flags1 = pIn1->flags;
65314  u.ai.flags3 = pIn3->flags;
65315  if( (u.ai.flags1 | u.ai.flags3)&MEM_Null ){
65316    /* One or both operands are NULL */
65317    if( pOp->p5 & SQLITE_NULLEQ ){
65318      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
65319      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
65320      ** or not both operands are null.
65321      */
65322      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65323      u.ai.res = (u.ai.flags1 & u.ai.flags3 & MEM_Null)==0;
65324    }else{
65325      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
65326      ** then the result is always NULL.
65327      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
65328      */
65329      if( pOp->p5 & SQLITE_STOREP2 ){
65330        pOut = &aMem[pOp->p2];
65331        MemSetTypeFlag(pOut, MEM_Null);
65332        REGISTER_TRACE(pOp->p2, pOut);
65333      }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
65334        pc = pOp->p2-1;
65335      }
65336      break;
65337    }
65338  }else{
65339    /* Neither operand is NULL.  Do a comparison. */
65340    u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
65341    if( u.ai.affinity ){
65342      applyAffinity(pIn1, u.ai.affinity, encoding);
65343      applyAffinity(pIn3, u.ai.affinity, encoding);
65344      if( db->mallocFailed ) goto no_mem;
65345    }
65346
65347    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
65348    ExpandBlob(pIn1);
65349    ExpandBlob(pIn3);
65350    u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
65351  }
65352  switch( pOp->opcode ){
65353    case OP_Eq:    u.ai.res = u.ai.res==0;     break;
65354    case OP_Ne:    u.ai.res = u.ai.res!=0;     break;
65355    case OP_Lt:    u.ai.res = u.ai.res<0;      break;
65356    case OP_Le:    u.ai.res = u.ai.res<=0;     break;
65357    case OP_Gt:    u.ai.res = u.ai.res>0;      break;
65358    default:       u.ai.res = u.ai.res>=0;     break;
65359  }
65360
65361  if( pOp->p5 & SQLITE_STOREP2 ){
65362    pOut = &aMem[pOp->p2];
65363    memAboutToChange(p, pOut);
65364    MemSetTypeFlag(pOut, MEM_Int);
65365    pOut->u.i = u.ai.res;
65366    REGISTER_TRACE(pOp->p2, pOut);
65367  }else if( u.ai.res ){
65368    pc = pOp->p2-1;
65369  }
65370
65371  /* Undo any changes made by applyAffinity() to the input registers. */
65372  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ai.flags1&MEM_TypeMask);
65373  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ai.flags3&MEM_TypeMask);
65374  break;
65375}
65376
65377/* Opcode: Permutation * * * P4 *
65378**
65379** Set the permutation used by the OP_Compare operator to be the array
65380** of integers in P4.
65381**
65382** The permutation is only valid until the next OP_Permutation, OP_Compare,
65383** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
65384** immediately prior to the OP_Compare.
65385*/
65386case OP_Permutation: {
65387  assert( pOp->p4type==P4_INTARRAY );
65388  assert( pOp->p4.ai );
65389  aPermute = pOp->p4.ai;
65390  break;
65391}
65392
65393/* Opcode: Compare P1 P2 P3 P4 *
65394**
65395** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
65396** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
65397** the comparison for use by the next OP_Jump instruct.
65398**
65399** P4 is a KeyInfo structure that defines collating sequences and sort
65400** orders for the comparison.  The permutation applies to registers
65401** only.  The KeyInfo elements are used sequentially.
65402**
65403** The comparison is a sort comparison, so NULLs compare equal,
65404** NULLs are less than numbers, numbers are less than strings,
65405** and strings are less than blobs.
65406*/
65407case OP_Compare: {
65408#if 0  /* local variables moved into u.aj */
65409  int n;
65410  int i;
65411  int p1;
65412  int p2;
65413  const KeyInfo *pKeyInfo;
65414  int idx;
65415  CollSeq *pColl;    /* Collating sequence to use on this term */
65416  int bRev;          /* True for DESCENDING sort order */
65417#endif /* local variables moved into u.aj */
65418
65419  u.aj.n = pOp->p3;
65420  u.aj.pKeyInfo = pOp->p4.pKeyInfo;
65421  assert( u.aj.n>0 );
65422  assert( u.aj.pKeyInfo!=0 );
65423  u.aj.p1 = pOp->p1;
65424  u.aj.p2 = pOp->p2;
65425#if SQLITE_DEBUG
65426  if( aPermute ){
65427    int k, mx = 0;
65428    for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65429    assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
65430    assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
65431  }else{
65432    assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
65433    assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
65434  }
65435#endif /* SQLITE_DEBUG */
65436  for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
65437    u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
65438    assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) );
65439    assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) );
65440    REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]);
65441    REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]);
65442    assert( u.aj.i<u.aj.pKeyInfo->nField );
65443    u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
65444    u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
65445    iCompare = sqlite3MemCompare(&aMem[u.aj.p1+u.aj.idx], &aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
65446    if( iCompare ){
65447      if( u.aj.bRev ) iCompare = -iCompare;
65448      break;
65449    }
65450  }
65451  aPermute = 0;
65452  break;
65453}
65454
65455/* Opcode: Jump P1 P2 P3 * *
65456**
65457** Jump to the instruction at address P1, P2, or P3 depending on whether
65458** in the most recent OP_Compare instruction the P1 vector was less than
65459** equal to, or greater than the P2 vector, respectively.
65460*/
65461case OP_Jump: {             /* jump */
65462  if( iCompare<0 ){
65463    pc = pOp->p1 - 1;
65464  }else if( iCompare==0 ){
65465    pc = pOp->p2 - 1;
65466  }else{
65467    pc = pOp->p3 - 1;
65468  }
65469  break;
65470}
65471
65472/* Opcode: And P1 P2 P3 * *
65473**
65474** Take the logical AND of the values in registers P1 and P2 and
65475** write the result into register P3.
65476**
65477** If either P1 or P2 is 0 (false) then the result is 0 even if
65478** the other input is NULL.  A NULL and true or two NULLs give
65479** a NULL output.
65480*/
65481/* Opcode: Or P1 P2 P3 * *
65482**
65483** Take the logical OR of the values in register P1 and P2 and
65484** store the answer in register P3.
65485**
65486** If either P1 or P2 is nonzero (true) then the result is 1 (true)
65487** even if the other input is NULL.  A NULL and false or two NULLs
65488** give a NULL output.
65489*/
65490case OP_And:              /* same as TK_AND, in1, in2, out3 */
65491case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
65492#if 0  /* local variables moved into u.ak */
65493  int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65494  int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65495#endif /* local variables moved into u.ak */
65496
65497  pIn1 = &aMem[pOp->p1];
65498  if( pIn1->flags & MEM_Null ){
65499    u.ak.v1 = 2;
65500  }else{
65501    u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
65502  }
65503  pIn2 = &aMem[pOp->p2];
65504  if( pIn2->flags & MEM_Null ){
65505    u.ak.v2 = 2;
65506  }else{
65507    u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
65508  }
65509  if( pOp->opcode==OP_And ){
65510    static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65511    u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
65512  }else{
65513    static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65514    u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
65515  }
65516  pOut = &aMem[pOp->p3];
65517  if( u.ak.v1==2 ){
65518    MemSetTypeFlag(pOut, MEM_Null);
65519  }else{
65520    pOut->u.i = u.ak.v1;
65521    MemSetTypeFlag(pOut, MEM_Int);
65522  }
65523  break;
65524}
65525
65526/* Opcode: Not P1 P2 * * *
65527**
65528** Interpret the value in register P1 as a boolean value.  Store the
65529** boolean complement in register P2.  If the value in register P1 is
65530** NULL, then a NULL is stored in P2.
65531*/
65532case OP_Not: {                /* same as TK_NOT, in1, out2 */
65533  pIn1 = &aMem[pOp->p1];
65534  pOut = &aMem[pOp->p2];
65535  if( pIn1->flags & MEM_Null ){
65536    sqlite3VdbeMemSetNull(pOut);
65537  }else{
65538    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
65539  }
65540  break;
65541}
65542
65543/* Opcode: BitNot P1 P2 * * *
65544**
65545** Interpret the content of register P1 as an integer.  Store the
65546** ones-complement of the P1 value into register P2.  If P1 holds
65547** a NULL then store a NULL in P2.
65548*/
65549case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
65550  pIn1 = &aMem[pOp->p1];
65551  pOut = &aMem[pOp->p2];
65552  if( pIn1->flags & MEM_Null ){
65553    sqlite3VdbeMemSetNull(pOut);
65554  }else{
65555    sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
65556  }
65557  break;
65558}
65559
65560/* Opcode: Once P1 P2 * * *
65561**
65562** Jump to P2 if the value in register P1 is a not null or zero.  If
65563** the value is NULL or zero, fall through and change the P1 register
65564** to an integer 1.
65565**
65566** When P1 is not used otherwise in a program, this opcode falls through
65567** once and jumps on all subsequent invocations.  It is the equivalent
65568** of "OP_If P1 P2", followed by "OP_Integer 1 P1".
65569*/
65570/* Opcode: If P1 P2 P3 * *
65571**
65572** Jump to P2 if the value in register P1 is true.  The value
65573** is considered true if it is numeric and non-zero.  If the value
65574** in P1 is NULL then take the jump if P3 is true.
65575*/
65576/* Opcode: IfNot P1 P2 P3 * *
65577**
65578** Jump to P2 if the value in register P1 is False.  The value
65579** is considered true if it has a numeric value of zero.  If the value
65580** in P1 is NULL then take the jump if P3 is true.
65581*/
65582case OP_Once:               /* jump, in1 */
65583case OP_If:                 /* jump, in1 */
65584case OP_IfNot: {            /* jump, in1 */
65585#if 0  /* local variables moved into u.al */
65586  int c;
65587#endif /* local variables moved into u.al */
65588  pIn1 = &aMem[pOp->p1];
65589  if( pIn1->flags & MEM_Null ){
65590    u.al.c = pOp->p3;
65591  }else{
65592#ifdef SQLITE_OMIT_FLOATING_POINT
65593    u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
65594#else
65595    u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
65596#endif
65597    if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
65598  }
65599  if( u.al.c ){
65600    pc = pOp->p2-1;
65601  }else if( pOp->opcode==OP_Once ){
65602    assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 );
65603    memAboutToChange(p, pIn1);
65604    pIn1->flags = MEM_Int;
65605    pIn1->u.i = 1;
65606    REGISTER_TRACE(pOp->p1, pIn1);
65607  }
65608  break;
65609}
65610
65611/* Opcode: IsNull P1 P2 * * *
65612**
65613** Jump to P2 if the value in register P1 is NULL.
65614*/
65615case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
65616  pIn1 = &aMem[pOp->p1];
65617  if( (pIn1->flags & MEM_Null)!=0 ){
65618    pc = pOp->p2 - 1;
65619  }
65620  break;
65621}
65622
65623/* Opcode: NotNull P1 P2 * * *
65624**
65625** Jump to P2 if the value in register P1 is not NULL.
65626*/
65627case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
65628  pIn1 = &aMem[pOp->p1];
65629  if( (pIn1->flags & MEM_Null)==0 ){
65630    pc = pOp->p2 - 1;
65631  }
65632  break;
65633}
65634
65635/* Opcode: Column P1 P2 P3 P4 P5
65636**
65637** Interpret the data that cursor P1 points to as a structure built using
65638** the MakeRecord instruction.  (See the MakeRecord opcode for additional
65639** information about the format of the data.)  Extract the P2-th column
65640** from this record.  If there are less that (P2+1)
65641** values in the record, extract a NULL.
65642**
65643** The value extracted is stored in register P3.
65644**
65645** If the column contains fewer than P2 fields, then extract a NULL.  Or,
65646** if the P4 argument is a P4_MEM use the value of the P4 argument as
65647** the result.
65648**
65649** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
65650** then the cache of the cursor is reset prior to extracting the column.
65651** The first OP_Column against a pseudo-table after the value of the content
65652** register has changed should have this bit set.
65653*/
65654case OP_Column: {
65655#if 0  /* local variables moved into u.am */
65656  u32 payloadSize;   /* Number of bytes in the record */
65657  i64 payloadSize64; /* Number of bytes in the record */
65658  int p1;            /* P1 value of the opcode */
65659  int p2;            /* column number to retrieve */
65660  VdbeCursor *pC;    /* The VDBE cursor */
65661  char *zRec;        /* Pointer to complete record-data */
65662  BtCursor *pCrsr;   /* The BTree cursor */
65663  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
65664  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
65665  int nField;        /* number of fields in the record */
65666  int len;           /* The length of the serialized data for the column */
65667  int i;             /* Loop counter */
65668  char *zData;       /* Part of the record being decoded */
65669  Mem *pDest;        /* Where to write the extracted value */
65670  Mem sMem;          /* For storing the record being decoded */
65671  u8 *zIdx;          /* Index into header */
65672  u8 *zEndHdr;       /* Pointer to first byte after the header */
65673  u32 offset;        /* Offset into the data */
65674  u32 szField;       /* Number of bytes in the content of a field */
65675  int szHdr;         /* Size of the header size field at start of record */
65676  int avail;         /* Number of bytes of available data */
65677  u32 t;             /* A type code from the record header */
65678  Mem *pReg;         /* PseudoTable input register */
65679#endif /* local variables moved into u.am */
65680
65681
65682  u.am.p1 = pOp->p1;
65683  u.am.p2 = pOp->p2;
65684  u.am.pC = 0;
65685  memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65686  assert( u.am.p1<p->nCursor );
65687  assert( pOp->p3>0 && pOp->p3<=p->nMem );
65688  u.am.pDest = &aMem[pOp->p3];
65689  memAboutToChange(p, u.am.pDest);
65690  u.am.zRec = 0;
65691
65692  /* This block sets the variable u.am.payloadSize to be the total number of
65693  ** bytes in the record.
65694  **
65695  ** u.am.zRec is set to be the complete text of the record if it is available.
65696  ** The complete record text is always available for pseudo-tables
65697  ** If the record is stored in a cursor, the complete record text
65698  ** might be available in the  u.am.pC->aRow cache.  Or it might not be.
65699  ** If the data is unavailable,  u.am.zRec is set to NULL.
65700  **
65701  ** We also compute the number of columns in the record.  For cursors,
65702  ** the number of columns is stored in the VdbeCursor.nField element.
65703  */
65704  u.am.pC = p->apCsr[u.am.p1];
65705  assert( u.am.pC!=0 );
65706#ifndef SQLITE_OMIT_VIRTUALTABLE
65707  assert( u.am.pC->pVtabCursor==0 );
65708#endif
65709  u.am.pCrsr = u.am.pC->pCursor;
65710  if( u.am.pCrsr!=0 ){
65711    /* The record is stored in a B-Tree */
65712    rc = sqlite3VdbeCursorMoveto(u.am.pC);
65713    if( rc ) goto abort_due_to_error;
65714    if( u.am.pC->nullRow ){
65715      u.am.payloadSize = 0;
65716    }else if( u.am.pC->cacheStatus==p->cacheCtr ){
65717      u.am.payloadSize = u.am.pC->payloadSize;
65718      u.am.zRec = (char*)u.am.pC->aRow;
65719    }else if( u.am.pC->isIndex ){
65720      assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65721      rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
65722      assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
65723      /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65724      ** payload size, so it is impossible for u.am.payloadSize64 to be
65725      ** larger than 32 bits. */
65726      assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
65727      u.am.payloadSize = (u32)u.am.payloadSize64;
65728    }else{
65729      assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65730      rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65731      assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
65732    }
65733  }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65734    u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65735    assert( u.am.pReg->flags & MEM_Blob );
65736    assert( memIsValid(u.am.pReg) );
65737    u.am.payloadSize = u.am.pReg->n;
65738    u.am.zRec = u.am.pReg->z;
65739    u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65740    assert( u.am.payloadSize==0 || u.am.zRec!=0 );
65741  }else{
65742    /* Consider the row to be NULL */
65743    u.am.payloadSize = 0;
65744  }
65745
65746  /* If u.am.payloadSize is 0, then just store a NULL.  This can happen because of
65747  ** nullRow or because of a corrupt database. */
65748  if( u.am.payloadSize==0 ){
65749    MemSetTypeFlag(u.am.pDest, MEM_Null);
65750    goto op_column_out;
65751  }
65752  assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65753  if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65754    goto too_big;
65755  }
65756
65757  u.am.nField = u.am.pC->nField;
65758  assert( u.am.p2<u.am.nField );
65759
65760  /* Read and parse the table header.  Store the results of the parse
65761  ** into the record header cache fields of the cursor.
65762  */
65763  u.am.aType = u.am.pC->aType;
65764  if( u.am.pC->cacheStatus==p->cacheCtr ){
65765    u.am.aOffset = u.am.pC->aOffset;
65766  }else{
65767    assert(u.am.aType);
65768    u.am.avail = 0;
65769    u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
65770    u.am.pC->payloadSize = u.am.payloadSize;
65771    u.am.pC->cacheStatus = p->cacheCtr;
65772
65773    /* Figure out how many bytes are in the header */
65774    if( u.am.zRec ){
65775      u.am.zData = u.am.zRec;
65776    }else{
65777      if( u.am.pC->isIndex ){
65778        u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
65779      }else{
65780        u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
65781      }
65782      /* If KeyFetch()/DataFetch() managed to get the entire payload,
65783      ** save the payload in the u.am.pC->aRow cache.  That will save us from
65784      ** having to make additional calls to fetch the content portion of
65785      ** the record.
65786      */
65787      assert( u.am.avail>=0 );
65788      if( u.am.payloadSize <= (u32)u.am.avail ){
65789        u.am.zRec = u.am.zData;
65790        u.am.pC->aRow = (u8*)u.am.zData;
65791      }else{
65792        u.am.pC->aRow = 0;
65793      }
65794    }
65795    /* The following assert is true in all cases accept when
65796    ** the database file has been corrupted externally.
65797    **    assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
65798    u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
65799
65800    /* Make sure a corrupt database has not given us an oversize header.
65801    ** Do this now to avoid an oversize memory allocation.
65802    **
65803    ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
65804    ** types use so much data space that there can only be 4096 and 32 of
65805    ** them, respectively.  So the maximum header length results from a
65806    ** 3-byte type for each of the maximum of 32768 columns plus three
65807    ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
65808    */
65809    if( u.am.offset > 98307 ){
65810      rc = SQLITE_CORRUPT_BKPT;
65811      goto op_column_out;
65812    }
65813
65814    /* Compute in u.am.len the number of bytes of data we need to read in order
65815    ** to get u.am.nField type values.  u.am.offset is an upper bound on this.  But
65816    ** u.am.nField might be significantly less than the true number of columns
65817    ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
65818    ** We want to minimize u.am.len in order to limit the size of the memory
65819    ** allocation, especially if a corrupt database file has caused u.am.offset
65820    ** to be oversized. Offset is limited to 98307 above.  But 98307 might
65821    ** still exceed Robson memory allocation limits on some configurations.
65822    ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
65823    ** will likely be much smaller since u.am.nField will likely be less than
65824    ** 20 or so.  This insures that Robson memory allocation limits are
65825    ** not exceeded even for corrupt database files.
65826    */
65827    u.am.len = u.am.nField*5 + 3;
65828    if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
65829
65830    /* The KeyFetch() or DataFetch() above are fast and will get the entire
65831    ** record header in most cases.  But they will fail to get the complete
65832    ** record header if the record header does not fit on a single page
65833    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
65834    ** acquire the complete header text.
65835    */
65836    if( !u.am.zRec && u.am.avail<u.am.len ){
65837      u.am.sMem.flags = 0;
65838      u.am.sMem.db = 0;
65839      rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
65840      if( rc!=SQLITE_OK ){
65841        goto op_column_out;
65842      }
65843      u.am.zData = u.am.sMem.z;
65844    }
65845    u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
65846    u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
65847
65848    /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
65849    ** arrays.  u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
65850    ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
65851    ** of the record to the start of the data for the u.am.i-th column
65852    */
65853    for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65854      if( u.am.zIdx<u.am.zEndHdr ){
65855        u.am.aOffset[u.am.i] = u.am.offset;
65856        if( u.am.zIdx[0]<0x80 ){
65857          u.am.t = u.am.zIdx[0];
65858          u.am.zIdx++;
65859        }else{
65860          u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65861        }
65862        u.am.aType[u.am.i] = u.am.t;
65863        u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
65864        u.am.offset += u.am.szField;
65865        if( u.am.offset<u.am.szField ){  /* True if u.am.offset overflows */
65866          u.am.zIdx = &u.am.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
65867          break;
65868        }
65869      }else{
65870        /* If u.am.i is less that u.am.nField, then there are less fields in this
65871        ** record than SetNumColumns indicated there are columns in the
65872        ** table. Set the u.am.offset for any extra columns not present in
65873        ** the record to 0. This tells code below to store a NULL
65874        ** instead of deserializing a value from the record.
65875        */
65876        u.am.aOffset[u.am.i] = 0;
65877      }
65878    }
65879    sqlite3VdbeMemRelease(&u.am.sMem);
65880    u.am.sMem.flags = MEM_Null;
65881
65882    /* If we have read more header data than was contained in the header,
65883    ** or if the end of the last field appears to be past the end of the
65884    ** record, or if the end of the last field appears to be before the end
65885    ** of the record (when all fields present), then we must be dealing
65886    ** with a corrupt database.
65887    */
65888    if( (u.am.zIdx > u.am.zEndHdr) || (u.am.offset > u.am.payloadSize)
65889         || (u.am.zIdx==u.am.zEndHdr && u.am.offset!=u.am.payloadSize) ){
65890      rc = SQLITE_CORRUPT_BKPT;
65891      goto op_column_out;
65892    }
65893  }
65894
65895  /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
65896  ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
65897  ** then there are not enough fields in the record to satisfy the
65898  ** request.  In this case, set the value NULL or to P4 if P4 is
65899  ** a pointer to a Mem object.
65900  */
65901  if( u.am.aOffset[u.am.p2] ){
65902    assert( rc==SQLITE_OK );
65903    if( u.am.zRec ){
65904      MemReleaseExt(u.am.pDest);
65905      sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65906    }else{
65907      u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65908      sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65909      rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
65910      if( rc!=SQLITE_OK ){
65911        goto op_column_out;
65912      }
65913      u.am.zData = u.am.sMem.z;
65914      sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
65915    }
65916    u.am.pDest->enc = encoding;
65917  }else{
65918    if( pOp->p4type==P4_MEM ){
65919      sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65920    }else{
65921      MemSetTypeFlag(u.am.pDest, MEM_Null);
65922    }
65923  }
65924
65925  /* If we dynamically allocated space to hold the data (in the
65926  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65927  ** dynamically allocated space over to the u.am.pDest structure.
65928  ** This prevents a memory copy.
65929  */
65930  if( u.am.sMem.zMalloc ){
65931    assert( u.am.sMem.z==u.am.sMem.zMalloc );
65932    assert( !(u.am.pDest->flags & MEM_Dyn) );
65933    assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
65934    u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65935    u.am.pDest->flags |= MEM_Term;
65936    u.am.pDest->z = u.am.sMem.z;
65937    u.am.pDest->zMalloc = u.am.sMem.zMalloc;
65938  }
65939
65940  rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
65941
65942op_column_out:
65943  UPDATE_MAX_BLOBSIZE(u.am.pDest);
65944  REGISTER_TRACE(pOp->p3, u.am.pDest);
65945  break;
65946}
65947
65948/* Opcode: Affinity P1 P2 * P4 *
65949**
65950** Apply affinities to a range of P2 registers starting with P1.
65951**
65952** P4 is a string that is P2 characters long. The nth character of the
65953** string indicates the column affinity that should be used for the nth
65954** memory cell in the range.
65955*/
65956case OP_Affinity: {
65957#if 0  /* local variables moved into u.an */
65958  const char *zAffinity;   /* The affinity to be applied */
65959  char cAff;               /* A single character of affinity */
65960#endif /* local variables moved into u.an */
65961
65962  u.an.zAffinity = pOp->p4.z;
65963  assert( u.an.zAffinity!=0 );
65964  assert( u.an.zAffinity[pOp->p2]==0 );
65965  pIn1 = &aMem[pOp->p1];
65966  while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){
65967    assert( pIn1 <= &p->aMem[p->nMem] );
65968    assert( memIsValid(pIn1) );
65969    ExpandBlob(pIn1);
65970    applyAffinity(pIn1, u.an.cAff, encoding);
65971    pIn1++;
65972  }
65973  break;
65974}
65975
65976/* Opcode: MakeRecord P1 P2 P3 P4 *
65977**
65978** Convert P2 registers beginning with P1 into the [record format]
65979** use as a data record in a database table or as a key
65980** in an index.  The OP_Column opcode can decode the record later.
65981**
65982** P4 may be a string that is P2 characters long.  The nth character of the
65983** string indicates the column affinity that should be used for the nth
65984** field of the index key.
65985**
65986** The mapping from character to affinity is given by the SQLITE_AFF_
65987** macros defined in sqliteInt.h.
65988**
65989** If P4 is NULL then all index fields have the affinity NONE.
65990*/
65991case OP_MakeRecord: {
65992#if 0  /* local variables moved into u.ao */
65993  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
65994  Mem *pRec;             /* The new record */
65995  u64 nData;             /* Number of bytes of data space */
65996  int nHdr;              /* Number of bytes of header space */
65997  i64 nByte;             /* Data space required for this record */
65998  int nZero;             /* Number of zero bytes at the end of the record */
65999  int nVarint;           /* Number of bytes in a varint */
66000  u32 serial_type;       /* Type field */
66001  Mem *pData0;           /* First field to be combined into the record */
66002  Mem *pLast;            /* Last field of the record */
66003  int nField;            /* Number of fields in the record */
66004  char *zAffinity;       /* The affinity string for the record */
66005  int file_format;       /* File format to use for encoding */
66006  int i;                 /* Space used in zNewRecord[] */
66007  int len;               /* Length of a field */
66008#endif /* local variables moved into u.ao */
66009
66010  /* Assuming the record contains N fields, the record format looks
66011  ** like this:
66012  **
66013  ** ------------------------------------------------------------------------
66014  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
66015  ** ------------------------------------------------------------------------
66016  **
66017  ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
66018  ** and so froth.
66019  **
66020  ** Each type field is a varint representing the serial type of the
66021  ** corresponding data element (see sqlite3VdbeSerialType()). The
66022  ** hdr-size field is also a varint which is the offset from the beginning
66023  ** of the record to data0.
66024  */
66025  u.ao.nData = 0;         /* Number of bytes of data space */
66026  u.ao.nHdr = 0;          /* Number of bytes of header space */
66027  u.ao.nZero = 0;         /* Number of zero bytes at the end of the record */
66028  u.ao.nField = pOp->p1;
66029  u.ao.zAffinity = pOp->p4.z;
66030  assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
66031  u.ao.pData0 = &aMem[u.ao.nField];
66032  u.ao.nField = pOp->p2;
66033  u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
66034  u.ao.file_format = p->minWriteFileFormat;
66035
66036  /* Identify the output register */
66037  assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
66038  pOut = &aMem[pOp->p3];
66039  memAboutToChange(p, pOut);
66040
66041  /* Loop through the elements that will make up the record to figure
66042  ** out how much space is required for the new record.
66043  */
66044  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
66045    assert( memIsValid(u.ao.pRec) );
66046    if( u.ao.zAffinity ){
66047      applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
66048    }
66049    if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
66050      sqlite3VdbeMemExpandBlob(u.ao.pRec);
66051    }
66052    u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
66053    u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
66054    u.ao.nData += u.ao.len;
66055    u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
66056    if( u.ao.pRec->flags & MEM_Zero ){
66057      /* Only pure zero-filled BLOBs can be input to this Opcode.
66058      ** We do not allow blobs with a prefix and a zero-filled tail. */
66059      u.ao.nZero += u.ao.pRec->u.nZero;
66060    }else if( u.ao.len ){
66061      u.ao.nZero = 0;
66062    }
66063  }
66064
66065  /* Add the initial header varint and total the size */
66066  u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
66067  if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
66068    u.ao.nHdr++;
66069  }
66070  u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
66071  if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66072    goto too_big;
66073  }
66074
66075  /* Make sure the output register has a buffer large enough to store
66076  ** the new record. The output register (pOp->p3) is not allowed to
66077  ** be one of the input registers (because the following call to
66078  ** sqlite3VdbeMemGrow() could clobber the value before it is used).
66079  */
66080  if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
66081    goto no_mem;
66082  }
66083  u.ao.zNewRecord = (u8 *)pOut->z;
66084
66085  /* Write the record */
66086  u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
66087  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
66088    u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
66089    u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type);      /* serial type */
66090  }
66091  for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){  /* serial data */
66092    u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
66093  }
66094  assert( u.ao.i==u.ao.nByte );
66095
66096  assert( pOp->p3>0 && pOp->p3<=p->nMem );
66097  pOut->n = (int)u.ao.nByte;
66098  pOut->flags = MEM_Blob | MEM_Dyn;
66099  pOut->xDel = 0;
66100  if( u.ao.nZero ){
66101    pOut->u.nZero = u.ao.nZero;
66102    pOut->flags |= MEM_Zero;
66103  }
66104  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
66105  REGISTER_TRACE(pOp->p3, pOut);
66106  UPDATE_MAX_BLOBSIZE(pOut);
66107  break;
66108}
66109
66110/* Opcode: Count P1 P2 * * *
66111**
66112** Store the number of entries (an integer value) in the table or index
66113** opened by cursor P1 in register P2
66114*/
66115#ifndef SQLITE_OMIT_BTREECOUNT
66116case OP_Count: {         /* out2-prerelease */
66117#if 0  /* local variables moved into u.ap */
66118  i64 nEntry;
66119  BtCursor *pCrsr;
66120#endif /* local variables moved into u.ap */
66121
66122  u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
66123  if( ALWAYS(u.ap.pCrsr) ){
66124    rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
66125  }else{
66126    u.ap.nEntry = 0;
66127  }
66128  pOut->u.i = u.ap.nEntry;
66129  break;
66130}
66131#endif
66132
66133/* Opcode: Savepoint P1 * * P4 *
66134**
66135** Open, release or rollback the savepoint named by parameter P4, depending
66136** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
66137** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
66138*/
66139case OP_Savepoint: {
66140#if 0  /* local variables moved into u.aq */
66141  int p1;                         /* Value of P1 operand */
66142  char *zName;                    /* Name of savepoint */
66143  int nName;
66144  Savepoint *pNew;
66145  Savepoint *pSavepoint;
66146  Savepoint *pTmp;
66147  int iSavepoint;
66148  int ii;
66149#endif /* local variables moved into u.aq */
66150
66151  u.aq.p1 = pOp->p1;
66152  u.aq.zName = pOp->p4.z;
66153
66154  /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
66155  ** transaction, then there cannot be any savepoints.
66156  */
66157  assert( db->pSavepoint==0 || db->autoCommit==0 );
66158  assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
66159  assert( db->pSavepoint || db->isTransactionSavepoint==0 );
66160  assert( checkSavepointCount(db) );
66161
66162  if( u.aq.p1==SAVEPOINT_BEGIN ){
66163    if( db->writeVdbeCnt>0 ){
66164      /* A new savepoint cannot be created if there are active write
66165      ** statements (i.e. open read/write incremental blob handles).
66166      */
66167      sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
66168        "SQL statements in progress");
66169      rc = SQLITE_BUSY;
66170    }else{
66171      u.aq.nName = sqlite3Strlen30(u.aq.zName);
66172
66173#ifndef SQLITE_OMIT_VIRTUALTABLE
66174      /* This call is Ok even if this savepoint is actually a transaction
66175      ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
66176      ** If this is a transaction savepoint being opened, it is guaranteed
66177      ** that the db->aVTrans[] array is empty.  */
66178      assert( db->autoCommit==0 || db->nVTrans==0 );
66179      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
66180                                db->nStatement+db->nSavepoint);
66181      if( rc!=SQLITE_OK ) goto abort_due_to_error;
66182#endif
66183
66184      /* Create a new savepoint structure. */
66185      u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
66186      if( u.aq.pNew ){
66187        u.aq.pNew->zName = (char *)&u.aq.pNew[1];
66188        memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
66189
66190        /* If there is no open transaction, then mark this as a special
66191        ** "transaction savepoint". */
66192        if( db->autoCommit ){
66193          db->autoCommit = 0;
66194          db->isTransactionSavepoint = 1;
66195        }else{
66196          db->nSavepoint++;
66197        }
66198
66199        /* Link the new savepoint into the database handle's list. */
66200        u.aq.pNew->pNext = db->pSavepoint;
66201        db->pSavepoint = u.aq.pNew;
66202        u.aq.pNew->nDeferredCons = db->nDeferredCons;
66203      }
66204    }
66205  }else{
66206    u.aq.iSavepoint = 0;
66207
66208    /* Find the named savepoint. If there is no such savepoint, then an
66209    ** an error is returned to the user.  */
66210    for(
66211      u.aq.pSavepoint = db->pSavepoint;
66212      u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
66213      u.aq.pSavepoint = u.aq.pSavepoint->pNext
66214    ){
66215      u.aq.iSavepoint++;
66216    }
66217    if( !u.aq.pSavepoint ){
66218      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
66219      rc = SQLITE_ERROR;
66220    }else if(
66221        db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
66222    ){
66223      /* It is not possible to release (commit) a savepoint if there are
66224      ** active write statements. It is not possible to rollback a savepoint
66225      ** if there are any active statements at all.
66226      */
66227      sqlite3SetString(&p->zErrMsg, db,
66228        "cannot %s savepoint - SQL statements in progress",
66229        (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
66230      );
66231      rc = SQLITE_BUSY;
66232    }else{
66233
66234      /* Determine whether or not this is a transaction savepoint. If so,
66235      ** and this is a RELEASE command, then the current transaction
66236      ** is committed.
66237      */
66238      int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66239      if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
66240        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66241          goto vdbe_return;
66242        }
66243        db->autoCommit = 1;
66244        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
66245          p->pc = pc;
66246          db->autoCommit = 0;
66247          p->rc = rc = SQLITE_BUSY;
66248          goto vdbe_return;
66249        }
66250        db->isTransactionSavepoint = 0;
66251        rc = p->rc;
66252      }else{
66253        u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
66254        for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
66255          rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
66256          if( rc!=SQLITE_OK ){
66257            goto abort_due_to_error;
66258          }
66259        }
66260        if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
66261          sqlite3ExpirePreparedStatements(db);
66262          sqlite3ResetInternalSchema(db, -1);
66263          db->flags = (db->flags | SQLITE_InternChanges);
66264        }
66265      }
66266
66267      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
66268      ** savepoints nested inside of the savepoint being operated on. */
66269      while( db->pSavepoint!=u.aq.pSavepoint ){
66270        u.aq.pTmp = db->pSavepoint;
66271        db->pSavepoint = u.aq.pTmp->pNext;
66272        sqlite3DbFree(db, u.aq.pTmp);
66273        db->nSavepoint--;
66274      }
66275
66276      /* If it is a RELEASE, then destroy the savepoint being operated on
66277      ** too. If it is a ROLLBACK TO, then set the number of deferred
66278      ** constraint violations present in the database to the value stored
66279      ** when the savepoint was created.  */
66280      if( u.aq.p1==SAVEPOINT_RELEASE ){
66281        assert( u.aq.pSavepoint==db->pSavepoint );
66282        db->pSavepoint = u.aq.pSavepoint->pNext;
66283        sqlite3DbFree(db, u.aq.pSavepoint);
66284        if( !isTransaction ){
66285          db->nSavepoint--;
66286        }
66287      }else{
66288        db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
66289      }
66290
66291      if( !isTransaction ){
66292        rc = sqlite3VtabSavepoint(db, u.aq.p1, u.aq.iSavepoint);
66293        if( rc!=SQLITE_OK ) goto abort_due_to_error;
66294      }
66295    }
66296  }
66297
66298  break;
66299}
66300
66301/* Opcode: AutoCommit P1 P2 * * *
66302**
66303** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
66304** back any currently active btree transactions. If there are any active
66305** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
66306** there are active writing VMs or active VMs that use shared cache.
66307**
66308** This instruction causes the VM to halt.
66309*/
66310case OP_AutoCommit: {
66311#if 0  /* local variables moved into u.ar */
66312  int desiredAutoCommit;
66313  int iRollback;
66314  int turnOnAC;
66315#endif /* local variables moved into u.ar */
66316
66317  u.ar.desiredAutoCommit = pOp->p1;
66318  u.ar.iRollback = pOp->p2;
66319  u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
66320  assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
66321  assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
66322  assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
66323
66324  if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
66325    /* If this instruction implements a ROLLBACK and other VMs are
66326    ** still running, and a transaction is active, return an error indicating
66327    ** that the other VMs must complete first.
66328    */
66329    sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
66330        "SQL statements in progress");
66331    rc = SQLITE_BUSY;
66332  }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
66333    /* If this instruction implements a COMMIT and other VMs are writing
66334    ** return an error indicating that the other VMs must complete first.
66335    */
66336    sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
66337        "SQL statements in progress");
66338    rc = SQLITE_BUSY;
66339  }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
66340    if( u.ar.iRollback ){
66341      assert( u.ar.desiredAutoCommit==1 );
66342      sqlite3RollbackAll(db);
66343      db->autoCommit = 1;
66344    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66345      goto vdbe_return;
66346    }else{
66347      db->autoCommit = (u8)u.ar.desiredAutoCommit;
66348      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
66349        p->pc = pc;
66350        db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
66351        p->rc = rc = SQLITE_BUSY;
66352        goto vdbe_return;
66353      }
66354    }
66355    assert( db->nStatement==0 );
66356    sqlite3CloseSavepoints(db);
66357    if( p->rc==SQLITE_OK ){
66358      rc = SQLITE_DONE;
66359    }else{
66360      rc = SQLITE_ERROR;
66361    }
66362    goto vdbe_return;
66363  }else{
66364    sqlite3SetString(&p->zErrMsg, db,
66365        (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66366        (u.ar.iRollback)?"cannot rollback - no transaction is active":
66367                   "cannot commit - no transaction is active"));
66368
66369    rc = SQLITE_ERROR;
66370  }
66371  break;
66372}
66373
66374/* Opcode: Transaction P1 P2 * * *
66375**
66376** Begin a transaction.  The transaction ends when a Commit or Rollback
66377** opcode is encountered.  Depending on the ON CONFLICT setting, the
66378** transaction might also be rolled back if an error is encountered.
66379**
66380** P1 is the index of the database file on which the transaction is
66381** started.  Index 0 is the main database file and index 1 is the
66382** file used for temporary tables.  Indices of 2 or more are used for
66383** attached databases.
66384**
66385** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
66386** obtained on the database file when a write-transaction is started.  No
66387** other process can start another write transaction while this transaction is
66388** underway.  Starting a write transaction also creates a rollback journal. A
66389** write transaction must be started before any changes can be made to the
66390** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
66391** on the file.
66392**
66393** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
66394** true (this flag is set if the Vdbe may modify more than one row and may
66395** throw an ABORT exception), a statement transaction may also be opened.
66396** More specifically, a statement transaction is opened iff the database
66397** connection is currently not in autocommit mode, or if there are other
66398** active statements. A statement transaction allows the affects of this
66399** VDBE to be rolled back after an error without having to roll back the
66400** entire transaction. If no error is encountered, the statement transaction
66401** will automatically commit when the VDBE halts.
66402**
66403** If P2 is zero, then a read-lock is obtained on the database file.
66404*/
66405case OP_Transaction: {
66406#if 0  /* local variables moved into u.as */
66407  Btree *pBt;
66408#endif /* local variables moved into u.as */
66409
66410  assert( pOp->p1>=0 && pOp->p1<db->nDb );
66411  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66412  u.as.pBt = db->aDb[pOp->p1].pBt;
66413
66414  if( u.as.pBt ){
66415    rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
66416    if( rc==SQLITE_BUSY ){
66417      p->pc = pc;
66418      p->rc = rc = SQLITE_BUSY;
66419      goto vdbe_return;
66420    }
66421    if( rc!=SQLITE_OK ){
66422      goto abort_due_to_error;
66423    }
66424
66425    if( pOp->p2 && p->usesStmtJournal
66426     && (db->autoCommit==0 || db->activeVdbeCnt>1)
66427    ){
66428      assert( sqlite3BtreeIsInTrans(u.as.pBt) );
66429      if( p->iStatement==0 ){
66430        assert( db->nStatement>=0 && db->nSavepoint>=0 );
66431        db->nStatement++;
66432        p->iStatement = db->nSavepoint + db->nStatement;
66433      }
66434
66435      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
66436      if( rc==SQLITE_OK ){
66437        rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
66438      }
66439
66440      /* Store the current value of the database handles deferred constraint
66441      ** counter. If the statement transaction needs to be rolled back,
66442      ** the value of this counter needs to be restored too.  */
66443      p->nStmtDefCons = db->nDeferredCons;
66444    }
66445  }
66446  break;
66447}
66448
66449/* Opcode: ReadCookie P1 P2 P3 * *
66450**
66451** Read cookie number P3 from database P1 and write it into register P2.
66452** P3==1 is the schema version.  P3==2 is the database format.
66453** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
66454** the main database file and P1==1 is the database file used to store
66455** temporary tables.
66456**
66457** There must be a read-lock on the database (either a transaction
66458** must be started or there must be an open cursor) before
66459** executing this instruction.
66460*/
66461case OP_ReadCookie: {               /* out2-prerelease */
66462#if 0  /* local variables moved into u.at */
66463  int iMeta;
66464  int iDb;
66465  int iCookie;
66466#endif /* local variables moved into u.at */
66467
66468  u.at.iDb = pOp->p1;
66469  u.at.iCookie = pOp->p3;
66470  assert( pOp->p3<SQLITE_N_BTREE_META );
66471  assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
66472  assert( db->aDb[u.at.iDb].pBt!=0 );
66473  assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
66474
66475  sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
66476  pOut->u.i = u.at.iMeta;
66477  break;
66478}
66479
66480/* Opcode: SetCookie P1 P2 P3 * *
66481**
66482** Write the content of register P3 (interpreted as an integer)
66483** into cookie number P2 of database P1.  P2==1 is the schema version.
66484** P2==2 is the database format. P2==3 is the recommended pager cache
66485** size, and so forth.  P1==0 is the main database file and P1==1 is the
66486** database file used to store temporary tables.
66487**
66488** A transaction must be started before executing this opcode.
66489*/
66490case OP_SetCookie: {       /* in3 */
66491#if 0  /* local variables moved into u.au */
66492  Db *pDb;
66493#endif /* local variables moved into u.au */
66494  assert( pOp->p2<SQLITE_N_BTREE_META );
66495  assert( pOp->p1>=0 && pOp->p1<db->nDb );
66496  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66497  u.au.pDb = &db->aDb[pOp->p1];
66498  assert( u.au.pDb->pBt!=0 );
66499  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66500  pIn3 = &aMem[pOp->p3];
66501  sqlite3VdbeMemIntegerify(pIn3);
66502  /* See note about index shifting on OP_ReadCookie */
66503  rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
66504  if( pOp->p2==BTREE_SCHEMA_VERSION ){
66505    /* When the schema cookie changes, record the new cookie internally */
66506    u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
66507    db->flags |= SQLITE_InternChanges;
66508  }else if( pOp->p2==BTREE_FILE_FORMAT ){
66509    /* Record changes in the file format */
66510    u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
66511  }
66512  if( pOp->p1==1 ){
66513    /* Invalidate all prepared statements whenever the TEMP database
66514    ** schema is changed.  Ticket #1644 */
66515    sqlite3ExpirePreparedStatements(db);
66516    p->expired = 0;
66517  }
66518  break;
66519}
66520
66521/* Opcode: VerifyCookie P1 P2 P3 * *
66522**
66523** Check the value of global database parameter number 0 (the
66524** schema version) and make sure it is equal to P2 and that the
66525** generation counter on the local schema parse equals P3.
66526**
66527** P1 is the database number which is 0 for the main database file
66528** and 1 for the file holding temporary tables and some higher number
66529** for auxiliary databases.
66530**
66531** The cookie changes its value whenever the database schema changes.
66532** This operation is used to detect when that the cookie has changed
66533** and that the current process needs to reread the schema.
66534**
66535** Either a transaction needs to have been started or an OP_Open needs
66536** to be executed (to establish a read lock) before this opcode is
66537** invoked.
66538*/
66539case OP_VerifyCookie: {
66540#if 0  /* local variables moved into u.av */
66541  int iMeta;
66542  int iGen;
66543  Btree *pBt;
66544#endif /* local variables moved into u.av */
66545
66546  assert( pOp->p1>=0 && pOp->p1<db->nDb );
66547  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66548  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66549  u.av.pBt = db->aDb[pOp->p1].pBt;
66550  if( u.av.pBt ){
66551    sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
66552    u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
66553  }else{
66554    u.av.iGen = u.av.iMeta = 0;
66555  }
66556  if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
66557    sqlite3DbFree(db, p->zErrMsg);
66558    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
66559    /* If the schema-cookie from the database file matches the cookie
66560    ** stored with the in-memory representation of the schema, do
66561    ** not reload the schema from the database file.
66562    **
66563    ** If virtual-tables are in use, this is not just an optimization.
66564    ** Often, v-tables store their data in other SQLite tables, which
66565    ** are queried from within xNext() and other v-table methods using
66566    ** prepared queries. If such a query is out-of-date, we do not want to
66567    ** discard the database schema, as the user code implementing the
66568    ** v-table would have to be ready for the sqlite3_vtab structure itself
66569    ** to be invalidated whenever sqlite3_step() is called from within
66570    ** a v-table method.
66571    */
66572    if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
66573      sqlite3ResetInternalSchema(db, pOp->p1);
66574    }
66575
66576    p->expired = 1;
66577    rc = SQLITE_SCHEMA;
66578  }
66579  break;
66580}
66581
66582/* Opcode: OpenRead P1 P2 P3 P4 P5
66583**
66584** Open a read-only cursor for the database table whose root page is
66585** P2 in a database file.  The database file is determined by P3.
66586** P3==0 means the main database, P3==1 means the database used for
66587** temporary tables, and P3>1 means used the corresponding attached
66588** database.  Give the new cursor an identifier of P1.  The P1
66589** values need not be contiguous but all P1 values should be small integers.
66590** It is an error for P1 to be negative.
66591**
66592** If P5!=0 then use the content of register P2 as the root page, not
66593** the value of P2 itself.
66594**
66595** There will be a read lock on the database whenever there is an
66596** open cursor.  If the database was unlocked prior to this instruction
66597** then a read lock is acquired as part of this instruction.  A read
66598** lock allows other processes to read the database but prohibits
66599** any other process from modifying the database.  The read lock is
66600** released when all cursors are closed.  If this instruction attempts
66601** to get a read lock but fails, the script terminates with an
66602** SQLITE_BUSY error code.
66603**
66604** The P4 value may be either an integer (P4_INT32) or a pointer to
66605** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
66606** structure, then said structure defines the content and collating
66607** sequence of the index being opened. Otherwise, if P4 is an integer
66608** value, it is set to the number of columns in the table.
66609**
66610** See also OpenWrite.
66611*/
66612/* Opcode: OpenWrite P1 P2 P3 P4 P5
66613**
66614** Open a read/write cursor named P1 on the table or index whose root
66615** page is P2.  Or if P5!=0 use the content of register P2 to find the
66616** root page.
66617**
66618** The P4 value may be either an integer (P4_INT32) or a pointer to
66619** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
66620** structure, then said structure defines the content and collating
66621** sequence of the index being opened. Otherwise, if P4 is an integer
66622** value, it is set to the number of columns in the table, or to the
66623** largest index of any column of the table that is actually used.
66624**
66625** This instruction works just like OpenRead except that it opens the cursor
66626** in read/write mode.  For a given table, there can be one or more read-only
66627** cursors or a single read/write cursor but not both.
66628**
66629** See also OpenRead.
66630*/
66631case OP_OpenRead:
66632case OP_OpenWrite: {
66633#if 0  /* local variables moved into u.aw */
66634  int nField;
66635  KeyInfo *pKeyInfo;
66636  int p2;
66637  int iDb;
66638  int wrFlag;
66639  Btree *pX;
66640  VdbeCursor *pCur;
66641  Db *pDb;
66642#endif /* local variables moved into u.aw */
66643
66644  if( p->expired ){
66645    rc = SQLITE_ABORT;
66646    break;
66647  }
66648
66649  u.aw.nField = 0;
66650  u.aw.pKeyInfo = 0;
66651  u.aw.p2 = pOp->p2;
66652  u.aw.iDb = pOp->p3;
66653  assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
66654  assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
66655  u.aw.pDb = &db->aDb[u.aw.iDb];
66656  u.aw.pX = u.aw.pDb->pBt;
66657  assert( u.aw.pX!=0 );
66658  if( pOp->opcode==OP_OpenWrite ){
66659    u.aw.wrFlag = 1;
66660    assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
66661    if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
66662      p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
66663    }
66664  }else{
66665    u.aw.wrFlag = 0;
66666  }
66667  if( pOp->p5 ){
66668    assert( u.aw.p2>0 );
66669    assert( u.aw.p2<=p->nMem );
66670    pIn2 = &aMem[u.aw.p2];
66671    assert( memIsValid(pIn2) );
66672    assert( (pIn2->flags & MEM_Int)!=0 );
66673    sqlite3VdbeMemIntegerify(pIn2);
66674    u.aw.p2 = (int)pIn2->u.i;
66675    /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
66676    ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
66677    ** If there were a failure, the prepared statement would have halted
66678    ** before reaching this instruction. */
66679    if( NEVER(u.aw.p2<2) ) {
66680      rc = SQLITE_CORRUPT_BKPT;
66681      goto abort_due_to_error;
66682    }
66683  }
66684  if( pOp->p4type==P4_KEYINFO ){
66685    u.aw.pKeyInfo = pOp->p4.pKeyInfo;
66686    u.aw.pKeyInfo->enc = ENC(p->db);
66687    u.aw.nField = u.aw.pKeyInfo->nField+1;
66688  }else if( pOp->p4type==P4_INT32 ){
66689    u.aw.nField = pOp->p4.i;
66690  }
66691  assert( pOp->p1>=0 );
66692  u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
66693  if( u.aw.pCur==0 ) goto no_mem;
66694  u.aw.pCur->nullRow = 1;
66695  u.aw.pCur->isOrdered = 1;
66696  rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66697  u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66698
66699  /* Since it performs no memory allocation or IO, the only value that
66700  ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66701  assert( rc==SQLITE_OK );
66702
66703  /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66704  ** SQLite used to check if the root-page flags were sane at this point
66705  ** and report database corruption if they were not, but this check has
66706  ** since moved into the btree layer.  */
66707  u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66708  u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66709  break;
66710}
66711
66712/* Opcode: OpenEphemeral P1 P2 * P4 P5
66713**
66714** Open a new cursor P1 to a transient table.
66715** The cursor is always opened read/write even if
66716** the main database is read-only.  The ephemeral
66717** table is deleted automatically when the cursor is closed.
66718**
66719** P2 is the number of columns in the ephemeral table.
66720** The cursor points to a BTree table if P4==0 and to a BTree index
66721** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
66722** that defines the format of keys in the index.
66723**
66724** This opcode was once called OpenTemp.  But that created
66725** confusion because the term "temp table", might refer either
66726** to a TEMP table at the SQL level, or to a table opened by
66727** this opcode.  Then this opcode was call OpenVirtual.  But
66728** that created confusion with the whole virtual-table idea.
66729**
66730** The P5 parameter can be a mask of the BTREE_* flags defined
66731** in btree.h.  These flags control aspects of the operation of
66732** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
66733** added automatically.
66734*/
66735/* Opcode: OpenAutoindex P1 P2 * P4 *
66736**
66737** This opcode works the same as OP_OpenEphemeral.  It has a
66738** different name to distinguish its use.  Tables created using
66739** by this opcode will be used for automatically created transient
66740** indices in joins.
66741*/
66742case OP_OpenAutoindex:
66743case OP_OpenEphemeral: {
66744#if 0  /* local variables moved into u.ax */
66745  VdbeCursor *pCx;
66746#endif /* local variables moved into u.ax */
66747  static const int vfsFlags =
66748      SQLITE_OPEN_READWRITE |
66749      SQLITE_OPEN_CREATE |
66750      SQLITE_OPEN_EXCLUSIVE |
66751      SQLITE_OPEN_DELETEONCLOSE |
66752      SQLITE_OPEN_TRANSIENT_DB;
66753
66754  assert( pOp->p1>=0 );
66755  u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66756  if( u.ax.pCx==0 ) goto no_mem;
66757  u.ax.pCx->nullRow = 1;
66758  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
66759                        BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
66760  if( rc==SQLITE_OK ){
66761    rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
66762  }
66763  if( rc==SQLITE_OK ){
66764    /* If a transient index is required, create it by calling
66765    ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
66766    ** opening it. If a transient table is required, just use the
66767    ** automatically created table with root-page 1 (an BLOB_INTKEY table).
66768    */
66769    if( pOp->p4.pKeyInfo ){
66770      int pgno;
66771      assert( pOp->p4type==P4_KEYINFO );
66772      rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
66773      if( rc==SQLITE_OK ){
66774        assert( pgno==MASTER_ROOT+1 );
66775        rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
66776                                (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
66777        u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66778        u.ax.pCx->pKeyInfo->enc = ENC(p->db);
66779      }
66780      u.ax.pCx->isTable = 0;
66781    }else{
66782      rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
66783      u.ax.pCx->isTable = 1;
66784    }
66785  }
66786  u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66787  u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66788  break;
66789}
66790
66791/* Opcode: OpenSorter P1 P2 * P4 *
66792**
66793** This opcode works like OP_OpenEphemeral except that it opens
66794** a transient index that is specifically designed to sort large
66795** tables using an external merge-sort algorithm.
66796*/
66797case OP_SorterOpen: {
66798#if 0  /* local variables moved into u.ay */
66799  VdbeCursor *pCx;
66800#endif /* local variables moved into u.ay */
66801#ifndef SQLITE_OMIT_MERGE_SORT
66802  u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66803  if( u.ay.pCx==0 ) goto no_mem;
66804  u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66805  u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66806  u.ay.pCx->isSorter = 1;
66807  rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
66808#else
66809  pOp->opcode = OP_OpenEphemeral;
66810  pc--;
66811#endif
66812  break;
66813}
66814
66815/* Opcode: OpenPseudo P1 P2 P3 * *
66816**
66817** Open a new cursor that points to a fake table that contains a single
66818** row of data.  The content of that one row in the content of memory
66819** register P2.  In other words, cursor P1 becomes an alias for the
66820** MEM_Blob content contained in register P2.
66821**
66822** A pseudo-table created by this opcode is used to hold a single
66823** row output from the sorter so that the row can be decomposed into
66824** individual columns using the OP_Column opcode.  The OP_Column opcode
66825** is the only cursor opcode that works with a pseudo-table.
66826**
66827** P3 is the number of fields in the records that will be stored by
66828** the pseudo-table.
66829*/
66830case OP_OpenPseudo: {
66831#if 0  /* local variables moved into u.az */
66832  VdbeCursor *pCx;
66833#endif /* local variables moved into u.az */
66834
66835  assert( pOp->p1>=0 );
66836  u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66837  if( u.az.pCx==0 ) goto no_mem;
66838  u.az.pCx->nullRow = 1;
66839  u.az.pCx->pseudoTableReg = pOp->p2;
66840  u.az.pCx->isTable = 1;
66841  u.az.pCx->isIndex = 0;
66842  break;
66843}
66844
66845/* Opcode: Close P1 * * * *
66846**
66847** Close a cursor previously opened as P1.  If P1 is not
66848** currently open, this instruction is a no-op.
66849*/
66850case OP_Close: {
66851  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66852  sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
66853  p->apCsr[pOp->p1] = 0;
66854  break;
66855}
66856
66857/* Opcode: SeekGe P1 P2 P3 P4 *
66858**
66859** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66860** use the value in register P3 as the key.  If cursor P1 refers
66861** to an SQL index, then P3 is the first in an array of P4 registers
66862** that are used as an unpacked index key.
66863**
66864** Reposition cursor P1 so that  it points to the smallest entry that
66865** is greater than or equal to the key value. If there are no records
66866** greater than or equal to the key and P2 is not zero, then jump to P2.
66867**
66868** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
66869*/
66870/* Opcode: SeekGt P1 P2 P3 P4 *
66871**
66872** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66873** use the value in register P3 as a key. If cursor P1 refers
66874** to an SQL index, then P3 is the first in an array of P4 registers
66875** that are used as an unpacked index key.
66876**
66877** Reposition cursor P1 so that  it points to the smallest entry that
66878** is greater than the key value. If there are no records greater than
66879** the key and P2 is not zero, then jump to P2.
66880**
66881** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
66882*/
66883/* Opcode: SeekLt P1 P2 P3 P4 *
66884**
66885** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66886** use the value in register P3 as a key. If cursor P1 refers
66887** to an SQL index, then P3 is the first in an array of P4 registers
66888** that are used as an unpacked index key.
66889**
66890** Reposition cursor P1 so that  it points to the largest entry that
66891** is less than the key value. If there are no records less than
66892** the key and P2 is not zero, then jump to P2.
66893**
66894** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
66895*/
66896/* Opcode: SeekLe P1 P2 P3 P4 *
66897**
66898** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
66899** use the value in register P3 as a key. If cursor P1 refers
66900** to an SQL index, then P3 is the first in an array of P4 registers
66901** that are used as an unpacked index key.
66902**
66903** Reposition cursor P1 so that it points to the largest entry that
66904** is less than or equal to the key value. If there are no records
66905** less than or equal to the key and P2 is not zero, then jump to P2.
66906**
66907** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
66908*/
66909case OP_SeekLt:         /* jump, in3 */
66910case OP_SeekLe:         /* jump, in3 */
66911case OP_SeekGe:         /* jump, in3 */
66912case OP_SeekGt: {       /* jump, in3 */
66913#if 0  /* local variables moved into u.ba */
66914  int res;
66915  int oc;
66916  VdbeCursor *pC;
66917  UnpackedRecord r;
66918  int nField;
66919  i64 iKey;      /* The rowid we are to seek to */
66920#endif /* local variables moved into u.ba */
66921
66922  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66923  assert( pOp->p2!=0 );
66924  u.ba.pC = p->apCsr[pOp->p1];
66925  assert( u.ba.pC!=0 );
66926  assert( u.ba.pC->pseudoTableReg==0 );
66927  assert( OP_SeekLe == OP_SeekLt+1 );
66928  assert( OP_SeekGe == OP_SeekLt+2 );
66929  assert( OP_SeekGt == OP_SeekLt+3 );
66930  assert( u.ba.pC->isOrdered );
66931  if( ALWAYS(u.ba.pC->pCursor!=0) ){
66932    u.ba.oc = pOp->opcode;
66933    u.ba.pC->nullRow = 0;
66934    if( u.ba.pC->isTable ){
66935      /* The input value in P3 might be of any type: integer, real, string,
66936      ** blob, or NULL.  But it needs to be an integer before we can do
66937      ** the seek, so covert it. */
66938      pIn3 = &aMem[pOp->p3];
66939      applyNumericAffinity(pIn3);
66940      u.ba.iKey = sqlite3VdbeIntValue(pIn3);
66941      u.ba.pC->rowidIsValid = 0;
66942
66943      /* If the P3 value could not be converted into an integer without
66944      ** loss of information, then special processing is required... */
66945      if( (pIn3->flags & MEM_Int)==0 ){
66946        if( (pIn3->flags & MEM_Real)==0 ){
66947          /* If the P3 value cannot be converted into any kind of a number,
66948          ** then the seek is not possible, so jump to P2 */
66949          pc = pOp->p2 - 1;
66950          break;
66951        }
66952        /* If we reach this point, then the P3 value must be a floating
66953        ** point number. */
66954        assert( (pIn3->flags & MEM_Real)!=0 );
66955
66956        if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
66957          /* The P3 value is too large in magnitude to be expressed as an
66958          ** integer. */
66959          u.ba.res = 1;
66960          if( pIn3->r<0 ){
66961            if( u.ba.oc>=OP_SeekGe ){  assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66962              rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
66963              if( rc!=SQLITE_OK ) goto abort_due_to_error;
66964            }
66965          }else{
66966            if( u.ba.oc<=OP_SeekLe ){  assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66967              rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
66968              if( rc!=SQLITE_OK ) goto abort_due_to_error;
66969            }
66970          }
66971          if( u.ba.res ){
66972            pc = pOp->p2 - 1;
66973          }
66974          break;
66975        }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
66976          /* Use the ceiling() function to convert real->int */
66977          if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
66978        }else{
66979          /* Use the floor() function to convert real->int */
66980          assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
66981          if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
66982        }
66983      }
66984      rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
66985      if( rc!=SQLITE_OK ){
66986        goto abort_due_to_error;
66987      }
66988      if( u.ba.res==0 ){
66989        u.ba.pC->rowidIsValid = 1;
66990        u.ba.pC->lastRowid = u.ba.iKey;
66991      }
66992    }else{
66993      u.ba.nField = pOp->p4.i;
66994      assert( pOp->p4type==P4_INT32 );
66995      assert( u.ba.nField>0 );
66996      u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
66997      u.ba.r.nField = (u16)u.ba.nField;
66998
66999      /* The next line of code computes as follows, only faster:
67000      **   if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
67001      **     u.ba.r.flags = UNPACKED_INCRKEY;
67002      **   }else{
67003      **     u.ba.r.flags = 0;
67004      **   }
67005      */
67006      u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
67007      assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
67008      assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
67009      assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
67010      assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
67011
67012      u.ba.r.aMem = &aMem[pOp->p3];
67013#ifdef SQLITE_DEBUG
67014      { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
67015#endif
67016      ExpandBlob(u.ba.r.aMem);
67017      rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
67018      if( rc!=SQLITE_OK ){
67019        goto abort_due_to_error;
67020      }
67021      u.ba.pC->rowidIsValid = 0;
67022    }
67023    u.ba.pC->deferredMoveto = 0;
67024    u.ba.pC->cacheStatus = CACHE_STALE;
67025#ifdef SQLITE_TEST
67026    sqlite3_search_count++;
67027#endif
67028    if( u.ba.oc>=OP_SeekGe ){  assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
67029      if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
67030        rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
67031        if( rc!=SQLITE_OK ) goto abort_due_to_error;
67032        u.ba.pC->rowidIsValid = 0;
67033      }else{
67034        u.ba.res = 0;
67035      }
67036    }else{
67037      assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
67038      if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
67039        rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
67040        if( rc!=SQLITE_OK ) goto abort_due_to_error;
67041        u.ba.pC->rowidIsValid = 0;
67042      }else{
67043        /* u.ba.res might be negative because the table is empty.  Check to
67044        ** see if this is the case.
67045        */
67046        u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
67047      }
67048    }
67049    assert( pOp->p2>0 );
67050    if( u.ba.res ){
67051      pc = pOp->p2 - 1;
67052    }
67053  }else{
67054    /* This happens when attempting to open the sqlite3_master table
67055    ** for read access returns SQLITE_EMPTY. In this case always
67056    ** take the jump (since there are no records in the table).
67057    */
67058    pc = pOp->p2 - 1;
67059  }
67060  break;
67061}
67062
67063/* Opcode: Seek P1 P2 * * *
67064**
67065** P1 is an open table cursor and P2 is a rowid integer.  Arrange
67066** for P1 to move so that it points to the rowid given by P2.
67067**
67068** This is actually a deferred seek.  Nothing actually happens until
67069** the cursor is used to read a record.  That way, if no reads
67070** occur, no unnecessary I/O happens.
67071*/
67072case OP_Seek: {    /* in2 */
67073#if 0  /* local variables moved into u.bb */
67074  VdbeCursor *pC;
67075#endif /* local variables moved into u.bb */
67076
67077  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67078  u.bb.pC = p->apCsr[pOp->p1];
67079  assert( u.bb.pC!=0 );
67080  if( ALWAYS(u.bb.pC->pCursor!=0) ){
67081    assert( u.bb.pC->isTable );
67082    u.bb.pC->nullRow = 0;
67083    pIn2 = &aMem[pOp->p2];
67084    u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67085    u.bb.pC->rowidIsValid = 0;
67086    u.bb.pC->deferredMoveto = 1;
67087  }
67088  break;
67089}
67090
67091
67092/* Opcode: Found P1 P2 P3 P4 *
67093**
67094** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
67095** P4>0 then register P3 is the first of P4 registers that form an unpacked
67096** record.
67097**
67098** Cursor P1 is on an index btree.  If the record identified by P3 and P4
67099** is a prefix of any entry in P1 then a jump is made to P2 and
67100** P1 is left pointing at the matching entry.
67101*/
67102/* Opcode: NotFound P1 P2 P3 P4 *
67103**
67104** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
67105** P4>0 then register P3 is the first of P4 registers that form an unpacked
67106** record.
67107**
67108** Cursor P1 is on an index btree.  If the record identified by P3 and P4
67109** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
67110** does contain an entry whose prefix matches the P3/P4 record then control
67111** falls through to the next instruction and P1 is left pointing at the
67112** matching entry.
67113**
67114** See also: Found, NotExists, IsUnique
67115*/
67116case OP_NotFound:       /* jump, in3 */
67117case OP_Found: {        /* jump, in3 */
67118#if 0  /* local variables moved into u.bc */
67119  int alreadyExists;
67120  VdbeCursor *pC;
67121  int res;
67122  char *pFree;
67123  UnpackedRecord *pIdxKey;
67124  UnpackedRecord r;
67125  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67126#endif /* local variables moved into u.bc */
67127
67128#ifdef SQLITE_TEST
67129  sqlite3_found_count++;
67130#endif
67131
67132  u.bc.alreadyExists = 0;
67133  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67134  assert( pOp->p4type==P4_INT32 );
67135  u.bc.pC = p->apCsr[pOp->p1];
67136  assert( u.bc.pC!=0 );
67137  pIn3 = &aMem[pOp->p3];
67138  if( ALWAYS(u.bc.pC->pCursor!=0) ){
67139
67140    assert( u.bc.pC->isTable==0 );
67141    if( pOp->p4.i>0 ){
67142      u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67143      u.bc.r.nField = (u16)pOp->p4.i;
67144      u.bc.r.aMem = pIn3;
67145#ifdef SQLITE_DEBUG
67146      { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67147#endif
67148      u.bc.r.flags = UNPACKED_PREFIX_MATCH;
67149      u.bc.pIdxKey = &u.bc.r;
67150    }else{
67151      u.bc.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67152          u.bc.pC->pKeyInfo, u.bc.aTempRec, sizeof(u.bc.aTempRec), &u.bc.pFree
67153      );
67154      if( u.bc.pIdxKey==0 ) goto no_mem;
67155      assert( pIn3->flags & MEM_Blob );
67156      assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
67157      sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z, u.bc.pIdxKey);
67158      u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67159    }
67160    rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
67161    if( pOp->p4.i==0 ){
67162      sqlite3DbFree(db, u.bc.pFree);
67163    }
67164    if( rc!=SQLITE_OK ){
67165      break;
67166    }
67167    u.bc.alreadyExists = (u.bc.res==0);
67168    u.bc.pC->deferredMoveto = 0;
67169    u.bc.pC->cacheStatus = CACHE_STALE;
67170  }
67171  if( pOp->opcode==OP_Found ){
67172    if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
67173  }else{
67174    if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
67175  }
67176  break;
67177}
67178
67179/* Opcode: IsUnique P1 P2 P3 P4 *
67180**
67181** Cursor P1 is open on an index b-tree - that is to say, a btree which
67182** no data and where the key are records generated by OP_MakeRecord with
67183** the list field being the integer ROWID of the entry that the index
67184** entry refers to.
67185**
67186** The P3 register contains an integer record number. Call this record
67187** number R. Register P4 is the first in a set of N contiguous registers
67188** that make up an unpacked index key that can be used with cursor P1.
67189** The value of N can be inferred from the cursor. N includes the rowid
67190** value appended to the end of the index record. This rowid value may
67191** or may not be the same as R.
67192**
67193** If any of the N registers beginning with register P4 contains a NULL
67194** value, jump immediately to P2.
67195**
67196** Otherwise, this instruction checks if cursor P1 contains an entry
67197** where the first (N-1) fields match but the rowid value at the end
67198** of the index entry is not R. If there is no such entry, control jumps
67199** to instruction P2. Otherwise, the rowid of the conflicting index
67200** entry is copied to register P3 and control falls through to the next
67201** instruction.
67202**
67203** See also: NotFound, NotExists, Found
67204*/
67205case OP_IsUnique: {        /* jump, in3 */
67206#if 0  /* local variables moved into u.bd */
67207  u16 ii;
67208  VdbeCursor *pCx;
67209  BtCursor *pCrsr;
67210  u16 nField;
67211  Mem *aMx;
67212  UnpackedRecord r;                  /* B-Tree index search key */
67213  i64 R;                             /* Rowid stored in register P3 */
67214#endif /* local variables moved into u.bd */
67215
67216  pIn3 = &aMem[pOp->p3];
67217  u.bd.aMx = &aMem[pOp->p4.i];
67218  /* Assert that the values of parameters P1 and P4 are in range. */
67219  assert( pOp->p4type==P4_INT32 );
67220  assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
67221  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67222
67223  /* Find the index cursor. */
67224  u.bd.pCx = p->apCsr[pOp->p1];
67225  assert( u.bd.pCx->deferredMoveto==0 );
67226  u.bd.pCx->seekResult = 0;
67227  u.bd.pCx->cacheStatus = CACHE_STALE;
67228  u.bd.pCrsr = u.bd.pCx->pCursor;
67229
67230  /* If any of the values are NULL, take the jump. */
67231  u.bd.nField = u.bd.pCx->pKeyInfo->nField;
67232  for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
67233    if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
67234      pc = pOp->p2 - 1;
67235      u.bd.pCrsr = 0;
67236      break;
67237    }
67238  }
67239  assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
67240
67241  if( u.bd.pCrsr!=0 ){
67242    /* Populate the index search key. */
67243    u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
67244    u.bd.r.nField = u.bd.nField + 1;
67245    u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
67246    u.bd.r.aMem = u.bd.aMx;
67247#ifdef SQLITE_DEBUG
67248    { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
67249#endif
67250
67251    /* Extract the value of u.bd.R from register P3. */
67252    sqlite3VdbeMemIntegerify(pIn3);
67253    u.bd.R = pIn3->u.i;
67254
67255    /* Search the B-Tree index. If no conflicting record is found, jump
67256    ** to P2. Otherwise, copy the rowid of the conflicting record to
67257    ** register P3 and fall through to the next instruction.  */
67258    rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
67259    if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
67260      pc = pOp->p2 - 1;
67261    }else{
67262      pIn3->u.i = u.bd.r.rowid;
67263    }
67264  }
67265  break;
67266}
67267
67268/* Opcode: NotExists P1 P2 P3 * *
67269**
67270** Use the content of register P3 as an integer key.  If a record
67271** with that key does not exist in table of P1, then jump to P2.
67272** If the record does exist, then fall through.  The cursor is left
67273** pointing to the record if it exists.
67274**
67275** The difference between this operation and NotFound is that this
67276** operation assumes the key is an integer and that P1 is a table whereas
67277** NotFound assumes key is a blob constructed from MakeRecord and
67278** P1 is an index.
67279**
67280** See also: Found, NotFound, IsUnique
67281*/
67282case OP_NotExists: {        /* jump, in3 */
67283#if 0  /* local variables moved into u.be */
67284  VdbeCursor *pC;
67285  BtCursor *pCrsr;
67286  int res;
67287  u64 iKey;
67288#endif /* local variables moved into u.be */
67289
67290  pIn3 = &aMem[pOp->p3];
67291  assert( pIn3->flags & MEM_Int );
67292  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67293  u.be.pC = p->apCsr[pOp->p1];
67294  assert( u.be.pC!=0 );
67295  assert( u.be.pC->isTable );
67296  assert( u.be.pC->pseudoTableReg==0 );
67297  u.be.pCrsr = u.be.pC->pCursor;
67298  if( ALWAYS(u.be.pCrsr!=0) ){
67299    u.be.res = 0;
67300    u.be.iKey = pIn3->u.i;
67301    rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
67302    u.be.pC->lastRowid = pIn3->u.i;
67303    u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
67304    u.be.pC->nullRow = 0;
67305    u.be.pC->cacheStatus = CACHE_STALE;
67306    u.be.pC->deferredMoveto = 0;
67307    if( u.be.res!=0 ){
67308      pc = pOp->p2 - 1;
67309      assert( u.be.pC->rowidIsValid==0 );
67310    }
67311    u.be.pC->seekResult = u.be.res;
67312  }else{
67313    /* This happens when an attempt to open a read cursor on the
67314    ** sqlite_master table returns SQLITE_EMPTY.
67315    */
67316    pc = pOp->p2 - 1;
67317    assert( u.be.pC->rowidIsValid==0 );
67318    u.be.pC->seekResult = 0;
67319  }
67320  break;
67321}
67322
67323/* Opcode: Sequence P1 P2 * * *
67324**
67325** Find the next available sequence number for cursor P1.
67326** Write the sequence number into register P2.
67327** The sequence number on the cursor is incremented after this
67328** instruction.
67329*/
67330case OP_Sequence: {           /* out2-prerelease */
67331  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67332  assert( p->apCsr[pOp->p1]!=0 );
67333  pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
67334  break;
67335}
67336
67337
67338/* Opcode: NewRowid P1 P2 P3 * *
67339**
67340** Get a new integer record number (a.k.a "rowid") used as the key to a table.
67341** The record number is not previously used as a key in the database
67342** table that cursor P1 points to.  The new record number is written
67343** written to register P2.
67344**
67345** If P3>0 then P3 is a register in the root frame of this VDBE that holds
67346** the largest previously generated record number. No new record numbers are
67347** allowed to be less than this value. When this value reaches its maximum,
67348** an SQLITE_FULL error is generated. The P3 register is updated with the '
67349** generated record number. This P3 mechanism is used to help implement the
67350** AUTOINCREMENT feature.
67351*/
67352case OP_NewRowid: {           /* out2-prerelease */
67353#if 0  /* local variables moved into u.bf */
67354  i64 v;                 /* The new rowid */
67355  VdbeCursor *pC;        /* Cursor of table to get the new rowid */
67356  int res;               /* Result of an sqlite3BtreeLast() */
67357  int cnt;               /* Counter to limit the number of searches */
67358  Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
67359  VdbeFrame *pFrame;     /* Root frame of VDBE */
67360#endif /* local variables moved into u.bf */
67361
67362  u.bf.v = 0;
67363  u.bf.res = 0;
67364  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67365  u.bf.pC = p->apCsr[pOp->p1];
67366  assert( u.bf.pC!=0 );
67367  if( NEVER(u.bf.pC->pCursor==0) ){
67368    /* The zero initialization above is all that is needed */
67369  }else{
67370    /* The next rowid or record number (different terms for the same
67371    ** thing) is obtained in a two-step algorithm.
67372    **
67373    ** First we attempt to find the largest existing rowid and add one
67374    ** to that.  But if the largest existing rowid is already the maximum
67375    ** positive integer, we have to fall through to the second
67376    ** probabilistic algorithm
67377    **
67378    ** The second algorithm is to select a rowid at random and see if
67379    ** it already exists in the table.  If it does not exist, we have
67380    ** succeeded.  If the random rowid does exist, we select a new one
67381    ** and try again, up to 100 times.
67382    */
67383    assert( u.bf.pC->isTable );
67384
67385#ifdef SQLITE_32BIT_ROWID
67386#   define MAX_ROWID 0x7fffffff
67387#else
67388    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
67389    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
67390    ** to provide the constant while making all compilers happy.
67391    */
67392#   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
67393#endif
67394
67395    if( !u.bf.pC->useRandomRowid ){
67396      u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
67397      if( u.bf.v==0 ){
67398        rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
67399        if( rc!=SQLITE_OK ){
67400          goto abort_due_to_error;
67401        }
67402        if( u.bf.res ){
67403          u.bf.v = 1;   /* IMP: R-61914-48074 */
67404        }else{
67405          assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
67406          rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
67407          assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
67408          if( u.bf.v==MAX_ROWID ){
67409            u.bf.pC->useRandomRowid = 1;
67410          }else{
67411            u.bf.v++;   /* IMP: R-29538-34987 */
67412          }
67413        }
67414      }
67415
67416#ifndef SQLITE_OMIT_AUTOINCREMENT
67417      if( pOp->p3 ){
67418        /* Assert that P3 is a valid memory cell. */
67419        assert( pOp->p3>0 );
67420        if( p->pFrame ){
67421          for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
67422          /* Assert that P3 is a valid memory cell. */
67423          assert( pOp->p3<=u.bf.pFrame->nMem );
67424          u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
67425        }else{
67426          /* Assert that P3 is a valid memory cell. */
67427          assert( pOp->p3<=p->nMem );
67428          u.bf.pMem = &aMem[pOp->p3];
67429          memAboutToChange(p, u.bf.pMem);
67430        }
67431        assert( memIsValid(u.bf.pMem) );
67432
67433        REGISTER_TRACE(pOp->p3, u.bf.pMem);
67434        sqlite3VdbeMemIntegerify(u.bf.pMem);
67435        assert( (u.bf.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
67436        if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
67437          rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
67438          goto abort_due_to_error;
67439        }
67440        if( u.bf.v<u.bf.pMem->u.i+1 ){
67441          u.bf.v = u.bf.pMem->u.i + 1;
67442        }
67443        u.bf.pMem->u.i = u.bf.v;
67444      }
67445#endif
67446
67447      sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
67448    }
67449    if( u.bf.pC->useRandomRowid ){
67450      /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
67451      ** largest possible integer (9223372036854775807) then the database
67452      ** engine starts picking positive candidate ROWIDs at random until
67453      ** it finds one that is not previously used. */
67454      assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
67455                             ** an AUTOINCREMENT table. */
67456      /* on the first attempt, simply do one more than previous */
67457      u.bf.v = lastRowid;
67458      u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67459      u.bf.v++; /* ensure non-zero */
67460      u.bf.cnt = 0;
67461      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
67462                                                 0, &u.bf.res))==SQLITE_OK)
67463            && (u.bf.res==0)
67464            && (++u.bf.cnt<100)){
67465        /* collision - try another random rowid */
67466        sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
67467        if( u.bf.cnt<5 ){
67468          /* try "small" random rowids for the initial attempts */
67469          u.bf.v &= 0xffffff;
67470        }else{
67471          u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67472        }
67473        u.bf.v++; /* ensure non-zero */
67474      }
67475      if( rc==SQLITE_OK && u.bf.res==0 ){
67476        rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
67477        goto abort_due_to_error;
67478      }
67479      assert( u.bf.v>0 );  /* EV: R-40812-03570 */
67480    }
67481    u.bf.pC->rowidIsValid = 0;
67482    u.bf.pC->deferredMoveto = 0;
67483    u.bf.pC->cacheStatus = CACHE_STALE;
67484  }
67485  pOut->u.i = u.bf.v;
67486  break;
67487}
67488
67489/* Opcode: Insert P1 P2 P3 P4 P5
67490**
67491** Write an entry into the table of cursor P1.  A new entry is
67492** created if it doesn't already exist or the data for an existing
67493** entry is overwritten.  The data is the value MEM_Blob stored in register
67494** number P2. The key is stored in register P3. The key must
67495** be a MEM_Int.
67496**
67497** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
67498** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
67499** then rowid is stored for subsequent return by the
67500** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
67501**
67502** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
67503** the last seek operation (OP_NotExists) was a success, then this
67504** operation will not attempt to find the appropriate row before doing
67505** the insert but will instead overwrite the row that the cursor is
67506** currently pointing to.  Presumably, the prior OP_NotExists opcode
67507** has already positioned the cursor correctly.  This is an optimization
67508** that boosts performance by avoiding redundant seeks.
67509**
67510** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
67511** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
67512** is part of an INSERT operation.  The difference is only important to
67513** the update hook.
67514**
67515** Parameter P4 may point to a string containing the table-name, or
67516** may be NULL. If it is not NULL, then the update-hook
67517** (sqlite3.xUpdateCallback) is invoked following a successful insert.
67518**
67519** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
67520** allocated, then ownership of P2 is transferred to the pseudo-cursor
67521** and register P2 becomes ephemeral.  If the cursor is changed, the
67522** value of register P2 will then change.  Make sure this does not
67523** cause any problems.)
67524**
67525** This instruction only works on tables.  The equivalent instruction
67526** for indices is OP_IdxInsert.
67527*/
67528/* Opcode: InsertInt P1 P2 P3 P4 P5
67529**
67530** This works exactly like OP_Insert except that the key is the
67531** integer value P3, not the value of the integer stored in register P3.
67532*/
67533case OP_Insert:
67534case OP_InsertInt: {
67535#if 0  /* local variables moved into u.bg */
67536  Mem *pData;       /* MEM cell holding data for the record to be inserted */
67537  Mem *pKey;        /* MEM cell holding key  for the record */
67538  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
67539  VdbeCursor *pC;   /* Cursor to table into which insert is written */
67540  int nZero;        /* Number of zero-bytes to append */
67541  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
67542  const char *zDb;  /* database name - used by the update hook */
67543  const char *zTbl; /* Table name - used by the opdate hook */
67544  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67545#endif /* local variables moved into u.bg */
67546
67547  u.bg.pData = &aMem[pOp->p2];
67548  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67549  assert( memIsValid(u.bg.pData) );
67550  u.bg.pC = p->apCsr[pOp->p1];
67551  assert( u.bg.pC!=0 );
67552  assert( u.bg.pC->pCursor!=0 );
67553  assert( u.bg.pC->pseudoTableReg==0 );
67554  assert( u.bg.pC->isTable );
67555  REGISTER_TRACE(pOp->p2, u.bg.pData);
67556
67557  if( pOp->opcode==OP_Insert ){
67558    u.bg.pKey = &aMem[pOp->p3];
67559    assert( u.bg.pKey->flags & MEM_Int );
67560    assert( memIsValid(u.bg.pKey) );
67561    REGISTER_TRACE(pOp->p3, u.bg.pKey);
67562    u.bg.iKey = u.bg.pKey->u.i;
67563  }else{
67564    assert( pOp->opcode==OP_InsertInt );
67565    u.bg.iKey = pOp->p3;
67566  }
67567
67568  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67569  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
67570  if( u.bg.pData->flags & MEM_Null ){
67571    u.bg.pData->z = 0;
67572    u.bg.pData->n = 0;
67573  }else{
67574    assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
67575  }
67576  u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
67577  if( u.bg.pData->flags & MEM_Zero ){
67578    u.bg.nZero = u.bg.pData->u.nZero;
67579  }else{
67580    u.bg.nZero = 0;
67581  }
67582  sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67583  rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
67584                          u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
67585                          pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
67586  );
67587  u.bg.pC->rowidIsValid = 0;
67588  u.bg.pC->deferredMoveto = 0;
67589  u.bg.pC->cacheStatus = CACHE_STALE;
67590
67591  /* Invoke the update-hook if required. */
67592  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67593    u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
67594    u.bg.zTbl = pOp->p4.z;
67595    u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67596    assert( u.bg.pC->isTable );
67597    db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
67598    assert( u.bg.pC->iDb>=0 );
67599  }
67600  break;
67601}
67602
67603/* Opcode: Delete P1 P2 * P4 *
67604**
67605** Delete the record at which the P1 cursor is currently pointing.
67606**
67607** The cursor will be left pointing at either the next or the previous
67608** record in the table. If it is left pointing at the next record, then
67609** the next Next instruction will be a no-op.  Hence it is OK to delete
67610** a record from within an Next loop.
67611**
67612** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
67613** incremented (otherwise not).
67614**
67615** P1 must not be pseudo-table.  It has to be a real table with
67616** multiple rows.
67617**
67618** If P4 is not NULL, then it is the name of the table that P1 is
67619** pointing to.  The update hook will be invoked, if it exists.
67620** If P4 is not NULL then the P1 cursor must have been positioned
67621** using OP_NotFound prior to invoking this opcode.
67622*/
67623case OP_Delete: {
67624#if 0  /* local variables moved into u.bh */
67625  i64 iKey;
67626  VdbeCursor *pC;
67627#endif /* local variables moved into u.bh */
67628
67629  u.bh.iKey = 0;
67630  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67631  u.bh.pC = p->apCsr[pOp->p1];
67632  assert( u.bh.pC!=0 );
67633  assert( u.bh.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
67634
67635  /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
67636  ** row being deleted.
67637  */
67638  if( db->xUpdateCallback && pOp->p4.z ){
67639    assert( u.bh.pC->isTable );
67640    assert( u.bh.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
67641    u.bh.iKey = u.bh.pC->lastRowid;
67642  }
67643
67644  /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67645  ** OP_Column on the same table without any intervening operations that
67646  ** might move or invalidate the cursor.  Hence cursor u.bh.pC is always pointing
67647  ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67648  ** below is always a no-op and cannot fail.  We will run it anyhow, though,
67649  ** to guard against future changes to the code generator.
67650  **/
67651  assert( u.bh.pC->deferredMoveto==0 );
67652  rc = sqlite3VdbeCursorMoveto(u.bh.pC);
67653  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67654
67655  sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67656  rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
67657  u.bh.pC->cacheStatus = CACHE_STALE;
67658
67659  /* Invoke the update-hook if required. */
67660  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67661    const char *zDb = db->aDb[u.bh.pC->iDb].zName;
67662    const char *zTbl = pOp->p4.z;
67663    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
67664    assert( u.bh.pC->iDb>=0 );
67665  }
67666  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67667  break;
67668}
67669/* Opcode: ResetCount * * * * *
67670**
67671** The value of the change counter is copied to the database handle
67672** change counter (returned by subsequent calls to sqlite3_changes()).
67673** Then the VMs internal change counter resets to 0.
67674** This is used by trigger programs.
67675*/
67676case OP_ResetCount: {
67677  sqlite3VdbeSetChanges(db, p->nChange);
67678  p->nChange = 0;
67679  break;
67680}
67681
67682/* Opcode: SorterCompare P1 P2 P3
67683**
67684** P1 is a sorter cursor. This instruction compares the record blob in
67685** register P3 with the entry that the sorter cursor currently points to.
67686** If, excluding the rowid fields at the end, the two records are a match,
67687** fall through to the next instruction. Otherwise, jump to instruction P2.
67688*/
67689case OP_SorterCompare: {
67690#if 0  /* local variables moved into u.bi */
67691  VdbeCursor *pC;
67692  int res;
67693#endif /* local variables moved into u.bi */
67694
67695  u.bi.pC = p->apCsr[pOp->p1];
67696  assert( isSorter(u.bi.pC) );
67697  pIn3 = &aMem[pOp->p3];
67698  rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
67699  if( u.bi.res ){
67700    pc = pOp->p2-1;
67701  }
67702  break;
67703};
67704
67705/* Opcode: SorterData P1 P2 * * *
67706**
67707** Write into register P2 the current sorter data for sorter cursor P1.
67708*/
67709case OP_SorterData: {
67710#if 0  /* local variables moved into u.bj */
67711  VdbeCursor *pC;
67712#endif /* local variables moved into u.bj */
67713#ifndef SQLITE_OMIT_MERGE_SORT
67714  pOut = &aMem[pOp->p2];
67715  u.bj.pC = p->apCsr[pOp->p1];
67716  assert( u.bj.pC->isSorter );
67717  rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
67718#else
67719  pOp->opcode = OP_RowKey;
67720  pc--;
67721#endif
67722  break;
67723}
67724
67725/* Opcode: RowData P1 P2 * * *
67726**
67727** Write into register P2 the complete row data for cursor P1.
67728** There is no interpretation of the data.
67729** It is just copied onto the P2 register exactly as
67730** it is found in the database file.
67731**
67732** If the P1 cursor must be pointing to a valid row (not a NULL row)
67733** of a real table, not a pseudo-table.
67734*/
67735/* Opcode: RowKey P1 P2 * * *
67736**
67737** Write into register P2 the complete row key for cursor P1.
67738** There is no interpretation of the data.
67739** The key is copied onto the P3 register exactly as
67740** it is found in the database file.
67741**
67742** If the P1 cursor must be pointing to a valid row (not a NULL row)
67743** of a real table, not a pseudo-table.
67744*/
67745case OP_RowKey:
67746case OP_RowData: {
67747#if 0  /* local variables moved into u.bk */
67748  VdbeCursor *pC;
67749  BtCursor *pCrsr;
67750  u32 n;
67751  i64 n64;
67752#endif /* local variables moved into u.bk */
67753
67754  pOut = &aMem[pOp->p2];
67755  memAboutToChange(p, pOut);
67756
67757  /* Note that RowKey and RowData are really exactly the same instruction */
67758  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67759  u.bk.pC = p->apCsr[pOp->p1];
67760  assert( u.bk.pC->isSorter==0 );
67761  assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
67762  assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
67763  assert( u.bk.pC!=0 );
67764  assert( u.bk.pC->nullRow==0 );
67765  assert( u.bk.pC->pseudoTableReg==0 );
67766  assert( !u.bk.pC->isSorter );
67767  assert( u.bk.pC->pCursor!=0 );
67768  u.bk.pCrsr = u.bk.pC->pCursor;
67769  assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
67770
67771  /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67772  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67773  ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
67774  ** a no-op and can never fail.  But we leave it in place as a safety.
67775  */
67776  assert( u.bk.pC->deferredMoveto==0 );
67777  rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67778  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67779
67780  if( u.bk.pC->isIndex ){
67781    assert( !u.bk.pC->isTable );
67782    rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67783    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
67784    if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67785      goto too_big;
67786    }
67787    u.bk.n = (u32)u.bk.n64;
67788  }else{
67789    rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67790    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
67791    if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67792      goto too_big;
67793    }
67794  }
67795  if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
67796    goto no_mem;
67797  }
67798  pOut->n = u.bk.n;
67799  MemSetTypeFlag(pOut, MEM_Blob);
67800  if( u.bk.pC->isIndex ){
67801    rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67802  }else{
67803    rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67804  }
67805  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
67806  UPDATE_MAX_BLOBSIZE(pOut);
67807  break;
67808}
67809
67810/* Opcode: Rowid P1 P2 * * *
67811**
67812** Store in register P2 an integer which is the key of the table entry that
67813** P1 is currently point to.
67814**
67815** P1 can be either an ordinary table or a virtual table.  There used to
67816** be a separate OP_VRowid opcode for use with virtual tables, but this
67817** one opcode now works for both table types.
67818*/
67819case OP_Rowid: {                 /* out2-prerelease */
67820#if 0  /* local variables moved into u.bl */
67821  VdbeCursor *pC;
67822  i64 v;
67823  sqlite3_vtab *pVtab;
67824  const sqlite3_module *pModule;
67825#endif /* local variables moved into u.bl */
67826
67827  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67828  u.bl.pC = p->apCsr[pOp->p1];
67829  assert( u.bl.pC!=0 );
67830  assert( u.bl.pC->pseudoTableReg==0 );
67831  if( u.bl.pC->nullRow ){
67832    pOut->flags = MEM_Null;
67833    break;
67834  }else if( u.bl.pC->deferredMoveto ){
67835    u.bl.v = u.bl.pC->movetoTarget;
67836#ifndef SQLITE_OMIT_VIRTUALTABLE
67837  }else if( u.bl.pC->pVtabCursor ){
67838    u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
67839    u.bl.pModule = u.bl.pVtab->pModule;
67840    assert( u.bl.pModule->xRowid );
67841    rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
67842    importVtabErrMsg(p, u.bl.pVtab);
67843#endif /* SQLITE_OMIT_VIRTUALTABLE */
67844  }else{
67845    assert( u.bl.pC->pCursor!=0 );
67846    rc = sqlite3VdbeCursorMoveto(u.bl.pC);
67847    if( rc ) goto abort_due_to_error;
67848    if( u.bl.pC->rowidIsValid ){
67849      u.bl.v = u.bl.pC->lastRowid;
67850    }else{
67851      rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
67852      assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
67853    }
67854  }
67855  pOut->u.i = u.bl.v;
67856  break;
67857}
67858
67859/* Opcode: NullRow P1 * * * *
67860**
67861** Move the cursor P1 to a null row.  Any OP_Column operations
67862** that occur while the cursor is on the null row will always
67863** write a NULL.
67864*/
67865case OP_NullRow: {
67866#if 0  /* local variables moved into u.bm */
67867  VdbeCursor *pC;
67868#endif /* local variables moved into u.bm */
67869
67870  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67871  u.bm.pC = p->apCsr[pOp->p1];
67872  assert( u.bm.pC!=0 );
67873  u.bm.pC->nullRow = 1;
67874  u.bm.pC->rowidIsValid = 0;
67875  assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
67876  if( u.bm.pC->pCursor ){
67877    sqlite3BtreeClearCursor(u.bm.pC->pCursor);
67878  }
67879  break;
67880}
67881
67882/* Opcode: Last P1 P2 * * *
67883**
67884** The next use of the Rowid or Column or Next instruction for P1
67885** will refer to the last entry in the database table or index.
67886** If the table or index is empty and P2>0, then jump immediately to P2.
67887** If P2 is 0 or if the table or index is not empty, fall through
67888** to the following instruction.
67889*/
67890case OP_Last: {        /* jump */
67891#if 0  /* local variables moved into u.bn */
67892  VdbeCursor *pC;
67893  BtCursor *pCrsr;
67894  int res;
67895#endif /* local variables moved into u.bn */
67896
67897  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67898  u.bn.pC = p->apCsr[pOp->p1];
67899  assert( u.bn.pC!=0 );
67900  u.bn.pCrsr = u.bn.pC->pCursor;
67901  if( NEVER(u.bn.pCrsr==0) ){
67902    u.bn.res = 1;
67903  }else{
67904    rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
67905  }
67906  u.bn.pC->nullRow = (u8)u.bn.res;
67907  u.bn.pC->deferredMoveto = 0;
67908  u.bn.pC->rowidIsValid = 0;
67909  u.bn.pC->cacheStatus = CACHE_STALE;
67910  if( pOp->p2>0 && u.bn.res ){
67911    pc = pOp->p2 - 1;
67912  }
67913  break;
67914}
67915
67916
67917/* Opcode: Sort P1 P2 * * *
67918**
67919** This opcode does exactly the same thing as OP_Rewind except that
67920** it increments an undocumented global variable used for testing.
67921**
67922** Sorting is accomplished by writing records into a sorting index,
67923** then rewinding that index and playing it back from beginning to
67924** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
67925** rewinding so that the global variable will be incremented and
67926** regression tests can determine whether or not the optimizer is
67927** correctly optimizing out sorts.
67928*/
67929case OP_SorterSort:    /* jump */
67930#ifdef SQLITE_OMIT_MERGE_SORT
67931  pOp->opcode = OP_Sort;
67932#endif
67933case OP_Sort: {        /* jump */
67934#ifdef SQLITE_TEST
67935  sqlite3_sort_count++;
67936  sqlite3_search_count--;
67937#endif
67938  p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
67939  /* Fall through into OP_Rewind */
67940}
67941/* Opcode: Rewind P1 P2 * * *
67942**
67943** The next use of the Rowid or Column or Next instruction for P1
67944** will refer to the first entry in the database table or index.
67945** If the table or index is empty and P2>0, then jump immediately to P2.
67946** If P2 is 0 or if the table or index is not empty, fall through
67947** to the following instruction.
67948*/
67949case OP_Rewind: {        /* jump */
67950#if 0  /* local variables moved into u.bo */
67951  VdbeCursor *pC;
67952  BtCursor *pCrsr;
67953  int res;
67954#endif /* local variables moved into u.bo */
67955
67956  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67957  u.bo.pC = p->apCsr[pOp->p1];
67958  assert( u.bo.pC!=0 );
67959  assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
67960  u.bo.res = 1;
67961  if( isSorter(u.bo.pC) ){
67962    rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
67963  }else{
67964    u.bo.pCrsr = u.bo.pC->pCursor;
67965    assert( u.bo.pCrsr );
67966    rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
67967    u.bo.pC->atFirst = u.bo.res==0 ?1:0;
67968    u.bo.pC->deferredMoveto = 0;
67969    u.bo.pC->cacheStatus = CACHE_STALE;
67970    u.bo.pC->rowidIsValid = 0;
67971  }
67972  u.bo.pC->nullRow = (u8)u.bo.res;
67973  assert( pOp->p2>0 && pOp->p2<p->nOp );
67974  if( u.bo.res ){
67975    pc = pOp->p2 - 1;
67976  }
67977  break;
67978}
67979
67980/* Opcode: Next P1 P2 * P4 P5
67981**
67982** Advance cursor P1 so that it points to the next key/data pair in its
67983** table or index.  If there are no more key/value pairs then fall through
67984** to the following instruction.  But if the cursor advance was successful,
67985** jump immediately to P2.
67986**
67987** The P1 cursor must be for a real table, not a pseudo-table.
67988**
67989** P4 is always of type P4_ADVANCE. The function pointer points to
67990** sqlite3BtreeNext().
67991**
67992** If P5 is positive and the jump is taken, then event counter
67993** number P5-1 in the prepared statement is incremented.
67994**
67995** See also: Prev
67996*/
67997/* Opcode: Prev P1 P2 * * P5
67998**
67999** Back up cursor P1 so that it points to the previous key/data pair in its
68000** table or index.  If there is no previous key/value pairs then fall through
68001** to the following instruction.  But if the cursor backup was successful,
68002** jump immediately to P2.
68003**
68004** The P1 cursor must be for a real table, not a pseudo-table.
68005**
68006** P4 is always of type P4_ADVANCE. The function pointer points to
68007** sqlite3BtreePrevious().
68008**
68009** If P5 is positive and the jump is taken, then event counter
68010** number P5-1 in the prepared statement is incremented.
68011*/
68012case OP_SorterNext:    /* jump */
68013#ifdef SQLITE_OMIT_MERGE_SORT
68014  pOp->opcode = OP_Next;
68015#endif
68016case OP_Prev:          /* jump */
68017case OP_Next: {        /* jump */
68018#if 0  /* local variables moved into u.bp */
68019  VdbeCursor *pC;
68020  int res;
68021#endif /* local variables moved into u.bp */
68022
68023  CHECK_FOR_INTERRUPT;
68024  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68025  assert( pOp->p5<=ArraySize(p->aCounter) );
68026  u.bp.pC = p->apCsr[pOp->p1];
68027  if( u.bp.pC==0 ){
68028    break;  /* See ticket #2273 */
68029  }
68030  assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68031  if( isSorter(u.bp.pC) ){
68032    assert( pOp->opcode==OP_SorterNext );
68033    rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
68034  }else{
68035    u.bp.res = 1;
68036    assert( u.bp.pC->deferredMoveto==0 );
68037    assert( u.bp.pC->pCursor );
68038    assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
68039    assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68040    rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
68041  }
68042  u.bp.pC->nullRow = (u8)u.bp.res;
68043  u.bp.pC->cacheStatus = CACHE_STALE;
68044  if( u.bp.res==0 ){
68045    pc = pOp->p2 - 1;
68046    if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
68047#ifdef SQLITE_TEST
68048    sqlite3_search_count++;
68049#endif
68050  }
68051  u.bp.pC->rowidIsValid = 0;
68052  break;
68053}
68054
68055/* Opcode: IdxInsert P1 P2 P3 * P5
68056**
68057** Register P2 holds an SQL index key made using the
68058** MakeRecord instructions.  This opcode writes that key
68059** into the index P1.  Data for the entry is nil.
68060**
68061** P3 is a flag that provides a hint to the b-tree layer that this
68062** insert is likely to be an append.
68063**
68064** This instruction only works for indices.  The equivalent instruction
68065** for tables is OP_Insert.
68066*/
68067case OP_SorterInsert:       /* in2 */
68068#ifdef SQLITE_OMIT_MERGE_SORT
68069  pOp->opcode = OP_IdxInsert;
68070#endif
68071case OP_IdxInsert: {        /* in2 */
68072#if 0  /* local variables moved into u.bq */
68073  VdbeCursor *pC;
68074  BtCursor *pCrsr;
68075  int nKey;
68076  const char *zKey;
68077#endif /* local variables moved into u.bq */
68078
68079  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68080  u.bq.pC = p->apCsr[pOp->p1];
68081  assert( u.bq.pC!=0 );
68082  assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
68083  pIn2 = &aMem[pOp->p2];
68084  assert( pIn2->flags & MEM_Blob );
68085  u.bq.pCrsr = u.bq.pC->pCursor;
68086  if( ALWAYS(u.bq.pCrsr!=0) ){
68087    assert( u.bq.pC->isTable==0 );
68088    rc = ExpandBlob(pIn2);
68089    if( rc==SQLITE_OK ){
68090      if( isSorter(u.bq.pC) ){
68091        rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
68092      }else{
68093        u.bq.nKey = pIn2->n;
68094        u.bq.zKey = pIn2->z;
68095        rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
68096            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
68097            );
68098        assert( u.bq.pC->deferredMoveto==0 );
68099        u.bq.pC->cacheStatus = CACHE_STALE;
68100      }
68101    }
68102  }
68103  break;
68104}
68105
68106/* Opcode: IdxDelete P1 P2 P3 * *
68107**
68108** The content of P3 registers starting at register P2 form
68109** an unpacked index key. This opcode removes that entry from the
68110** index opened by cursor P1.
68111*/
68112case OP_IdxDelete: {
68113#if 0  /* local variables moved into u.br */
68114  VdbeCursor *pC;
68115  BtCursor *pCrsr;
68116  int res;
68117  UnpackedRecord r;
68118#endif /* local variables moved into u.br */
68119
68120  assert( pOp->p3>0 );
68121  assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
68122  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68123  u.br.pC = p->apCsr[pOp->p1];
68124  assert( u.br.pC!=0 );
68125  u.br.pCrsr = u.br.pC->pCursor;
68126  if( ALWAYS(u.br.pCrsr!=0) ){
68127    u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
68128    u.br.r.nField = (u16)pOp->p3;
68129    u.br.r.flags = 0;
68130    u.br.r.aMem = &aMem[pOp->p2];
68131#ifdef SQLITE_DEBUG
68132    { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
68133#endif
68134    rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
68135    if( rc==SQLITE_OK && u.br.res==0 ){
68136      rc = sqlite3BtreeDelete(u.br.pCrsr);
68137    }
68138    assert( u.br.pC->deferredMoveto==0 );
68139    u.br.pC->cacheStatus = CACHE_STALE;
68140  }
68141  break;
68142}
68143
68144/* Opcode: IdxRowid P1 P2 * * *
68145**
68146** Write into register P2 an integer which is the last entry in the record at
68147** the end of the index key pointed to by cursor P1.  This integer should be
68148** the rowid of the table entry to which this index entry points.
68149**
68150** See also: Rowid, MakeRecord.
68151*/
68152case OP_IdxRowid: {              /* out2-prerelease */
68153#if 0  /* local variables moved into u.bs */
68154  BtCursor *pCrsr;
68155  VdbeCursor *pC;
68156  i64 rowid;
68157#endif /* local variables moved into u.bs */
68158
68159  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68160  u.bs.pC = p->apCsr[pOp->p1];
68161  assert( u.bs.pC!=0 );
68162  u.bs.pCrsr = u.bs.pC->pCursor;
68163  pOut->flags = MEM_Null;
68164  if( ALWAYS(u.bs.pCrsr!=0) ){
68165    rc = sqlite3VdbeCursorMoveto(u.bs.pC);
68166    if( NEVER(rc) ) goto abort_due_to_error;
68167    assert( u.bs.pC->deferredMoveto==0 );
68168    assert( u.bs.pC->isTable==0 );
68169    if( !u.bs.pC->nullRow ){
68170      rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
68171      if( rc!=SQLITE_OK ){
68172        goto abort_due_to_error;
68173      }
68174      pOut->u.i = u.bs.rowid;
68175      pOut->flags = MEM_Int;
68176    }
68177  }
68178  break;
68179}
68180
68181/* Opcode: IdxGE P1 P2 P3 P4 P5
68182**
68183** The P4 register values beginning with P3 form an unpacked index
68184** key that omits the ROWID.  Compare this key value against the index
68185** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
68186**
68187** If the P1 index entry is greater than or equal to the key value
68188** then jump to P2.  Otherwise fall through to the next instruction.
68189**
68190** If P5 is non-zero then the key value is increased by an epsilon
68191** prior to the comparison.  This make the opcode work like IdxGT except
68192** that if the key from register P3 is a prefix of the key in the cursor,
68193** the result is false whereas it would be true with IdxGT.
68194*/
68195/* Opcode: IdxLT P1 P2 P3 P4 P5
68196**
68197** The P4 register values beginning with P3 form an unpacked index
68198** key that omits the ROWID.  Compare this key value against the index
68199** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
68200**
68201** If the P1 index entry is less than the key value then jump to P2.
68202** Otherwise fall through to the next instruction.
68203**
68204** If P5 is non-zero then the key value is increased by an epsilon prior
68205** to the comparison.  This makes the opcode work like IdxLE.
68206*/
68207case OP_IdxLT:          /* jump */
68208case OP_IdxGE: {        /* jump */
68209#if 0  /* local variables moved into u.bt */
68210  VdbeCursor *pC;
68211  int res;
68212  UnpackedRecord r;
68213#endif /* local variables moved into u.bt */
68214
68215  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68216  u.bt.pC = p->apCsr[pOp->p1];
68217  assert( u.bt.pC!=0 );
68218  assert( u.bt.pC->isOrdered );
68219  if( ALWAYS(u.bt.pC->pCursor!=0) ){
68220    assert( u.bt.pC->deferredMoveto==0 );
68221    assert( pOp->p5==0 || pOp->p5==1 );
68222    assert( pOp->p4type==P4_INT32 );
68223    u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68224    u.bt.r.nField = (u16)pOp->p4.i;
68225    if( pOp->p5 ){
68226      u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
68227    }else{
68228      u.bt.r.flags = UNPACKED_IGNORE_ROWID;
68229    }
68230    u.bt.r.aMem = &aMem[pOp->p3];
68231#ifdef SQLITE_DEBUG
68232    { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
68233#endif
68234    rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
68235    if( pOp->opcode==OP_IdxLT ){
68236      u.bt.res = -u.bt.res;
68237    }else{
68238      assert( pOp->opcode==OP_IdxGE );
68239      u.bt.res++;
68240    }
68241    if( u.bt.res>0 ){
68242      pc = pOp->p2 - 1 ;
68243    }
68244  }
68245  break;
68246}
68247
68248/* Opcode: Destroy P1 P2 P3 * *
68249**
68250** Delete an entire database table or index whose root page in the database
68251** file is given by P1.
68252**
68253** The table being destroyed is in the main database file if P3==0.  If
68254** P3==1 then the table to be clear is in the auxiliary database file
68255** that is used to store tables create using CREATE TEMPORARY TABLE.
68256**
68257** If AUTOVACUUM is enabled then it is possible that another root page
68258** might be moved into the newly deleted root page in order to keep all
68259** root pages contiguous at the beginning of the database.  The former
68260** value of the root page that moved - its value before the move occurred -
68261** is stored in register P2.  If no page
68262** movement was required (because the table being dropped was already
68263** the last one in the database) then a zero is stored in register P2.
68264** If AUTOVACUUM is disabled then a zero is stored in register P2.
68265**
68266** See also: Clear
68267*/
68268case OP_Destroy: {     /* out2-prerelease */
68269#if 0  /* local variables moved into u.bu */
68270  int iMoved;
68271  int iCnt;
68272  Vdbe *pVdbe;
68273  int iDb;
68274#endif /* local variables moved into u.bu */
68275#ifndef SQLITE_OMIT_VIRTUALTABLE
68276  u.bu.iCnt = 0;
68277  for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
68278    if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
68279      u.bu.iCnt++;
68280    }
68281  }
68282#else
68283  u.bu.iCnt = db->activeVdbeCnt;
68284#endif
68285  pOut->flags = MEM_Null;
68286  if( u.bu.iCnt>1 ){
68287    rc = SQLITE_LOCKED;
68288    p->errorAction = OE_Abort;
68289  }else{
68290    u.bu.iDb = pOp->p3;
68291    assert( u.bu.iCnt==1 );
68292    assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
68293    rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
68294    pOut->flags = MEM_Int;
68295    pOut->u.i = u.bu.iMoved;
68296#ifndef SQLITE_OMIT_AUTOVACUUM
68297    if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
68298      sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
68299      /* All OP_Destroy operations occur on the same btree */
68300      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
68301      resetSchemaOnFault = u.bu.iDb+1;
68302    }
68303#endif
68304  }
68305  break;
68306}
68307
68308/* Opcode: Clear P1 P2 P3
68309**
68310** Delete all contents of the database table or index whose root page
68311** in the database file is given by P1.  But, unlike Destroy, do not
68312** remove the table or index from the database file.
68313**
68314** The table being clear is in the main database file if P2==0.  If
68315** P2==1 then the table to be clear is in the auxiliary database file
68316** that is used to store tables create using CREATE TEMPORARY TABLE.
68317**
68318** If the P3 value is non-zero, then the table referred to must be an
68319** intkey table (an SQL table, not an index). In this case the row change
68320** count is incremented by the number of rows in the table being cleared.
68321** If P3 is greater than zero, then the value stored in register P3 is
68322** also incremented by the number of rows in the table being cleared.
68323**
68324** See also: Destroy
68325*/
68326case OP_Clear: {
68327#if 0  /* local variables moved into u.bv */
68328  int nChange;
68329#endif /* local variables moved into u.bv */
68330
68331  u.bv.nChange = 0;
68332  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
68333  rc = sqlite3BtreeClearTable(
68334      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
68335  );
68336  if( pOp->p3 ){
68337    p->nChange += u.bv.nChange;
68338    if( pOp->p3>0 ){
68339      assert( memIsValid(&aMem[pOp->p3]) );
68340      memAboutToChange(p, &aMem[pOp->p3]);
68341      aMem[pOp->p3].u.i += u.bv.nChange;
68342    }
68343  }
68344  break;
68345}
68346
68347/* Opcode: CreateTable P1 P2 * * *
68348**
68349** Allocate a new table in the main database file if P1==0 or in the
68350** auxiliary database file if P1==1 or in an attached database if
68351** P1>1.  Write the root page number of the new table into
68352** register P2
68353**
68354** The difference between a table and an index is this:  A table must
68355** have a 4-byte integer key and can have arbitrary data.  An index
68356** has an arbitrary key but no data.
68357**
68358** See also: CreateIndex
68359*/
68360/* Opcode: CreateIndex P1 P2 * * *
68361**
68362** Allocate a new index in the main database file if P1==0 or in the
68363** auxiliary database file if P1==1 or in an attached database if
68364** P1>1.  Write the root page number of the new table into
68365** register P2.
68366**
68367** See documentation on OP_CreateTable for additional information.
68368*/
68369case OP_CreateIndex:            /* out2-prerelease */
68370case OP_CreateTable: {          /* out2-prerelease */
68371#if 0  /* local variables moved into u.bw */
68372  int pgno;
68373  int flags;
68374  Db *pDb;
68375#endif /* local variables moved into u.bw */
68376
68377  u.bw.pgno = 0;
68378  assert( pOp->p1>=0 && pOp->p1<db->nDb );
68379  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68380  u.bw.pDb = &db->aDb[pOp->p1];
68381  assert( u.bw.pDb->pBt!=0 );
68382  if( pOp->opcode==OP_CreateTable ){
68383    /* u.bw.flags = BTREE_INTKEY; */
68384    u.bw.flags = BTREE_INTKEY;
68385  }else{
68386    u.bw.flags = BTREE_BLOBKEY;
68387  }
68388  rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
68389  pOut->u.i = u.bw.pgno;
68390  break;
68391}
68392
68393/* Opcode: ParseSchema P1 * * P4 *
68394**
68395** Read and parse all entries from the SQLITE_MASTER table of database P1
68396** that match the WHERE clause P4.
68397**
68398** This opcode invokes the parser to create a new virtual machine,
68399** then runs the new virtual machine.  It is thus a re-entrant opcode.
68400*/
68401case OP_ParseSchema: {
68402#if 0  /* local variables moved into u.bx */
68403  int iDb;
68404  const char *zMaster;
68405  char *zSql;
68406  InitData initData;
68407#endif /* local variables moved into u.bx */
68408
68409  /* Any prepared statement that invokes this opcode will hold mutexes
68410  ** on every btree.  This is a prerequisite for invoking
68411  ** sqlite3InitCallback().
68412  */
68413#ifdef SQLITE_DEBUG
68414  for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
68415    assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
68416  }
68417#endif
68418
68419  u.bx.iDb = pOp->p1;
68420  assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
68421  assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
68422  /* Used to be a conditional */ {
68423    u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
68424    u.bx.initData.db = db;
68425    u.bx.initData.iDb = pOp->p1;
68426    u.bx.initData.pzErrMsg = &p->zErrMsg;
68427    u.bx.zSql = sqlite3MPrintf(db,
68428       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68429       db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
68430    if( u.bx.zSql==0 ){
68431      rc = SQLITE_NOMEM;
68432    }else{
68433      assert( db->init.busy==0 );
68434      db->init.busy = 1;
68435      u.bx.initData.rc = SQLITE_OK;
68436      assert( !db->mallocFailed );
68437      rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
68438      if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
68439      sqlite3DbFree(db, u.bx.zSql);
68440      db->init.busy = 0;
68441    }
68442  }
68443  if( rc==SQLITE_NOMEM ){
68444    goto no_mem;
68445  }
68446  break;
68447}
68448
68449#if !defined(SQLITE_OMIT_ANALYZE)
68450/* Opcode: LoadAnalysis P1 * * * *
68451**
68452** Read the sqlite_stat1 table for database P1 and load the content
68453** of that table into the internal index hash table.  This will cause
68454** the analysis to be used when preparing all subsequent queries.
68455*/
68456case OP_LoadAnalysis: {
68457  assert( pOp->p1>=0 && pOp->p1<db->nDb );
68458  rc = sqlite3AnalysisLoad(db, pOp->p1);
68459  break;
68460}
68461#endif /* !defined(SQLITE_OMIT_ANALYZE) */
68462
68463/* Opcode: DropTable P1 * * P4 *
68464**
68465** Remove the internal (in-memory) data structures that describe
68466** the table named P4 in database P1.  This is called after a table
68467** is dropped in order to keep the internal representation of the
68468** schema consistent with what is on disk.
68469*/
68470case OP_DropTable: {
68471  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
68472  break;
68473}
68474
68475/* Opcode: DropIndex P1 * * P4 *
68476**
68477** Remove the internal (in-memory) data structures that describe
68478** the index named P4 in database P1.  This is called after an index
68479** is dropped in order to keep the internal representation of the
68480** schema consistent with what is on disk.
68481*/
68482case OP_DropIndex: {
68483  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
68484  break;
68485}
68486
68487/* Opcode: DropTrigger P1 * * P4 *
68488**
68489** Remove the internal (in-memory) data structures that describe
68490** the trigger named P4 in database P1.  This is called after a trigger
68491** is dropped in order to keep the internal representation of the
68492** schema consistent with what is on disk.
68493*/
68494case OP_DropTrigger: {
68495  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
68496  break;
68497}
68498
68499
68500#ifndef SQLITE_OMIT_INTEGRITY_CHECK
68501/* Opcode: IntegrityCk P1 P2 P3 * P5
68502**
68503** Do an analysis of the currently open database.  Store in
68504** register P1 the text of an error message describing any problems.
68505** If no problems are found, store a NULL in register P1.
68506**
68507** The register P3 contains the maximum number of allowed errors.
68508** At most reg(P3) errors will be reported.
68509** In other words, the analysis stops as soon as reg(P1) errors are
68510** seen.  Reg(P1) is updated with the number of errors remaining.
68511**
68512** The root page numbers of all tables in the database are integer
68513** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
68514** total.
68515**
68516** If P5 is not zero, the check is done on the auxiliary database
68517** file, not the main database file.
68518**
68519** This opcode is used to implement the integrity_check pragma.
68520*/
68521case OP_IntegrityCk: {
68522#if 0  /* local variables moved into u.by */
68523  int nRoot;      /* Number of tables to check.  (Number of root pages.) */
68524  int *aRoot;     /* Array of rootpage numbers for tables to be checked */
68525  int j;          /* Loop counter */
68526  int nErr;       /* Number of errors reported */
68527  char *z;        /* Text of the error report */
68528  Mem *pnErr;     /* Register keeping track of errors remaining */
68529#endif /* local variables moved into u.by */
68530
68531  u.by.nRoot = pOp->p2;
68532  assert( u.by.nRoot>0 );
68533  u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
68534  if( u.by.aRoot==0 ) goto no_mem;
68535  assert( pOp->p3>0 && pOp->p3<=p->nMem );
68536  u.by.pnErr = &aMem[pOp->p3];
68537  assert( (u.by.pnErr->flags & MEM_Int)!=0 );
68538  assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68539  pIn1 = &aMem[pOp->p1];
68540  for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
68541    u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
68542  }
68543  u.by.aRoot[u.by.j] = 0;
68544  assert( pOp->p5<db->nDb );
68545  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68546  u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
68547                                 (int)u.by.pnErr->u.i, &u.by.nErr);
68548  sqlite3DbFree(db, u.by.aRoot);
68549  u.by.pnErr->u.i -= u.by.nErr;
68550  sqlite3VdbeMemSetNull(pIn1);
68551  if( u.by.nErr==0 ){
68552    assert( u.by.z==0 );
68553  }else if( u.by.z==0 ){
68554    goto no_mem;
68555  }else{
68556    sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
68557  }
68558  UPDATE_MAX_BLOBSIZE(pIn1);
68559  sqlite3VdbeChangeEncoding(pIn1, encoding);
68560  break;
68561}
68562#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
68563
68564/* Opcode: RowSetAdd P1 P2 * * *
68565**
68566** Insert the integer value held by register P2 into a boolean index
68567** held in register P1.
68568**
68569** An assertion fails if P2 is not an integer.
68570*/
68571case OP_RowSetAdd: {       /* in1, in2 */
68572  pIn1 = &aMem[pOp->p1];
68573  pIn2 = &aMem[pOp->p2];
68574  assert( (pIn2->flags & MEM_Int)!=0 );
68575  if( (pIn1->flags & MEM_RowSet)==0 ){
68576    sqlite3VdbeMemSetRowSet(pIn1);
68577    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68578  }
68579  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
68580  break;
68581}
68582
68583/* Opcode: RowSetRead P1 P2 P3 * *
68584**
68585** Extract the smallest value from boolean index P1 and put that value into
68586** register P3.  Or, if boolean index P1 is initially empty, leave P3
68587** unchanged and jump to instruction P2.
68588*/
68589case OP_RowSetRead: {       /* jump, in1, out3 */
68590#if 0  /* local variables moved into u.bz */
68591  i64 val;
68592#endif /* local variables moved into u.bz */
68593  CHECK_FOR_INTERRUPT;
68594  pIn1 = &aMem[pOp->p1];
68595  if( (pIn1->flags & MEM_RowSet)==0
68596   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
68597  ){
68598    /* The boolean index is empty */
68599    sqlite3VdbeMemSetNull(pIn1);
68600    pc = pOp->p2 - 1;
68601  }else{
68602    /* A value was pulled from the index */
68603    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
68604  }
68605  break;
68606}
68607
68608/* Opcode: RowSetTest P1 P2 P3 P4
68609**
68610** Register P3 is assumed to hold a 64-bit integer value. If register P1
68611** contains a RowSet object and that RowSet object contains
68612** the value held in P3, jump to register P2. Otherwise, insert the
68613** integer in P3 into the RowSet and continue on to the
68614** next opcode.
68615**
68616** The RowSet object is optimized for the case where successive sets
68617** of integers, where each set contains no duplicates. Each set
68618** of values is identified by a unique P4 value. The first set
68619** must have P4==0, the final set P4=-1.  P4 must be either -1 or
68620** non-negative.  For non-negative values of P4 only the lower 4
68621** bits are significant.
68622**
68623** This allows optimizations: (a) when P4==0 there is no need to test
68624** the rowset object for P3, as it is guaranteed not to contain it,
68625** (b) when P4==-1 there is no need to insert the value, as it will
68626** never be tested for, and (c) when a value that is part of set X is
68627** inserted, there is no need to search to see if the same value was
68628** previously inserted as part of set X (only if it was previously
68629** inserted as part of some other set).
68630*/
68631case OP_RowSetTest: {                     /* jump, in1, in3 */
68632#if 0  /* local variables moved into u.ca */
68633  int iSet;
68634  int exists;
68635#endif /* local variables moved into u.ca */
68636
68637  pIn1 = &aMem[pOp->p1];
68638  pIn3 = &aMem[pOp->p3];
68639  u.ca.iSet = pOp->p4.i;
68640  assert( pIn3->flags&MEM_Int );
68641
68642  /* If there is anything other than a rowset object in memory cell P1,
68643  ** delete it now and initialize P1 with an empty rowset
68644  */
68645  if( (pIn1->flags & MEM_RowSet)==0 ){
68646    sqlite3VdbeMemSetRowSet(pIn1);
68647    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68648  }
68649
68650  assert( pOp->p4type==P4_INT32 );
68651  assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
68652  if( u.ca.iSet ){
68653    u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68654                               (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
68655                               pIn3->u.i);
68656    if( u.ca.exists ){
68657      pc = pOp->p2 - 1;
68658      break;
68659    }
68660  }
68661  if( u.ca.iSet>=0 ){
68662    sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
68663  }
68664  break;
68665}
68666
68667
68668#ifndef SQLITE_OMIT_TRIGGER
68669
68670/* Opcode: Program P1 P2 P3 P4 *
68671**
68672** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
68673**
68674** P1 contains the address of the memory cell that contains the first memory
68675** cell in an array of values used as arguments to the sub-program. P2
68676** contains the address to jump to if the sub-program throws an IGNORE
68677** exception using the RAISE() function. Register P3 contains the address
68678** of a memory cell in this (the parent) VM that is used to allocate the
68679** memory required by the sub-vdbe at runtime.
68680**
68681** P4 is a pointer to the VM containing the trigger program.
68682*/
68683case OP_Program: {        /* jump */
68684#if 0  /* local variables moved into u.cb */
68685  int nMem;               /* Number of memory registers for sub-program */
68686  int nByte;              /* Bytes of runtime space required for sub-program */
68687  Mem *pRt;               /* Register to allocate runtime space */
68688  Mem *pMem;              /* Used to iterate through memory cells */
68689  Mem *pEnd;              /* Last memory cell in new array */
68690  VdbeFrame *pFrame;      /* New vdbe frame to execute in */
68691  SubProgram *pProgram;   /* Sub-program to execute */
68692  void *t;                /* Token identifying trigger */
68693#endif /* local variables moved into u.cb */
68694
68695  u.cb.pProgram = pOp->p4.pProgram;
68696  u.cb.pRt = &aMem[pOp->p3];
68697  assert( memIsValid(u.cb.pRt) );
68698  assert( u.cb.pProgram->nOp>0 );
68699
68700  /* If the p5 flag is clear, then recursive invocation of triggers is
68701  ** disabled for backwards compatibility (p5 is set if this sub-program
68702  ** is really a trigger, not a foreign key action, and the flag set
68703  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
68704  **
68705  ** It is recursive invocation of triggers, at the SQL level, that is
68706  ** disabled. In some cases a single trigger may generate more than one
68707  ** SubProgram (if the trigger may be executed with more than one different
68708  ** ON CONFLICT algorithm). SubProgram structures associated with a
68709  ** single trigger all have the same value for the SubProgram.token
68710  ** variable.  */
68711  if( pOp->p5 ){
68712    u.cb.t = u.cb.pProgram->token;
68713    for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
68714    if( u.cb.pFrame ) break;
68715  }
68716
68717  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68718    rc = SQLITE_ERROR;
68719    sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68720    break;
68721  }
68722
68723  /* Register u.cb.pRt is used to store the memory required to save the state
68724  ** of the current program, and the memory required at runtime to execute
68725  ** the trigger program. If this trigger has been fired before, then u.cb.pRt
68726  ** is already allocated. Otherwise, it must be initialized.  */
68727  if( (u.cb.pRt->flags&MEM_Frame)==0 ){
68728    /* SubProgram.nMem is set to the number of memory cells used by the
68729    ** program stored in SubProgram.aOp. As well as these, one memory
68730    ** cell is required for each cursor used by the program. Set local
68731    ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
68732    */
68733    u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
68734    u.cb.nByte = ROUND8(sizeof(VdbeFrame))
68735              + u.cb.nMem * sizeof(Mem)
68736              + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
68737    u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
68738    if( !u.cb.pFrame ){
68739      goto no_mem;
68740    }
68741    sqlite3VdbeMemRelease(u.cb.pRt);
68742    u.cb.pRt->flags = MEM_Frame;
68743    u.cb.pRt->u.pFrame = u.cb.pFrame;
68744
68745    u.cb.pFrame->v = p;
68746    u.cb.pFrame->nChildMem = u.cb.nMem;
68747    u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
68748    u.cb.pFrame->pc = pc;
68749    u.cb.pFrame->aMem = p->aMem;
68750    u.cb.pFrame->nMem = p->nMem;
68751    u.cb.pFrame->apCsr = p->apCsr;
68752    u.cb.pFrame->nCursor = p->nCursor;
68753    u.cb.pFrame->aOp = p->aOp;
68754    u.cb.pFrame->nOp = p->nOp;
68755    u.cb.pFrame->token = u.cb.pProgram->token;
68756
68757    u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
68758    for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
68759      u.cb.pMem->flags = MEM_Null;
68760      u.cb.pMem->db = db;
68761    }
68762  }else{
68763    u.cb.pFrame = u.cb.pRt->u.pFrame;
68764    assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
68765    assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
68766    assert( pc==u.cb.pFrame->pc );
68767  }
68768
68769  p->nFrame++;
68770  u.cb.pFrame->pParent = p->pFrame;
68771  u.cb.pFrame->lastRowid = lastRowid;
68772  u.cb.pFrame->nChange = p->nChange;
68773  p->nChange = 0;
68774  p->pFrame = u.cb.pFrame;
68775  p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
68776  p->nMem = u.cb.pFrame->nChildMem;
68777  p->nCursor = (u16)u.cb.pFrame->nChildCsr;
68778  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68779  p->aOp = aOp = u.cb.pProgram->aOp;
68780  p->nOp = u.cb.pProgram->nOp;
68781  pc = -1;
68782
68783  break;
68784}
68785
68786/* Opcode: Param P1 P2 * * *
68787**
68788** This opcode is only ever present in sub-programs called via the
68789** OP_Program instruction. Copy a value currently stored in a memory
68790** cell of the calling (parent) frame to cell P2 in the current frames
68791** address space. This is used by trigger programs to access the new.*
68792** and old.* values.
68793**
68794** The address of the cell in the parent frame is determined by adding
68795** the value of the P1 argument to the value of the P1 argument to the
68796** calling OP_Program instruction.
68797*/
68798case OP_Param: {           /* out2-prerelease */
68799#if 0  /* local variables moved into u.cc */
68800  VdbeFrame *pFrame;
68801  Mem *pIn;
68802#endif /* local variables moved into u.cc */
68803  u.cc.pFrame = p->pFrame;
68804  u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
68805  sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
68806  break;
68807}
68808
68809#endif /* #ifndef SQLITE_OMIT_TRIGGER */
68810
68811#ifndef SQLITE_OMIT_FOREIGN_KEY
68812/* Opcode: FkCounter P1 P2 * * *
68813**
68814** Increment a "constraint counter" by P2 (P2 may be negative or positive).
68815** If P1 is non-zero, the database constraint counter is incremented
68816** (deferred foreign key constraints). Otherwise, if P1 is zero, the
68817** statement counter is incremented (immediate foreign key constraints).
68818*/
68819case OP_FkCounter: {
68820  if( pOp->p1 ){
68821    db->nDeferredCons += pOp->p2;
68822  }else{
68823    p->nFkConstraint += pOp->p2;
68824  }
68825  break;
68826}
68827
68828/* Opcode: FkIfZero P1 P2 * * *
68829**
68830** This opcode tests if a foreign key constraint-counter is currently zero.
68831** If so, jump to instruction P2. Otherwise, fall through to the next
68832** instruction.
68833**
68834** If P1 is non-zero, then the jump is taken if the database constraint-counter
68835** is zero (the one that counts deferred constraint violations). If P1 is
68836** zero, the jump is taken if the statement constraint-counter is zero
68837** (immediate foreign key constraint violations).
68838*/
68839case OP_FkIfZero: {         /* jump */
68840  if( pOp->p1 ){
68841    if( db->nDeferredCons==0 ) pc = pOp->p2-1;
68842  }else{
68843    if( p->nFkConstraint==0 ) pc = pOp->p2-1;
68844  }
68845  break;
68846}
68847#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
68848
68849#ifndef SQLITE_OMIT_AUTOINCREMENT
68850/* Opcode: MemMax P1 P2 * * *
68851**
68852** P1 is a register in the root frame of this VM (the root frame is
68853** different from the current frame if this instruction is being executed
68854** within a sub-program). Set the value of register P1 to the maximum of
68855** its current value and the value in register P2.
68856**
68857** This instruction throws an error if the memory cell is not initially
68858** an integer.
68859*/
68860case OP_MemMax: {        /* in2 */
68861#if 0  /* local variables moved into u.cd */
68862  Mem *pIn1;
68863  VdbeFrame *pFrame;
68864#endif /* local variables moved into u.cd */
68865  if( p->pFrame ){
68866    for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
68867    u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
68868  }else{
68869    u.cd.pIn1 = &aMem[pOp->p1];
68870  }
68871  assert( memIsValid(u.cd.pIn1) );
68872  sqlite3VdbeMemIntegerify(u.cd.pIn1);
68873  pIn2 = &aMem[pOp->p2];
68874  sqlite3VdbeMemIntegerify(pIn2);
68875  if( u.cd.pIn1->u.i<pIn2->u.i){
68876    u.cd.pIn1->u.i = pIn2->u.i;
68877  }
68878  break;
68879}
68880#endif /* SQLITE_OMIT_AUTOINCREMENT */
68881
68882/* Opcode: IfPos P1 P2 * * *
68883**
68884** If the value of register P1 is 1 or greater, jump to P2.
68885**
68886** It is illegal to use this instruction on a register that does
68887** not contain an integer.  An assertion fault will result if you try.
68888*/
68889case OP_IfPos: {        /* jump, in1 */
68890  pIn1 = &aMem[pOp->p1];
68891  assert( pIn1->flags&MEM_Int );
68892  if( pIn1->u.i>0 ){
68893     pc = pOp->p2 - 1;
68894  }
68895  break;
68896}
68897
68898/* Opcode: IfNeg P1 P2 * * *
68899**
68900** If the value of register P1 is less than zero, jump to P2.
68901**
68902** It is illegal to use this instruction on a register that does
68903** not contain an integer.  An assertion fault will result if you try.
68904*/
68905case OP_IfNeg: {        /* jump, in1 */
68906  pIn1 = &aMem[pOp->p1];
68907  assert( pIn1->flags&MEM_Int );
68908  if( pIn1->u.i<0 ){
68909     pc = pOp->p2 - 1;
68910  }
68911  break;
68912}
68913
68914/* Opcode: IfZero P1 P2 P3 * *
68915**
68916** The register P1 must contain an integer.  Add literal P3 to the
68917** value in register P1.  If the result is exactly 0, jump to P2.
68918**
68919** It is illegal to use this instruction on a register that does
68920** not contain an integer.  An assertion fault will result if you try.
68921*/
68922case OP_IfZero: {        /* jump, in1 */
68923  pIn1 = &aMem[pOp->p1];
68924  assert( pIn1->flags&MEM_Int );
68925  pIn1->u.i += pOp->p3;
68926  if( pIn1->u.i==0 ){
68927     pc = pOp->p2 - 1;
68928  }
68929  break;
68930}
68931
68932/* Opcode: AggStep * P2 P3 P4 P5
68933**
68934** Execute the step function for an aggregate.  The
68935** function has P5 arguments.   P4 is a pointer to the FuncDef
68936** structure that specifies the function.  Use register
68937** P3 as the accumulator.
68938**
68939** The P5 arguments are taken from register P2 and its
68940** successors.
68941*/
68942case OP_AggStep: {
68943#if 0  /* local variables moved into u.ce */
68944  int n;
68945  int i;
68946  Mem *pMem;
68947  Mem *pRec;
68948  sqlite3_context ctx;
68949  sqlite3_value **apVal;
68950#endif /* local variables moved into u.ce */
68951
68952  u.ce.n = pOp->p5;
68953  assert( u.ce.n>=0 );
68954  u.ce.pRec = &aMem[pOp->p2];
68955  u.ce.apVal = p->apArg;
68956  assert( u.ce.apVal || u.ce.n==0 );
68957  for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
68958    assert( memIsValid(u.ce.pRec) );
68959    u.ce.apVal[u.ce.i] = u.ce.pRec;
68960    memAboutToChange(p, u.ce.pRec);
68961    sqlite3VdbeMemStoreType(u.ce.pRec);
68962  }
68963  u.ce.ctx.pFunc = pOp->p4.pFunc;
68964  assert( pOp->p3>0 && pOp->p3<=p->nMem );
68965  u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
68966  u.ce.pMem->n++;
68967  u.ce.ctx.s.flags = MEM_Null;
68968  u.ce.ctx.s.z = 0;
68969  u.ce.ctx.s.zMalloc = 0;
68970  u.ce.ctx.s.xDel = 0;
68971  u.ce.ctx.s.db = db;
68972  u.ce.ctx.isError = 0;
68973  u.ce.ctx.pColl = 0;
68974  if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68975    assert( pOp>p->aOp );
68976    assert( pOp[-1].p4type==P4_COLLSEQ );
68977    assert( pOp[-1].opcode==OP_CollSeq );
68978    u.ce.ctx.pColl = pOp[-1].p4.pColl;
68979  }
68980  (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
68981  if( u.ce.ctx.isError ){
68982    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
68983    rc = u.ce.ctx.isError;
68984  }
68985
68986  sqlite3VdbeMemRelease(&u.ce.ctx.s);
68987
68988  break;
68989}
68990
68991/* Opcode: AggFinal P1 P2 * P4 *
68992**
68993** Execute the finalizer function for an aggregate.  P1 is
68994** the memory location that is the accumulator for the aggregate.
68995**
68996** P2 is the number of arguments that the step function takes and
68997** P4 is a pointer to the FuncDef for this function.  The P2
68998** argument is not used by this opcode.  It is only there to disambiguate
68999** functions that can take varying numbers of arguments.  The
69000** P4 argument is only needed for the degenerate case where
69001** the step function was not previously called.
69002*/
69003case OP_AggFinal: {
69004#if 0  /* local variables moved into u.cf */
69005  Mem *pMem;
69006#endif /* local variables moved into u.cf */
69007  assert( pOp->p1>0 && pOp->p1<=p->nMem );
69008  u.cf.pMem = &aMem[pOp->p1];
69009  assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
69010  rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
69011  if( rc ){
69012    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
69013  }
69014  sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
69015  UPDATE_MAX_BLOBSIZE(u.cf.pMem);
69016  if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
69017    goto too_big;
69018  }
69019  break;
69020}
69021
69022#ifndef SQLITE_OMIT_WAL
69023/* Opcode: Checkpoint P1 P2 P3 * *
69024**
69025** Checkpoint database P1. This is a no-op if P1 is not currently in
69026** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
69027** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
69028** SQLITE_BUSY or not, respectively.  Write the number of pages in the
69029** WAL after the checkpoint into mem[P3+1] and the number of pages
69030** in the WAL that have been checkpointed after the checkpoint
69031** completes into mem[P3+2].  However on an error, mem[P3+1] and
69032** mem[P3+2] are initialized to -1.
69033*/
69034case OP_Checkpoint: {
69035#if 0  /* local variables moved into u.cg */
69036  int i;                          /* Loop counter */
69037  int aRes[3];                    /* Results */
69038  Mem *pMem;                      /* Write results here */
69039#endif /* local variables moved into u.cg */
69040
69041  u.cg.aRes[0] = 0;
69042  u.cg.aRes[1] = u.cg.aRes[2] = -1;
69043  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
69044       || pOp->p2==SQLITE_CHECKPOINT_FULL
69045       || pOp->p2==SQLITE_CHECKPOINT_RESTART
69046  );
69047  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
69048  if( rc==SQLITE_BUSY ){
69049    rc = SQLITE_OK;
69050    u.cg.aRes[0] = 1;
69051  }
69052  for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
69053    sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
69054  }
69055  break;
69056};
69057#endif
69058
69059#ifndef SQLITE_OMIT_PRAGMA
69060/* Opcode: JournalMode P1 P2 P3 * P5
69061**
69062** Change the journal mode of database P1 to P3. P3 must be one of the
69063** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
69064** modes (delete, truncate, persist, off and memory), this is a simple
69065** operation. No IO is required.
69066**
69067** If changing into or out of WAL mode the procedure is more complicated.
69068**
69069** Write a string containing the final journal-mode to register P2.
69070*/
69071case OP_JournalMode: {    /* out2-prerelease */
69072#if 0  /* local variables moved into u.ch */
69073  Btree *pBt;                     /* Btree to change journal mode of */
69074  Pager *pPager;                  /* Pager associated with pBt */
69075  int eNew;                       /* New journal mode */
69076  int eOld;                       /* The old journal mode */
69077  const char *zFilename;          /* Name of database file for pPager */
69078#endif /* local variables moved into u.ch */
69079
69080  u.ch.eNew = pOp->p3;
69081  assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
69082       || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
69083       || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
69084       || u.ch.eNew==PAGER_JOURNALMODE_OFF
69085       || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
69086       || u.ch.eNew==PAGER_JOURNALMODE_WAL
69087       || u.ch.eNew==PAGER_JOURNALMODE_QUERY
69088  );
69089  assert( pOp->p1>=0 && pOp->p1<db->nDb );
69090
69091  u.ch.pBt = db->aDb[pOp->p1].pBt;
69092  u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
69093  u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
69094  if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
69095  if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
69096
69097#ifndef SQLITE_OMIT_WAL
69098  u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
69099
69100  /* Do not allow a transition to journal_mode=WAL for a database
69101  ** in temporary storage or if the VFS does not support shared memory
69102  */
69103  if( u.ch.eNew==PAGER_JOURNALMODE_WAL
69104   && (u.ch.zFilename[0]==0                         /* Temp file */
69105       || !sqlite3PagerWalSupported(u.ch.pPager))   /* No shared-memory support */
69106  ){
69107    u.ch.eNew = u.ch.eOld;
69108  }
69109
69110  if( (u.ch.eNew!=u.ch.eOld)
69111   && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
69112  ){
69113    if( !db->autoCommit || db->activeVdbeCnt>1 ){
69114      rc = SQLITE_ERROR;
69115      sqlite3SetString(&p->zErrMsg, db,
69116          "cannot change %s wal mode from within a transaction",
69117          (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69118      );
69119      break;
69120    }else{
69121
69122      if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
69123        /* If leaving WAL mode, close the log file. If successful, the call
69124        ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
69125        ** file. An EXCLUSIVE lock may still be held on the database file
69126        ** after a successful return.
69127        */
69128        rc = sqlite3PagerCloseWal(u.ch.pPager);
69129        if( rc==SQLITE_OK ){
69130          sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69131        }
69132      }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
69133        /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
69134        ** as an intermediate */
69135        sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
69136      }
69137
69138      /* Open a transaction on the database file. Regardless of the journal
69139      ** mode, this transaction always uses a rollback journal.
69140      */
69141      assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
69142      if( rc==SQLITE_OK ){
69143        rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69144      }
69145    }
69146  }
69147#endif /* ifndef SQLITE_OMIT_WAL */
69148
69149  if( rc ){
69150    u.ch.eNew = u.ch.eOld;
69151  }
69152  u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69153
69154  pOut = &aMem[pOp->p2];
69155  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69156  pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
69157  pOut->n = sqlite3Strlen30(pOut->z);
69158  pOut->enc = SQLITE_UTF8;
69159  sqlite3VdbeChangeEncoding(pOut, encoding);
69160  break;
69161};
69162#endif /* SQLITE_OMIT_PRAGMA */
69163
69164#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
69165/* Opcode: Vacuum * * * * *
69166**
69167** Vacuum the entire database.  This opcode will cause other virtual
69168** machines to be created and run.  It may not be called from within
69169** a transaction.
69170*/
69171case OP_Vacuum: {
69172  rc = sqlite3RunVacuum(&p->zErrMsg, db);
69173  break;
69174}
69175#endif
69176
69177#if !defined(SQLITE_OMIT_AUTOVACUUM)
69178/* Opcode: IncrVacuum P1 P2 * * *
69179**
69180** Perform a single step of the incremental vacuum procedure on
69181** the P1 database. If the vacuum has finished, jump to instruction
69182** P2. Otherwise, fall through to the next instruction.
69183*/
69184case OP_IncrVacuum: {        /* jump */
69185#if 0  /* local variables moved into u.ci */
69186  Btree *pBt;
69187#endif /* local variables moved into u.ci */
69188
69189  assert( pOp->p1>=0 && pOp->p1<db->nDb );
69190  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69191  u.ci.pBt = db->aDb[pOp->p1].pBt;
69192  rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
69193  if( rc==SQLITE_DONE ){
69194    pc = pOp->p2 - 1;
69195    rc = SQLITE_OK;
69196  }
69197  break;
69198}
69199#endif
69200
69201/* Opcode: Expire P1 * * * *
69202**
69203** Cause precompiled statements to become expired. An expired statement
69204** fails with an error code of SQLITE_SCHEMA if it is ever executed
69205** (via sqlite3_step()).
69206**
69207** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
69208** then only the currently executing statement is affected.
69209*/
69210case OP_Expire: {
69211  if( !pOp->p1 ){
69212    sqlite3ExpirePreparedStatements(db);
69213  }else{
69214    p->expired = 1;
69215  }
69216  break;
69217}
69218
69219#ifndef SQLITE_OMIT_SHARED_CACHE
69220/* Opcode: TableLock P1 P2 P3 P4 *
69221**
69222** Obtain a lock on a particular table. This instruction is only used when
69223** the shared-cache feature is enabled.
69224**
69225** P1 is the index of the database in sqlite3.aDb[] of the database
69226** on which the lock is acquired.  A readlock is obtained if P3==0 or
69227** a write lock if P3==1.
69228**
69229** P2 contains the root-page of the table to lock.
69230**
69231** P4 contains a pointer to the name of the table being locked. This is only
69232** used to generate an error message if the lock cannot be obtained.
69233*/
69234case OP_TableLock: {
69235  u8 isWriteLock = (u8)pOp->p3;
69236  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
69237    int p1 = pOp->p1;
69238    assert( p1>=0 && p1<db->nDb );
69239    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
69240    assert( isWriteLock==0 || isWriteLock==1 );
69241    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
69242    if( (rc&0xFF)==SQLITE_LOCKED ){
69243      const char *z = pOp->p4.z;
69244      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
69245    }
69246  }
69247  break;
69248}
69249#endif /* SQLITE_OMIT_SHARED_CACHE */
69250
69251#ifndef SQLITE_OMIT_VIRTUALTABLE
69252/* Opcode: VBegin * * * P4 *
69253**
69254** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
69255** xBegin method for that table.
69256**
69257** Also, whether or not P4 is set, check that this is not being called from
69258** within a callback to a virtual table xSync() method. If it is, the error
69259** code will be set to SQLITE_LOCKED.
69260*/
69261case OP_VBegin: {
69262#if 0  /* local variables moved into u.cj */
69263  VTable *pVTab;
69264#endif /* local variables moved into u.cj */
69265  u.cj.pVTab = pOp->p4.pVtab;
69266  rc = sqlite3VtabBegin(db, u.cj.pVTab);
69267  if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
69268  break;
69269}
69270#endif /* SQLITE_OMIT_VIRTUALTABLE */
69271
69272#ifndef SQLITE_OMIT_VIRTUALTABLE
69273/* Opcode: VCreate P1 * * P4 *
69274**
69275** P4 is the name of a virtual table in database P1. Call the xCreate method
69276** for that table.
69277*/
69278case OP_VCreate: {
69279  rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
69280  break;
69281}
69282#endif /* SQLITE_OMIT_VIRTUALTABLE */
69283
69284#ifndef SQLITE_OMIT_VIRTUALTABLE
69285/* Opcode: VDestroy P1 * * P4 *
69286**
69287** P4 is the name of a virtual table in database P1.  Call the xDestroy method
69288** of that table.
69289*/
69290case OP_VDestroy: {
69291  p->inVtabMethod = 2;
69292  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
69293  p->inVtabMethod = 0;
69294  break;
69295}
69296#endif /* SQLITE_OMIT_VIRTUALTABLE */
69297
69298#ifndef SQLITE_OMIT_VIRTUALTABLE
69299/* Opcode: VOpen P1 * * P4 *
69300**
69301** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69302** P1 is a cursor number.  This opcode opens a cursor to the virtual
69303** table and stores that cursor in P1.
69304*/
69305case OP_VOpen: {
69306#if 0  /* local variables moved into u.ck */
69307  VdbeCursor *pCur;
69308  sqlite3_vtab_cursor *pVtabCursor;
69309  sqlite3_vtab *pVtab;
69310  sqlite3_module *pModule;
69311#endif /* local variables moved into u.ck */
69312
69313  u.ck.pCur = 0;
69314  u.ck.pVtabCursor = 0;
69315  u.ck.pVtab = pOp->p4.pVtab->pVtab;
69316  u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
69317  assert(u.ck.pVtab && u.ck.pModule);
69318  rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
69319  importVtabErrMsg(p, u.ck.pVtab);
69320  if( SQLITE_OK==rc ){
69321    /* Initialize sqlite3_vtab_cursor base class */
69322    u.ck.pVtabCursor->pVtab = u.ck.pVtab;
69323
69324    /* Initialise vdbe cursor object */
69325    u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69326    if( u.ck.pCur ){
69327      u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
69328      u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
69329    }else{
69330      db->mallocFailed = 1;
69331      u.ck.pModule->xClose(u.ck.pVtabCursor);
69332    }
69333  }
69334  break;
69335}
69336#endif /* SQLITE_OMIT_VIRTUALTABLE */
69337
69338#ifndef SQLITE_OMIT_VIRTUALTABLE
69339/* Opcode: VFilter P1 P2 P3 P4 *
69340**
69341** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
69342** the filtered result set is empty.
69343**
69344** P4 is either NULL or a string that was generated by the xBestIndex
69345** method of the module.  The interpretation of the P4 string is left
69346** to the module implementation.
69347**
69348** This opcode invokes the xFilter method on the virtual table specified
69349** by P1.  The integer query plan parameter to xFilter is stored in register
69350** P3. Register P3+1 stores the argc parameter to be passed to the
69351** xFilter method. Registers P3+2..P3+1+argc are the argc
69352** additional parameters which are passed to
69353** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
69354**
69355** A jump is made to P2 if the result set after filtering would be empty.
69356*/
69357case OP_VFilter: {   /* jump */
69358#if 0  /* local variables moved into u.cl */
69359  int nArg;
69360  int iQuery;
69361  const sqlite3_module *pModule;
69362  Mem *pQuery;
69363  Mem *pArgc;
69364  sqlite3_vtab_cursor *pVtabCursor;
69365  sqlite3_vtab *pVtab;
69366  VdbeCursor *pCur;
69367  int res;
69368  int i;
69369  Mem **apArg;
69370#endif /* local variables moved into u.cl */
69371
69372  u.cl.pQuery = &aMem[pOp->p3];
69373  u.cl.pArgc = &u.cl.pQuery[1];
69374  u.cl.pCur = p->apCsr[pOp->p1];
69375  assert( memIsValid(u.cl.pQuery) );
69376  REGISTER_TRACE(pOp->p3, u.cl.pQuery);
69377  assert( u.cl.pCur->pVtabCursor );
69378  u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
69379  u.cl.pVtab = u.cl.pVtabCursor->pVtab;
69380  u.cl.pModule = u.cl.pVtab->pModule;
69381
69382  /* Grab the index number and argc parameters */
69383  assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
69384  u.cl.nArg = (int)u.cl.pArgc->u.i;
69385  u.cl.iQuery = (int)u.cl.pQuery->u.i;
69386
69387  /* Invoke the xFilter method */
69388  {
69389    u.cl.res = 0;
69390    u.cl.apArg = p->apArg;
69391    for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
69392      u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
69393      sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
69394    }
69395
69396    p->inVtabMethod = 1;
69397    rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
69398    p->inVtabMethod = 0;
69399    importVtabErrMsg(p, u.cl.pVtab);
69400    if( rc==SQLITE_OK ){
69401      u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
69402    }
69403
69404    if( u.cl.res ){
69405      pc = pOp->p2 - 1;
69406    }
69407  }
69408  u.cl.pCur->nullRow = 0;
69409
69410  break;
69411}
69412#endif /* SQLITE_OMIT_VIRTUALTABLE */
69413
69414#ifndef SQLITE_OMIT_VIRTUALTABLE
69415/* Opcode: VColumn P1 P2 P3 * *
69416**
69417** Store the value of the P2-th column of
69418** the row of the virtual-table that the
69419** P1 cursor is pointing to into register P3.
69420*/
69421case OP_VColumn: {
69422#if 0  /* local variables moved into u.cm */
69423  sqlite3_vtab *pVtab;
69424  const sqlite3_module *pModule;
69425  Mem *pDest;
69426  sqlite3_context sContext;
69427#endif /* local variables moved into u.cm */
69428
69429  VdbeCursor *pCur = p->apCsr[pOp->p1];
69430  assert( pCur->pVtabCursor );
69431  assert( pOp->p3>0 && pOp->p3<=p->nMem );
69432  u.cm.pDest = &aMem[pOp->p3];
69433  memAboutToChange(p, u.cm.pDest);
69434  if( pCur->nullRow ){
69435    sqlite3VdbeMemSetNull(u.cm.pDest);
69436    break;
69437  }
69438  u.cm.pVtab = pCur->pVtabCursor->pVtab;
69439  u.cm.pModule = u.cm.pVtab->pModule;
69440  assert( u.cm.pModule->xColumn );
69441  memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
69442
69443  /* The output cell may already have a buffer allocated. Move
69444  ** the current contents to u.cm.sContext.s so in case the user-function
69445  ** can use the already allocated buffer instead of allocating a
69446  ** new one.
69447  */
69448  sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
69449  MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
69450
69451  rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
69452  importVtabErrMsg(p, u.cm.pVtab);
69453  if( u.cm.sContext.isError ){
69454    rc = u.cm.sContext.isError;
69455  }
69456
69457  /* Copy the result of the function to the P3 register. We
69458  ** do this regardless of whether or not an error occurred to ensure any
69459  ** dynamic allocation in u.cm.sContext.s (a Mem struct) is  released.
69460  */
69461  sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
69462  sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
69463  REGISTER_TRACE(pOp->p3, u.cm.pDest);
69464  UPDATE_MAX_BLOBSIZE(u.cm.pDest);
69465
69466  if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
69467    goto too_big;
69468  }
69469  break;
69470}
69471#endif /* SQLITE_OMIT_VIRTUALTABLE */
69472
69473#ifndef SQLITE_OMIT_VIRTUALTABLE
69474/* Opcode: VNext P1 P2 * * *
69475**
69476** Advance virtual table P1 to the next row in its result set and
69477** jump to instruction P2.  Or, if the virtual table has reached
69478** the end of its result set, then fall through to the next instruction.
69479*/
69480case OP_VNext: {   /* jump */
69481#if 0  /* local variables moved into u.cn */
69482  sqlite3_vtab *pVtab;
69483  const sqlite3_module *pModule;
69484  int res;
69485  VdbeCursor *pCur;
69486#endif /* local variables moved into u.cn */
69487
69488  u.cn.res = 0;
69489  u.cn.pCur = p->apCsr[pOp->p1];
69490  assert( u.cn.pCur->pVtabCursor );
69491  if( u.cn.pCur->nullRow ){
69492    break;
69493  }
69494  u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
69495  u.cn.pModule = u.cn.pVtab->pModule;
69496  assert( u.cn.pModule->xNext );
69497
69498  /* Invoke the xNext() method of the module. There is no way for the
69499  ** underlying implementation to return an error if one occurs during
69500  ** xNext(). Instead, if an error occurs, true is returned (indicating that
69501  ** data is available) and the error code returned when xColumn or
69502  ** some other method is next invoked on the save virtual table cursor.
69503  */
69504  p->inVtabMethod = 1;
69505  rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
69506  p->inVtabMethod = 0;
69507  importVtabErrMsg(p, u.cn.pVtab);
69508  if( rc==SQLITE_OK ){
69509    u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
69510  }
69511
69512  if( !u.cn.res ){
69513    /* If there is data, jump to P2 */
69514    pc = pOp->p2 - 1;
69515  }
69516  break;
69517}
69518#endif /* SQLITE_OMIT_VIRTUALTABLE */
69519
69520#ifndef SQLITE_OMIT_VIRTUALTABLE
69521/* Opcode: VRename P1 * * P4 *
69522**
69523** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69524** This opcode invokes the corresponding xRename method. The value
69525** in register P1 is passed as the zName argument to the xRename method.
69526*/
69527case OP_VRename: {
69528#if 0  /* local variables moved into u.co */
69529  sqlite3_vtab *pVtab;
69530  Mem *pName;
69531#endif /* local variables moved into u.co */
69532
69533  u.co.pVtab = pOp->p4.pVtab->pVtab;
69534  u.co.pName = &aMem[pOp->p1];
69535  assert( u.co.pVtab->pModule->xRename );
69536  assert( memIsValid(u.co.pName) );
69537  REGISTER_TRACE(pOp->p1, u.co.pName);
69538  assert( u.co.pName->flags & MEM_Str );
69539  rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
69540  importVtabErrMsg(p, u.co.pVtab);
69541  p->expired = 0;
69542
69543  break;
69544}
69545#endif
69546
69547#ifndef SQLITE_OMIT_VIRTUALTABLE
69548/* Opcode: VUpdate P1 P2 P3 P4 *
69549**
69550** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69551** This opcode invokes the corresponding xUpdate method. P2 values
69552** are contiguous memory cells starting at P3 to pass to the xUpdate
69553** invocation. The value in register (P3+P2-1) corresponds to the
69554** p2th element of the argv array passed to xUpdate.
69555**
69556** The xUpdate method will do a DELETE or an INSERT or both.
69557** The argv[0] element (which corresponds to memory cell P3)
69558** is the rowid of a row to delete.  If argv[0] is NULL then no
69559** deletion occurs.  The argv[1] element is the rowid of the new
69560** row.  This can be NULL to have the virtual table select the new
69561** rowid for itself.  The subsequent elements in the array are
69562** the values of columns in the new row.
69563**
69564** If P2==1 then no insert is performed.  argv[0] is the rowid of
69565** a row to delete.
69566**
69567** P1 is a boolean flag. If it is set to true and the xUpdate call
69568** is successful, then the value returned by sqlite3_last_insert_rowid()
69569** is set to the value of the rowid for the row just inserted.
69570*/
69571case OP_VUpdate: {
69572#if 0  /* local variables moved into u.cp */
69573  sqlite3_vtab *pVtab;
69574  sqlite3_module *pModule;
69575  int nArg;
69576  int i;
69577  sqlite_int64 rowid;
69578  Mem **apArg;
69579  Mem *pX;
69580#endif /* local variables moved into u.cp */
69581
69582  assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
69583       || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
69584  );
69585  u.cp.pVtab = pOp->p4.pVtab->pVtab;
69586  u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
69587  u.cp.nArg = pOp->p2;
69588  assert( pOp->p4type==P4_VTAB );
69589  if( ALWAYS(u.cp.pModule->xUpdate) ){
69590    u8 vtabOnConflict = db->vtabOnConflict;
69591    u.cp.apArg = p->apArg;
69592    u.cp.pX = &aMem[pOp->p3];
69593    for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
69594      assert( memIsValid(u.cp.pX) );
69595      memAboutToChange(p, u.cp.pX);
69596      sqlite3VdbeMemStoreType(u.cp.pX);
69597      u.cp.apArg[u.cp.i] = u.cp.pX;
69598      u.cp.pX++;
69599    }
69600    db->vtabOnConflict = pOp->p5;
69601    rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
69602    db->vtabOnConflict = vtabOnConflict;
69603    importVtabErrMsg(p, u.cp.pVtab);
69604    if( rc==SQLITE_OK && pOp->p1 ){
69605      assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
69606      db->lastRowid = lastRowid = u.cp.rowid;
69607    }
69608    if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
69609      if( pOp->p5==OE_Ignore ){
69610        rc = SQLITE_OK;
69611      }else{
69612        p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
69613      }
69614    }else{
69615      p->nChange++;
69616    }
69617  }
69618  break;
69619}
69620#endif /* SQLITE_OMIT_VIRTUALTABLE */
69621
69622#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
69623/* Opcode: Pagecount P1 P2 * * *
69624**
69625** Write the current number of pages in database P1 to memory cell P2.
69626*/
69627case OP_Pagecount: {            /* out2-prerelease */
69628  pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
69629  break;
69630}
69631#endif
69632
69633
69634#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
69635/* Opcode: MaxPgcnt P1 P2 P3 * *
69636**
69637** Try to set the maximum page count for database P1 to the value in P3.
69638** Do not let the maximum page count fall below the current page count and
69639** do not change the maximum page count value if P3==0.
69640**
69641** Store the maximum page count after the change in register P2.
69642*/
69643case OP_MaxPgcnt: {            /* out2-prerelease */
69644  unsigned int newMax;
69645  Btree *pBt;
69646
69647  pBt = db->aDb[pOp->p1].pBt;
69648  newMax = 0;
69649  if( pOp->p3 ){
69650    newMax = sqlite3BtreeLastPage(pBt);
69651    if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
69652  }
69653  pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
69654  break;
69655}
69656#endif
69657
69658
69659#ifndef SQLITE_OMIT_TRACE
69660/* Opcode: Trace * * * P4 *
69661**
69662** If tracing is enabled (by the sqlite3_trace()) interface, then
69663** the UTF-8 string contained in P4 is emitted on the trace callback.
69664*/
69665case OP_Trace: {
69666#if 0  /* local variables moved into u.cq */
69667  char *zTrace;
69668  char *z;
69669#endif /* local variables moved into u.cq */
69670
69671  if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
69672    u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
69673    db->xTrace(db->pTraceArg, u.cq.z);
69674    sqlite3DbFree(db, u.cq.z);
69675  }
69676#ifdef SQLITE_DEBUG
69677  if( (db->flags & SQLITE_SqlTrace)!=0
69678   && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69679  ){
69680    sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
69681  }
69682#endif /* SQLITE_DEBUG */
69683  break;
69684}
69685#endif
69686
69687
69688/* Opcode: Noop * * * * *
69689**
69690** Do nothing.  This instruction is often useful as a jump
69691** destination.
69692*/
69693/*
69694** The magic Explain opcode are only inserted when explain==2 (which
69695** is to say when the EXPLAIN QUERY PLAN syntax is used.)
69696** This opcode records information from the optimizer.  It is the
69697** the same as a no-op.  This opcodesnever appears in a real VM program.
69698*/
69699default: {          /* This is really OP_Noop and OP_Explain */
69700  assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
69701  break;
69702}
69703
69704/*****************************************************************************
69705** The cases of the switch statement above this line should all be indented
69706** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
69707** readability.  From this point on down, the normal indentation rules are
69708** restored.
69709*****************************************************************************/
69710    }
69711
69712#ifdef VDBE_PROFILE
69713    {
69714      u64 elapsed = sqlite3Hwtime() - start;
69715      pOp->cycles += elapsed;
69716      pOp->cnt++;
69717#if 0
69718        fprintf(stdout, "%10llu ", elapsed);
69719        sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
69720#endif
69721    }
69722#endif
69723
69724    /* The following code adds nothing to the actual functionality
69725    ** of the program.  It is only here for testing and debugging.
69726    ** On the other hand, it does burn CPU cycles every time through
69727    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
69728    */
69729#ifndef NDEBUG
69730    assert( pc>=-1 && pc<p->nOp );
69731
69732#ifdef SQLITE_DEBUG
69733    if( p->trace ){
69734      if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
69735      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
69736        registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
69737      }
69738      if( pOp->opflags & OPFLG_OUT3 ){
69739        registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
69740      }
69741    }
69742#endif  /* SQLITE_DEBUG */
69743#endif  /* NDEBUG */
69744  }  /* The end of the for(;;) loop the loops through opcodes */
69745
69746  /* If we reach this point, it means that execution is finished with
69747  ** an error of some kind.
69748  */
69749vdbe_error_halt:
69750  assert( rc );
69751  p->rc = rc;
69752  testcase( sqlite3GlobalConfig.xLog!=0 );
69753  sqlite3_log(rc, "statement aborts at %d: [%s] %s",
69754                   pc, p->zSql, p->zErrMsg);
69755  sqlite3VdbeHalt(p);
69756  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
69757  rc = SQLITE_ERROR;
69758  if( resetSchemaOnFault>0 ){
69759    sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
69760  }
69761
69762  /* This is the only way out of this procedure.  We have to
69763  ** release the mutexes on btrees that were acquired at the
69764  ** top. */
69765vdbe_return:
69766  db->lastRowid = lastRowid;
69767  sqlite3VdbeLeave(p);
69768  return rc;
69769
69770  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
69771  ** is encountered.
69772  */
69773too_big:
69774  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
69775  rc = SQLITE_TOOBIG;
69776  goto vdbe_error_halt;
69777
69778  /* Jump to here if a malloc() fails.
69779  */
69780no_mem:
69781  db->mallocFailed = 1;
69782  sqlite3SetString(&p->zErrMsg, db, "out of memory");
69783  rc = SQLITE_NOMEM;
69784  goto vdbe_error_halt;
69785
69786  /* Jump to here for any other kind of fatal error.  The "rc" variable
69787  ** should hold the error number.
69788  */
69789abort_due_to_error:
69790  assert( p->zErrMsg==0 );
69791  if( db->mallocFailed ) rc = SQLITE_NOMEM;
69792  if( rc!=SQLITE_IOERR_NOMEM ){
69793    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
69794  }
69795  goto vdbe_error_halt;
69796
69797  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
69798  ** flag.
69799  */
69800abort_due_to_interrupt:
69801  assert( db->u1.isInterrupted );
69802  rc = SQLITE_INTERRUPT;
69803  p->rc = rc;
69804  sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
69805  goto vdbe_error_halt;
69806}
69807
69808/************** End of vdbe.c ************************************************/
69809/************** Begin file vdbeblob.c ****************************************/
69810/*
69811** 2007 May 1
69812**
69813** The author disclaims copyright to this source code.  In place of
69814** a legal notice, here is a blessing:
69815**
69816**    May you do good and not evil.
69817**    May you find forgiveness for yourself and forgive others.
69818**    May you share freely, never taking more than you give.
69819**
69820*************************************************************************
69821**
69822** This file contains code used to implement incremental BLOB I/O.
69823*/
69824
69825
69826#ifndef SQLITE_OMIT_INCRBLOB
69827
69828/*
69829** Valid sqlite3_blob* handles point to Incrblob structures.
69830*/
69831typedef struct Incrblob Incrblob;
69832struct Incrblob {
69833  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
69834  int nByte;              /* Size of open blob, in bytes */
69835  int iOffset;            /* Byte offset of blob in cursor data */
69836  int iCol;               /* Table column this handle is open on */
69837  BtCursor *pCsr;         /* Cursor pointing at blob row */
69838  sqlite3_stmt *pStmt;    /* Statement holding cursor open */
69839  sqlite3 *db;            /* The associated database */
69840};
69841
69842
69843/*
69844** This function is used by both blob_open() and blob_reopen(). It seeks
69845** the b-tree cursor associated with blob handle p to point to row iRow.
69846** If successful, SQLITE_OK is returned and subsequent calls to
69847** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
69848**
69849** If an error occurs, or if the specified row does not exist or does not
69850** contain a value of type TEXT or BLOB in the column nominated when the
69851** blob handle was opened, then an error code is returned and *pzErr may
69852** be set to point to a buffer containing an error message. It is the
69853** responsibility of the caller to free the error message buffer using
69854** sqlite3DbFree().
69855**
69856** If an error does occur, then the b-tree cursor is closed. All subsequent
69857** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
69858** immediately return SQLITE_ABORT.
69859*/
69860static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
69861  int rc;                         /* Error code */
69862  char *zErr = 0;                 /* Error message */
69863  Vdbe *v = (Vdbe *)p->pStmt;
69864
69865  /* Set the value of the SQL statements only variable to integer iRow.
69866  ** This is done directly instead of using sqlite3_bind_int64() to avoid
69867  ** triggering asserts related to mutexes.
69868  */
69869  assert( v->aVar[0].flags&MEM_Int );
69870  v->aVar[0].u.i = iRow;
69871
69872  rc = sqlite3_step(p->pStmt);
69873  if( rc==SQLITE_ROW ){
69874    u32 type = v->apCsr[0]->aType[p->iCol];
69875    if( type<12 ){
69876      zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
69877          type==0?"null": type==7?"real": "integer"
69878      );
69879      rc = SQLITE_ERROR;
69880      sqlite3_finalize(p->pStmt);
69881      p->pStmt = 0;
69882    }else{
69883      p->iOffset = v->apCsr[0]->aOffset[p->iCol];
69884      p->nByte = sqlite3VdbeSerialTypeLen(type);
69885      p->pCsr =  v->apCsr[0]->pCursor;
69886      sqlite3BtreeEnterCursor(p->pCsr);
69887      sqlite3BtreeCacheOverflow(p->pCsr);
69888      sqlite3BtreeLeaveCursor(p->pCsr);
69889    }
69890  }
69891
69892  if( rc==SQLITE_ROW ){
69893    rc = SQLITE_OK;
69894  }else if( p->pStmt ){
69895    rc = sqlite3_finalize(p->pStmt);
69896    p->pStmt = 0;
69897    if( rc==SQLITE_OK ){
69898      zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
69899      rc = SQLITE_ERROR;
69900    }else{
69901      zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
69902    }
69903  }
69904
69905  assert( rc!=SQLITE_OK || zErr==0 );
69906  assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
69907
69908  *pzErr = zErr;
69909  return rc;
69910}
69911
69912/*
69913** Open a blob handle.
69914*/
69915SQLITE_API int sqlite3_blob_open(
69916  sqlite3* db,            /* The database connection */
69917  const char *zDb,        /* The attached database containing the blob */
69918  const char *zTable,     /* The table containing the blob */
69919  const char *zColumn,    /* The column containing the blob */
69920  sqlite_int64 iRow,      /* The row containing the glob */
69921  int flags,              /* True -> read/write access, false -> read-only */
69922  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
69923){
69924  int nAttempt = 0;
69925  int iCol;               /* Index of zColumn in row-record */
69926
69927  /* This VDBE program seeks a btree cursor to the identified
69928  ** db/table/row entry. The reason for using a vdbe program instead
69929  ** of writing code to use the b-tree layer directly is that the
69930  ** vdbe program will take advantage of the various transaction,
69931  ** locking and error handling infrastructure built into the vdbe.
69932  **
69933  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
69934  ** Code external to the Vdbe then "borrows" the b-tree cursor and
69935  ** uses it to implement the blob_read(), blob_write() and
69936  ** blob_bytes() functions.
69937  **
69938  ** The sqlite3_blob_close() function finalizes the vdbe program,
69939  ** which closes the b-tree cursor and (possibly) commits the
69940  ** transaction.
69941  */
69942  static const VdbeOpList openBlob[] = {
69943    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
69944    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
69945    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
69946
69947    /* One of the following two instructions is replaced by an OP_Noop. */
69948    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
69949    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
69950
69951    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
69952    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
69953    {OP_Column, 0, 0, 1},          /* 7  */
69954    {OP_ResultRow, 1, 0, 0},       /* 8  */
69955    {OP_Goto, 0, 5, 0},            /* 9  */
69956    {OP_Close, 0, 0, 0},           /* 10 */
69957    {OP_Halt, 0, 0, 0},            /* 11 */
69958  };
69959
69960  int rc = SQLITE_OK;
69961  char *zErr = 0;
69962  Table *pTab;
69963  Parse *pParse = 0;
69964  Incrblob *pBlob = 0;
69965
69966  flags = !!flags;                /* flags = (flags ? 1 : 0); */
69967  *ppBlob = 0;
69968
69969  sqlite3_mutex_enter(db->mutex);
69970
69971  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
69972  if( !pBlob ) goto blob_open_out;
69973  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
69974  if( !pParse ) goto blob_open_out;
69975
69976  do {
69977    memset(pParse, 0, sizeof(Parse));
69978    pParse->db = db;
69979    sqlite3DbFree(db, zErr);
69980    zErr = 0;
69981
69982    sqlite3BtreeEnterAll(db);
69983    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
69984    if( pTab && IsVirtual(pTab) ){
69985      pTab = 0;
69986      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
69987    }
69988#ifndef SQLITE_OMIT_VIEW
69989    if( pTab && pTab->pSelect ){
69990      pTab = 0;
69991      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
69992    }
69993#endif
69994    if( !pTab ){
69995      if( pParse->zErrMsg ){
69996        sqlite3DbFree(db, zErr);
69997        zErr = pParse->zErrMsg;
69998        pParse->zErrMsg = 0;
69999      }
70000      rc = SQLITE_ERROR;
70001      sqlite3BtreeLeaveAll(db);
70002      goto blob_open_out;
70003    }
70004
70005    /* Now search pTab for the exact column. */
70006    for(iCol=0; iCol<pTab->nCol; iCol++) {
70007      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
70008        break;
70009      }
70010    }
70011    if( iCol==pTab->nCol ){
70012      sqlite3DbFree(db, zErr);
70013      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
70014      rc = SQLITE_ERROR;
70015      sqlite3BtreeLeaveAll(db);
70016      goto blob_open_out;
70017    }
70018
70019    /* If the value is being opened for writing, check that the
70020    ** column is not indexed, and that it is not part of a foreign key.
70021    ** It is against the rules to open a column to which either of these
70022    ** descriptions applies for writing.  */
70023    if( flags ){
70024      const char *zFault = 0;
70025      Index *pIdx;
70026#ifndef SQLITE_OMIT_FOREIGN_KEY
70027      if( db->flags&SQLITE_ForeignKeys ){
70028        /* Check that the column is not part of an FK child key definition. It
70029        ** is not necessary to check if it is part of a parent key, as parent
70030        ** key columns must be indexed. The check below will pick up this
70031        ** case.  */
70032        FKey *pFKey;
70033        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
70034          int j;
70035          for(j=0; j<pFKey->nCol; j++){
70036            if( pFKey->aCol[j].iFrom==iCol ){
70037              zFault = "foreign key";
70038            }
70039          }
70040        }
70041      }
70042#endif
70043      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
70044        int j;
70045        for(j=0; j<pIdx->nColumn; j++){
70046          if( pIdx->aiColumn[j]==iCol ){
70047            zFault = "indexed";
70048          }
70049        }
70050      }
70051      if( zFault ){
70052        sqlite3DbFree(db, zErr);
70053        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
70054        rc = SQLITE_ERROR;
70055        sqlite3BtreeLeaveAll(db);
70056        goto blob_open_out;
70057      }
70058    }
70059
70060    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
70061    assert( pBlob->pStmt || db->mallocFailed );
70062    if( pBlob->pStmt ){
70063      Vdbe *v = (Vdbe *)pBlob->pStmt;
70064      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
70065
70066      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
70067
70068
70069      /* Configure the OP_Transaction */
70070      sqlite3VdbeChangeP1(v, 0, iDb);
70071      sqlite3VdbeChangeP2(v, 0, flags);
70072
70073      /* Configure the OP_VerifyCookie */
70074      sqlite3VdbeChangeP1(v, 1, iDb);
70075      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
70076      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
70077
70078      /* Make sure a mutex is held on the table to be accessed */
70079      sqlite3VdbeUsesBtree(v, iDb);
70080
70081      /* Configure the OP_TableLock instruction */
70082#ifdef SQLITE_OMIT_SHARED_CACHE
70083      sqlite3VdbeChangeToNoop(v, 2);
70084#else
70085      sqlite3VdbeChangeP1(v, 2, iDb);
70086      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
70087      sqlite3VdbeChangeP3(v, 2, flags);
70088      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
70089#endif
70090
70091      /* Remove either the OP_OpenWrite or OpenRead. Set the P2
70092      ** parameter of the other to pTab->tnum.  */
70093      sqlite3VdbeChangeToNoop(v, 4 - flags);
70094      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
70095      sqlite3VdbeChangeP3(v, 3 + flags, iDb);
70096
70097      /* Configure the number of columns. Configure the cursor to
70098      ** think that the table has one more column than it really
70099      ** does. An OP_Column to retrieve this imaginary column will
70100      ** always return an SQL NULL. This is useful because it means
70101      ** we can invoke OP_Column to fill in the vdbe cursors type
70102      ** and offset cache without causing any IO.
70103      */
70104      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
70105      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
70106      if( !db->mallocFailed ){
70107        pParse->nVar = 1;
70108        pParse->nMem = 1;
70109        pParse->nTab = 1;
70110        sqlite3VdbeMakeReady(v, pParse);
70111      }
70112    }
70113
70114    pBlob->flags = flags;
70115    pBlob->iCol = iCol;
70116    pBlob->db = db;
70117    sqlite3BtreeLeaveAll(db);
70118    if( db->mallocFailed ){
70119      goto blob_open_out;
70120    }
70121    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
70122    rc = blobSeekToRow(pBlob, iRow, &zErr);
70123  } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
70124
70125blob_open_out:
70126  if( rc==SQLITE_OK && db->mallocFailed==0 ){
70127    *ppBlob = (sqlite3_blob *)pBlob;
70128  }else{
70129    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
70130    sqlite3DbFree(db, pBlob);
70131  }
70132  sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
70133  sqlite3DbFree(db, zErr);
70134  sqlite3StackFree(db, pParse);
70135  rc = sqlite3ApiExit(db, rc);
70136  sqlite3_mutex_leave(db->mutex);
70137  return rc;
70138}
70139
70140/*
70141** Close a blob handle that was previously created using
70142** sqlite3_blob_open().
70143*/
70144SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
70145  Incrblob *p = (Incrblob *)pBlob;
70146  int rc;
70147  sqlite3 *db;
70148
70149  if( p ){
70150    db = p->db;
70151    sqlite3_mutex_enter(db->mutex);
70152    rc = sqlite3_finalize(p->pStmt);
70153    sqlite3DbFree(db, p);
70154    sqlite3_mutex_leave(db->mutex);
70155  }else{
70156    rc = SQLITE_OK;
70157  }
70158  return rc;
70159}
70160
70161/*
70162** Perform a read or write operation on a blob
70163*/
70164static int blobReadWrite(
70165  sqlite3_blob *pBlob,
70166  void *z,
70167  int n,
70168  int iOffset,
70169  int (*xCall)(BtCursor*, u32, u32, void*)
70170){
70171  int rc;
70172  Incrblob *p = (Incrblob *)pBlob;
70173  Vdbe *v;
70174  sqlite3 *db;
70175
70176  if( p==0 ) return SQLITE_MISUSE_BKPT;
70177  db = p->db;
70178  sqlite3_mutex_enter(db->mutex);
70179  v = (Vdbe*)p->pStmt;
70180
70181  if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
70182    /* Request is out of range. Return a transient error. */
70183    rc = SQLITE_ERROR;
70184    sqlite3Error(db, SQLITE_ERROR, 0);
70185  }else if( v==0 ){
70186    /* If there is no statement handle, then the blob-handle has
70187    ** already been invalidated. Return SQLITE_ABORT in this case.
70188    */
70189    rc = SQLITE_ABORT;
70190  }else{
70191    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
70192    ** returned, clean-up the statement handle.
70193    */
70194    assert( db == v->db );
70195    sqlite3BtreeEnterCursor(p->pCsr);
70196    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
70197    sqlite3BtreeLeaveCursor(p->pCsr);
70198    if( rc==SQLITE_ABORT ){
70199      sqlite3VdbeFinalize(v);
70200      p->pStmt = 0;
70201    }else{
70202      db->errCode = rc;
70203      v->rc = rc;
70204    }
70205  }
70206  rc = sqlite3ApiExit(db, rc);
70207  sqlite3_mutex_leave(db->mutex);
70208  return rc;
70209}
70210
70211/*
70212** Read data from a blob handle.
70213*/
70214SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
70215  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
70216}
70217
70218/*
70219** Write data to a blob handle.
70220*/
70221SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
70222  return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
70223}
70224
70225/*
70226** Query a blob handle for the size of the data.
70227**
70228** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
70229** so no mutex is required for access.
70230*/
70231SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
70232  Incrblob *p = (Incrblob *)pBlob;
70233  return (p && p->pStmt) ? p->nByte : 0;
70234}
70235
70236/*
70237** Move an existing blob handle to point to a different row of the same
70238** database table.
70239**
70240** If an error occurs, or if the specified row does not exist or does not
70241** contain a blob or text value, then an error code is returned and the
70242** database handle error code and message set. If this happens, then all
70243** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
70244** immediately return SQLITE_ABORT.
70245*/
70246SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
70247  int rc;
70248  Incrblob *p = (Incrblob *)pBlob;
70249  sqlite3 *db;
70250
70251  if( p==0 ) return SQLITE_MISUSE_BKPT;
70252  db = p->db;
70253  sqlite3_mutex_enter(db->mutex);
70254
70255  if( p->pStmt==0 ){
70256    /* If there is no statement handle, then the blob-handle has
70257    ** already been invalidated. Return SQLITE_ABORT in this case.
70258    */
70259    rc = SQLITE_ABORT;
70260  }else{
70261    char *zErr;
70262    rc = blobSeekToRow(p, iRow, &zErr);
70263    if( rc!=SQLITE_OK ){
70264      sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
70265      sqlite3DbFree(db, zErr);
70266    }
70267    assert( rc!=SQLITE_SCHEMA );
70268  }
70269
70270  rc = sqlite3ApiExit(db, rc);
70271  assert( rc==SQLITE_OK || p->pStmt==0 );
70272  sqlite3_mutex_leave(db->mutex);
70273  return rc;
70274}
70275
70276#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
70277
70278/************** End of vdbeblob.c ********************************************/
70279/************** Begin file vdbesort.c ****************************************/
70280/*
70281** 2011 July 9
70282**
70283** The author disclaims copyright to this source code.  In place of
70284** a legal notice, here is a blessing:
70285**
70286**    May you do good and not evil.
70287**    May you find forgiveness for yourself and forgive others.
70288**    May you share freely, never taking more than you give.
70289**
70290*************************************************************************
70291** This file contains code for the VdbeSorter object, used in concert with
70292** a VdbeCursor to sort large numbers of keys (as may be required, for
70293** example, by CREATE INDEX statements on tables too large to fit in main
70294** memory).
70295*/
70296
70297
70298#ifndef SQLITE_OMIT_MERGE_SORT
70299
70300typedef struct VdbeSorterIter VdbeSorterIter;
70301typedef struct SorterRecord SorterRecord;
70302
70303/*
70304** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
70305**
70306** As keys are added to the sorter, they are written to disk in a series
70307** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
70308** the same as the cache-size allowed for temporary databases. In order
70309** to allow the caller to extract keys from the sorter in sorted order,
70310** all PMAs currently stored on disk must be merged together. This comment
70311** describes the data structure used to do so. The structure supports
70312** merging any number of arrays in a single pass with no redundant comparison
70313** operations.
70314**
70315** The aIter[] array contains an iterator for each of the PMAs being merged.
70316** An aIter[] iterator either points to a valid key or else is at EOF. For
70317** the purposes of the paragraphs below, we assume that the array is actually
70318** N elements in size, where N is the smallest power of 2 greater to or equal
70319** to the number of iterators being merged. The extra aIter[] elements are
70320** treated as if they are empty (always at EOF).
70321**
70322** The aTree[] array is also N elements in size. The value of N is stored in
70323** the VdbeSorter.nTree variable.
70324**
70325** The final (N/2) elements of aTree[] contain the results of comparing
70326** pairs of iterator keys together. Element i contains the result of
70327** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
70328** aTree element is set to the index of it.
70329**
70330** For the purposes of this comparison, EOF is considered greater than any
70331** other key value. If the keys are equal (only possible with two EOF
70332** values), it doesn't matter which index is stored.
70333**
70334** The (N/4) elements of aTree[] that preceed the final (N/2) described
70335** above contains the index of the smallest of each block of 4 iterators.
70336** And so on. So that aTree[1] contains the index of the iterator that
70337** currently points to the smallest key value. aTree[0] is unused.
70338**
70339** Example:
70340**
70341**     aIter[0] -> Banana
70342**     aIter[1] -> Feijoa
70343**     aIter[2] -> Elderberry
70344**     aIter[3] -> Currant
70345**     aIter[4] -> Grapefruit
70346**     aIter[5] -> Apple
70347**     aIter[6] -> Durian
70348**     aIter[7] -> EOF
70349**
70350**     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
70351**
70352** The current element is "Apple" (the value of the key indicated by
70353** iterator 5). When the Next() operation is invoked, iterator 5 will
70354** be advanced to the next key in its segment. Say the next key is
70355** "Eggplant":
70356**
70357**     aIter[5] -> Eggplant
70358**
70359** The contents of aTree[] are updated first by comparing the new iterator
70360** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
70361** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
70362** The value of iterator 6 - "Durian" - is now smaller than that of iterator
70363** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
70364** so the value written into element 1 of the array is 0. As follows:
70365**
70366**     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
70367**
70368** In other words, each time we advance to the next sorter element, log2(N)
70369** key comparison operations are required, where N is the number of segments
70370** being merged (rounded up to the next power of 2).
70371*/
70372struct VdbeSorter {
70373  int nInMemory;                  /* Current size of pRecord list as PMA */
70374  int nTree;                      /* Used size of aTree/aIter (power of 2) */
70375  VdbeSorterIter *aIter;          /* Array of iterators to merge */
70376  int *aTree;                     /* Current state of incremental merge */
70377  i64 iWriteOff;                  /* Current write offset within file pTemp1 */
70378  i64 iReadOff;                   /* Current read offset within file pTemp1 */
70379  sqlite3_file *pTemp1;           /* PMA file 1 */
70380  int nPMA;                       /* Number of PMAs stored in pTemp1 */
70381  SorterRecord *pRecord;          /* Head of in-memory record list */
70382  int mnPmaSize;                  /* Minimum PMA size, in bytes */
70383  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
70384  UnpackedRecord *pUnpacked;      /* Used to unpack keys */
70385};
70386
70387/*
70388** The following type is an iterator for a PMA. It caches the current key in
70389** variables nKey/aKey. If the iterator is at EOF, pFile==0.
70390*/
70391struct VdbeSorterIter {
70392  i64 iReadOff;                   /* Current read offset */
70393  i64 iEof;                       /* 1 byte past EOF for this iterator */
70394  sqlite3_file *pFile;            /* File iterator is reading from */
70395  int nAlloc;                     /* Bytes of space at aAlloc */
70396  u8 *aAlloc;                     /* Allocated space */
70397  int nKey;                       /* Number of bytes in key */
70398  u8 *aKey;                       /* Pointer to current key */
70399};
70400
70401/*
70402** A structure to store a single record. All in-memory records are connected
70403** together into a linked list headed at VdbeSorter.pRecord using the
70404** SorterRecord.pNext pointer.
70405*/
70406struct SorterRecord {
70407  void *pVal;
70408  int nVal;
70409  SorterRecord *pNext;
70410};
70411
70412/* Minimum allowable value for the VdbeSorter.nWorking variable */
70413#define SORTER_MIN_WORKING 10
70414
70415/* Maximum number of segments to merge in a single pass. */
70416#define SORTER_MAX_MERGE_COUNT 16
70417
70418/*
70419** Free all memory belonging to the VdbeSorterIter object passed as the second
70420** argument. All structure fields are set to zero before returning.
70421*/
70422static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
70423  sqlite3DbFree(db, pIter->aAlloc);
70424  memset(pIter, 0, sizeof(VdbeSorterIter));
70425}
70426
70427/*
70428** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
70429** no error occurs, or an SQLite error code if one does.
70430*/
70431static int vdbeSorterIterNext(
70432  sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
70433  VdbeSorterIter *pIter           /* Iterator to advance */
70434){
70435  int rc;                         /* Return Code */
70436  int nRead;                      /* Number of bytes read */
70437  int nRec = 0;                   /* Size of record in bytes */
70438  int iOff = 0;                   /* Size of serialized size varint in bytes */
70439
70440  assert( pIter->iEof>=pIter->iReadOff );
70441  if( pIter->iEof-pIter->iReadOff>5 ){
70442    nRead = 5;
70443  }else{
70444    nRead = (int)(pIter->iEof - pIter->iReadOff);
70445  }
70446  if( nRead<=0 ){
70447    /* This is an EOF condition */
70448    vdbeSorterIterZero(db, pIter);
70449    return SQLITE_OK;
70450  }
70451
70452  rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
70453  if( rc==SQLITE_OK ){
70454    iOff = getVarint32(pIter->aAlloc, nRec);
70455    if( (iOff+nRec)>nRead ){
70456      int nRead2;                   /* Number of extra bytes to read */
70457      if( (iOff+nRec)>pIter->nAlloc ){
70458        int nNew = pIter->nAlloc*2;
70459        while( (iOff+nRec)>nNew ) nNew = nNew*2;
70460        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
70461        if( !pIter->aAlloc ) return SQLITE_NOMEM;
70462        pIter->nAlloc = nNew;
70463      }
70464
70465      nRead2 = iOff + nRec - nRead;
70466      rc = sqlite3OsRead(
70467          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
70468      );
70469    }
70470  }
70471
70472  assert( rc!=SQLITE_OK || nRec>0 );
70473  pIter->iReadOff += iOff+nRec;
70474  pIter->nKey = nRec;
70475  pIter->aKey = &pIter->aAlloc[iOff];
70476  return rc;
70477}
70478
70479/*
70480** Write a single varint, value iVal, to file-descriptor pFile. Return
70481** SQLITE_OK if successful, or an SQLite error code if some error occurs.
70482**
70483** The value of *piOffset when this function is called is used as the byte
70484** offset in file pFile to write to. Before returning, *piOffset is
70485** incremented by the number of bytes written.
70486*/
70487static int vdbeSorterWriteVarint(
70488  sqlite3_file *pFile,            /* File to write to */
70489  i64 iVal,                       /* Value to write as a varint */
70490  i64 *piOffset                   /* IN/OUT: Write offset in file pFile */
70491){
70492  u8 aVarint[9];                  /* Buffer large enough for a varint */
70493  int nVarint;                    /* Number of used bytes in varint */
70494  int rc;                         /* Result of write() call */
70495
70496  nVarint = sqlite3PutVarint(aVarint, iVal);
70497  rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
70498  *piOffset += nVarint;
70499
70500  return rc;
70501}
70502
70503/*
70504** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
70505** successful, or an SQLite error code if some error occurs.
70506**
70507** The value of *piOffset when this function is called is used as the
70508** byte offset in file pFile from whence to read the varint. If successful
70509** (i.e. if no IO error occurs), then *piOffset is set to the offset of
70510** the first byte past the end of the varint before returning. *piVal is
70511** set to the integer value read. If an error occurs, the final values of
70512** both *piOffset and *piVal are undefined.
70513*/
70514static int vdbeSorterReadVarint(
70515  sqlite3_file *pFile,            /* File to read from */
70516  i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
70517  i64 *piVal                      /* OUT: Value read from file */
70518){
70519  u8 aVarint[9];                  /* Buffer large enough for a varint */
70520  i64 iOff = *piOffset;           /* Offset in file to read from */
70521  int rc;                         /* Return code */
70522
70523  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
70524  if( rc==SQLITE_OK ){
70525    *piOffset += getVarint(aVarint, (u64 *)piVal);
70526  }
70527
70528  return rc;
70529}
70530
70531/*
70532** Initialize iterator pIter to scan through the PMA stored in file pFile
70533** starting at offset iStart and ending at offset iEof-1. This function
70534** leaves the iterator pointing to the first key in the PMA (or EOF if the
70535** PMA is empty).
70536*/
70537static int vdbeSorterIterInit(
70538  sqlite3 *db,                    /* Database handle */
70539  VdbeSorter *pSorter,            /* Sorter object */
70540  i64 iStart,                     /* Start offset in pFile */
70541  VdbeSorterIter *pIter,          /* Iterator to populate */
70542  i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
70543){
70544  int rc;
70545
70546  assert( pSorter->iWriteOff>iStart );
70547  assert( pIter->aAlloc==0 );
70548  pIter->pFile = pSorter->pTemp1;
70549  pIter->iReadOff = iStart;
70550  pIter->nAlloc = 128;
70551  pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
70552  if( !pIter->aAlloc ){
70553    rc = SQLITE_NOMEM;
70554  }else{
70555    i64 nByte;                         /* Total size of PMA in bytes */
70556    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
70557    *pnByte += nByte;
70558    pIter->iEof = pIter->iReadOff + nByte;
70559  }
70560  if( rc==SQLITE_OK ){
70561    rc = vdbeSorterIterNext(db, pIter);
70562  }
70563  return rc;
70564}
70565
70566
70567/*
70568** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
70569** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
70570** used by the comparison. If an error occurs, return an SQLite error code.
70571** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
70572** value, depending on whether key1 is smaller, equal to or larger than key2.
70573**
70574** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
70575** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
70576** is true and key1 contains even a single NULL value, it is considered to
70577** be less than key2. Even if key2 also contains NULL values.
70578**
70579** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
70580** has been allocated and contains an unpacked record that is used as key2.
70581*/
70582static void vdbeSorterCompare(
70583  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
70584  int bOmitRowid,                 /* Ignore rowid field at end of keys */
70585  void *pKey1, int nKey1,         /* Left side of comparison */
70586  void *pKey2, int nKey2,         /* Right side of comparison */
70587  int *pRes                       /* OUT: Result of comparison */
70588){
70589  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
70590  VdbeSorter *pSorter = pCsr->pSorter;
70591  UnpackedRecord *r2 = pSorter->pUnpacked;
70592  int i;
70593
70594  if( pKey2 ){
70595    sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
70596  }
70597
70598  if( bOmitRowid ){
70599    r2->nField = pKeyInfo->nField;
70600    assert( r2->nField>0 );
70601    for(i=0; i<r2->nField; i++){
70602      if( r2->aMem[i].flags & MEM_Null ){
70603        *pRes = -1;
70604        return;
70605      }
70606    }
70607    r2->flags |= UNPACKED_PREFIX_MATCH;
70608  }
70609
70610  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
70611}
70612
70613/*
70614** This function is called to compare two iterator keys when merging
70615** multiple b-tree segments. Parameter iOut is the index of the aTree[]
70616** value to recalculate.
70617*/
70618static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
70619  VdbeSorter *pSorter = pCsr->pSorter;
70620  int i1;
70621  int i2;
70622  int iRes;
70623  VdbeSorterIter *p1;
70624  VdbeSorterIter *p2;
70625
70626  assert( iOut<pSorter->nTree && iOut>0 );
70627
70628  if( iOut>=(pSorter->nTree/2) ){
70629    i1 = (iOut - pSorter->nTree/2) * 2;
70630    i2 = i1 + 1;
70631  }else{
70632    i1 = pSorter->aTree[iOut*2];
70633    i2 = pSorter->aTree[iOut*2+1];
70634  }
70635
70636  p1 = &pSorter->aIter[i1];
70637  p2 = &pSorter->aIter[i2];
70638
70639  if( p1->pFile==0 ){
70640    iRes = i2;
70641  }else if( p2->pFile==0 ){
70642    iRes = i1;
70643  }else{
70644    int res;
70645    assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
70646    vdbeSorterCompare(
70647        pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
70648    );
70649    if( res<=0 ){
70650      iRes = i1;
70651    }else{
70652      iRes = i2;
70653    }
70654  }
70655
70656  pSorter->aTree[iOut] = iRes;
70657  return SQLITE_OK;
70658}
70659
70660/*
70661** Initialize the temporary index cursor just opened as a sorter cursor.
70662*/
70663SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
70664  int pgsz;                       /* Page size of main database */
70665  int mxCache;                    /* Cache size */
70666  VdbeSorter *pSorter;            /* The new sorter */
70667  char *d;                        /* Dummy */
70668
70669  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
70670  pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
70671  if( pSorter==0 ){
70672    return SQLITE_NOMEM;
70673  }
70674
70675  pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
70676  if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
70677  assert( pSorter->pUnpacked==(UnpackedRecord *)d );
70678
70679  if( !sqlite3TempInMemory(db) ){
70680    pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
70681    pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
70682    mxCache = db->aDb[0].pSchema->cache_size;
70683    if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
70684    pSorter->mxPmaSize = mxCache * pgsz;
70685  }
70686
70687  return SQLITE_OK;
70688}
70689
70690/*
70691** Free the list of sorted records starting at pRecord.
70692*/
70693static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
70694  SorterRecord *p;
70695  SorterRecord *pNext;
70696  for(p=pRecord; p; p=pNext){
70697    pNext = p->pNext;
70698    sqlite3DbFree(db, p);
70699  }
70700}
70701
70702/*
70703** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
70704*/
70705SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
70706  VdbeSorter *pSorter = pCsr->pSorter;
70707  if( pSorter ){
70708    if( pSorter->aIter ){
70709      int i;
70710      for(i=0; i<pSorter->nTree; i++){
70711        vdbeSorterIterZero(db, &pSorter->aIter[i]);
70712      }
70713      sqlite3DbFree(db, pSorter->aIter);
70714    }
70715    if( pSorter->pTemp1 ){
70716      sqlite3OsCloseFree(pSorter->pTemp1);
70717    }
70718    vdbeSorterRecordFree(db, pSorter->pRecord);
70719    sqlite3DbFree(db, pSorter->pUnpacked);
70720    sqlite3DbFree(db, pSorter);
70721    pCsr->pSorter = 0;
70722  }
70723}
70724
70725/*
70726** Allocate space for a file-handle and open a temporary file. If successful,
70727** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
70728** Otherwise, set *ppFile to 0 and return an SQLite error code.
70729*/
70730static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
70731  int dummy;
70732  return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
70733      SQLITE_OPEN_TEMP_JOURNAL |
70734      SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
70735      SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
70736  );
70737}
70738
70739/*
70740** Merge the two sorted lists p1 and p2 into a single list.
70741** Set *ppOut to the head of the new list.
70742*/
70743static void vdbeSorterMerge(
70744  VdbeCursor *pCsr,               /* For pKeyInfo */
70745  SorterRecord *p1,               /* First list to merge */
70746  SorterRecord *p2,               /* Second list to merge */
70747  SorterRecord **ppOut            /* OUT: Head of merged list */
70748){
70749  SorterRecord *pFinal = 0;
70750  SorterRecord **pp = &pFinal;
70751  void *pVal2 = p2 ? p2->pVal : 0;
70752
70753  while( p1 && p2 ){
70754    int res;
70755    vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
70756    if( res<=0 ){
70757      *pp = p1;
70758      pp = &p1->pNext;
70759      p1 = p1->pNext;
70760      pVal2 = 0;
70761    }else{
70762      *pp = p2;
70763       pp = &p2->pNext;
70764      p2 = p2->pNext;
70765      if( p2==0 ) break;
70766      pVal2 = p2->pVal;
70767    }
70768  }
70769  *pp = p1 ? p1 : p2;
70770  *ppOut = pFinal;
70771}
70772
70773/*
70774** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
70775** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
70776** occurs.
70777*/
70778static int vdbeSorterSort(VdbeCursor *pCsr){
70779  int i;
70780  SorterRecord **aSlot;
70781  SorterRecord *p;
70782  VdbeSorter *pSorter = pCsr->pSorter;
70783
70784  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
70785  if( !aSlot ){
70786    return SQLITE_NOMEM;
70787  }
70788
70789  p = pSorter->pRecord;
70790  while( p ){
70791    SorterRecord *pNext = p->pNext;
70792    p->pNext = 0;
70793    for(i=0; aSlot[i]; i++){
70794      vdbeSorterMerge(pCsr, p, aSlot[i], &p);
70795      aSlot[i] = 0;
70796    }
70797    aSlot[i] = p;
70798    p = pNext;
70799  }
70800
70801  p = 0;
70802  for(i=0; i<64; i++){
70803    vdbeSorterMerge(pCsr, p, aSlot[i], &p);
70804  }
70805  pSorter->pRecord = p;
70806
70807  sqlite3_free(aSlot);
70808  return SQLITE_OK;
70809}
70810
70811
70812/*
70813** Write the current contents of the in-memory linked-list to a PMA. Return
70814** SQLITE_OK if successful, or an SQLite error code otherwise.
70815**
70816** The format of a PMA is:
70817**
70818**     * A varint. This varint contains the total number of bytes of content
70819**       in the PMA (not including the varint itself).
70820**
70821**     * One or more records packed end-to-end in order of ascending keys.
70822**       Each record consists of a varint followed by a blob of data (the
70823**       key). The varint is the number of bytes in the blob of data.
70824*/
70825static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
70826  int rc = SQLITE_OK;             /* Return code */
70827  VdbeSorter *pSorter = pCsr->pSorter;
70828
70829  if( pSorter->nInMemory==0 ){
70830    assert( pSorter->pRecord==0 );
70831    return rc;
70832  }
70833
70834  rc = vdbeSorterSort(pCsr);
70835
70836  /* If the first temporary PMA file has not been opened, open it now. */
70837  if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
70838    rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
70839    assert( rc!=SQLITE_OK || pSorter->pTemp1 );
70840    assert( pSorter->iWriteOff==0 );
70841    assert( pSorter->nPMA==0 );
70842  }
70843
70844  if( rc==SQLITE_OK ){
70845    i64 iOff = pSorter->iWriteOff;
70846    SorterRecord *p;
70847    SorterRecord *pNext = 0;
70848    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
70849
70850    pSorter->nPMA++;
70851    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
70852    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
70853      pNext = p->pNext;
70854      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
70855
70856      if( rc==SQLITE_OK ){
70857        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
70858        iOff += p->nVal;
70859      }
70860
70861      sqlite3DbFree(db, p);
70862    }
70863
70864    /* This assert verifies that unless an error has occurred, the size of
70865    ** the PMA on disk is the same as the expected size stored in
70866    ** pSorter->nInMemory. */
70867    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
70868          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
70869    ));
70870
70871    pSorter->iWriteOff = iOff;
70872    if( rc==SQLITE_OK ){
70873      /* Terminate each file with 8 extra bytes so that from any offset
70874      ** in the file we can always read 9 bytes without a SHORT_READ error */
70875      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
70876    }
70877    pSorter->pRecord = p;
70878  }
70879
70880  return rc;
70881}
70882
70883/*
70884** Add a record to the sorter.
70885*/
70886SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
70887  sqlite3 *db,                    /* Database handle */
70888  VdbeCursor *pCsr,               /* Sorter cursor */
70889  Mem *pVal                       /* Memory cell containing record */
70890){
70891  VdbeSorter *pSorter = pCsr->pSorter;
70892  int rc = SQLITE_OK;             /* Return Code */
70893  SorterRecord *pNew;             /* New list element */
70894
70895  assert( pSorter );
70896  pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
70897
70898  pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
70899  if( pNew==0 ){
70900    rc = SQLITE_NOMEM;
70901  }else{
70902    pNew->pVal = (void *)&pNew[1];
70903    memcpy(pNew->pVal, pVal->z, pVal->n);
70904    pNew->nVal = pVal->n;
70905    pNew->pNext = pSorter->pRecord;
70906    pSorter->pRecord = pNew;
70907  }
70908
70909  /* See if the contents of the sorter should now be written out. They
70910  ** are written out when either of the following are true:
70911  **
70912  **   * The total memory allocated for the in-memory list is greater
70913  **     than (page-size * cache-size), or
70914  **
70915  **   * The total memory allocated for the in-memory list is greater
70916  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
70917  */
70918  if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
70919        (pSorter->nInMemory>pSorter->mxPmaSize)
70920     || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
70921  )){
70922    rc = vdbeSorterListToPMA(db, pCsr);
70923    pSorter->nInMemory = 0;
70924  }
70925
70926  return rc;
70927}
70928
70929/*
70930** Helper function for sqlite3VdbeSorterRewind().
70931*/
70932static int vdbeSorterInitMerge(
70933  sqlite3 *db,                    /* Database handle */
70934  VdbeCursor *pCsr,               /* Cursor handle for this sorter */
70935  i64 *pnByte                     /* Sum of bytes in all opened PMAs */
70936){
70937  VdbeSorter *pSorter = pCsr->pSorter;
70938  int rc = SQLITE_OK;             /* Return code */
70939  int i;                          /* Used to iterator through aIter[] */
70940  i64 nByte = 0;                  /* Total bytes in all opened PMAs */
70941
70942  /* Initialize the iterators. */
70943  for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
70944    VdbeSorterIter *pIter = &pSorter->aIter[i];
70945    rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
70946    pSorter->iReadOff = pIter->iEof;
70947    assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
70948    if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
70949  }
70950
70951  /* Initialize the aTree[] array. */
70952  for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
70953    rc = vdbeSorterDoCompare(pCsr, i);
70954  }
70955
70956  *pnByte = nByte;
70957  return rc;
70958}
70959
70960/*
70961** Once the sorter has been populated, this function is called to prepare
70962** for iterating through its contents in sorted order.
70963*/
70964SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70965  VdbeSorter *pSorter = pCsr->pSorter;
70966  int rc;                         /* Return code */
70967  sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
70968  i64 iWrite2 = 0;                /* Write offset for pTemp2 */
70969  int nIter;                      /* Number of iterators used */
70970  int nByte;                      /* Bytes of space required for aIter/aTree */
70971  int N = 2;                      /* Power of 2 >= nIter */
70972
70973  assert( pSorter );
70974
70975  /* If no data has been written to disk, then do not do so now. Instead,
70976  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
70977  ** from the in-memory list.  */
70978  if( pSorter->nPMA==0 ){
70979    *pbEof = !pSorter->pRecord;
70980    assert( pSorter->aTree==0 );
70981    return vdbeSorterSort(pCsr);
70982  }
70983
70984  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
70985  rc = vdbeSorterListToPMA(db, pCsr);
70986  if( rc!=SQLITE_OK ) return rc;
70987
70988  /* Allocate space for aIter[] and aTree[]. */
70989  nIter = pSorter->nPMA;
70990  if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
70991  assert( nIter>0 );
70992  while( N<nIter ) N += N;
70993  nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
70994  pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
70995  if( !pSorter->aIter ) return SQLITE_NOMEM;
70996  pSorter->aTree = (int *)&pSorter->aIter[N];
70997  pSorter->nTree = N;
70998
70999  do {
71000    int iNew;                     /* Index of new, merged, PMA */
71001
71002    for(iNew=0;
71003        rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
71004        iNew++
71005    ){
71006      i64 nWrite;                 /* Number of bytes in new PMA */
71007
71008      /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
71009      ** initialize an iterator for each of them and break out of the loop.
71010      ** These iterators will be incrementally merged as the VDBE layer calls
71011      ** sqlite3VdbeSorterNext().
71012      **
71013      ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
71014      ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
71015      ** are merged into a single PMA that is written to file pTemp2.
71016      */
71017      rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
71018      assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
71019      if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71020        break;
71021      }
71022
71023      /* Open the second temp file, if it is not already open. */
71024      if( pTemp2==0 ){
71025        assert( iWrite2==0 );
71026        rc = vdbeSorterOpenTempFile(db, &pTemp2);
71027      }
71028
71029      if( rc==SQLITE_OK ){
71030        rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
71031      }
71032
71033      if( rc==SQLITE_OK ){
71034        int bEof = 0;
71035        while( rc==SQLITE_OK && bEof==0 ){
71036          int nToWrite;
71037          VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71038          assert( pIter->pFile );
71039          nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
71040          rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
71041          iWrite2 += nToWrite;
71042          if( rc==SQLITE_OK ){
71043            rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
71044          }
71045        }
71046      }
71047    }
71048
71049    if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71050      break;
71051    }else{
71052      sqlite3_file *pTmp = pSorter->pTemp1;
71053      pSorter->nPMA = iNew;
71054      pSorter->pTemp1 = pTemp2;
71055      pTemp2 = pTmp;
71056      pSorter->iWriteOff = iWrite2;
71057      pSorter->iReadOff = 0;
71058      iWrite2 = 0;
71059    }
71060  }while( rc==SQLITE_OK );
71061
71062  if( pTemp2 ){
71063    sqlite3OsCloseFree(pTemp2);
71064  }
71065  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71066  return rc;
71067}
71068
71069/*
71070** Advance to the next element in the sorter.
71071*/
71072SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
71073  VdbeSorter *pSorter = pCsr->pSorter;
71074  int rc;                         /* Return code */
71075
71076  if( pSorter->aTree ){
71077    int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
71078    int i;                        /* Index of aTree[] to recalculate */
71079
71080    rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
71081    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
71082      rc = vdbeSorterDoCompare(pCsr, i);
71083    }
71084
71085    *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71086  }else{
71087    SorterRecord *pFree = pSorter->pRecord;
71088    pSorter->pRecord = pFree->pNext;
71089    pFree->pNext = 0;
71090    vdbeSorterRecordFree(db, pFree);
71091    *pbEof = !pSorter->pRecord;
71092    rc = SQLITE_OK;
71093  }
71094  return rc;
71095}
71096
71097/*
71098** Return a pointer to a buffer owned by the sorter that contains the
71099** current key.
71100*/
71101static void *vdbeSorterRowkey(
71102  VdbeSorter *pSorter,            /* Sorter object */
71103  int *pnKey                      /* OUT: Size of current key in bytes */
71104){
71105  void *pKey;
71106  if( pSorter->aTree ){
71107    VdbeSorterIter *pIter;
71108    pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71109    *pnKey = pIter->nKey;
71110    pKey = pIter->aKey;
71111  }else{
71112    *pnKey = pSorter->pRecord->nVal;
71113    pKey = pSorter->pRecord->pVal;
71114  }
71115  return pKey;
71116}
71117
71118/*
71119** Copy the current sorter key into the memory cell pOut.
71120*/
71121SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
71122  VdbeSorter *pSorter = pCsr->pSorter;
71123  void *pKey; int nKey;           /* Sorter key to copy into pOut */
71124
71125  pKey = vdbeSorterRowkey(pSorter, &nKey);
71126  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
71127    return SQLITE_NOMEM;
71128  }
71129  pOut->n = nKey;
71130  MemSetTypeFlag(pOut, MEM_Blob);
71131  memcpy(pOut->z, pKey, nKey);
71132
71133  return SQLITE_OK;
71134}
71135
71136/*
71137** Compare the key in memory cell pVal with the key that the sorter cursor
71138** passed as the first argument currently points to. For the purposes of
71139** the comparison, ignore the rowid field at the end of each record.
71140**
71141** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
71142** Otherwise, set *pRes to a negative, zero or positive value if the
71143** key in pVal is smaller than, equal to or larger than the current sorter
71144** key.
71145*/
71146SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
71147  VdbeCursor *pCsr,               /* Sorter cursor */
71148  Mem *pVal,                      /* Value to compare to current sorter key */
71149  int *pRes                       /* OUT: Result of comparison */
71150){
71151  VdbeSorter *pSorter = pCsr->pSorter;
71152  void *pKey; int nKey;           /* Sorter key to compare pVal with */
71153
71154  pKey = vdbeSorterRowkey(pSorter, &nKey);
71155  vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
71156  return SQLITE_OK;
71157}
71158
71159#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
71160
71161/************** End of vdbesort.c ********************************************/
71162/************** Begin file journal.c *****************************************/
71163/*
71164** 2007 August 22
71165**
71166** The author disclaims copyright to this source code.  In place of
71167** a legal notice, here is a blessing:
71168**
71169**    May you do good and not evil.
71170**    May you find forgiveness for yourself and forgive others.
71171**    May you share freely, never taking more than you give.
71172**
71173*************************************************************************
71174**
71175** This file implements a special kind of sqlite3_file object used
71176** by SQLite to create journal files if the atomic-write optimization
71177** is enabled.
71178**
71179** The distinctive characteristic of this sqlite3_file is that the
71180** actual on disk file is created lazily. When the file is created,
71181** the caller specifies a buffer size for an in-memory buffer to
71182** be used to service read() and write() requests. The actual file
71183** on disk is not created or populated until either:
71184**
71185**   1) The in-memory representation grows too large for the allocated
71186**      buffer, or
71187**   2) The sqlite3JournalCreate() function is called.
71188*/
71189#ifdef SQLITE_ENABLE_ATOMIC_WRITE
71190
71191
71192/*
71193** A JournalFile object is a subclass of sqlite3_file used by
71194** as an open file handle for journal files.
71195*/
71196struct JournalFile {
71197  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
71198  int nBuf;                       /* Size of zBuf[] in bytes */
71199  char *zBuf;                     /* Space to buffer journal writes */
71200  int iSize;                      /* Amount of zBuf[] currently used */
71201  int flags;                      /* xOpen flags */
71202  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
71203  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
71204  const char *zJournal;           /* Name of the journal file */
71205};
71206typedef struct JournalFile JournalFile;
71207
71208/*
71209** If it does not already exists, create and populate the on-disk file
71210** for JournalFile p.
71211*/
71212static int createFile(JournalFile *p){
71213  int rc = SQLITE_OK;
71214  if( !p->pReal ){
71215    sqlite3_file *pReal = (sqlite3_file *)&p[1];
71216    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
71217    if( rc==SQLITE_OK ){
71218      p->pReal = pReal;
71219      if( p->iSize>0 ){
71220        assert(p->iSize<=p->nBuf);
71221        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
71222      }
71223    }
71224  }
71225  return rc;
71226}
71227
71228/*
71229** Close the file.
71230*/
71231static int jrnlClose(sqlite3_file *pJfd){
71232  JournalFile *p = (JournalFile *)pJfd;
71233  if( p->pReal ){
71234    sqlite3OsClose(p->pReal);
71235  }
71236  sqlite3_free(p->zBuf);
71237  return SQLITE_OK;
71238}
71239
71240/*
71241** Read data from the file.
71242*/
71243static int jrnlRead(
71244  sqlite3_file *pJfd,    /* The journal file from which to read */
71245  void *zBuf,            /* Put the results here */
71246  int iAmt,              /* Number of bytes to read */
71247  sqlite_int64 iOfst     /* Begin reading at this offset */
71248){
71249  int rc = SQLITE_OK;
71250  JournalFile *p = (JournalFile *)pJfd;
71251  if( p->pReal ){
71252    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
71253  }else if( (iAmt+iOfst)>p->iSize ){
71254    rc = SQLITE_IOERR_SHORT_READ;
71255  }else{
71256    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
71257  }
71258  return rc;
71259}
71260
71261/*
71262** Write data to the file.
71263*/
71264static int jrnlWrite(
71265  sqlite3_file *pJfd,    /* The journal file into which to write */
71266  const void *zBuf,      /* Take data to be written from here */
71267  int iAmt,              /* Number of bytes to write */
71268  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
71269){
71270  int rc = SQLITE_OK;
71271  JournalFile *p = (JournalFile *)pJfd;
71272  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
71273    rc = createFile(p);
71274  }
71275  if( rc==SQLITE_OK ){
71276    if( p->pReal ){
71277      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
71278    }else{
71279      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
71280      if( p->iSize<(iOfst+iAmt) ){
71281        p->iSize = (iOfst+iAmt);
71282      }
71283    }
71284  }
71285  return rc;
71286}
71287
71288/*
71289** Truncate the file.
71290*/
71291static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
71292  int rc = SQLITE_OK;
71293  JournalFile *p = (JournalFile *)pJfd;
71294  if( p->pReal ){
71295    rc = sqlite3OsTruncate(p->pReal, size);
71296  }else if( size<p->iSize ){
71297    p->iSize = size;
71298  }
71299  return rc;
71300}
71301
71302/*
71303** Sync the file.
71304*/
71305static int jrnlSync(sqlite3_file *pJfd, int flags){
71306  int rc;
71307  JournalFile *p = (JournalFile *)pJfd;
71308  if( p->pReal ){
71309    rc = sqlite3OsSync(p->pReal, flags);
71310  }else{
71311    rc = SQLITE_OK;
71312  }
71313  return rc;
71314}
71315
71316/*
71317** Query the size of the file in bytes.
71318*/
71319static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
71320  int rc = SQLITE_OK;
71321  JournalFile *p = (JournalFile *)pJfd;
71322  if( p->pReal ){
71323    rc = sqlite3OsFileSize(p->pReal, pSize);
71324  }else{
71325    *pSize = (sqlite_int64) p->iSize;
71326  }
71327  return rc;
71328}
71329
71330/*
71331** Table of methods for JournalFile sqlite3_file object.
71332*/
71333static struct sqlite3_io_methods JournalFileMethods = {
71334  1,             /* iVersion */
71335  jrnlClose,     /* xClose */
71336  jrnlRead,      /* xRead */
71337  jrnlWrite,     /* xWrite */
71338  jrnlTruncate,  /* xTruncate */
71339  jrnlSync,      /* xSync */
71340  jrnlFileSize,  /* xFileSize */
71341  0,             /* xLock */
71342  0,             /* xUnlock */
71343  0,             /* xCheckReservedLock */
71344  0,             /* xFileControl */
71345  0,             /* xSectorSize */
71346  0,             /* xDeviceCharacteristics */
71347  0,             /* xShmMap */
71348  0,             /* xShmLock */
71349  0,             /* xShmBarrier */
71350  0              /* xShmUnmap */
71351};
71352
71353/*
71354** Open a journal file.
71355*/
71356SQLITE_PRIVATE int sqlite3JournalOpen(
71357  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
71358  const char *zName,         /* Name of the journal file */
71359  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
71360  int flags,                 /* Opening flags */
71361  int nBuf                   /* Bytes buffered before opening the file */
71362){
71363  JournalFile *p = (JournalFile *)pJfd;
71364  memset(p, 0, sqlite3JournalSize(pVfs));
71365  if( nBuf>0 ){
71366    p->zBuf = sqlite3MallocZero(nBuf);
71367    if( !p->zBuf ){
71368      return SQLITE_NOMEM;
71369    }
71370  }else{
71371    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
71372  }
71373  p->pMethod = &JournalFileMethods;
71374  p->nBuf = nBuf;
71375  p->flags = flags;
71376  p->zJournal = zName;
71377  p->pVfs = pVfs;
71378  return SQLITE_OK;
71379}
71380
71381/*
71382** If the argument p points to a JournalFile structure, and the underlying
71383** file has not yet been created, create it now.
71384*/
71385SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
71386  if( p->pMethods!=&JournalFileMethods ){
71387    return SQLITE_OK;
71388  }
71389  return createFile((JournalFile *)p);
71390}
71391
71392/*
71393** Return the number of bytes required to store a JournalFile that uses vfs
71394** pVfs to create the underlying on-disk files.
71395*/
71396SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
71397  return (pVfs->szOsFile+sizeof(JournalFile));
71398}
71399#endif
71400
71401/************** End of journal.c *********************************************/
71402/************** Begin file memjournal.c **************************************/
71403/*
71404** 2008 October 7
71405**
71406** The author disclaims copyright to this source code.  In place of
71407** a legal notice, here is a blessing:
71408**
71409**    May you do good and not evil.
71410**    May you find forgiveness for yourself and forgive others.
71411**    May you share freely, never taking more than you give.
71412**
71413*************************************************************************
71414**
71415** This file contains code use to implement an in-memory rollback journal.
71416** The in-memory rollback journal is used to journal transactions for
71417** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
71418*/
71419
71420/* Forward references to internal structures */
71421typedef struct MemJournal MemJournal;
71422typedef struct FilePoint FilePoint;
71423typedef struct FileChunk FileChunk;
71424
71425/* Space to hold the rollback journal is allocated in increments of
71426** this many bytes.
71427**
71428** The size chosen is a little less than a power of two.  That way,
71429** the FileChunk object will have a size that almost exactly fills
71430** a power-of-two allocation.  This mimimizes wasted space in power-of-two
71431** memory allocators.
71432*/
71433#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
71434
71435/* Macro to find the minimum of two numeric values.
71436*/
71437#ifndef MIN
71438# define MIN(x,y) ((x)<(y)?(x):(y))
71439#endif
71440
71441/*
71442** The rollback journal is composed of a linked list of these structures.
71443*/
71444struct FileChunk {
71445  FileChunk *pNext;               /* Next chunk in the journal */
71446  u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
71447};
71448
71449/*
71450** An instance of this object serves as a cursor into the rollback journal.
71451** The cursor can be either for reading or writing.
71452*/
71453struct FilePoint {
71454  sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
71455  FileChunk *pChunk;              /* Specific chunk into which cursor points */
71456};
71457
71458/*
71459** This subclass is a subclass of sqlite3_file.  Each open memory-journal
71460** is an instance of this class.
71461*/
71462struct MemJournal {
71463  sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
71464  FileChunk *pFirst;              /* Head of in-memory chunk-list */
71465  FilePoint endpoint;             /* Pointer to the end of the file */
71466  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
71467};
71468
71469/*
71470** Read data from the in-memory journal file.  This is the implementation
71471** of the sqlite3_vfs.xRead method.
71472*/
71473static int memjrnlRead(
71474  sqlite3_file *pJfd,    /* The journal file from which to read */
71475  void *zBuf,            /* Put the results here */
71476  int iAmt,              /* Number of bytes to read */
71477  sqlite_int64 iOfst     /* Begin reading at this offset */
71478){
71479  MemJournal *p = (MemJournal *)pJfd;
71480  u8 *zOut = zBuf;
71481  int nRead = iAmt;
71482  int iChunkOffset;
71483  FileChunk *pChunk;
71484
71485  /* SQLite never tries to read past the end of a rollback journal file */
71486  assert( iOfst+iAmt<=p->endpoint.iOffset );
71487
71488  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
71489    sqlite3_int64 iOff = 0;
71490    for(pChunk=p->pFirst;
71491        ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
71492        pChunk=pChunk->pNext
71493    ){
71494      iOff += JOURNAL_CHUNKSIZE;
71495    }
71496  }else{
71497    pChunk = p->readpoint.pChunk;
71498  }
71499
71500  iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
71501  do {
71502    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
71503    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
71504    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
71505    zOut += nCopy;
71506    nRead -= iSpace;
71507    iChunkOffset = 0;
71508  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
71509  p->readpoint.iOffset = iOfst+iAmt;
71510  p->readpoint.pChunk = pChunk;
71511
71512  return SQLITE_OK;
71513}
71514
71515/*
71516** Write data to the file.
71517*/
71518static int memjrnlWrite(
71519  sqlite3_file *pJfd,    /* The journal file into which to write */
71520  const void *zBuf,      /* Take data to be written from here */
71521  int iAmt,              /* Number of bytes to write */
71522  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
71523){
71524  MemJournal *p = (MemJournal *)pJfd;
71525  int nWrite = iAmt;
71526  u8 *zWrite = (u8 *)zBuf;
71527
71528  /* An in-memory journal file should only ever be appended to. Random
71529  ** access writes are not required by sqlite.
71530  */
71531  assert( iOfst==p->endpoint.iOffset );
71532  UNUSED_PARAMETER(iOfst);
71533
71534  while( nWrite>0 ){
71535    FileChunk *pChunk = p->endpoint.pChunk;
71536    int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
71537    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
71538
71539    if( iChunkOffset==0 ){
71540      /* New chunk is required to extend the file. */
71541      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
71542      if( !pNew ){
71543        return SQLITE_IOERR_NOMEM;
71544      }
71545      pNew->pNext = 0;
71546      if( pChunk ){
71547        assert( p->pFirst );
71548        pChunk->pNext = pNew;
71549      }else{
71550        assert( !p->pFirst );
71551        p->pFirst = pNew;
71552      }
71553      p->endpoint.pChunk = pNew;
71554    }
71555
71556    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
71557    zWrite += iSpace;
71558    nWrite -= iSpace;
71559    p->endpoint.iOffset += iSpace;
71560  }
71561
71562  return SQLITE_OK;
71563}
71564
71565/*
71566** Truncate the file.
71567*/
71568static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
71569  MemJournal *p = (MemJournal *)pJfd;
71570  FileChunk *pChunk;
71571  assert(size==0);
71572  UNUSED_PARAMETER(size);
71573  pChunk = p->pFirst;
71574  while( pChunk ){
71575    FileChunk *pTmp = pChunk;
71576    pChunk = pChunk->pNext;
71577    sqlite3_free(pTmp);
71578  }
71579  sqlite3MemJournalOpen(pJfd);
71580  return SQLITE_OK;
71581}
71582
71583/*
71584** Close the file.
71585*/
71586static int memjrnlClose(sqlite3_file *pJfd){
71587  memjrnlTruncate(pJfd, 0);
71588  return SQLITE_OK;
71589}
71590
71591
71592/*
71593** Sync the file.
71594**
71595** Syncing an in-memory journal is a no-op.  And, in fact, this routine
71596** is never called in a working implementation.  This implementation
71597** exists purely as a contingency, in case some malfunction in some other
71598** part of SQLite causes Sync to be called by mistake.
71599*/
71600static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
71601  UNUSED_PARAMETER2(NotUsed, NotUsed2);
71602  return SQLITE_OK;
71603}
71604
71605/*
71606** Query the size of the file in bytes.
71607*/
71608static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
71609  MemJournal *p = (MemJournal *)pJfd;
71610  *pSize = (sqlite_int64) p->endpoint.iOffset;
71611  return SQLITE_OK;
71612}
71613
71614/*
71615** Table of methods for MemJournal sqlite3_file object.
71616*/
71617static const struct sqlite3_io_methods MemJournalMethods = {
71618  1,                /* iVersion */
71619  memjrnlClose,     /* xClose */
71620  memjrnlRead,      /* xRead */
71621  memjrnlWrite,     /* xWrite */
71622  memjrnlTruncate,  /* xTruncate */
71623  memjrnlSync,      /* xSync */
71624  memjrnlFileSize,  /* xFileSize */
71625  0,                /* xLock */
71626  0,                /* xUnlock */
71627  0,                /* xCheckReservedLock */
71628  0,                /* xFileControl */
71629  0,                /* xSectorSize */
71630  0,                /* xDeviceCharacteristics */
71631  0,                /* xShmMap */
71632  0,                /* xShmLock */
71633  0,                /* xShmBarrier */
71634  0                 /* xShmUnlock */
71635};
71636
71637/*
71638** Open a journal file.
71639*/
71640SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
71641  MemJournal *p = (MemJournal *)pJfd;
71642  assert( EIGHT_BYTE_ALIGNMENT(p) );
71643  memset(p, 0, sqlite3MemJournalSize());
71644  p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
71645}
71646
71647/*
71648** Return true if the file-handle passed as an argument is
71649** an in-memory journal
71650*/
71651SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
71652  return pJfd->pMethods==&MemJournalMethods;
71653}
71654
71655/*
71656** Return the number of bytes required to store a MemJournal file descriptor.
71657*/
71658SQLITE_PRIVATE int sqlite3MemJournalSize(void){
71659  return sizeof(MemJournal);
71660}
71661
71662/************** End of memjournal.c ******************************************/
71663/************** Begin file walker.c ******************************************/
71664/*
71665** 2008 August 16
71666**
71667** The author disclaims copyright to this source code.  In place of
71668** a legal notice, here is a blessing:
71669**
71670**    May you do good and not evil.
71671**    May you find forgiveness for yourself and forgive others.
71672**    May you share freely, never taking more than you give.
71673**
71674*************************************************************************
71675** This file contains routines used for walking the parser tree for
71676** an SQL statement.
71677*/
71678/* #include <stdlib.h> */
71679/* #include <string.h> */
71680
71681
71682/*
71683** Walk an expression tree.  Invoke the callback once for each node
71684** of the expression, while decending.  (In other words, the callback
71685** is invoked before visiting children.)
71686**
71687** The return value from the callback should be one of the WRC_*
71688** constants to specify how to proceed with the walk.
71689**
71690**    WRC_Continue      Continue descending down the tree.
71691**
71692**    WRC_Prune         Do not descend into child nodes.  But allow
71693**                      the walk to continue with sibling nodes.
71694**
71695**    WRC_Abort         Do no more callbacks.  Unwind the stack and
71696**                      return the top-level walk call.
71697**
71698** The return value from this routine is WRC_Abort to abandon the tree walk
71699** and WRC_Continue to continue.
71700*/
71701SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
71702  int rc;
71703  if( pExpr==0 ) return WRC_Continue;
71704  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
71705  testcase( ExprHasProperty(pExpr, EP_Reduced) );
71706  rc = pWalker->xExprCallback(pWalker, pExpr);
71707  if( rc==WRC_Continue
71708              && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
71709    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
71710    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
71711    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
71712      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
71713    }else{
71714      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
71715    }
71716  }
71717  return rc & WRC_Abort;
71718}
71719
71720/*
71721** Call sqlite3WalkExpr() for every expression in list p or until
71722** an abort request is seen.
71723*/
71724SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
71725  int i;
71726  struct ExprList_item *pItem;
71727  if( p ){
71728    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
71729      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
71730    }
71731  }
71732  return WRC_Continue;
71733}
71734
71735/*
71736** Walk all expressions associated with SELECT statement p.  Do
71737** not invoke the SELECT callback on p, but do (of course) invoke
71738** any expr callbacks and SELECT callbacks that come from subqueries.
71739** Return WRC_Abort or WRC_Continue.
71740*/
71741SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
71742  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
71743  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
71744  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
71745  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
71746  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
71747  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
71748  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
71749  return WRC_Continue;
71750}
71751
71752/*
71753** Walk the parse trees associated with all subqueries in the
71754** FROM clause of SELECT statement p.  Do not invoke the select
71755** callback on p, but do invoke it on each FROM clause subquery
71756** and on any subqueries further down in the tree.  Return
71757** WRC_Abort or WRC_Continue;
71758*/
71759SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
71760  SrcList *pSrc;
71761  int i;
71762  struct SrcList_item *pItem;
71763
71764  pSrc = p->pSrc;
71765  if( ALWAYS(pSrc) ){
71766    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
71767      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
71768        return WRC_Abort;
71769      }
71770    }
71771  }
71772  return WRC_Continue;
71773}
71774
71775/*
71776** Call sqlite3WalkExpr() for every expression in Select statement p.
71777** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
71778** on the compound select chain, p->pPrior.
71779**
71780** Return WRC_Continue under normal conditions.  Return WRC_Abort if
71781** there is an abort request.
71782**
71783** If the Walker does not have an xSelectCallback() then this routine
71784** is a no-op returning WRC_Continue.
71785*/
71786SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
71787  int rc;
71788  if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
71789  rc = WRC_Continue;
71790  while( p  ){
71791    rc = pWalker->xSelectCallback(pWalker, p);
71792    if( rc ) break;
71793    if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
71794    if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
71795    p = p->pPrior;
71796  }
71797  return rc & WRC_Abort;
71798}
71799
71800/************** End of walker.c **********************************************/
71801/************** Begin file resolve.c *****************************************/
71802/*
71803** 2008 August 18
71804**
71805** The author disclaims copyright to this source code.  In place of
71806** a legal notice, here is a blessing:
71807**
71808**    May you do good and not evil.
71809**    May you find forgiveness for yourself and forgive others.
71810**    May you share freely, never taking more than you give.
71811**
71812*************************************************************************
71813**
71814** This file contains routines used for walking the parser tree and
71815** resolve all identifiers by associating them with a particular
71816** table and column.
71817*/
71818/* #include <stdlib.h> */
71819/* #include <string.h> */
71820
71821/*
71822** Turn the pExpr expression into an alias for the iCol-th column of the
71823** result set in pEList.
71824**
71825** If the result set column is a simple column reference, then this routine
71826** makes an exact copy.  But for any other kind of expression, this
71827** routine make a copy of the result set column as the argument to the
71828** TK_AS operator.  The TK_AS operator causes the expression to be
71829** evaluated just once and then reused for each alias.
71830**
71831** The reason for suppressing the TK_AS term when the expression is a simple
71832** column reference is so that the column reference will be recognized as
71833** usable by indices within the WHERE clause processing logic.
71834**
71835** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
71836** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
71837**
71838**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
71839**
71840** Is equivalent to:
71841**
71842**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
71843**
71844** The result of random()%5 in the GROUP BY clause is probably different
71845** from the result in the result-set.  We might fix this someday.  Or
71846** then again, we might not...
71847*/
71848static void resolveAlias(
71849  Parse *pParse,         /* Parsing context */
71850  ExprList *pEList,      /* A result set */
71851  int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
71852  Expr *pExpr,           /* Transform this into an alias to the result set */
71853  const char *zType      /* "GROUP" or "ORDER" or "" */
71854){
71855  Expr *pOrig;           /* The iCol-th column of the result set */
71856  Expr *pDup;            /* Copy of pOrig */
71857  sqlite3 *db;           /* The database connection */
71858
71859  assert( iCol>=0 && iCol<pEList->nExpr );
71860  pOrig = pEList->a[iCol].pExpr;
71861  assert( pOrig!=0 );
71862  assert( pOrig->flags & EP_Resolved );
71863  db = pParse->db;
71864  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
71865    pDup = sqlite3ExprDup(db, pOrig, 0);
71866    pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
71867    if( pDup==0 ) return;
71868    if( pEList->a[iCol].iAlias==0 ){
71869      pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
71870    }
71871    pDup->iTable = pEList->a[iCol].iAlias;
71872  }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
71873    pDup = sqlite3ExprDup(db, pOrig, 0);
71874    if( pDup==0 ) return;
71875  }else{
71876    char *zToken = pOrig->u.zToken;
71877    assert( zToken!=0 );
71878    pOrig->u.zToken = 0;
71879    pDup = sqlite3ExprDup(db, pOrig, 0);
71880    pOrig->u.zToken = zToken;
71881    if( pDup==0 ) return;
71882    assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
71883    pDup->flags2 |= EP2_MallocedToken;
71884    pDup->u.zToken = sqlite3DbStrDup(db, zToken);
71885  }
71886  if( pExpr->flags & EP_ExpCollate ){
71887    pDup->pColl = pExpr->pColl;
71888    pDup->flags |= EP_ExpCollate;
71889  }
71890
71891  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
71892  ** prevents ExprDelete() from deleting the Expr structure itself,
71893  ** allowing it to be repopulated by the memcpy() on the following line.
71894  */
71895  ExprSetProperty(pExpr, EP_Static);
71896  sqlite3ExprDelete(db, pExpr);
71897  memcpy(pExpr, pDup, sizeof(*pExpr));
71898  sqlite3DbFree(db, pDup);
71899}
71900
71901/*
71902** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
71903** that name in the set of source tables in pSrcList and make the pExpr
71904** expression node refer back to that source column.  The following changes
71905** are made to pExpr:
71906**
71907**    pExpr->iDb           Set the index in db->aDb[] of the database X
71908**                         (even if X is implied).
71909**    pExpr->iTable        Set to the cursor number for the table obtained
71910**                         from pSrcList.
71911**    pExpr->pTab          Points to the Table structure of X.Y (even if
71912**                         X and/or Y are implied.)
71913**    pExpr->iColumn       Set to the column number within the table.
71914**    pExpr->op            Set to TK_COLUMN.
71915**    pExpr->pLeft         Any expression this points to is deleted
71916**    pExpr->pRight        Any expression this points to is deleted.
71917**
71918** The zDb variable is the name of the database (the "X").  This value may be
71919** NULL meaning that name is of the form Y.Z or Z.  Any available database
71920** can be used.  The zTable variable is the name of the table (the "Y").  This
71921** value can be NULL if zDb is also NULL.  If zTable is NULL it
71922** means that the form of the name is Z and that columns from any table
71923** can be used.
71924**
71925** If the name cannot be resolved unambiguously, leave an error message
71926** in pParse and return WRC_Abort.  Return WRC_Prune on success.
71927*/
71928static int lookupName(
71929  Parse *pParse,       /* The parsing context */
71930  const char *zDb,     /* Name of the database containing table, or NULL */
71931  const char *zTab,    /* Name of table containing column, or NULL */
71932  const char *zCol,    /* Name of the column. */
71933  NameContext *pNC,    /* The name context used to resolve the name */
71934  Expr *pExpr          /* Make this EXPR node point to the selected column */
71935){
71936  int i, j;            /* Loop counters */
71937  int cnt = 0;                      /* Number of matching column names */
71938  int cntTab = 0;                   /* Number of matching table names */
71939  sqlite3 *db = pParse->db;         /* The database connection */
71940  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
71941  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
71942  NameContext *pTopNC = pNC;        /* First namecontext in the list */
71943  Schema *pSchema = 0;              /* Schema of the expression */
71944  int isTrigger = 0;
71945
71946  assert( pNC );     /* the name context cannot be NULL. */
71947  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
71948  assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
71949
71950  /* Initialize the node to no-match */
71951  pExpr->iTable = -1;
71952  pExpr->pTab = 0;
71953  ExprSetIrreducible(pExpr);
71954
71955  /* Start at the inner-most context and move outward until a match is found */
71956  while( pNC && cnt==0 ){
71957    ExprList *pEList;
71958    SrcList *pSrcList = pNC->pSrcList;
71959
71960    if( pSrcList ){
71961      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
71962        Table *pTab;
71963        int iDb;
71964        Column *pCol;
71965
71966        pTab = pItem->pTab;
71967        assert( pTab!=0 && pTab->zName!=0 );
71968        iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
71969        assert( pTab->nCol>0 );
71970        if( zTab ){
71971          if( pItem->zAlias ){
71972            char *zTabName = pItem->zAlias;
71973            if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
71974          }else{
71975            char *zTabName = pTab->zName;
71976            if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
71977              continue;
71978            }
71979            if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
71980              continue;
71981            }
71982          }
71983        }
71984        if( 0==(cntTab++) ){
71985          pExpr->iTable = pItem->iCursor;
71986          pExpr->pTab = pTab;
71987          pSchema = pTab->pSchema;
71988          pMatch = pItem;
71989        }
71990        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
71991          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
71992            IdList *pUsing;
71993            cnt++;
71994            pExpr->iTable = pItem->iCursor;
71995            pExpr->pTab = pTab;
71996            pMatch = pItem;
71997            pSchema = pTab->pSchema;
71998            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
71999            pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
72000            if( i<pSrcList->nSrc-1 ){
72001              if( pItem[1].jointype & JT_NATURAL ){
72002                /* If this match occurred in the left table of a natural join,
72003                ** then skip the right table to avoid a duplicate match */
72004                pItem++;
72005                i++;
72006              }else if( (pUsing = pItem[1].pUsing)!=0 ){
72007                /* If this match occurs on a column that is in the USING clause
72008                ** of a join, skip the search of the right table of the join
72009                ** to avoid a duplicate match there. */
72010                int k;
72011                for(k=0; k<pUsing->nId; k++){
72012                  if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
72013                    pItem++;
72014                    i++;
72015                    break;
72016                  }
72017                }
72018              }
72019            }
72020            break;
72021          }
72022        }
72023      }
72024    }
72025
72026#ifndef SQLITE_OMIT_TRIGGER
72027    /* If we have not already resolved the name, then maybe
72028    ** it is a new.* or old.* trigger argument reference
72029    */
72030    if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
72031      int op = pParse->eTriggerOp;
72032      Table *pTab = 0;
72033      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
72034      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
72035        pExpr->iTable = 1;
72036        pTab = pParse->pTriggerTab;
72037      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
72038        pExpr->iTable = 0;
72039        pTab = pParse->pTriggerTab;
72040      }
72041
72042      if( pTab ){
72043        int iCol;
72044        pSchema = pTab->pSchema;
72045        cntTab++;
72046        for(iCol=0; iCol<pTab->nCol; iCol++){
72047          Column *pCol = &pTab->aCol[iCol];
72048          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
72049            if( iCol==pTab->iPKey ){
72050              iCol = -1;
72051            }
72052            break;
72053          }
72054        }
72055        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
72056          iCol = -1;        /* IMP: R-44911-55124 */
72057        }
72058        if( iCol<pTab->nCol ){
72059          cnt++;
72060          if( iCol<0 ){
72061            pExpr->affinity = SQLITE_AFF_INTEGER;
72062          }else if( pExpr->iTable==0 ){
72063            testcase( iCol==31 );
72064            testcase( iCol==32 );
72065            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
72066          }else{
72067            testcase( iCol==31 );
72068            testcase( iCol==32 );
72069            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
72070          }
72071          pExpr->iColumn = (i16)iCol;
72072          pExpr->pTab = pTab;
72073          isTrigger = 1;
72074        }
72075      }
72076    }
72077#endif /* !defined(SQLITE_OMIT_TRIGGER) */
72078
72079    /*
72080    ** Perhaps the name is a reference to the ROWID
72081    */
72082    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
72083      cnt = 1;
72084      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
72085      pExpr->affinity = SQLITE_AFF_INTEGER;
72086    }
72087
72088    /*
72089    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
72090    ** might refer to an result-set alias.  This happens, for example, when
72091    ** we are resolving names in the WHERE clause of the following command:
72092    **
72093    **     SELECT a+b AS x FROM table WHERE x<10;
72094    **
72095    ** In cases like this, replace pExpr with a copy of the expression that
72096    ** forms the result set entry ("a+b" in the example) and return immediately.
72097    ** Note that the expression in the result set should have already been
72098    ** resolved by the time the WHERE clause is resolved.
72099    */
72100    if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
72101      for(j=0; j<pEList->nExpr; j++){
72102        char *zAs = pEList->a[j].zName;
72103        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
72104          Expr *pOrig;
72105          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
72106          assert( pExpr->x.pList==0 );
72107          assert( pExpr->x.pSelect==0 );
72108          pOrig = pEList->a[j].pExpr;
72109          if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
72110            sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
72111            return WRC_Abort;
72112          }
72113          resolveAlias(pParse, pEList, j, pExpr, "");
72114          cnt = 1;
72115          pMatch = 0;
72116          assert( zTab==0 && zDb==0 );
72117          goto lookupname_end;
72118        }
72119      }
72120    }
72121
72122    /* Advance to the next name context.  The loop will exit when either
72123    ** we have a match (cnt>0) or when we run out of name contexts.
72124    */
72125    if( cnt==0 ){
72126      pNC = pNC->pNext;
72127    }
72128  }
72129
72130  /*
72131  ** If X and Y are NULL (in other words if only the column name Z is
72132  ** supplied) and the value of Z is enclosed in double-quotes, then
72133  ** Z is a string literal if it doesn't match any column names.  In that
72134  ** case, we need to return right away and not make any changes to
72135  ** pExpr.
72136  **
72137  ** Because no reference was made to outer contexts, the pNC->nRef
72138  ** fields are not changed in any context.
72139  */
72140  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
72141    pExpr->op = TK_STRING;
72142    pExpr->pTab = 0;
72143    return WRC_Prune;
72144  }
72145
72146  /*
72147  ** cnt==0 means there was not match.  cnt>1 means there were two or
72148  ** more matches.  Either way, we have an error.
72149  */
72150  if( cnt!=1 ){
72151    const char *zErr;
72152    zErr = cnt==0 ? "no such column" : "ambiguous column name";
72153    if( zDb ){
72154      sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
72155    }else if( zTab ){
72156      sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
72157    }else{
72158      sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
72159    }
72160    pParse->checkSchema = 1;
72161    pTopNC->nErr++;
72162  }
72163
72164  /* If a column from a table in pSrcList is referenced, then record
72165  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
72166  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
72167  ** column number is greater than the number of bits in the bitmask
72168  ** then set the high-order bit of the bitmask.
72169  */
72170  if( pExpr->iColumn>=0 && pMatch!=0 ){
72171    int n = pExpr->iColumn;
72172    testcase( n==BMS-1 );
72173    if( n>=BMS ){
72174      n = BMS-1;
72175    }
72176    assert( pMatch->iCursor==pExpr->iTable );
72177    pMatch->colUsed |= ((Bitmask)1)<<n;
72178  }
72179
72180  /* Clean up and return
72181  */
72182  sqlite3ExprDelete(db, pExpr->pLeft);
72183  pExpr->pLeft = 0;
72184  sqlite3ExprDelete(db, pExpr->pRight);
72185  pExpr->pRight = 0;
72186  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
72187lookupname_end:
72188  if( cnt==1 ){
72189    assert( pNC!=0 );
72190    sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
72191    /* Increment the nRef value on all name contexts from TopNC up to
72192    ** the point where the name matched. */
72193    for(;;){
72194      assert( pTopNC!=0 );
72195      pTopNC->nRef++;
72196      if( pTopNC==pNC ) break;
72197      pTopNC = pTopNC->pNext;
72198    }
72199    return WRC_Prune;
72200  } else {
72201    return WRC_Abort;
72202  }
72203}
72204
72205/*
72206** Allocate and return a pointer to an expression to load the column iCol
72207** from datasource iSrc in SrcList pSrc.
72208*/
72209SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
72210  Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
72211  if( p ){
72212    struct SrcList_item *pItem = &pSrc->a[iSrc];
72213    p->pTab = pItem->pTab;
72214    p->iTable = pItem->iCursor;
72215    if( p->pTab->iPKey==iCol ){
72216      p->iColumn = -1;
72217    }else{
72218      p->iColumn = (ynVar)iCol;
72219      testcase( iCol==BMS );
72220      testcase( iCol==BMS-1 );
72221      pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
72222    }
72223    ExprSetProperty(p, EP_Resolved);
72224  }
72225  return p;
72226}
72227
72228/*
72229** This routine is callback for sqlite3WalkExpr().
72230**
72231** Resolve symbolic names into TK_COLUMN operators for the current
72232** node in the expression tree.  Return 0 to continue the search down
72233** the tree or 2 to abort the tree walk.
72234**
72235** This routine also does error checking and name resolution for
72236** function names.  The operator for aggregate functions is changed
72237** to TK_AGG_FUNCTION.
72238*/
72239static int resolveExprStep(Walker *pWalker, Expr *pExpr){
72240  NameContext *pNC;
72241  Parse *pParse;
72242
72243  pNC = pWalker->u.pNC;
72244  assert( pNC!=0 );
72245  pParse = pNC->pParse;
72246  assert( pParse==pWalker->pParse );
72247
72248  if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
72249  ExprSetProperty(pExpr, EP_Resolved);
72250#ifndef NDEBUG
72251  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
72252    SrcList *pSrcList = pNC->pSrcList;
72253    int i;
72254    for(i=0; i<pNC->pSrcList->nSrc; i++){
72255      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
72256    }
72257  }
72258#endif
72259  switch( pExpr->op ){
72260
72261#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
72262    /* The special operator TK_ROW means use the rowid for the first
72263    ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
72264    ** clause processing on UPDATE and DELETE statements.
72265    */
72266    case TK_ROW: {
72267      SrcList *pSrcList = pNC->pSrcList;
72268      struct SrcList_item *pItem;
72269      assert( pSrcList && pSrcList->nSrc==1 );
72270      pItem = pSrcList->a;
72271      pExpr->op = TK_COLUMN;
72272      pExpr->pTab = pItem->pTab;
72273      pExpr->iTable = pItem->iCursor;
72274      pExpr->iColumn = -1;
72275      pExpr->affinity = SQLITE_AFF_INTEGER;
72276      break;
72277    }
72278#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
72279
72280    /* A lone identifier is the name of a column.
72281    */
72282    case TK_ID: {
72283      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
72284    }
72285
72286    /* A table name and column name:     ID.ID
72287    ** Or a database, table and column:  ID.ID.ID
72288    */
72289    case TK_DOT: {
72290      const char *zColumn;
72291      const char *zTable;
72292      const char *zDb;
72293      Expr *pRight;
72294
72295      /* if( pSrcList==0 ) break; */
72296      pRight = pExpr->pRight;
72297      if( pRight->op==TK_ID ){
72298        zDb = 0;
72299        zTable = pExpr->pLeft->u.zToken;
72300        zColumn = pRight->u.zToken;
72301      }else{
72302        assert( pRight->op==TK_DOT );
72303        zDb = pExpr->pLeft->u.zToken;
72304        zTable = pRight->pLeft->u.zToken;
72305        zColumn = pRight->pRight->u.zToken;
72306      }
72307      return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
72308    }
72309
72310    /* Resolve function names
72311    */
72312    case TK_CONST_FUNC:
72313    case TK_FUNCTION: {
72314      ExprList *pList = pExpr->x.pList;    /* The argument list */
72315      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
72316      int no_such_func = 0;       /* True if no such function exists */
72317      int wrong_num_args = 0;     /* True if wrong number of arguments */
72318      int is_agg = 0;             /* True if is an aggregate function */
72319      int auth;                   /* Authorization to use the function */
72320      int nId;                    /* Number of characters in function name */
72321      const char *zId;            /* The function name. */
72322      FuncDef *pDef;              /* Information about the function */
72323      u8 enc = ENC(pParse->db);   /* The database encoding */
72324
72325      testcase( pExpr->op==TK_CONST_FUNC );
72326      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
72327      zId = pExpr->u.zToken;
72328      nId = sqlite3Strlen30(zId);
72329      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
72330      if( pDef==0 ){
72331        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
72332        if( pDef==0 ){
72333          no_such_func = 1;
72334        }else{
72335          wrong_num_args = 1;
72336        }
72337      }else{
72338        is_agg = pDef->xFunc==0;
72339      }
72340#ifndef SQLITE_OMIT_AUTHORIZATION
72341      if( pDef ){
72342        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
72343        if( auth!=SQLITE_OK ){
72344          if( auth==SQLITE_DENY ){
72345            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
72346                                    pDef->zName);
72347            pNC->nErr++;
72348          }
72349          pExpr->op = TK_NULL;
72350          return WRC_Prune;
72351        }
72352      }
72353#endif
72354      if( is_agg && !pNC->allowAgg ){
72355        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
72356        pNC->nErr++;
72357        is_agg = 0;
72358      }else if( no_such_func ){
72359        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
72360        pNC->nErr++;
72361      }else if( wrong_num_args ){
72362        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
72363             nId, zId);
72364        pNC->nErr++;
72365      }
72366      if( is_agg ){
72367        pExpr->op = TK_AGG_FUNCTION;
72368        pNC->hasAgg = 1;
72369      }
72370      if( is_agg ) pNC->allowAgg = 0;
72371      sqlite3WalkExprList(pWalker, pList);
72372      if( is_agg ) pNC->allowAgg = 1;
72373      /* FIX ME:  Compute pExpr->affinity based on the expected return
72374      ** type of the function
72375      */
72376      return WRC_Prune;
72377    }
72378#ifndef SQLITE_OMIT_SUBQUERY
72379    case TK_SELECT:
72380    case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
72381#endif
72382    case TK_IN: {
72383      testcase( pExpr->op==TK_IN );
72384      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
72385        int nRef = pNC->nRef;
72386#ifndef SQLITE_OMIT_CHECK
72387        if( pNC->isCheck ){
72388          sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
72389        }
72390#endif
72391        sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
72392        assert( pNC->nRef>=nRef );
72393        if( nRef!=pNC->nRef ){
72394          ExprSetProperty(pExpr, EP_VarSelect);
72395        }
72396      }
72397      break;
72398    }
72399#ifndef SQLITE_OMIT_CHECK
72400    case TK_VARIABLE: {
72401      if( pNC->isCheck ){
72402        sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
72403      }
72404      break;
72405    }
72406#endif
72407  }
72408  return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
72409}
72410
72411/*
72412** pEList is a list of expressions which are really the result set of the
72413** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
72414** This routine checks to see if pE is a simple identifier which corresponds
72415** to the AS-name of one of the terms of the expression list.  If it is,
72416** this routine return an integer between 1 and N where N is the number of
72417** elements in pEList, corresponding to the matching entry.  If there is
72418** no match, or if pE is not a simple identifier, then this routine
72419** return 0.
72420**
72421** pEList has been resolved.  pE has not.
72422*/
72423static int resolveAsName(
72424  Parse *pParse,     /* Parsing context for error messages */
72425  ExprList *pEList,  /* List of expressions to scan */
72426  Expr *pE           /* Expression we are trying to match */
72427){
72428  int i;             /* Loop counter */
72429
72430  UNUSED_PARAMETER(pParse);
72431
72432  if( pE->op==TK_ID ){
72433    char *zCol = pE->u.zToken;
72434    for(i=0; i<pEList->nExpr; i++){
72435      char *zAs = pEList->a[i].zName;
72436      if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
72437        return i+1;
72438      }
72439    }
72440  }
72441  return 0;
72442}
72443
72444/*
72445** pE is a pointer to an expression which is a single term in the
72446** ORDER BY of a compound SELECT.  The expression has not been
72447** name resolved.
72448**
72449** At the point this routine is called, we already know that the
72450** ORDER BY term is not an integer index into the result set.  That
72451** case is handled by the calling routine.
72452**
72453** Attempt to match pE against result set columns in the left-most
72454** SELECT statement.  Return the index i of the matching column,
72455** as an indication to the caller that it should sort by the i-th column.
72456** The left-most column is 1.  In other words, the value returned is the
72457** same integer value that would be used in the SQL statement to indicate
72458** the column.
72459**
72460** If there is no match, return 0.  Return -1 if an error occurs.
72461*/
72462static int resolveOrderByTermToExprList(
72463  Parse *pParse,     /* Parsing context for error messages */
72464  Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
72465  Expr *pE           /* The specific ORDER BY term */
72466){
72467  int i;             /* Loop counter */
72468  ExprList *pEList;  /* The columns of the result set */
72469  NameContext nc;    /* Name context for resolving pE */
72470  sqlite3 *db;       /* Database connection */
72471  int rc;            /* Return code from subprocedures */
72472  u8 savedSuppErr;   /* Saved value of db->suppressErr */
72473
72474  assert( sqlite3ExprIsInteger(pE, &i)==0 );
72475  pEList = pSelect->pEList;
72476
72477  /* Resolve all names in the ORDER BY term expression
72478  */
72479  memset(&nc, 0, sizeof(nc));
72480  nc.pParse = pParse;
72481  nc.pSrcList = pSelect->pSrc;
72482  nc.pEList = pEList;
72483  nc.allowAgg = 1;
72484  nc.nErr = 0;
72485  db = pParse->db;
72486  savedSuppErr = db->suppressErr;
72487  db->suppressErr = 1;
72488  rc = sqlite3ResolveExprNames(&nc, pE);
72489  db->suppressErr = savedSuppErr;
72490  if( rc ) return 0;
72491
72492  /* Try to match the ORDER BY expression against an expression
72493  ** in the result set.  Return an 1-based index of the matching
72494  ** result-set entry.
72495  */
72496  for(i=0; i<pEList->nExpr; i++){
72497    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
72498      return i+1;
72499    }
72500  }
72501
72502  /* If no match, return 0. */
72503  return 0;
72504}
72505
72506/*
72507** Generate an ORDER BY or GROUP BY term out-of-range error.
72508*/
72509static void resolveOutOfRangeError(
72510  Parse *pParse,         /* The error context into which to write the error */
72511  const char *zType,     /* "ORDER" or "GROUP" */
72512  int i,                 /* The index (1-based) of the term out of range */
72513  int mx                 /* Largest permissible value of i */
72514){
72515  sqlite3ErrorMsg(pParse,
72516    "%r %s BY term out of range - should be "
72517    "between 1 and %d", i, zType, mx);
72518}
72519
72520/*
72521** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
72522** each term of the ORDER BY clause is a constant integer between 1
72523** and N where N is the number of columns in the compound SELECT.
72524**
72525** ORDER BY terms that are already an integer between 1 and N are
72526** unmodified.  ORDER BY terms that are integers outside the range of
72527** 1 through N generate an error.  ORDER BY terms that are expressions
72528** are matched against result set expressions of compound SELECT
72529** beginning with the left-most SELECT and working toward the right.
72530** At the first match, the ORDER BY expression is transformed into
72531** the integer column number.
72532**
72533** Return the number of errors seen.
72534*/
72535static int resolveCompoundOrderBy(
72536  Parse *pParse,        /* Parsing context.  Leave error messages here */
72537  Select *pSelect       /* The SELECT statement containing the ORDER BY */
72538){
72539  int i;
72540  ExprList *pOrderBy;
72541  ExprList *pEList;
72542  sqlite3 *db;
72543  int moreToDo = 1;
72544
72545  pOrderBy = pSelect->pOrderBy;
72546  if( pOrderBy==0 ) return 0;
72547  db = pParse->db;
72548#if SQLITE_MAX_COLUMN
72549  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
72550    sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
72551    return 1;
72552  }
72553#endif
72554  for(i=0; i<pOrderBy->nExpr; i++){
72555    pOrderBy->a[i].done = 0;
72556  }
72557  pSelect->pNext = 0;
72558  while( pSelect->pPrior ){
72559    pSelect->pPrior->pNext = pSelect;
72560    pSelect = pSelect->pPrior;
72561  }
72562  while( pSelect && moreToDo ){
72563    struct ExprList_item *pItem;
72564    moreToDo = 0;
72565    pEList = pSelect->pEList;
72566    assert( pEList!=0 );
72567    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
72568      int iCol = -1;
72569      Expr *pE, *pDup;
72570      if( pItem->done ) continue;
72571      pE = pItem->pExpr;
72572      if( sqlite3ExprIsInteger(pE, &iCol) ){
72573        if( iCol<=0 || iCol>pEList->nExpr ){
72574          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
72575          return 1;
72576        }
72577      }else{
72578        iCol = resolveAsName(pParse, pEList, pE);
72579        if( iCol==0 ){
72580          pDup = sqlite3ExprDup(db, pE, 0);
72581          if( !db->mallocFailed ){
72582            assert(pDup);
72583            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
72584          }
72585          sqlite3ExprDelete(db, pDup);
72586        }
72587      }
72588      if( iCol>0 ){
72589        CollSeq *pColl = pE->pColl;
72590        int flags = pE->flags & EP_ExpCollate;
72591        sqlite3ExprDelete(db, pE);
72592        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
72593        if( pE==0 ) return 1;
72594        pE->pColl = pColl;
72595        pE->flags |= EP_IntValue | flags;
72596        pE->u.iValue = iCol;
72597        pItem->iCol = (u16)iCol;
72598        pItem->done = 1;
72599      }else{
72600        moreToDo = 1;
72601      }
72602    }
72603    pSelect = pSelect->pNext;
72604  }
72605  for(i=0; i<pOrderBy->nExpr; i++){
72606    if( pOrderBy->a[i].done==0 ){
72607      sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
72608            "column in the result set", i+1);
72609      return 1;
72610    }
72611  }
72612  return 0;
72613}
72614
72615/*
72616** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
72617** the SELECT statement pSelect.  If any term is reference to a
72618** result set expression (as determined by the ExprList.a.iCol field)
72619** then convert that term into a copy of the corresponding result set
72620** column.
72621**
72622** If any errors are detected, add an error message to pParse and
72623** return non-zero.  Return zero if no errors are seen.
72624*/
72625SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
72626  Parse *pParse,        /* Parsing context.  Leave error messages here */
72627  Select *pSelect,      /* The SELECT statement containing the clause */
72628  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
72629  const char *zType     /* "ORDER" or "GROUP" */
72630){
72631  int i;
72632  sqlite3 *db = pParse->db;
72633  ExprList *pEList;
72634  struct ExprList_item *pItem;
72635
72636  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
72637#if SQLITE_MAX_COLUMN
72638  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
72639    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
72640    return 1;
72641  }
72642#endif
72643  pEList = pSelect->pEList;
72644  assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
72645  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
72646    if( pItem->iCol ){
72647      if( pItem->iCol>pEList->nExpr ){
72648        resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
72649        return 1;
72650      }
72651      resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
72652    }
72653  }
72654  return 0;
72655}
72656
72657/*
72658** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
72659** The Name context of the SELECT statement is pNC.  zType is either
72660** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
72661**
72662** This routine resolves each term of the clause into an expression.
72663** If the order-by term is an integer I between 1 and N (where N is the
72664** number of columns in the result set of the SELECT) then the expression
72665** in the resolution is a copy of the I-th result-set expression.  If
72666** the order-by term is an identify that corresponds to the AS-name of
72667** a result-set expression, then the term resolves to a copy of the
72668** result-set expression.  Otherwise, the expression is resolved in
72669** the usual way - using sqlite3ResolveExprNames().
72670**
72671** This routine returns the number of errors.  If errors occur, then
72672** an appropriate error message might be left in pParse.  (OOM errors
72673** excepted.)
72674*/
72675static int resolveOrderGroupBy(
72676  NameContext *pNC,     /* The name context of the SELECT statement */
72677  Select *pSelect,      /* The SELECT statement holding pOrderBy */
72678  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
72679  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
72680){
72681  int i;                         /* Loop counter */
72682  int iCol;                      /* Column number */
72683  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
72684  Parse *pParse;                 /* Parsing context */
72685  int nResult;                   /* Number of terms in the result set */
72686
72687  if( pOrderBy==0 ) return 0;
72688  nResult = pSelect->pEList->nExpr;
72689  pParse = pNC->pParse;
72690  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
72691    Expr *pE = pItem->pExpr;
72692    iCol = resolveAsName(pParse, pSelect->pEList, pE);
72693    if( iCol>0 ){
72694      /* If an AS-name match is found, mark this ORDER BY column as being
72695      ** a copy of the iCol-th result-set column.  The subsequent call to
72696      ** sqlite3ResolveOrderGroupBy() will convert the expression to a
72697      ** copy of the iCol-th result-set expression. */
72698      pItem->iCol = (u16)iCol;
72699      continue;
72700    }
72701    if( sqlite3ExprIsInteger(pE, &iCol) ){
72702      /* The ORDER BY term is an integer constant.  Again, set the column
72703      ** number so that sqlite3ResolveOrderGroupBy() will convert the
72704      ** order-by term to a copy of the result-set expression */
72705      if( iCol<1 ){
72706        resolveOutOfRangeError(pParse, zType, i+1, nResult);
72707        return 1;
72708      }
72709      pItem->iCol = (u16)iCol;
72710      continue;
72711    }
72712
72713    /* Otherwise, treat the ORDER BY term as an ordinary expression */
72714    pItem->iCol = 0;
72715    if( sqlite3ResolveExprNames(pNC, pE) ){
72716      return 1;
72717    }
72718  }
72719  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
72720}
72721
72722/*
72723** Resolve names in the SELECT statement p and all of its descendents.
72724*/
72725static int resolveSelectStep(Walker *pWalker, Select *p){
72726  NameContext *pOuterNC;  /* Context that contains this SELECT */
72727  NameContext sNC;        /* Name context of this SELECT */
72728  int isCompound;         /* True if p is a compound select */
72729  int nCompound;          /* Number of compound terms processed so far */
72730  Parse *pParse;          /* Parsing context */
72731  ExprList *pEList;       /* Result set expression list */
72732  int i;                  /* Loop counter */
72733  ExprList *pGroupBy;     /* The GROUP BY clause */
72734  Select *pLeftmost;      /* Left-most of SELECT of a compound */
72735  sqlite3 *db;            /* Database connection */
72736
72737
72738  assert( p!=0 );
72739  if( p->selFlags & SF_Resolved ){
72740    return WRC_Prune;
72741  }
72742  pOuterNC = pWalker->u.pNC;
72743  pParse = pWalker->pParse;
72744  db = pParse->db;
72745
72746  /* Normally sqlite3SelectExpand() will be called first and will have
72747  ** already expanded this SELECT.  However, if this is a subquery within
72748  ** an expression, sqlite3ResolveExprNames() will be called without a
72749  ** prior call to sqlite3SelectExpand().  When that happens, let
72750  ** sqlite3SelectPrep() do all of the processing for this SELECT.
72751  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
72752  ** this routine in the correct order.
72753  */
72754  if( (p->selFlags & SF_Expanded)==0 ){
72755    sqlite3SelectPrep(pParse, p, pOuterNC);
72756    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
72757  }
72758
72759  isCompound = p->pPrior!=0;
72760  nCompound = 0;
72761  pLeftmost = p;
72762  while( p ){
72763    assert( (p->selFlags & SF_Expanded)!=0 );
72764    assert( (p->selFlags & SF_Resolved)==0 );
72765    p->selFlags |= SF_Resolved;
72766
72767    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
72768    ** are not allowed to refer to any names, so pass an empty NameContext.
72769    */
72770    memset(&sNC, 0, sizeof(sNC));
72771    sNC.pParse = pParse;
72772    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
72773        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
72774      return WRC_Abort;
72775    }
72776
72777    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
72778    ** resolve the result-set expression list.
72779    */
72780    sNC.allowAgg = 1;
72781    sNC.pSrcList = p->pSrc;
72782    sNC.pNext = pOuterNC;
72783
72784    /* Resolve names in the result set. */
72785    pEList = p->pEList;
72786    assert( pEList!=0 );
72787    for(i=0; i<pEList->nExpr; i++){
72788      Expr *pX = pEList->a[i].pExpr;
72789      if( sqlite3ResolveExprNames(&sNC, pX) ){
72790        return WRC_Abort;
72791      }
72792    }
72793
72794    /* Recursively resolve names in all subqueries
72795    */
72796    for(i=0; i<p->pSrc->nSrc; i++){
72797      struct SrcList_item *pItem = &p->pSrc->a[i];
72798      if( pItem->pSelect ){
72799        NameContext *pNC;         /* Used to iterate name contexts */
72800        int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
72801        const char *zSavedContext = pParse->zAuthContext;
72802
72803        /* Count the total number of references to pOuterNC and all of its
72804        ** parent contexts. After resolving references to expressions in
72805        ** pItem->pSelect, check if this value has changed. If so, then
72806        ** SELECT statement pItem->pSelect must be correlated. Set the
72807        ** pItem->isCorrelated flag if this is the case. */
72808        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
72809
72810        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
72811        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
72812        pParse->zAuthContext = zSavedContext;
72813        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
72814
72815        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
72816        assert( pItem->isCorrelated==0 && nRef<=0 );
72817        pItem->isCorrelated = (nRef!=0);
72818      }
72819    }
72820
72821    /* If there are no aggregate functions in the result-set, and no GROUP BY
72822    ** expression, do not allow aggregates in any of the other expressions.
72823    */
72824    assert( (p->selFlags & SF_Aggregate)==0 );
72825    pGroupBy = p->pGroupBy;
72826    if( pGroupBy || sNC.hasAgg ){
72827      p->selFlags |= SF_Aggregate;
72828    }else{
72829      sNC.allowAgg = 0;
72830    }
72831
72832    /* If a HAVING clause is present, then there must be a GROUP BY clause.
72833    */
72834    if( p->pHaving && !pGroupBy ){
72835      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
72836      return WRC_Abort;
72837    }
72838
72839    /* Add the expression list to the name-context before parsing the
72840    ** other expressions in the SELECT statement. This is so that
72841    ** expressions in the WHERE clause (etc.) can refer to expressions by
72842    ** aliases in the result set.
72843    **
72844    ** Minor point: If this is the case, then the expression will be
72845    ** re-evaluated for each reference to it.
72846    */
72847    sNC.pEList = p->pEList;
72848    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
72849       sqlite3ResolveExprNames(&sNC, p->pHaving)
72850    ){
72851      return WRC_Abort;
72852    }
72853
72854    /* The ORDER BY and GROUP BY clauses may not refer to terms in
72855    ** outer queries
72856    */
72857    sNC.pNext = 0;
72858    sNC.allowAgg = 1;
72859
72860    /* Process the ORDER BY clause for singleton SELECT statements.
72861    ** The ORDER BY clause for compounds SELECT statements is handled
72862    ** below, after all of the result-sets for all of the elements of
72863    ** the compound have been resolved.
72864    */
72865    if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
72866      return WRC_Abort;
72867    }
72868    if( db->mallocFailed ){
72869      return WRC_Abort;
72870    }
72871
72872    /* Resolve the GROUP BY clause.  At the same time, make sure
72873    ** the GROUP BY clause does not contain aggregate functions.
72874    */
72875    if( pGroupBy ){
72876      struct ExprList_item *pItem;
72877
72878      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
72879        return WRC_Abort;
72880      }
72881      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
72882        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
72883          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
72884              "the GROUP BY clause");
72885          return WRC_Abort;
72886        }
72887      }
72888    }
72889
72890    /* Advance to the next term of the compound
72891    */
72892    p = p->pPrior;
72893    nCompound++;
72894  }
72895
72896  /* Resolve the ORDER BY on a compound SELECT after all terms of
72897  ** the compound have been resolved.
72898  */
72899  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
72900    return WRC_Abort;
72901  }
72902
72903  return WRC_Prune;
72904}
72905
72906/*
72907** This routine walks an expression tree and resolves references to
72908** table columns and result-set columns.  At the same time, do error
72909** checking on function usage and set a flag if any aggregate functions
72910** are seen.
72911**
72912** To resolve table columns references we look for nodes (or subtrees) of the
72913** form X.Y.Z or Y.Z or just Z where
72914**
72915**      X:   The name of a database.  Ex:  "main" or "temp" or
72916**           the symbolic name assigned to an ATTACH-ed database.
72917**
72918**      Y:   The name of a table in a FROM clause.  Or in a trigger
72919**           one of the special names "old" or "new".
72920**
72921**      Z:   The name of a column in table Y.
72922**
72923** The node at the root of the subtree is modified as follows:
72924**
72925**    Expr.op        Changed to TK_COLUMN
72926**    Expr.pTab      Points to the Table object for X.Y
72927**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
72928**    Expr.iTable    The VDBE cursor number for X.Y
72929**
72930**
72931** To resolve result-set references, look for expression nodes of the
72932** form Z (with no X and Y prefix) where the Z matches the right-hand
72933** size of an AS clause in the result-set of a SELECT.  The Z expression
72934** is replaced by a copy of the left-hand side of the result-set expression.
72935** Table-name and function resolution occurs on the substituted expression
72936** tree.  For example, in:
72937**
72938**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
72939**
72940** The "x" term of the order by is replaced by "a+b" to render:
72941**
72942**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
72943**
72944** Function calls are checked to make sure that the function is
72945** defined and that the correct number of arguments are specified.
72946** If the function is an aggregate function, then the pNC->hasAgg is
72947** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
72948** If an expression contains aggregate functions then the EP_Agg
72949** property on the expression is set.
72950**
72951** An error message is left in pParse if anything is amiss.  The number
72952** if errors is returned.
72953*/
72954SQLITE_PRIVATE int sqlite3ResolveExprNames(
72955  NameContext *pNC,       /* Namespace to resolve expressions in. */
72956  Expr *pExpr             /* The expression to be analyzed. */
72957){
72958  int savedHasAgg;
72959  Walker w;
72960
72961  if( pExpr==0 ) return 0;
72962#if SQLITE_MAX_EXPR_DEPTH>0
72963  {
72964    Parse *pParse = pNC->pParse;
72965    if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
72966      return 1;
72967    }
72968    pParse->nHeight += pExpr->nHeight;
72969  }
72970#endif
72971  savedHasAgg = pNC->hasAgg;
72972  pNC->hasAgg = 0;
72973  w.xExprCallback = resolveExprStep;
72974  w.xSelectCallback = resolveSelectStep;
72975  w.pParse = pNC->pParse;
72976  w.u.pNC = pNC;
72977  sqlite3WalkExpr(&w, pExpr);
72978#if SQLITE_MAX_EXPR_DEPTH>0
72979  pNC->pParse->nHeight -= pExpr->nHeight;
72980#endif
72981  if( pNC->nErr>0 || w.pParse->nErr>0 ){
72982    ExprSetProperty(pExpr, EP_Error);
72983  }
72984  if( pNC->hasAgg ){
72985    ExprSetProperty(pExpr, EP_Agg);
72986  }else if( savedHasAgg ){
72987    pNC->hasAgg = 1;
72988  }
72989  return ExprHasProperty(pExpr, EP_Error);
72990}
72991
72992
72993/*
72994** Resolve all names in all expressions of a SELECT and in all
72995** decendents of the SELECT, including compounds off of p->pPrior,
72996** subqueries in expressions, and subqueries used as FROM clause
72997** terms.
72998**
72999** See sqlite3ResolveExprNames() for a description of the kinds of
73000** transformations that occur.
73001**
73002** All SELECT statements should have been expanded using
73003** sqlite3SelectExpand() prior to invoking this routine.
73004*/
73005SQLITE_PRIVATE void sqlite3ResolveSelectNames(
73006  Parse *pParse,         /* The parser context */
73007  Select *p,             /* The SELECT statement being coded. */
73008  NameContext *pOuterNC  /* Name context for parent SELECT statement */
73009){
73010  Walker w;
73011
73012  assert( p!=0 );
73013  w.xExprCallback = resolveExprStep;
73014  w.xSelectCallback = resolveSelectStep;
73015  w.pParse = pParse;
73016  w.u.pNC = pOuterNC;
73017  sqlite3WalkSelect(&w, p);
73018}
73019
73020/************** End of resolve.c *********************************************/
73021/************** Begin file expr.c ********************************************/
73022/*
73023** 2001 September 15
73024**
73025** The author disclaims copyright to this source code.  In place of
73026** a legal notice, here is a blessing:
73027**
73028**    May you do good and not evil.
73029**    May you find forgiveness for yourself and forgive others.
73030**    May you share freely, never taking more than you give.
73031**
73032*************************************************************************
73033** This file contains routines used for analyzing expressions and
73034** for generating VDBE code that evaluates expressions in SQLite.
73035*/
73036
73037/*
73038** Return the 'affinity' of the expression pExpr if any.
73039**
73040** If pExpr is a column, a reference to a column via an 'AS' alias,
73041** or a sub-select with a column as the return value, then the
73042** affinity of that column is returned. Otherwise, 0x00 is returned,
73043** indicating no affinity for the expression.
73044**
73045** i.e. the WHERE clause expresssions in the following statements all
73046** have an affinity:
73047**
73048** CREATE TABLE t1(a);
73049** SELECT * FROM t1 WHERE a;
73050** SELECT a AS b FROM t1 WHERE b;
73051** SELECT * FROM t1 WHERE (select a from t1);
73052*/
73053SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
73054  int op = pExpr->op;
73055  if( op==TK_SELECT ){
73056    assert( pExpr->flags&EP_xIsSelect );
73057    return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
73058  }
73059#ifndef SQLITE_OMIT_CAST
73060  if( op==TK_CAST ){
73061    assert( !ExprHasProperty(pExpr, EP_IntValue) );
73062    return sqlite3AffinityType(pExpr->u.zToken);
73063  }
73064#endif
73065  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
73066   && pExpr->pTab!=0
73067  ){
73068    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
73069    ** a TK_COLUMN but was previously evaluated and cached in a register */
73070    int j = pExpr->iColumn;
73071    if( j<0 ) return SQLITE_AFF_INTEGER;
73072    assert( pExpr->pTab && j<pExpr->pTab->nCol );
73073    return pExpr->pTab->aCol[j].affinity;
73074  }
73075  return pExpr->affinity;
73076}
73077
73078/*
73079** Set the explicit collating sequence for an expression to the
73080** collating sequence supplied in the second argument.
73081*/
73082SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
73083  if( pExpr && pColl ){
73084    pExpr->pColl = pColl;
73085    pExpr->flags |= EP_ExpCollate;
73086  }
73087  return pExpr;
73088}
73089
73090/*
73091** Set the collating sequence for expression pExpr to be the collating
73092** sequence named by pToken.   Return a pointer to the revised expression.
73093** The collating sequence is marked as "explicit" using the EP_ExpCollate
73094** flag.  An explicit collating sequence will override implicit
73095** collating sequences.
73096*/
73097SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
73098  char *zColl = 0;            /* Dequoted name of collation sequence */
73099  CollSeq *pColl;
73100  sqlite3 *db = pParse->db;
73101  zColl = sqlite3NameFromToken(db, pCollName);
73102  pColl = sqlite3LocateCollSeq(pParse, zColl);
73103  sqlite3ExprSetColl(pExpr, pColl);
73104  sqlite3DbFree(db, zColl);
73105  return pExpr;
73106}
73107
73108/*
73109** Return the default collation sequence for the expression pExpr. If
73110** there is no default collation type, return 0.
73111*/
73112SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
73113  CollSeq *pColl = 0;
73114  Expr *p = pExpr;
73115  while( p ){
73116    int op;
73117    pColl = p->pColl;
73118    if( pColl ) break;
73119    op = p->op;
73120    if( p->pTab!=0 && (
73121        op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
73122    )){
73123      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
73124      ** a TK_COLUMN but was previously evaluated and cached in a register */
73125      const char *zColl;
73126      int j = p->iColumn;
73127      if( j>=0 ){
73128        sqlite3 *db = pParse->db;
73129        zColl = p->pTab->aCol[j].zColl;
73130        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
73131        pExpr->pColl = pColl;
73132      }
73133      break;
73134    }
73135    if( op!=TK_CAST && op!=TK_UPLUS ){
73136      break;
73137    }
73138    p = p->pLeft;
73139  }
73140  if( sqlite3CheckCollSeq(pParse, pColl) ){
73141    pColl = 0;
73142  }
73143  return pColl;
73144}
73145
73146/*
73147** pExpr is an operand of a comparison operator.  aff2 is the
73148** type affinity of the other operand.  This routine returns the
73149** type affinity that should be used for the comparison operator.
73150*/
73151SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
73152  char aff1 = sqlite3ExprAffinity(pExpr);
73153  if( aff1 && aff2 ){
73154    /* Both sides of the comparison are columns. If one has numeric
73155    ** affinity, use that. Otherwise use no affinity.
73156    */
73157    if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
73158      return SQLITE_AFF_NUMERIC;
73159    }else{
73160      return SQLITE_AFF_NONE;
73161    }
73162  }else if( !aff1 && !aff2 ){
73163    /* Neither side of the comparison is a column.  Compare the
73164    ** results directly.
73165    */
73166    return SQLITE_AFF_NONE;
73167  }else{
73168    /* One side is a column, the other is not. Use the columns affinity. */
73169    assert( aff1==0 || aff2==0 );
73170    return (aff1 + aff2);
73171  }
73172}
73173
73174/*
73175** pExpr is a comparison operator.  Return the type affinity that should
73176** be applied to both operands prior to doing the comparison.
73177*/
73178static char comparisonAffinity(Expr *pExpr){
73179  char aff;
73180  assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
73181          pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
73182          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
73183  assert( pExpr->pLeft );
73184  aff = sqlite3ExprAffinity(pExpr->pLeft);
73185  if( pExpr->pRight ){
73186    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
73187  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
73188    aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
73189  }else if( !aff ){
73190    aff = SQLITE_AFF_NONE;
73191  }
73192  return aff;
73193}
73194
73195/*
73196** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
73197** idx_affinity is the affinity of an indexed column. Return true
73198** if the index with affinity idx_affinity may be used to implement
73199** the comparison in pExpr.
73200*/
73201SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
73202  char aff = comparisonAffinity(pExpr);
73203  switch( aff ){
73204    case SQLITE_AFF_NONE:
73205      return 1;
73206    case SQLITE_AFF_TEXT:
73207      return idx_affinity==SQLITE_AFF_TEXT;
73208    default:
73209      return sqlite3IsNumericAffinity(idx_affinity);
73210  }
73211}
73212
73213/*
73214** Return the P5 value that should be used for a binary comparison
73215** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
73216*/
73217static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
73218  u8 aff = (char)sqlite3ExprAffinity(pExpr2);
73219  aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
73220  return aff;
73221}
73222
73223/*
73224** Return a pointer to the collation sequence that should be used by
73225** a binary comparison operator comparing pLeft and pRight.
73226**
73227** If the left hand expression has a collating sequence type, then it is
73228** used. Otherwise the collation sequence for the right hand expression
73229** is used, or the default (BINARY) if neither expression has a collating
73230** type.
73231**
73232** Argument pRight (but not pLeft) may be a null pointer. In this case,
73233** it is not considered.
73234*/
73235SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
73236  Parse *pParse,
73237  Expr *pLeft,
73238  Expr *pRight
73239){
73240  CollSeq *pColl;
73241  assert( pLeft );
73242  if( pLeft->flags & EP_ExpCollate ){
73243    assert( pLeft->pColl );
73244    pColl = pLeft->pColl;
73245  }else if( pRight && pRight->flags & EP_ExpCollate ){
73246    assert( pRight->pColl );
73247    pColl = pRight->pColl;
73248  }else{
73249    pColl = sqlite3ExprCollSeq(pParse, pLeft);
73250    if( !pColl ){
73251      pColl = sqlite3ExprCollSeq(pParse, pRight);
73252    }
73253  }
73254  return pColl;
73255}
73256
73257/*
73258** Generate code for a comparison operator.
73259*/
73260static int codeCompare(
73261  Parse *pParse,    /* The parsing (and code generating) context */
73262  Expr *pLeft,      /* The left operand */
73263  Expr *pRight,     /* The right operand */
73264  int opcode,       /* The comparison opcode */
73265  int in1, int in2, /* Register holding operands */
73266  int dest,         /* Jump here if true.  */
73267  int jumpIfNull    /* If true, jump if either operand is NULL */
73268){
73269  int p5;
73270  int addr;
73271  CollSeq *p4;
73272
73273  p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
73274  p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
73275  addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
73276                           (void*)p4, P4_COLLSEQ);
73277  sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
73278  return addr;
73279}
73280
73281#if SQLITE_MAX_EXPR_DEPTH>0
73282/*
73283** Check that argument nHeight is less than or equal to the maximum
73284** expression depth allowed. If it is not, leave an error message in
73285** pParse.
73286*/
73287SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
73288  int rc = SQLITE_OK;
73289  int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
73290  if( nHeight>mxHeight ){
73291    sqlite3ErrorMsg(pParse,
73292       "Expression tree is too large (maximum depth %d)", mxHeight
73293    );
73294    rc = SQLITE_ERROR;
73295  }
73296  return rc;
73297}
73298
73299/* The following three functions, heightOfExpr(), heightOfExprList()
73300** and heightOfSelect(), are used to determine the maximum height
73301** of any expression tree referenced by the structure passed as the
73302** first argument.
73303**
73304** If this maximum height is greater than the current value pointed
73305** to by pnHeight, the second parameter, then set *pnHeight to that
73306** value.
73307*/
73308static void heightOfExpr(Expr *p, int *pnHeight){
73309  if( p ){
73310    if( p->nHeight>*pnHeight ){
73311      *pnHeight = p->nHeight;
73312    }
73313  }
73314}
73315static void heightOfExprList(ExprList *p, int *pnHeight){
73316  if( p ){
73317    int i;
73318    for(i=0; i<p->nExpr; i++){
73319      heightOfExpr(p->a[i].pExpr, pnHeight);
73320    }
73321  }
73322}
73323static void heightOfSelect(Select *p, int *pnHeight){
73324  if( p ){
73325    heightOfExpr(p->pWhere, pnHeight);
73326    heightOfExpr(p->pHaving, pnHeight);
73327    heightOfExpr(p->pLimit, pnHeight);
73328    heightOfExpr(p->pOffset, pnHeight);
73329    heightOfExprList(p->pEList, pnHeight);
73330    heightOfExprList(p->pGroupBy, pnHeight);
73331    heightOfExprList(p->pOrderBy, pnHeight);
73332    heightOfSelect(p->pPrior, pnHeight);
73333  }
73334}
73335
73336/*
73337** Set the Expr.nHeight variable in the structure passed as an
73338** argument. An expression with no children, Expr.pList or
73339** Expr.pSelect member has a height of 1. Any other expression
73340** has a height equal to the maximum height of any other
73341** referenced Expr plus one.
73342*/
73343static void exprSetHeight(Expr *p){
73344  int nHeight = 0;
73345  heightOfExpr(p->pLeft, &nHeight);
73346  heightOfExpr(p->pRight, &nHeight);
73347  if( ExprHasProperty(p, EP_xIsSelect) ){
73348    heightOfSelect(p->x.pSelect, &nHeight);
73349  }else{
73350    heightOfExprList(p->x.pList, &nHeight);
73351  }
73352  p->nHeight = nHeight + 1;
73353}
73354
73355/*
73356** Set the Expr.nHeight variable using the exprSetHeight() function. If
73357** the height is greater than the maximum allowed expression depth,
73358** leave an error in pParse.
73359*/
73360SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
73361  exprSetHeight(p);
73362  sqlite3ExprCheckHeight(pParse, p->nHeight);
73363}
73364
73365/*
73366** Return the maximum height of any expression tree referenced
73367** by the select statement passed as an argument.
73368*/
73369SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
73370  int nHeight = 0;
73371  heightOfSelect(p, &nHeight);
73372  return nHeight;
73373}
73374#else
73375  #define exprSetHeight(y)
73376#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
73377
73378/*
73379** This routine is the core allocator for Expr nodes.
73380**
73381** Construct a new expression node and return a pointer to it.  Memory
73382** for this node and for the pToken argument is a single allocation
73383** obtained from sqlite3DbMalloc().  The calling function
73384** is responsible for making sure the node eventually gets freed.
73385**
73386** If dequote is true, then the token (if it exists) is dequoted.
73387** If dequote is false, no dequoting is performance.  The deQuote
73388** parameter is ignored if pToken is NULL or if the token does not
73389** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
73390** then the EP_DblQuoted flag is set on the expression node.
73391**
73392** Special case:  If op==TK_INTEGER and pToken points to a string that
73393** can be translated into a 32-bit integer, then the token is not
73394** stored in u.zToken.  Instead, the integer values is written
73395** into u.iValue and the EP_IntValue flag is set.  No extra storage
73396** is allocated to hold the integer text and the dequote flag is ignored.
73397*/
73398SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
73399  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
73400  int op,                 /* Expression opcode */
73401  const Token *pToken,    /* Token argument.  Might be NULL */
73402  int dequote             /* True to dequote */
73403){
73404  Expr *pNew;
73405  int nExtra = 0;
73406  int iValue = 0;
73407
73408  if( pToken ){
73409    if( op!=TK_INTEGER || pToken->z==0
73410          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
73411      nExtra = pToken->n+1;
73412      assert( iValue>=0 );
73413    }
73414  }
73415  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
73416  if( pNew ){
73417    pNew->op = (u8)op;
73418    pNew->iAgg = -1;
73419    if( pToken ){
73420      if( nExtra==0 ){
73421        pNew->flags |= EP_IntValue;
73422        pNew->u.iValue = iValue;
73423      }else{
73424        int c;
73425        pNew->u.zToken = (char*)&pNew[1];
73426        memcpy(pNew->u.zToken, pToken->z, pToken->n);
73427        pNew->u.zToken[pToken->n] = 0;
73428        if( dequote && nExtra>=3
73429             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
73430          sqlite3Dequote(pNew->u.zToken);
73431          if( c=='"' ) pNew->flags |= EP_DblQuoted;
73432        }
73433      }
73434    }
73435#if SQLITE_MAX_EXPR_DEPTH>0
73436    pNew->nHeight = 1;
73437#endif
73438  }
73439  return pNew;
73440}
73441
73442/*
73443** Allocate a new expression node from a zero-terminated token that has
73444** already been dequoted.
73445*/
73446SQLITE_PRIVATE Expr *sqlite3Expr(
73447  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
73448  int op,                 /* Expression opcode */
73449  const char *zToken      /* Token argument.  Might be NULL */
73450){
73451  Token x;
73452  x.z = zToken;
73453  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
73454  return sqlite3ExprAlloc(db, op, &x, 0);
73455}
73456
73457/*
73458** Attach subtrees pLeft and pRight to the Expr node pRoot.
73459**
73460** If pRoot==NULL that means that a memory allocation error has occurred.
73461** In that case, delete the subtrees pLeft and pRight.
73462*/
73463SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
73464  sqlite3 *db,
73465  Expr *pRoot,
73466  Expr *pLeft,
73467  Expr *pRight
73468){
73469  if( pRoot==0 ){
73470    assert( db->mallocFailed );
73471    sqlite3ExprDelete(db, pLeft);
73472    sqlite3ExprDelete(db, pRight);
73473  }else{
73474    if( pRight ){
73475      pRoot->pRight = pRight;
73476      if( pRight->flags & EP_ExpCollate ){
73477        pRoot->flags |= EP_ExpCollate;
73478        pRoot->pColl = pRight->pColl;
73479      }
73480    }
73481    if( pLeft ){
73482      pRoot->pLeft = pLeft;
73483      if( pLeft->flags & EP_ExpCollate ){
73484        pRoot->flags |= EP_ExpCollate;
73485        pRoot->pColl = pLeft->pColl;
73486      }
73487    }
73488    exprSetHeight(pRoot);
73489  }
73490}
73491
73492/*
73493** Allocate a Expr node which joins as many as two subtrees.
73494**
73495** One or both of the subtrees can be NULL.  Return a pointer to the new
73496** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
73497** free the subtrees and return NULL.
73498*/
73499SQLITE_PRIVATE Expr *sqlite3PExpr(
73500  Parse *pParse,          /* Parsing context */
73501  int op,                 /* Expression opcode */
73502  Expr *pLeft,            /* Left operand */
73503  Expr *pRight,           /* Right operand */
73504  const Token *pToken     /* Argument token */
73505){
73506  Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
73507  sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
73508  if( p ) {
73509    sqlite3ExprCheckHeight(pParse, p->nHeight);
73510  }
73511  return p;
73512}
73513
73514/*
73515** Join two expressions using an AND operator.  If either expression is
73516** NULL, then just return the other expression.
73517*/
73518SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
73519  if( pLeft==0 ){
73520    return pRight;
73521  }else if( pRight==0 ){
73522    return pLeft;
73523  }else{
73524    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
73525    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
73526    return pNew;
73527  }
73528}
73529
73530/*
73531** Construct a new expression node for a function with multiple
73532** arguments.
73533*/
73534SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
73535  Expr *pNew;
73536  sqlite3 *db = pParse->db;
73537  assert( pToken );
73538  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
73539  if( pNew==0 ){
73540    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
73541    return 0;
73542  }
73543  pNew->x.pList = pList;
73544  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
73545  sqlite3ExprSetHeight(pParse, pNew);
73546  return pNew;
73547}
73548
73549/*
73550** Assign a variable number to an expression that encodes a wildcard
73551** in the original SQL statement.
73552**
73553** Wildcards consisting of a single "?" are assigned the next sequential
73554** variable number.
73555**
73556** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
73557** sure "nnn" is not too be to avoid a denial of service attack when
73558** the SQL statement comes from an external source.
73559**
73560** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
73561** as the previous instance of the same wildcard.  Or if this is the first
73562** instance of the wildcard, the next sequenial variable number is
73563** assigned.
73564*/
73565SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
73566  sqlite3 *db = pParse->db;
73567  const char *z;
73568
73569  if( pExpr==0 ) return;
73570  assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
73571  z = pExpr->u.zToken;
73572  assert( z!=0 );
73573  assert( z[0]!=0 );
73574  if( z[1]==0 ){
73575    /* Wildcard of the form "?".  Assign the next variable number */
73576    assert( z[0]=='?' );
73577    pExpr->iColumn = (ynVar)(++pParse->nVar);
73578  }else{
73579    ynVar x = 0;
73580    u32 n = sqlite3Strlen30(z);
73581    if( z[0]=='?' ){
73582      /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
73583      ** use it as the variable number */
73584      i64 i;
73585      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
73586      pExpr->iColumn = x = (ynVar)i;
73587      testcase( i==0 );
73588      testcase( i==1 );
73589      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
73590      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
73591      if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
73592        sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
73593            db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
73594        x = 0;
73595      }
73596      if( i>pParse->nVar ){
73597        pParse->nVar = (int)i;
73598      }
73599    }else{
73600      /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
73601      ** number as the prior appearance of the same name, or if the name
73602      ** has never appeared before, reuse the same variable number
73603      */
73604      ynVar i;
73605      for(i=0; i<pParse->nzVar; i++){
73606        if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
73607          pExpr->iColumn = x = (ynVar)i+1;
73608          break;
73609        }
73610      }
73611      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
73612    }
73613    if( x>0 ){
73614      if( x>pParse->nzVar ){
73615        char **a;
73616        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
73617        if( a==0 ) return;  /* Error reported through db->mallocFailed */
73618        pParse->azVar = a;
73619        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
73620        pParse->nzVar = x;
73621      }
73622      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
73623        sqlite3DbFree(db, pParse->azVar[x-1]);
73624        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
73625      }
73626    }
73627  }
73628  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
73629    sqlite3ErrorMsg(pParse, "too many SQL variables");
73630  }
73631}
73632
73633/*
73634** Recursively delete an expression tree.
73635*/
73636SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
73637  if( p==0 ) return;
73638  /* Sanity check: Assert that the IntValue is non-negative if it exists */
73639  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
73640  if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
73641    sqlite3ExprDelete(db, p->pLeft);
73642    sqlite3ExprDelete(db, p->pRight);
73643    if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
73644      sqlite3DbFree(db, p->u.zToken);
73645    }
73646    if( ExprHasProperty(p, EP_xIsSelect) ){
73647      sqlite3SelectDelete(db, p->x.pSelect);
73648    }else{
73649      sqlite3ExprListDelete(db, p->x.pList);
73650    }
73651  }
73652  if( !ExprHasProperty(p, EP_Static) ){
73653    sqlite3DbFree(db, p);
73654  }
73655}
73656
73657/*
73658** Return the number of bytes allocated for the expression structure
73659** passed as the first argument. This is always one of EXPR_FULLSIZE,
73660** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
73661*/
73662static int exprStructSize(Expr *p){
73663  if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
73664  if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
73665  return EXPR_FULLSIZE;
73666}
73667
73668/*
73669** The dupedExpr*Size() routines each return the number of bytes required
73670** to store a copy of an expression or expression tree.  They differ in
73671** how much of the tree is measured.
73672**
73673**     dupedExprStructSize()     Size of only the Expr structure
73674**     dupedExprNodeSize()       Size of Expr + space for token
73675**     dupedExprSize()           Expr + token + subtree components
73676**
73677***************************************************************************
73678**
73679** The dupedExprStructSize() function returns two values OR-ed together:
73680** (1) the space required for a copy of the Expr structure only and
73681** (2) the EP_xxx flags that indicate what the structure size should be.
73682** The return values is always one of:
73683**
73684**      EXPR_FULLSIZE
73685**      EXPR_REDUCEDSIZE   | EP_Reduced
73686**      EXPR_TOKENONLYSIZE | EP_TokenOnly
73687**
73688** The size of the structure can be found by masking the return value
73689** of this routine with 0xfff.  The flags can be found by masking the
73690** return value with EP_Reduced|EP_TokenOnly.
73691**
73692** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
73693** (unreduced) Expr objects as they or originally constructed by the parser.
73694** During expression analysis, extra information is computed and moved into
73695** later parts of teh Expr object and that extra information might get chopped
73696** off if the expression is reduced.  Note also that it does not work to
73697** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
73698** to reduce a pristine expression tree from the parser.  The implementation
73699** of dupedExprStructSize() contain multiple assert() statements that attempt
73700** to enforce this constraint.
73701*/
73702static int dupedExprStructSize(Expr *p, int flags){
73703  int nSize;
73704  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
73705  if( 0==(flags&EXPRDUP_REDUCE) ){
73706    nSize = EXPR_FULLSIZE;
73707  }else{
73708    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
73709    assert( !ExprHasProperty(p, EP_FromJoin) );
73710    assert( (p->flags2 & EP2_MallocedToken)==0 );
73711    assert( (p->flags2 & EP2_Irreducible)==0 );
73712    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
73713      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
73714    }else{
73715      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
73716    }
73717  }
73718  return nSize;
73719}
73720
73721/*
73722** This function returns the space in bytes required to store the copy
73723** of the Expr structure and a copy of the Expr.u.zToken string (if that
73724** string is defined.)
73725*/
73726static int dupedExprNodeSize(Expr *p, int flags){
73727  int nByte = dupedExprStructSize(p, flags) & 0xfff;
73728  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
73729    nByte += sqlite3Strlen30(p->u.zToken)+1;
73730  }
73731  return ROUND8(nByte);
73732}
73733
73734/*
73735** Return the number of bytes required to create a duplicate of the
73736** expression passed as the first argument. The second argument is a
73737** mask containing EXPRDUP_XXX flags.
73738**
73739** The value returned includes space to create a copy of the Expr struct
73740** itself and the buffer referred to by Expr.u.zToken, if any.
73741**
73742** If the EXPRDUP_REDUCE flag is set, then the return value includes
73743** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
73744** and Expr.pRight variables (but not for any structures pointed to or
73745** descended from the Expr.x.pList or Expr.x.pSelect variables).
73746*/
73747static int dupedExprSize(Expr *p, int flags){
73748  int nByte = 0;
73749  if( p ){
73750    nByte = dupedExprNodeSize(p, flags);
73751    if( flags&EXPRDUP_REDUCE ){
73752      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
73753    }
73754  }
73755  return nByte;
73756}
73757
73758/*
73759** This function is similar to sqlite3ExprDup(), except that if pzBuffer
73760** is not NULL then *pzBuffer is assumed to point to a buffer large enough
73761** to store the copy of expression p, the copies of p->u.zToken
73762** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
73763** if any. Before returning, *pzBuffer is set to the first byte passed the
73764** portion of the buffer copied into by this function.
73765*/
73766static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
73767  Expr *pNew = 0;                      /* Value to return */
73768  if( p ){
73769    const int isReduced = (flags&EXPRDUP_REDUCE);
73770    u8 *zAlloc;
73771    u32 staticFlag = 0;
73772
73773    assert( pzBuffer==0 || isReduced );
73774
73775    /* Figure out where to write the new Expr structure. */
73776    if( pzBuffer ){
73777      zAlloc = *pzBuffer;
73778      staticFlag = EP_Static;
73779    }else{
73780      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
73781    }
73782    pNew = (Expr *)zAlloc;
73783
73784    if( pNew ){
73785      /* Set nNewSize to the size allocated for the structure pointed to
73786      ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
73787      ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
73788      ** by the copy of the p->u.zToken string (if any).
73789      */
73790      const unsigned nStructSize = dupedExprStructSize(p, flags);
73791      const int nNewSize = nStructSize & 0xfff;
73792      int nToken;
73793      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
73794        nToken = sqlite3Strlen30(p->u.zToken) + 1;
73795      }else{
73796        nToken = 0;
73797      }
73798      if( isReduced ){
73799        assert( ExprHasProperty(p, EP_Reduced)==0 );
73800        memcpy(zAlloc, p, nNewSize);
73801      }else{
73802        int nSize = exprStructSize(p);
73803        memcpy(zAlloc, p, nSize);
73804        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
73805      }
73806
73807      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
73808      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
73809      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
73810      pNew->flags |= staticFlag;
73811
73812      /* Copy the p->u.zToken string, if any. */
73813      if( nToken ){
73814        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
73815        memcpy(zToken, p->u.zToken, nToken);
73816      }
73817
73818      if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
73819        /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
73820        if( ExprHasProperty(p, EP_xIsSelect) ){
73821          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
73822        }else{
73823          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
73824        }
73825      }
73826
73827      /* Fill in pNew->pLeft and pNew->pRight. */
73828      if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
73829        zAlloc += dupedExprNodeSize(p, flags);
73830        if( ExprHasProperty(pNew, EP_Reduced) ){
73831          pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
73832          pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
73833        }
73834        if( pzBuffer ){
73835          *pzBuffer = zAlloc;
73836        }
73837      }else{
73838        pNew->flags2 = 0;
73839        if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
73840          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
73841          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
73842        }
73843      }
73844
73845    }
73846  }
73847  return pNew;
73848}
73849
73850/*
73851** The following group of routines make deep copies of expressions,
73852** expression lists, ID lists, and select statements.  The copies can
73853** be deleted (by being passed to their respective ...Delete() routines)
73854** without effecting the originals.
73855**
73856** The expression list, ID, and source lists return by sqlite3ExprListDup(),
73857** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
73858** by subsequent calls to sqlite*ListAppend() routines.
73859**
73860** Any tables that the SrcList might point to are not duplicated.
73861**
73862** The flags parameter contains a combination of the EXPRDUP_XXX flags.
73863** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
73864** truncated version of the usual Expr structure that will be stored as
73865** part of the in-memory representation of the database schema.
73866*/
73867SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
73868  return exprDup(db, p, flags, 0);
73869}
73870SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
73871  ExprList *pNew;
73872  struct ExprList_item *pItem, *pOldItem;
73873  int i;
73874  if( p==0 ) return 0;
73875  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
73876  if( pNew==0 ) return 0;
73877  pNew->iECursor = 0;
73878  pNew->nExpr = pNew->nAlloc = p->nExpr;
73879  pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
73880  if( pItem==0 ){
73881    sqlite3DbFree(db, pNew);
73882    return 0;
73883  }
73884  pOldItem = p->a;
73885  for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
73886    Expr *pOldExpr = pOldItem->pExpr;
73887    pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
73888    pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
73889    pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
73890    pItem->sortOrder = pOldItem->sortOrder;
73891    pItem->done = 0;
73892    pItem->iCol = pOldItem->iCol;
73893    pItem->iAlias = pOldItem->iAlias;
73894  }
73895  return pNew;
73896}
73897
73898/*
73899** If cursors, triggers, views and subqueries are all omitted from
73900** the build, then none of the following routines, except for
73901** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
73902** called with a NULL argument.
73903*/
73904#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
73905 || !defined(SQLITE_OMIT_SUBQUERY)
73906SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
73907  SrcList *pNew;
73908  int i;
73909  int nByte;
73910  if( p==0 ) return 0;
73911  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
73912  pNew = sqlite3DbMallocRaw(db, nByte );
73913  if( pNew==0 ) return 0;
73914  pNew->nSrc = pNew->nAlloc = p->nSrc;
73915  for(i=0; i<p->nSrc; i++){
73916    struct SrcList_item *pNewItem = &pNew->a[i];
73917    struct SrcList_item *pOldItem = &p->a[i];
73918    Table *pTab;
73919    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
73920    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
73921    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
73922    pNewItem->jointype = pOldItem->jointype;
73923    pNewItem->iCursor = pOldItem->iCursor;
73924    pNewItem->addrFillSub = pOldItem->addrFillSub;
73925    pNewItem->regReturn = pOldItem->regReturn;
73926    pNewItem->isCorrelated = pOldItem->isCorrelated;
73927    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
73928    pNewItem->notIndexed = pOldItem->notIndexed;
73929    pNewItem->pIndex = pOldItem->pIndex;
73930    pTab = pNewItem->pTab = pOldItem->pTab;
73931    if( pTab ){
73932      pTab->nRef++;
73933    }
73934    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
73935    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
73936    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
73937    pNewItem->colUsed = pOldItem->colUsed;
73938  }
73939  return pNew;
73940}
73941SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
73942  IdList *pNew;
73943  int i;
73944  if( p==0 ) return 0;
73945  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
73946  if( pNew==0 ) return 0;
73947  pNew->nId = pNew->nAlloc = p->nId;
73948  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
73949  if( pNew->a==0 ){
73950    sqlite3DbFree(db, pNew);
73951    return 0;
73952  }
73953  for(i=0; i<p->nId; i++){
73954    struct IdList_item *pNewItem = &pNew->a[i];
73955    struct IdList_item *pOldItem = &p->a[i];
73956    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
73957    pNewItem->idx = pOldItem->idx;
73958  }
73959  return pNew;
73960}
73961SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
73962  Select *pNew;
73963  if( p==0 ) return 0;
73964  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
73965  if( pNew==0 ) return 0;
73966  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
73967  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
73968  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
73969  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
73970  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
73971  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
73972  pNew->op = p->op;
73973  pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
73974  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
73975  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
73976  pNew->iLimit = 0;
73977  pNew->iOffset = 0;
73978  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
73979  pNew->pRightmost = 0;
73980  pNew->addrOpenEphm[0] = -1;
73981  pNew->addrOpenEphm[1] = -1;
73982  pNew->addrOpenEphm[2] = -1;
73983  return pNew;
73984}
73985#else
73986SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
73987  assert( p==0 );
73988  return 0;
73989}
73990#endif
73991
73992
73993/*
73994** Add a new element to the end of an expression list.  If pList is
73995** initially NULL, then create a new expression list.
73996**
73997** If a memory allocation error occurs, the entire list is freed and
73998** NULL is returned.  If non-NULL is returned, then it is guaranteed
73999** that the new entry was successfully appended.
74000*/
74001SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
74002  Parse *pParse,          /* Parsing context */
74003  ExprList *pList,        /* List to which to append. Might be NULL */
74004  Expr *pExpr             /* Expression to be appended. Might be NULL */
74005){
74006  sqlite3 *db = pParse->db;
74007  if( pList==0 ){
74008    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
74009    if( pList==0 ){
74010      goto no_mem;
74011    }
74012    assert( pList->nAlloc==0 );
74013  }
74014  if( pList->nAlloc<=pList->nExpr ){
74015    struct ExprList_item *a;
74016    int n = pList->nAlloc*2 + 4;
74017    a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
74018    if( a==0 ){
74019      goto no_mem;
74020    }
74021    pList->a = a;
74022    pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
74023  }
74024  assert( pList->a!=0 );
74025  if( 1 ){
74026    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
74027    memset(pItem, 0, sizeof(*pItem));
74028    pItem->pExpr = pExpr;
74029  }
74030  return pList;
74031
74032no_mem:
74033  /* Avoid leaking memory if malloc has failed. */
74034  sqlite3ExprDelete(db, pExpr);
74035  sqlite3ExprListDelete(db, pList);
74036  return 0;
74037}
74038
74039/*
74040** Set the ExprList.a[].zName element of the most recently added item
74041** on the expression list.
74042**
74043** pList might be NULL following an OOM error.  But pName should never be
74044** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
74045** is set.
74046*/
74047SQLITE_PRIVATE void sqlite3ExprListSetName(
74048  Parse *pParse,          /* Parsing context */
74049  ExprList *pList,        /* List to which to add the span. */
74050  Token *pName,           /* Name to be added */
74051  int dequote             /* True to cause the name to be dequoted */
74052){
74053  assert( pList!=0 || pParse->db->mallocFailed!=0 );
74054  if( pList ){
74055    struct ExprList_item *pItem;
74056    assert( pList->nExpr>0 );
74057    pItem = &pList->a[pList->nExpr-1];
74058    assert( pItem->zName==0 );
74059    pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
74060    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
74061  }
74062}
74063
74064/*
74065** Set the ExprList.a[].zSpan element of the most recently added item
74066** on the expression list.
74067**
74068** pList might be NULL following an OOM error.  But pSpan should never be
74069** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
74070** is set.
74071*/
74072SQLITE_PRIVATE void sqlite3ExprListSetSpan(
74073  Parse *pParse,          /* Parsing context */
74074  ExprList *pList,        /* List to which to add the span. */
74075  ExprSpan *pSpan         /* The span to be added */
74076){
74077  sqlite3 *db = pParse->db;
74078  assert( pList!=0 || db->mallocFailed!=0 );
74079  if( pList ){
74080    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
74081    assert( pList->nExpr>0 );
74082    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
74083    sqlite3DbFree(db, pItem->zSpan);
74084    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
74085                                    (int)(pSpan->zEnd - pSpan->zStart));
74086  }
74087}
74088
74089/*
74090** If the expression list pEList contains more than iLimit elements,
74091** leave an error message in pParse.
74092*/
74093SQLITE_PRIVATE void sqlite3ExprListCheckLength(
74094  Parse *pParse,
74095  ExprList *pEList,
74096  const char *zObject
74097){
74098  int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
74099  testcase( pEList && pEList->nExpr==mx );
74100  testcase( pEList && pEList->nExpr==mx+1 );
74101  if( pEList && pEList->nExpr>mx ){
74102    sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
74103  }
74104}
74105
74106/*
74107** Delete an entire expression list.
74108*/
74109SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
74110  int i;
74111  struct ExprList_item *pItem;
74112  if( pList==0 ) return;
74113  assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
74114  assert( pList->nExpr<=pList->nAlloc );
74115  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
74116    sqlite3ExprDelete(db, pItem->pExpr);
74117    sqlite3DbFree(db, pItem->zName);
74118    sqlite3DbFree(db, pItem->zSpan);
74119  }
74120  sqlite3DbFree(db, pList->a);
74121  sqlite3DbFree(db, pList);
74122}
74123
74124/*
74125** These routines are Walker callbacks.  Walker.u.pi is a pointer
74126** to an integer.  These routines are checking an expression to see
74127** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
74128** not constant.
74129**
74130** These callback routines are used to implement the following:
74131**
74132**     sqlite3ExprIsConstant()
74133**     sqlite3ExprIsConstantNotJoin()
74134**     sqlite3ExprIsConstantOrFunction()
74135**
74136*/
74137static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
74138
74139  /* If pWalker->u.i is 3 then any term of the expression that comes from
74140  ** the ON or USING clauses of a join disqualifies the expression
74141  ** from being considered constant. */
74142  if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
74143    pWalker->u.i = 0;
74144    return WRC_Abort;
74145  }
74146
74147  switch( pExpr->op ){
74148    /* Consider functions to be constant if all their arguments are constant
74149    ** and pWalker->u.i==2 */
74150    case TK_FUNCTION:
74151      if( pWalker->u.i==2 ) return 0;
74152      /* Fall through */
74153    case TK_ID:
74154    case TK_COLUMN:
74155    case TK_AGG_FUNCTION:
74156    case TK_AGG_COLUMN:
74157      testcase( pExpr->op==TK_ID );
74158      testcase( pExpr->op==TK_COLUMN );
74159      testcase( pExpr->op==TK_AGG_FUNCTION );
74160      testcase( pExpr->op==TK_AGG_COLUMN );
74161      pWalker->u.i = 0;
74162      return WRC_Abort;
74163    default:
74164      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
74165      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
74166      return WRC_Continue;
74167  }
74168}
74169static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
74170  UNUSED_PARAMETER(NotUsed);
74171  pWalker->u.i = 0;
74172  return WRC_Abort;
74173}
74174static int exprIsConst(Expr *p, int initFlag){
74175  Walker w;
74176  w.u.i = initFlag;
74177  w.xExprCallback = exprNodeIsConstant;
74178  w.xSelectCallback = selectNodeIsConstant;
74179  sqlite3WalkExpr(&w, p);
74180  return w.u.i;
74181}
74182
74183/*
74184** Walk an expression tree.  Return 1 if the expression is constant
74185** and 0 if it involves variables or function calls.
74186**
74187** For the purposes of this function, a double-quoted string (ex: "abc")
74188** is considered a variable but a single-quoted string (ex: 'abc') is
74189** a constant.
74190*/
74191SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
74192  return exprIsConst(p, 1);
74193}
74194
74195/*
74196** Walk an expression tree.  Return 1 if the expression is constant
74197** that does no originate from the ON or USING clauses of a join.
74198** Return 0 if it involves variables or function calls or terms from
74199** an ON or USING clause.
74200*/
74201SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
74202  return exprIsConst(p, 3);
74203}
74204
74205/*
74206** Walk an expression tree.  Return 1 if the expression is constant
74207** or a function call with constant arguments.  Return and 0 if there
74208** are any variables.
74209**
74210** For the purposes of this function, a double-quoted string (ex: "abc")
74211** is considered a variable but a single-quoted string (ex: 'abc') is
74212** a constant.
74213*/
74214SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
74215  return exprIsConst(p, 2);
74216}
74217
74218/*
74219** If the expression p codes a constant integer that is small enough
74220** to fit in a 32-bit integer, return 1 and put the value of the integer
74221** in *pValue.  If the expression is not an integer or if it is too big
74222** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
74223*/
74224SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
74225  int rc = 0;
74226
74227  /* If an expression is an integer literal that fits in a signed 32-bit
74228  ** integer, then the EP_IntValue flag will have already been set */
74229  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
74230           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
74231
74232  if( p->flags & EP_IntValue ){
74233    *pValue = p->u.iValue;
74234    return 1;
74235  }
74236  switch( p->op ){
74237    case TK_UPLUS: {
74238      rc = sqlite3ExprIsInteger(p->pLeft, pValue);
74239      break;
74240    }
74241    case TK_UMINUS: {
74242      int v;
74243      if( sqlite3ExprIsInteger(p->pLeft, &v) ){
74244        *pValue = -v;
74245        rc = 1;
74246      }
74247      break;
74248    }
74249    default: break;
74250  }
74251  return rc;
74252}
74253
74254/*
74255** Return FALSE if there is no chance that the expression can be NULL.
74256**
74257** If the expression might be NULL or if the expression is too complex
74258** to tell return TRUE.
74259**
74260** This routine is used as an optimization, to skip OP_IsNull opcodes
74261** when we know that a value cannot be NULL.  Hence, a false positive
74262** (returning TRUE when in fact the expression can never be NULL) might
74263** be a small performance hit but is otherwise harmless.  On the other
74264** hand, a false negative (returning FALSE when the result could be NULL)
74265** will likely result in an incorrect answer.  So when in doubt, return
74266** TRUE.
74267*/
74268SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
74269  u8 op;
74270  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
74271  op = p->op;
74272  if( op==TK_REGISTER ) op = p->op2;
74273  switch( op ){
74274    case TK_INTEGER:
74275    case TK_STRING:
74276    case TK_FLOAT:
74277    case TK_BLOB:
74278      return 0;
74279    default:
74280      return 1;
74281  }
74282}
74283
74284/*
74285** Generate an OP_IsNull instruction that tests register iReg and jumps
74286** to location iDest if the value in iReg is NULL.  The value in iReg
74287** was computed by pExpr.  If we can look at pExpr at compile-time and
74288** determine that it can never generate a NULL, then the OP_IsNull operation
74289** can be omitted.
74290*/
74291SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
74292  Vdbe *v,            /* The VDBE under construction */
74293  const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
74294  int iReg,           /* Test the value in this register for NULL */
74295  int iDest           /* Jump here if the value is null */
74296){
74297  if( sqlite3ExprCanBeNull(pExpr) ){
74298    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
74299  }
74300}
74301
74302/*
74303** Return TRUE if the given expression is a constant which would be
74304** unchanged by OP_Affinity with the affinity given in the second
74305** argument.
74306**
74307** This routine is used to determine if the OP_Affinity operation
74308** can be omitted.  When in doubt return FALSE.  A false negative
74309** is harmless.  A false positive, however, can result in the wrong
74310** answer.
74311*/
74312SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
74313  u8 op;
74314  if( aff==SQLITE_AFF_NONE ) return 1;
74315  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
74316  op = p->op;
74317  if( op==TK_REGISTER ) op = p->op2;
74318  switch( op ){
74319    case TK_INTEGER: {
74320      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
74321    }
74322    case TK_FLOAT: {
74323      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
74324    }
74325    case TK_STRING: {
74326      return aff==SQLITE_AFF_TEXT;
74327    }
74328    case TK_BLOB: {
74329      return 1;
74330    }
74331    case TK_COLUMN: {
74332      assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
74333      return p->iColumn<0
74334          && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
74335    }
74336    default: {
74337      return 0;
74338    }
74339  }
74340}
74341
74342/*
74343** Return TRUE if the given string is a row-id column name.
74344*/
74345SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
74346  if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
74347  if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
74348  if( sqlite3StrICmp(z, "OID")==0 ) return 1;
74349  return 0;
74350}
74351
74352/*
74353** Return true if we are able to the IN operator optimization on a
74354** query of the form
74355**
74356**       x IN (SELECT ...)
74357**
74358** Where the SELECT... clause is as specified by the parameter to this
74359** routine.
74360**
74361** The Select object passed in has already been preprocessed and no
74362** errors have been found.
74363*/
74364#ifndef SQLITE_OMIT_SUBQUERY
74365static int isCandidateForInOpt(Select *p){
74366  SrcList *pSrc;
74367  ExprList *pEList;
74368  Table *pTab;
74369  if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
74370  if( p->pPrior ) return 0;              /* Not a compound SELECT */
74371  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
74372    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
74373    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
74374    return 0; /* No DISTINCT keyword and no aggregate functions */
74375  }
74376  assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
74377  if( p->pLimit ) return 0;              /* Has no LIMIT clause */
74378  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
74379  if( p->pWhere ) return 0;              /* Has no WHERE clause */
74380  pSrc = p->pSrc;
74381  assert( pSrc!=0 );
74382  if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
74383  if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
74384  pTab = pSrc->a[0].pTab;
74385  if( NEVER(pTab==0) ) return 0;
74386  assert( pTab->pSelect==0 );            /* FROM clause is not a view */
74387  if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
74388  pEList = p->pEList;
74389  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
74390  if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
74391  return 1;
74392}
74393#endif /* SQLITE_OMIT_SUBQUERY */
74394
74395/*
74396** This function is used by the implementation of the IN (...) operator.
74397** It's job is to find or create a b-tree structure that may be used
74398** either to test for membership of the (...) set or to iterate through
74399** its members, skipping duplicates.
74400**
74401** The index of the cursor opened on the b-tree (database table, database index
74402** or ephermal table) is stored in pX->iTable before this function returns.
74403** The returned value of this function indicates the b-tree type, as follows:
74404**
74405**   IN_INDEX_ROWID - The cursor was opened on a database table.
74406**   IN_INDEX_INDEX - The cursor was opened on a database index.
74407**   IN_INDEX_EPH -   The cursor was opened on a specially created and
74408**                    populated epheremal table.
74409**
74410** An existing b-tree may only be used if the SELECT is of the simple
74411** form:
74412**
74413**     SELECT <column> FROM <table>
74414**
74415** If the prNotFound parameter is 0, then the b-tree will be used to iterate
74416** through the set members, skipping any duplicates. In this case an
74417** epheremal table must be used unless the selected <column> is guaranteed
74418** to be unique - either because it is an INTEGER PRIMARY KEY or it
74419** has a UNIQUE constraint or UNIQUE index.
74420**
74421** If the prNotFound parameter is not 0, then the b-tree will be used
74422** for fast set membership tests. In this case an epheremal table must
74423** be used unless <column> is an INTEGER PRIMARY KEY or an index can
74424** be found with <column> as its left-most column.
74425**
74426** When the b-tree is being used for membership tests, the calling function
74427** needs to know whether or not the structure contains an SQL NULL
74428** value in order to correctly evaluate expressions like "X IN (Y, Z)".
74429** If there is any chance that the (...) might contain a NULL value at
74430** runtime, then a register is allocated and the register number written
74431** to *prNotFound. If there is no chance that the (...) contains a
74432** NULL value, then *prNotFound is left unchanged.
74433**
74434** If a register is allocated and its location stored in *prNotFound, then
74435** its initial value is NULL.  If the (...) does not remain constant
74436** for the duration of the query (i.e. the SELECT within the (...)
74437** is a correlated subquery) then the value of the allocated register is
74438** reset to NULL each time the subquery is rerun. This allows the
74439** caller to use vdbe code equivalent to the following:
74440**
74441**   if( register==NULL ){
74442**     has_null = <test if data structure contains null>
74443**     register = 1
74444**   }
74445**
74446** in order to avoid running the <test if data structure contains null>
74447** test more often than is necessary.
74448*/
74449#ifndef SQLITE_OMIT_SUBQUERY
74450SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
74451  Select *p;                            /* SELECT to the right of IN operator */
74452  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
74453  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
74454  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
74455
74456  assert( pX->op==TK_IN );
74457
74458  /* Check to see if an existing table or index can be used to
74459  ** satisfy the query.  This is preferable to generating a new
74460  ** ephemeral table.
74461  */
74462  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
74463  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
74464    sqlite3 *db = pParse->db;              /* Database connection */
74465    Expr *pExpr = p->pEList->a[0].pExpr;   /* Expression <column> */
74466    int iCol = pExpr->iColumn;             /* Index of column <column> */
74467    Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
74468    Table *pTab = p->pSrc->a[0].pTab;      /* Table <table>. */
74469    int iDb;                               /* Database idx for pTab */
74470
74471    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
74472    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
74473    sqlite3CodeVerifySchema(pParse, iDb);
74474    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
74475
74476    /* This function is only called from two places. In both cases the vdbe
74477    ** has already been allocated. So assume sqlite3GetVdbe() is always
74478    ** successful here.
74479    */
74480    assert(v);
74481    if( iCol<0 ){
74482      int iMem = ++pParse->nMem;
74483      int iAddr;
74484
74485      iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
74486
74487      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
74488      eType = IN_INDEX_ROWID;
74489
74490      sqlite3VdbeJumpHere(v, iAddr);
74491    }else{
74492      Index *pIdx;                         /* Iterator variable */
74493
74494      /* The collation sequence used by the comparison. If an index is to
74495      ** be used in place of a temp-table, it must be ordered according
74496      ** to this collation sequence.  */
74497      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
74498
74499      /* Check that the affinity that will be used to perform the
74500      ** comparison is the same as the affinity of the column. If
74501      ** it is not, it is not possible to use any index.
74502      */
74503      char aff = comparisonAffinity(pX);
74504      int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
74505
74506      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
74507        if( (pIdx->aiColumn[0]==iCol)
74508         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
74509         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
74510        ){
74511          int iMem = ++pParse->nMem;
74512          int iAddr;
74513          char *pKey;
74514
74515          pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
74516          iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
74517
74518          sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
74519                               pKey,P4_KEYINFO_HANDOFF);
74520          VdbeComment((v, "%s", pIdx->zName));
74521          eType = IN_INDEX_INDEX;
74522
74523          sqlite3VdbeJumpHere(v, iAddr);
74524          if( prNotFound && !pTab->aCol[iCol].notNull ){
74525            *prNotFound = ++pParse->nMem;
74526          }
74527        }
74528      }
74529    }
74530  }
74531
74532  if( eType==0 ){
74533    /* Could not found an existing table or index to use as the RHS b-tree.
74534    ** We will have to generate an ephemeral table to do the job.
74535    */
74536    double savedNQueryLoop = pParse->nQueryLoop;
74537    int rMayHaveNull = 0;
74538    eType = IN_INDEX_EPH;
74539    if( prNotFound ){
74540      *prNotFound = rMayHaveNull = ++pParse->nMem;
74541    }else{
74542      testcase( pParse->nQueryLoop>(double)1 );
74543      pParse->nQueryLoop = (double)1;
74544      if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
74545        eType = IN_INDEX_ROWID;
74546      }
74547    }
74548    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
74549    pParse->nQueryLoop = savedNQueryLoop;
74550  }else{
74551    pX->iTable = iTab;
74552  }
74553  return eType;
74554}
74555#endif
74556
74557/*
74558** Generate code for scalar subqueries used as a subquery expression, EXISTS,
74559** or IN operators.  Examples:
74560**
74561**     (SELECT a FROM b)          -- subquery
74562**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
74563**     x IN (4,5,11)              -- IN operator with list on right-hand side
74564**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
74565**
74566** The pExpr parameter describes the expression that contains the IN
74567** operator or subquery.
74568**
74569** If parameter isRowid is non-zero, then expression pExpr is guaranteed
74570** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
74571** to some integer key column of a table B-Tree. In this case, use an
74572** intkey B-Tree to store the set of IN(...) values instead of the usual
74573** (slower) variable length keys B-Tree.
74574**
74575** If rMayHaveNull is non-zero, that means that the operation is an IN
74576** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
74577** Furthermore, the IN is in a WHERE clause and that we really want
74578** to iterate over the RHS of the IN operator in order to quickly locate
74579** all corresponding LHS elements.  All this routine does is initialize
74580** the register given by rMayHaveNull to NULL.  Calling routines will take
74581** care of changing this register value to non-NULL if the RHS is NULL-free.
74582**
74583** If rMayHaveNull is zero, that means that the subquery is being used
74584** for membership testing only.  There is no need to initialize any
74585** registers to indicate the presense or absence of NULLs on the RHS.
74586**
74587** For a SELECT or EXISTS operator, return the register that holds the
74588** result.  For IN operators or if an error occurs, the return value is 0.
74589*/
74590#ifndef SQLITE_OMIT_SUBQUERY
74591SQLITE_PRIVATE int sqlite3CodeSubselect(
74592  Parse *pParse,          /* Parsing context */
74593  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
74594  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
74595  int isRowid             /* If true, LHS of IN operator is a rowid */
74596){
74597  int testAddr = -1;                      /* One-time test address */
74598  int rReg = 0;                           /* Register storing resulting */
74599  Vdbe *v = sqlite3GetVdbe(pParse);
74600  if( NEVER(v==0) ) return 0;
74601  sqlite3ExprCachePush(pParse);
74602
74603  /* This code must be run in its entirety every time it is encountered
74604  ** if any of the following is true:
74605  **
74606  **    *  The right-hand side is a correlated subquery
74607  **    *  The right-hand side is an expression list containing variables
74608  **    *  We are inside a trigger
74609  **
74610  ** If all of the above are false, then we can run this code just once
74611  ** save the results, and reuse the same result on subsequent invocations.
74612  */
74613  if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
74614    int mem = ++pParse->nMem;
74615    testAddr = sqlite3VdbeAddOp1(v, OP_Once, mem);
74616  }
74617
74618#ifndef SQLITE_OMIT_EXPLAIN
74619  if( pParse->explain==2 ){
74620    char *zMsg = sqlite3MPrintf(
74621        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
74622        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
74623    );
74624    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
74625  }
74626#endif
74627
74628  switch( pExpr->op ){
74629    case TK_IN: {
74630      char affinity;              /* Affinity of the LHS of the IN */
74631      KeyInfo keyInfo;            /* Keyinfo for the generated table */
74632      int addr;                   /* Address of OP_OpenEphemeral instruction */
74633      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
74634
74635      if( rMayHaveNull ){
74636        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
74637      }
74638
74639      affinity = sqlite3ExprAffinity(pLeft);
74640
74641      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
74642      ** expression it is handled the same way.  An ephemeral table is
74643      ** filled with single-field index keys representing the results
74644      ** from the SELECT or the <exprlist>.
74645      **
74646      ** If the 'x' expression is a column value, or the SELECT...
74647      ** statement returns a column value, then the affinity of that
74648      ** column is used to build the index keys. If both 'x' and the
74649      ** SELECT... statement are columns, then numeric affinity is used
74650      ** if either column has NUMERIC or INTEGER affinity. If neither
74651      ** 'x' nor the SELECT... statement are columns, then numeric affinity
74652      ** is used.
74653      */
74654      pExpr->iTable = pParse->nTab++;
74655      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
74656      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
74657      memset(&keyInfo, 0, sizeof(keyInfo));
74658      keyInfo.nField = 1;
74659
74660      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
74661        /* Case 1:     expr IN (SELECT ...)
74662        **
74663        ** Generate code to write the results of the select into the temporary
74664        ** table allocated and opened above.
74665        */
74666        SelectDest dest;
74667        ExprList *pEList;
74668
74669        assert( !isRowid );
74670        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
74671        dest.affinity = (u8)affinity;
74672        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
74673        pExpr->x.pSelect->iLimit = 0;
74674        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
74675          return 0;
74676        }
74677        pEList = pExpr->x.pSelect->pEList;
74678        if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){
74679          keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
74680              pEList->a[0].pExpr);
74681        }
74682      }else if( ALWAYS(pExpr->x.pList!=0) ){
74683        /* Case 2:     expr IN (exprlist)
74684        **
74685        ** For each expression, build an index key from the evaluation and
74686        ** store it in the temporary table. If <expr> is a column, then use
74687        ** that columns affinity when building index keys. If <expr> is not
74688        ** a column, use numeric affinity.
74689        */
74690        int i;
74691        ExprList *pList = pExpr->x.pList;
74692        struct ExprList_item *pItem;
74693        int r1, r2, r3;
74694
74695        if( !affinity ){
74696          affinity = SQLITE_AFF_NONE;
74697        }
74698        keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
74699
74700        /* Loop through each expression in <exprlist>. */
74701        r1 = sqlite3GetTempReg(pParse);
74702        r2 = sqlite3GetTempReg(pParse);
74703        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
74704        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
74705          Expr *pE2 = pItem->pExpr;
74706          int iValToIns;
74707
74708          /* If the expression is not constant then we will need to
74709          ** disable the test that was generated above that makes sure
74710          ** this code only executes once.  Because for a non-constant
74711          ** expression we need to rerun this code each time.
74712          */
74713          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
74714            sqlite3VdbeChangeToNoop(v, testAddr);
74715            testAddr = -1;
74716          }
74717
74718          /* Evaluate the expression and insert it into the temp table */
74719          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
74720            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
74721          }else{
74722            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
74723            if( isRowid ){
74724              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
74725                                sqlite3VdbeCurrentAddr(v)+2);
74726              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
74727            }else{
74728              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
74729              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
74730              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
74731            }
74732          }
74733        }
74734        sqlite3ReleaseTempReg(pParse, r1);
74735        sqlite3ReleaseTempReg(pParse, r2);
74736      }
74737      if( !isRowid ){
74738        sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
74739      }
74740      break;
74741    }
74742
74743    case TK_EXISTS:
74744    case TK_SELECT:
74745    default: {
74746      /* If this has to be a scalar SELECT.  Generate code to put the
74747      ** value of this select in a memory cell and record the number
74748      ** of the memory cell in iColumn.  If this is an EXISTS, write
74749      ** an integer 0 (not exists) or 1 (exists) into a memory cell
74750      ** and record that memory cell in iColumn.
74751      */
74752      Select *pSel;                         /* SELECT statement to encode */
74753      SelectDest dest;                      /* How to deal with SELECt result */
74754
74755      testcase( pExpr->op==TK_EXISTS );
74756      testcase( pExpr->op==TK_SELECT );
74757      assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
74758
74759      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
74760      pSel = pExpr->x.pSelect;
74761      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
74762      if( pExpr->op==TK_SELECT ){
74763        dest.eDest = SRT_Mem;
74764        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
74765        VdbeComment((v, "Init subquery result"));
74766      }else{
74767        dest.eDest = SRT_Exists;
74768        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
74769        VdbeComment((v, "Init EXISTS result"));
74770      }
74771      sqlite3ExprDelete(pParse->db, pSel->pLimit);
74772      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
74773                                  &sqlite3IntTokens[1]);
74774      pSel->iLimit = 0;
74775      if( sqlite3Select(pParse, pSel, &dest) ){
74776        return 0;
74777      }
74778      rReg = dest.iParm;
74779      ExprSetIrreducible(pExpr);
74780      break;
74781    }
74782  }
74783
74784  if( testAddr>=0 ){
74785    sqlite3VdbeJumpHere(v, testAddr);
74786  }
74787  sqlite3ExprCachePop(pParse, 1);
74788
74789  return rReg;
74790}
74791#endif /* SQLITE_OMIT_SUBQUERY */
74792
74793#ifndef SQLITE_OMIT_SUBQUERY
74794/*
74795** Generate code for an IN expression.
74796**
74797**      x IN (SELECT ...)
74798**      x IN (value, value, ...)
74799**
74800** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
74801** is an array of zero or more values.  The expression is true if the LHS is
74802** contained within the RHS.  The value of the expression is unknown (NULL)
74803** if the LHS is NULL or if the LHS is not contained within the RHS and the
74804** RHS contains one or more NULL values.
74805**
74806** This routine generates code will jump to destIfFalse if the LHS is not
74807** contained within the RHS.  If due to NULLs we cannot determine if the LHS
74808** is contained in the RHS then jump to destIfNull.  If the LHS is contained
74809** within the RHS then fall through.
74810*/
74811static void sqlite3ExprCodeIN(
74812  Parse *pParse,        /* Parsing and code generating context */
74813  Expr *pExpr,          /* The IN expression */
74814  int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
74815  int destIfNull        /* Jump here if the results are unknown due to NULLs */
74816){
74817  int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
74818  char affinity;        /* Comparison affinity to use */
74819  int eType;            /* Type of the RHS */
74820  int r1;               /* Temporary use register */
74821  Vdbe *v;              /* Statement under construction */
74822
74823  /* Compute the RHS.   After this step, the table with cursor
74824  ** pExpr->iTable will contains the values that make up the RHS.
74825  */
74826  v = pParse->pVdbe;
74827  assert( v!=0 );       /* OOM detected prior to this routine */
74828  VdbeNoopComment((v, "begin IN expr"));
74829  eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
74830
74831  /* Figure out the affinity to use to create a key from the results
74832  ** of the expression. affinityStr stores a static string suitable for
74833  ** P4 of OP_MakeRecord.
74834  */
74835  affinity = comparisonAffinity(pExpr);
74836
74837  /* Code the LHS, the <expr> from "<expr> IN (...)".
74838  */
74839  sqlite3ExprCachePush(pParse);
74840  r1 = sqlite3GetTempReg(pParse);
74841  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
74842
74843  /* If the LHS is NULL, then the result is either false or NULL depending
74844  ** on whether the RHS is empty or not, respectively.
74845  */
74846  if( destIfNull==destIfFalse ){
74847    /* Shortcut for the common case where the false and NULL outcomes are
74848    ** the same. */
74849    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
74850  }else{
74851    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
74852    sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
74853    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
74854    sqlite3VdbeJumpHere(v, addr1);
74855  }
74856
74857  if( eType==IN_INDEX_ROWID ){
74858    /* In this case, the RHS is the ROWID of table b-tree
74859    */
74860    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
74861    sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
74862  }else{
74863    /* In this case, the RHS is an index b-tree.
74864    */
74865    sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
74866
74867    /* If the set membership test fails, then the result of the
74868    ** "x IN (...)" expression must be either 0 or NULL. If the set
74869    ** contains no NULL values, then the result is 0. If the set
74870    ** contains one or more NULL values, then the result of the
74871    ** expression is also NULL.
74872    */
74873    if( rRhsHasNull==0 || destIfFalse==destIfNull ){
74874      /* This branch runs if it is known at compile time that the RHS
74875      ** cannot contain NULL values. This happens as the result
74876      ** of a "NOT NULL" constraint in the database schema.
74877      **
74878      ** Also run this branch if NULL is equivalent to FALSE
74879      ** for this particular IN operator.
74880      */
74881      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
74882
74883    }else{
74884      /* In this branch, the RHS of the IN might contain a NULL and
74885      ** the presence of a NULL on the RHS makes a difference in the
74886      ** outcome.
74887      */
74888      int j1, j2, j3;
74889
74890      /* First check to see if the LHS is contained in the RHS.  If so,
74891      ** then the presence of NULLs in the RHS does not matter, so jump
74892      ** over all of the code that follows.
74893      */
74894      j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
74895
74896      /* Here we begin generating code that runs if the LHS is not
74897      ** contained within the RHS.  Generate additional code that
74898      ** tests the RHS for NULLs.  If the RHS contains a NULL then
74899      ** jump to destIfNull.  If there are no NULLs in the RHS then
74900      ** jump to destIfFalse.
74901      */
74902      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
74903      j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
74904      sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
74905      sqlite3VdbeJumpHere(v, j3);
74906      sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
74907      sqlite3VdbeJumpHere(v, j2);
74908
74909      /* Jump to the appropriate target depending on whether or not
74910      ** the RHS contains a NULL
74911      */
74912      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
74913      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
74914
74915      /* The OP_Found at the top of this branch jumps here when true,
74916      ** causing the overall IN expression evaluation to fall through.
74917      */
74918      sqlite3VdbeJumpHere(v, j1);
74919    }
74920  }
74921  sqlite3ReleaseTempReg(pParse, r1);
74922  sqlite3ExprCachePop(pParse, 1);
74923  VdbeComment((v, "end IN expr"));
74924}
74925#endif /* SQLITE_OMIT_SUBQUERY */
74926
74927/*
74928** Duplicate an 8-byte value
74929*/
74930static char *dup8bytes(Vdbe *v, const char *in){
74931  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
74932  if( out ){
74933    memcpy(out, in, 8);
74934  }
74935  return out;
74936}
74937
74938#ifndef SQLITE_OMIT_FLOATING_POINT
74939/*
74940** Generate an instruction that will put the floating point
74941** value described by z[0..n-1] into register iMem.
74942**
74943** The z[] string will probably not be zero-terminated.  But the
74944** z[n] character is guaranteed to be something that does not look
74945** like the continuation of the number.
74946*/
74947static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
74948  if( ALWAYS(z!=0) ){
74949    double value;
74950    char *zV;
74951    sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
74952    assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
74953    if( negateFlag ) value = -value;
74954    zV = dup8bytes(v, (char*)&value);
74955    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
74956  }
74957}
74958#endif
74959
74960
74961/*
74962** Generate an instruction that will put the integer describe by
74963** text z[0..n-1] into register iMem.
74964**
74965** Expr.u.zToken is always UTF8 and zero-terminated.
74966*/
74967static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
74968  Vdbe *v = pParse->pVdbe;
74969  if( pExpr->flags & EP_IntValue ){
74970    int i = pExpr->u.iValue;
74971    assert( i>=0 );
74972    if( negFlag ) i = -i;
74973    sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
74974  }else{
74975    int c;
74976    i64 value;
74977    const char *z = pExpr->u.zToken;
74978    assert( z!=0 );
74979    c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
74980    if( c==0 || (c==2 && negFlag) ){
74981      char *zV;
74982      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
74983      zV = dup8bytes(v, (char*)&value);
74984      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
74985    }else{
74986#ifdef SQLITE_OMIT_FLOATING_POINT
74987      sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
74988#else
74989      codeReal(v, z, negFlag, iMem);
74990#endif
74991    }
74992  }
74993}
74994
74995/*
74996** Clear a cache entry.
74997*/
74998static void cacheEntryClear(Parse *pParse, struct yColCache *p){
74999  if( p->tempReg ){
75000    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
75001      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
75002    }
75003    p->tempReg = 0;
75004  }
75005}
75006
75007
75008/*
75009** Record in the column cache that a particular column from a
75010** particular table is stored in a particular register.
75011*/
75012SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
75013  int i;
75014  int minLru;
75015  int idxLru;
75016  struct yColCache *p;
75017
75018  assert( iReg>0 );  /* Register numbers are always positive */
75019  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
75020
75021  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
75022  ** for testing only - to verify that SQLite always gets the same answer
75023  ** with and without the column cache.
75024  */
75025  if( pParse->db->flags & SQLITE_ColumnCache ) return;
75026
75027  /* First replace any existing entry.
75028  **
75029  ** Actually, the way the column cache is currently used, we are guaranteed
75030  ** that the object will never already be in cache.  Verify this guarantee.
75031  */
75032#ifndef NDEBUG
75033  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75034#if 0 /* This code wold remove the entry from the cache if it existed */
75035    if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
75036      cacheEntryClear(pParse, p);
75037      p->iLevel = pParse->iCacheLevel;
75038      p->iReg = iReg;
75039      p->lru = pParse->iCacheCnt++;
75040      return;
75041    }
75042#endif
75043    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
75044  }
75045#endif
75046
75047  /* Find an empty slot and replace it */
75048  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75049    if( p->iReg==0 ){
75050      p->iLevel = pParse->iCacheLevel;
75051      p->iTable = iTab;
75052      p->iColumn = iCol;
75053      p->iReg = iReg;
75054      p->tempReg = 0;
75055      p->lru = pParse->iCacheCnt++;
75056      return;
75057    }
75058  }
75059
75060  /* Replace the last recently used */
75061  minLru = 0x7fffffff;
75062  idxLru = -1;
75063  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75064    if( p->lru<minLru ){
75065      idxLru = i;
75066      minLru = p->lru;
75067    }
75068  }
75069  if( ALWAYS(idxLru>=0) ){
75070    p = &pParse->aColCache[idxLru];
75071    p->iLevel = pParse->iCacheLevel;
75072    p->iTable = iTab;
75073    p->iColumn = iCol;
75074    p->iReg = iReg;
75075    p->tempReg = 0;
75076    p->lru = pParse->iCacheCnt++;
75077    return;
75078  }
75079}
75080
75081/*
75082** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
75083** Purge the range of registers from the column cache.
75084*/
75085SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
75086  int i;
75087  int iLast = iReg + nReg - 1;
75088  struct yColCache *p;
75089  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75090    int r = p->iReg;
75091    if( r>=iReg && r<=iLast ){
75092      cacheEntryClear(pParse, p);
75093      p->iReg = 0;
75094    }
75095  }
75096}
75097
75098/*
75099** Remember the current column cache context.  Any new entries added
75100** added to the column cache after this call are removed when the
75101** corresponding pop occurs.
75102*/
75103SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
75104  pParse->iCacheLevel++;
75105}
75106
75107/*
75108** Remove from the column cache any entries that were added since the
75109** the previous N Push operations.  In other words, restore the cache
75110** to the state it was in N Pushes ago.
75111*/
75112SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
75113  int i;
75114  struct yColCache *p;
75115  assert( N>0 );
75116  assert( pParse->iCacheLevel>=N );
75117  pParse->iCacheLevel -= N;
75118  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75119    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
75120      cacheEntryClear(pParse, p);
75121      p->iReg = 0;
75122    }
75123  }
75124}
75125
75126/*
75127** When a cached column is reused, make sure that its register is
75128** no longer available as a temp register.  ticket #3879:  that same
75129** register might be in the cache in multiple places, so be sure to
75130** get them all.
75131*/
75132static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
75133  int i;
75134  struct yColCache *p;
75135  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75136    if( p->iReg==iReg ){
75137      p->tempReg = 0;
75138    }
75139  }
75140}
75141
75142/*
75143** Generate code to extract the value of the iCol-th column of a table.
75144*/
75145SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
75146  Vdbe *v,        /* The VDBE under construction */
75147  Table *pTab,    /* The table containing the value */
75148  int iTabCur,    /* The cursor for this table */
75149  int iCol,       /* Index of the column to extract */
75150  int regOut      /* Extract the valud into this register */
75151){
75152  if( iCol<0 || iCol==pTab->iPKey ){
75153    sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
75154  }else{
75155    int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
75156    sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
75157  }
75158  if( iCol>=0 ){
75159    sqlite3ColumnDefault(v, pTab, iCol, regOut);
75160  }
75161}
75162
75163/*
75164** Generate code that will extract the iColumn-th column from
75165** table pTab and store the column value in a register.  An effort
75166** is made to store the column value in register iReg, but this is
75167** not guaranteed.  The location of the column value is returned.
75168**
75169** There must be an open cursor to pTab in iTable when this routine
75170** is called.  If iColumn<0 then code is generated that extracts the rowid.
75171*/
75172SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
75173  Parse *pParse,   /* Parsing and code generating context */
75174  Table *pTab,     /* Description of the table we are reading from */
75175  int iColumn,     /* Index of the table column */
75176  int iTable,      /* The cursor pointing to the table */
75177  int iReg         /* Store results here */
75178){
75179  Vdbe *v = pParse->pVdbe;
75180  int i;
75181  struct yColCache *p;
75182
75183  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75184    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
75185      p->lru = pParse->iCacheCnt++;
75186      sqlite3ExprCachePinRegister(pParse, p->iReg);
75187      return p->iReg;
75188    }
75189  }
75190  assert( v!=0 );
75191  sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
75192  sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
75193  return iReg;
75194}
75195
75196/*
75197** Clear all column cache entries.
75198*/
75199SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
75200  int i;
75201  struct yColCache *p;
75202
75203  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75204    if( p->iReg ){
75205      cacheEntryClear(pParse, p);
75206      p->iReg = 0;
75207    }
75208  }
75209}
75210
75211/*
75212** Record the fact that an affinity change has occurred on iCount
75213** registers starting with iStart.
75214*/
75215SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
75216  sqlite3ExprCacheRemove(pParse, iStart, iCount);
75217}
75218
75219/*
75220** Generate code to move content from registers iFrom...iFrom+nReg-1
75221** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
75222*/
75223SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
75224  int i;
75225  struct yColCache *p;
75226  if( NEVER(iFrom==iTo) ) return;
75227  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
75228  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75229    int x = p->iReg;
75230    if( x>=iFrom && x<iFrom+nReg ){
75231      p->iReg += iTo-iFrom;
75232    }
75233  }
75234}
75235
75236/*
75237** Generate code to copy content from registers iFrom...iFrom+nReg-1
75238** over to iTo..iTo+nReg-1.
75239*/
75240SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
75241  int i;
75242  if( NEVER(iFrom==iTo) ) return;
75243  for(i=0; i<nReg; i++){
75244    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
75245  }
75246}
75247
75248#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
75249/*
75250** Return true if any register in the range iFrom..iTo (inclusive)
75251** is used as part of the column cache.
75252**
75253** This routine is used within assert() and testcase() macros only
75254** and does not appear in a normal build.
75255*/
75256static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
75257  int i;
75258  struct yColCache *p;
75259  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75260    int r = p->iReg;
75261    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
75262  }
75263  return 0;
75264}
75265#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
75266
75267/*
75268** Generate code into the current Vdbe to evaluate the given
75269** expression.  Attempt to store the results in register "target".
75270** Return the register where results are stored.
75271**
75272** With this routine, there is no guarantee that results will
75273** be stored in target.  The result might be stored in some other
75274** register if it is convenient to do so.  The calling function
75275** must check the return code and move the results to the desired
75276** register.
75277*/
75278SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
75279  Vdbe *v = pParse->pVdbe;  /* The VM under construction */
75280  int op;                   /* The opcode being coded */
75281  int inReg = target;       /* Results stored in register inReg */
75282  int regFree1 = 0;         /* If non-zero free this temporary register */
75283  int regFree2 = 0;         /* If non-zero free this temporary register */
75284  int r1, r2, r3, r4;       /* Various register numbers */
75285  sqlite3 *db = pParse->db; /* The database connection */
75286
75287  assert( target>0 && target<=pParse->nMem );
75288  if( v==0 ){
75289    assert( pParse->db->mallocFailed );
75290    return 0;
75291  }
75292
75293  if( pExpr==0 ){
75294    op = TK_NULL;
75295  }else{
75296    op = pExpr->op;
75297  }
75298  switch( op ){
75299    case TK_AGG_COLUMN: {
75300      AggInfo *pAggInfo = pExpr->pAggInfo;
75301      struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
75302      if( !pAggInfo->directMode ){
75303        assert( pCol->iMem>0 );
75304        inReg = pCol->iMem;
75305        break;
75306      }else if( pAggInfo->useSortingIdx ){
75307        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
75308                              pCol->iSorterColumn, target);
75309        break;
75310      }
75311      /* Otherwise, fall thru into the TK_COLUMN case */
75312    }
75313    case TK_COLUMN: {
75314      if( pExpr->iTable<0 ){
75315        /* This only happens when coding check constraints */
75316        assert( pParse->ckBase>0 );
75317        inReg = pExpr->iColumn + pParse->ckBase;
75318      }else{
75319        inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
75320                                 pExpr->iColumn, pExpr->iTable, target);
75321      }
75322      break;
75323    }
75324    case TK_INTEGER: {
75325      codeInteger(pParse, pExpr, 0, target);
75326      break;
75327    }
75328#ifndef SQLITE_OMIT_FLOATING_POINT
75329    case TK_FLOAT: {
75330      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75331      codeReal(v, pExpr->u.zToken, 0, target);
75332      break;
75333    }
75334#endif
75335    case TK_STRING: {
75336      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75337      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
75338      break;
75339    }
75340    case TK_NULL: {
75341      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
75342      break;
75343    }
75344#ifndef SQLITE_OMIT_BLOB_LITERAL
75345    case TK_BLOB: {
75346      int n;
75347      const char *z;
75348      char *zBlob;
75349      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75350      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
75351      assert( pExpr->u.zToken[1]=='\'' );
75352      z = &pExpr->u.zToken[2];
75353      n = sqlite3Strlen30(z) - 1;
75354      assert( z[n]=='\'' );
75355      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
75356      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
75357      break;
75358    }
75359#endif
75360    case TK_VARIABLE: {
75361      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75362      assert( pExpr->u.zToken!=0 );
75363      assert( pExpr->u.zToken[0]!=0 );
75364      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
75365      if( pExpr->u.zToken[1]!=0 ){
75366        assert( pExpr->u.zToken[0]=='?'
75367             || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
75368        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
75369      }
75370      break;
75371    }
75372    case TK_REGISTER: {
75373      inReg = pExpr->iTable;
75374      break;
75375    }
75376    case TK_AS: {
75377      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
75378      break;
75379    }
75380#ifndef SQLITE_OMIT_CAST
75381    case TK_CAST: {
75382      /* Expressions of the form:   CAST(pLeft AS token) */
75383      int aff, to_op;
75384      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
75385      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75386      aff = sqlite3AffinityType(pExpr->u.zToken);
75387      to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
75388      assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
75389      assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
75390      assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
75391      assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
75392      assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
75393      testcase( to_op==OP_ToText );
75394      testcase( to_op==OP_ToBlob );
75395      testcase( to_op==OP_ToNumeric );
75396      testcase( to_op==OP_ToInt );
75397      testcase( to_op==OP_ToReal );
75398      if( inReg!=target ){
75399        sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
75400        inReg = target;
75401      }
75402      sqlite3VdbeAddOp1(v, to_op, inReg);
75403      testcase( usedAsColumnCache(pParse, inReg, inReg) );
75404      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
75405      break;
75406    }
75407#endif /* SQLITE_OMIT_CAST */
75408    case TK_LT:
75409    case TK_LE:
75410    case TK_GT:
75411    case TK_GE:
75412    case TK_NE:
75413    case TK_EQ: {
75414      assert( TK_LT==OP_Lt );
75415      assert( TK_LE==OP_Le );
75416      assert( TK_GT==OP_Gt );
75417      assert( TK_GE==OP_Ge );
75418      assert( TK_EQ==OP_Eq );
75419      assert( TK_NE==OP_Ne );
75420      testcase( op==TK_LT );
75421      testcase( op==TK_LE );
75422      testcase( op==TK_GT );
75423      testcase( op==TK_GE );
75424      testcase( op==TK_EQ );
75425      testcase( op==TK_NE );
75426      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
75427      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
75428      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
75429                  r1, r2, inReg, SQLITE_STOREP2);
75430      testcase( regFree1==0 );
75431      testcase( regFree2==0 );
75432      break;
75433    }
75434    case TK_IS:
75435    case TK_ISNOT: {
75436      testcase( op==TK_IS );
75437      testcase( op==TK_ISNOT );
75438      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
75439      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
75440      op = (op==TK_IS) ? TK_EQ : TK_NE;
75441      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
75442                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
75443      testcase( regFree1==0 );
75444      testcase( regFree2==0 );
75445      break;
75446    }
75447    case TK_AND:
75448    case TK_OR:
75449    case TK_PLUS:
75450    case TK_STAR:
75451    case TK_MINUS:
75452    case TK_REM:
75453    case TK_BITAND:
75454    case TK_BITOR:
75455    case TK_SLASH:
75456    case TK_LSHIFT:
75457    case TK_RSHIFT:
75458    case TK_CONCAT: {
75459      assert( TK_AND==OP_And );
75460      assert( TK_OR==OP_Or );
75461      assert( TK_PLUS==OP_Add );
75462      assert( TK_MINUS==OP_Subtract );
75463      assert( TK_REM==OP_Remainder );
75464      assert( TK_BITAND==OP_BitAnd );
75465      assert( TK_BITOR==OP_BitOr );
75466      assert( TK_SLASH==OP_Divide );
75467      assert( TK_LSHIFT==OP_ShiftLeft );
75468      assert( TK_RSHIFT==OP_ShiftRight );
75469      assert( TK_CONCAT==OP_Concat );
75470      testcase( op==TK_AND );
75471      testcase( op==TK_OR );
75472      testcase( op==TK_PLUS );
75473      testcase( op==TK_MINUS );
75474      testcase( op==TK_REM );
75475      testcase( op==TK_BITAND );
75476      testcase( op==TK_BITOR );
75477      testcase( op==TK_SLASH );
75478      testcase( op==TK_LSHIFT );
75479      testcase( op==TK_RSHIFT );
75480      testcase( op==TK_CONCAT );
75481      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
75482      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
75483      sqlite3VdbeAddOp3(v, op, r2, r1, target);
75484      testcase( regFree1==0 );
75485      testcase( regFree2==0 );
75486      break;
75487    }
75488    case TK_UMINUS: {
75489      Expr *pLeft = pExpr->pLeft;
75490      assert( pLeft );
75491      if( pLeft->op==TK_INTEGER ){
75492        codeInteger(pParse, pLeft, 1, target);
75493#ifndef SQLITE_OMIT_FLOATING_POINT
75494      }else if( pLeft->op==TK_FLOAT ){
75495        assert( !ExprHasProperty(pExpr, EP_IntValue) );
75496        codeReal(v, pLeft->u.zToken, 1, target);
75497#endif
75498      }else{
75499        regFree1 = r1 = sqlite3GetTempReg(pParse);
75500        sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
75501        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
75502        sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
75503        testcase( regFree2==0 );
75504      }
75505      inReg = target;
75506      break;
75507    }
75508    case TK_BITNOT:
75509    case TK_NOT: {
75510      assert( TK_BITNOT==OP_BitNot );
75511      assert( TK_NOT==OP_Not );
75512      testcase( op==TK_BITNOT );
75513      testcase( op==TK_NOT );
75514      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
75515      testcase( regFree1==0 );
75516      inReg = target;
75517      sqlite3VdbeAddOp2(v, op, r1, inReg);
75518      break;
75519    }
75520    case TK_ISNULL:
75521    case TK_NOTNULL: {
75522      int addr;
75523      assert( TK_ISNULL==OP_IsNull );
75524      assert( TK_NOTNULL==OP_NotNull );
75525      testcase( op==TK_ISNULL );
75526      testcase( op==TK_NOTNULL );
75527      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
75528      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
75529      testcase( regFree1==0 );
75530      addr = sqlite3VdbeAddOp1(v, op, r1);
75531      sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
75532      sqlite3VdbeJumpHere(v, addr);
75533      break;
75534    }
75535    case TK_AGG_FUNCTION: {
75536      AggInfo *pInfo = pExpr->pAggInfo;
75537      if( pInfo==0 ){
75538        assert( !ExprHasProperty(pExpr, EP_IntValue) );
75539        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
75540      }else{
75541        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
75542      }
75543      break;
75544    }
75545    case TK_CONST_FUNC:
75546    case TK_FUNCTION: {
75547      ExprList *pFarg;       /* List of function arguments */
75548      int nFarg;             /* Number of function arguments */
75549      FuncDef *pDef;         /* The function definition object */
75550      int nId;               /* Length of the function name in bytes */
75551      const char *zId;       /* The function name */
75552      int constMask = 0;     /* Mask of function arguments that are constant */
75553      int i;                 /* Loop counter */
75554      u8 enc = ENC(db);      /* The text encoding used by this database */
75555      CollSeq *pColl = 0;    /* A collating sequence */
75556
75557      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
75558      testcase( op==TK_CONST_FUNC );
75559      testcase( op==TK_FUNCTION );
75560      if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
75561        pFarg = 0;
75562      }else{
75563        pFarg = pExpr->x.pList;
75564      }
75565      nFarg = pFarg ? pFarg->nExpr : 0;
75566      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75567      zId = pExpr->u.zToken;
75568      nId = sqlite3Strlen30(zId);
75569      pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
75570      if( pDef==0 ){
75571        sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
75572        break;
75573      }
75574
75575      /* Attempt a direct implementation of the built-in COALESCE() and
75576      ** IFNULL() functions.  This avoids unnecessary evalation of
75577      ** arguments past the first non-NULL argument.
75578      */
75579      if( pDef->flags & SQLITE_FUNC_COALESCE ){
75580        int endCoalesce = sqlite3VdbeMakeLabel(v);
75581        assert( nFarg>=2 );
75582        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
75583        for(i=1; i<nFarg; i++){
75584          sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
75585          sqlite3ExprCacheRemove(pParse, target, 1);
75586          sqlite3ExprCachePush(pParse);
75587          sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
75588          sqlite3ExprCachePop(pParse, 1);
75589        }
75590        sqlite3VdbeResolveLabel(v, endCoalesce);
75591        break;
75592      }
75593
75594
75595      if( pFarg ){
75596        r1 = sqlite3GetTempRange(pParse, nFarg);
75597        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
75598        sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
75599        sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
75600      }else{
75601        r1 = 0;
75602      }
75603#ifndef SQLITE_OMIT_VIRTUALTABLE
75604      /* Possibly overload the function if the first argument is
75605      ** a virtual table column.
75606      **
75607      ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
75608      ** second argument, not the first, as the argument to test to
75609      ** see if it is a column in a virtual table.  This is done because
75610      ** the left operand of infix functions (the operand we want to
75611      ** control overloading) ends up as the second argument to the
75612      ** function.  The expression "A glob B" is equivalent to
75613      ** "glob(B,A).  We want to use the A in "A glob B" to test
75614      ** for function overloading.  But we use the B term in "glob(B,A)".
75615      */
75616      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
75617        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
75618      }else if( nFarg>0 ){
75619        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
75620      }
75621#endif
75622      for(i=0; i<nFarg; i++){
75623        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
75624          constMask |= (1<<i);
75625        }
75626        if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
75627          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
75628        }
75629      }
75630      if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
75631        if( !pColl ) pColl = db->pDfltColl;
75632        sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
75633      }
75634      sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
75635                        (char*)pDef, P4_FUNCDEF);
75636      sqlite3VdbeChangeP5(v, (u8)nFarg);
75637      if( nFarg ){
75638        sqlite3ReleaseTempRange(pParse, r1, nFarg);
75639      }
75640      break;
75641    }
75642#ifndef SQLITE_OMIT_SUBQUERY
75643    case TK_EXISTS:
75644    case TK_SELECT: {
75645      testcase( op==TK_EXISTS );
75646      testcase( op==TK_SELECT );
75647      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
75648      break;
75649    }
75650    case TK_IN: {
75651      int destIfFalse = sqlite3VdbeMakeLabel(v);
75652      int destIfNull = sqlite3VdbeMakeLabel(v);
75653      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
75654      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
75655      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
75656      sqlite3VdbeResolveLabel(v, destIfFalse);
75657      sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
75658      sqlite3VdbeResolveLabel(v, destIfNull);
75659      break;
75660    }
75661#endif /* SQLITE_OMIT_SUBQUERY */
75662
75663
75664    /*
75665    **    x BETWEEN y AND z
75666    **
75667    ** This is equivalent to
75668    **
75669    **    x>=y AND x<=z
75670    **
75671    ** X is stored in pExpr->pLeft.
75672    ** Y is stored in pExpr->pList->a[0].pExpr.
75673    ** Z is stored in pExpr->pList->a[1].pExpr.
75674    */
75675    case TK_BETWEEN: {
75676      Expr *pLeft = pExpr->pLeft;
75677      struct ExprList_item *pLItem = pExpr->x.pList->a;
75678      Expr *pRight = pLItem->pExpr;
75679
75680      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
75681      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
75682      testcase( regFree1==0 );
75683      testcase( regFree2==0 );
75684      r3 = sqlite3GetTempReg(pParse);
75685      r4 = sqlite3GetTempReg(pParse);
75686      codeCompare(pParse, pLeft, pRight, OP_Ge,
75687                  r1, r2, r3, SQLITE_STOREP2);
75688      pLItem++;
75689      pRight = pLItem->pExpr;
75690      sqlite3ReleaseTempReg(pParse, regFree2);
75691      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
75692      testcase( regFree2==0 );
75693      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
75694      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
75695      sqlite3ReleaseTempReg(pParse, r3);
75696      sqlite3ReleaseTempReg(pParse, r4);
75697      break;
75698    }
75699    case TK_UPLUS: {
75700      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
75701      break;
75702    }
75703
75704    case TK_TRIGGER: {
75705      /* If the opcode is TK_TRIGGER, then the expression is a reference
75706      ** to a column in the new.* or old.* pseudo-tables available to
75707      ** trigger programs. In this case Expr.iTable is set to 1 for the
75708      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
75709      ** is set to the column of the pseudo-table to read, or to -1 to
75710      ** read the rowid field.
75711      **
75712      ** The expression is implemented using an OP_Param opcode. The p1
75713      ** parameter is set to 0 for an old.rowid reference, or to (i+1)
75714      ** to reference another column of the old.* pseudo-table, where
75715      ** i is the index of the column. For a new.rowid reference, p1 is
75716      ** set to (n+1), where n is the number of columns in each pseudo-table.
75717      ** For a reference to any other column in the new.* pseudo-table, p1
75718      ** is set to (n+2+i), where n and i are as defined previously. For
75719      ** example, if the table on which triggers are being fired is
75720      ** declared as:
75721      **
75722      **   CREATE TABLE t1(a, b);
75723      **
75724      ** Then p1 is interpreted as follows:
75725      **
75726      **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
75727      **   p1==1   ->    old.a         p1==4   ->    new.a
75728      **   p1==2   ->    old.b         p1==5   ->    new.b
75729      */
75730      Table *pTab = pExpr->pTab;
75731      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
75732
75733      assert( pExpr->iTable==0 || pExpr->iTable==1 );
75734      assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
75735      assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
75736      assert( p1>=0 && p1<(pTab->nCol*2+2) );
75737
75738      sqlite3VdbeAddOp2(v, OP_Param, p1, target);
75739      VdbeComment((v, "%s.%s -> $%d",
75740        (pExpr->iTable ? "new" : "old"),
75741        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
75742        target
75743      ));
75744
75745#ifndef SQLITE_OMIT_FLOATING_POINT
75746      /* If the column has REAL affinity, it may currently be stored as an
75747      ** integer. Use OP_RealAffinity to make sure it is really real.  */
75748      if( pExpr->iColumn>=0
75749       && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
75750      ){
75751        sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
75752      }
75753#endif
75754      break;
75755    }
75756
75757
75758    /*
75759    ** Form A:
75760    **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
75761    **
75762    ** Form B:
75763    **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
75764    **
75765    ** Form A is can be transformed into the equivalent form B as follows:
75766    **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
75767    **        WHEN x=eN THEN rN ELSE y END
75768    **
75769    ** X (if it exists) is in pExpr->pLeft.
75770    ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
75771    ** ELSE clause and no other term matches, then the result of the
75772    ** exprssion is NULL.
75773    ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
75774    **
75775    ** The result of the expression is the Ri for the first matching Ei,
75776    ** or if there is no matching Ei, the ELSE term Y, or if there is
75777    ** no ELSE term, NULL.
75778    */
75779    default: assert( op==TK_CASE ); {
75780      int endLabel;                     /* GOTO label for end of CASE stmt */
75781      int nextCase;                     /* GOTO label for next WHEN clause */
75782      int nExpr;                        /* 2x number of WHEN terms */
75783      int i;                            /* Loop counter */
75784      ExprList *pEList;                 /* List of WHEN terms */
75785      struct ExprList_item *aListelem;  /* Array of WHEN terms */
75786      Expr opCompare;                   /* The X==Ei expression */
75787      Expr cacheX;                      /* Cached expression X */
75788      Expr *pX;                         /* The X expression */
75789      Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
75790      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
75791
75792      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
75793      assert((pExpr->x.pList->nExpr % 2) == 0);
75794      assert(pExpr->x.pList->nExpr > 0);
75795      pEList = pExpr->x.pList;
75796      aListelem = pEList->a;
75797      nExpr = pEList->nExpr;
75798      endLabel = sqlite3VdbeMakeLabel(v);
75799      if( (pX = pExpr->pLeft)!=0 ){
75800        cacheX = *pX;
75801        testcase( pX->op==TK_COLUMN );
75802        testcase( pX->op==TK_REGISTER );
75803        cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
75804        testcase( regFree1==0 );
75805        cacheX.op = TK_REGISTER;
75806        opCompare.op = TK_EQ;
75807        opCompare.pLeft = &cacheX;
75808        pTest = &opCompare;
75809        /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
75810        ** The value in regFree1 might get SCopy-ed into the file result.
75811        ** So make sure that the regFree1 register is not reused for other
75812        ** purposes and possibly overwritten.  */
75813        regFree1 = 0;
75814      }
75815      for(i=0; i<nExpr; i=i+2){
75816        sqlite3ExprCachePush(pParse);
75817        if( pX ){
75818          assert( pTest!=0 );
75819          opCompare.pRight = aListelem[i].pExpr;
75820        }else{
75821          pTest = aListelem[i].pExpr;
75822        }
75823        nextCase = sqlite3VdbeMakeLabel(v);
75824        testcase( pTest->op==TK_COLUMN );
75825        sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
75826        testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
75827        testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
75828        sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
75829        sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
75830        sqlite3ExprCachePop(pParse, 1);
75831        sqlite3VdbeResolveLabel(v, nextCase);
75832      }
75833      if( pExpr->pRight ){
75834        sqlite3ExprCachePush(pParse);
75835        sqlite3ExprCode(pParse, pExpr->pRight, target);
75836        sqlite3ExprCachePop(pParse, 1);
75837      }else{
75838        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
75839      }
75840      assert( db->mallocFailed || pParse->nErr>0
75841           || pParse->iCacheLevel==iCacheLevel );
75842      sqlite3VdbeResolveLabel(v, endLabel);
75843      break;
75844    }
75845#ifndef SQLITE_OMIT_TRIGGER
75846    case TK_RAISE: {
75847      assert( pExpr->affinity==OE_Rollback
75848           || pExpr->affinity==OE_Abort
75849           || pExpr->affinity==OE_Fail
75850           || pExpr->affinity==OE_Ignore
75851      );
75852      if( !pParse->pTriggerTab ){
75853        sqlite3ErrorMsg(pParse,
75854                       "RAISE() may only be used within a trigger-program");
75855        return 0;
75856      }
75857      if( pExpr->affinity==OE_Abort ){
75858        sqlite3MayAbort(pParse);
75859      }
75860      assert( !ExprHasProperty(pExpr, EP_IntValue) );
75861      if( pExpr->affinity==OE_Ignore ){
75862        sqlite3VdbeAddOp4(
75863            v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
75864      }else{
75865        sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
75866      }
75867
75868      break;
75869    }
75870#endif
75871  }
75872  sqlite3ReleaseTempReg(pParse, regFree1);
75873  sqlite3ReleaseTempReg(pParse, regFree2);
75874  return inReg;
75875}
75876
75877/*
75878** Generate code to evaluate an expression and store the results
75879** into a register.  Return the register number where the results
75880** are stored.
75881**
75882** If the register is a temporary register that can be deallocated,
75883** then write its number into *pReg.  If the result register is not
75884** a temporary, then set *pReg to zero.
75885*/
75886SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
75887  int r1 = sqlite3GetTempReg(pParse);
75888  int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
75889  if( r2==r1 ){
75890    *pReg = r1;
75891  }else{
75892    sqlite3ReleaseTempReg(pParse, r1);
75893    *pReg = 0;
75894  }
75895  return r2;
75896}
75897
75898/*
75899** Generate code that will evaluate expression pExpr and store the
75900** results in register target.  The results are guaranteed to appear
75901** in register target.
75902*/
75903SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
75904  int inReg;
75905
75906  assert( target>0 && target<=pParse->nMem );
75907  if( pExpr && pExpr->op==TK_REGISTER ){
75908    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
75909  }else{
75910    inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
75911    assert( pParse->pVdbe || pParse->db->mallocFailed );
75912    if( inReg!=target && pParse->pVdbe ){
75913      sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
75914    }
75915  }
75916  return target;
75917}
75918
75919/*
75920** Generate code that evalutes the given expression and puts the result
75921** in register target.
75922**
75923** Also make a copy of the expression results into another "cache" register
75924** and modify the expression so that the next time it is evaluated,
75925** the result is a copy of the cache register.
75926**
75927** This routine is used for expressions that are used multiple
75928** times.  They are evaluated once and the results of the expression
75929** are reused.
75930*/
75931SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
75932  Vdbe *v = pParse->pVdbe;
75933  int inReg;
75934  inReg = sqlite3ExprCode(pParse, pExpr, target);
75935  assert( target>0 );
75936  /* This routine is called for terms to INSERT or UPDATE.  And the only
75937  ** other place where expressions can be converted into TK_REGISTER is
75938  ** in WHERE clause processing.  So as currently implemented, there is
75939  ** no way for a TK_REGISTER to exist here.  But it seems prudent to
75940  ** keep the ALWAYS() in case the conditions above change with future
75941  ** modifications or enhancements. */
75942  if( ALWAYS(pExpr->op!=TK_REGISTER) ){
75943    int iMem;
75944    iMem = ++pParse->nMem;
75945    sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
75946    pExpr->iTable = iMem;
75947    pExpr->op2 = pExpr->op;
75948    pExpr->op = TK_REGISTER;
75949  }
75950  return inReg;
75951}
75952
75953/*
75954** Return TRUE if pExpr is an constant expression that is appropriate
75955** for factoring out of a loop.  Appropriate expressions are:
75956**
75957**    *  Any expression that evaluates to two or more opcodes.
75958**
75959**    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
75960**       or OP_Variable that does not need to be placed in a
75961**       specific register.
75962**
75963** There is no point in factoring out single-instruction constant
75964** expressions that need to be placed in a particular register.
75965** We could factor them out, but then we would end up adding an
75966** OP_SCopy instruction to move the value into the correct register
75967** later.  We might as well just use the original instruction and
75968** avoid the OP_SCopy.
75969*/
75970static int isAppropriateForFactoring(Expr *p){
75971  if( !sqlite3ExprIsConstantNotJoin(p) ){
75972    return 0;  /* Only constant expressions are appropriate for factoring */
75973  }
75974  if( (p->flags & EP_FixedDest)==0 ){
75975    return 1;  /* Any constant without a fixed destination is appropriate */
75976  }
75977  while( p->op==TK_UPLUS ) p = p->pLeft;
75978  switch( p->op ){
75979#ifndef SQLITE_OMIT_BLOB_LITERAL
75980    case TK_BLOB:
75981#endif
75982    case TK_VARIABLE:
75983    case TK_INTEGER:
75984    case TK_FLOAT:
75985    case TK_NULL:
75986    case TK_STRING: {
75987      testcase( p->op==TK_BLOB );
75988      testcase( p->op==TK_VARIABLE );
75989      testcase( p->op==TK_INTEGER );
75990      testcase( p->op==TK_FLOAT );
75991      testcase( p->op==TK_NULL );
75992      testcase( p->op==TK_STRING );
75993      /* Single-instruction constants with a fixed destination are
75994      ** better done in-line.  If we factor them, they will just end
75995      ** up generating an OP_SCopy to move the value to the destination
75996      ** register. */
75997      return 0;
75998    }
75999    case TK_UMINUS: {
76000      if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
76001        return 0;
76002      }
76003      break;
76004    }
76005    default: {
76006      break;
76007    }
76008  }
76009  return 1;
76010}
76011
76012/*
76013** If pExpr is a constant expression that is appropriate for
76014** factoring out of a loop, then evaluate the expression
76015** into a register and convert the expression into a TK_REGISTER
76016** expression.
76017*/
76018static int evalConstExpr(Walker *pWalker, Expr *pExpr){
76019  Parse *pParse = pWalker->pParse;
76020  switch( pExpr->op ){
76021    case TK_IN:
76022    case TK_REGISTER: {
76023      return WRC_Prune;
76024    }
76025    case TK_FUNCTION:
76026    case TK_AGG_FUNCTION:
76027    case TK_CONST_FUNC: {
76028      /* The arguments to a function have a fixed destination.
76029      ** Mark them this way to avoid generated unneeded OP_SCopy
76030      ** instructions.
76031      */
76032      ExprList *pList = pExpr->x.pList;
76033      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
76034      if( pList ){
76035        int i = pList->nExpr;
76036        struct ExprList_item *pItem = pList->a;
76037        for(; i>0; i--, pItem++){
76038          if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
76039        }
76040      }
76041      break;
76042    }
76043  }
76044  if( isAppropriateForFactoring(pExpr) ){
76045    int r1 = ++pParse->nMem;
76046    int r2;
76047    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
76048    if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
76049    pExpr->op2 = pExpr->op;
76050    pExpr->op = TK_REGISTER;
76051    pExpr->iTable = r2;
76052    return WRC_Prune;
76053  }
76054  return WRC_Continue;
76055}
76056
76057/*
76058** Preevaluate constant subexpressions within pExpr and store the
76059** results in registers.  Modify pExpr so that the constant subexpresions
76060** are TK_REGISTER opcodes that refer to the precomputed values.
76061**
76062** This routine is a no-op if the jump to the cookie-check code has
76063** already occur.  Since the cookie-check jump is generated prior to
76064** any other serious processing, this check ensures that there is no
76065** way to accidently bypass the constant initializations.
76066**
76067** This routine is also a no-op if the SQLITE_FactorOutConst optimization
76068** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
76069** interface.  This allows test logic to verify that the same answer is
76070** obtained for queries regardless of whether or not constants are
76071** precomputed into registers or if they are inserted in-line.
76072*/
76073SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
76074  Walker w;
76075  if( pParse->cookieGoto ) return;
76076  if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
76077  w.xExprCallback = evalConstExpr;
76078  w.xSelectCallback = 0;
76079  w.pParse = pParse;
76080  sqlite3WalkExpr(&w, pExpr);
76081}
76082
76083
76084/*
76085** Generate code that pushes the value of every element of the given
76086** expression list into a sequence of registers beginning at target.
76087**
76088** Return the number of elements evaluated.
76089*/
76090SQLITE_PRIVATE int sqlite3ExprCodeExprList(
76091  Parse *pParse,     /* Parsing context */
76092  ExprList *pList,   /* The expression list to be coded */
76093  int target,        /* Where to write results */
76094  int doHardCopy     /* Make a hard copy of every element */
76095){
76096  struct ExprList_item *pItem;
76097  int i, n;
76098  assert( pList!=0 );
76099  assert( target>0 );
76100  assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
76101  n = pList->nExpr;
76102  for(pItem=pList->a, i=0; i<n; i++, pItem++){
76103    Expr *pExpr = pItem->pExpr;
76104    int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
76105    if( inReg!=target+i ){
76106      sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
76107                        inReg, target+i);
76108    }
76109  }
76110  return n;
76111}
76112
76113/*
76114** Generate code for a BETWEEN operator.
76115**
76116**    x BETWEEN y AND z
76117**
76118** The above is equivalent to
76119**
76120**    x>=y AND x<=z
76121**
76122** Code it as such, taking care to do the common subexpression
76123** elementation of x.
76124*/
76125static void exprCodeBetween(
76126  Parse *pParse,    /* Parsing and code generating context */
76127  Expr *pExpr,      /* The BETWEEN expression */
76128  int dest,         /* Jump here if the jump is taken */
76129  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
76130  int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
76131){
76132  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
76133  Expr compLeft;    /* The  x>=y  term */
76134  Expr compRight;   /* The  x<=z  term */
76135  Expr exprX;       /* The  x  subexpression */
76136  int regFree1 = 0; /* Temporary use register */
76137
76138  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
76139  exprX = *pExpr->pLeft;
76140  exprAnd.op = TK_AND;
76141  exprAnd.pLeft = &compLeft;
76142  exprAnd.pRight = &compRight;
76143  compLeft.op = TK_GE;
76144  compLeft.pLeft = &exprX;
76145  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
76146  compRight.op = TK_LE;
76147  compRight.pLeft = &exprX;
76148  compRight.pRight = pExpr->x.pList->a[1].pExpr;
76149  exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
76150  exprX.op = TK_REGISTER;
76151  if( jumpIfTrue ){
76152    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
76153  }else{
76154    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
76155  }
76156  sqlite3ReleaseTempReg(pParse, regFree1);
76157
76158  /* Ensure adequate test coverage */
76159  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
76160  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
76161  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
76162  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
76163  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
76164  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
76165  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
76166  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
76167}
76168
76169/*
76170** Generate code for a boolean expression such that a jump is made
76171** to the label "dest" if the expression is true but execution
76172** continues straight thru if the expression is false.
76173**
76174** If the expression evaluates to NULL (neither true nor false), then
76175** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
76176**
76177** This code depends on the fact that certain token values (ex: TK_EQ)
76178** are the same as opcode values (ex: OP_Eq) that implement the corresponding
76179** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
76180** the make process cause these values to align.  Assert()s in the code
76181** below verify that the numbers are aligned correctly.
76182*/
76183SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
76184  Vdbe *v = pParse->pVdbe;
76185  int op = 0;
76186  int regFree1 = 0;
76187  int regFree2 = 0;
76188  int r1, r2;
76189
76190  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
76191  if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
76192  if( NEVER(pExpr==0) ) return;  /* No way this can happen */
76193  op = pExpr->op;
76194  switch( op ){
76195    case TK_AND: {
76196      int d2 = sqlite3VdbeMakeLabel(v);
76197      testcase( jumpIfNull==0 );
76198      sqlite3ExprCachePush(pParse);
76199      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
76200      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
76201      sqlite3VdbeResolveLabel(v, d2);
76202      sqlite3ExprCachePop(pParse, 1);
76203      break;
76204    }
76205    case TK_OR: {
76206      testcase( jumpIfNull==0 );
76207      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
76208      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
76209      break;
76210    }
76211    case TK_NOT: {
76212      testcase( jumpIfNull==0 );
76213      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
76214      break;
76215    }
76216    case TK_LT:
76217    case TK_LE:
76218    case TK_GT:
76219    case TK_GE:
76220    case TK_NE:
76221    case TK_EQ: {
76222      assert( TK_LT==OP_Lt );
76223      assert( TK_LE==OP_Le );
76224      assert( TK_GT==OP_Gt );
76225      assert( TK_GE==OP_Ge );
76226      assert( TK_EQ==OP_Eq );
76227      assert( TK_NE==OP_Ne );
76228      testcase( op==TK_LT );
76229      testcase( op==TK_LE );
76230      testcase( op==TK_GT );
76231      testcase( op==TK_GE );
76232      testcase( op==TK_EQ );
76233      testcase( op==TK_NE );
76234      testcase( jumpIfNull==0 );
76235      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76236      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
76237      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
76238                  r1, r2, dest, jumpIfNull);
76239      testcase( regFree1==0 );
76240      testcase( regFree2==0 );
76241      break;
76242    }
76243    case TK_IS:
76244    case TK_ISNOT: {
76245      testcase( op==TK_IS );
76246      testcase( op==TK_ISNOT );
76247      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76248      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
76249      op = (op==TK_IS) ? TK_EQ : TK_NE;
76250      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
76251                  r1, r2, dest, SQLITE_NULLEQ);
76252      testcase( regFree1==0 );
76253      testcase( regFree2==0 );
76254      break;
76255    }
76256    case TK_ISNULL:
76257    case TK_NOTNULL: {
76258      assert( TK_ISNULL==OP_IsNull );
76259      assert( TK_NOTNULL==OP_NotNull );
76260      testcase( op==TK_ISNULL );
76261      testcase( op==TK_NOTNULL );
76262      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76263      sqlite3VdbeAddOp2(v, op, r1, dest);
76264      testcase( regFree1==0 );
76265      break;
76266    }
76267    case TK_BETWEEN: {
76268      testcase( jumpIfNull==0 );
76269      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
76270      break;
76271    }
76272#ifndef SQLITE_OMIT_SUBQUERY
76273    case TK_IN: {
76274      int destIfFalse = sqlite3VdbeMakeLabel(v);
76275      int destIfNull = jumpIfNull ? dest : destIfFalse;
76276      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
76277      sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
76278      sqlite3VdbeResolveLabel(v, destIfFalse);
76279      break;
76280    }
76281#endif
76282    default: {
76283      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
76284      sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
76285      testcase( regFree1==0 );
76286      testcase( jumpIfNull==0 );
76287      break;
76288    }
76289  }
76290  sqlite3ReleaseTempReg(pParse, regFree1);
76291  sqlite3ReleaseTempReg(pParse, regFree2);
76292}
76293
76294/*
76295** Generate code for a boolean expression such that a jump is made
76296** to the label "dest" if the expression is false but execution
76297** continues straight thru if the expression is true.
76298**
76299** If the expression evaluates to NULL (neither true nor false) then
76300** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
76301** is 0.
76302*/
76303SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
76304  Vdbe *v = pParse->pVdbe;
76305  int op = 0;
76306  int regFree1 = 0;
76307  int regFree2 = 0;
76308  int r1, r2;
76309
76310  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
76311  if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
76312  if( pExpr==0 )    return;
76313
76314  /* The value of pExpr->op and op are related as follows:
76315  **
76316  **       pExpr->op            op
76317  **       ---------          ----------
76318  **       TK_ISNULL          OP_NotNull
76319  **       TK_NOTNULL         OP_IsNull
76320  **       TK_NE              OP_Eq
76321  **       TK_EQ              OP_Ne
76322  **       TK_GT              OP_Le
76323  **       TK_LE              OP_Gt
76324  **       TK_GE              OP_Lt
76325  **       TK_LT              OP_Ge
76326  **
76327  ** For other values of pExpr->op, op is undefined and unused.
76328  ** The value of TK_ and OP_ constants are arranged such that we
76329  ** can compute the mapping above using the following expression.
76330  ** Assert()s verify that the computation is correct.
76331  */
76332  op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
76333
76334  /* Verify correct alignment of TK_ and OP_ constants
76335  */
76336  assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
76337  assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
76338  assert( pExpr->op!=TK_NE || op==OP_Eq );
76339  assert( pExpr->op!=TK_EQ || op==OP_Ne );
76340  assert( pExpr->op!=TK_LT || op==OP_Ge );
76341  assert( pExpr->op!=TK_LE || op==OP_Gt );
76342  assert( pExpr->op!=TK_GT || op==OP_Le );
76343  assert( pExpr->op!=TK_GE || op==OP_Lt );
76344
76345  switch( pExpr->op ){
76346    case TK_AND: {
76347      testcase( jumpIfNull==0 );
76348      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
76349      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
76350      break;
76351    }
76352    case TK_OR: {
76353      int d2 = sqlite3VdbeMakeLabel(v);
76354      testcase( jumpIfNull==0 );
76355      sqlite3ExprCachePush(pParse);
76356      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
76357      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
76358      sqlite3VdbeResolveLabel(v, d2);
76359      sqlite3ExprCachePop(pParse, 1);
76360      break;
76361    }
76362    case TK_NOT: {
76363      testcase( jumpIfNull==0 );
76364      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
76365      break;
76366    }
76367    case TK_LT:
76368    case TK_LE:
76369    case TK_GT:
76370    case TK_GE:
76371    case TK_NE:
76372    case TK_EQ: {
76373      testcase( op==TK_LT );
76374      testcase( op==TK_LE );
76375      testcase( op==TK_GT );
76376      testcase( op==TK_GE );
76377      testcase( op==TK_EQ );
76378      testcase( op==TK_NE );
76379      testcase( jumpIfNull==0 );
76380      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76381      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
76382      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
76383                  r1, r2, dest, jumpIfNull);
76384      testcase( regFree1==0 );
76385      testcase( regFree2==0 );
76386      break;
76387    }
76388    case TK_IS:
76389    case TK_ISNOT: {
76390      testcase( pExpr->op==TK_IS );
76391      testcase( pExpr->op==TK_ISNOT );
76392      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76393      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
76394      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
76395      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
76396                  r1, r2, dest, SQLITE_NULLEQ);
76397      testcase( regFree1==0 );
76398      testcase( regFree2==0 );
76399      break;
76400    }
76401    case TK_ISNULL:
76402    case TK_NOTNULL: {
76403      testcase( op==TK_ISNULL );
76404      testcase( op==TK_NOTNULL );
76405      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
76406      sqlite3VdbeAddOp2(v, op, r1, dest);
76407      testcase( regFree1==0 );
76408      break;
76409    }
76410    case TK_BETWEEN: {
76411      testcase( jumpIfNull==0 );
76412      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
76413      break;
76414    }
76415#ifndef SQLITE_OMIT_SUBQUERY
76416    case TK_IN: {
76417      if( jumpIfNull ){
76418        sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
76419      }else{
76420        int destIfNull = sqlite3VdbeMakeLabel(v);
76421        sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
76422        sqlite3VdbeResolveLabel(v, destIfNull);
76423      }
76424      break;
76425    }
76426#endif
76427    default: {
76428      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
76429      sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
76430      testcase( regFree1==0 );
76431      testcase( jumpIfNull==0 );
76432      break;
76433    }
76434  }
76435  sqlite3ReleaseTempReg(pParse, regFree1);
76436  sqlite3ReleaseTempReg(pParse, regFree2);
76437}
76438
76439/*
76440** Do a deep comparison of two expression trees.  Return 0 if the two
76441** expressions are completely identical.  Return 1 if they differ only
76442** by a COLLATE operator at the top level.  Return 2 if there are differences
76443** other than the top-level COLLATE operator.
76444**
76445** Sometimes this routine will return 2 even if the two expressions
76446** really are equivalent.  If we cannot prove that the expressions are
76447** identical, we return 2 just to be safe.  So if this routine
76448** returns 2, then you do not really know for certain if the two
76449** expressions are the same.  But if you get a 0 or 1 return, then you
76450** can be sure the expressions are the same.  In the places where
76451** this routine is used, it does not hurt to get an extra 2 - that
76452** just might result in some slightly slower code.  But returning
76453** an incorrect 0 or 1 could lead to a malfunction.
76454*/
76455SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
76456  if( pA==0||pB==0 ){
76457    return pB==pA ? 0 : 2;
76458  }
76459  assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
76460  assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
76461  if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
76462    return 2;
76463  }
76464  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
76465  if( pA->op!=pB->op ) return 2;
76466  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
76467  if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
76468  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
76469  if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
76470  if( ExprHasProperty(pA, EP_IntValue) ){
76471    if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
76472      return 2;
76473    }
76474  }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
76475    if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
76476    if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
76477      return 2;
76478    }
76479  }
76480  if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
76481  if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
76482  return 0;
76483}
76484
76485/*
76486** Compare two ExprList objects.  Return 0 if they are identical and
76487** non-zero if they differ in any way.
76488**
76489** This routine might return non-zero for equivalent ExprLists.  The
76490** only consequence will be disabled optimizations.  But this routine
76491** must never return 0 if the two ExprList objects are different, or
76492** a malfunction will result.
76493**
76494** Two NULL pointers are considered to be the same.  But a NULL pointer
76495** always differs from a non-NULL pointer.
76496*/
76497SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
76498  int i;
76499  if( pA==0 && pB==0 ) return 0;
76500  if( pA==0 || pB==0 ) return 1;
76501  if( pA->nExpr!=pB->nExpr ) return 1;
76502  for(i=0; i<pA->nExpr; i++){
76503    Expr *pExprA = pA->a[i].pExpr;
76504    Expr *pExprB = pB->a[i].pExpr;
76505    if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
76506    if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
76507  }
76508  return 0;
76509}
76510
76511/*
76512** Add a new element to the pAggInfo->aCol[] array.  Return the index of
76513** the new element.  Return a negative number if malloc fails.
76514*/
76515static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
76516  int i;
76517  pInfo->aCol = sqlite3ArrayAllocate(
76518       db,
76519       pInfo->aCol,
76520       sizeof(pInfo->aCol[0]),
76521       3,
76522       &pInfo->nColumn,
76523       &pInfo->nColumnAlloc,
76524       &i
76525  );
76526  return i;
76527}
76528
76529/*
76530** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
76531** the new element.  Return a negative number if malloc fails.
76532*/
76533static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
76534  int i;
76535  pInfo->aFunc = sqlite3ArrayAllocate(
76536       db,
76537       pInfo->aFunc,
76538       sizeof(pInfo->aFunc[0]),
76539       3,
76540       &pInfo->nFunc,
76541       &pInfo->nFuncAlloc,
76542       &i
76543  );
76544  return i;
76545}
76546
76547/*
76548** This is the xExprCallback for a tree walker.  It is used to
76549** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
76550** for additional information.
76551*/
76552static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
76553  int i;
76554  NameContext *pNC = pWalker->u.pNC;
76555  Parse *pParse = pNC->pParse;
76556  SrcList *pSrcList = pNC->pSrcList;
76557  AggInfo *pAggInfo = pNC->pAggInfo;
76558
76559  switch( pExpr->op ){
76560    case TK_AGG_COLUMN:
76561    case TK_COLUMN: {
76562      testcase( pExpr->op==TK_AGG_COLUMN );
76563      testcase( pExpr->op==TK_COLUMN );
76564      /* Check to see if the column is in one of the tables in the FROM
76565      ** clause of the aggregate query */
76566      if( ALWAYS(pSrcList!=0) ){
76567        struct SrcList_item *pItem = pSrcList->a;
76568        for(i=0; i<pSrcList->nSrc; i++, pItem++){
76569          struct AggInfo_col *pCol;
76570          assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
76571          if( pExpr->iTable==pItem->iCursor ){
76572            /* If we reach this point, it means that pExpr refers to a table
76573            ** that is in the FROM clause of the aggregate query.
76574            **
76575            ** Make an entry for the column in pAggInfo->aCol[] if there
76576            ** is not an entry there already.
76577            */
76578            int k;
76579            pCol = pAggInfo->aCol;
76580            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
76581              if( pCol->iTable==pExpr->iTable &&
76582                  pCol->iColumn==pExpr->iColumn ){
76583                break;
76584              }
76585            }
76586            if( (k>=pAggInfo->nColumn)
76587             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
76588            ){
76589              pCol = &pAggInfo->aCol[k];
76590              pCol->pTab = pExpr->pTab;
76591              pCol->iTable = pExpr->iTable;
76592              pCol->iColumn = pExpr->iColumn;
76593              pCol->iMem = ++pParse->nMem;
76594              pCol->iSorterColumn = -1;
76595              pCol->pExpr = pExpr;
76596              if( pAggInfo->pGroupBy ){
76597                int j, n;
76598                ExprList *pGB = pAggInfo->pGroupBy;
76599                struct ExprList_item *pTerm = pGB->a;
76600                n = pGB->nExpr;
76601                for(j=0; j<n; j++, pTerm++){
76602                  Expr *pE = pTerm->pExpr;
76603                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
76604                      pE->iColumn==pExpr->iColumn ){
76605                    pCol->iSorterColumn = j;
76606                    break;
76607                  }
76608                }
76609              }
76610              if( pCol->iSorterColumn<0 ){
76611                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
76612              }
76613            }
76614            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
76615            ** because it was there before or because we just created it).
76616            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
76617            ** pAggInfo->aCol[] entry.
76618            */
76619            ExprSetIrreducible(pExpr);
76620            pExpr->pAggInfo = pAggInfo;
76621            pExpr->op = TK_AGG_COLUMN;
76622            pExpr->iAgg = (i16)k;
76623            break;
76624          } /* endif pExpr->iTable==pItem->iCursor */
76625        } /* end loop over pSrcList */
76626      }
76627      return WRC_Prune;
76628    }
76629    case TK_AGG_FUNCTION: {
76630      /* The pNC->nDepth==0 test causes aggregate functions in subqueries
76631      ** to be ignored */
76632      if( pNC->nDepth==0 ){
76633        /* Check to see if pExpr is a duplicate of another aggregate
76634        ** function that is already in the pAggInfo structure
76635        */
76636        struct AggInfo_func *pItem = pAggInfo->aFunc;
76637        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
76638          if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
76639            break;
76640          }
76641        }
76642        if( i>=pAggInfo->nFunc ){
76643          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
76644          */
76645          u8 enc = ENC(pParse->db);
76646          i = addAggInfoFunc(pParse->db, pAggInfo);
76647          if( i>=0 ){
76648            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
76649            pItem = &pAggInfo->aFunc[i];
76650            pItem->pExpr = pExpr;
76651            pItem->iMem = ++pParse->nMem;
76652            assert( !ExprHasProperty(pExpr, EP_IntValue) );
76653            pItem->pFunc = sqlite3FindFunction(pParse->db,
76654                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
76655                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
76656            if( pExpr->flags & EP_Distinct ){
76657              pItem->iDistinct = pParse->nTab++;
76658            }else{
76659              pItem->iDistinct = -1;
76660            }
76661          }
76662        }
76663        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
76664        */
76665        assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
76666        ExprSetIrreducible(pExpr);
76667        pExpr->iAgg = (i16)i;
76668        pExpr->pAggInfo = pAggInfo;
76669        return WRC_Prune;
76670      }
76671    }
76672  }
76673  return WRC_Continue;
76674}
76675static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
76676  NameContext *pNC = pWalker->u.pNC;
76677  if( pNC->nDepth==0 ){
76678    pNC->nDepth++;
76679    sqlite3WalkSelect(pWalker, pSelect);
76680    pNC->nDepth--;
76681    return WRC_Prune;
76682  }else{
76683    return WRC_Continue;
76684  }
76685}
76686
76687/*
76688** Analyze the given expression looking for aggregate functions and
76689** for variables that need to be added to the pParse->aAgg[] array.
76690** Make additional entries to the pParse->aAgg[] array as necessary.
76691**
76692** This routine should only be called after the expression has been
76693** analyzed by sqlite3ResolveExprNames().
76694*/
76695SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
76696  Walker w;
76697  w.xExprCallback = analyzeAggregate;
76698  w.xSelectCallback = analyzeAggregatesInSelect;
76699  w.u.pNC = pNC;
76700  assert( pNC->pSrcList!=0 );
76701  sqlite3WalkExpr(&w, pExpr);
76702}
76703
76704/*
76705** Call sqlite3ExprAnalyzeAggregates() for every expression in an
76706** expression list.  Return the number of errors.
76707**
76708** If an error is found, the analysis is cut short.
76709*/
76710SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
76711  struct ExprList_item *pItem;
76712  int i;
76713  if( pList ){
76714    for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
76715      sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
76716    }
76717  }
76718}
76719
76720/*
76721** Allocate a single new register for use to hold some intermediate result.
76722*/
76723SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
76724  if( pParse->nTempReg==0 ){
76725    return ++pParse->nMem;
76726  }
76727  return pParse->aTempReg[--pParse->nTempReg];
76728}
76729
76730/*
76731** Deallocate a register, making available for reuse for some other
76732** purpose.
76733**
76734** If a register is currently being used by the column cache, then
76735** the dallocation is deferred until the column cache line that uses
76736** the register becomes stale.
76737*/
76738SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
76739  if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
76740    int i;
76741    struct yColCache *p;
76742    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
76743      if( p->iReg==iReg ){
76744        p->tempReg = 1;
76745        return;
76746      }
76747    }
76748    pParse->aTempReg[pParse->nTempReg++] = iReg;
76749  }
76750}
76751
76752/*
76753** Allocate or deallocate a block of nReg consecutive registers
76754*/
76755SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
76756  int i, n;
76757  i = pParse->iRangeReg;
76758  n = pParse->nRangeReg;
76759  if( nReg<=n ){
76760    assert( !usedAsColumnCache(pParse, i, i+n-1) );
76761    pParse->iRangeReg += nReg;
76762    pParse->nRangeReg -= nReg;
76763  }else{
76764    i = pParse->nMem+1;
76765    pParse->nMem += nReg;
76766  }
76767  return i;
76768}
76769SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
76770  sqlite3ExprCacheRemove(pParse, iReg, nReg);
76771  if( nReg>pParse->nRangeReg ){
76772    pParse->nRangeReg = nReg;
76773    pParse->iRangeReg = iReg;
76774  }
76775}
76776
76777/************** End of expr.c ************************************************/
76778/************** Begin file alter.c *******************************************/
76779/*
76780** 2005 February 15
76781**
76782** The author disclaims copyright to this source code.  In place of
76783** a legal notice, here is a blessing:
76784**
76785**    May you do good and not evil.
76786**    May you find forgiveness for yourself and forgive others.
76787**    May you share freely, never taking more than you give.
76788**
76789*************************************************************************
76790** This file contains C code routines that used to generate VDBE code
76791** that implements the ALTER TABLE command.
76792*/
76793
76794/*
76795** The code in this file only exists if we are not omitting the
76796** ALTER TABLE logic from the build.
76797*/
76798#ifndef SQLITE_OMIT_ALTERTABLE
76799
76800
76801/*
76802** This function is used by SQL generated to implement the
76803** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
76804** CREATE INDEX command. The second is a table name. The table name in
76805** the CREATE TABLE or CREATE INDEX statement is replaced with the third
76806** argument and the result returned. Examples:
76807**
76808** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
76809**     -> 'CREATE TABLE def(a, b, c)'
76810**
76811** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
76812**     -> 'CREATE INDEX i ON def(a, b, c)'
76813*/
76814static void renameTableFunc(
76815  sqlite3_context *context,
76816  int NotUsed,
76817  sqlite3_value **argv
76818){
76819  unsigned char const *zSql = sqlite3_value_text(argv[0]);
76820  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
76821
76822  int token;
76823  Token tname;
76824  unsigned char const *zCsr = zSql;
76825  int len = 0;
76826  char *zRet;
76827
76828  sqlite3 *db = sqlite3_context_db_handle(context);
76829
76830  UNUSED_PARAMETER(NotUsed);
76831
76832  /* The principle used to locate the table name in the CREATE TABLE
76833  ** statement is that the table name is the first non-space token that
76834  ** is immediately followed by a TK_LP or TK_USING token.
76835  */
76836  if( zSql ){
76837    do {
76838      if( !*zCsr ){
76839        /* Ran out of input before finding an opening bracket. Return NULL. */
76840        return;
76841      }
76842
76843      /* Store the token that zCsr points to in tname. */
76844      tname.z = (char*)zCsr;
76845      tname.n = len;
76846
76847      /* Advance zCsr to the next token. Store that token type in 'token',
76848      ** and its length in 'len' (to be used next iteration of this loop).
76849      */
76850      do {
76851        zCsr += len;
76852        len = sqlite3GetToken(zCsr, &token);
76853      } while( token==TK_SPACE );
76854      assert( len>0 );
76855    } while( token!=TK_LP && token!=TK_USING );
76856
76857    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
76858       zTableName, tname.z+tname.n);
76859    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
76860  }
76861}
76862
76863/*
76864** This C function implements an SQL user function that is used by SQL code
76865** generated by the ALTER TABLE ... RENAME command to modify the definition
76866** of any foreign key constraints that use the table being renamed as the
76867** parent table. It is passed three arguments:
76868**
76869**   1) The complete text of the CREATE TABLE statement being modified,
76870**   2) The old name of the table being renamed, and
76871**   3) The new name of the table being renamed.
76872**
76873** It returns the new CREATE TABLE statement. For example:
76874**
76875**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
76876**       -> 'CREATE TABLE t1(a REFERENCES t3)'
76877*/
76878#ifndef SQLITE_OMIT_FOREIGN_KEY
76879static void renameParentFunc(
76880  sqlite3_context *context,
76881  int NotUsed,
76882  sqlite3_value **argv
76883){
76884  sqlite3 *db = sqlite3_context_db_handle(context);
76885  char *zOutput = 0;
76886  char *zResult;
76887  unsigned char const *zInput = sqlite3_value_text(argv[0]);
76888  unsigned char const *zOld = sqlite3_value_text(argv[1]);
76889  unsigned char const *zNew = sqlite3_value_text(argv[2]);
76890
76891  unsigned const char *z;         /* Pointer to token */
76892  int n;                          /* Length of token z */
76893  int token;                      /* Type of token */
76894
76895  UNUSED_PARAMETER(NotUsed);
76896  for(z=zInput; *z; z=z+n){
76897    n = sqlite3GetToken(z, &token);
76898    if( token==TK_REFERENCES ){
76899      char *zParent;
76900      do {
76901        z += n;
76902        n = sqlite3GetToken(z, &token);
76903      }while( token==TK_SPACE );
76904
76905      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
76906      if( zParent==0 ) break;
76907      sqlite3Dequote(zParent);
76908      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
76909        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
76910            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
76911        );
76912        sqlite3DbFree(db, zOutput);
76913        zOutput = zOut;
76914        zInput = &z[n];
76915      }
76916      sqlite3DbFree(db, zParent);
76917    }
76918  }
76919
76920  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
76921  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
76922  sqlite3DbFree(db, zOutput);
76923}
76924#endif
76925
76926#ifndef SQLITE_OMIT_TRIGGER
76927/* This function is used by SQL generated to implement the
76928** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
76929** statement. The second is a table name. The table name in the CREATE
76930** TRIGGER statement is replaced with the third argument and the result
76931** returned. This is analagous to renameTableFunc() above, except for CREATE
76932** TRIGGER, not CREATE INDEX and CREATE TABLE.
76933*/
76934static void renameTriggerFunc(
76935  sqlite3_context *context,
76936  int NotUsed,
76937  sqlite3_value **argv
76938){
76939  unsigned char const *zSql = sqlite3_value_text(argv[0]);
76940  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
76941
76942  int token;
76943  Token tname;
76944  int dist = 3;
76945  unsigned char const *zCsr = zSql;
76946  int len = 0;
76947  char *zRet;
76948  sqlite3 *db = sqlite3_context_db_handle(context);
76949
76950  UNUSED_PARAMETER(NotUsed);
76951
76952  /* The principle used to locate the table name in the CREATE TRIGGER
76953  ** statement is that the table name is the first token that is immediatedly
76954  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
76955  ** of TK_WHEN, TK_BEGIN or TK_FOR.
76956  */
76957  if( zSql ){
76958    do {
76959
76960      if( !*zCsr ){
76961        /* Ran out of input before finding the table name. Return NULL. */
76962        return;
76963      }
76964
76965      /* Store the token that zCsr points to in tname. */
76966      tname.z = (char*)zCsr;
76967      tname.n = len;
76968
76969      /* Advance zCsr to the next token. Store that token type in 'token',
76970      ** and its length in 'len' (to be used next iteration of this loop).
76971      */
76972      do {
76973        zCsr += len;
76974        len = sqlite3GetToken(zCsr, &token);
76975      }while( token==TK_SPACE );
76976      assert( len>0 );
76977
76978      /* Variable 'dist' stores the number of tokens read since the most
76979      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
76980      ** token is read and 'dist' equals 2, the condition stated above
76981      ** to be met.
76982      **
76983      ** Note that ON cannot be a database, table or column name, so
76984      ** there is no need to worry about syntax like
76985      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
76986      */
76987      dist++;
76988      if( token==TK_DOT || token==TK_ON ){
76989        dist = 0;
76990      }
76991    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
76992
76993    /* Variable tname now contains the token that is the old table-name
76994    ** in the CREATE TRIGGER statement.
76995    */
76996    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
76997       zTableName, tname.z+tname.n);
76998    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
76999  }
77000}
77001#endif   /* !SQLITE_OMIT_TRIGGER */
77002
77003/*
77004** Register built-in functions used to help implement ALTER TABLE
77005*/
77006SQLITE_PRIVATE void sqlite3AlterFunctions(void){
77007  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
77008    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
77009#ifndef SQLITE_OMIT_TRIGGER
77010    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
77011#endif
77012#ifndef SQLITE_OMIT_FOREIGN_KEY
77013    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
77014#endif
77015  };
77016  int i;
77017  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
77018  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
77019
77020  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
77021    sqlite3FuncDefInsert(pHash, &aFunc[i]);
77022  }
77023}
77024
77025/*
77026** This function is used to create the text of expressions of the form:
77027**
77028**   name=<constant1> OR name=<constant2> OR ...
77029**
77030** If argument zWhere is NULL, then a pointer string containing the text
77031** "name=<constant>" is returned, where <constant> is the quoted version
77032** of the string passed as argument zConstant. The returned buffer is
77033** allocated using sqlite3DbMalloc(). It is the responsibility of the
77034** caller to ensure that it is eventually freed.
77035**
77036** If argument zWhere is not NULL, then the string returned is
77037** "<where> OR name=<constant>", where <where> is the contents of zWhere.
77038** In this case zWhere is passed to sqlite3DbFree() before returning.
77039**
77040*/
77041static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
77042  char *zNew;
77043  if( !zWhere ){
77044    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
77045  }else{
77046    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
77047    sqlite3DbFree(db, zWhere);
77048  }
77049  return zNew;
77050}
77051
77052#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
77053/*
77054** Generate the text of a WHERE expression which can be used to select all
77055** tables that have foreign key constraints that refer to table pTab (i.e.
77056** constraints for which pTab is the parent table) from the sqlite_master
77057** table.
77058*/
77059static char *whereForeignKeys(Parse *pParse, Table *pTab){
77060  FKey *p;
77061  char *zWhere = 0;
77062  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
77063    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
77064  }
77065  return zWhere;
77066}
77067#endif
77068
77069/*
77070** Generate the text of a WHERE expression which can be used to select all
77071** temporary triggers on table pTab from the sqlite_temp_master table. If
77072** table pTab has no temporary triggers, or is itself stored in the
77073** temporary database, NULL is returned.
77074*/
77075static char *whereTempTriggers(Parse *pParse, Table *pTab){
77076  Trigger *pTrig;
77077  char *zWhere = 0;
77078  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
77079
77080  /* If the table is not located in the temp-db (in which case NULL is
77081  ** returned, loop through the tables list of triggers. For each trigger
77082  ** that is not part of the temp-db schema, add a clause to the WHERE
77083  ** expression being built up in zWhere.
77084  */
77085  if( pTab->pSchema!=pTempSchema ){
77086    sqlite3 *db = pParse->db;
77087    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
77088      if( pTrig->pSchema==pTempSchema ){
77089        zWhere = whereOrName(db, zWhere, pTrig->zName);
77090      }
77091    }
77092  }
77093  if( zWhere ){
77094    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
77095    sqlite3DbFree(pParse->db, zWhere);
77096    zWhere = zNew;
77097  }
77098  return zWhere;
77099}
77100
77101/*
77102** Generate code to drop and reload the internal representation of table
77103** pTab from the database, including triggers and temporary triggers.
77104** Argument zName is the name of the table in the database schema at
77105** the time the generated code is executed. This can be different from
77106** pTab->zName if this function is being called to code part of an
77107** "ALTER TABLE RENAME TO" statement.
77108*/
77109static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
77110  Vdbe *v;
77111  char *zWhere;
77112  int iDb;                   /* Index of database containing pTab */
77113#ifndef SQLITE_OMIT_TRIGGER
77114  Trigger *pTrig;
77115#endif
77116
77117  v = sqlite3GetVdbe(pParse);
77118  if( NEVER(v==0) ) return;
77119  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
77120  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
77121  assert( iDb>=0 );
77122
77123#ifndef SQLITE_OMIT_TRIGGER
77124  /* Drop any table triggers from the internal schema. */
77125  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
77126    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
77127    assert( iTrigDb==iDb || iTrigDb==1 );
77128    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
77129  }
77130#endif
77131
77132  /* Drop the table and index from the internal schema.  */
77133  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
77134
77135  /* Reload the table, index and permanent trigger schemas. */
77136  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
77137  if( !zWhere ) return;
77138  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
77139
77140#ifndef SQLITE_OMIT_TRIGGER
77141  /* Now, if the table is not stored in the temp database, reload any temp
77142  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
77143  */
77144  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
77145    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
77146  }
77147#endif
77148}
77149
77150/*
77151** Parameter zName is the name of a table that is about to be altered
77152** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
77153** If the table is a system table, this function leaves an error message
77154** in pParse->zErr (system tables may not be altered) and returns non-zero.
77155**
77156** Or, if zName is not a system table, zero is returned.
77157*/
77158static int isSystemTable(Parse *pParse, const char *zName){
77159  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
77160    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
77161    return 1;
77162  }
77163  return 0;
77164}
77165
77166/*
77167** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
77168** command.
77169*/
77170SQLITE_PRIVATE void sqlite3AlterRenameTable(
77171  Parse *pParse,            /* Parser context. */
77172  SrcList *pSrc,            /* The table to rename. */
77173  Token *pName              /* The new table name. */
77174){
77175  int iDb;                  /* Database that contains the table */
77176  char *zDb;                /* Name of database iDb */
77177  Table *pTab;              /* Table being renamed */
77178  char *zName = 0;          /* NULL-terminated version of pName */
77179  sqlite3 *db = pParse->db; /* Database connection */
77180  int nTabName;             /* Number of UTF-8 characters in zTabName */
77181  const char *zTabName;     /* Original name of the table */
77182  Vdbe *v;
77183#ifndef SQLITE_OMIT_TRIGGER
77184  char *zWhere = 0;         /* Where clause to locate temp triggers */
77185#endif
77186  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
77187  int savedDbFlags;         /* Saved value of db->flags */
77188
77189  savedDbFlags = db->flags;
77190  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
77191  assert( pSrc->nSrc==1 );
77192  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
77193
77194  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
77195  if( !pTab ) goto exit_rename_table;
77196  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
77197  zDb = db->aDb[iDb].zName;
77198  db->flags |= SQLITE_PreferBuiltin;
77199
77200  /* Get a NULL terminated version of the new table name. */
77201  zName = sqlite3NameFromToken(db, pName);
77202  if( !zName ) goto exit_rename_table;
77203
77204  /* Check that a table or index named 'zName' does not already exist
77205  ** in database iDb. If so, this is an error.
77206  */
77207  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
77208    sqlite3ErrorMsg(pParse,
77209        "there is already another table or index with this name: %s", zName);
77210    goto exit_rename_table;
77211  }
77212
77213  /* Make sure it is not a system table being altered, or a reserved name
77214  ** that the table is being renamed to.
77215  */
77216  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
77217    goto exit_rename_table;
77218  }
77219  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
77220    exit_rename_table;
77221  }
77222
77223#ifndef SQLITE_OMIT_VIEW
77224  if( pTab->pSelect ){
77225    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
77226    goto exit_rename_table;
77227  }
77228#endif
77229
77230#ifndef SQLITE_OMIT_AUTHORIZATION
77231  /* Invoke the authorization callback. */
77232  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
77233    goto exit_rename_table;
77234  }
77235#endif
77236
77237#ifndef SQLITE_OMIT_VIRTUALTABLE
77238  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
77239    goto exit_rename_table;
77240  }
77241  if( IsVirtual(pTab) ){
77242    pVTab = sqlite3GetVTable(db, pTab);
77243    if( pVTab->pVtab->pModule->xRename==0 ){
77244      pVTab = 0;
77245    }
77246  }
77247#endif
77248
77249  /* Begin a transaction and code the VerifyCookie for database iDb.
77250  ** Then modify the schema cookie (since the ALTER TABLE modifies the
77251  ** schema). Open a statement transaction if the table is a virtual
77252  ** table.
77253  */
77254  v = sqlite3GetVdbe(pParse);
77255  if( v==0 ){
77256    goto exit_rename_table;
77257  }
77258  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
77259  sqlite3ChangeCookie(pParse, iDb);
77260
77261  /* If this is a virtual table, invoke the xRename() function if
77262  ** one is defined. The xRename() callback will modify the names
77263  ** of any resources used by the v-table implementation (including other
77264  ** SQLite tables) that are identified by the name of the virtual table.
77265  */
77266#ifndef SQLITE_OMIT_VIRTUALTABLE
77267  if( pVTab ){
77268    int i = ++pParse->nMem;
77269    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
77270    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
77271    sqlite3MayAbort(pParse);
77272  }
77273#endif
77274
77275  /* figure out how many UTF-8 characters are in zName */
77276  zTabName = pTab->zName;
77277  nTabName = sqlite3Utf8CharLen(zTabName, -1);
77278
77279#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
77280  if( db->flags&SQLITE_ForeignKeys ){
77281    /* If foreign-key support is enabled, rewrite the CREATE TABLE
77282    ** statements corresponding to all child tables of foreign key constraints
77283    ** for which the renamed table is the parent table.  */
77284    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
77285      sqlite3NestedParse(pParse,
77286          "UPDATE \"%w\".%s SET "
77287              "sql = sqlite_rename_parent(sql, %Q, %Q) "
77288              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
77289      sqlite3DbFree(db, zWhere);
77290    }
77291  }
77292#endif
77293
77294  /* Modify the sqlite_master table to use the new table name. */
77295  sqlite3NestedParse(pParse,
77296      "UPDATE %Q.%s SET "
77297#ifdef SQLITE_OMIT_TRIGGER
77298          "sql = sqlite_rename_table(sql, %Q), "
77299#else
77300          "sql = CASE "
77301            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
77302            "ELSE sqlite_rename_table(sql, %Q) END, "
77303#endif
77304          "tbl_name = %Q, "
77305          "name = CASE "
77306            "WHEN type='table' THEN %Q "
77307            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
77308             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
77309            "ELSE name END "
77310      "WHERE tbl_name=%Q AND "
77311          "(type='table' OR type='index' OR type='trigger');",
77312      zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
77313#ifndef SQLITE_OMIT_TRIGGER
77314      zName,
77315#endif
77316      zName, nTabName, zTabName
77317  );
77318
77319#ifndef SQLITE_OMIT_AUTOINCREMENT
77320  /* If the sqlite_sequence table exists in this database, then update
77321  ** it with the new table name.
77322  */
77323  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
77324    sqlite3NestedParse(pParse,
77325        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
77326        zDb, zName, pTab->zName);
77327  }
77328#endif
77329
77330#ifndef SQLITE_OMIT_TRIGGER
77331  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
77332  ** table. Don't do this if the table being ALTERed is itself located in
77333  ** the temp database.
77334  */
77335  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
77336    sqlite3NestedParse(pParse,
77337        "UPDATE sqlite_temp_master SET "
77338            "sql = sqlite_rename_trigger(sql, %Q), "
77339            "tbl_name = %Q "
77340            "WHERE %s;", zName, zName, zWhere);
77341    sqlite3DbFree(db, zWhere);
77342  }
77343#endif
77344
77345#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
77346  if( db->flags&SQLITE_ForeignKeys ){
77347    FKey *p;
77348    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
77349      Table *pFrom = p->pFrom;
77350      if( pFrom!=pTab ){
77351        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
77352      }
77353    }
77354  }
77355#endif
77356
77357  /* Drop and reload the internal table schema. */
77358  reloadTableSchema(pParse, pTab, zName);
77359
77360exit_rename_table:
77361  sqlite3SrcListDelete(db, pSrc);
77362  sqlite3DbFree(db, zName);
77363  db->flags = savedDbFlags;
77364}
77365
77366
77367/*
77368** Generate code to make sure the file format number is at least minFormat.
77369** The generated code will increase the file format number if necessary.
77370*/
77371SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
77372  Vdbe *v;
77373  v = sqlite3GetVdbe(pParse);
77374  /* The VDBE should have been allocated before this routine is called.
77375  ** If that allocation failed, we would have quit before reaching this
77376  ** point */
77377  if( ALWAYS(v) ){
77378    int r1 = sqlite3GetTempReg(pParse);
77379    int r2 = sqlite3GetTempReg(pParse);
77380    int j1;
77381    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
77382    sqlite3VdbeUsesBtree(v, iDb);
77383    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
77384    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
77385    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
77386    sqlite3VdbeJumpHere(v, j1);
77387    sqlite3ReleaseTempReg(pParse, r1);
77388    sqlite3ReleaseTempReg(pParse, r2);
77389  }
77390}
77391
77392/*
77393** This function is called after an "ALTER TABLE ... ADD" statement
77394** has been parsed. Argument pColDef contains the text of the new
77395** column definition.
77396**
77397** The Table structure pParse->pNewTable was extended to include
77398** the new column during parsing.
77399*/
77400SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
77401  Table *pNew;              /* Copy of pParse->pNewTable */
77402  Table *pTab;              /* Table being altered */
77403  int iDb;                  /* Database number */
77404  const char *zDb;          /* Database name */
77405  const char *zTab;         /* Table name */
77406  char *zCol;               /* Null-terminated column definition */
77407  Column *pCol;             /* The new column */
77408  Expr *pDflt;              /* Default value for the new column */
77409  sqlite3 *db;              /* The database connection; */
77410
77411  db = pParse->db;
77412  if( pParse->nErr || db->mallocFailed ) return;
77413  pNew = pParse->pNewTable;
77414  assert( pNew );
77415
77416  assert( sqlite3BtreeHoldsAllMutexes(db) );
77417  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
77418  zDb = db->aDb[iDb].zName;
77419  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
77420  pCol = &pNew->aCol[pNew->nCol-1];
77421  pDflt = pCol->pDflt;
77422  pTab = sqlite3FindTable(db, zTab, zDb);
77423  assert( pTab );
77424
77425#ifndef SQLITE_OMIT_AUTHORIZATION
77426  /* Invoke the authorization callback. */
77427  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
77428    return;
77429  }
77430#endif
77431
77432  /* If the default value for the new column was specified with a
77433  ** literal NULL, then set pDflt to 0. This simplifies checking
77434  ** for an SQL NULL default below.
77435  */
77436  if( pDflt && pDflt->op==TK_NULL ){
77437    pDflt = 0;
77438  }
77439
77440  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
77441  ** If there is a NOT NULL constraint, then the default value for the
77442  ** column must not be NULL.
77443  */
77444  if( pCol->isPrimKey ){
77445    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
77446    return;
77447  }
77448  if( pNew->pIndex ){
77449    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
77450    return;
77451  }
77452  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
77453    sqlite3ErrorMsg(pParse,
77454        "Cannot add a REFERENCES column with non-NULL default value");
77455    return;
77456  }
77457  if( pCol->notNull && !pDflt ){
77458    sqlite3ErrorMsg(pParse,
77459        "Cannot add a NOT NULL column with default value NULL");
77460    return;
77461  }
77462
77463  /* Ensure the default expression is something that sqlite3ValueFromExpr()
77464  ** can handle (i.e. not CURRENT_TIME etc.)
77465  */
77466  if( pDflt ){
77467    sqlite3_value *pVal;
77468    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
77469      db->mallocFailed = 1;
77470      return;
77471    }
77472    if( !pVal ){
77473      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
77474      return;
77475    }
77476    sqlite3ValueFree(pVal);
77477  }
77478
77479  /* Modify the CREATE TABLE statement. */
77480  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
77481  if( zCol ){
77482    char *zEnd = &zCol[pColDef->n-1];
77483    int savedDbFlags = db->flags;
77484    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
77485      *zEnd-- = '\0';
77486    }
77487    db->flags |= SQLITE_PreferBuiltin;
77488    sqlite3NestedParse(pParse,
77489        "UPDATE \"%w\".%s SET "
77490          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
77491        "WHERE type = 'table' AND name = %Q",
77492      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
77493      zTab
77494    );
77495    sqlite3DbFree(db, zCol);
77496    db->flags = savedDbFlags;
77497  }
77498
77499  /* If the default value of the new column is NULL, then set the file
77500  ** format to 2. If the default value of the new column is not NULL,
77501  ** the file format becomes 3.
77502  */
77503  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
77504
77505  /* Reload the schema of the modified table. */
77506  reloadTableSchema(pParse, pTab, pTab->zName);
77507}
77508
77509/*
77510** This function is called by the parser after the table-name in
77511** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
77512** pSrc is the full-name of the table being altered.
77513**
77514** This routine makes a (partial) copy of the Table structure
77515** for the table being altered and sets Parse.pNewTable to point
77516** to it. Routines called by the parser as the column definition
77517** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
77518** the copy. The copy of the Table structure is deleted by tokenize.c
77519** after parsing is finished.
77520**
77521** Routine sqlite3AlterFinishAddColumn() will be called to complete
77522** coding the "ALTER TABLE ... ADD" statement.
77523*/
77524SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
77525  Table *pNew;
77526  Table *pTab;
77527  Vdbe *v;
77528  int iDb;
77529  int i;
77530  int nAlloc;
77531  sqlite3 *db = pParse->db;
77532
77533  /* Look up the table being altered. */
77534  assert( pParse->pNewTable==0 );
77535  assert( sqlite3BtreeHoldsAllMutexes(db) );
77536  if( db->mallocFailed ) goto exit_begin_add_column;
77537  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
77538  if( !pTab ) goto exit_begin_add_column;
77539
77540#ifndef SQLITE_OMIT_VIRTUALTABLE
77541  if( IsVirtual(pTab) ){
77542    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
77543    goto exit_begin_add_column;
77544  }
77545#endif
77546
77547  /* Make sure this is not an attempt to ALTER a view. */
77548  if( pTab->pSelect ){
77549    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
77550    goto exit_begin_add_column;
77551  }
77552  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
77553    goto exit_begin_add_column;
77554  }
77555
77556  assert( pTab->addColOffset>0 );
77557  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
77558
77559  /* Put a copy of the Table struct in Parse.pNewTable for the
77560  ** sqlite3AddColumn() function and friends to modify.  But modify
77561  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
77562  ** prefix, we insure that the name will not collide with an existing
77563  ** table because user table are not allowed to have the "sqlite_"
77564  ** prefix on their name.
77565  */
77566  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
77567  if( !pNew ) goto exit_begin_add_column;
77568  pParse->pNewTable = pNew;
77569  pNew->nRef = 1;
77570  pNew->nCol = pTab->nCol;
77571  assert( pNew->nCol>0 );
77572  nAlloc = (((pNew->nCol-1)/8)*8)+8;
77573  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
77574  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
77575  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
77576  if( !pNew->aCol || !pNew->zName ){
77577    db->mallocFailed = 1;
77578    goto exit_begin_add_column;
77579  }
77580  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
77581  for(i=0; i<pNew->nCol; i++){
77582    Column *pCol = &pNew->aCol[i];
77583    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
77584    pCol->zColl = 0;
77585    pCol->zType = 0;
77586    pCol->pDflt = 0;
77587    pCol->zDflt = 0;
77588  }
77589  pNew->pSchema = db->aDb[iDb].pSchema;
77590  pNew->addColOffset = pTab->addColOffset;
77591  pNew->nRef = 1;
77592
77593  /* Begin a transaction and increment the schema cookie.  */
77594  sqlite3BeginWriteOperation(pParse, 0, iDb);
77595  v = sqlite3GetVdbe(pParse);
77596  if( !v ) goto exit_begin_add_column;
77597  sqlite3ChangeCookie(pParse, iDb);
77598
77599exit_begin_add_column:
77600  sqlite3SrcListDelete(db, pSrc);
77601  return;
77602}
77603#endif  /* SQLITE_ALTER_TABLE */
77604
77605/************** End of alter.c ***********************************************/
77606/************** Begin file analyze.c *****************************************/
77607/*
77608** 2005 July 8
77609**
77610** The author disclaims copyright to this source code.  In place of
77611** a legal notice, here is a blessing:
77612**
77613**    May you do good and not evil.
77614**    May you find forgiveness for yourself and forgive others.
77615**    May you share freely, never taking more than you give.
77616**
77617*************************************************************************
77618** This file contains code associated with the ANALYZE command.
77619*/
77620#ifndef SQLITE_OMIT_ANALYZE
77621
77622/*
77623** This routine generates code that opens the sqlite_stat1 table for
77624** writing with cursor iStatCur. If the library was built with the
77625** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
77626** opened for writing using cursor (iStatCur+1)
77627**
77628** If the sqlite_stat1 tables does not previously exist, it is created.
77629** Similarly, if the sqlite_stat2 table does not exist and the library
77630** is compiled with SQLITE_ENABLE_STAT2 defined, it is created.
77631**
77632** Argument zWhere may be a pointer to a buffer containing a table name,
77633** or it may be a NULL pointer. If it is not NULL, then all entries in
77634** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
77635** with the named table are deleted. If zWhere==0, then code is generated
77636** to delete all stat table entries.
77637*/
77638static void openStatTable(
77639  Parse *pParse,          /* Parsing context */
77640  int iDb,                /* The database we are looking in */
77641  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
77642  const char *zWhere,     /* Delete entries for this table or index */
77643  const char *zWhereType  /* Either "tbl" or "idx" */
77644){
77645  static const struct {
77646    const char *zName;
77647    const char *zCols;
77648  } aTable[] = {
77649    { "sqlite_stat1", "tbl,idx,stat" },
77650#ifdef SQLITE_ENABLE_STAT2
77651    { "sqlite_stat2", "tbl,idx,sampleno,sample" },
77652#endif
77653  };
77654
77655  int aRoot[] = {0, 0};
77656  u8 aCreateTbl[] = {0, 0};
77657
77658  int i;
77659  sqlite3 *db = pParse->db;
77660  Db *pDb;
77661  Vdbe *v = sqlite3GetVdbe(pParse);
77662  if( v==0 ) return;
77663  assert( sqlite3BtreeHoldsAllMutexes(db) );
77664  assert( sqlite3VdbeDb(v)==db );
77665  pDb = &db->aDb[iDb];
77666
77667  for(i=0; i<ArraySize(aTable); i++){
77668    const char *zTab = aTable[i].zName;
77669    Table *pStat;
77670    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
77671      /* The sqlite_stat[12] table does not exist. Create it. Note that a
77672      ** side-effect of the CREATE TABLE statement is to leave the rootpage
77673      ** of the new table in register pParse->regRoot. This is important
77674      ** because the OpenWrite opcode below will be needing it. */
77675      sqlite3NestedParse(pParse,
77676          "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
77677      );
77678      aRoot[i] = pParse->regRoot;
77679      aCreateTbl[i] = 1;
77680    }else{
77681      /* The table already exists. If zWhere is not NULL, delete all entries
77682      ** associated with the table zWhere. If zWhere is NULL, delete the
77683      ** entire contents of the table. */
77684      aRoot[i] = pStat->tnum;
77685      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
77686      if( zWhere ){
77687        sqlite3NestedParse(pParse,
77688           "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
77689        );
77690      }else{
77691        /* The sqlite_stat[12] table already exists.  Delete all rows. */
77692        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
77693      }
77694    }
77695  }
77696
77697  /* Open the sqlite_stat[12] tables for writing. */
77698  for(i=0; i<ArraySize(aTable); i++){
77699    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
77700    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
77701    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
77702  }
77703}
77704
77705/*
77706** Generate code to do an analysis of all indices associated with
77707** a single table.
77708*/
77709static void analyzeOneTable(
77710  Parse *pParse,   /* Parser context */
77711  Table *pTab,     /* Table whose indices are to be analyzed */
77712  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
77713  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
77714  int iMem         /* Available memory locations begin here */
77715){
77716  sqlite3 *db = pParse->db;    /* Database handle */
77717  Index *pIdx;                 /* An index to being analyzed */
77718  int iIdxCur;                 /* Cursor open on index being analyzed */
77719  Vdbe *v;                     /* The virtual machine being built up */
77720  int i;                       /* Loop counter */
77721  int topOfLoop;               /* The top of the loop */
77722  int endOfLoop;               /* The end of the loop */
77723  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
77724  int iDb;                     /* Index of database containing pTab */
77725  int regTabname = iMem++;     /* Register containing table name */
77726  int regIdxname = iMem++;     /* Register containing index name */
77727  int regSampleno = iMem++;    /* Register containing next sample number */
77728  int regCol = iMem++;         /* Content of a column analyzed table */
77729  int regRec = iMem++;         /* Register holding completed record */
77730  int regTemp = iMem++;        /* Temporary use register */
77731  int regRowid = iMem++;       /* Rowid for the inserted record */
77732
77733#ifdef SQLITE_ENABLE_STAT2
77734  int addr = 0;                /* Instruction address */
77735  int regTemp2 = iMem++;       /* Temporary use register */
77736  int regSamplerecno = iMem++; /* Index of next sample to record */
77737  int regRecno = iMem++;       /* Current sample index */
77738  int regLast = iMem++;        /* Index of last sample to record */
77739  int regFirst = iMem++;       /* Index of first sample to record */
77740#endif
77741
77742  v = sqlite3GetVdbe(pParse);
77743  if( v==0 || NEVER(pTab==0) ){
77744    return;
77745  }
77746  if( pTab->tnum==0 ){
77747    /* Do not gather statistics on views or virtual tables */
77748    return;
77749  }
77750  if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
77751    /* Do not gather statistics on system tables */
77752    return;
77753  }
77754  assert( sqlite3BtreeHoldsAllMutexes(db) );
77755  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
77756  assert( iDb>=0 );
77757  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77758#ifndef SQLITE_OMIT_AUTHORIZATION
77759  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
77760      db->aDb[iDb].zName ) ){
77761    return;
77762  }
77763#endif
77764
77765  /* Establish a read-lock on the table at the shared-cache level. */
77766  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
77767
77768  iIdxCur = pParse->nTab++;
77769  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
77770  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
77771    int nCol;
77772    KeyInfo *pKey;
77773
77774    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
77775    nCol = pIdx->nColumn;
77776    pKey = sqlite3IndexKeyinfo(pParse, pIdx);
77777    if( iMem+1+(nCol*2)>pParse->nMem ){
77778      pParse->nMem = iMem+1+(nCol*2);
77779    }
77780
77781    /* Open a cursor to the index to be analyzed. */
77782    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
77783    sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
77784        (char *)pKey, P4_KEYINFO_HANDOFF);
77785    VdbeComment((v, "%s", pIdx->zName));
77786
77787    /* Populate the register containing the index name. */
77788    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
77789
77790#ifdef SQLITE_ENABLE_STAT2
77791
77792    /* If this iteration of the loop is generating code to analyze the
77793    ** first index in the pTab->pIndex list, then register regLast has
77794    ** not been populated. In this case populate it now.  */
77795    if( pTab->pIndex==pIdx ){
77796      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77797      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77798      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77799
77800      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77801      sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77802      addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77803      sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77804      sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77805      sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77806      sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
77807      sqlite3VdbeJumpHere(v, addr);
77808    }
77809
77810    /* Zero the regSampleno and regRecno registers. */
77811    sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77812    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77813    sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77814#endif
77815
77816    /* The block of memory cells initialized here is used as follows.
77817    **
77818    **    iMem:
77819    **        The total number of rows in the table.
77820    **
77821    **    iMem+1 .. iMem+nCol:
77822    **        Number of distinct entries in index considering the
77823    **        left-most N columns only, where N is between 1 and nCol,
77824    **        inclusive.
77825    **
77826    **    iMem+nCol+1 .. Mem+2*nCol:
77827    **        Previous value of indexed columns, from left to right.
77828    **
77829    ** Cells iMem through iMem+nCol are initialized to 0. The others are
77830    ** initialized to contain an SQL NULL.
77831    */
77832    for(i=0; i<=nCol; i++){
77833      sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
77834    }
77835    for(i=0; i<nCol; i++){
77836      sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
77837    }
77838
77839    /* Start the analysis loop. This loop runs through all the entries in
77840    ** the index b-tree.  */
77841    endOfLoop = sqlite3VdbeMakeLabel(v);
77842    sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
77843    topOfLoop = sqlite3VdbeCurrentAddr(v);
77844    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
77845
77846    for(i=0; i<nCol; i++){
77847      CollSeq *pColl;
77848      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
77849      if( i==0 ){
77850#ifdef SQLITE_ENABLE_STAT2
77851        /* Check if the record that cursor iIdxCur points to contains a
77852        ** value that should be stored in the sqlite_stat2 table. If so,
77853        ** store it.  */
77854        int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77855        assert( regTabname+1==regIdxname
77856             && regTabname+2==regSampleno
77857             && regTabname+3==regCol
77858        );
77859        sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77860        sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77861        sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77862        sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77863
77864        /* Calculate new values for regSamplerecno and regSampleno.
77865        **
77866        **   sampleno = sampleno + 1
77867        **   samplerecno = samplerecno+(remaining records)/(remaining samples)
77868        */
77869        sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77870        sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77871        sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77872        sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77873        sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77874        sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77875        sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77876
77877        sqlite3VdbeJumpHere(v, ne);
77878        sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77879#endif
77880
77881        /* Always record the very first row */
77882        sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77883      }
77884      assert( pIdx->azColl!=0 );
77885      assert( pIdx->azColl[i]!=0 );
77886      pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
77887      sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77888                       (char*)pColl, P4_COLLSEQ);
77889      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
77890    }
77891    if( db->mallocFailed ){
77892      /* If a malloc failure has occurred, then the result of the expression
77893      ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77894      ** below may be negative. Which causes an assert() to fail (or an
77895      ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
77896      return;
77897    }
77898    sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
77899    for(i=0; i<nCol; i++){
77900      int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
77901      if( i==0 ){
77902        sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
77903      }
77904      sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
77905      sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
77906      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
77907    }
77908
77909    /* End of the analysis loop. */
77910    sqlite3VdbeResolveLabel(v, endOfLoop);
77911    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
77912    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77913
77914    /* Store the results in sqlite_stat1.
77915    **
77916    ** The result is a single row of the sqlite_stat1 table.  The first
77917    ** two columns are the names of the table and index.  The third column
77918    ** is a string composed of a list of integer statistics about the
77919    ** index.  The first integer in the list is the total number of entries
77920    ** in the index.  There is one additional integer in the list for each
77921    ** column of the table.  This additional integer is a guess of how many
77922    ** rows of the table the index will select.  If D is the count of distinct
77923    ** values and K is the total number of rows, then the integer is computed
77924    ** as:
77925    **
77926    **        I = (K+D-1)/D
77927    **
77928    ** If K==0 then no entry is made into the sqlite_stat1 table.
77929    ** If K>0 then it is always the case the D>0 so division by zero
77930    ** is never possible.
77931    */
77932    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
77933    if( jZeroRows<0 ){
77934      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
77935    }
77936    for(i=0; i<nCol; i++){
77937      sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
77938      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77939      sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
77940      sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77941      sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
77942      sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
77943      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77944    }
77945    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77946    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77947    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77948    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77949  }
77950
77951  /* If the table has no indices, create a single sqlite_stat1 entry
77952  ** containing NULL as the index name and the row count as the content.
77953  */
77954  if( pTab->pIndex==0 ){
77955    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
77956    VdbeComment((v, "%s", pTab->zName));
77957    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
77958    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77959    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
77960  }else{
77961    sqlite3VdbeJumpHere(v, jZeroRows);
77962    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
77963  }
77964  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
77965  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77966  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77967  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77968  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77969  if( pParse->nMem<regRec ) pParse->nMem = regRec;
77970  sqlite3VdbeJumpHere(v, jZeroRows);
77971}
77972
77973/*
77974** Generate code that will cause the most recent index analysis to
77975** be loaded into internal hash tables where is can be used.
77976*/
77977static void loadAnalysis(Parse *pParse, int iDb){
77978  Vdbe *v = sqlite3GetVdbe(pParse);
77979  if( v ){
77980    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
77981  }
77982}
77983
77984/*
77985** Generate code that will do an analysis of an entire database
77986*/
77987static void analyzeDatabase(Parse *pParse, int iDb){
77988  sqlite3 *db = pParse->db;
77989  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
77990  HashElem *k;
77991  int iStatCur;
77992  int iMem;
77993
77994  sqlite3BeginWriteOperation(pParse, 0, iDb);
77995  iStatCur = pParse->nTab;
77996  pParse->nTab += 2;
77997  openStatTable(pParse, iDb, iStatCur, 0, 0);
77998  iMem = pParse->nMem+1;
77999  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78000  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
78001    Table *pTab = (Table*)sqliteHashData(k);
78002    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
78003  }
78004  loadAnalysis(pParse, iDb);
78005}
78006
78007/*
78008** Generate code that will do an analysis of a single table in
78009** a database.  If pOnlyIdx is not NULL then it is a single index
78010** in pTab that should be analyzed.
78011*/
78012static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
78013  int iDb;
78014  int iStatCur;
78015
78016  assert( pTab!=0 );
78017  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
78018  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78019  sqlite3BeginWriteOperation(pParse, 0, iDb);
78020  iStatCur = pParse->nTab;
78021  pParse->nTab += 2;
78022  if( pOnlyIdx ){
78023    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
78024  }else{
78025    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
78026  }
78027  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
78028  loadAnalysis(pParse, iDb);
78029}
78030
78031/*
78032** Generate code for the ANALYZE command.  The parser calls this routine
78033** when it recognizes an ANALYZE command.
78034**
78035**        ANALYZE                            -- 1
78036**        ANALYZE  <database>                -- 2
78037**        ANALYZE  ?<database>.?<tablename>  -- 3
78038**
78039** Form 1 causes all indices in all attached databases to be analyzed.
78040** Form 2 analyzes all indices the single database named.
78041** Form 3 analyzes all indices associated with the named table.
78042*/
78043SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
78044  sqlite3 *db = pParse->db;
78045  int iDb;
78046  int i;
78047  char *z, *zDb;
78048  Table *pTab;
78049  Index *pIdx;
78050  Token *pTableName;
78051
78052  /* Read the database schema. If an error occurs, leave an error message
78053  ** and code in pParse and return NULL. */
78054  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
78055  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
78056    return;
78057  }
78058
78059  assert( pName2!=0 || pName1==0 );
78060  if( pName1==0 ){
78061    /* Form 1:  Analyze everything */
78062    for(i=0; i<db->nDb; i++){
78063      if( i==1 ) continue;  /* Do not analyze the TEMP database */
78064      analyzeDatabase(pParse, i);
78065    }
78066  }else if( pName2->n==0 ){
78067    /* Form 2:  Analyze the database or table named */
78068    iDb = sqlite3FindDb(db, pName1);
78069    if( iDb>=0 ){
78070      analyzeDatabase(pParse, iDb);
78071    }else{
78072      z = sqlite3NameFromToken(db, pName1);
78073      if( z ){
78074        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
78075          analyzeTable(pParse, pIdx->pTable, pIdx);
78076        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
78077          analyzeTable(pParse, pTab, 0);
78078        }
78079        sqlite3DbFree(db, z);
78080      }
78081    }
78082  }else{
78083    /* Form 3: Analyze the fully qualified table name */
78084    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
78085    if( iDb>=0 ){
78086      zDb = db->aDb[iDb].zName;
78087      z = sqlite3NameFromToken(db, pTableName);
78088      if( z ){
78089        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
78090          analyzeTable(pParse, pIdx->pTable, pIdx);
78091        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
78092          analyzeTable(pParse, pTab, 0);
78093        }
78094        sqlite3DbFree(db, z);
78095      }
78096    }
78097  }
78098}
78099
78100/*
78101** Used to pass information from the analyzer reader through to the
78102** callback routine.
78103*/
78104typedef struct analysisInfo analysisInfo;
78105struct analysisInfo {
78106  sqlite3 *db;
78107  const char *zDatabase;
78108};
78109
78110/*
78111** This callback is invoked once for each index when reading the
78112** sqlite_stat1 table.
78113**
78114**     argv[0] = name of the table
78115**     argv[1] = name of the index (might be NULL)
78116**     argv[2] = results of analysis - on integer for each column
78117**
78118** Entries for which argv[1]==NULL simply record the number of rows in
78119** the table.
78120*/
78121static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
78122  analysisInfo *pInfo = (analysisInfo*)pData;
78123  Index *pIndex;
78124  Table *pTable;
78125  int i, c, n;
78126  unsigned int v;
78127  const char *z;
78128
78129  assert( argc==3 );
78130  UNUSED_PARAMETER2(NotUsed, argc);
78131
78132  if( argv==0 || argv[0]==0 || argv[2]==0 ){
78133    return 0;
78134  }
78135  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
78136  if( pTable==0 ){
78137    return 0;
78138  }
78139  if( argv[1] ){
78140    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
78141  }else{
78142    pIndex = 0;
78143  }
78144  n = pIndex ? pIndex->nColumn : 0;
78145  z = argv[2];
78146  for(i=0; *z && i<=n; i++){
78147    v = 0;
78148    while( (c=z[0])>='0' && c<='9' ){
78149      v = v*10 + c - '0';
78150      z++;
78151    }
78152    if( i==0 ) pTable->nRowEst = v;
78153    if( pIndex==0 ) break;
78154    pIndex->aiRowEst[i] = v;
78155    if( *z==' ' ) z++;
78156    if( memcmp(z, "unordered", 10)==0 ){
78157      pIndex->bUnordered = 1;
78158      break;
78159    }
78160  }
78161  return 0;
78162}
78163
78164/*
78165** If the Index.aSample variable is not NULL, delete the aSample[] array
78166** and its contents.
78167*/
78168SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
78169#ifdef SQLITE_ENABLE_STAT2
78170  if( pIdx->aSample ){
78171    int j;
78172    for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
78173      IndexSample *p = &pIdx->aSample[j];
78174      if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
78175        sqlite3DbFree(db, p->u.z);
78176      }
78177    }
78178    sqlite3DbFree(db, pIdx->aSample);
78179  }
78180#else
78181  UNUSED_PARAMETER(db);
78182  UNUSED_PARAMETER(pIdx);
78183#endif
78184}
78185
78186/*
78187** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
78188** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
78189** arrays. The contents of sqlite_stat2 are used to populate the
78190** Index.aSample[] arrays.
78191**
78192** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
78193** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
78194** during compilation and the sqlite_stat2 table is present, no data is
78195** read from it.
78196**
78197** If SQLITE_ENABLE_STAT2 was defined during compilation and the
78198** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
78199** returned. However, in this case, data is read from the sqlite_stat1
78200** table (if it is present) before returning.
78201**
78202** If an OOM error occurs, this function always sets db->mallocFailed.
78203** This means if the caller does not care about other errors, the return
78204** code may be ignored.
78205*/
78206SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
78207  analysisInfo sInfo;
78208  HashElem *i;
78209  char *zSql;
78210  int rc;
78211
78212  assert( iDb>=0 && iDb<db->nDb );
78213  assert( db->aDb[iDb].pBt!=0 );
78214
78215  /* Clear any prior statistics */
78216  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78217  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
78218    Index *pIdx = sqliteHashData(i);
78219    sqlite3DefaultRowEst(pIdx);
78220    sqlite3DeleteIndexSamples(db, pIdx);
78221    pIdx->aSample = 0;
78222  }
78223
78224  /* Check to make sure the sqlite_stat1 table exists */
78225  sInfo.db = db;
78226  sInfo.zDatabase = db->aDb[iDb].zName;
78227  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
78228    return SQLITE_ERROR;
78229  }
78230
78231  /* Load new statistics out of the sqlite_stat1 table */
78232  zSql = sqlite3MPrintf(db,
78233      "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78234  if( zSql==0 ){
78235    rc = SQLITE_NOMEM;
78236  }else{
78237    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
78238    sqlite3DbFree(db, zSql);
78239  }
78240
78241
78242  /* Load the statistics from the sqlite_stat2 table. */
78243#ifdef SQLITE_ENABLE_STAT2
78244  if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78245    rc = SQLITE_ERROR;
78246  }
78247  if( rc==SQLITE_OK ){
78248    sqlite3_stmt *pStmt = 0;
78249
78250    zSql = sqlite3MPrintf(db,
78251        "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78252    if( !zSql ){
78253      rc = SQLITE_NOMEM;
78254    }else{
78255      rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78256      sqlite3DbFree(db, zSql);
78257    }
78258
78259    if( rc==SQLITE_OK ){
78260      while( sqlite3_step(pStmt)==SQLITE_ROW ){
78261        char *zIndex;   /* Index name */
78262        Index *pIdx;    /* Pointer to the index object */
78263
78264        zIndex = (char *)sqlite3_column_text(pStmt, 0);
78265        pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78266        if( pIdx ){
78267          int iSample = sqlite3_column_int(pStmt, 1);
78268          if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78269            int eType = sqlite3_column_type(pStmt, 2);
78270
78271            if( pIdx->aSample==0 ){
78272              static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78273              pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78274              if( pIdx->aSample==0 ){
78275                db->mallocFailed = 1;
78276                break;
78277              }
78278	      memset(pIdx->aSample, 0, sz);
78279            }
78280
78281            assert( pIdx->aSample );
78282            {
78283              IndexSample *pSample = &pIdx->aSample[iSample];
78284              pSample->eType = (u8)eType;
78285              if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78286                pSample->u.r = sqlite3_column_double(pStmt, 2);
78287              }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78288                const char *z = (const char *)(
78289                    (eType==SQLITE_BLOB) ?
78290                    sqlite3_column_blob(pStmt, 2):
78291                    sqlite3_column_text(pStmt, 2)
78292                );
78293                int n = sqlite3_column_bytes(pStmt, 2);
78294                if( n>24 ){
78295                  n = 24;
78296                }
78297                pSample->nByte = (u8)n;
78298                if( n < 1){
78299                  pSample->u.z = 0;
78300                }else{
78301                  pSample->u.z = sqlite3DbStrNDup(0, z, n);
78302                  if( pSample->u.z==0 ){
78303                    db->mallocFailed = 1;
78304                    break;
78305                  }
78306                }
78307              }
78308            }
78309          }
78310        }
78311      }
78312      rc = sqlite3_finalize(pStmt);
78313    }
78314  }
78315#endif
78316
78317  if( rc==SQLITE_NOMEM ){
78318    db->mallocFailed = 1;
78319  }
78320  return rc;
78321}
78322
78323
78324#endif /* SQLITE_OMIT_ANALYZE */
78325
78326/************** End of analyze.c *********************************************/
78327/************** Begin file attach.c ******************************************/
78328/*
78329** 2003 April 6
78330**
78331** The author disclaims copyright to this source code.  In place of
78332** a legal notice, here is a blessing:
78333**
78334**    May you do good and not evil.
78335**    May you find forgiveness for yourself and forgive others.
78336**    May you share freely, never taking more than you give.
78337**
78338*************************************************************************
78339** This file contains code used to implement the ATTACH and DETACH commands.
78340*/
78341
78342#ifndef SQLITE_OMIT_ATTACH
78343/*
78344** Resolve an expression that was part of an ATTACH or DETACH statement. This
78345** is slightly different from resolving a normal SQL expression, because simple
78346** identifiers are treated as strings, not possible column names or aliases.
78347**
78348** i.e. if the parser sees:
78349**
78350**     ATTACH DATABASE abc AS def
78351**
78352** it treats the two expressions as literal strings 'abc' and 'def' instead of
78353** looking for columns of the same name.
78354**
78355** This only applies to the root node of pExpr, so the statement:
78356**
78357**     ATTACH DATABASE abc||def AS 'db2'
78358**
78359** will fail because neither abc or def can be resolved.
78360*/
78361static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
78362{
78363  int rc = SQLITE_OK;
78364  if( pExpr ){
78365    if( pExpr->op!=TK_ID ){
78366      rc = sqlite3ResolveExprNames(pName, pExpr);
78367      if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
78368        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
78369        return SQLITE_ERROR;
78370      }
78371    }else{
78372      pExpr->op = TK_STRING;
78373    }
78374  }
78375  return rc;
78376}
78377
78378/*
78379** An SQL user-function registered to do the work of an ATTACH statement. The
78380** three arguments to the function come directly from an attach statement:
78381**
78382**     ATTACH DATABASE x AS y KEY z
78383**
78384**     SELECT sqlite_attach(x, y, z)
78385**
78386** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
78387** third argument.
78388*/
78389static void attachFunc(
78390  sqlite3_context *context,
78391  int NotUsed,
78392  sqlite3_value **argv
78393){
78394  int i;
78395  int rc = 0;
78396  sqlite3 *db = sqlite3_context_db_handle(context);
78397  const char *zName;
78398  const char *zFile;
78399  char *zPath = 0;
78400  char *zErr = 0;
78401  unsigned int flags;
78402  Db *aNew;
78403  char *zErrDyn = 0;
78404  sqlite3_vfs *pVfs;
78405
78406  UNUSED_PARAMETER(NotUsed);
78407
78408  zFile = (const char *)sqlite3_value_text(argv[0]);
78409  zName = (const char *)sqlite3_value_text(argv[1]);
78410  if( zFile==0 ) zFile = "";
78411  if( zName==0 ) zName = "";
78412
78413  /* Check for the following errors:
78414  **
78415  **     * Too many attached databases,
78416  **     * Transaction currently open
78417  **     * Specified database name already being used.
78418  */
78419  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
78420    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
78421      db->aLimit[SQLITE_LIMIT_ATTACHED]
78422    );
78423    goto attach_error;
78424  }
78425  if( !db->autoCommit ){
78426    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
78427    goto attach_error;
78428  }
78429  for(i=0; i<db->nDb; i++){
78430    char *z = db->aDb[i].zName;
78431    assert( z && zName );
78432    if( sqlite3StrICmp(z, zName)==0 ){
78433      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
78434      goto attach_error;
78435    }
78436  }
78437
78438  /* Allocate the new entry in the db->aDb[] array and initialise the schema
78439  ** hash tables.
78440  */
78441  if( db->aDb==db->aDbStatic ){
78442    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
78443    if( aNew==0 ) return;
78444    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
78445  }else{
78446    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
78447    if( aNew==0 ) return;
78448  }
78449  db->aDb = aNew;
78450  aNew = &db->aDb[db->nDb];
78451  memset(aNew, 0, sizeof(*aNew));
78452
78453  /* Open the database file. If the btree is successfully opened, use
78454  ** it to obtain the database schema. At this point the schema may
78455  ** or may not be initialised.
78456  */
78457  flags = db->openFlags;
78458  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
78459  if( rc!=SQLITE_OK ){
78460    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
78461    sqlite3_result_error(context, zErr, -1);
78462    sqlite3_free(zErr);
78463    return;
78464  }
78465  assert( pVfs );
78466  flags |= SQLITE_OPEN_MAIN_DB;
78467  rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
78468  sqlite3_free( zPath );
78469  db->nDb++;
78470  if( rc==SQLITE_CONSTRAINT ){
78471    rc = SQLITE_ERROR;
78472    zErrDyn = sqlite3MPrintf(db, "database is already attached");
78473  }else if( rc==SQLITE_OK ){
78474    Pager *pPager;
78475    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
78476    if( !aNew->pSchema ){
78477      rc = SQLITE_NOMEM;
78478    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
78479      zErrDyn = sqlite3MPrintf(db,
78480        "attached databases must use the same text encoding as main database");
78481      rc = SQLITE_ERROR;
78482    }
78483    pPager = sqlite3BtreePager(aNew->pBt);
78484    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
78485    sqlite3BtreeSecureDelete(aNew->pBt,
78486                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
78487  }
78488  aNew->safety_level = 3;
78489  aNew->zName = sqlite3DbStrDup(db, zName);
78490  if( rc==SQLITE_OK && aNew->zName==0 ){
78491    rc = SQLITE_NOMEM;
78492  }
78493
78494
78495#ifdef SQLITE_HAS_CODEC
78496  if( rc==SQLITE_OK ){
78497    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
78498    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
78499    int nKey;
78500    char *zKey;
78501    int t = sqlite3_value_type(argv[2]);
78502    switch( t ){
78503      case SQLITE_INTEGER:
78504      case SQLITE_FLOAT:
78505        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
78506        rc = SQLITE_ERROR;
78507        break;
78508
78509      case SQLITE_TEXT:
78510      case SQLITE_BLOB:
78511        nKey = sqlite3_value_bytes(argv[2]);
78512        zKey = (char *)sqlite3_value_blob(argv[2]);
78513        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
78514        break;
78515
78516      case SQLITE_NULL:
78517        /* No key specified.  Use the key from the main database */
78518        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
78519        if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
78520          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
78521        }
78522        break;
78523    }
78524  }
78525#endif
78526
78527  /* If the file was opened successfully, read the schema for the new database.
78528  ** If this fails, or if opening the file failed, then close the file and
78529  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
78530  ** we found it.
78531  */
78532  if( rc==SQLITE_OK ){
78533    sqlite3BtreeEnterAll(db);
78534    rc = sqlite3Init(db, &zErrDyn);
78535    sqlite3BtreeLeaveAll(db);
78536  }
78537  if( rc ){
78538    int iDb = db->nDb - 1;
78539    assert( iDb>=2 );
78540    if( db->aDb[iDb].pBt ){
78541      sqlite3BtreeClose(db->aDb[iDb].pBt);
78542      db->aDb[iDb].pBt = 0;
78543      db->aDb[iDb].pSchema = 0;
78544    }
78545    sqlite3ResetInternalSchema(db, -1);
78546    db->nDb = iDb;
78547    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
78548      db->mallocFailed = 1;
78549      sqlite3DbFree(db, zErrDyn);
78550      zErrDyn = sqlite3MPrintf(db, "out of memory");
78551    }else if( zErrDyn==0 ){
78552      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
78553    }
78554    goto attach_error;
78555  }
78556
78557  return;
78558
78559attach_error:
78560  /* Return an error if we get here */
78561  if( zErrDyn ){
78562    sqlite3_result_error(context, zErrDyn, -1);
78563    sqlite3DbFree(db, zErrDyn);
78564  }
78565  if( rc ) sqlite3_result_error_code(context, rc);
78566}
78567
78568/*
78569** An SQL user-function registered to do the work of an DETACH statement. The
78570** three arguments to the function come directly from a detach statement:
78571**
78572**     DETACH DATABASE x
78573**
78574**     SELECT sqlite_detach(x)
78575*/
78576static void detachFunc(
78577  sqlite3_context *context,
78578  int NotUsed,
78579  sqlite3_value **argv
78580){
78581  const char *zName = (const char *)sqlite3_value_text(argv[0]);
78582  sqlite3 *db = sqlite3_context_db_handle(context);
78583  int i;
78584  Db *pDb = 0;
78585  char zErr[128];
78586
78587  UNUSED_PARAMETER(NotUsed);
78588
78589  if( zName==0 ) zName = "";
78590  for(i=0; i<db->nDb; i++){
78591    pDb = &db->aDb[i];
78592    if( pDb->pBt==0 ) continue;
78593    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
78594  }
78595
78596  if( i>=db->nDb ){
78597    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
78598    goto detach_error;
78599  }
78600  if( i<2 ){
78601    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
78602    goto detach_error;
78603  }
78604  if( !db->autoCommit ){
78605    sqlite3_snprintf(sizeof(zErr), zErr,
78606                     "cannot DETACH database within transaction");
78607    goto detach_error;
78608  }
78609  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
78610    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
78611    goto detach_error;
78612  }
78613
78614  sqlite3BtreeClose(pDb->pBt);
78615  pDb->pBt = 0;
78616  pDb->pSchema = 0;
78617  sqlite3ResetInternalSchema(db, -1);
78618  return;
78619
78620detach_error:
78621  sqlite3_result_error(context, zErr, -1);
78622}
78623
78624/*
78625** This procedure generates VDBE code for a single invocation of either the
78626** sqlite_detach() or sqlite_attach() SQL user functions.
78627*/
78628static void codeAttach(
78629  Parse *pParse,       /* The parser context */
78630  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
78631  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
78632  Expr *pAuthArg,      /* Expression to pass to authorization callback */
78633  Expr *pFilename,     /* Name of database file */
78634  Expr *pDbname,       /* Name of the database to use internally */
78635  Expr *pKey           /* Database key for encryption extension */
78636){
78637  int rc;
78638  NameContext sName;
78639  Vdbe *v;
78640  sqlite3* db = pParse->db;
78641  int regArgs;
78642
78643  memset(&sName, 0, sizeof(NameContext));
78644  sName.pParse = pParse;
78645
78646  if(
78647      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
78648      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
78649      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
78650  ){
78651    pParse->nErr++;
78652    goto attach_end;
78653  }
78654
78655#ifndef SQLITE_OMIT_AUTHORIZATION
78656  if( pAuthArg ){
78657    char *zAuthArg;
78658    if( pAuthArg->op==TK_STRING ){
78659      zAuthArg = pAuthArg->u.zToken;
78660    }else{
78661      zAuthArg = 0;
78662    }
78663    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
78664    if(rc!=SQLITE_OK ){
78665      goto attach_end;
78666    }
78667  }
78668#endif /* SQLITE_OMIT_AUTHORIZATION */
78669
78670
78671  v = sqlite3GetVdbe(pParse);
78672  regArgs = sqlite3GetTempRange(pParse, 4);
78673  sqlite3ExprCode(pParse, pFilename, regArgs);
78674  sqlite3ExprCode(pParse, pDbname, regArgs+1);
78675  sqlite3ExprCode(pParse, pKey, regArgs+2);
78676
78677  assert( v || db->mallocFailed );
78678  if( v ){
78679    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
78680    assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
78681    sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
78682    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
78683
78684    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
78685    ** statement only). For DETACH, set it to false (expire all existing
78686    ** statements).
78687    */
78688    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
78689  }
78690
78691attach_end:
78692  sqlite3ExprDelete(db, pFilename);
78693  sqlite3ExprDelete(db, pDbname);
78694  sqlite3ExprDelete(db, pKey);
78695}
78696
78697/*
78698** Called by the parser to compile a DETACH statement.
78699**
78700**     DETACH pDbname
78701*/
78702SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
78703  static const FuncDef detach_func = {
78704    1,                /* nArg */
78705    SQLITE_UTF8,      /* iPrefEnc */
78706    0,                /* flags */
78707    0,                /* pUserData */
78708    0,                /* pNext */
78709    detachFunc,       /* xFunc */
78710    0,                /* xStep */
78711    0,                /* xFinalize */
78712    "sqlite_detach",  /* zName */
78713    0,                /* pHash */
78714    0                 /* pDestructor */
78715  };
78716  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
78717}
78718
78719/*
78720** Called by the parser to compile an ATTACH statement.
78721**
78722**     ATTACH p AS pDbname KEY pKey
78723*/
78724SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
78725  static const FuncDef attach_func = {
78726    3,                /* nArg */
78727    SQLITE_UTF8,      /* iPrefEnc */
78728    0,                /* flags */
78729    0,                /* pUserData */
78730    0,                /* pNext */
78731    attachFunc,       /* xFunc */
78732    0,                /* xStep */
78733    0,                /* xFinalize */
78734    "sqlite_attach",  /* zName */
78735    0,                /* pHash */
78736    0                 /* pDestructor */
78737  };
78738  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
78739}
78740#endif /* SQLITE_OMIT_ATTACH */
78741
78742/*
78743** Initialize a DbFixer structure.  This routine must be called prior
78744** to passing the structure to one of the sqliteFixAAAA() routines below.
78745**
78746** The return value indicates whether or not fixation is required.  TRUE
78747** means we do need to fix the database references, FALSE means we do not.
78748*/
78749SQLITE_PRIVATE int sqlite3FixInit(
78750  DbFixer *pFix,      /* The fixer to be initialized */
78751  Parse *pParse,      /* Error messages will be written here */
78752  int iDb,            /* This is the database that must be used */
78753  const char *zType,  /* "view", "trigger", or "index" */
78754  const Token *pName  /* Name of the view, trigger, or index */
78755){
78756  sqlite3 *db;
78757
78758  if( NEVER(iDb<0) || iDb==1 ) return 0;
78759  db = pParse->db;
78760  assert( db->nDb>iDb );
78761  pFix->pParse = pParse;
78762  pFix->zDb = db->aDb[iDb].zName;
78763  pFix->zType = zType;
78764  pFix->pName = pName;
78765  return 1;
78766}
78767
78768/*
78769** The following set of routines walk through the parse tree and assign
78770** a specific database to all table references where the database name
78771** was left unspecified in the original SQL statement.  The pFix structure
78772** must have been initialized by a prior call to sqlite3FixInit().
78773**
78774** These routines are used to make sure that an index, trigger, or
78775** view in one database does not refer to objects in a different database.
78776** (Exception: indices, triggers, and views in the TEMP database are
78777** allowed to refer to anything.)  If a reference is explicitly made
78778** to an object in a different database, an error message is added to
78779** pParse->zErrMsg and these routines return non-zero.  If everything
78780** checks out, these routines return 0.
78781*/
78782SQLITE_PRIVATE int sqlite3FixSrcList(
78783  DbFixer *pFix,       /* Context of the fixation */
78784  SrcList *pList       /* The Source list to check and modify */
78785){
78786  int i;
78787  const char *zDb;
78788  struct SrcList_item *pItem;
78789
78790  if( NEVER(pList==0) ) return 0;
78791  zDb = pFix->zDb;
78792  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
78793    if( pItem->zDatabase==0 ){
78794      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
78795    }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
78796      sqlite3ErrorMsg(pFix->pParse,
78797         "%s %T cannot reference objects in database %s",
78798         pFix->zType, pFix->pName, pItem->zDatabase);
78799      return 1;
78800    }
78801#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
78802    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
78803    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
78804#endif
78805  }
78806  return 0;
78807}
78808#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
78809SQLITE_PRIVATE int sqlite3FixSelect(
78810  DbFixer *pFix,       /* Context of the fixation */
78811  Select *pSelect      /* The SELECT statement to be fixed to one database */
78812){
78813  while( pSelect ){
78814    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
78815      return 1;
78816    }
78817    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
78818      return 1;
78819    }
78820    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
78821      return 1;
78822    }
78823    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
78824      return 1;
78825    }
78826    pSelect = pSelect->pPrior;
78827  }
78828  return 0;
78829}
78830SQLITE_PRIVATE int sqlite3FixExpr(
78831  DbFixer *pFix,     /* Context of the fixation */
78832  Expr *pExpr        /* The expression to be fixed to one database */
78833){
78834  while( pExpr ){
78835    if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
78836    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
78837      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
78838    }else{
78839      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
78840    }
78841    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
78842      return 1;
78843    }
78844    pExpr = pExpr->pLeft;
78845  }
78846  return 0;
78847}
78848SQLITE_PRIVATE int sqlite3FixExprList(
78849  DbFixer *pFix,     /* Context of the fixation */
78850  ExprList *pList    /* The expression to be fixed to one database */
78851){
78852  int i;
78853  struct ExprList_item *pItem;
78854  if( pList==0 ) return 0;
78855  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
78856    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
78857      return 1;
78858    }
78859  }
78860  return 0;
78861}
78862#endif
78863
78864#ifndef SQLITE_OMIT_TRIGGER
78865SQLITE_PRIVATE int sqlite3FixTriggerStep(
78866  DbFixer *pFix,     /* Context of the fixation */
78867  TriggerStep *pStep /* The trigger step be fixed to one database */
78868){
78869  while( pStep ){
78870    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
78871      return 1;
78872    }
78873    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
78874      return 1;
78875    }
78876    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
78877      return 1;
78878    }
78879    pStep = pStep->pNext;
78880  }
78881  return 0;
78882}
78883#endif
78884
78885/************** End of attach.c **********************************************/
78886/************** Begin file auth.c ********************************************/
78887/*
78888** 2003 January 11
78889**
78890** The author disclaims copyright to this source code.  In place of
78891** a legal notice, here is a blessing:
78892**
78893**    May you do good and not evil.
78894**    May you find forgiveness for yourself and forgive others.
78895**    May you share freely, never taking more than you give.
78896**
78897*************************************************************************
78898** This file contains code used to implement the sqlite3_set_authorizer()
78899** API.  This facility is an optional feature of the library.  Embedded
78900** systems that do not need this facility may omit it by recompiling
78901** the library with -DSQLITE_OMIT_AUTHORIZATION=1
78902*/
78903
78904/*
78905** All of the code in this file may be omitted by defining a single
78906** macro.
78907*/
78908#ifndef SQLITE_OMIT_AUTHORIZATION
78909
78910/*
78911** Set or clear the access authorization function.
78912**
78913** The access authorization function is be called during the compilation
78914** phase to verify that the user has read and/or write access permission on
78915** various fields of the database.  The first argument to the auth function
78916** is a copy of the 3rd argument to this routine.  The second argument
78917** to the auth function is one of these constants:
78918**
78919**       SQLITE_CREATE_INDEX
78920**       SQLITE_CREATE_TABLE
78921**       SQLITE_CREATE_TEMP_INDEX
78922**       SQLITE_CREATE_TEMP_TABLE
78923**       SQLITE_CREATE_TEMP_TRIGGER
78924**       SQLITE_CREATE_TEMP_VIEW
78925**       SQLITE_CREATE_TRIGGER
78926**       SQLITE_CREATE_VIEW
78927**       SQLITE_DELETE
78928**       SQLITE_DROP_INDEX
78929**       SQLITE_DROP_TABLE
78930**       SQLITE_DROP_TEMP_INDEX
78931**       SQLITE_DROP_TEMP_TABLE
78932**       SQLITE_DROP_TEMP_TRIGGER
78933**       SQLITE_DROP_TEMP_VIEW
78934**       SQLITE_DROP_TRIGGER
78935**       SQLITE_DROP_VIEW
78936**       SQLITE_INSERT
78937**       SQLITE_PRAGMA
78938**       SQLITE_READ
78939**       SQLITE_SELECT
78940**       SQLITE_TRANSACTION
78941**       SQLITE_UPDATE
78942**
78943** The third and fourth arguments to the auth function are the name of
78944** the table and the column that are being accessed.  The auth function
78945** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
78946** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
78947** means that the SQL statement will never-run - the sqlite3_exec() call
78948** will return with an error.  SQLITE_IGNORE means that the SQL statement
78949** should run but attempts to read the specified column will return NULL
78950** and attempts to write the column will be ignored.
78951**
78952** Setting the auth function to NULL disables this hook.  The default
78953** setting of the auth function is NULL.
78954*/
78955SQLITE_API int sqlite3_set_authorizer(
78956  sqlite3 *db,
78957  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
78958  void *pArg
78959){
78960  sqlite3_mutex_enter(db->mutex);
78961  db->xAuth = xAuth;
78962  db->pAuthArg = pArg;
78963  sqlite3ExpirePreparedStatements(db);
78964  sqlite3_mutex_leave(db->mutex);
78965  return SQLITE_OK;
78966}
78967
78968/*
78969** Write an error message into pParse->zErrMsg that explains that the
78970** user-supplied authorization function returned an illegal value.
78971*/
78972static void sqliteAuthBadReturnCode(Parse *pParse){
78973  sqlite3ErrorMsg(pParse, "authorizer malfunction");
78974  pParse->rc = SQLITE_ERROR;
78975}
78976
78977/*
78978** Invoke the authorization callback for permission to read column zCol from
78979** table zTab in database zDb. This function assumes that an authorization
78980** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
78981**
78982** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
78983** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
78984** is treated as SQLITE_DENY. In this case an error is left in pParse.
78985*/
78986SQLITE_PRIVATE int sqlite3AuthReadCol(
78987  Parse *pParse,                  /* The parser context */
78988  const char *zTab,               /* Table name */
78989  const char *zCol,               /* Column name */
78990  int iDb                         /* Index of containing database. */
78991){
78992  sqlite3 *db = pParse->db;       /* Database handle */
78993  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
78994  int rc;                         /* Auth callback return code */
78995
78996  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
78997  if( rc==SQLITE_DENY ){
78998    if( db->nDb>2 || iDb!=0 ){
78999      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
79000    }else{
79001      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
79002    }
79003    pParse->rc = SQLITE_AUTH;
79004  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
79005    sqliteAuthBadReturnCode(pParse);
79006  }
79007  return rc;
79008}
79009
79010/*
79011** The pExpr should be a TK_COLUMN expression.  The table referred to
79012** is in pTabList or else it is the NEW or OLD table of a trigger.
79013** Check to see if it is OK to read this particular column.
79014**
79015** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
79016** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
79017** then generate an error.
79018*/
79019SQLITE_PRIVATE void sqlite3AuthRead(
79020  Parse *pParse,        /* The parser context */
79021  Expr *pExpr,          /* The expression to check authorization on */
79022  Schema *pSchema,      /* The schema of the expression */
79023  SrcList *pTabList     /* All table that pExpr might refer to */
79024){
79025  sqlite3 *db = pParse->db;
79026  Table *pTab = 0;      /* The table being read */
79027  const char *zCol;     /* Name of the column of the table */
79028  int iSrc;             /* Index in pTabList->a[] of table being read */
79029  int iDb;              /* The index of the database the expression refers to */
79030  int iCol;             /* Index of column in table */
79031
79032  if( db->xAuth==0 ) return;
79033  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
79034  if( iDb<0 ){
79035    /* An attempt to read a column out of a subquery or other
79036    ** temporary table. */
79037    return;
79038  }
79039
79040  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
79041  if( pExpr->op==TK_TRIGGER ){
79042    pTab = pParse->pTriggerTab;
79043  }else{
79044    assert( pTabList );
79045    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
79046      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
79047        pTab = pTabList->a[iSrc].pTab;
79048        break;
79049      }
79050    }
79051  }
79052  iCol = pExpr->iColumn;
79053  if( NEVER(pTab==0) ) return;
79054
79055  if( iCol>=0 ){
79056    assert( iCol<pTab->nCol );
79057    zCol = pTab->aCol[iCol].zName;
79058  }else if( pTab->iPKey>=0 ){
79059    assert( pTab->iPKey<pTab->nCol );
79060    zCol = pTab->aCol[pTab->iPKey].zName;
79061  }else{
79062    zCol = "ROWID";
79063  }
79064  assert( iDb>=0 && iDb<db->nDb );
79065  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
79066    pExpr->op = TK_NULL;
79067  }
79068}
79069
79070/*
79071** Do an authorization check using the code and arguments given.  Return
79072** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
79073** is returned, then the error count and error message in pParse are
79074** modified appropriately.
79075*/
79076SQLITE_PRIVATE int sqlite3AuthCheck(
79077  Parse *pParse,
79078  int code,
79079  const char *zArg1,
79080  const char *zArg2,
79081  const char *zArg3
79082){
79083  sqlite3 *db = pParse->db;
79084  int rc;
79085
79086  /* Don't do any authorization checks if the database is initialising
79087  ** or if the parser is being invoked from within sqlite3_declare_vtab.
79088  */
79089  if( db->init.busy || IN_DECLARE_VTAB ){
79090    return SQLITE_OK;
79091  }
79092
79093  if( db->xAuth==0 ){
79094    return SQLITE_OK;
79095  }
79096  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
79097  if( rc==SQLITE_DENY ){
79098    sqlite3ErrorMsg(pParse, "not authorized");
79099    pParse->rc = SQLITE_AUTH;
79100  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
79101    rc = SQLITE_DENY;
79102    sqliteAuthBadReturnCode(pParse);
79103  }
79104  return rc;
79105}
79106
79107/*
79108** Push an authorization context.  After this routine is called, the
79109** zArg3 argument to authorization callbacks will be zContext until
79110** popped.  Or if pParse==0, this routine is a no-op.
79111*/
79112SQLITE_PRIVATE void sqlite3AuthContextPush(
79113  Parse *pParse,
79114  AuthContext *pContext,
79115  const char *zContext
79116){
79117  assert( pParse );
79118  pContext->pParse = pParse;
79119  pContext->zAuthContext = pParse->zAuthContext;
79120  pParse->zAuthContext = zContext;
79121}
79122
79123/*
79124** Pop an authorization context that was previously pushed
79125** by sqlite3AuthContextPush
79126*/
79127SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
79128  if( pContext->pParse ){
79129    pContext->pParse->zAuthContext = pContext->zAuthContext;
79130    pContext->pParse = 0;
79131  }
79132}
79133
79134#endif /* SQLITE_OMIT_AUTHORIZATION */
79135
79136/************** End of auth.c ************************************************/
79137/************** Begin file build.c *******************************************/
79138/*
79139** 2001 September 15
79140**
79141** The author disclaims copyright to this source code.  In place of
79142** a legal notice, here is a blessing:
79143**
79144**    May you do good and not evil.
79145**    May you find forgiveness for yourself and forgive others.
79146**    May you share freely, never taking more than you give.
79147**
79148*************************************************************************
79149** This file contains C code routines that are called by the SQLite parser
79150** when syntax rules are reduced.  The routines in this file handle the
79151** following kinds of SQL syntax:
79152**
79153**     CREATE TABLE
79154**     DROP TABLE
79155**     CREATE INDEX
79156**     DROP INDEX
79157**     creating ID lists
79158**     BEGIN TRANSACTION
79159**     COMMIT
79160**     ROLLBACK
79161*/
79162
79163/*
79164** This routine is called when a new SQL statement is beginning to
79165** be parsed.  Initialize the pParse structure as needed.
79166*/
79167SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
79168  pParse->explain = (u8)explainFlag;
79169  pParse->nVar = 0;
79170}
79171
79172#ifndef SQLITE_OMIT_SHARED_CACHE
79173/*
79174** The TableLock structure is only used by the sqlite3TableLock() and
79175** codeTableLocks() functions.
79176*/
79177struct TableLock {
79178  int iDb;             /* The database containing the table to be locked */
79179  int iTab;            /* The root page of the table to be locked */
79180  u8 isWriteLock;      /* True for write lock.  False for a read lock */
79181  const char *zName;   /* Name of the table */
79182};
79183
79184/*
79185** Record the fact that we want to lock a table at run-time.
79186**
79187** The table to be locked has root page iTab and is found in database iDb.
79188** A read or a write lock can be taken depending on isWritelock.
79189**
79190** This routine just records the fact that the lock is desired.  The
79191** code to make the lock occur is generated by a later call to
79192** codeTableLocks() which occurs during sqlite3FinishCoding().
79193*/
79194SQLITE_PRIVATE void sqlite3TableLock(
79195  Parse *pParse,     /* Parsing context */
79196  int iDb,           /* Index of the database containing the table to lock */
79197  int iTab,          /* Root page number of the table to be locked */
79198  u8 isWriteLock,    /* True for a write lock */
79199  const char *zName  /* Name of the table to be locked */
79200){
79201  Parse *pToplevel = sqlite3ParseToplevel(pParse);
79202  int i;
79203  int nBytes;
79204  TableLock *p;
79205  assert( iDb>=0 );
79206
79207  for(i=0; i<pToplevel->nTableLock; i++){
79208    p = &pToplevel->aTableLock[i];
79209    if( p->iDb==iDb && p->iTab==iTab ){
79210      p->isWriteLock = (p->isWriteLock || isWriteLock);
79211      return;
79212    }
79213  }
79214
79215  nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
79216  pToplevel->aTableLock =
79217      sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
79218  if( pToplevel->aTableLock ){
79219    p = &pToplevel->aTableLock[pToplevel->nTableLock++];
79220    p->iDb = iDb;
79221    p->iTab = iTab;
79222    p->isWriteLock = isWriteLock;
79223    p->zName = zName;
79224  }else{
79225    pToplevel->nTableLock = 0;
79226    pToplevel->db->mallocFailed = 1;
79227  }
79228}
79229
79230/*
79231** Code an OP_TableLock instruction for each table locked by the
79232** statement (configured by calls to sqlite3TableLock()).
79233*/
79234static void codeTableLocks(Parse *pParse){
79235  int i;
79236  Vdbe *pVdbe;
79237
79238  pVdbe = sqlite3GetVdbe(pParse);
79239  assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
79240
79241  for(i=0; i<pParse->nTableLock; i++){
79242    TableLock *p = &pParse->aTableLock[i];
79243    int p1 = p->iDb;
79244    sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
79245                      p->zName, P4_STATIC);
79246  }
79247}
79248#else
79249  #define codeTableLocks(x)
79250#endif
79251
79252/*
79253** This routine is called after a single SQL statement has been
79254** parsed and a VDBE program to execute that statement has been
79255** prepared.  This routine puts the finishing touches on the
79256** VDBE program and resets the pParse structure for the next
79257** parse.
79258**
79259** Note that if an error occurred, it might be the case that
79260** no VDBE code was generated.
79261*/
79262SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
79263  sqlite3 *db;
79264  Vdbe *v;
79265
79266  db = pParse->db;
79267  if( db->mallocFailed ) return;
79268  if( pParse->nested ) return;
79269  if( pParse->nErr ) return;
79270
79271  /* Begin by generating some termination code at the end of the
79272  ** vdbe program
79273  */
79274  v = sqlite3GetVdbe(pParse);
79275  assert( !pParse->isMultiWrite
79276       || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
79277  if( v ){
79278    sqlite3VdbeAddOp0(v, OP_Halt);
79279
79280    /* The cookie mask contains one bit for each database file open.
79281    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
79282    ** set for each database that is used.  Generate code to start a
79283    ** transaction on each used database and to verify the schema cookie
79284    ** on each used database.
79285    */
79286    if( pParse->cookieGoto>0 ){
79287      yDbMask mask;
79288      int iDb;
79289      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
79290      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
79291        if( (mask & pParse->cookieMask)==0 ) continue;
79292        sqlite3VdbeUsesBtree(v, iDb);
79293        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
79294        if( db->init.busy==0 ){
79295          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79296          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
79297                            iDb, pParse->cookieValue[iDb],
79298                            db->aDb[iDb].pSchema->iGeneration);
79299        }
79300      }
79301#ifndef SQLITE_OMIT_VIRTUALTABLE
79302      {
79303        int i;
79304        for(i=0; i<pParse->nVtabLock; i++){
79305          char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
79306          sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
79307        }
79308        pParse->nVtabLock = 0;
79309      }
79310#endif
79311
79312      /* Once all the cookies have been verified and transactions opened,
79313      ** obtain the required table-locks. This is a no-op unless the
79314      ** shared-cache feature is enabled.
79315      */
79316      codeTableLocks(pParse);
79317
79318      /* Initialize any AUTOINCREMENT data structures required.
79319      */
79320      sqlite3AutoincrementBegin(pParse);
79321
79322      /* Finally, jump back to the beginning of the executable code. */
79323      sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
79324    }
79325  }
79326
79327
79328  /* Get the VDBE program ready for execution
79329  */
79330  if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
79331#ifdef SQLITE_DEBUG
79332    FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
79333    sqlite3VdbeTrace(v, trace);
79334#endif
79335    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
79336    /* A minimum of one cursor is required if autoincrement is used
79337    *  See ticket [a696379c1f08866] */
79338    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
79339    sqlite3VdbeMakeReady(v, pParse);
79340    pParse->rc = SQLITE_DONE;
79341    pParse->colNamesSet = 0;
79342  }else{
79343    pParse->rc = SQLITE_ERROR;
79344  }
79345  pParse->nTab = 0;
79346  pParse->nMem = 0;
79347  pParse->nSet = 0;
79348  pParse->nVar = 0;
79349  pParse->cookieMask = 0;
79350  pParse->cookieGoto = 0;
79351}
79352
79353/*
79354** Run the parser and code generator recursively in order to generate
79355** code for the SQL statement given onto the end of the pParse context
79356** currently under construction.  When the parser is run recursively
79357** this way, the final OP_Halt is not appended and other initialization
79358** and finalization steps are omitted because those are handling by the
79359** outermost parser.
79360**
79361** Not everything is nestable.  This facility is designed to permit
79362** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
79363** care if you decide to try to use this routine for some other purposes.
79364*/
79365SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
79366  va_list ap;
79367  char *zSql;
79368  char *zErrMsg = 0;
79369  sqlite3 *db = pParse->db;
79370# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
79371  char saveBuf[SAVE_SZ];
79372
79373  if( pParse->nErr ) return;
79374  assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
79375  va_start(ap, zFormat);
79376  zSql = sqlite3VMPrintf(db, zFormat, ap);
79377  va_end(ap);
79378  if( zSql==0 ){
79379    return;   /* A malloc must have failed */
79380  }
79381  pParse->nested++;
79382  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
79383  memset(&pParse->nVar, 0, SAVE_SZ);
79384  sqlite3RunParser(pParse, zSql, &zErrMsg);
79385  sqlite3DbFree(db, zErrMsg);
79386  sqlite3DbFree(db, zSql);
79387  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
79388  pParse->nested--;
79389}
79390
79391/*
79392** Locate the in-memory structure that describes a particular database
79393** table given the name of that table and (optionally) the name of the
79394** database containing the table.  Return NULL if not found.
79395**
79396** If zDatabase is 0, all databases are searched for the table and the
79397** first matching table is returned.  (No checking for duplicate table
79398** names is done.)  The search order is TEMP first, then MAIN, then any
79399** auxiliary databases added using the ATTACH command.
79400**
79401** See also sqlite3LocateTable().
79402*/
79403SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
79404  Table *p = 0;
79405  int i;
79406  int nName;
79407  assert( zName!=0 );
79408  nName = sqlite3Strlen30(zName);
79409  /* All mutexes are required for schema access.  Make sure we hold them. */
79410  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
79411  for(i=OMIT_TEMPDB; i<db->nDb; i++){
79412    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
79413    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
79414    assert( sqlite3SchemaMutexHeld(db, j, 0) );
79415    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
79416    if( p ) break;
79417  }
79418  return p;
79419}
79420
79421/*
79422** Locate the in-memory structure that describes a particular database
79423** table given the name of that table and (optionally) the name of the
79424** database containing the table.  Return NULL if not found.  Also leave an
79425** error message in pParse->zErrMsg.
79426**
79427** The difference between this routine and sqlite3FindTable() is that this
79428** routine leaves an error message in pParse->zErrMsg where
79429** sqlite3FindTable() does not.
79430*/
79431SQLITE_PRIVATE Table *sqlite3LocateTable(
79432  Parse *pParse,         /* context in which to report errors */
79433  int isView,            /* True if looking for a VIEW rather than a TABLE */
79434  const char *zName,     /* Name of the table we are looking for */
79435  const char *zDbase     /* Name of the database.  Might be NULL */
79436){
79437  Table *p;
79438
79439  /* Read the database schema. If an error occurs, leave an error message
79440  ** and code in pParse and return NULL. */
79441  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
79442    return 0;
79443  }
79444
79445  p = sqlite3FindTable(pParse->db, zName, zDbase);
79446  if( p==0 ){
79447    const char *zMsg = isView ? "no such view" : "no such table";
79448    if( zDbase ){
79449      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
79450    }else{
79451      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
79452    }
79453    pParse->checkSchema = 1;
79454  }
79455  return p;
79456}
79457
79458/*
79459** Locate the in-memory structure that describes
79460** a particular index given the name of that index
79461** and the name of the database that contains the index.
79462** Return NULL if not found.
79463**
79464** If zDatabase is 0, all databases are searched for the
79465** table and the first matching index is returned.  (No checking
79466** for duplicate index names is done.)  The search order is
79467** TEMP first, then MAIN, then any auxiliary databases added
79468** using the ATTACH command.
79469*/
79470SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
79471  Index *p = 0;
79472  int i;
79473  int nName = sqlite3Strlen30(zName);
79474  /* All mutexes are required for schema access.  Make sure we hold them. */
79475  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
79476  for(i=OMIT_TEMPDB; i<db->nDb; i++){
79477    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
79478    Schema *pSchema = db->aDb[j].pSchema;
79479    assert( pSchema );
79480    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
79481    assert( sqlite3SchemaMutexHeld(db, j, 0) );
79482    p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
79483    if( p ) break;
79484  }
79485  return p;
79486}
79487
79488/*
79489** Reclaim the memory used by an index
79490*/
79491static void freeIndex(sqlite3 *db, Index *p){
79492#ifndef SQLITE_OMIT_ANALYZE
79493  sqlite3DeleteIndexSamples(db, p);
79494#endif
79495  sqlite3DbFree(db, p->zColAff);
79496  sqlite3DbFree(db, p);
79497}
79498
79499/*
79500** For the index called zIdxName which is found in the database iDb,
79501** unlike that index from its Table then remove the index from
79502** the index hash table and free all memory structures associated
79503** with the index.
79504*/
79505SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
79506  Index *pIndex;
79507  int len;
79508  Hash *pHash;
79509
79510  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79511  pHash = &db->aDb[iDb].pSchema->idxHash;
79512  len = sqlite3Strlen30(zIdxName);
79513  pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
79514  if( ALWAYS(pIndex) ){
79515    if( pIndex->pTable->pIndex==pIndex ){
79516      pIndex->pTable->pIndex = pIndex->pNext;
79517    }else{
79518      Index *p;
79519      /* Justification of ALWAYS();  The index must be on the list of
79520      ** indices. */
79521      p = pIndex->pTable->pIndex;
79522      while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
79523      if( ALWAYS(p && p->pNext==pIndex) ){
79524        p->pNext = pIndex->pNext;
79525      }
79526    }
79527    freeIndex(db, pIndex);
79528  }
79529  db->flags |= SQLITE_InternChanges;
79530}
79531
79532/*
79533** Erase all schema information from the in-memory hash tables of
79534** a single database.  This routine is called to reclaim memory
79535** before the database closes.  It is also called during a rollback
79536** if there were schema changes during the transaction or if a
79537** schema-cookie mismatch occurs.
79538**
79539** If iDb<0 then reset the internal schema tables for all database
79540** files.  If iDb>=0 then reset the internal schema for only the
79541** single file indicated.
79542*/
79543SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
79544  int i, j;
79545  assert( iDb<db->nDb );
79546
79547  if( iDb>=0 ){
79548    /* Case 1:  Reset the single schema identified by iDb */
79549    Db *pDb = &db->aDb[iDb];
79550    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79551    assert( pDb->pSchema!=0 );
79552    sqlite3SchemaClear(pDb->pSchema);
79553
79554    /* If any database other than TEMP is reset, then also reset TEMP
79555    ** since TEMP might be holding triggers that reference tables in the
79556    ** other database.
79557    */
79558    if( iDb!=1 ){
79559      pDb = &db->aDb[1];
79560      assert( pDb->pSchema!=0 );
79561      sqlite3SchemaClear(pDb->pSchema);
79562    }
79563    return;
79564  }
79565  /* Case 2 (from here to the end): Reset all schemas for all attached
79566  ** databases. */
79567  assert( iDb<0 );
79568  sqlite3BtreeEnterAll(db);
79569  for(i=0; i<db->nDb; i++){
79570    Db *pDb = &db->aDb[i];
79571    if( pDb->pSchema ){
79572      sqlite3SchemaClear(pDb->pSchema);
79573    }
79574  }
79575  db->flags &= ~SQLITE_InternChanges;
79576  sqlite3VtabUnlockList(db);
79577  sqlite3BtreeLeaveAll(db);
79578
79579  /* If one or more of the auxiliary database files has been closed,
79580  ** then remove them from the auxiliary database list.  We take the
79581  ** opportunity to do this here since we have just deleted all of the
79582  ** schema hash tables and therefore do not have to make any changes
79583  ** to any of those tables.
79584  */
79585  for(i=j=2; i<db->nDb; i++){
79586    struct Db *pDb = &db->aDb[i];
79587    if( pDb->pBt==0 ){
79588      sqlite3DbFree(db, pDb->zName);
79589      pDb->zName = 0;
79590      continue;
79591    }
79592    if( j<i ){
79593      db->aDb[j] = db->aDb[i];
79594    }
79595    j++;
79596  }
79597  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
79598  db->nDb = j;
79599  if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
79600    memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
79601    sqlite3DbFree(db, db->aDb);
79602    db->aDb = db->aDbStatic;
79603  }
79604}
79605
79606/*
79607** This routine is called when a commit occurs.
79608*/
79609SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
79610  db->flags &= ~SQLITE_InternChanges;
79611}
79612
79613/*
79614** Delete memory allocated for the column names of a table or view (the
79615** Table.aCol[] array).
79616*/
79617static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
79618  int i;
79619  Column *pCol;
79620  assert( pTable!=0 );
79621  if( (pCol = pTable->aCol)!=0 ){
79622    for(i=0; i<pTable->nCol; i++, pCol++){
79623      sqlite3DbFree(db, pCol->zName);
79624      sqlite3ExprDelete(db, pCol->pDflt);
79625      sqlite3DbFree(db, pCol->zDflt);
79626      sqlite3DbFree(db, pCol->zType);
79627      sqlite3DbFree(db, pCol->zColl);
79628    }
79629    sqlite3DbFree(db, pTable->aCol);
79630  }
79631}
79632
79633/*
79634** Remove the memory data structures associated with the given
79635** Table.  No changes are made to disk by this routine.
79636**
79637** This routine just deletes the data structure.  It does not unlink
79638** the table data structure from the hash table.  But it does destroy
79639** memory structures of the indices and foreign keys associated with
79640** the table.
79641*/
79642SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
79643  Index *pIndex, *pNext;
79644
79645  assert( !pTable || pTable->nRef>0 );
79646
79647  /* Do not delete the table until the reference count reaches zero. */
79648  if( !pTable ) return;
79649  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
79650
79651  /* Delete all indices associated with this table. */
79652  for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
79653    pNext = pIndex->pNext;
79654    assert( pIndex->pSchema==pTable->pSchema );
79655    if( !db || db->pnBytesFreed==0 ){
79656      char *zName = pIndex->zName;
79657      TESTONLY ( Index *pOld = ) sqlite3HashInsert(
79658	  &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
79659      );
79660      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
79661      assert( pOld==pIndex || pOld==0 );
79662    }
79663    freeIndex(db, pIndex);
79664  }
79665
79666  /* Delete any foreign keys attached to this table. */
79667  sqlite3FkDelete(db, pTable);
79668
79669  /* Delete the Table structure itself.
79670  */
79671  sqliteDeleteColumnNames(db, pTable);
79672  sqlite3DbFree(db, pTable->zName);
79673  sqlite3DbFree(db, pTable->zColAff);
79674  sqlite3SelectDelete(db, pTable->pSelect);
79675#ifndef SQLITE_OMIT_CHECK
79676  sqlite3ExprDelete(db, pTable->pCheck);
79677#endif
79678#ifndef SQLITE_OMIT_VIRTUALTABLE
79679  sqlite3VtabClear(db, pTable);
79680#endif
79681  sqlite3DbFree(db, pTable);
79682}
79683
79684/*
79685** Unlink the given table from the hash tables and the delete the
79686** table structure with all its indices and foreign keys.
79687*/
79688SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
79689  Table *p;
79690  Db *pDb;
79691
79692  assert( db!=0 );
79693  assert( iDb>=0 && iDb<db->nDb );
79694  assert( zTabName );
79695  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79696  testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
79697  pDb = &db->aDb[iDb];
79698  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
79699                        sqlite3Strlen30(zTabName),0);
79700  sqlite3DeleteTable(db, p);
79701  db->flags |= SQLITE_InternChanges;
79702}
79703
79704/*
79705** Given a token, return a string that consists of the text of that
79706** token.  Space to hold the returned string
79707** is obtained from sqliteMalloc() and must be freed by the calling
79708** function.
79709**
79710** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
79711** surround the body of the token are removed.
79712**
79713** Tokens are often just pointers into the original SQL text and so
79714** are not \000 terminated and are not persistent.  The returned string
79715** is \000 terminated and is persistent.
79716*/
79717SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
79718  char *zName;
79719  if( pName ){
79720    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
79721    sqlite3Dequote(zName);
79722  }else{
79723    zName = 0;
79724  }
79725  return zName;
79726}
79727
79728/*
79729** Open the sqlite_master table stored in database number iDb for
79730** writing. The table is opened using cursor 0.
79731*/
79732SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
79733  Vdbe *v = sqlite3GetVdbe(p);
79734  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
79735  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
79736  sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
79737  if( p->nTab==0 ){
79738    p->nTab = 1;
79739  }
79740}
79741
79742/*
79743** Parameter zName points to a nul-terminated buffer containing the name
79744** of a database ("main", "temp" or the name of an attached db). This
79745** function returns the index of the named database in db->aDb[], or
79746** -1 if the named db cannot be found.
79747*/
79748SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
79749  int i = -1;         /* Database number */
79750  if( zName ){
79751    Db *pDb;
79752    int n = sqlite3Strlen30(zName);
79753    for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
79754      if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
79755          0==sqlite3StrICmp(pDb->zName, zName) ){
79756        break;
79757      }
79758    }
79759  }
79760  return i;
79761}
79762
79763/*
79764** The token *pName contains the name of a database (either "main" or
79765** "temp" or the name of an attached db). This routine returns the
79766** index of the named database in db->aDb[], or -1 if the named db
79767** does not exist.
79768*/
79769SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
79770  int i;                               /* Database number */
79771  char *zName;                         /* Name we are searching for */
79772  zName = sqlite3NameFromToken(db, pName);
79773  i = sqlite3FindDbName(db, zName);
79774  sqlite3DbFree(db, zName);
79775  return i;
79776}
79777
79778/* The table or view or trigger name is passed to this routine via tokens
79779** pName1 and pName2. If the table name was fully qualified, for example:
79780**
79781** CREATE TABLE xxx.yyy (...);
79782**
79783** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
79784** the table name is not fully qualified, i.e.:
79785**
79786** CREATE TABLE yyy(...);
79787**
79788** Then pName1 is set to "yyy" and pName2 is "".
79789**
79790** This routine sets the *ppUnqual pointer to point at the token (pName1 or
79791** pName2) that stores the unqualified table name.  The index of the
79792** database "xxx" is returned.
79793*/
79794SQLITE_PRIVATE int sqlite3TwoPartName(
79795  Parse *pParse,      /* Parsing and code generating context */
79796  Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
79797  Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
79798  Token **pUnqual     /* Write the unqualified object name here */
79799){
79800  int iDb;                    /* Database holding the object */
79801  sqlite3 *db = pParse->db;
79802
79803  if( ALWAYS(pName2!=0) && pName2->n>0 ){
79804    if( db->init.busy ) {
79805      sqlite3ErrorMsg(pParse, "corrupt database");
79806      pParse->nErr++;
79807      return -1;
79808    }
79809    *pUnqual = pName2;
79810    iDb = sqlite3FindDb(db, pName1);
79811    if( iDb<0 ){
79812      sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
79813      pParse->nErr++;
79814      return -1;
79815    }
79816  }else{
79817    assert( db->init.iDb==0 || db->init.busy );
79818    iDb = db->init.iDb;
79819    *pUnqual = pName1;
79820  }
79821  return iDb;
79822}
79823
79824/*
79825** This routine is used to check if the UTF-8 string zName is a legal
79826** unqualified name for a new schema object (table, index, view or
79827** trigger). All names are legal except those that begin with the string
79828** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
79829** is reserved for internal use.
79830*/
79831SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
79832  if( !pParse->db->init.busy && pParse->nested==0
79833          && (pParse->db->flags & SQLITE_WriteSchema)==0
79834          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
79835    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
79836    return SQLITE_ERROR;
79837  }
79838  return SQLITE_OK;
79839}
79840
79841/*
79842** Begin constructing a new table representation in memory.  This is
79843** the first of several action routines that get called in response
79844** to a CREATE TABLE statement.  In particular, this routine is called
79845** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
79846** flag is true if the table should be stored in the auxiliary database
79847** file instead of in the main database file.  This is normally the case
79848** when the "TEMP" or "TEMPORARY" keyword occurs in between
79849** CREATE and TABLE.
79850**
79851** The new table record is initialized and put in pParse->pNewTable.
79852** As more of the CREATE TABLE statement is parsed, additional action
79853** routines will be called to add more information to this record.
79854** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
79855** is called to complete the construction of the new table record.
79856*/
79857SQLITE_PRIVATE void sqlite3StartTable(
79858  Parse *pParse,   /* Parser context */
79859  Token *pName1,   /* First part of the name of the table or view */
79860  Token *pName2,   /* Second part of the name of the table or view */
79861  int isTemp,      /* True if this is a TEMP table */
79862  int isView,      /* True if this is a VIEW */
79863  int isVirtual,   /* True if this is a VIRTUAL table */
79864  int noErr        /* Do nothing if table already exists */
79865){
79866  Table *pTable;
79867  char *zName = 0; /* The name of the new table */
79868  sqlite3 *db = pParse->db;
79869  Vdbe *v;
79870  int iDb;         /* Database number to create the table in */
79871  Token *pName;    /* Unqualified name of the table to create */
79872
79873  /* The table or view name to create is passed to this routine via tokens
79874  ** pName1 and pName2. If the table name was fully qualified, for example:
79875  **
79876  ** CREATE TABLE xxx.yyy (...);
79877  **
79878  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
79879  ** the table name is not fully qualified, i.e.:
79880  **
79881  ** CREATE TABLE yyy(...);
79882  **
79883  ** Then pName1 is set to "yyy" and pName2 is "".
79884  **
79885  ** The call below sets the pName pointer to point at the token (pName1 or
79886  ** pName2) that stores the unqualified table name. The variable iDb is
79887  ** set to the index of the database that the table or view is to be
79888  ** created in.
79889  */
79890  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
79891  if( iDb<0 ) return;
79892  if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
79893    /* If creating a temp table, the name may not be qualified. Unless
79894    ** the database name is "temp" anyway.  */
79895    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
79896    return;
79897  }
79898  if( !OMIT_TEMPDB && isTemp ) iDb = 1;
79899
79900  pParse->sNameToken = *pName;
79901  zName = sqlite3NameFromToken(db, pName);
79902  if( zName==0 ) return;
79903  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
79904    goto begin_table_error;
79905  }
79906  if( db->init.iDb==1 ) isTemp = 1;
79907#ifndef SQLITE_OMIT_AUTHORIZATION
79908  assert( (isTemp & 1)==isTemp );
79909  {
79910    int code;
79911    char *zDb = db->aDb[iDb].zName;
79912    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
79913      goto begin_table_error;
79914    }
79915    if( isView ){
79916      if( !OMIT_TEMPDB && isTemp ){
79917        code = SQLITE_CREATE_TEMP_VIEW;
79918      }else{
79919        code = SQLITE_CREATE_VIEW;
79920      }
79921    }else{
79922      if( !OMIT_TEMPDB && isTemp ){
79923        code = SQLITE_CREATE_TEMP_TABLE;
79924      }else{
79925        code = SQLITE_CREATE_TABLE;
79926      }
79927    }
79928    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
79929      goto begin_table_error;
79930    }
79931  }
79932#endif
79933
79934  /* Make sure the new table name does not collide with an existing
79935  ** index or table name in the same database.  Issue an error message if
79936  ** it does. The exception is if the statement being parsed was passed
79937  ** to an sqlite3_declare_vtab() call. In that case only the column names
79938  ** and types will be used, so there is no need to test for namespace
79939  ** collisions.
79940  */
79941  if( !IN_DECLARE_VTAB ){
79942    char *zDb = db->aDb[iDb].zName;
79943    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
79944      goto begin_table_error;
79945    }
79946    pTable = sqlite3FindTable(db, zName, zDb);
79947    if( pTable ){
79948      if( !noErr ){
79949        sqlite3ErrorMsg(pParse, "table %T already exists", pName);
79950      }else{
79951        assert( !db->init.busy );
79952        sqlite3CodeVerifySchema(pParse, iDb);
79953      }
79954      goto begin_table_error;
79955    }
79956    if( sqlite3FindIndex(db, zName, zDb)!=0 ){
79957      sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
79958      goto begin_table_error;
79959    }
79960  }
79961
79962  pTable = sqlite3DbMallocZero(db, sizeof(Table));
79963  if( pTable==0 ){
79964    db->mallocFailed = 1;
79965    pParse->rc = SQLITE_NOMEM;
79966    pParse->nErr++;
79967    goto begin_table_error;
79968  }
79969  pTable->zName = zName;
79970  pTable->iPKey = -1;
79971  pTable->pSchema = db->aDb[iDb].pSchema;
79972  pTable->nRef = 1;
79973  pTable->nRowEst = 1000000;
79974  assert( pParse->pNewTable==0 );
79975  pParse->pNewTable = pTable;
79976
79977  /* If this is the magic sqlite_sequence table used by autoincrement,
79978  ** then record a pointer to this table in the main database structure
79979  ** so that INSERT can find the table easily.
79980  */
79981#ifndef SQLITE_OMIT_AUTOINCREMENT
79982  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
79983    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
79984    pTable->pSchema->pSeqTab = pTable;
79985  }
79986#endif
79987
79988  /* Begin generating the code that will insert the table record into
79989  ** the SQLITE_MASTER table.  Note in particular that we must go ahead
79990  ** and allocate the record number for the table entry now.  Before any
79991  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
79992  ** indices to be created and the table record must come before the
79993  ** indices.  Hence, the record number for the table must be allocated
79994  ** now.
79995  */
79996  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
79997    int j1;
79998    int fileFormat;
79999    int reg1, reg2, reg3;
80000    sqlite3BeginWriteOperation(pParse, 0, iDb);
80001
80002#ifndef SQLITE_OMIT_VIRTUALTABLE
80003    if( isVirtual ){
80004      sqlite3VdbeAddOp0(v, OP_VBegin);
80005    }
80006#endif
80007
80008    /* If the file format and encoding in the database have not been set,
80009    ** set them now.
80010    */
80011    reg1 = pParse->regRowid = ++pParse->nMem;
80012    reg2 = pParse->regRoot = ++pParse->nMem;
80013    reg3 = ++pParse->nMem;
80014    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
80015    sqlite3VdbeUsesBtree(v, iDb);
80016    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
80017    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
80018                  1 : SQLITE_MAX_FILE_FORMAT;
80019    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
80020    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
80021    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
80022    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
80023    sqlite3VdbeJumpHere(v, j1);
80024
80025    /* This just creates a place-holder record in the sqlite_master table.
80026    ** The record created does not contain anything yet.  It will be replaced
80027    ** by the real entry in code generated at sqlite3EndTable().
80028    **
80029    ** The rowid for the new entry is left in register pParse->regRowid.
80030    ** The root page number of the new table is left in reg pParse->regRoot.
80031    ** The rowid and root page number values are needed by the code that
80032    ** sqlite3EndTable will generate.
80033    */
80034#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
80035    if( isView || isVirtual ){
80036      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
80037    }else
80038#endif
80039    {
80040      sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
80041    }
80042    sqlite3OpenMasterTable(pParse, iDb);
80043    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
80044    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
80045    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
80046    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
80047    sqlite3VdbeAddOp0(v, OP_Close);
80048  }
80049
80050  /* Normal (non-error) return. */
80051  return;
80052
80053  /* If an error occurs, we jump here */
80054begin_table_error:
80055  sqlite3DbFree(db, zName);
80056  return;
80057}
80058
80059/*
80060** This macro is used to compare two strings in a case-insensitive manner.
80061** It is slightly faster than calling sqlite3StrICmp() directly, but
80062** produces larger code.
80063**
80064** WARNING: This macro is not compatible with the strcmp() family. It
80065** returns true if the two strings are equal, otherwise false.
80066*/
80067#define STRICMP(x, y) (\
80068sqlite3UpperToLower[*(unsigned char *)(x)]==   \
80069sqlite3UpperToLower[*(unsigned char *)(y)]     \
80070&& sqlite3StrICmp((x)+1,(y)+1)==0 )
80071
80072/*
80073** Add a new column to the table currently being constructed.
80074**
80075** The parser calls this routine once for each column declaration
80076** in a CREATE TABLE statement.  sqlite3StartTable() gets called
80077** first to get things going.  Then this routine is called for each
80078** column.
80079*/
80080SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
80081  Table *p;
80082  int i;
80083  char *z;
80084  Column *pCol;
80085  sqlite3 *db = pParse->db;
80086  if( (p = pParse->pNewTable)==0 ) return;
80087#if SQLITE_MAX_COLUMN
80088  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
80089    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
80090    return;
80091  }
80092#endif
80093  z = sqlite3NameFromToken(db, pName);
80094  if( z==0 ) return;
80095  for(i=0; i<p->nCol; i++){
80096    if( STRICMP(z, p->aCol[i].zName) ){
80097      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
80098      sqlite3DbFree(db, z);
80099      return;
80100    }
80101  }
80102  if( (p->nCol & 0x7)==0 ){
80103    Column *aNew;
80104    aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
80105    if( aNew==0 ){
80106      sqlite3DbFree(db, z);
80107      return;
80108    }
80109    p->aCol = aNew;
80110  }
80111  pCol = &p->aCol[p->nCol];
80112  memset(pCol, 0, sizeof(p->aCol[0]));
80113  pCol->zName = z;
80114
80115  /* If there is no type specified, columns have the default affinity
80116  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
80117  ** be called next to set pCol->affinity correctly.
80118  */
80119  pCol->affinity = SQLITE_AFF_NONE;
80120  p->nCol++;
80121}
80122
80123/*
80124** This routine is called by the parser while in the middle of
80125** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
80126** been seen on a column.  This routine sets the notNull flag on
80127** the column currently under construction.
80128*/
80129SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
80130  Table *p;
80131  p = pParse->pNewTable;
80132  if( p==0 || NEVER(p->nCol<1) ) return;
80133  p->aCol[p->nCol-1].notNull = (u8)onError;
80134}
80135
80136/*
80137** Scan the column type name zType (length nType) and return the
80138** associated affinity type.
80139**
80140** This routine does a case-independent search of zType for the
80141** substrings in the following table. If one of the substrings is
80142** found, the corresponding affinity is returned. If zType contains
80143** more than one of the substrings, entries toward the top of
80144** the table take priority. For example, if zType is 'BLOBINT',
80145** SQLITE_AFF_INTEGER is returned.
80146**
80147** Substring     | Affinity
80148** --------------------------------
80149** 'INT'         | SQLITE_AFF_INTEGER
80150** 'CHAR'        | SQLITE_AFF_TEXT
80151** 'CLOB'        | SQLITE_AFF_TEXT
80152** 'TEXT'        | SQLITE_AFF_TEXT
80153** 'BLOB'        | SQLITE_AFF_NONE
80154** 'REAL'        | SQLITE_AFF_REAL
80155** 'FLOA'        | SQLITE_AFF_REAL
80156** 'DOUB'        | SQLITE_AFF_REAL
80157**
80158** If none of the substrings in the above table are found,
80159** SQLITE_AFF_NUMERIC is returned.
80160*/
80161SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
80162  u32 h = 0;
80163  char aff = SQLITE_AFF_NUMERIC;
80164
80165  if( zIn ) while( zIn[0] ){
80166    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
80167    zIn++;
80168    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
80169      aff = SQLITE_AFF_TEXT;
80170    }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
80171      aff = SQLITE_AFF_TEXT;
80172    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
80173      aff = SQLITE_AFF_TEXT;
80174    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
80175        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
80176      aff = SQLITE_AFF_NONE;
80177#ifndef SQLITE_OMIT_FLOATING_POINT
80178    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
80179        && aff==SQLITE_AFF_NUMERIC ){
80180      aff = SQLITE_AFF_REAL;
80181    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
80182        && aff==SQLITE_AFF_NUMERIC ){
80183      aff = SQLITE_AFF_REAL;
80184    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
80185        && aff==SQLITE_AFF_NUMERIC ){
80186      aff = SQLITE_AFF_REAL;
80187#endif
80188    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
80189      aff = SQLITE_AFF_INTEGER;
80190      break;
80191    }
80192  }
80193
80194  return aff;
80195}
80196
80197/*
80198** This routine is called by the parser while in the middle of
80199** parsing a CREATE TABLE statement.  The pFirst token is the first
80200** token in the sequence of tokens that describe the type of the
80201** column currently under construction.   pLast is the last token
80202** in the sequence.  Use this information to construct a string
80203** that contains the typename of the column and store that string
80204** in zType.
80205*/
80206SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
80207  Table *p;
80208  Column *pCol;
80209
80210  p = pParse->pNewTable;
80211  if( p==0 || NEVER(p->nCol<1) ) return;
80212  pCol = &p->aCol[p->nCol-1];
80213  assert( pCol->zType==0 );
80214  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
80215  pCol->affinity = sqlite3AffinityType(pCol->zType);
80216}
80217
80218/*
80219** The expression is the default value for the most recently added column
80220** of the table currently under construction.
80221**
80222** Default value expressions must be constant.  Raise an exception if this
80223** is not the case.
80224**
80225** This routine is called by the parser while in the middle of
80226** parsing a CREATE TABLE statement.
80227*/
80228SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
80229  Table *p;
80230  Column *pCol;
80231  sqlite3 *db = pParse->db;
80232  p = pParse->pNewTable;
80233  if( p!=0 ){
80234    pCol = &(p->aCol[p->nCol-1]);
80235    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
80236      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
80237          pCol->zName);
80238    }else{
80239      /* A copy of pExpr is used instead of the original, as pExpr contains
80240      ** tokens that point to volatile memory. The 'span' of the expression
80241      ** is required by pragma table_info.
80242      */
80243      sqlite3ExprDelete(db, pCol->pDflt);
80244      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
80245      sqlite3DbFree(db, pCol->zDflt);
80246      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
80247                                     (int)(pSpan->zEnd - pSpan->zStart));
80248    }
80249  }
80250  sqlite3ExprDelete(db, pSpan->pExpr);
80251}
80252
80253/*
80254** Designate the PRIMARY KEY for the table.  pList is a list of names
80255** of columns that form the primary key.  If pList is NULL, then the
80256** most recently added column of the table is the primary key.
80257**
80258** A table can have at most one primary key.  If the table already has
80259** a primary key (and this is the second primary key) then create an
80260** error.
80261**
80262** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
80263** then we will try to use that column as the rowid.  Set the Table.iPKey
80264** field of the table under construction to be the index of the
80265** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
80266** no INTEGER PRIMARY KEY.
80267**
80268** If the key is not an INTEGER PRIMARY KEY, then create a unique
80269** index for the key.  No index is created for INTEGER PRIMARY KEYs.
80270*/
80271SQLITE_PRIVATE void sqlite3AddPrimaryKey(
80272  Parse *pParse,    /* Parsing context */
80273  ExprList *pList,  /* List of field names to be indexed */
80274  int onError,      /* What to do with a uniqueness conflict */
80275  int autoInc,      /* True if the AUTOINCREMENT keyword is present */
80276  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
80277){
80278  Table *pTab = pParse->pNewTable;
80279  char *zType = 0;
80280  int iCol = -1, i;
80281  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
80282  if( pTab->tabFlags & TF_HasPrimaryKey ){
80283    sqlite3ErrorMsg(pParse,
80284      "table \"%s\" has more than one primary key", pTab->zName);
80285    goto primary_key_exit;
80286  }
80287  pTab->tabFlags |= TF_HasPrimaryKey;
80288  if( pList==0 ){
80289    iCol = pTab->nCol - 1;
80290    pTab->aCol[iCol].isPrimKey = 1;
80291  }else{
80292    for(i=0; i<pList->nExpr; i++){
80293      for(iCol=0; iCol<pTab->nCol; iCol++){
80294        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
80295          break;
80296        }
80297      }
80298      if( iCol<pTab->nCol ){
80299        pTab->aCol[iCol].isPrimKey = 1;
80300      }
80301    }
80302    if( pList->nExpr>1 ) iCol = -1;
80303  }
80304  if( iCol>=0 && iCol<pTab->nCol ){
80305    zType = pTab->aCol[iCol].zType;
80306  }
80307  if( zType && sqlite3StrICmp(zType, "INTEGER")==0
80308        && sortOrder==SQLITE_SO_ASC ){
80309    pTab->iPKey = iCol;
80310    pTab->keyConf = (u8)onError;
80311    assert( autoInc==0 || autoInc==1 );
80312    pTab->tabFlags |= autoInc*TF_Autoincrement;
80313  }else if( autoInc ){
80314#ifndef SQLITE_OMIT_AUTOINCREMENT
80315    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
80316       "INTEGER PRIMARY KEY");
80317#endif
80318  }else{
80319    Index *p;
80320    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
80321    if( p ){
80322      p->autoIndex = 2;
80323    }
80324    pList = 0;
80325  }
80326
80327primary_key_exit:
80328  sqlite3ExprListDelete(pParse->db, pList);
80329  return;
80330}
80331
80332/*
80333** Add a new CHECK constraint to the table currently under construction.
80334*/
80335SQLITE_PRIVATE void sqlite3AddCheckConstraint(
80336  Parse *pParse,    /* Parsing context */
80337  Expr *pCheckExpr  /* The check expression */
80338){
80339  sqlite3 *db = pParse->db;
80340#ifndef SQLITE_OMIT_CHECK
80341  Table *pTab = pParse->pNewTable;
80342  if( pTab && !IN_DECLARE_VTAB ){
80343    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
80344  }else
80345#endif
80346  {
80347    sqlite3ExprDelete(db, pCheckExpr);
80348  }
80349}
80350
80351/*
80352** Set the collation function of the most recently parsed table column
80353** to the CollSeq given.
80354*/
80355SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
80356  Table *p;
80357  int i;
80358  char *zColl;              /* Dequoted name of collation sequence */
80359  sqlite3 *db;
80360
80361  if( (p = pParse->pNewTable)==0 ) return;
80362  i = p->nCol-1;
80363  db = pParse->db;
80364  zColl = sqlite3NameFromToken(db, pToken);
80365  if( !zColl ) return;
80366
80367  if( sqlite3LocateCollSeq(pParse, zColl) ){
80368    Index *pIdx;
80369    p->aCol[i].zColl = zColl;
80370
80371    /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
80372    ** then an index may have been created on this column before the
80373    ** collation type was added. Correct this if it is the case.
80374    */
80375    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
80376      assert( pIdx->nColumn==1 );
80377      if( pIdx->aiColumn[0]==i ){
80378        pIdx->azColl[0] = p->aCol[i].zColl;
80379      }
80380    }
80381  }else{
80382    sqlite3DbFree(db, zColl);
80383  }
80384}
80385
80386/*
80387** This function returns the collation sequence for database native text
80388** encoding identified by the string zName, length nName.
80389**
80390** If the requested collation sequence is not available, or not available
80391** in the database native encoding, the collation factory is invoked to
80392** request it. If the collation factory does not supply such a sequence,
80393** and the sequence is available in another text encoding, then that is
80394** returned instead.
80395**
80396** If no versions of the requested collations sequence are available, or
80397** another error occurs, NULL is returned and an error message written into
80398** pParse.
80399**
80400** This routine is a wrapper around sqlite3FindCollSeq().  This routine
80401** invokes the collation factory if the named collation cannot be found
80402** and generates an error message.
80403**
80404** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
80405*/
80406SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
80407  sqlite3 *db = pParse->db;
80408  u8 enc = ENC(db);
80409  u8 initbusy = db->init.busy;
80410  CollSeq *pColl;
80411
80412  pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
80413  if( !initbusy && (!pColl || !pColl->xCmp) ){
80414    pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
80415    if( !pColl ){
80416      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
80417    }
80418  }
80419
80420  return pColl;
80421}
80422
80423
80424/*
80425** Generate code that will increment the schema cookie.
80426**
80427** The schema cookie is used to determine when the schema for the
80428** database changes.  After each schema change, the cookie value
80429** changes.  When a process first reads the schema it records the
80430** cookie.  Thereafter, whenever it goes to access the database,
80431** it checks the cookie to make sure the schema has not changed
80432** since it was last read.
80433**
80434** This plan is not completely bullet-proof.  It is possible for
80435** the schema to change multiple times and for the cookie to be
80436** set back to prior value.  But schema changes are infrequent
80437** and the probability of hitting the same cookie value is only
80438** 1 chance in 2^32.  So we're safe enough.
80439*/
80440SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
80441  int r1 = sqlite3GetTempReg(pParse);
80442  sqlite3 *db = pParse->db;
80443  Vdbe *v = pParse->pVdbe;
80444  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
80445  sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
80446  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
80447  sqlite3ReleaseTempReg(pParse, r1);
80448}
80449
80450/*
80451** Measure the number of characters needed to output the given
80452** identifier.  The number returned includes any quotes used
80453** but does not include the null terminator.
80454**
80455** The estimate is conservative.  It might be larger that what is
80456** really needed.
80457*/
80458static int identLength(const char *z){
80459  int n;
80460  for(n=0; *z; n++, z++){
80461    if( *z=='"' ){ n++; }
80462  }
80463  return n + 2;
80464}
80465
80466/*
80467** The first parameter is a pointer to an output buffer. The second
80468** parameter is a pointer to an integer that contains the offset at
80469** which to write into the output buffer. This function copies the
80470** nul-terminated string pointed to by the third parameter, zSignedIdent,
80471** to the specified offset in the buffer and updates *pIdx to refer
80472** to the first byte after the last byte written before returning.
80473**
80474** If the string zSignedIdent consists entirely of alpha-numeric
80475** characters, does not begin with a digit and is not an SQL keyword,
80476** then it is copied to the output buffer exactly as it is. Otherwise,
80477** it is quoted using double-quotes.
80478*/
80479static void identPut(char *z, int *pIdx, char *zSignedIdent){
80480  unsigned char *zIdent = (unsigned char*)zSignedIdent;
80481  int i, j, needQuote;
80482  i = *pIdx;
80483
80484  for(j=0; zIdent[j]; j++){
80485    if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
80486  }
80487  needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
80488  if( !needQuote ){
80489    needQuote = zIdent[j];
80490  }
80491
80492  if( needQuote ) z[i++] = '"';
80493  for(j=0; zIdent[j]; j++){
80494    z[i++] = zIdent[j];
80495    if( zIdent[j]=='"' ) z[i++] = '"';
80496  }
80497  if( needQuote ) z[i++] = '"';
80498  z[i] = 0;
80499  *pIdx = i;
80500}
80501
80502/*
80503** Generate a CREATE TABLE statement appropriate for the given
80504** table.  Memory to hold the text of the statement is obtained
80505** from sqliteMalloc() and must be freed by the calling function.
80506*/
80507static char *createTableStmt(sqlite3 *db, Table *p){
80508  int i, k, n;
80509  char *zStmt;
80510  char *zSep, *zSep2, *zEnd;
80511  Column *pCol;
80512  n = 0;
80513  for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
80514    n += identLength(pCol->zName) + 5;
80515  }
80516  n += identLength(p->zName);
80517  if( n<50 ){
80518    zSep = "";
80519    zSep2 = ",";
80520    zEnd = ")";
80521  }else{
80522    zSep = "\n  ";
80523    zSep2 = ",\n  ";
80524    zEnd = "\n)";
80525  }
80526  n += 35 + 6*p->nCol;
80527  zStmt = sqlite3DbMallocRaw(0, n);
80528  if( zStmt==0 ){
80529    db->mallocFailed = 1;
80530    return 0;
80531  }
80532  sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
80533  k = sqlite3Strlen30(zStmt);
80534  identPut(zStmt, &k, p->zName);
80535  zStmt[k++] = '(';
80536  for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
80537    static const char * const azType[] = {
80538        /* SQLITE_AFF_TEXT    */ " TEXT",
80539        /* SQLITE_AFF_NONE    */ "",
80540        /* SQLITE_AFF_NUMERIC */ " NUM",
80541        /* SQLITE_AFF_INTEGER */ " INT",
80542        /* SQLITE_AFF_REAL    */ " REAL"
80543    };
80544    int len;
80545    const char *zType;
80546
80547    sqlite3_snprintf(n-k, &zStmt[k], zSep);
80548    k += sqlite3Strlen30(&zStmt[k]);
80549    zSep = zSep2;
80550    identPut(zStmt, &k, pCol->zName);
80551    assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
80552    assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
80553    testcase( pCol->affinity==SQLITE_AFF_TEXT );
80554    testcase( pCol->affinity==SQLITE_AFF_NONE );
80555    testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
80556    testcase( pCol->affinity==SQLITE_AFF_INTEGER );
80557    testcase( pCol->affinity==SQLITE_AFF_REAL );
80558
80559    zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
80560    len = sqlite3Strlen30(zType);
80561    assert( pCol->affinity==SQLITE_AFF_NONE
80562            || pCol->affinity==sqlite3AffinityType(zType) );
80563    memcpy(&zStmt[k], zType, len);
80564    k += len;
80565    assert( k<=n );
80566  }
80567  sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
80568  return zStmt;
80569}
80570
80571/*
80572** This routine is called to report the final ")" that terminates
80573** a CREATE TABLE statement.
80574**
80575** The table structure that other action routines have been building
80576** is added to the internal hash tables, assuming no errors have
80577** occurred.
80578**
80579** An entry for the table is made in the master table on disk, unless
80580** this is a temporary table or db->init.busy==1.  When db->init.busy==1
80581** it means we are reading the sqlite_master table because we just
80582** connected to the database or because the sqlite_master table has
80583** recently changed, so the entry for this table already exists in
80584** the sqlite_master table.  We do not want to create it again.
80585**
80586** If the pSelect argument is not NULL, it means that this routine
80587** was called to create a table generated from a
80588** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
80589** the new table will match the result set of the SELECT.
80590*/
80591SQLITE_PRIVATE void sqlite3EndTable(
80592  Parse *pParse,          /* Parse context */
80593  Token *pCons,           /* The ',' token after the last column defn. */
80594  Token *pEnd,            /* The final ')' token in the CREATE TABLE */
80595  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
80596){
80597  Table *p;
80598  sqlite3 *db = pParse->db;
80599  int iDb;
80600
80601  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
80602    return;
80603  }
80604  p = pParse->pNewTable;
80605  if( p==0 ) return;
80606
80607  assert( !db->init.busy || !pSelect );
80608
80609  iDb = sqlite3SchemaToIndex(db, p->pSchema);
80610
80611#ifndef SQLITE_OMIT_CHECK
80612  /* Resolve names in all CHECK constraint expressions.
80613  */
80614  if( p->pCheck ){
80615    SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
80616    NameContext sNC;                /* Name context for pParse->pNewTable */
80617
80618    memset(&sNC, 0, sizeof(sNC));
80619    memset(&sSrc, 0, sizeof(sSrc));
80620    sSrc.nSrc = 1;
80621    sSrc.a[0].zName = p->zName;
80622    sSrc.a[0].pTab = p;
80623    sSrc.a[0].iCursor = -1;
80624    sNC.pParse = pParse;
80625    sNC.pSrcList = &sSrc;
80626    sNC.isCheck = 1;
80627    if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
80628      return;
80629    }
80630  }
80631#endif /* !defined(SQLITE_OMIT_CHECK) */
80632
80633  /* If the db->init.busy is 1 it means we are reading the SQL off the
80634  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
80635  ** So do not write to the disk again.  Extract the root page number
80636  ** for the table from the db->init.newTnum field.  (The page number
80637  ** should have been put there by the sqliteOpenCb routine.)
80638  */
80639  if( db->init.busy ){
80640    p->tnum = db->init.newTnum;
80641  }
80642
80643  /* If not initializing, then create a record for the new table
80644  ** in the SQLITE_MASTER table of the database.
80645  **
80646  ** If this is a TEMPORARY table, write the entry into the auxiliary
80647  ** file instead of into the main database file.
80648  */
80649  if( !db->init.busy ){
80650    int n;
80651    Vdbe *v;
80652    char *zType;    /* "view" or "table" */
80653    char *zType2;   /* "VIEW" or "TABLE" */
80654    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
80655
80656    v = sqlite3GetVdbe(pParse);
80657    if( NEVER(v==0) ) return;
80658
80659    sqlite3VdbeAddOp1(v, OP_Close, 0);
80660
80661    /*
80662    ** Initialize zType for the new view or table.
80663    */
80664    if( p->pSelect==0 ){
80665      /* A regular table */
80666      zType = "table";
80667      zType2 = "TABLE";
80668#ifndef SQLITE_OMIT_VIEW
80669    }else{
80670      /* A view */
80671      zType = "view";
80672      zType2 = "VIEW";
80673#endif
80674    }
80675
80676    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
80677    ** statement to populate the new table. The root-page number for the
80678    ** new table is in register pParse->regRoot.
80679    **
80680    ** Once the SELECT has been coded by sqlite3Select(), it is in a
80681    ** suitable state to query for the column names and types to be used
80682    ** by the new table.
80683    **
80684    ** A shared-cache write-lock is not required to write to the new table,
80685    ** as a schema-lock must have already been obtained to create it. Since
80686    ** a schema-lock excludes all other database users, the write-lock would
80687    ** be redundant.
80688    */
80689    if( pSelect ){
80690      SelectDest dest;
80691      Table *pSelTab;
80692
80693      assert(pParse->nTab==1);
80694      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
80695      sqlite3VdbeChangeP5(v, 1);
80696      pParse->nTab = 2;
80697      sqlite3SelectDestInit(&dest, SRT_Table, 1);
80698      sqlite3Select(pParse, pSelect, &dest);
80699      sqlite3VdbeAddOp1(v, OP_Close, 1);
80700      if( pParse->nErr==0 ){
80701        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
80702        if( pSelTab==0 ) return;
80703        assert( p->aCol==0 );
80704        p->nCol = pSelTab->nCol;
80705        p->aCol = pSelTab->aCol;
80706        pSelTab->nCol = 0;
80707        pSelTab->aCol = 0;
80708        sqlite3DeleteTable(db, pSelTab);
80709      }
80710    }
80711
80712    /* Compute the complete text of the CREATE statement */
80713    if( pSelect ){
80714      zStmt = createTableStmt(db, p);
80715    }else{
80716      n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
80717      zStmt = sqlite3MPrintf(db,
80718          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
80719      );
80720    }
80721
80722    /* A slot for the record has already been allocated in the
80723    ** SQLITE_MASTER table.  We just need to update that slot with all
80724    ** the information we've collected.
80725    */
80726    sqlite3NestedParse(pParse,
80727      "UPDATE %Q.%s "
80728         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
80729       "WHERE rowid=#%d",
80730      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
80731      zType,
80732      p->zName,
80733      p->zName,
80734      pParse->regRoot,
80735      zStmt,
80736      pParse->regRowid
80737    );
80738    sqlite3DbFree(db, zStmt);
80739    sqlite3ChangeCookie(pParse, iDb);
80740
80741#ifndef SQLITE_OMIT_AUTOINCREMENT
80742    /* Check to see if we need to create an sqlite_sequence table for
80743    ** keeping track of autoincrement keys.
80744    */
80745    if( p->tabFlags & TF_Autoincrement ){
80746      Db *pDb = &db->aDb[iDb];
80747      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
80748      if( pDb->pSchema->pSeqTab==0 ){
80749        sqlite3NestedParse(pParse,
80750          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
80751          pDb->zName
80752        );
80753      }
80754    }
80755#endif
80756
80757    /* Reparse everything to update our internal data structures */
80758    sqlite3VdbeAddParseSchemaOp(v, iDb,
80759               sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
80760  }
80761
80762
80763  /* Add the table to the in-memory representation of the database.
80764  */
80765  if( db->init.busy ){
80766    Table *pOld;
80767    Schema *pSchema = p->pSchema;
80768    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
80769    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
80770                             sqlite3Strlen30(p->zName),p);
80771    if( pOld ){
80772      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
80773      db->mallocFailed = 1;
80774      return;
80775    }
80776    pParse->pNewTable = 0;
80777    db->nTable++;
80778    db->flags |= SQLITE_InternChanges;
80779
80780#ifndef SQLITE_OMIT_ALTERTABLE
80781    if( !p->pSelect ){
80782      const char *zName = (const char *)pParse->sNameToken.z;
80783      int nName;
80784      assert( !pSelect && pCons && pEnd );
80785      if( pCons->z==0 ){
80786        pCons = pEnd;
80787      }
80788      nName = (int)((const char *)pCons->z - zName);
80789      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
80790    }
80791#endif
80792  }
80793}
80794
80795#ifndef SQLITE_OMIT_VIEW
80796/*
80797** The parser calls this routine in order to create a new VIEW
80798*/
80799SQLITE_PRIVATE void sqlite3CreateView(
80800  Parse *pParse,     /* The parsing context */
80801  Token *pBegin,     /* The CREATE token that begins the statement */
80802  Token *pName1,     /* The token that holds the name of the view */
80803  Token *pName2,     /* The token that holds the name of the view */
80804  Select *pSelect,   /* A SELECT statement that will become the new view */
80805  int isTemp,        /* TRUE for a TEMPORARY view */
80806  int noErr          /* Suppress error messages if VIEW already exists */
80807){
80808  Table *p;
80809  int n;
80810  const char *z;
80811  Token sEnd;
80812  DbFixer sFix;
80813  Token *pName = 0;
80814  int iDb;
80815  sqlite3 *db = pParse->db;
80816
80817  if( pParse->nVar>0 ){
80818    sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
80819    sqlite3SelectDelete(db, pSelect);
80820    return;
80821  }
80822  sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
80823  p = pParse->pNewTable;
80824  if( p==0 || pParse->nErr ){
80825    sqlite3SelectDelete(db, pSelect);
80826    return;
80827  }
80828  sqlite3TwoPartName(pParse, pName1, pName2, &pName);
80829  iDb = sqlite3SchemaToIndex(db, p->pSchema);
80830  if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
80831    && sqlite3FixSelect(&sFix, pSelect)
80832  ){
80833    sqlite3SelectDelete(db, pSelect);
80834    return;
80835  }
80836
80837  /* Make a copy of the entire SELECT statement that defines the view.
80838  ** This will force all the Expr.token.z values to be dynamically
80839  ** allocated rather than point to the input string - which means that
80840  ** they will persist after the current sqlite3_exec() call returns.
80841  */
80842  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
80843  sqlite3SelectDelete(db, pSelect);
80844  if( db->mallocFailed ){
80845    return;
80846  }
80847  if( !db->init.busy ){
80848    sqlite3ViewGetColumnNames(pParse, p);
80849  }
80850
80851  /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
80852  ** the end.
80853  */
80854  sEnd = pParse->sLastToken;
80855  if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
80856    sEnd.z += sEnd.n;
80857  }
80858  sEnd.n = 0;
80859  n = (int)(sEnd.z - pBegin->z);
80860  z = pBegin->z;
80861  while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
80862  sEnd.z = &z[n-1];
80863  sEnd.n = 1;
80864
80865  /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
80866  sqlite3EndTable(pParse, 0, &sEnd, 0);
80867  return;
80868}
80869#endif /* SQLITE_OMIT_VIEW */
80870
80871#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
80872/*
80873** The Table structure pTable is really a VIEW.  Fill in the names of
80874** the columns of the view in the pTable structure.  Return the number
80875** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
80876*/
80877SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
80878  Table *pSelTab;   /* A fake table from which we get the result set */
80879  Select *pSel;     /* Copy of the SELECT that implements the view */
80880  int nErr = 0;     /* Number of errors encountered */
80881  int n;            /* Temporarily holds the number of cursors assigned */
80882  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
80883  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
80884
80885  assert( pTable );
80886
80887#ifndef SQLITE_OMIT_VIRTUALTABLE
80888  if( sqlite3VtabCallConnect(pParse, pTable) ){
80889    return SQLITE_ERROR;
80890  }
80891  if( IsVirtual(pTable) ) return 0;
80892#endif
80893
80894#ifndef SQLITE_OMIT_VIEW
80895  /* A positive nCol means the columns names for this view are
80896  ** already known.
80897  */
80898  if( pTable->nCol>0 ) return 0;
80899
80900  /* A negative nCol is a special marker meaning that we are currently
80901  ** trying to compute the column names.  If we enter this routine with
80902  ** a negative nCol, it means two or more views form a loop, like this:
80903  **
80904  **     CREATE VIEW one AS SELECT * FROM two;
80905  **     CREATE VIEW two AS SELECT * FROM one;
80906  **
80907  ** Actually, the error above is now caught prior to reaching this point.
80908  ** But the following test is still important as it does come up
80909  ** in the following:
80910  **
80911  **     CREATE TABLE main.ex1(a);
80912  **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
80913  **     SELECT * FROM temp.ex1;
80914  */
80915  if( pTable->nCol<0 ){
80916    sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
80917    return 1;
80918  }
80919  assert( pTable->nCol>=0 );
80920
80921  /* If we get this far, it means we need to compute the table names.
80922  ** Note that the call to sqlite3ResultSetOfSelect() will expand any
80923  ** "*" elements in the results set of the view and will assign cursors
80924  ** to the elements of the FROM clause.  But we do not want these changes
80925  ** to be permanent.  So the computation is done on a copy of the SELECT
80926  ** statement that defines the view.
80927  */
80928  assert( pTable->pSelect );
80929  pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
80930  if( pSel ){
80931    u8 enableLookaside = db->lookaside.bEnabled;
80932    n = pParse->nTab;
80933    sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
80934    pTable->nCol = -1;
80935    db->lookaside.bEnabled = 0;
80936#ifndef SQLITE_OMIT_AUTHORIZATION
80937    xAuth = db->xAuth;
80938    db->xAuth = 0;
80939    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
80940    db->xAuth = xAuth;
80941#else
80942    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
80943#endif
80944    db->lookaside.bEnabled = enableLookaside;
80945    pParse->nTab = n;
80946    if( pSelTab ){
80947      assert( pTable->aCol==0 );
80948      pTable->nCol = pSelTab->nCol;
80949      pTable->aCol = pSelTab->aCol;
80950      pSelTab->nCol = 0;
80951      pSelTab->aCol = 0;
80952      sqlite3DeleteTable(db, pSelTab);
80953      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
80954      pTable->pSchema->flags |= DB_UnresetViews;
80955    }else{
80956      pTable->nCol = 0;
80957      nErr++;
80958    }
80959    sqlite3SelectDelete(db, pSel);
80960  } else {
80961    nErr++;
80962  }
80963#endif /* SQLITE_OMIT_VIEW */
80964  return nErr;
80965}
80966#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
80967
80968#ifndef SQLITE_OMIT_VIEW
80969/*
80970** Clear the column names from every VIEW in database idx.
80971*/
80972static void sqliteViewResetAll(sqlite3 *db, int idx){
80973  HashElem *i;
80974  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
80975  if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
80976  for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
80977    Table *pTab = sqliteHashData(i);
80978    if( pTab->pSelect ){
80979      sqliteDeleteColumnNames(db, pTab);
80980      pTab->aCol = 0;
80981      pTab->nCol = 0;
80982    }
80983  }
80984  DbClearProperty(db, idx, DB_UnresetViews);
80985}
80986#else
80987# define sqliteViewResetAll(A,B)
80988#endif /* SQLITE_OMIT_VIEW */
80989
80990/*
80991** This function is called by the VDBE to adjust the internal schema
80992** used by SQLite when the btree layer moves a table root page. The
80993** root-page of a table or index in database iDb has changed from iFrom
80994** to iTo.
80995**
80996** Ticket #1728:  The symbol table might still contain information
80997** on tables and/or indices that are the process of being deleted.
80998** If you are unlucky, one of those deleted indices or tables might
80999** have the same rootpage number as the real table or index that is
81000** being moved.  So we cannot stop searching after the first match
81001** because the first match might be for one of the deleted indices
81002** or tables and not the table/index that is actually being moved.
81003** We must continue looping until all tables and indices with
81004** rootpage==iFrom have been converted to have a rootpage of iTo
81005** in order to be certain that we got the right one.
81006*/
81007#ifndef SQLITE_OMIT_AUTOVACUUM
81008SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
81009  HashElem *pElem;
81010  Hash *pHash;
81011  Db *pDb;
81012
81013  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81014  pDb = &db->aDb[iDb];
81015  pHash = &pDb->pSchema->tblHash;
81016  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
81017    Table *pTab = sqliteHashData(pElem);
81018    if( pTab->tnum==iFrom ){
81019      pTab->tnum = iTo;
81020    }
81021  }
81022  pHash = &pDb->pSchema->idxHash;
81023  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
81024    Index *pIdx = sqliteHashData(pElem);
81025    if( pIdx->tnum==iFrom ){
81026      pIdx->tnum = iTo;
81027    }
81028  }
81029}
81030#endif
81031
81032/*
81033** Write code to erase the table with root-page iTable from database iDb.
81034** Also write code to modify the sqlite_master table and internal schema
81035** if a root-page of another table is moved by the btree-layer whilst
81036** erasing iTable (this can happen with an auto-vacuum database).
81037*/
81038static void destroyRootPage(Parse *pParse, int iTable, int iDb){
81039  Vdbe *v = sqlite3GetVdbe(pParse);
81040  int r1 = sqlite3GetTempReg(pParse);
81041  sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
81042  sqlite3MayAbort(pParse);
81043#ifndef SQLITE_OMIT_AUTOVACUUM
81044  /* OP_Destroy stores an in integer r1. If this integer
81045  ** is non-zero, then it is the root page number of a table moved to
81046  ** location iTable. The following code modifies the sqlite_master table to
81047  ** reflect this.
81048  **
81049  ** The "#NNN" in the SQL is a special constant that means whatever value
81050  ** is in register NNN.  See grammar rules associated with the TK_REGISTER
81051  ** token for additional information.
81052  */
81053  sqlite3NestedParse(pParse,
81054     "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
81055     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
81056#endif
81057  sqlite3ReleaseTempReg(pParse, r1);
81058}
81059
81060/*
81061** Write VDBE code to erase table pTab and all associated indices on disk.
81062** Code to update the sqlite_master tables and internal schema definitions
81063** in case a root-page belonging to another table is moved by the btree layer
81064** is also added (this can happen with an auto-vacuum database).
81065*/
81066static void destroyTable(Parse *pParse, Table *pTab){
81067#ifdef SQLITE_OMIT_AUTOVACUUM
81068  Index *pIdx;
81069  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81070  destroyRootPage(pParse, pTab->tnum, iDb);
81071  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81072    destroyRootPage(pParse, pIdx->tnum, iDb);
81073  }
81074#else
81075  /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
81076  ** is not defined), then it is important to call OP_Destroy on the
81077  ** table and index root-pages in order, starting with the numerically
81078  ** largest root-page number. This guarantees that none of the root-pages
81079  ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
81080  ** following were coded:
81081  **
81082  ** OP_Destroy 4 0
81083  ** ...
81084  ** OP_Destroy 5 0
81085  **
81086  ** and root page 5 happened to be the largest root-page number in the
81087  ** database, then root page 5 would be moved to page 4 by the
81088  ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
81089  ** a free-list page.
81090  */
81091  int iTab = pTab->tnum;
81092  int iDestroyed = 0;
81093
81094  while( 1 ){
81095    Index *pIdx;
81096    int iLargest = 0;
81097
81098    if( iDestroyed==0 || iTab<iDestroyed ){
81099      iLargest = iTab;
81100    }
81101    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81102      int iIdx = pIdx->tnum;
81103      assert( pIdx->pSchema==pTab->pSchema );
81104      if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
81105        iLargest = iIdx;
81106      }
81107    }
81108    if( iLargest==0 ){
81109      return;
81110    }else{
81111      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81112      destroyRootPage(pParse, iLargest, iDb);
81113      iDestroyed = iLargest;
81114    }
81115  }
81116#endif
81117}
81118
81119/*
81120** Remove entries from the sqlite_stat1 and sqlite_stat2 tables
81121** after a DROP INDEX or DROP TABLE command.
81122*/
81123static void sqlite3ClearStatTables(
81124  Parse *pParse,         /* The parsing context */
81125  int iDb,               /* The database number */
81126  const char *zType,     /* "idx" or "tbl" */
81127  const char *zName      /* Name of index or table */
81128){
81129  static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
81130  int i;
81131  const char *zDbName = pParse->db->aDb[iDb].zName;
81132  for(i=0; i<ArraySize(azStatTab); i++){
81133    if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
81134      sqlite3NestedParse(pParse,
81135        "DELETE FROM %Q.%s WHERE %s=%Q",
81136        zDbName, azStatTab[i], zType, zName
81137      );
81138    }
81139  }
81140}
81141
81142/*
81143** This routine is called to do the work of a DROP TABLE statement.
81144** pName is the name of the table to be dropped.
81145*/
81146SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
81147  Table *pTab;
81148  Vdbe *v;
81149  sqlite3 *db = pParse->db;
81150  int iDb;
81151
81152  if( db->mallocFailed ){
81153    goto exit_drop_table;
81154  }
81155  assert( pParse->nErr==0 );
81156  assert( pName->nSrc==1 );
81157  if( noErr ) db->suppressErr++;
81158  pTab = sqlite3LocateTable(pParse, isView,
81159                            pName->a[0].zName, pName->a[0].zDatabase);
81160  if( noErr ) db->suppressErr--;
81161
81162  if( pTab==0 ){
81163    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
81164    goto exit_drop_table;
81165  }
81166  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81167  assert( iDb>=0 && iDb<db->nDb );
81168
81169  /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
81170  ** it is initialized.
81171  */
81172  if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
81173    goto exit_drop_table;
81174  }
81175#ifndef SQLITE_OMIT_AUTHORIZATION
81176  {
81177    int code;
81178    const char *zTab = SCHEMA_TABLE(iDb);
81179    const char *zDb = db->aDb[iDb].zName;
81180    const char *zArg2 = 0;
81181    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
81182      goto exit_drop_table;
81183    }
81184    if( isView ){
81185      if( !OMIT_TEMPDB && iDb==1 ){
81186        code = SQLITE_DROP_TEMP_VIEW;
81187      }else{
81188        code = SQLITE_DROP_VIEW;
81189      }
81190#ifndef SQLITE_OMIT_VIRTUALTABLE
81191    }else if( IsVirtual(pTab) ){
81192      code = SQLITE_DROP_VTABLE;
81193      zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
81194#endif
81195    }else{
81196      if( !OMIT_TEMPDB && iDb==1 ){
81197        code = SQLITE_DROP_TEMP_TABLE;
81198      }else{
81199        code = SQLITE_DROP_TABLE;
81200      }
81201    }
81202    if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
81203      goto exit_drop_table;
81204    }
81205    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
81206      goto exit_drop_table;
81207    }
81208  }
81209#endif
81210  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
81211    sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
81212    goto exit_drop_table;
81213  }
81214
81215#ifndef SQLITE_OMIT_VIEW
81216  /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
81217  ** on a table.
81218  */
81219  if( isView && pTab->pSelect==0 ){
81220    sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
81221    goto exit_drop_table;
81222  }
81223  if( !isView && pTab->pSelect ){
81224    sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
81225    goto exit_drop_table;
81226  }
81227#endif
81228
81229  /* Generate code to remove the table from the master table
81230  ** on disk.
81231  */
81232  v = sqlite3GetVdbe(pParse);
81233  if( v ){
81234    Trigger *pTrigger;
81235    Db *pDb = &db->aDb[iDb];
81236    sqlite3BeginWriteOperation(pParse, 1, iDb);
81237
81238#ifndef SQLITE_OMIT_VIRTUALTABLE
81239    if( IsVirtual(pTab) ){
81240      sqlite3VdbeAddOp0(v, OP_VBegin);
81241    }
81242#endif
81243    sqlite3FkDropTable(pParse, pName, pTab);
81244
81245    /* Drop all triggers associated with the table being dropped. Code
81246    ** is generated to remove entries from sqlite_master and/or
81247    ** sqlite_temp_master if required.
81248    */
81249    pTrigger = sqlite3TriggerList(pParse, pTab);
81250    while( pTrigger ){
81251      assert( pTrigger->pSchema==pTab->pSchema ||
81252          pTrigger->pSchema==db->aDb[1].pSchema );
81253      sqlite3DropTriggerPtr(pParse, pTrigger);
81254      pTrigger = pTrigger->pNext;
81255    }
81256
81257#ifndef SQLITE_OMIT_AUTOINCREMENT
81258    /* Remove any entries of the sqlite_sequence table associated with
81259    ** the table being dropped. This is done before the table is dropped
81260    ** at the btree level, in case the sqlite_sequence table needs to
81261    ** move as a result of the drop (can happen in auto-vacuum mode).
81262    */
81263    if( pTab->tabFlags & TF_Autoincrement ){
81264      sqlite3NestedParse(pParse,
81265        "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81266        pDb->zName, pTab->zName
81267      );
81268    }
81269#endif
81270
81271    /* Drop all SQLITE_MASTER table and index entries that refer to the
81272    ** table. The program name loops through the master table and deletes
81273    ** every row that refers to a table of the same name as the one being
81274    ** dropped. Triggers are handled seperately because a trigger can be
81275    ** created in the temp database that refers to a table in another
81276    ** database.
81277    */
81278    sqlite3NestedParse(pParse,
81279        "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81280        pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81281    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81282    if( !isView && !IsVirtual(pTab) ){
81283      destroyTable(pParse, pTab);
81284    }
81285
81286    /* Remove the table entry from SQLite's internal schema and modify
81287    ** the schema cookie.
81288    */
81289    if( IsVirtual(pTab) ){
81290      sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81291    }
81292    sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81293    sqlite3ChangeCookie(pParse, iDb);
81294  }
81295  sqliteViewResetAll(db, iDb);
81296
81297exit_drop_table:
81298  sqlite3SrcListDelete(db, pName);
81299}
81300
81301/*
81302** This routine is called to create a new foreign key on the table
81303** currently under construction.  pFromCol determines which columns
81304** in the current table point to the foreign key.  If pFromCol==0 then
81305** connect the key to the last column inserted.  pTo is the name of
81306** the table referred to.  pToCol is a list of tables in the other
81307** pTo table that the foreign key points to.  flags contains all
81308** information about the conflict resolution algorithms specified
81309** in the ON DELETE, ON UPDATE and ON INSERT clauses.
81310**
81311** An FKey structure is created and added to the table currently
81312** under construction in the pParse->pNewTable field.
81313**
81314** The foreign key is set for IMMEDIATE processing.  A subsequent call
81315** to sqlite3DeferForeignKey() might change this to DEFERRED.
81316*/
81317SQLITE_PRIVATE void sqlite3CreateForeignKey(
81318  Parse *pParse,       /* Parsing context */
81319  ExprList *pFromCol,  /* Columns in this table that point to other table */
81320  Token *pTo,          /* Name of the other table */
81321  ExprList *pToCol,    /* Columns in the other table */
81322  int flags            /* Conflict resolution algorithms. */
81323){
81324  sqlite3 *db = pParse->db;
81325#ifndef SQLITE_OMIT_FOREIGN_KEY
81326  FKey *pFKey = 0;
81327  FKey *pNextTo;
81328  Table *p = pParse->pNewTable;
81329  int nByte;
81330  int i;
81331  int nCol;
81332  char *z;
81333
81334  assert( pTo!=0 );
81335  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
81336  if( pFromCol==0 ){
81337    int iCol = p->nCol-1;
81338    if( NEVER(iCol<0) ) goto fk_end;
81339    if( pToCol && pToCol->nExpr!=1 ){
81340      sqlite3ErrorMsg(pParse, "foreign key on %s"
81341         " should reference only one column of table %T",
81342         p->aCol[iCol].zName, pTo);
81343      goto fk_end;
81344    }
81345    nCol = 1;
81346  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
81347    sqlite3ErrorMsg(pParse,
81348        "number of columns in foreign key does not match the number of "
81349        "columns in the referenced table");
81350    goto fk_end;
81351  }else{
81352    nCol = pFromCol->nExpr;
81353  }
81354  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
81355  if( pToCol ){
81356    for(i=0; i<pToCol->nExpr; i++){
81357      nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
81358    }
81359  }
81360  pFKey = sqlite3DbMallocZero(db, nByte );
81361  if( pFKey==0 ){
81362    goto fk_end;
81363  }
81364  pFKey->pFrom = p;
81365  pFKey->pNextFrom = p->pFKey;
81366  z = (char*)&pFKey->aCol[nCol];
81367  pFKey->zTo = z;
81368  memcpy(z, pTo->z, pTo->n);
81369  z[pTo->n] = 0;
81370  sqlite3Dequote(z);
81371  z += pTo->n+1;
81372  pFKey->nCol = nCol;
81373  if( pFromCol==0 ){
81374    pFKey->aCol[0].iFrom = p->nCol-1;
81375  }else{
81376    for(i=0; i<nCol; i++){
81377      int j;
81378      for(j=0; j<p->nCol; j++){
81379        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
81380          pFKey->aCol[i].iFrom = j;
81381          break;
81382        }
81383      }
81384      if( j>=p->nCol ){
81385        sqlite3ErrorMsg(pParse,
81386          "unknown column \"%s\" in foreign key definition",
81387          pFromCol->a[i].zName);
81388        goto fk_end;
81389      }
81390    }
81391  }
81392  if( pToCol ){
81393    for(i=0; i<nCol; i++){
81394      int n = sqlite3Strlen30(pToCol->a[i].zName);
81395      pFKey->aCol[i].zCol = z;
81396      memcpy(z, pToCol->a[i].zName, n);
81397      z[n] = 0;
81398      z += n+1;
81399    }
81400  }
81401  pFKey->isDeferred = 0;
81402  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
81403  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
81404
81405  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
81406  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
81407      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
81408  );
81409  if( pNextTo==pFKey ){
81410    db->mallocFailed = 1;
81411    goto fk_end;
81412  }
81413  if( pNextTo ){
81414    assert( pNextTo->pPrevTo==0 );
81415    pFKey->pNextTo = pNextTo;
81416    pNextTo->pPrevTo = pFKey;
81417  }
81418
81419  /* Link the foreign key to the table as the last step.
81420  */
81421  p->pFKey = pFKey;
81422  pFKey = 0;
81423
81424fk_end:
81425  sqlite3DbFree(db, pFKey);
81426#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
81427  sqlite3ExprListDelete(db, pFromCol);
81428  sqlite3ExprListDelete(db, pToCol);
81429}
81430
81431/*
81432** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
81433** clause is seen as part of a foreign key definition.  The isDeferred
81434** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
81435** The behavior of the most recently created foreign key is adjusted
81436** accordingly.
81437*/
81438SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
81439#ifndef SQLITE_OMIT_FOREIGN_KEY
81440  Table *pTab;
81441  FKey *pFKey;
81442  if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
81443  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
81444  pFKey->isDeferred = (u8)isDeferred;
81445#endif
81446}
81447
81448/*
81449** Generate code that will erase and refill index *pIdx.  This is
81450** used to initialize a newly created index or to recompute the
81451** content of an index in response to a REINDEX command.
81452**
81453** if memRootPage is not negative, it means that the index is newly
81454** created.  The register specified by memRootPage contains the
81455** root page number of the index.  If memRootPage is negative, then
81456** the index already exists and must be cleared before being refilled and
81457** the root page number of the index is taken from pIndex->tnum.
81458*/
81459static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
81460  Table *pTab = pIndex->pTable;  /* The table that is indexed */
81461  int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
81462  int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
81463  int iSorter = iTab;            /* Cursor opened by OpenSorter (if in use) */
81464  int addr1;                     /* Address of top of loop */
81465  int addr2;                     /* Address to jump to for next iteration */
81466  int tnum;                      /* Root page of index */
81467  Vdbe *v;                       /* Generate code into this virtual machine */
81468  KeyInfo *pKey;                 /* KeyInfo for index */
81469  int regIdxKey;                 /* Registers containing the index key */
81470  int regRecord;                 /* Register holding assemblied index record */
81471  sqlite3 *db = pParse->db;      /* The database connection */
81472  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
81473
81474#ifndef SQLITE_OMIT_AUTHORIZATION
81475  if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
81476      db->aDb[iDb].zName ) ){
81477    return;
81478  }
81479#endif
81480
81481  /* Require a write-lock on the table to perform this operation */
81482  sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
81483
81484  v = sqlite3GetVdbe(pParse);
81485  if( v==0 ) return;
81486  if( memRootPage>=0 ){
81487    tnum = memRootPage;
81488  }else{
81489    tnum = pIndex->tnum;
81490    sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
81491  }
81492  pKey = sqlite3IndexKeyinfo(pParse, pIndex);
81493  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
81494                    (char *)pKey, P4_KEYINFO_HANDOFF);
81495  if( memRootPage>=0 ){
81496    sqlite3VdbeChangeP5(v, 1);
81497  }
81498
81499#ifndef SQLITE_OMIT_MERGE_SORT
81500  /* Open the sorter cursor if we are to use one. */
81501  iSorter = pParse->nTab++;
81502  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81503#endif
81504
81505  /* Open the table. Loop through all rows of the table, inserting index
81506  ** records into the sorter. */
81507  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
81508  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81509  addr2 = addr1 + 1;
81510  regRecord = sqlite3GetTempReg(pParse);
81511  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81512
81513#ifndef SQLITE_OMIT_MERGE_SORT
81514  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
81515  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81516  sqlite3VdbeJumpHere(v, addr1);
81517  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
81518  if( pIndex->onError!=OE_None ){
81519    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
81520    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
81521    addr2 = sqlite3VdbeCurrentAddr(v);
81522    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
81523    sqlite3HaltConstraint(
81524        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
81525    );
81526  }else{
81527    addr2 = sqlite3VdbeCurrentAddr(v);
81528  }
81529  sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
81530  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
81531  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81532#else
81533  if( pIndex->onError!=OE_None ){
81534    const int regRowid = regIdxKey + pIndex->nColumn;
81535    const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
81536    void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
81537
81538    /* The registers accessed by the OP_IsUnique opcode were allocated
81539    ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
81540    ** call above. Just before that function was freed they were released
81541    ** (made available to the compiler for reuse) using
81542    ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
81543    ** opcode use the values stored within seems dangerous. However, since
81544    ** we can be sure that no other temp registers have been allocated
81545    ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
81546    */
81547    sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
81548    sqlite3HaltConstraint(
81549        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
81550  }
81551  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
81552  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81553#endif
81554  sqlite3ReleaseTempReg(pParse, regRecord);
81555  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
81556  sqlite3VdbeJumpHere(v, addr1);
81557
81558  sqlite3VdbeAddOp1(v, OP_Close, iTab);
81559  sqlite3VdbeAddOp1(v, OP_Close, iIdx);
81560  sqlite3VdbeAddOp1(v, OP_Close, iSorter);
81561}
81562
81563/*
81564** Create a new index for an SQL table.  pName1.pName2 is the name of the index
81565** and pTblList is the name of the table that is to be indexed.  Both will
81566** be NULL for a primary key or an index that is created to satisfy a
81567** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
81568** as the table to be indexed.  pParse->pNewTable is a table that is
81569** currently being constructed by a CREATE TABLE statement.
81570**
81571** pList is a list of columns to be indexed.  pList will be NULL if this
81572** is a primary key or unique-constraint on the most recent column added
81573** to the table currently under construction.
81574**
81575** If the index is created successfully, return a pointer to the new Index
81576** structure. This is used by sqlite3AddPrimaryKey() to mark the index
81577** as the tables primary key (Index.autoIndex==2).
81578*/
81579SQLITE_PRIVATE Index *sqlite3CreateIndex(
81580  Parse *pParse,     /* All information about this parse */
81581  Token *pName1,     /* First part of index name. May be NULL */
81582  Token *pName2,     /* Second part of index name. May be NULL */
81583  SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
81584  ExprList *pList,   /* A list of columns to be indexed */
81585  int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
81586  Token *pStart,     /* The CREATE token that begins this statement */
81587  Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
81588  int sortOrder,     /* Sort order of primary key when pList==NULL */
81589  int ifNotExist     /* Omit error if index already exists */
81590){
81591  Index *pRet = 0;     /* Pointer to return */
81592  Table *pTab = 0;     /* Table to be indexed */
81593  Index *pIndex = 0;   /* The index to be created */
81594  char *zName = 0;     /* Name of the index */
81595  int nName;           /* Number of characters in zName */
81596  int i, j;
81597  Token nullId;        /* Fake token for an empty ID list */
81598  DbFixer sFix;        /* For assigning database names to pTable */
81599  int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
81600  sqlite3 *db = pParse->db;
81601  Db *pDb;             /* The specific table containing the indexed database */
81602  int iDb;             /* Index of the database that is being written */
81603  Token *pName = 0;    /* Unqualified name of the index to create */
81604  struct ExprList_item *pListItem; /* For looping over pList */
81605  int nCol;
81606  int nExtra = 0;
81607  char *zExtra;
81608
81609  assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
81610  assert( pParse->nErr==0 );      /* Never called with prior errors */
81611  if( db->mallocFailed || IN_DECLARE_VTAB ){
81612    goto exit_create_index;
81613  }
81614  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
81615    goto exit_create_index;
81616  }
81617
81618  /*
81619  ** Find the table that is to be indexed.  Return early if not found.
81620  */
81621  if( pTblName!=0 ){
81622
81623    /* Use the two-part index name to determine the database
81624    ** to search for the table. 'Fix' the table name to this db
81625    ** before looking up the table.
81626    */
81627    assert( pName1 && pName2 );
81628    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
81629    if( iDb<0 ) goto exit_create_index;
81630
81631#ifndef SQLITE_OMIT_TEMPDB
81632    /* If the index name was unqualified, check if the the table
81633    ** is a temp table. If so, set the database to 1. Do not do this
81634    ** if initialising a database schema.
81635    */
81636    if( !db->init.busy ){
81637      pTab = sqlite3SrcListLookup(pParse, pTblName);
81638      if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
81639        iDb = 1;
81640      }
81641    }
81642#endif
81643
81644    if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
81645        sqlite3FixSrcList(&sFix, pTblName)
81646    ){
81647      /* Because the parser constructs pTblName from a single identifier,
81648      ** sqlite3FixSrcList can never fail. */
81649      assert(0);
81650    }
81651    pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName,
81652        pTblName->a[0].zDatabase);
81653    if( !pTab || db->mallocFailed ) goto exit_create_index;
81654    assert( db->aDb[iDb].pSchema==pTab->pSchema );
81655  }else{
81656    assert( pName==0 );
81657    pTab = pParse->pNewTable;
81658    if( !pTab ) goto exit_create_index;
81659    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81660  }
81661  pDb = &db->aDb[iDb];
81662
81663  assert( pTab!=0 );
81664  assert( pParse->nErr==0 );
81665  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
81666       && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
81667    sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
81668    goto exit_create_index;
81669  }
81670#ifndef SQLITE_OMIT_VIEW
81671  if( pTab->pSelect ){
81672    sqlite3ErrorMsg(pParse, "views may not be indexed");
81673    goto exit_create_index;
81674  }
81675#endif
81676#ifndef SQLITE_OMIT_VIRTUALTABLE
81677  if( IsVirtual(pTab) ){
81678    sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
81679    goto exit_create_index;
81680  }
81681#endif
81682
81683  /*
81684  ** Find the name of the index.  Make sure there is not already another
81685  ** index or table with the same name.
81686  **
81687  ** Exception:  If we are reading the names of permanent indices from the
81688  ** sqlite_master table (because some other process changed the schema) and
81689  ** one of the index names collides with the name of a temporary table or
81690  ** index, then we will continue to process this index.
81691  **
81692  ** If pName==0 it means that we are
81693  ** dealing with a primary key or UNIQUE constraint.  We have to invent our
81694  ** own name.
81695  */
81696  if( pName ){
81697    zName = sqlite3NameFromToken(db, pName);
81698    if( zName==0 ) goto exit_create_index;
81699    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
81700      goto exit_create_index;
81701    }
81702    if( !db->init.busy ){
81703      if( sqlite3FindTable(db, zName, 0)!=0 ){
81704        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
81705        goto exit_create_index;
81706      }
81707    }
81708    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
81709      if( !ifNotExist ){
81710        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
81711      }else{
81712        assert( !db->init.busy );
81713        sqlite3CodeVerifySchema(pParse, iDb);
81714      }
81715      goto exit_create_index;
81716    }
81717  }else{
81718    int n;
81719    Index *pLoop;
81720    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
81721    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
81722    if( zName==0 ){
81723      goto exit_create_index;
81724    }
81725  }
81726
81727  /* Check for authorization to create an index.
81728  */
81729#ifndef SQLITE_OMIT_AUTHORIZATION
81730  {
81731    const char *zDb = pDb->zName;
81732    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
81733      goto exit_create_index;
81734    }
81735    i = SQLITE_CREATE_INDEX;
81736    if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
81737    if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
81738      goto exit_create_index;
81739    }
81740  }
81741#endif
81742
81743  /* If pList==0, it means this routine was called to make a primary
81744  ** key out of the last column added to the table under construction.
81745  ** So create a fake list to simulate this.
81746  */
81747  if( pList==0 ){
81748    nullId.z = pTab->aCol[pTab->nCol-1].zName;
81749    nullId.n = sqlite3Strlen30((char*)nullId.z);
81750    pList = sqlite3ExprListAppend(pParse, 0, 0);
81751    if( pList==0 ) goto exit_create_index;
81752    sqlite3ExprListSetName(pParse, pList, &nullId, 0);
81753    pList->a[0].sortOrder = (u8)sortOrder;
81754  }
81755
81756  /* Figure out how many bytes of space are required to store explicitly
81757  ** specified collation sequence names.
81758  */
81759  for(i=0; i<pList->nExpr; i++){
81760    Expr *pExpr = pList->a[i].pExpr;
81761    if( pExpr ){
81762      CollSeq *pColl = pExpr->pColl;
81763      /* Either pColl!=0 or there was an OOM failure.  But if an OOM
81764      ** failure we have quit before reaching this point. */
81765      if( ALWAYS(pColl) ){
81766        nExtra += (1 + sqlite3Strlen30(pColl->zName));
81767      }
81768    }
81769  }
81770
81771  /*
81772  ** Allocate the index structure.
81773  */
81774  nName = sqlite3Strlen30(zName);
81775  nCol = pList->nExpr;
81776  pIndex = sqlite3DbMallocZero(db,
81777      sizeof(Index) +              /* Index structure  */
81778      sizeof(int)*nCol +           /* Index.aiColumn   */
81779      sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
81780      sizeof(char *)*nCol +        /* Index.azColl     */
81781      sizeof(u8)*nCol +            /* Index.aSortOrder */
81782      nName + 1 +                  /* Index.zName      */
81783      nExtra                       /* Collation sequence names */
81784  );
81785  if( db->mallocFailed ){
81786    goto exit_create_index;
81787  }
81788  pIndex->azColl = (char**)(&pIndex[1]);
81789  pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
81790  pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81791  pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
81792  pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
81793  zExtra = (char *)(&pIndex->zName[nName+1]);
81794  memcpy(pIndex->zName, zName, nName+1);
81795  pIndex->pTable = pTab;
81796  pIndex->nColumn = pList->nExpr;
81797  pIndex->onError = (u8)onError;
81798  pIndex->autoIndex = (u8)(pName==0);
81799  pIndex->pSchema = db->aDb[iDb].pSchema;
81800  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81801
81802  /* Check to see if we should honor DESC requests on index columns
81803  */
81804  if( pDb->pSchema->file_format>=4 ){
81805    sortOrderMask = -1;   /* Honor DESC */
81806  }else{
81807    sortOrderMask = 0;    /* Ignore DESC */
81808  }
81809
81810  /* Scan the names of the columns of the table to be indexed and
81811  ** load the column indices into the Index structure.  Report an error
81812  ** if any column is not found.
81813  **
81814  ** TODO:  Add a test to make sure that the same column is not named
81815  ** more than once within the same index.  Only the first instance of
81816  ** the column will ever be used by the optimizer.  Note that using the
81817  ** same column more than once cannot be an error because that would
81818  ** break backwards compatibility - it needs to be a warning.
81819  */
81820  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
81821    const char *zColName = pListItem->zName;
81822    Column *pTabCol;
81823    int requestedSortOrder;
81824    char *zColl;                   /* Collation sequence name */
81825
81826    for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
81827      if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
81828    }
81829    if( j>=pTab->nCol ){
81830      sqlite3ErrorMsg(pParse, "table %s has no column named %s",
81831        pTab->zName, zColName);
81832      pParse->checkSchema = 1;
81833      goto exit_create_index;
81834    }
81835    pIndex->aiColumn[i] = j;
81836    /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
81837    ** the way the "idxlist" non-terminal is constructed by the parser,
81838    ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
81839    ** must exist or else there must have been an OOM error.  But if there
81840    ** was an OOM error, we would never reach this point. */
81841    if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
81842      int nColl;
81843      zColl = pListItem->pExpr->pColl->zName;
81844      nColl = sqlite3Strlen30(zColl) + 1;
81845      assert( nExtra>=nColl );
81846      memcpy(zExtra, zColl, nColl);
81847      zColl = zExtra;
81848      zExtra += nColl;
81849      nExtra -= nColl;
81850    }else{
81851      zColl = pTab->aCol[j].zColl;
81852      if( !zColl ){
81853        zColl = db->pDfltColl->zName;
81854      }
81855    }
81856    if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
81857      goto exit_create_index;
81858    }
81859    pIndex->azColl[i] = zColl;
81860    requestedSortOrder = pListItem->sortOrder & sortOrderMask;
81861    pIndex->aSortOrder[i] = (u8)requestedSortOrder;
81862  }
81863  sqlite3DefaultRowEst(pIndex);
81864
81865  if( pTab==pParse->pNewTable ){
81866    /* This routine has been called to create an automatic index as a
81867    ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
81868    ** a PRIMARY KEY or UNIQUE clause following the column definitions.
81869    ** i.e. one of:
81870    **
81871    ** CREATE TABLE t(x PRIMARY KEY, y);
81872    ** CREATE TABLE t(x, y, UNIQUE(x, y));
81873    **
81874    ** Either way, check to see if the table already has such an index. If
81875    ** so, don't bother creating this one. This only applies to
81876    ** automatically created indices. Users can do as they wish with
81877    ** explicit indices.
81878    **
81879    ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
81880    ** (and thus suppressing the second one) even if they have different
81881    ** sort orders.
81882    **
81883    ** If there are different collating sequences or if the columns of
81884    ** the constraint occur in different orders, then the constraints are
81885    ** considered distinct and both result in separate indices.
81886    */
81887    Index *pIdx;
81888    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81889      int k;
81890      assert( pIdx->onError!=OE_None );
81891      assert( pIdx->autoIndex );
81892      assert( pIndex->onError!=OE_None );
81893
81894      if( pIdx->nColumn!=pIndex->nColumn ) continue;
81895      for(k=0; k<pIdx->nColumn; k++){
81896        const char *z1;
81897        const char *z2;
81898        if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
81899        z1 = pIdx->azColl[k];
81900        z2 = pIndex->azColl[k];
81901        if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
81902      }
81903      if( k==pIdx->nColumn ){
81904        if( pIdx->onError!=pIndex->onError ){
81905          /* This constraint creates the same index as a previous
81906          ** constraint specified somewhere in the CREATE TABLE statement.
81907          ** However the ON CONFLICT clauses are different. If both this
81908          ** constraint and the previous equivalent constraint have explicit
81909          ** ON CONFLICT clauses this is an error. Otherwise, use the
81910          ** explicitly specified behaviour for the index.
81911          */
81912          if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
81913            sqlite3ErrorMsg(pParse,
81914                "conflicting ON CONFLICT clauses specified", 0);
81915          }
81916          if( pIdx->onError==OE_Default ){
81917            pIdx->onError = pIndex->onError;
81918          }
81919        }
81920        goto exit_create_index;
81921      }
81922    }
81923  }
81924
81925  /* Link the new Index structure to its table and to the other
81926  ** in-memory database structures.
81927  */
81928  if( db->init.busy ){
81929    Index *p;
81930    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
81931    p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
81932                          pIndex->zName, sqlite3Strlen30(pIndex->zName),
81933                          pIndex);
81934    if( p ){
81935      assert( p==pIndex );  /* Malloc must have failed */
81936      db->mallocFailed = 1;
81937      goto exit_create_index;
81938    }
81939    db->flags |= SQLITE_InternChanges;
81940    if( pTblName!=0 ){
81941      pIndex->tnum = db->init.newTnum;
81942    }
81943  }
81944
81945  /* If the db->init.busy is 0 then create the index on disk.  This
81946  ** involves writing the index into the master table and filling in the
81947  ** index with the current table contents.
81948  **
81949  ** The db->init.busy is 0 when the user first enters a CREATE INDEX
81950  ** command.  db->init.busy is 1 when a database is opened and
81951  ** CREATE INDEX statements are read out of the master table.  In
81952  ** the latter case the index already exists on disk, which is why
81953  ** we don't want to recreate it.
81954  **
81955  ** If pTblName==0 it means this index is generated as a primary key
81956  ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
81957  ** has just been created, it contains no data and the index initialization
81958  ** step can be skipped.
81959  */
81960  else{ /* if( db->init.busy==0 ) */
81961    Vdbe *v;
81962    char *zStmt;
81963    int iMem = ++pParse->nMem;
81964
81965    v = sqlite3GetVdbe(pParse);
81966    if( v==0 ) goto exit_create_index;
81967
81968
81969    /* Create the rootpage for the index
81970    */
81971    sqlite3BeginWriteOperation(pParse, 1, iDb);
81972    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
81973
81974    /* Gather the complete text of the CREATE INDEX statement into
81975    ** the zStmt variable
81976    */
81977    if( pStart ){
81978      assert( pEnd!=0 );
81979      /* A named index with an explicit CREATE INDEX statement */
81980      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
81981        onError==OE_None ? "" : " UNIQUE",
81982        (int)(pEnd->z - pName->z) + 1,
81983        pName->z);
81984    }else{
81985      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
81986      /* zStmt = sqlite3MPrintf(""); */
81987      zStmt = 0;
81988    }
81989
81990    /* Add an entry in sqlite_master for this index
81991    */
81992    sqlite3NestedParse(pParse,
81993        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
81994        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
81995        pIndex->zName,
81996        pTab->zName,
81997        iMem,
81998        zStmt
81999    );
82000    sqlite3DbFree(db, zStmt);
82001
82002    /* Fill the index with data and reparse the schema. Code an OP_Expire
82003    ** to invalidate all pre-compiled statements.
82004    */
82005    if( pTblName ){
82006      sqlite3RefillIndex(pParse, pIndex, iMem);
82007      sqlite3ChangeCookie(pParse, iDb);
82008      sqlite3VdbeAddParseSchemaOp(v, iDb,
82009         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
82010      sqlite3VdbeAddOp1(v, OP_Expire, 0);
82011    }
82012  }
82013
82014  /* When adding an index to the list of indices for a table, make
82015  ** sure all indices labeled OE_Replace come after all those labeled
82016  ** OE_Ignore.  This is necessary for the correct constraint check
82017  ** processing (in sqlite3GenerateConstraintChecks()) as part of
82018  ** UPDATE and INSERT statements.
82019  */
82020  if( db->init.busy || pTblName==0 ){
82021    if( onError!=OE_Replace || pTab->pIndex==0
82022         || pTab->pIndex->onError==OE_Replace){
82023      pIndex->pNext = pTab->pIndex;
82024      pTab->pIndex = pIndex;
82025    }else{
82026      Index *pOther = pTab->pIndex;
82027      while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
82028        pOther = pOther->pNext;
82029      }
82030      pIndex->pNext = pOther->pNext;
82031      pOther->pNext = pIndex;
82032    }
82033    pRet = pIndex;
82034    pIndex = 0;
82035  }
82036
82037  /* Clean up before exiting */
82038exit_create_index:
82039  if( pIndex ){
82040    sqlite3DbFree(db, pIndex->zColAff);
82041    sqlite3DbFree(db, pIndex);
82042  }
82043  sqlite3ExprListDelete(db, pList);
82044  sqlite3SrcListDelete(db, pTblName);
82045  sqlite3DbFree(db, zName);
82046  return pRet;
82047}
82048
82049/*
82050** Fill the Index.aiRowEst[] array with default information - information
82051** to be used when we have not run the ANALYZE command.
82052**
82053** aiRowEst[0] is suppose to contain the number of elements in the index.
82054** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
82055** number of rows in the table that match any particular value of the
82056** first column of the index.  aiRowEst[2] is an estimate of the number
82057** of rows that match any particular combiniation of the first 2 columns
82058** of the index.  And so forth.  It must always be the case that
82059*
82060**           aiRowEst[N]<=aiRowEst[N-1]
82061**           aiRowEst[N]>=1
82062**
82063** Apart from that, we have little to go on besides intuition as to
82064** how aiRowEst[] should be initialized.  The numbers generated here
82065** are based on typical values found in actual indices.
82066*/
82067SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
82068  unsigned *a = pIdx->aiRowEst;
82069  int i;
82070  unsigned n;
82071  assert( a!=0 );
82072  a[0] = pIdx->pTable->nRowEst;
82073  if( a[0]<10 ) a[0] = 10;
82074  n = 10;
82075  for(i=1; i<=pIdx->nColumn; i++){
82076    a[i] = n;
82077    if( n>5 ) n--;
82078  }
82079  if( pIdx->onError!=OE_None ){
82080    a[pIdx->nColumn] = 1;
82081  }
82082}
82083
82084/*
82085** This routine will drop an existing named index.  This routine
82086** implements the DROP INDEX statement.
82087*/
82088SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
82089  Index *pIndex;
82090  Vdbe *v;
82091  sqlite3 *db = pParse->db;
82092  int iDb;
82093
82094  assert( pParse->nErr==0 );   /* Never called with prior errors */
82095  if( db->mallocFailed ){
82096    goto exit_drop_index;
82097  }
82098  assert( pName->nSrc==1 );
82099  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
82100    goto exit_drop_index;
82101  }
82102  pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
82103  if( pIndex==0 ){
82104    if( !ifExists ){
82105      sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
82106    }else{
82107      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
82108    }
82109    pParse->checkSchema = 1;
82110    goto exit_drop_index;
82111  }
82112  if( pIndex->autoIndex ){
82113    sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
82114      "or PRIMARY KEY constraint cannot be dropped", 0);
82115    goto exit_drop_index;
82116  }
82117  iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
82118#ifndef SQLITE_OMIT_AUTHORIZATION
82119  {
82120    int code = SQLITE_DROP_INDEX;
82121    Table *pTab = pIndex->pTable;
82122    const char *zDb = db->aDb[iDb].zName;
82123    const char *zTab = SCHEMA_TABLE(iDb);
82124    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
82125      goto exit_drop_index;
82126    }
82127    if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
82128    if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
82129      goto exit_drop_index;
82130    }
82131  }
82132#endif
82133
82134  /* Generate code to remove the index and from the master table */
82135  v = sqlite3GetVdbe(pParse);
82136  if( v ){
82137    sqlite3BeginWriteOperation(pParse, 1, iDb);
82138    sqlite3NestedParse(pParse,
82139       "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
82140       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
82141    );
82142    sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
82143    sqlite3ChangeCookie(pParse, iDb);
82144    destroyRootPage(pParse, pIndex->tnum, iDb);
82145    sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
82146  }
82147
82148exit_drop_index:
82149  sqlite3SrcListDelete(db, pName);
82150}
82151
82152/*
82153** pArray is a pointer to an array of objects.  Each object in the
82154** array is szEntry bytes in size.  This routine allocates a new
82155** object on the end of the array.
82156**
82157** *pnEntry is the number of entries already in use.  *pnAlloc is
82158** the previously allocated size of the array.  initSize is the
82159** suggested initial array size allocation.
82160**
82161** The index of the new entry is returned in *pIdx.
82162**
82163** This routine returns a pointer to the array of objects.  This
82164** might be the same as the pArray parameter or it might be a different
82165** pointer if the array was resized.
82166*/
82167SQLITE_PRIVATE void *sqlite3ArrayAllocate(
82168  sqlite3 *db,      /* Connection to notify of malloc failures */
82169  void *pArray,     /* Array of objects.  Might be reallocated */
82170  int szEntry,      /* Size of each object in the array */
82171  int initSize,     /* Suggested initial allocation, in elements */
82172  int *pnEntry,     /* Number of objects currently in use */
82173  int *pnAlloc,     /* Current size of the allocation, in elements */
82174  int *pIdx         /* Write the index of a new slot here */
82175){
82176  char *z;
82177  if( *pnEntry >= *pnAlloc ){
82178    void *pNew;
82179    int newSize;
82180    newSize = (*pnAlloc)*2 + initSize;
82181    pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
82182    if( pNew==0 ){
82183      *pIdx = -1;
82184      return pArray;
82185    }
82186    *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
82187    pArray = pNew;
82188  }
82189  z = (char*)pArray;
82190  memset(&z[*pnEntry * szEntry], 0, szEntry);
82191  *pIdx = *pnEntry;
82192  ++*pnEntry;
82193  return pArray;
82194}
82195
82196/*
82197** Append a new element to the given IdList.  Create a new IdList if
82198** need be.
82199**
82200** A new IdList is returned, or NULL if malloc() fails.
82201*/
82202SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
82203  int i;
82204  if( pList==0 ){
82205    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
82206    if( pList==0 ) return 0;
82207    pList->nAlloc = 0;
82208  }
82209  pList->a = sqlite3ArrayAllocate(
82210      db,
82211      pList->a,
82212      sizeof(pList->a[0]),
82213      5,
82214      &pList->nId,
82215      &pList->nAlloc,
82216      &i
82217  );
82218  if( i<0 ){
82219    sqlite3IdListDelete(db, pList);
82220    return 0;
82221  }
82222  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
82223  return pList;
82224}
82225
82226/*
82227** Delete an IdList.
82228*/
82229SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
82230  int i;
82231  if( pList==0 ) return;
82232  for(i=0; i<pList->nId; i++){
82233    sqlite3DbFree(db, pList->a[i].zName);
82234  }
82235  sqlite3DbFree(db, pList->a);
82236  sqlite3DbFree(db, pList);
82237}
82238
82239/*
82240** Return the index in pList of the identifier named zId.  Return -1
82241** if not found.
82242*/
82243SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
82244  int i;
82245  if( pList==0 ) return -1;
82246  for(i=0; i<pList->nId; i++){
82247    if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
82248  }
82249  return -1;
82250}
82251
82252/*
82253** Expand the space allocated for the given SrcList object by
82254** creating nExtra new slots beginning at iStart.  iStart is zero based.
82255** New slots are zeroed.
82256**
82257** For example, suppose a SrcList initially contains two entries: A,B.
82258** To append 3 new entries onto the end, do this:
82259**
82260**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
82261**
82262** After the call above it would contain:  A, B, nil, nil, nil.
82263** If the iStart argument had been 1 instead of 2, then the result
82264** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
82265** the iStart value would be 0.  The result then would
82266** be: nil, nil, nil, A, B.
82267**
82268** If a memory allocation fails the SrcList is unchanged.  The
82269** db->mallocFailed flag will be set to true.
82270*/
82271SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
82272  sqlite3 *db,       /* Database connection to notify of OOM errors */
82273  SrcList *pSrc,     /* The SrcList to be enlarged */
82274  int nExtra,        /* Number of new slots to add to pSrc->a[] */
82275  int iStart         /* Index in pSrc->a[] of first new slot */
82276){
82277  int i;
82278
82279  /* Sanity checking on calling parameters */
82280  assert( iStart>=0 );
82281  assert( nExtra>=1 );
82282  assert( pSrc!=0 );
82283  assert( iStart<=pSrc->nSrc );
82284
82285  /* Allocate additional space if needed */
82286  if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
82287    SrcList *pNew;
82288    int nAlloc = pSrc->nSrc+nExtra;
82289    int nGot;
82290    pNew = sqlite3DbRealloc(db, pSrc,
82291               sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
82292    if( pNew==0 ){
82293      assert( db->mallocFailed );
82294      return pSrc;
82295    }
82296    pSrc = pNew;
82297    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
82298    pSrc->nAlloc = (u16)nGot;
82299  }
82300
82301  /* Move existing slots that come after the newly inserted slots
82302  ** out of the way */
82303  for(i=pSrc->nSrc-1; i>=iStart; i--){
82304    pSrc->a[i+nExtra] = pSrc->a[i];
82305  }
82306  pSrc->nSrc += (i16)nExtra;
82307
82308  /* Zero the newly allocated slots */
82309  memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
82310  for(i=iStart; i<iStart+nExtra; i++){
82311    pSrc->a[i].iCursor = -1;
82312  }
82313
82314  /* Return a pointer to the enlarged SrcList */
82315  return pSrc;
82316}
82317
82318
82319/*
82320** Append a new table name to the given SrcList.  Create a new SrcList if
82321** need be.  A new entry is created in the SrcList even if pTable is NULL.
82322**
82323** A SrcList is returned, or NULL if there is an OOM error.  The returned
82324** SrcList might be the same as the SrcList that was input or it might be
82325** a new one.  If an OOM error does occurs, then the prior value of pList
82326** that is input to this routine is automatically freed.
82327**
82328** If pDatabase is not null, it means that the table has an optional
82329** database name prefix.  Like this:  "database.table".  The pDatabase
82330** points to the table name and the pTable points to the database name.
82331** The SrcList.a[].zName field is filled with the table name which might
82332** come from pTable (if pDatabase is NULL) or from pDatabase.
82333** SrcList.a[].zDatabase is filled with the database name from pTable,
82334** or with NULL if no database is specified.
82335**
82336** In other words, if call like this:
82337**
82338**         sqlite3SrcListAppend(D,A,B,0);
82339**
82340** Then B is a table name and the database name is unspecified.  If called
82341** like this:
82342**
82343**         sqlite3SrcListAppend(D,A,B,C);
82344**
82345** Then C is the table name and B is the database name.  If C is defined
82346** then so is B.  In other words, we never have a case where:
82347**
82348**         sqlite3SrcListAppend(D,A,0,C);
82349**
82350** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
82351** before being added to the SrcList.
82352*/
82353SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
82354  sqlite3 *db,        /* Connection to notify of malloc failures */
82355  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
82356  Token *pTable,      /* Table to append */
82357  Token *pDatabase    /* Database of the table */
82358){
82359  struct SrcList_item *pItem;
82360  assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
82361  if( pList==0 ){
82362    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
82363    if( pList==0 ) return 0;
82364    pList->nAlloc = 1;
82365  }
82366  pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
82367  if( db->mallocFailed ){
82368    sqlite3SrcListDelete(db, pList);
82369    return 0;
82370  }
82371  pItem = &pList->a[pList->nSrc-1];
82372  if( pDatabase && pDatabase->z==0 ){
82373    pDatabase = 0;
82374  }
82375  if( pDatabase ){
82376    Token *pTemp = pDatabase;
82377    pDatabase = pTable;
82378    pTable = pTemp;
82379  }
82380  pItem->zName = sqlite3NameFromToken(db, pTable);
82381  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
82382  return pList;
82383}
82384
82385/*
82386** Assign VdbeCursor index numbers to all tables in a SrcList
82387*/
82388SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
82389  int i;
82390  struct SrcList_item *pItem;
82391  assert(pList || pParse->db->mallocFailed );
82392  if( pList ){
82393    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
82394      if( pItem->iCursor>=0 ) break;
82395      pItem->iCursor = pParse->nTab++;
82396      if( pItem->pSelect ){
82397        sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
82398      }
82399    }
82400  }
82401}
82402
82403/*
82404** Delete an entire SrcList including all its substructure.
82405*/
82406SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
82407  int i;
82408  struct SrcList_item *pItem;
82409  if( pList==0 ) return;
82410  for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
82411    sqlite3DbFree(db, pItem->zDatabase);
82412    sqlite3DbFree(db, pItem->zName);
82413    sqlite3DbFree(db, pItem->zAlias);
82414    sqlite3DbFree(db, pItem->zIndex);
82415    sqlite3DeleteTable(db, pItem->pTab);
82416    sqlite3SelectDelete(db, pItem->pSelect);
82417    sqlite3ExprDelete(db, pItem->pOn);
82418    sqlite3IdListDelete(db, pItem->pUsing);
82419  }
82420  sqlite3DbFree(db, pList);
82421}
82422
82423/*
82424** This routine is called by the parser to add a new term to the
82425** end of a growing FROM clause.  The "p" parameter is the part of
82426** the FROM clause that has already been constructed.  "p" is NULL
82427** if this is the first term of the FROM clause.  pTable and pDatabase
82428** are the name of the table and database named in the FROM clause term.
82429** pDatabase is NULL if the database name qualifier is missing - the
82430** usual case.  If the term has a alias, then pAlias points to the
82431** alias token.  If the term is a subquery, then pSubquery is the
82432** SELECT statement that the subquery encodes.  The pTable and
82433** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
82434** parameters are the content of the ON and USING clauses.
82435**
82436** Return a new SrcList which encodes is the FROM with the new
82437** term added.
82438*/
82439SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
82440  Parse *pParse,          /* Parsing context */
82441  SrcList *p,             /* The left part of the FROM clause already seen */
82442  Token *pTable,          /* Name of the table to add to the FROM clause */
82443  Token *pDatabase,       /* Name of the database containing pTable */
82444  Token *pAlias,          /* The right-hand side of the AS subexpression */
82445  Select *pSubquery,      /* A subquery used in place of a table name */
82446  Expr *pOn,              /* The ON clause of a join */
82447  IdList *pUsing          /* The USING clause of a join */
82448){
82449  struct SrcList_item *pItem;
82450  sqlite3 *db = pParse->db;
82451  if( !p && (pOn || pUsing) ){
82452    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
82453      (pOn ? "ON" : "USING")
82454    );
82455    goto append_from_error;
82456  }
82457  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
82458  if( p==0 || NEVER(p->nSrc==0) ){
82459    goto append_from_error;
82460  }
82461  pItem = &p->a[p->nSrc-1];
82462  assert( pAlias!=0 );
82463  if( pAlias->n ){
82464    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
82465  }
82466  pItem->pSelect = pSubquery;
82467  pItem->pOn = pOn;
82468  pItem->pUsing = pUsing;
82469  return p;
82470
82471 append_from_error:
82472  assert( p==0 );
82473  sqlite3ExprDelete(db, pOn);
82474  sqlite3IdListDelete(db, pUsing);
82475  sqlite3SelectDelete(db, pSubquery);
82476  return 0;
82477}
82478
82479/*
82480** Add an INDEXED BY or NOT INDEXED clause to the most recently added
82481** element of the source-list passed as the second argument.
82482*/
82483SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
82484  assert( pIndexedBy!=0 );
82485  if( p && ALWAYS(p->nSrc>0) ){
82486    struct SrcList_item *pItem = &p->a[p->nSrc-1];
82487    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
82488    if( pIndexedBy->n==1 && !pIndexedBy->z ){
82489      /* A "NOT INDEXED" clause was supplied. See parse.y
82490      ** construct "indexed_opt" for details. */
82491      pItem->notIndexed = 1;
82492    }else{
82493      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
82494    }
82495  }
82496}
82497
82498/*
82499** When building up a FROM clause in the parser, the join operator
82500** is initially attached to the left operand.  But the code generator
82501** expects the join operator to be on the right operand.  This routine
82502** Shifts all join operators from left to right for an entire FROM
82503** clause.
82504**
82505** Example: Suppose the join is like this:
82506**
82507**           A natural cross join B
82508**
82509** The operator is "natural cross join".  The A and B operands are stored
82510** in p->a[0] and p->a[1], respectively.  The parser initially stores the
82511** operator with A.  This routine shifts that operator over to B.
82512*/
82513SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
82514  if( p ){
82515    int i;
82516    assert( p->a || p->nSrc==0 );
82517    for(i=p->nSrc-1; i>0; i--){
82518      p->a[i].jointype = p->a[i-1].jointype;
82519    }
82520    p->a[0].jointype = 0;
82521  }
82522}
82523
82524/*
82525** Begin a transaction
82526*/
82527SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
82528  sqlite3 *db;
82529  Vdbe *v;
82530  int i;
82531
82532  assert( pParse!=0 );
82533  db = pParse->db;
82534  assert( db!=0 );
82535/*  if( db->aDb[0].pBt==0 ) return; */
82536  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
82537    return;
82538  }
82539  v = sqlite3GetVdbe(pParse);
82540  if( !v ) return;
82541  if( type!=TK_DEFERRED ){
82542    for(i=0; i<db->nDb; i++){
82543      sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
82544      sqlite3VdbeUsesBtree(v, i);
82545    }
82546  }
82547  sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
82548}
82549
82550/*
82551** Commit a transaction
82552*/
82553SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
82554  sqlite3 *db;
82555  Vdbe *v;
82556
82557  assert( pParse!=0 );
82558  db = pParse->db;
82559  assert( db!=0 );
82560/*  if( db->aDb[0].pBt==0 ) return; */
82561  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
82562    return;
82563  }
82564  v = sqlite3GetVdbe(pParse);
82565  if( v ){
82566    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
82567  }
82568}
82569
82570/*
82571** Rollback a transaction
82572*/
82573SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
82574  sqlite3 *db;
82575  Vdbe *v;
82576
82577  assert( pParse!=0 );
82578  db = pParse->db;
82579  assert( db!=0 );
82580/*  if( db->aDb[0].pBt==0 ) return; */
82581  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
82582    return;
82583  }
82584  v = sqlite3GetVdbe(pParse);
82585  if( v ){
82586    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
82587  }
82588}
82589
82590/*
82591** This function is called by the parser when it parses a command to create,
82592** release or rollback an SQL savepoint.
82593*/
82594SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
82595  char *zName = sqlite3NameFromToken(pParse->db, pName);
82596  if( zName ){
82597    Vdbe *v = sqlite3GetVdbe(pParse);
82598#ifndef SQLITE_OMIT_AUTHORIZATION
82599    static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
82600    assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
82601#endif
82602    if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
82603      sqlite3DbFree(pParse->db, zName);
82604      return;
82605    }
82606    sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
82607  }
82608}
82609
82610/*
82611** Make sure the TEMP database is open and available for use.  Return
82612** the number of errors.  Leave any error messages in the pParse structure.
82613*/
82614SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
82615  sqlite3 *db = pParse->db;
82616  if( db->aDb[1].pBt==0 && !pParse->explain ){
82617    int rc;
82618    Btree *pBt;
82619    static const int flags =
82620          SQLITE_OPEN_READWRITE |
82621          SQLITE_OPEN_CREATE |
82622          SQLITE_OPEN_EXCLUSIVE |
82623          SQLITE_OPEN_DELETEONCLOSE |
82624          SQLITE_OPEN_TEMP_DB;
82625
82626    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
82627    if( rc!=SQLITE_OK ){
82628      sqlite3ErrorMsg(pParse, "unable to open a temporary database "
82629        "file for storing temporary tables");
82630      pParse->rc = rc;
82631      return 1;
82632    }
82633    db->aDb[1].pBt = pBt;
82634    assert( db->aDb[1].pSchema );
82635    if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
82636      db->mallocFailed = 1;
82637      return 1;
82638    }
82639  }
82640  return 0;
82641}
82642
82643/*
82644** Generate VDBE code that will verify the schema cookie and start
82645** a read-transaction for all named database files.
82646**
82647** It is important that all schema cookies be verified and all
82648** read transactions be started before anything else happens in
82649** the VDBE program.  But this routine can be called after much other
82650** code has been generated.  So here is what we do:
82651**
82652** The first time this routine is called, we code an OP_Goto that
82653** will jump to a subroutine at the end of the program.  Then we
82654** record every database that needs its schema verified in the
82655** pParse->cookieMask field.  Later, after all other code has been
82656** generated, the subroutine that does the cookie verifications and
82657** starts the transactions will be coded and the OP_Goto P2 value
82658** will be made to point to that subroutine.  The generation of the
82659** cookie verification subroutine code happens in sqlite3FinishCoding().
82660**
82661** If iDb<0 then code the OP_Goto only - don't set flag to verify the
82662** schema on any databases.  This can be used to position the OP_Goto
82663** early in the code, before we know if any database tables will be used.
82664*/
82665SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
82666  Parse *pToplevel = sqlite3ParseToplevel(pParse);
82667
82668  if( pToplevel->cookieGoto==0 ){
82669    Vdbe *v = sqlite3GetVdbe(pToplevel);
82670    if( v==0 ) return;  /* This only happens if there was a prior error */
82671    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
82672  }
82673  if( iDb>=0 ){
82674    sqlite3 *db = pToplevel->db;
82675    yDbMask mask;
82676
82677    assert( iDb<db->nDb );
82678    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
82679    assert( iDb<SQLITE_MAX_ATTACHED+2 );
82680    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82681    mask = ((yDbMask)1)<<iDb;
82682    if( (pToplevel->cookieMask & mask)==0 ){
82683      pToplevel->cookieMask |= mask;
82684      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
82685      if( !OMIT_TEMPDB && iDb==1 ){
82686        sqlite3OpenTempDatabase(pToplevel);
82687      }
82688    }
82689  }
82690}
82691
82692/*
82693** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
82694** attached database. Otherwise, invoke it for the database named zDb only.
82695*/
82696SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
82697  sqlite3 *db = pParse->db;
82698  int i;
82699  for(i=0; i<db->nDb; i++){
82700    Db *pDb = &db->aDb[i];
82701    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
82702      sqlite3CodeVerifySchema(pParse, i);
82703    }
82704  }
82705}
82706
82707/*
82708** Generate VDBE code that prepares for doing an operation that
82709** might change the database.
82710**
82711** This routine starts a new transaction if we are not already within
82712** a transaction.  If we are already within a transaction, then a checkpoint
82713** is set if the setStatement parameter is true.  A checkpoint should
82714** be set for operations that might fail (due to a constraint) part of
82715** the way through and which will need to undo some writes without having to
82716** rollback the whole transaction.  For operations where all constraints
82717** can be checked before any changes are made to the database, it is never
82718** necessary to undo a write and the checkpoint should not be set.
82719*/
82720SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
82721  Parse *pToplevel = sqlite3ParseToplevel(pParse);
82722  sqlite3CodeVerifySchema(pParse, iDb);
82723  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
82724  pToplevel->isMultiWrite |= setStatement;
82725}
82726
82727/*
82728** Indicate that the statement currently under construction might write
82729** more than one entry (example: deleting one row then inserting another,
82730** inserting multiple rows in a table, or inserting a row and index entries.)
82731** If an abort occurs after some of these writes have completed, then it will
82732** be necessary to undo the completed writes.
82733*/
82734SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
82735  Parse *pToplevel = sqlite3ParseToplevel(pParse);
82736  pToplevel->isMultiWrite = 1;
82737}
82738
82739/*
82740** The code generator calls this routine if is discovers that it is
82741** possible to abort a statement prior to completion.  In order to
82742** perform this abort without corrupting the database, we need to make
82743** sure that the statement is protected by a statement transaction.
82744**
82745** Technically, we only need to set the mayAbort flag if the
82746** isMultiWrite flag was previously set.  There is a time dependency
82747** such that the abort must occur after the multiwrite.  This makes
82748** some statements involving the REPLACE conflict resolution algorithm
82749** go a little faster.  But taking advantage of this time dependency
82750** makes it more difficult to prove that the code is correct (in
82751** particular, it prevents us from writing an effective
82752** implementation of sqlite3AssertMayAbort()) and so we have chosen
82753** to take the safe route and skip the optimization.
82754*/
82755SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
82756  Parse *pToplevel = sqlite3ParseToplevel(pParse);
82757  pToplevel->mayAbort = 1;
82758}
82759
82760/*
82761** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
82762** error. The onError parameter determines which (if any) of the statement
82763** and/or current transaction is rolled back.
82764*/
82765SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
82766  Vdbe *v = sqlite3GetVdbe(pParse);
82767  if( onError==OE_Abort ){
82768    sqlite3MayAbort(pParse);
82769  }
82770  sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
82771}
82772
82773/*
82774** Check to see if pIndex uses the collating sequence pColl.  Return
82775** true if it does and false if it does not.
82776*/
82777#ifndef SQLITE_OMIT_REINDEX
82778static int collationMatch(const char *zColl, Index *pIndex){
82779  int i;
82780  assert( zColl!=0 );
82781  for(i=0; i<pIndex->nColumn; i++){
82782    const char *z = pIndex->azColl[i];
82783    assert( z!=0 );
82784    if( 0==sqlite3StrICmp(z, zColl) ){
82785      return 1;
82786    }
82787  }
82788  return 0;
82789}
82790#endif
82791
82792/*
82793** Recompute all indices of pTab that use the collating sequence pColl.
82794** If pColl==0 then recompute all indices of pTab.
82795*/
82796#ifndef SQLITE_OMIT_REINDEX
82797static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
82798  Index *pIndex;              /* An index associated with pTab */
82799
82800  for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
82801    if( zColl==0 || collationMatch(zColl, pIndex) ){
82802      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
82803      sqlite3BeginWriteOperation(pParse, 0, iDb);
82804      sqlite3RefillIndex(pParse, pIndex, -1);
82805    }
82806  }
82807}
82808#endif
82809
82810/*
82811** Recompute all indices of all tables in all databases where the
82812** indices use the collating sequence pColl.  If pColl==0 then recompute
82813** all indices everywhere.
82814*/
82815#ifndef SQLITE_OMIT_REINDEX
82816static void reindexDatabases(Parse *pParse, char const *zColl){
82817  Db *pDb;                    /* A single database */
82818  int iDb;                    /* The database index number */
82819  sqlite3 *db = pParse->db;   /* The database connection */
82820  HashElem *k;                /* For looping over tables in pDb */
82821  Table *pTab;                /* A table in the database */
82822
82823  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
82824  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
82825    assert( pDb!=0 );
82826    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
82827      pTab = (Table*)sqliteHashData(k);
82828      reindexTable(pParse, pTab, zColl);
82829    }
82830  }
82831}
82832#endif
82833
82834/*
82835** Generate code for the REINDEX command.
82836**
82837**        REINDEX                            -- 1
82838**        REINDEX  <collation>               -- 2
82839**        REINDEX  ?<database>.?<tablename>  -- 3
82840**        REINDEX  ?<database>.?<indexname>  -- 4
82841**
82842** Form 1 causes all indices in all attached databases to be rebuilt.
82843** Form 2 rebuilds all indices in all databases that use the named
82844** collating function.  Forms 3 and 4 rebuild the named index or all
82845** indices associated with the named table.
82846*/
82847#ifndef SQLITE_OMIT_REINDEX
82848SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
82849  CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
82850  char *z;                    /* Name of a table or index */
82851  const char *zDb;            /* Name of the database */
82852  Table *pTab;                /* A table in the database */
82853  Index *pIndex;              /* An index associated with pTab */
82854  int iDb;                    /* The database index number */
82855  sqlite3 *db = pParse->db;   /* The database connection */
82856  Token *pObjName;            /* Name of the table or index to be reindexed */
82857
82858  /* Read the database schema. If an error occurs, leave an error message
82859  ** and code in pParse and return NULL. */
82860  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
82861    return;
82862  }
82863
82864  if( pName1==0 ){
82865    reindexDatabases(pParse, 0);
82866    return;
82867  }else if( NEVER(pName2==0) || pName2->z==0 ){
82868    char *zColl;
82869    assert( pName1->z );
82870    zColl = sqlite3NameFromToken(pParse->db, pName1);
82871    if( !zColl ) return;
82872    pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
82873    if( pColl ){
82874      reindexDatabases(pParse, zColl);
82875      sqlite3DbFree(db, zColl);
82876      return;
82877    }
82878    sqlite3DbFree(db, zColl);
82879  }
82880  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
82881  if( iDb<0 ) return;
82882  z = sqlite3NameFromToken(db, pObjName);
82883  if( z==0 ) return;
82884  zDb = db->aDb[iDb].zName;
82885  pTab = sqlite3FindTable(db, z, zDb);
82886  if( pTab ){
82887    reindexTable(pParse, pTab, 0);
82888    sqlite3DbFree(db, z);
82889    return;
82890  }
82891  pIndex = sqlite3FindIndex(db, z, zDb);
82892  sqlite3DbFree(db, z);
82893  if( pIndex ){
82894    sqlite3BeginWriteOperation(pParse, 0, iDb);
82895    sqlite3RefillIndex(pParse, pIndex, -1);
82896    return;
82897  }
82898  sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
82899}
82900#endif
82901
82902/*
82903** Return a dynamicly allocated KeyInfo structure that can be used
82904** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
82905**
82906** If successful, a pointer to the new structure is returned. In this case
82907** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
82908** pointer. If an error occurs (out of memory or missing collation
82909** sequence), NULL is returned and the state of pParse updated to reflect
82910** the error.
82911*/
82912SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
82913  int i;
82914  int nCol = pIdx->nColumn;
82915  int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
82916  sqlite3 *db = pParse->db;
82917  KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
82918
82919  if( pKey ){
82920    pKey->db = pParse->db;
82921    pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
82922    assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
82923    for(i=0; i<nCol; i++){
82924      char *zColl = pIdx->azColl[i];
82925      assert( zColl );
82926      pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
82927      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
82928    }
82929    pKey->nField = (u16)nCol;
82930  }
82931
82932  if( pParse->nErr ){
82933    sqlite3DbFree(db, pKey);
82934    pKey = 0;
82935  }
82936  return pKey;
82937}
82938
82939/************** End of build.c ***********************************************/
82940/************** Begin file callback.c ****************************************/
82941/*
82942** 2005 May 23
82943**
82944** The author disclaims copyright to this source code.  In place of
82945** a legal notice, here is a blessing:
82946**
82947**    May you do good and not evil.
82948**    May you find forgiveness for yourself and forgive others.
82949**    May you share freely, never taking more than you give.
82950**
82951*************************************************************************
82952**
82953** This file contains functions used to access the internal hash tables
82954** of user defined functions and collation sequences.
82955*/
82956
82957
82958/*
82959** Invoke the 'collation needed' callback to request a collation sequence
82960** in the encoding enc of name zName, length nName.
82961*/
82962static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
82963  assert( !db->xCollNeeded || !db->xCollNeeded16 );
82964  if( db->xCollNeeded ){
82965    char *zExternal = sqlite3DbStrDup(db, zName);
82966    if( !zExternal ) return;
82967    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
82968    sqlite3DbFree(db, zExternal);
82969  }
82970#ifndef SQLITE_OMIT_UTF16
82971  if( db->xCollNeeded16 ){
82972    char const *zExternal;
82973    sqlite3_value *pTmp = sqlite3ValueNew(db);
82974    sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
82975    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
82976    if( zExternal ){
82977      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
82978    }
82979    sqlite3ValueFree(pTmp);
82980  }
82981#endif
82982}
82983
82984/*
82985** This routine is called if the collation factory fails to deliver a
82986** collation function in the best encoding but there may be other versions
82987** of this collation function (for other text encodings) available. Use one
82988** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
82989** possible.
82990*/
82991static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
82992  CollSeq *pColl2;
82993  char *z = pColl->zName;
82994  int i;
82995  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
82996  for(i=0; i<3; i++){
82997    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
82998    if( pColl2->xCmp!=0 ){
82999      memcpy(pColl, pColl2, sizeof(CollSeq));
83000      pColl->xDel = 0;         /* Do not copy the destructor */
83001      return SQLITE_OK;
83002    }
83003  }
83004  return SQLITE_ERROR;
83005}
83006
83007/*
83008** This function is responsible for invoking the collation factory callback
83009** or substituting a collation sequence of a different encoding when the
83010** requested collation sequence is not available in the desired encoding.
83011**
83012** If it is not NULL, then pColl must point to the database native encoding
83013** collation sequence with name zName, length nName.
83014**
83015** The return value is either the collation sequence to be used in database
83016** db for collation type name zName, length nName, or NULL, if no collation
83017** sequence can be found.
83018**
83019** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
83020*/
83021SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
83022  sqlite3* db,          /* The database connection */
83023  u8 enc,               /* The desired encoding for the collating sequence */
83024  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
83025  const char *zName     /* Collating sequence name */
83026){
83027  CollSeq *p;
83028
83029  p = pColl;
83030  if( !p ){
83031    p = sqlite3FindCollSeq(db, enc, zName, 0);
83032  }
83033  if( !p || !p->xCmp ){
83034    /* No collation sequence of this type for this encoding is registered.
83035    ** Call the collation factory to see if it can supply us with one.
83036    */
83037    callCollNeeded(db, enc, zName);
83038    p = sqlite3FindCollSeq(db, enc, zName, 0);
83039  }
83040  if( p && !p->xCmp && synthCollSeq(db, p) ){
83041    p = 0;
83042  }
83043  assert( !p || p->xCmp );
83044  return p;
83045}
83046
83047/*
83048** This routine is called on a collation sequence before it is used to
83049** check that it is defined. An undefined collation sequence exists when
83050** a database is loaded that contains references to collation sequences
83051** that have not been defined by sqlite3_create_collation() etc.
83052**
83053** If required, this routine calls the 'collation needed' callback to
83054** request a definition of the collating sequence. If this doesn't work,
83055** an equivalent collating sequence that uses a text encoding different
83056** from the main database is substituted, if one is available.
83057*/
83058SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
83059  if( pColl ){
83060    const char *zName = pColl->zName;
83061    sqlite3 *db = pParse->db;
83062    CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
83063    if( !p ){
83064      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
83065      pParse->nErr++;
83066      return SQLITE_ERROR;
83067    }
83068    assert( p==pColl );
83069  }
83070  return SQLITE_OK;
83071}
83072
83073
83074
83075/*
83076** Locate and return an entry from the db.aCollSeq hash table. If the entry
83077** specified by zName and nName is not found and parameter 'create' is
83078** true, then create a new entry. Otherwise return NULL.
83079**
83080** Each pointer stored in the sqlite3.aCollSeq hash table contains an
83081** array of three CollSeq structures. The first is the collation sequence
83082** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
83083**
83084** Stored immediately after the three collation sequences is a copy of
83085** the collation sequence name. A pointer to this string is stored in
83086** each collation sequence structure.
83087*/
83088static CollSeq *findCollSeqEntry(
83089  sqlite3 *db,          /* Database connection */
83090  const char *zName,    /* Name of the collating sequence */
83091  int create            /* Create a new entry if true */
83092){
83093  CollSeq *pColl;
83094  int nName = sqlite3Strlen30(zName);
83095  pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
83096
83097  if( 0==pColl && create ){
83098    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
83099    if( pColl ){
83100      CollSeq *pDel = 0;
83101      pColl[0].zName = (char*)&pColl[3];
83102      pColl[0].enc = SQLITE_UTF8;
83103      pColl[1].zName = (char*)&pColl[3];
83104      pColl[1].enc = SQLITE_UTF16LE;
83105      pColl[2].zName = (char*)&pColl[3];
83106      pColl[2].enc = SQLITE_UTF16BE;
83107      memcpy(pColl[0].zName, zName, nName);
83108      pColl[0].zName[nName] = 0;
83109      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
83110
83111      /* If a malloc() failure occurred in sqlite3HashInsert(), it will
83112      ** return the pColl pointer to be deleted (because it wasn't added
83113      ** to the hash table).
83114      */
83115      assert( pDel==0 || pDel==pColl );
83116      if( pDel!=0 ){
83117        db->mallocFailed = 1;
83118        sqlite3DbFree(db, pDel);
83119        pColl = 0;
83120      }
83121    }
83122  }
83123  return pColl;
83124}
83125
83126/*
83127** Parameter zName points to a UTF-8 encoded string nName bytes long.
83128** Return the CollSeq* pointer for the collation sequence named zName
83129** for the encoding 'enc' from the database 'db'.
83130**
83131** If the entry specified is not found and 'create' is true, then create a
83132** new entry.  Otherwise return NULL.
83133**
83134** A separate function sqlite3LocateCollSeq() is a wrapper around
83135** this routine.  sqlite3LocateCollSeq() invokes the collation factory
83136** if necessary and generates an error message if the collating sequence
83137** cannot be found.
83138**
83139** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
83140*/
83141SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
83142  sqlite3 *db,
83143  u8 enc,
83144  const char *zName,
83145  int create
83146){
83147  CollSeq *pColl;
83148  if( zName ){
83149    pColl = findCollSeqEntry(db, zName, create);
83150  }else{
83151    pColl = db->pDfltColl;
83152  }
83153  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
83154  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
83155  if( pColl ) pColl += enc-1;
83156  return pColl;
83157}
83158
83159/* During the search for the best function definition, this procedure
83160** is called to test how well the function passed as the first argument
83161** matches the request for a function with nArg arguments in a system
83162** that uses encoding enc. The value returned indicates how well the
83163** request is matched. A higher value indicates a better match.
83164**
83165** The returned value is always between 0 and 6, as follows:
83166**
83167** 0: Not a match, or if nArg<0 and the function is has no implementation.
83168** 1: A variable arguments function that prefers UTF-8 when a UTF-16
83169**    encoding is requested, or vice versa.
83170** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
83171**    requested, or vice versa.
83172** 3: A variable arguments function using the same text encoding.
83173** 4: A function with the exact number of arguments requested that
83174**    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
83175** 5: A function with the exact number of arguments requested that
83176**    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
83177** 6: An exact match.
83178**
83179*/
83180static int matchQuality(FuncDef *p, int nArg, u8 enc){
83181  int match = 0;
83182  if( p->nArg==-1 || p->nArg==nArg
83183   || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
83184  ){
83185    match = 1;
83186    if( p->nArg==nArg || nArg==-1 ){
83187      match = 4;
83188    }
83189    if( enc==p->iPrefEnc ){
83190      match += 2;
83191    }
83192    else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
83193             (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
83194      match += 1;
83195    }
83196  }
83197  return match;
83198}
83199
83200/*
83201** Search a FuncDefHash for a function with the given name.  Return
83202** a pointer to the matching FuncDef if found, or 0 if there is no match.
83203*/
83204static FuncDef *functionSearch(
83205  FuncDefHash *pHash,  /* Hash table to search */
83206  int h,               /* Hash of the name */
83207  const char *zFunc,   /* Name of function */
83208  int nFunc            /* Number of bytes in zFunc */
83209){
83210  FuncDef *p;
83211  for(p=pHash->a[h]; p; p=p->pHash){
83212    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
83213      return p;
83214    }
83215  }
83216  return 0;
83217}
83218
83219/*
83220** Insert a new FuncDef into a FuncDefHash hash table.
83221*/
83222SQLITE_PRIVATE void sqlite3FuncDefInsert(
83223  FuncDefHash *pHash,  /* The hash table into which to insert */
83224  FuncDef *pDef        /* The function definition to insert */
83225){
83226  FuncDef *pOther;
83227  int nName = sqlite3Strlen30(pDef->zName);
83228  u8 c1 = (u8)pDef->zName[0];
83229  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
83230  pOther = functionSearch(pHash, h, pDef->zName, nName);
83231  if( pOther ){
83232    assert( pOther!=pDef && pOther->pNext!=pDef );
83233    pDef->pNext = pOther->pNext;
83234    pOther->pNext = pDef;
83235  }else{
83236    pDef->pNext = 0;
83237    pDef->pHash = pHash->a[h];
83238    pHash->a[h] = pDef;
83239  }
83240}
83241
83242
83243
83244/*
83245** Locate a user function given a name, a number of arguments and a flag
83246** indicating whether the function prefers UTF-16 over UTF-8.  Return a
83247** pointer to the FuncDef structure that defines that function, or return
83248** NULL if the function does not exist.
83249**
83250** If the createFlag argument is true, then a new (blank) FuncDef
83251** structure is created and liked into the "db" structure if a
83252** no matching function previously existed.  When createFlag is true
83253** and the nArg parameter is -1, then only a function that accepts
83254** any number of arguments will be returned.
83255**
83256** If createFlag is false and nArg is -1, then the first valid
83257** function found is returned.  A function is valid if either xFunc
83258** or xStep is non-zero.
83259**
83260** If createFlag is false, then a function with the required name and
83261** number of arguments may be returned even if the eTextRep flag does not
83262** match that requested.
83263*/
83264SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
83265  sqlite3 *db,       /* An open database */
83266  const char *zName, /* Name of the function.  Not null-terminated */
83267  int nName,         /* Number of characters in the name */
83268  int nArg,          /* Number of arguments.  -1 means any number */
83269  u8 enc,            /* Preferred text encoding */
83270  int createFlag     /* Create new entry if true and does not otherwise exist */
83271){
83272  FuncDef *p;         /* Iterator variable */
83273  FuncDef *pBest = 0; /* Best match found so far */
83274  int bestScore = 0;  /* Score of best match */
83275  int h;              /* Hash value */
83276
83277
83278  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
83279  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
83280
83281  /* First search for a match amongst the application-defined functions.
83282  */
83283  p = functionSearch(&db->aFunc, h, zName, nName);
83284  while( p ){
83285    int score = matchQuality(p, nArg, enc);
83286    if( score>bestScore ){
83287      pBest = p;
83288      bestScore = score;
83289    }
83290    p = p->pNext;
83291  }
83292
83293  /* If no match is found, search the built-in functions.
83294  **
83295  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
83296  ** functions even if a prior app-defined function was found.  And give
83297  ** priority to built-in functions.
83298  **
83299  ** Except, if createFlag is true, that means that we are trying to
83300  ** install a new function.  Whatever FuncDef structure is returned it will
83301  ** have fields overwritten with new information appropriate for the
83302  ** new function.  But the FuncDefs for built-in functions are read-only.
83303  ** So we must not search for built-ins when creating a new function.
83304  */
83305  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
83306    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
83307    bestScore = 0;
83308    p = functionSearch(pHash, h, zName, nName);
83309    while( p ){
83310      int score = matchQuality(p, nArg, enc);
83311      if( score>bestScore ){
83312        pBest = p;
83313        bestScore = score;
83314      }
83315      p = p->pNext;
83316    }
83317  }
83318
83319  /* If the createFlag parameter is true and the search did not reveal an
83320  ** exact match for the name, number of arguments and encoding, then add a
83321  ** new entry to the hash table and return it.
83322  */
83323  if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
83324      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
83325    pBest->zName = (char *)&pBest[1];
83326    pBest->nArg = (u16)nArg;
83327    pBest->iPrefEnc = enc;
83328    memcpy(pBest->zName, zName, nName);
83329    pBest->zName[nName] = 0;
83330    sqlite3FuncDefInsert(&db->aFunc, pBest);
83331  }
83332
83333  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
83334    return pBest;
83335  }
83336  return 0;
83337}
83338
83339/*
83340** Free all resources held by the schema structure. The void* argument points
83341** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
83342** pointer itself, it just cleans up subsidiary resources (i.e. the contents
83343** of the schema hash tables).
83344**
83345** The Schema.cache_size variable is not cleared.
83346*/
83347SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
83348  Hash temp1;
83349  Hash temp2;
83350  HashElem *pElem;
83351  Schema *pSchema = (Schema *)p;
83352
83353  temp1 = pSchema->tblHash;
83354  temp2 = pSchema->trigHash;
83355  sqlite3HashInit(&pSchema->trigHash);
83356  sqlite3HashClear(&pSchema->idxHash);
83357  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
83358    sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
83359  }
83360  sqlite3HashClear(&temp2);
83361  sqlite3HashInit(&pSchema->tblHash);
83362  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
83363    Table *pTab = sqliteHashData(pElem);
83364    sqlite3DeleteTable(0, pTab);
83365  }
83366  sqlite3HashClear(&temp1);
83367  sqlite3HashClear(&pSchema->fkeyHash);
83368  pSchema->pSeqTab = 0;
83369  if( pSchema->flags & DB_SchemaLoaded ){
83370    pSchema->iGeneration++;
83371    pSchema->flags &= ~DB_SchemaLoaded;
83372  }
83373}
83374
83375/*
83376** Find and return the schema associated with a BTree.  Create
83377** a new one if necessary.
83378*/
83379SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
83380  Schema * p;
83381  if( pBt ){
83382    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
83383  }else{
83384    p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
83385  }
83386  if( !p ){
83387    db->mallocFailed = 1;
83388  }else if ( 0==p->file_format ){
83389    sqlite3HashInit(&p->tblHash);
83390    sqlite3HashInit(&p->idxHash);
83391    sqlite3HashInit(&p->trigHash);
83392    sqlite3HashInit(&p->fkeyHash);
83393    p->enc = SQLITE_UTF8;
83394  }
83395  return p;
83396}
83397
83398/************** End of callback.c ********************************************/
83399/************** Begin file delete.c ******************************************/
83400/*
83401** 2001 September 15
83402**
83403** The author disclaims copyright to this source code.  In place of
83404** a legal notice, here is a blessing:
83405**
83406**    May you do good and not evil.
83407**    May you find forgiveness for yourself and forgive others.
83408**    May you share freely, never taking more than you give.
83409**
83410*************************************************************************
83411** This file contains C code routines that are called by the parser
83412** in order to generate code for DELETE FROM statements.
83413*/
83414
83415/*
83416** While a SrcList can in general represent multiple tables and subqueries
83417** (as in the FROM clause of a SELECT statement) in this case it contains
83418** the name of a single table, as one might find in an INSERT, DELETE,
83419** or UPDATE statement.  Look up that table in the symbol table and
83420** return a pointer.  Set an error message and return NULL if the table
83421** name is not found or if any other error occurs.
83422**
83423** The following fields are initialized appropriate in pSrc:
83424**
83425**    pSrc->a[0].pTab       Pointer to the Table object
83426**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
83427**
83428*/
83429SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
83430  struct SrcList_item *pItem = pSrc->a;
83431  Table *pTab;
83432  assert( pItem && pSrc->nSrc==1 );
83433  pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
83434  sqlite3DeleteTable(pParse->db, pItem->pTab);
83435  pItem->pTab = pTab;
83436  if( pTab ){
83437    pTab->nRef++;
83438  }
83439  if( sqlite3IndexedByLookup(pParse, pItem) ){
83440    pTab = 0;
83441  }
83442  return pTab;
83443}
83444
83445/*
83446** Check to make sure the given table is writable.  If it is not
83447** writable, generate an error message and return 1.  If it is
83448** writable return 0;
83449*/
83450SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
83451  /* A table is not writable under the following circumstances:
83452  **
83453  **   1) It is a virtual table and no implementation of the xUpdate method
83454  **      has been provided, or
83455  **   2) It is a system table (i.e. sqlite_master), this call is not
83456  **      part of a nested parse and writable_schema pragma has not
83457  **      been specified.
83458  **
83459  ** In either case leave an error message in pParse and return non-zero.
83460  */
83461  if( ( IsVirtual(pTab)
83462     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
83463   || ( (pTab->tabFlags & TF_Readonly)!=0
83464     && (pParse->db->flags & SQLITE_WriteSchema)==0
83465     && pParse->nested==0 )
83466  ){
83467    sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
83468    return 1;
83469  }
83470
83471#ifndef SQLITE_OMIT_VIEW
83472  if( !viewOk && pTab->pSelect ){
83473    sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
83474    return 1;
83475  }
83476#endif
83477  return 0;
83478}
83479
83480
83481#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
83482/*
83483** Evaluate a view and store its result in an ephemeral table.  The
83484** pWhere argument is an optional WHERE clause that restricts the
83485** set of rows in the view that are to be added to the ephemeral table.
83486*/
83487SQLITE_PRIVATE void sqlite3MaterializeView(
83488  Parse *pParse,       /* Parsing context */
83489  Table *pView,        /* View definition */
83490  Expr *pWhere,        /* Optional WHERE clause to be added */
83491  int iCur             /* Cursor number for ephemerial table */
83492){
83493  SelectDest dest;
83494  Select *pDup;
83495  sqlite3 *db = pParse->db;
83496
83497  pDup = sqlite3SelectDup(db, pView->pSelect, 0);
83498  if( pWhere ){
83499    SrcList *pFrom;
83500
83501    pWhere = sqlite3ExprDup(db, pWhere, 0);
83502    pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
83503    if( pFrom ){
83504      assert( pFrom->nSrc==1 );
83505      pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
83506      pFrom->a[0].pSelect = pDup;
83507      assert( pFrom->a[0].pOn==0 );
83508      assert( pFrom->a[0].pUsing==0 );
83509    }else{
83510      sqlite3SelectDelete(db, pDup);
83511    }
83512    pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
83513  }
83514  sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
83515  sqlite3Select(pParse, pDup, &dest);
83516  sqlite3SelectDelete(db, pDup);
83517}
83518#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
83519
83520#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
83521/*
83522** Generate an expression tree to implement the WHERE, ORDER BY,
83523** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
83524**
83525**     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
83526**                            \__________________________/
83527**                               pLimitWhere (pInClause)
83528*/
83529SQLITE_PRIVATE Expr *sqlite3LimitWhere(
83530  Parse *pParse,               /* The parser context */
83531  SrcList *pSrc,               /* the FROM clause -- which tables to scan */
83532  Expr *pWhere,                /* The WHERE clause.  May be null */
83533  ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
83534  Expr *pLimit,                /* The LIMIT clause.  May be null */
83535  Expr *pOffset,               /* The OFFSET clause.  May be null */
83536  char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
83537){
83538  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
83539  Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
83540  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
83541  ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
83542  SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
83543  Select *pSelect = NULL;      /* Complete SELECT tree */
83544
83545  /* Check that there isn't an ORDER BY without a LIMIT clause.
83546  */
83547  if( pOrderBy && (pLimit == 0) ) {
83548    sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
83549    pParse->parseError = 1;
83550    goto limit_where_cleanup_2;
83551  }
83552
83553  /* We only need to generate a select expression if there
83554  ** is a limit/offset term to enforce.
83555  */
83556  if( pLimit == 0 ) {
83557    /* if pLimit is null, pOffset will always be null as well. */
83558    assert( pOffset == 0 );
83559    return pWhere;
83560  }
83561
83562  /* Generate a select expression tree to enforce the limit/offset
83563  ** term for the DELETE or UPDATE statement.  For example:
83564  **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
83565  ** becomes:
83566  **   DELETE FROM table_a WHERE rowid IN (
83567  **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
83568  **   );
83569  */
83570
83571  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
83572  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
83573  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
83574  if( pEList == 0 ) goto limit_where_cleanup_2;
83575
83576  /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
83577  ** and the SELECT subtree. */
83578  pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
83579  if( pSelectSrc == 0 ) {
83580    sqlite3ExprListDelete(pParse->db, pEList);
83581    goto limit_where_cleanup_2;
83582  }
83583
83584  /* generate the SELECT expression tree. */
83585  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
83586                             pOrderBy,0,pLimit,pOffset);
83587  if( pSelect == 0 ) return 0;
83588
83589  /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
83590  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
83591  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
83592  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
83593  if( pInClause == 0 ) goto limit_where_cleanup_1;
83594
83595  pInClause->x.pSelect = pSelect;
83596  pInClause->flags |= EP_xIsSelect;
83597  sqlite3ExprSetHeight(pParse, pInClause);
83598  return pInClause;
83599
83600  /* something went wrong. clean up anything allocated. */
83601limit_where_cleanup_1:
83602  sqlite3SelectDelete(pParse->db, pSelect);
83603  return 0;
83604
83605limit_where_cleanup_2:
83606  sqlite3ExprDelete(pParse->db, pWhere);
83607  sqlite3ExprListDelete(pParse->db, pOrderBy);
83608  sqlite3ExprDelete(pParse->db, pLimit);
83609  sqlite3ExprDelete(pParse->db, pOffset);
83610  return 0;
83611}
83612#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
83613
83614/*
83615** Generate code for a DELETE FROM statement.
83616**
83617**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
83618**                 \________/       \________________/
83619**                  pTabList              pWhere
83620*/
83621SQLITE_PRIVATE void sqlite3DeleteFrom(
83622  Parse *pParse,         /* The parser context */
83623  SrcList *pTabList,     /* The table from which we should delete things */
83624  Expr *pWhere           /* The WHERE clause.  May be null */
83625){
83626  Vdbe *v;               /* The virtual database engine */
83627  Table *pTab;           /* The table from which records will be deleted */
83628  const char *zDb;       /* Name of database holding pTab */
83629  int end, addr = 0;     /* A couple addresses of generated code */
83630  int i;                 /* Loop counter */
83631  WhereInfo *pWInfo;     /* Information about the WHERE clause */
83632  Index *pIdx;           /* For looping over indices of the table */
83633  int iCur;              /* VDBE Cursor number for pTab */
83634  sqlite3 *db;           /* Main database structure */
83635  AuthContext sContext;  /* Authorization context */
83636  NameContext sNC;       /* Name context to resolve expressions in */
83637  int iDb;               /* Database number */
83638  int memCnt = -1;       /* Memory cell used for change counting */
83639  int rcauth;            /* Value returned by authorization callback */
83640
83641#ifndef SQLITE_OMIT_TRIGGER
83642  int isView;                  /* True if attempting to delete from a view */
83643  Trigger *pTrigger;           /* List of table triggers, if required */
83644#endif
83645
83646  memset(&sContext, 0, sizeof(sContext));
83647  db = pParse->db;
83648  if( pParse->nErr || db->mallocFailed ){
83649    goto delete_from_cleanup;
83650  }
83651  assert( pTabList->nSrc==1 );
83652
83653  /* Locate the table which we want to delete.  This table has to be
83654  ** put in an SrcList structure because some of the subroutines we
83655  ** will be calling are designed to work with multiple tables and expect
83656  ** an SrcList* parameter instead of just a Table* parameter.
83657  */
83658  pTab = sqlite3SrcListLookup(pParse, pTabList);
83659  if( pTab==0 )  goto delete_from_cleanup;
83660
83661  /* Figure out if we have any triggers and if the table being
83662  ** deleted from is a view
83663  */
83664#ifndef SQLITE_OMIT_TRIGGER
83665  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
83666  isView = pTab->pSelect!=0;
83667#else
83668# define pTrigger 0
83669# define isView 0
83670#endif
83671#ifdef SQLITE_OMIT_VIEW
83672# undef isView
83673# define isView 0
83674#endif
83675
83676  /* If pTab is really a view, make sure it has been initialized.
83677  */
83678  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
83679    goto delete_from_cleanup;
83680  }
83681
83682  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
83683    goto delete_from_cleanup;
83684  }
83685  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
83686  assert( iDb<db->nDb );
83687  zDb = db->aDb[iDb].zName;
83688  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
83689  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
83690  if( rcauth==SQLITE_DENY ){
83691    goto delete_from_cleanup;
83692  }
83693  assert(!isView || pTrigger);
83694
83695  /* Assign  cursor number to the table and all its indices.
83696  */
83697  assert( pTabList->nSrc==1 );
83698  iCur = pTabList->a[0].iCursor = pParse->nTab++;
83699  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
83700    pParse->nTab++;
83701  }
83702
83703  /* Start the view context
83704  */
83705  if( isView ){
83706    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
83707  }
83708
83709  /* Begin generating code.
83710  */
83711  v = sqlite3GetVdbe(pParse);
83712  if( v==0 ){
83713    goto delete_from_cleanup;
83714  }
83715  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
83716  sqlite3BeginWriteOperation(pParse, 1, iDb);
83717
83718  /* If we are trying to delete from a view, realize that view into
83719  ** a ephemeral table.
83720  */
83721#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
83722  if( isView ){
83723    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
83724  }
83725#endif
83726
83727  /* Resolve the column names in the WHERE clause.
83728  */
83729  memset(&sNC, 0, sizeof(sNC));
83730  sNC.pParse = pParse;
83731  sNC.pSrcList = pTabList;
83732  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
83733    goto delete_from_cleanup;
83734  }
83735
83736  /* Initialize the counter of the number of rows deleted, if
83737  ** we are counting rows.
83738  */
83739  if( db->flags & SQLITE_CountRows ){
83740    memCnt = ++pParse->nMem;
83741    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
83742  }
83743
83744#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
83745  /* Special case: A DELETE without a WHERE clause deletes everything.
83746  ** It is easier just to erase the whole table. Prior to version 3.6.5,
83747  ** this optimization caused the row change count (the value returned by
83748  ** API function sqlite3_count_changes) to be set incorrectly.  */
83749  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
83750   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
83751  ){
83752    assert( !isView );
83753    sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
83754                      pTab->zName, P4_STATIC);
83755    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
83756      assert( pIdx->pSchema==pTab->pSchema );
83757      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
83758    }
83759  }else
83760#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
83761  /* The usual case: There is a WHERE clause so we have to scan through
83762  ** the table and pick which records to delete.
83763  */
83764  {
83765    int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
83766    int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
83767    int regRowid;                   /* Actual register containing rowids */
83768
83769    /* Collect rowids of every row to be deleted.
83770    */
83771    sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
83772    pWInfo = sqlite3WhereBegin(
83773        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
83774    );
83775    if( pWInfo==0 ) goto delete_from_cleanup;
83776    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
83777    sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
83778    if( db->flags & SQLITE_CountRows ){
83779      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
83780    }
83781    sqlite3WhereEnd(pWInfo);
83782
83783    /* Delete every item whose key was written to the list during the
83784    ** database scan.  We have to delete items after the scan is complete
83785    ** because deleting an item can change the scan order.  */
83786    end = sqlite3VdbeMakeLabel(v);
83787
83788    /* Unless this is a view, open cursors for the table we are
83789    ** deleting from and all its indices. If this is a view, then the
83790    ** only effect this statement has is to fire the INSTEAD OF
83791    ** triggers.  */
83792    if( !isView ){
83793      sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
83794    }
83795
83796    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
83797
83798    /* Delete the row */
83799#ifndef SQLITE_OMIT_VIRTUALTABLE
83800    if( IsVirtual(pTab) ){
83801      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
83802      sqlite3VtabMakeWritable(pParse, pTab);
83803      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
83804      sqlite3VdbeChangeP5(v, OE_Abort);
83805      sqlite3MayAbort(pParse);
83806    }else
83807#endif
83808    {
83809      int count = (pParse->nested==0);    /* True to count changes */
83810      sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
83811    }
83812
83813    /* End of the delete loop */
83814    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
83815    sqlite3VdbeResolveLabel(v, end);
83816
83817    /* Close the cursors open on the table and its indexes. */
83818    if( !isView && !IsVirtual(pTab) ){
83819      for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
83820        sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
83821      }
83822      sqlite3VdbeAddOp1(v, OP_Close, iCur);
83823    }
83824  }
83825
83826  /* Update the sqlite_sequence table by storing the content of the
83827  ** maximum rowid counter values recorded while inserting into
83828  ** autoincrement tables.
83829  */
83830  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
83831    sqlite3AutoincrementEnd(pParse);
83832  }
83833
83834  /* Return the number of rows that were deleted. If this routine is
83835  ** generating code because of a call to sqlite3NestedParse(), do not
83836  ** invoke the callback function.
83837  */
83838  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
83839    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
83840    sqlite3VdbeSetNumCols(v, 1);
83841    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
83842  }
83843
83844delete_from_cleanup:
83845  sqlite3AuthContextPop(&sContext);
83846  sqlite3SrcListDelete(db, pTabList);
83847  sqlite3ExprDelete(db, pWhere);
83848  return;
83849}
83850/* Make sure "isView" and other macros defined above are undefined. Otherwise
83851** thely may interfere with compilation of other functions in this file
83852** (or in another file, if this file becomes part of the amalgamation).  */
83853#ifdef isView
83854 #undef isView
83855#endif
83856#ifdef pTrigger
83857 #undef pTrigger
83858#endif
83859
83860/*
83861** This routine generates VDBE code that causes a single row of a
83862** single table to be deleted.
83863**
83864** The VDBE must be in a particular state when this routine is called.
83865** These are the requirements:
83866**
83867**   1.  A read/write cursor pointing to pTab, the table containing the row
83868**       to be deleted, must be opened as cursor number $iCur.
83869**
83870**   2.  Read/write cursors for all indices of pTab must be open as
83871**       cursor number base+i for the i-th index.
83872**
83873**   3.  The record number of the row to be deleted must be stored in
83874**       memory cell iRowid.
83875**
83876** This routine generates code to remove both the table record and all
83877** index entries that point to that record.
83878*/
83879SQLITE_PRIVATE void sqlite3GenerateRowDelete(
83880  Parse *pParse,     /* Parsing context */
83881  Table *pTab,       /* Table containing the row to be deleted */
83882  int iCur,          /* Cursor number for the table */
83883  int iRowid,        /* Memory cell that contains the rowid to delete */
83884  int count,         /* If non-zero, increment the row change counter */
83885  Trigger *pTrigger, /* List of triggers to (potentially) fire */
83886  int onconf         /* Default ON CONFLICT policy for triggers */
83887){
83888  Vdbe *v = pParse->pVdbe;        /* Vdbe */
83889  int iOld = 0;                   /* First register in OLD.* array */
83890  int iLabel;                     /* Label resolved to end of generated code */
83891
83892  /* Vdbe is guaranteed to have been allocated by this stage. */
83893  assert( v );
83894
83895  /* Seek cursor iCur to the row to delete. If this row no longer exists
83896  ** (this can happen if a trigger program has already deleted it), do
83897  ** not attempt to delete it or fire any DELETE triggers.  */
83898  iLabel = sqlite3VdbeMakeLabel(v);
83899  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
83900
83901  /* If there are any triggers to fire, allocate a range of registers to
83902  ** use for the old.* references in the triggers.  */
83903  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
83904    u32 mask;                     /* Mask of OLD.* columns in use */
83905    int iCol;                     /* Iterator used while populating OLD.* */
83906
83907    /* TODO: Could use temporary registers here. Also could attempt to
83908    ** avoid copying the contents of the rowid register.  */
83909    mask = sqlite3TriggerColmask(
83910        pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
83911    );
83912    mask |= sqlite3FkOldmask(pParse, pTab);
83913    iOld = pParse->nMem+1;
83914    pParse->nMem += (1 + pTab->nCol);
83915
83916    /* Populate the OLD.* pseudo-table register array. These values will be
83917    ** used by any BEFORE and AFTER triggers that exist.  */
83918    sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
83919    for(iCol=0; iCol<pTab->nCol; iCol++){
83920      if( mask==0xffffffff || mask&(1<<iCol) ){
83921        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
83922      }
83923    }
83924
83925    /* Invoke BEFORE DELETE trigger programs. */
83926    sqlite3CodeRowTrigger(pParse, pTrigger,
83927        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
83928    );
83929
83930    /* Seek the cursor to the row to be deleted again. It may be that
83931    ** the BEFORE triggers coded above have already removed the row
83932    ** being deleted. Do not attempt to delete the row a second time, and
83933    ** do not fire AFTER triggers.  */
83934    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
83935
83936    /* Do FK processing. This call checks that any FK constraints that
83937    ** refer to this table (i.e. constraints attached to other tables)
83938    ** are not violated by deleting this row.  */
83939    sqlite3FkCheck(pParse, pTab, iOld, 0);
83940  }
83941
83942  /* Delete the index and table entries. Skip this step if pTab is really
83943  ** a view (in which case the only effect of the DELETE statement is to
83944  ** fire the INSTEAD OF triggers).  */
83945  if( pTab->pSelect==0 ){
83946    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
83947    sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
83948    if( count ){
83949      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
83950    }
83951  }
83952
83953  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
83954  ** handle rows (possibly in other tables) that refer via a foreign key
83955  ** to the row just deleted. */
83956  sqlite3FkActions(pParse, pTab, 0, iOld);
83957
83958  /* Invoke AFTER DELETE trigger programs. */
83959  sqlite3CodeRowTrigger(pParse, pTrigger,
83960      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
83961  );
83962
83963  /* Jump here if the row had already been deleted before any BEFORE
83964  ** trigger programs were invoked. Or if a trigger program throws a
83965  ** RAISE(IGNORE) exception.  */
83966  sqlite3VdbeResolveLabel(v, iLabel);
83967}
83968
83969/*
83970** This routine generates VDBE code that causes the deletion of all
83971** index entries associated with a single row of a single table.
83972**
83973** The VDBE must be in a particular state when this routine is called.
83974** These are the requirements:
83975**
83976**   1.  A read/write cursor pointing to pTab, the table containing the row
83977**       to be deleted, must be opened as cursor number "iCur".
83978**
83979**   2.  Read/write cursors for all indices of pTab must be open as
83980**       cursor number iCur+i for the i-th index.
83981**
83982**   3.  The "iCur" cursor must be pointing to the row that is to be
83983**       deleted.
83984*/
83985SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
83986  Parse *pParse,     /* Parsing and code generating context */
83987  Table *pTab,       /* Table containing the row to be deleted */
83988  int iCur,          /* Cursor number for the table */
83989  int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
83990){
83991  int i;
83992  Index *pIdx;
83993  int r1;
83994
83995  for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
83996    if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
83997    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
83998    sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
83999  }
84000}
84001
84002/*
84003** Generate code that will assemble an index key and put it in register
84004** regOut.  The key with be for index pIdx which is an index on pTab.
84005** iCur is the index of a cursor open on the pTab table and pointing to
84006** the entry that needs indexing.
84007**
84008** Return a register number which is the first in a block of
84009** registers that holds the elements of the index key.  The
84010** block of registers has already been deallocated by the time
84011** this routine returns.
84012*/
84013SQLITE_PRIVATE int sqlite3GenerateIndexKey(
84014  Parse *pParse,     /* Parsing context */
84015  Index *pIdx,       /* The index for which to generate a key */
84016  int iCur,          /* Cursor number for the pIdx->pTable table */
84017  int regOut,        /* Write the new index key to this register */
84018  int doMakeRec      /* Run the OP_MakeRecord instruction if true */
84019){
84020  Vdbe *v = pParse->pVdbe;
84021  int j;
84022  Table *pTab = pIdx->pTable;
84023  int regBase;
84024  int nCol;
84025
84026  nCol = pIdx->nColumn;
84027  regBase = sqlite3GetTempRange(pParse, nCol+1);
84028  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
84029  for(j=0; j<nCol; j++){
84030    int idx = pIdx->aiColumn[j];
84031    if( idx==pTab->iPKey ){
84032      sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
84033    }else{
84034      sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
84035      sqlite3ColumnDefault(v, pTab, idx, -1);
84036    }
84037  }
84038  if( doMakeRec ){
84039    const char *zAff;
84040    if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
84041      zAff = 0;
84042    }else{
84043      zAff = sqlite3IndexAffinityStr(v, pIdx);
84044    }
84045    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
84046    sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
84047  }
84048  sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
84049  return regBase;
84050}
84051
84052/************** End of delete.c **********************************************/
84053/************** Begin file func.c ********************************************/
84054/*
84055** 2002 February 23
84056**
84057** The author disclaims copyright to this source code.  In place of
84058** a legal notice, here is a blessing:
84059**
84060**    May you do good and not evil.
84061**    May you find forgiveness for yourself and forgive others.
84062**    May you share freely, never taking more than you give.
84063**
84064*************************************************************************
84065** This file contains the C functions that implement various SQL
84066** functions of SQLite.
84067**
84068** There is only one exported symbol in this file - the function
84069** sqliteRegisterBuildinFunctions() found at the bottom of the file.
84070** All other code has file scope.
84071*/
84072/* #include <stdlib.h> */
84073/* #include <assert.h> */
84074
84075/*
84076** Return the collating function associated with a function.
84077*/
84078static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
84079  return context->pColl;
84080}
84081
84082/*
84083** Implementation of the non-aggregate min() and max() functions
84084*/
84085static void minmaxFunc(
84086  sqlite3_context *context,
84087  int argc,
84088  sqlite3_value **argv
84089){
84090  int i;
84091  int mask;    /* 0 for min() or 0xffffffff for max() */
84092  int iBest;
84093  CollSeq *pColl;
84094
84095  assert( argc>1 );
84096  mask = sqlite3_user_data(context)==0 ? 0 : -1;
84097  pColl = sqlite3GetFuncCollSeq(context);
84098  assert( pColl );
84099  assert( mask==-1 || mask==0 );
84100  iBest = 0;
84101  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
84102  for(i=1; i<argc; i++){
84103    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
84104    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
84105      testcase( mask==0 );
84106      iBest = i;
84107    }
84108  }
84109  sqlite3_result_value(context, argv[iBest]);
84110}
84111
84112/*
84113** Return the type of the argument.
84114*/
84115static void typeofFunc(
84116  sqlite3_context *context,
84117  int NotUsed,
84118  sqlite3_value **argv
84119){
84120  const char *z = 0;
84121  UNUSED_PARAMETER(NotUsed);
84122  switch( sqlite3_value_type(argv[0]) ){
84123    case SQLITE_INTEGER: z = "integer"; break;
84124    case SQLITE_TEXT:    z = "text";    break;
84125    case SQLITE_FLOAT:   z = "real";    break;
84126    case SQLITE_BLOB:    z = "blob";    break;
84127    default:             z = "null";    break;
84128  }
84129  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
84130}
84131
84132
84133/*
84134** Implementation of the length() function
84135*/
84136static void lengthFunc(
84137  sqlite3_context *context,
84138  int argc,
84139  sqlite3_value **argv
84140){
84141  int len;
84142
84143  assert( argc==1 );
84144  UNUSED_PARAMETER(argc);
84145  switch( sqlite3_value_type(argv[0]) ){
84146    case SQLITE_BLOB:
84147    case SQLITE_INTEGER:
84148    case SQLITE_FLOAT: {
84149      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
84150      break;
84151    }
84152    case SQLITE_TEXT: {
84153      const unsigned char *z = sqlite3_value_text(argv[0]);
84154      if( z==0 ) return;
84155      len = 0;
84156      while( *z ){
84157        len++;
84158        SQLITE_SKIP_UTF8(z);
84159      }
84160      sqlite3_result_int(context, len);
84161      break;
84162    }
84163    default: {
84164      sqlite3_result_null(context);
84165      break;
84166    }
84167  }
84168}
84169
84170/*
84171** Implementation of the abs() function.
84172**
84173** IMP: R-23979-26855 The abs(X) function returns the absolute value of
84174** the numeric argument X.
84175*/
84176static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84177  assert( argc==1 );
84178  UNUSED_PARAMETER(argc);
84179  switch( sqlite3_value_type(argv[0]) ){
84180    case SQLITE_INTEGER: {
84181      i64 iVal = sqlite3_value_int64(argv[0]);
84182      if( iVal<0 ){
84183        if( (iVal<<1)==0 ){
84184          /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
84185          ** abs(X) throws an integer overflow error since there is no
84186          ** equivalent positive 64-bit two complement value. */
84187          sqlite3_result_error(context, "integer overflow", -1);
84188          return;
84189        }
84190        iVal = -iVal;
84191      }
84192      sqlite3_result_int64(context, iVal);
84193      break;
84194    }
84195    case SQLITE_NULL: {
84196      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
84197      sqlite3_result_null(context);
84198      break;
84199    }
84200    default: {
84201      /* Because sqlite3_value_double() returns 0.0 if the argument is not
84202      ** something that can be converted into a number, we have:
84203      ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
84204      ** cannot be converted to a numeric value.
84205      */
84206      double rVal = sqlite3_value_double(argv[0]);
84207      if( rVal<0 ) rVal = -rVal;
84208      sqlite3_result_double(context, rVal);
84209      break;
84210    }
84211  }
84212}
84213
84214/*
84215** Implementation of the substr() function.
84216**
84217** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
84218** p1 is 1-indexed.  So substr(x,1,1) returns the first character
84219** of x.  If x is text, then we actually count UTF-8 characters.
84220** If x is a blob, then we count bytes.
84221**
84222** If p1 is negative, then we begin abs(p1) from the end of x[].
84223**
84224** If p2 is negative, return the p2 characters preceeding p1.
84225*/
84226static void substrFunc(
84227  sqlite3_context *context,
84228  int argc,
84229  sqlite3_value **argv
84230){
84231  const unsigned char *z;
84232  const unsigned char *z2;
84233  int len;
84234  int p0type;
84235  i64 p1, p2;
84236  int negP2 = 0;
84237
84238  assert( argc==3 || argc==2 );
84239  if( sqlite3_value_type(argv[1])==SQLITE_NULL
84240   || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
84241  ){
84242    return;
84243  }
84244  p0type = sqlite3_value_type(argv[0]);
84245  p1 = sqlite3_value_int(argv[1]);
84246  if( p0type==SQLITE_BLOB ){
84247    len = sqlite3_value_bytes(argv[0]);
84248    z = sqlite3_value_blob(argv[0]);
84249    if( z==0 ) return;
84250    assert( len==sqlite3_value_bytes(argv[0]) );
84251  }else{
84252    z = sqlite3_value_text(argv[0]);
84253    if( z==0 ) return;
84254    len = 0;
84255    if( p1<0 ){
84256      for(z2=z; *z2; len++){
84257        SQLITE_SKIP_UTF8(z2);
84258      }
84259    }
84260  }
84261  if( argc==3 ){
84262    p2 = sqlite3_value_int(argv[2]);
84263    if( p2<0 ){
84264      p2 = -p2;
84265      negP2 = 1;
84266    }
84267  }else{
84268    p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
84269  }
84270  if( p1<0 ){
84271    p1 += len;
84272    if( p1<0 ){
84273      p2 += p1;
84274      if( p2<0 ) p2 = 0;
84275      p1 = 0;
84276    }
84277  }else if( p1>0 ){
84278    p1--;
84279  }else if( p2>0 ){
84280    p2--;
84281  }
84282  if( negP2 ){
84283    p1 -= p2;
84284    if( p1<0 ){
84285      p2 += p1;
84286      p1 = 0;
84287    }
84288  }
84289  assert( p1>=0 && p2>=0 );
84290  if( p0type!=SQLITE_BLOB ){
84291    while( *z && p1 ){
84292      SQLITE_SKIP_UTF8(z);
84293      p1--;
84294    }
84295    for(z2=z; *z2 && p2; p2--){
84296      SQLITE_SKIP_UTF8(z2);
84297    }
84298    sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
84299  }else{
84300    if( p1+p2>len ){
84301      p2 = len-p1;
84302      if( p2<0 ) p2 = 0;
84303    }
84304    sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
84305  }
84306}
84307
84308/*
84309** Implementation of the round() function
84310*/
84311#ifndef SQLITE_OMIT_FLOATING_POINT
84312static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84313  int n = 0;
84314  double r;
84315  char *zBuf;
84316  assert( argc==1 || argc==2 );
84317  if( argc==2 ){
84318    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
84319    n = sqlite3_value_int(argv[1]);
84320    if( n>30 ) n = 30;
84321    if( n<0 ) n = 0;
84322  }
84323  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
84324  r = sqlite3_value_double(argv[0]);
84325  /* If Y==0 and X will fit in a 64-bit int,
84326  ** handle the rounding directly,
84327  ** otherwise use printf.
84328  */
84329  if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
84330    r = (double)((sqlite_int64)(r+0.5));
84331  }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
84332    r = -(double)((sqlite_int64)((-r)+0.5));
84333  }else{
84334    zBuf = sqlite3_mprintf("%.*f",n,r);
84335    if( zBuf==0 ){
84336      sqlite3_result_error_nomem(context);
84337      return;
84338    }
84339    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
84340    sqlite3_free(zBuf);
84341  }
84342  sqlite3_result_double(context, r);
84343}
84344#endif
84345
84346/*
84347** Allocate nByte bytes of space using sqlite3_malloc(). If the
84348** allocation fails, call sqlite3_result_error_nomem() to notify
84349** the database handle that malloc() has failed and return NULL.
84350** If nByte is larger than the maximum string or blob length, then
84351** raise an SQLITE_TOOBIG exception and return NULL.
84352*/
84353static void *contextMalloc(sqlite3_context *context, i64 nByte){
84354  char *z;
84355  sqlite3 *db = sqlite3_context_db_handle(context);
84356  assert( nByte>0 );
84357  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
84358  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
84359  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
84360    sqlite3_result_error_toobig(context);
84361    z = 0;
84362  }else{
84363    z = sqlite3Malloc((int)nByte);
84364    if( !z ){
84365      sqlite3_result_error_nomem(context);
84366    }
84367  }
84368  return z;
84369}
84370
84371/*
84372** Implementation of the upper() and lower() SQL functions.
84373*/
84374static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84375  char *z1;
84376  const char *z2;
84377  int i, n;
84378  UNUSED_PARAMETER(argc);
84379  z2 = (char*)sqlite3_value_text(argv[0]);
84380  n = sqlite3_value_bytes(argv[0]);
84381  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84382  assert( z2==(char*)sqlite3_value_text(argv[0]) );
84383  if( z2 ){
84384    z1 = contextMalloc(context, ((i64)n)+1);
84385    if( z1 ){
84386      memcpy(z1, z2, n+1);
84387      for(i=0; z1[i]; i++){
84388        z1[i] = (char)sqlite3Toupper(z1[i]);
84389      }
84390      sqlite3_result_text(context, z1, -1, sqlite3_free);
84391    }
84392  }
84393}
84394static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84395  u8 *z1;
84396  const char *z2;
84397  int i, n;
84398  UNUSED_PARAMETER(argc);
84399  z2 = (char*)sqlite3_value_text(argv[0]);
84400  n = sqlite3_value_bytes(argv[0]);
84401  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84402  assert( z2==(char*)sqlite3_value_text(argv[0]) );
84403  if( z2 ){
84404    z1 = contextMalloc(context, ((i64)n)+1);
84405    if( z1 ){
84406      memcpy(z1, z2, n+1);
84407      for(i=0; z1[i]; i++){
84408        z1[i] = sqlite3Tolower(z1[i]);
84409      }
84410      sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
84411    }
84412  }
84413}
84414
84415
84416#if 0  /* This function is never used. */
84417/*
84418** The COALESCE() and IFNULL() functions used to be implemented as shown
84419** here.  But now they are implemented as VDBE code so that unused arguments
84420** do not have to be computed.  This legacy implementation is retained as
84421** comment.
84422*/
84423/*
84424** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
84425** All three do the same thing.  They return the first non-NULL
84426** argument.
84427*/
84428static void ifnullFunc(
84429  sqlite3_context *context,
84430  int argc,
84431  sqlite3_value **argv
84432){
84433  int i;
84434  for(i=0; i<argc; i++){
84435    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
84436      sqlite3_result_value(context, argv[i]);
84437      break;
84438    }
84439  }
84440}
84441#endif /* NOT USED */
84442#define ifnullFunc versionFunc   /* Substitute function - never called */
84443
84444/*
84445** Implementation of random().  Return a random integer.
84446*/
84447static void randomFunc(
84448  sqlite3_context *context,
84449  int NotUsed,
84450  sqlite3_value **NotUsed2
84451){
84452  sqlite_int64 r;
84453  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84454  sqlite3_randomness(sizeof(r), &r);
84455  if( r<0 ){
84456    /* We need to prevent a random number of 0x8000000000000000
84457    ** (or -9223372036854775808) since when you do abs() of that
84458    ** number of you get the same value back again.  To do this
84459    ** in a way that is testable, mask the sign bit off of negative
84460    ** values, resulting in a positive value.  Then take the
84461    ** 2s complement of that positive value.  The end result can
84462    ** therefore be no less than -9223372036854775807.
84463    */
84464    r = -(r ^ (((sqlite3_int64)1)<<63));
84465  }
84466  sqlite3_result_int64(context, r);
84467}
84468
84469/*
84470** Implementation of randomblob(N).  Return a random blob
84471** that is N bytes long.
84472*/
84473static void randomBlob(
84474  sqlite3_context *context,
84475  int argc,
84476  sqlite3_value **argv
84477){
84478  int n;
84479  unsigned char *p;
84480  assert( argc==1 );
84481  UNUSED_PARAMETER(argc);
84482  n = sqlite3_value_int(argv[0]);
84483  if( n<1 ){
84484    n = 1;
84485  }
84486  p = contextMalloc(context, n);
84487  if( p ){
84488    sqlite3_randomness(n, p);
84489    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
84490  }
84491}
84492
84493/*
84494** Implementation of the last_insert_rowid() SQL function.  The return
84495** value is the same as the sqlite3_last_insert_rowid() API function.
84496*/
84497static void last_insert_rowid(
84498  sqlite3_context *context,
84499  int NotUsed,
84500  sqlite3_value **NotUsed2
84501){
84502  sqlite3 *db = sqlite3_context_db_handle(context);
84503  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84504  /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
84505  ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
84506  ** function. */
84507  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
84508}
84509
84510/*
84511** Implementation of the changes() SQL function.
84512**
84513** IMP: R-62073-11209 The changes() SQL function is a wrapper
84514** around the sqlite3_changes() C/C++ function and hence follows the same
84515** rules for counting changes.
84516*/
84517static void changes(
84518  sqlite3_context *context,
84519  int NotUsed,
84520  sqlite3_value **NotUsed2
84521){
84522  sqlite3 *db = sqlite3_context_db_handle(context);
84523  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84524  sqlite3_result_int(context, sqlite3_changes(db));
84525}
84526
84527/*
84528** Implementation of the total_changes() SQL function.  The return value is
84529** the same as the sqlite3_total_changes() API function.
84530*/
84531static void total_changes(
84532  sqlite3_context *context,
84533  int NotUsed,
84534  sqlite3_value **NotUsed2
84535){
84536  sqlite3 *db = sqlite3_context_db_handle(context);
84537  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84538  /* IMP: R-52756-41993 This function is a wrapper around the
84539  ** sqlite3_total_changes() C/C++ interface. */
84540  sqlite3_result_int(context, sqlite3_total_changes(db));
84541}
84542
84543/*
84544** A structure defining how to do GLOB-style comparisons.
84545*/
84546struct compareInfo {
84547  u8 matchAll;
84548  u8 matchOne;
84549  u8 matchSet;
84550  u8 noCase;
84551};
84552
84553/*
84554** For LIKE and GLOB matching on EBCDIC machines, assume that every
84555** character is exactly one byte in size.  Also, all characters are
84556** able to participate in upper-case-to-lower-case mappings in EBCDIC
84557** whereas only characters less than 0x80 do in ASCII.
84558*/
84559#if defined(SQLITE_EBCDIC)
84560# define sqlite3Utf8Read(A,C)  (*(A++))
84561# define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
84562#else
84563# define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
84564#endif
84565
84566static const struct compareInfo globInfo = { '*', '?', '[', 0 };
84567/* The correct SQL-92 behavior is for the LIKE operator to ignore
84568** case.  Thus  'a' LIKE 'A' would be true. */
84569static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
84570/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
84571** is case sensitive causing 'a' LIKE 'A' to be false */
84572static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
84573
84574/*
84575** Compare two UTF-8 strings for equality where the first string can
84576** potentially be a "glob" expression.  Return true (1) if they
84577** are the same and false (0) if they are different.
84578**
84579** Globbing rules:
84580**
84581**      '*'       Matches any sequence of zero or more characters.
84582**
84583**      '?'       Matches exactly one character.
84584**
84585**     [...]      Matches one character from the enclosed list of
84586**                characters.
84587**
84588**     [^...]     Matches one character not in the enclosed list.
84589**
84590** With the [...] and [^...] matching, a ']' character can be included
84591** in the list by making it the first character after '[' or '^'.  A
84592** range of characters can be specified using '-'.  Example:
84593** "[a-z]" matches any single lower-case letter.  To match a '-', make
84594** it the last character in the list.
84595**
84596** This routine is usually quick, but can be N**2 in the worst case.
84597**
84598** Hints: to match '*' or '?', put them in "[]".  Like this:
84599**
84600**         abc[*]xyz        Matches "abc*xyz" only
84601*/
84602static int patternCompare(
84603  const u8 *zPattern,              /* The glob pattern */
84604  const u8 *zString,               /* The string to compare against the glob */
84605  const struct compareInfo *pInfo, /* Information about how to do the compare */
84606  u32 esc                          /* The escape character */
84607){
84608  u32 c, c2;
84609  int invert;
84610  int seen;
84611  u8 matchOne = pInfo->matchOne;
84612  u8 matchAll = pInfo->matchAll;
84613  u8 matchSet = pInfo->matchSet;
84614  u8 noCase = pInfo->noCase;
84615  int prevEscape = 0;     /* True if the previous character was 'escape' */
84616
84617  while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
84618    if( !prevEscape && c==matchAll ){
84619      while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
84620               || c == matchOne ){
84621        if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
84622          return 0;
84623        }
84624      }
84625      if( c==0 ){
84626        return 1;
84627      }else if( c==esc ){
84628        c = sqlite3Utf8Read(zPattern, &zPattern);
84629        if( c==0 ){
84630          return 0;
84631        }
84632      }else if( c==matchSet ){
84633        assert( esc==0 );         /* This is GLOB, not LIKE */
84634        assert( matchSet<0x80 );  /* '[' is a single-byte character */
84635        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
84636          SQLITE_SKIP_UTF8(zString);
84637        }
84638        return *zString!=0;
84639      }
84640      while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
84641        if( noCase ){
84642          GlogUpperToLower(c2);
84643          GlogUpperToLower(c);
84644          while( c2 != 0 && c2 != c ){
84645            c2 = sqlite3Utf8Read(zString, &zString);
84646            GlogUpperToLower(c2);
84647          }
84648        }else{
84649          while( c2 != 0 && c2 != c ){
84650            c2 = sqlite3Utf8Read(zString, &zString);
84651          }
84652        }
84653        if( c2==0 ) return 0;
84654        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
84655      }
84656      return 0;
84657    }else if( !prevEscape && c==matchOne ){
84658      if( sqlite3Utf8Read(zString, &zString)==0 ){
84659        return 0;
84660      }
84661    }else if( c==matchSet ){
84662      u32 prior_c = 0;
84663      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
84664      seen = 0;
84665      invert = 0;
84666      c = sqlite3Utf8Read(zString, &zString);
84667      if( c==0 ) return 0;
84668      c2 = sqlite3Utf8Read(zPattern, &zPattern);
84669      if( c2=='^' ){
84670        invert = 1;
84671        c2 = sqlite3Utf8Read(zPattern, &zPattern);
84672      }
84673      if( c2==']' ){
84674        if( c==']' ) seen = 1;
84675        c2 = sqlite3Utf8Read(zPattern, &zPattern);
84676      }
84677      while( c2 && c2!=']' ){
84678        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
84679          c2 = sqlite3Utf8Read(zPattern, &zPattern);
84680          if( c>=prior_c && c<=c2 ) seen = 1;
84681          prior_c = 0;
84682        }else{
84683          if( c==c2 ){
84684            seen = 1;
84685          }
84686          prior_c = c2;
84687        }
84688        c2 = sqlite3Utf8Read(zPattern, &zPattern);
84689      }
84690      if( c2==0 || (seen ^ invert)==0 ){
84691        return 0;
84692      }
84693    }else if( esc==c && !prevEscape ){
84694      prevEscape = 1;
84695    }else{
84696      c2 = sqlite3Utf8Read(zString, &zString);
84697      if( noCase ){
84698        GlogUpperToLower(c);
84699        GlogUpperToLower(c2);
84700      }
84701      if( c!=c2 ){
84702        return 0;
84703      }
84704      prevEscape = 0;
84705    }
84706  }
84707  return *zString==0;
84708}
84709
84710/*
84711** Count the number of times that the LIKE operator (or GLOB which is
84712** just a variation of LIKE) gets called.  This is used for testing
84713** only.
84714*/
84715#ifdef SQLITE_TEST
84716SQLITE_API int sqlite3_like_count = 0;
84717#endif
84718
84719
84720/*
84721** Implementation of the like() SQL function.  This function implements
84722** the build-in LIKE operator.  The first argument to the function is the
84723** pattern and the second argument is the string.  So, the SQL statements:
84724**
84725**       A LIKE B
84726**
84727** is implemented as like(B,A).
84728**
84729** This same function (with a different compareInfo structure) computes
84730** the GLOB operator.
84731*/
84732static void likeFunc(
84733  sqlite3_context *context,
84734  int argc,
84735  sqlite3_value **argv
84736){
84737  const unsigned char *zA, *zB;
84738  u32 escape = 0;
84739  int nPat;
84740  sqlite3 *db = sqlite3_context_db_handle(context);
84741
84742  zB = sqlite3_value_text(argv[0]);
84743  zA = sqlite3_value_text(argv[1]);
84744
84745  /* Limit the length of the LIKE or GLOB pattern to avoid problems
84746  ** of deep recursion and N*N behavior in patternCompare().
84747  */
84748  nPat = sqlite3_value_bytes(argv[0]);
84749  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
84750  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
84751  if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
84752    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
84753    return;
84754  }
84755  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
84756
84757  if( argc==3 ){
84758    /* The escape character string must consist of a single UTF-8 character.
84759    ** Otherwise, return an error.
84760    */
84761    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
84762    if( zEsc==0 ) return;
84763    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
84764      sqlite3_result_error(context,
84765          "ESCAPE expression must be a single character", -1);
84766      return;
84767    }
84768    escape = sqlite3Utf8Read(zEsc, &zEsc);
84769  }
84770  if( zA && zB ){
84771    struct compareInfo *pInfo = sqlite3_user_data(context);
84772#ifdef SQLITE_TEST
84773    sqlite3_like_count++;
84774#endif
84775
84776    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
84777  }
84778}
84779
84780/*
84781** Implementation of the NULLIF(x,y) function.  The result is the first
84782** argument if the arguments are different.  The result is NULL if the
84783** arguments are equal to each other.
84784*/
84785static void nullifFunc(
84786  sqlite3_context *context,
84787  int NotUsed,
84788  sqlite3_value **argv
84789){
84790  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
84791  UNUSED_PARAMETER(NotUsed);
84792  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
84793    sqlite3_result_value(context, argv[0]);
84794  }
84795}
84796
84797/*
84798** Implementation of the sqlite_version() function.  The result is the version
84799** of the SQLite library that is running.
84800*/
84801static void versionFunc(
84802  sqlite3_context *context,
84803  int NotUsed,
84804  sqlite3_value **NotUsed2
84805){
84806  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84807  /* IMP: R-48699-48617 This function is an SQL wrapper around the
84808  ** sqlite3_libversion() C-interface. */
84809  sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
84810}
84811
84812/*
84813** Implementation of the sqlite_source_id() function. The result is a string
84814** that identifies the particular version of the source code used to build
84815** SQLite.
84816*/
84817static void sourceidFunc(
84818  sqlite3_context *context,
84819  int NotUsed,
84820  sqlite3_value **NotUsed2
84821){
84822  UNUSED_PARAMETER2(NotUsed, NotUsed2);
84823  /* IMP: R-24470-31136 This function is an SQL wrapper around the
84824  ** sqlite3_sourceid() C interface. */
84825  sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
84826}
84827
84828/*
84829** Implementation of the sqlite_log() function.  This is a wrapper around
84830** sqlite3_log().  The return value is NULL.  The function exists purely for
84831** its side-effects.
84832*/
84833static void errlogFunc(
84834  sqlite3_context *context,
84835  int argc,
84836  sqlite3_value **argv
84837){
84838  UNUSED_PARAMETER(argc);
84839  UNUSED_PARAMETER(context);
84840  sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
84841}
84842
84843/*
84844** Implementation of the sqlite_compileoption_used() function.
84845** The result is an integer that identifies if the compiler option
84846** was used to build SQLite.
84847*/
84848#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
84849static void compileoptionusedFunc(
84850  sqlite3_context *context,
84851  int argc,
84852  sqlite3_value **argv
84853){
84854  const char *zOptName;
84855  assert( argc==1 );
84856  UNUSED_PARAMETER(argc);
84857  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
84858  ** function is a wrapper around the sqlite3_compileoption_used() C/C++
84859  ** function.
84860  */
84861  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
84862    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
84863  }
84864}
84865#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
84866
84867/*
84868** Implementation of the sqlite_compileoption_get() function.
84869** The result is a string that identifies the compiler options
84870** used to build SQLite.
84871*/
84872#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
84873static void compileoptiongetFunc(
84874  sqlite3_context *context,
84875  int argc,
84876  sqlite3_value **argv
84877){
84878  int n;
84879  assert( argc==1 );
84880  UNUSED_PARAMETER(argc);
84881  /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
84882  ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
84883  */
84884  n = sqlite3_value_int(argv[0]);
84885  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
84886}
84887#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
84888
84889/* Array for converting from half-bytes (nybbles) into ASCII hex
84890** digits. */
84891static const char hexdigits[] = {
84892  '0', '1', '2', '3', '4', '5', '6', '7',
84893  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
84894};
84895
84896/*
84897** EXPERIMENTAL - This is not an official function.  The interface may
84898** change.  This function may disappear.  Do not write code that depends
84899** on this function.
84900**
84901** Implementation of the QUOTE() function.  This function takes a single
84902** argument.  If the argument is numeric, the return value is the same as
84903** the argument.  If the argument is NULL, the return value is the string
84904** "NULL".  Otherwise, the argument is enclosed in single quotes with
84905** single-quote escapes.
84906*/
84907static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84908  assert( argc==1 );
84909  UNUSED_PARAMETER(argc);
84910  switch( sqlite3_value_type(argv[0]) ){
84911    case SQLITE_INTEGER:
84912    case SQLITE_FLOAT: {
84913      sqlite3_result_value(context, argv[0]);
84914      break;
84915    }
84916    case SQLITE_BLOB: {
84917      char *zText = 0;
84918      char const *zBlob = sqlite3_value_blob(argv[0]);
84919      int nBlob = sqlite3_value_bytes(argv[0]);
84920      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
84921      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
84922      if( zText ){
84923        int i;
84924        for(i=0; i<nBlob; i++){
84925          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
84926          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
84927        }
84928        zText[(nBlob*2)+2] = '\'';
84929        zText[(nBlob*2)+3] = '\0';
84930        zText[0] = 'X';
84931        zText[1] = '\'';
84932        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
84933        sqlite3_free(zText);
84934      }
84935      break;
84936    }
84937    case SQLITE_TEXT: {
84938      int i,j;
84939      u64 n;
84940      const unsigned char *zArg = sqlite3_value_text(argv[0]);
84941      char *z;
84942
84943      if( zArg==0 ) return;
84944      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
84945      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
84946      if( z ){
84947        z[0] = '\'';
84948        for(i=0, j=1; zArg[i]; i++){
84949          z[j++] = zArg[i];
84950          if( zArg[i]=='\'' ){
84951            z[j++] = '\'';
84952          }
84953        }
84954        z[j++] = '\'';
84955        z[j] = 0;
84956        sqlite3_result_text(context, z, j, sqlite3_free);
84957      }
84958      break;
84959    }
84960    default: {
84961      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
84962      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
84963      break;
84964    }
84965  }
84966}
84967
84968/*
84969** The hex() function.  Interpret the argument as a blob.  Return
84970** a hexadecimal rendering as text.
84971*/
84972static void hexFunc(
84973  sqlite3_context *context,
84974  int argc,
84975  sqlite3_value **argv
84976){
84977  int i, n;
84978  const unsigned char *pBlob;
84979  char *zHex, *z;
84980  assert( argc==1 );
84981  UNUSED_PARAMETER(argc);
84982  pBlob = sqlite3_value_blob(argv[0]);
84983  n = sqlite3_value_bytes(argv[0]);
84984  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
84985  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
84986  if( zHex ){
84987    for(i=0; i<n; i++, pBlob++){
84988      unsigned char c = *pBlob;
84989      *(z++) = hexdigits[(c>>4)&0xf];
84990      *(z++) = hexdigits[c&0xf];
84991    }
84992    *z = 0;
84993    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
84994  }
84995}
84996
84997/*
84998** The zeroblob(N) function returns a zero-filled blob of size N bytes.
84999*/
85000static void zeroblobFunc(
85001  sqlite3_context *context,
85002  int argc,
85003  sqlite3_value **argv
85004){
85005  i64 n;
85006  sqlite3 *db = sqlite3_context_db_handle(context);
85007  assert( argc==1 );
85008  UNUSED_PARAMETER(argc);
85009  n = sqlite3_value_int64(argv[0]);
85010  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
85011  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
85012  if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85013    sqlite3_result_error_toobig(context);
85014  }else{
85015    sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
85016  }
85017}
85018
85019/*
85020** The replace() function.  Three arguments are all strings: call
85021** them A, B, and C. The result is also a string which is derived
85022** from A by replacing every occurance of B with C.  The match
85023** must be exact.  Collating sequences are not used.
85024*/
85025static void replaceFunc(
85026  sqlite3_context *context,
85027  int argc,
85028  sqlite3_value **argv
85029){
85030  const unsigned char *zStr;        /* The input string A */
85031  const unsigned char *zPattern;    /* The pattern string B */
85032  const unsigned char *zRep;        /* The replacement string C */
85033  unsigned char *zOut;              /* The output */
85034  int nStr;                /* Size of zStr */
85035  int nPattern;            /* Size of zPattern */
85036  int nRep;                /* Size of zRep */
85037  i64 nOut;                /* Maximum size of zOut */
85038  int loopLimit;           /* Last zStr[] that might match zPattern[] */
85039  int i, j;                /* Loop counters */
85040
85041  assert( argc==3 );
85042  UNUSED_PARAMETER(argc);
85043  zStr = sqlite3_value_text(argv[0]);
85044  if( zStr==0 ) return;
85045  nStr = sqlite3_value_bytes(argv[0]);
85046  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
85047  zPattern = sqlite3_value_text(argv[1]);
85048  if( zPattern==0 ){
85049    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
85050            || sqlite3_context_db_handle(context)->mallocFailed );
85051    return;
85052  }
85053  if( zPattern[0]==0 ){
85054    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
85055    sqlite3_result_value(context, argv[0]);
85056    return;
85057  }
85058  nPattern = sqlite3_value_bytes(argv[1]);
85059  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
85060  zRep = sqlite3_value_text(argv[2]);
85061  if( zRep==0 ) return;
85062  nRep = sqlite3_value_bytes(argv[2]);
85063  assert( zRep==sqlite3_value_text(argv[2]) );
85064  nOut = nStr + 1;
85065  assert( nOut<SQLITE_MAX_LENGTH );
85066  zOut = contextMalloc(context, (i64)nOut);
85067  if( zOut==0 ){
85068    return;
85069  }
85070  loopLimit = nStr - nPattern;
85071  for(i=j=0; i<=loopLimit; i++){
85072    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
85073      zOut[j++] = zStr[i];
85074    }else{
85075      u8 *zOld;
85076      sqlite3 *db = sqlite3_context_db_handle(context);
85077      nOut += nRep - nPattern;
85078      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
85079      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
85080      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85081        sqlite3_result_error_toobig(context);
85082        sqlite3_free(zOut);
85083        return;
85084      }
85085      zOld = zOut;
85086      zOut = sqlite3_realloc(zOut, (int)nOut);
85087      if( zOut==0 ){
85088        sqlite3_result_error_nomem(context);
85089        sqlite3_free(zOld);
85090        return;
85091      }
85092      memcpy(&zOut[j], zRep, nRep);
85093      j += nRep;
85094      i += nPattern-1;
85095    }
85096  }
85097  assert( j+nStr-i+1==nOut );
85098  memcpy(&zOut[j], &zStr[i], nStr-i);
85099  j += nStr - i;
85100  assert( j<=nOut );
85101  zOut[j] = 0;
85102  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
85103}
85104
85105/*
85106** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
85107** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
85108*/
85109static void trimFunc(
85110  sqlite3_context *context,
85111  int argc,
85112  sqlite3_value **argv
85113){
85114  const unsigned char *zIn;         /* Input string */
85115  const unsigned char *zCharSet;    /* Set of characters to trim */
85116  int nIn;                          /* Number of bytes in input */
85117  int flags;                        /* 1: trimleft  2: trimright  3: trim */
85118  int i;                            /* Loop counter */
85119  unsigned char *aLen = 0;          /* Length of each character in zCharSet */
85120  unsigned char **azChar = 0;       /* Individual characters in zCharSet */
85121  int nChar;                        /* Number of characters in zCharSet */
85122
85123  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
85124    return;
85125  }
85126  zIn = sqlite3_value_text(argv[0]);
85127  if( zIn==0 ) return;
85128  nIn = sqlite3_value_bytes(argv[0]);
85129  assert( zIn==sqlite3_value_text(argv[0]) );
85130  if( argc==1 ){
85131    static const unsigned char lenOne[] = { 1 };
85132    static unsigned char * const azOne[] = { (u8*)" " };
85133    nChar = 1;
85134    aLen = (u8*)lenOne;
85135    azChar = (unsigned char **)azOne;
85136    zCharSet = 0;
85137  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
85138    return;
85139  }else{
85140    const unsigned char *z;
85141    for(z=zCharSet, nChar=0; *z; nChar++){
85142      SQLITE_SKIP_UTF8(z);
85143    }
85144    if( nChar>0 ){
85145      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
85146      if( azChar==0 ){
85147        return;
85148      }
85149      aLen = (unsigned char*)&azChar[nChar];
85150      for(z=zCharSet, nChar=0; *z; nChar++){
85151        azChar[nChar] = (unsigned char *)z;
85152        SQLITE_SKIP_UTF8(z);
85153        aLen[nChar] = (u8)(z - azChar[nChar]);
85154      }
85155    }
85156  }
85157  if( nChar>0 ){
85158    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
85159    if( flags & 1 ){
85160      while( nIn>0 ){
85161        int len = 0;
85162        for(i=0; i<nChar; i++){
85163          len = aLen[i];
85164          if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
85165        }
85166        if( i>=nChar ) break;
85167        zIn += len;
85168        nIn -= len;
85169      }
85170    }
85171    if( flags & 2 ){
85172      while( nIn>0 ){
85173        int len = 0;
85174        for(i=0; i<nChar; i++){
85175          len = aLen[i];
85176          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
85177        }
85178        if( i>=nChar ) break;
85179        nIn -= len;
85180      }
85181    }
85182    if( zCharSet ){
85183      sqlite3_free(azChar);
85184    }
85185  }
85186  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
85187}
85188
85189
85190/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
85191** is only available if the SQLITE_SOUNDEX compile-time option is used
85192** when SQLite is built.
85193*/
85194#ifdef SQLITE_SOUNDEX
85195/*
85196** Compute the soundex encoding of a word.
85197**
85198** IMP: R-59782-00072 The soundex(X) function returns a string that is the
85199** soundex encoding of the string X.
85200*/
85201static void soundexFunc(
85202  sqlite3_context *context,
85203  int argc,
85204  sqlite3_value **argv
85205){
85206  char zResult[8];
85207  const u8 *zIn;
85208  int i, j;
85209  static const unsigned char iCode[] = {
85210    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85211    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85212    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85213    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85214    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
85215    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
85216    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
85217    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
85218  };
85219  assert( argc==1 );
85220  zIn = (u8*)sqlite3_value_text(argv[0]);
85221  if( zIn==0 ) zIn = (u8*)"";
85222  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
85223  if( zIn[i] ){
85224    u8 prevcode = iCode[zIn[i]&0x7f];
85225    zResult[0] = sqlite3Toupper(zIn[i]);
85226    for(j=1; j<4 && zIn[i]; i++){
85227      int code = iCode[zIn[i]&0x7f];
85228      if( code>0 ){
85229        if( code!=prevcode ){
85230          prevcode = code;
85231          zResult[j++] = code + '0';
85232        }
85233      }else{
85234        prevcode = 0;
85235      }
85236    }
85237    while( j<4 ){
85238      zResult[j++] = '0';
85239    }
85240    zResult[j] = 0;
85241    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
85242  }else{
85243    /* IMP: R-64894-50321 The string "?000" is returned if the argument
85244    ** is NULL or contains no ASCII alphabetic characters. */
85245    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
85246  }
85247}
85248#endif /* SQLITE_SOUNDEX */
85249
85250#ifndef SQLITE_OMIT_LOAD_EXTENSION
85251/*
85252** A function that loads a shared-library extension then returns NULL.
85253*/
85254static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
85255  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
85256  const char *zProc;
85257  sqlite3 *db = sqlite3_context_db_handle(context);
85258  char *zErrMsg = 0;
85259
85260  if( argc==2 ){
85261    zProc = (const char *)sqlite3_value_text(argv[1]);
85262  }else{
85263    zProc = 0;
85264  }
85265  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
85266    sqlite3_result_error(context, zErrMsg, -1);
85267    sqlite3_free(zErrMsg);
85268  }
85269}
85270#endif
85271
85272
85273/*
85274** An instance of the following structure holds the context of a
85275** sum() or avg() aggregate computation.
85276*/
85277typedef struct SumCtx SumCtx;
85278struct SumCtx {
85279  double rSum;      /* Floating point sum */
85280  i64 iSum;         /* Integer sum */
85281  i64 cnt;          /* Number of elements summed */
85282  u8 overflow;      /* True if integer overflow seen */
85283  u8 approx;        /* True if non-integer value was input to the sum */
85284};
85285
85286/*
85287** Routines used to compute the sum, average, and total.
85288**
85289** The SUM() function follows the (broken) SQL standard which means
85290** that it returns NULL if it sums over no inputs.  TOTAL returns
85291** 0.0 in that case.  In addition, TOTAL always returns a float where
85292** SUM might return an integer if it never encounters a floating point
85293** value.  TOTAL never fails, but SUM might through an exception if
85294** it overflows an integer.
85295*/
85296static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
85297  SumCtx *p;
85298  int type;
85299  assert( argc==1 );
85300  UNUSED_PARAMETER(argc);
85301  p = sqlite3_aggregate_context(context, sizeof(*p));
85302  type = sqlite3_value_numeric_type(argv[0]);
85303  if( p && type!=SQLITE_NULL ){
85304    p->cnt++;
85305    if( type==SQLITE_INTEGER ){
85306      i64 v = sqlite3_value_int64(argv[0]);
85307      p->rSum += v;
85308      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
85309        p->overflow = 1;
85310      }
85311    }else{
85312      p->rSum += sqlite3_value_double(argv[0]);
85313      p->approx = 1;
85314    }
85315  }
85316}
85317static void sumFinalize(sqlite3_context *context){
85318  SumCtx *p;
85319  p = sqlite3_aggregate_context(context, 0);
85320  if( p && p->cnt>0 ){
85321    if( p->overflow ){
85322      sqlite3_result_error(context,"integer overflow",-1);
85323    }else if( p->approx ){
85324      sqlite3_result_double(context, p->rSum);
85325    }else{
85326      sqlite3_result_int64(context, p->iSum);
85327    }
85328  }
85329}
85330static void avgFinalize(sqlite3_context *context){
85331  SumCtx *p;
85332  p = sqlite3_aggregate_context(context, 0);
85333  if( p && p->cnt>0 ){
85334    sqlite3_result_double(context, p->rSum/(double)p->cnt);
85335  }
85336}
85337static void totalFinalize(sqlite3_context *context){
85338  SumCtx *p;
85339  p = sqlite3_aggregate_context(context, 0);
85340  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
85341  sqlite3_result_double(context, p ? p->rSum : (double)0);
85342}
85343
85344/*
85345** The following structure keeps track of state information for the
85346** count() aggregate function.
85347*/
85348typedef struct CountCtx CountCtx;
85349struct CountCtx {
85350  i64 n;
85351};
85352
85353/*
85354** Routines to implement the count() aggregate function.
85355*/
85356static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
85357  CountCtx *p;
85358  p = sqlite3_aggregate_context(context, sizeof(*p));
85359  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
85360    p->n++;
85361  }
85362
85363#ifndef SQLITE_OMIT_DEPRECATED
85364  /* The sqlite3_aggregate_count() function is deprecated.  But just to make
85365  ** sure it still operates correctly, verify that its count agrees with our
85366  ** internal count when using count(*) and when the total count can be
85367  ** expressed as a 32-bit integer. */
85368  assert( argc==1 || p==0 || p->n>0x7fffffff
85369          || p->n==sqlite3_aggregate_count(context) );
85370#endif
85371}
85372static void countFinalize(sqlite3_context *context){
85373  CountCtx *p;
85374  p = sqlite3_aggregate_context(context, 0);
85375  sqlite3_result_int64(context, p ? p->n : 0);
85376}
85377
85378/*
85379** Routines to implement min() and max() aggregate functions.
85380*/
85381static void minmaxStep(
85382  sqlite3_context *context,
85383  int NotUsed,
85384  sqlite3_value **argv
85385){
85386  Mem *pArg  = (Mem *)argv[0];
85387  Mem *pBest;
85388  UNUSED_PARAMETER(NotUsed);
85389
85390  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
85391  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
85392  if( !pBest ) return;
85393
85394  if( pBest->flags ){
85395    int max;
85396    int cmp;
85397    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
85398    /* This step function is used for both the min() and max() aggregates,
85399    ** the only difference between the two being that the sense of the
85400    ** comparison is inverted. For the max() aggregate, the
85401    ** sqlite3_user_data() function returns (void *)-1. For min() it
85402    ** returns (void *)db, where db is the sqlite3* database pointer.
85403    ** Therefore the next statement sets variable 'max' to 1 for the max()
85404    ** aggregate, or 0 for min().
85405    */
85406    max = sqlite3_user_data(context)!=0;
85407    cmp = sqlite3MemCompare(pBest, pArg, pColl);
85408    if( (max && cmp<0) || (!max && cmp>0) ){
85409      sqlite3VdbeMemCopy(pBest, pArg);
85410    }
85411  }else{
85412    sqlite3VdbeMemCopy(pBest, pArg);
85413  }
85414}
85415static void minMaxFinalize(sqlite3_context *context){
85416  sqlite3_value *pRes;
85417  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
85418  if( pRes ){
85419    if( ALWAYS(pRes->flags) ){
85420      sqlite3_result_value(context, pRes);
85421    }
85422    sqlite3VdbeMemRelease(pRes);
85423  }
85424}
85425
85426/*
85427** group_concat(EXPR, ?SEPARATOR?)
85428*/
85429static void groupConcatStep(
85430  sqlite3_context *context,
85431  int argc,
85432  sqlite3_value **argv
85433){
85434  const char *zVal;
85435  StrAccum *pAccum;
85436  const char *zSep;
85437  int nVal, nSep;
85438  assert( argc==1 || argc==2 );
85439  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
85440  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
85441
85442  if( pAccum ){
85443    sqlite3 *db = sqlite3_context_db_handle(context);
85444    int firstTerm = pAccum->useMalloc==0;
85445    pAccum->useMalloc = 2;
85446    pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
85447    if( !firstTerm ){
85448      if( argc==2 ){
85449        zSep = (char*)sqlite3_value_text(argv[1]);
85450        nSep = sqlite3_value_bytes(argv[1]);
85451      }else{
85452        zSep = ",";
85453        nSep = 1;
85454      }
85455      sqlite3StrAccumAppend(pAccum, zSep, nSep);
85456    }
85457    zVal = (char*)sqlite3_value_text(argv[0]);
85458    nVal = sqlite3_value_bytes(argv[0]);
85459    sqlite3StrAccumAppend(pAccum, zVal, nVal);
85460  }
85461}
85462static void groupConcatFinalize(sqlite3_context *context){
85463  StrAccum *pAccum;
85464  pAccum = sqlite3_aggregate_context(context, 0);
85465  if( pAccum ){
85466    if( pAccum->tooBig ){
85467      sqlite3_result_error_toobig(context);
85468    }else if( pAccum->mallocFailed ){
85469      sqlite3_result_error_nomem(context);
85470    }else{
85471      sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
85472                          sqlite3_free);
85473    }
85474  }
85475}
85476
85477/*
85478** This routine does per-connection function registration.  Most
85479** of the built-in functions above are part of the global function set.
85480** This routine only deals with those that are not global.
85481*/
85482SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
85483  int rc = sqlite3_overload_function(db, "MATCH", 2);
85484  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
85485  if( rc==SQLITE_NOMEM ){
85486    db->mallocFailed = 1;
85487  }
85488}
85489
85490/*
85491** Set the LIKEOPT flag on the 2-argument function with the given name.
85492*/
85493static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
85494  FuncDef *pDef;
85495  pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
85496                             2, SQLITE_UTF8, 0);
85497  if( ALWAYS(pDef) ){
85498    pDef->flags = flagVal;
85499  }
85500}
85501
85502/*
85503** Register the built-in LIKE and GLOB functions.  The caseSensitive
85504** parameter determines whether or not the LIKE operator is case
85505** sensitive.  GLOB is always case sensitive.
85506*/
85507SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
85508  struct compareInfo *pInfo;
85509  if( caseSensitive ){
85510    pInfo = (struct compareInfo*)&likeInfoAlt;
85511  }else{
85512    pInfo = (struct compareInfo*)&likeInfoNorm;
85513  }
85514  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
85515  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
85516  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
85517      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
85518  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
85519  setLikeOptFlag(db, "like",
85520      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
85521}
85522
85523/*
85524** pExpr points to an expression which implements a function.  If
85525** it is appropriate to apply the LIKE optimization to that function
85526** then set aWc[0] through aWc[2] to the wildcard characters and
85527** return TRUE.  If the function is not a LIKE-style function then
85528** return FALSE.
85529*/
85530SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
85531  FuncDef *pDef;
85532  if( pExpr->op!=TK_FUNCTION
85533   || !pExpr->x.pList
85534   || pExpr->x.pList->nExpr!=2
85535  ){
85536    return 0;
85537  }
85538  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
85539  pDef = sqlite3FindFunction(db, pExpr->u.zToken,
85540                             sqlite3Strlen30(pExpr->u.zToken),
85541                             2, SQLITE_UTF8, 0);
85542  if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
85543    return 0;
85544  }
85545
85546  /* The memcpy() statement assumes that the wildcard characters are
85547  ** the first three statements in the compareInfo structure.  The
85548  ** asserts() that follow verify that assumption
85549  */
85550  memcpy(aWc, pDef->pUserData, 3);
85551  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
85552  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
85553  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
85554  *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
85555  return 1;
85556}
85557
85558/*
85559** All all of the FuncDef structures in the aBuiltinFunc[] array above
85560** to the global function hash table.  This occurs at start-time (as
85561** a consequence of calling sqlite3_initialize()).
85562**
85563** After this routine runs
85564*/
85565SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
85566  /*
85567  ** The following array holds FuncDef structures for all of the functions
85568  ** defined in this file.
85569  **
85570  ** The array cannot be constant since changes are made to the
85571  ** FuncDef.pHash elements at start-time.  The elements of this array
85572  ** are read-only after initialization is complete.
85573  */
85574  static SQLITE_WSD FuncDef aBuiltinFunc[] = {
85575    FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
85576    FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
85577    FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
85578    FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
85579    FUNCTION(trim,               1, 3, 0, trimFunc         ),
85580    FUNCTION(trim,               2, 3, 0, trimFunc         ),
85581    FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
85582    FUNCTION(min,                0, 0, 1, 0                ),
85583    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
85584    FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
85585    FUNCTION(max,                0, 1, 1, 0                ),
85586    AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
85587    FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
85588    FUNCTION(length,             1, 0, 0, lengthFunc       ),
85589    FUNCTION(substr,             2, 0, 0, substrFunc       ),
85590    FUNCTION(substr,             3, 0, 0, substrFunc       ),
85591    FUNCTION(abs,                1, 0, 0, absFunc          ),
85592#ifndef SQLITE_OMIT_FLOATING_POINT
85593    FUNCTION(round,              1, 0, 0, roundFunc        ),
85594    FUNCTION(round,              2, 0, 0, roundFunc        ),
85595#endif
85596    FUNCTION(upper,              1, 0, 0, upperFunc        ),
85597    FUNCTION(lower,              1, 0, 0, lowerFunc        ),
85598    FUNCTION(coalesce,           1, 0, 0, 0                ),
85599    FUNCTION(coalesce,           0, 0, 0, 0                ),
85600/*  FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ), */
85601    {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
85602    FUNCTION(hex,                1, 0, 0, hexFunc          ),
85603/*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
85604    {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
85605    FUNCTION(random,             0, 0, 0, randomFunc       ),
85606    FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
85607    FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
85608    FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
85609    FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
85610    FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
85611#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
85612    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
85613    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
85614#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
85615    FUNCTION(quote,              1, 0, 0, quoteFunc        ),
85616    FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
85617    FUNCTION(changes,            0, 0, 0, changes          ),
85618    FUNCTION(total_changes,      0, 0, 0, total_changes    ),
85619    FUNCTION(replace,            3, 0, 0, replaceFunc      ),
85620    FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
85621  #ifdef SQLITE_SOUNDEX
85622    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
85623  #endif
85624  #ifndef SQLITE_OMIT_LOAD_EXTENSION
85625    FUNCTION(load_extension,     1, 0, 0, loadExt          ),
85626    FUNCTION(load_extension,     2, 0, 0, loadExt          ),
85627  #endif
85628    AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
85629    AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
85630    AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
85631 /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
85632    {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
85633    AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
85634    AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
85635    AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
85636
85637    LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
85638  #ifdef SQLITE_CASE_SENSITIVE_LIKE
85639    LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
85640    LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
85641  #else
85642    LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
85643    LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
85644  #endif
85645  };
85646
85647  int i;
85648  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
85649  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
85650
85651  for(i=0; i<ArraySize(aBuiltinFunc); i++){
85652    sqlite3FuncDefInsert(pHash, &aFunc[i]);
85653  }
85654  sqlite3RegisterDateTimeFunctions();
85655#ifndef SQLITE_OMIT_ALTERTABLE
85656  sqlite3AlterFunctions();
85657#endif
85658}
85659
85660/************** End of func.c ************************************************/
85661/************** Begin file fkey.c ********************************************/
85662/*
85663**
85664** The author disclaims copyright to this source code.  In place of
85665** a legal notice, here is a blessing:
85666**
85667**    May you do good and not evil.
85668**    May you find forgiveness for yourself and forgive others.
85669**    May you share freely, never taking more than you give.
85670**
85671*************************************************************************
85672** This file contains code used by the compiler to add foreign key
85673** support to compiled SQL statements.
85674*/
85675
85676#ifndef SQLITE_OMIT_FOREIGN_KEY
85677#ifndef SQLITE_OMIT_TRIGGER
85678
85679/*
85680** Deferred and Immediate FKs
85681** --------------------------
85682**
85683** Foreign keys in SQLite come in two flavours: deferred and immediate.
85684** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
85685** is returned and the current statement transaction rolled back. If a
85686** deferred foreign key constraint is violated, no action is taken
85687** immediately. However if the application attempts to commit the
85688** transaction before fixing the constraint violation, the attempt fails.
85689**
85690** Deferred constraints are implemented using a simple counter associated
85691** with the database handle. The counter is set to zero each time a
85692** database transaction is opened. Each time a statement is executed
85693** that causes a foreign key violation, the counter is incremented. Each
85694** time a statement is executed that removes an existing violation from
85695** the database, the counter is decremented. When the transaction is
85696** committed, the commit fails if the current value of the counter is
85697** greater than zero. This scheme has two big drawbacks:
85698**
85699**   * When a commit fails due to a deferred foreign key constraint,
85700**     there is no way to tell which foreign constraint is not satisfied,
85701**     or which row it is not satisfied for.
85702**
85703**   * If the database contains foreign key violations when the
85704**     transaction is opened, this may cause the mechanism to malfunction.
85705**
85706** Despite these problems, this approach is adopted as it seems simpler
85707** than the alternatives.
85708**
85709** INSERT operations:
85710**
85711**   I.1) For each FK for which the table is the child table, search
85712**        the parent table for a match. If none is found increment the
85713**        constraint counter.
85714**
85715**   I.2) For each FK for which the table is the parent table,
85716**        search the child table for rows that correspond to the new
85717**        row in the parent table. Decrement the counter for each row
85718**        found (as the constraint is now satisfied).
85719**
85720** DELETE operations:
85721**
85722**   D.1) For each FK for which the table is the child table,
85723**        search the parent table for a row that corresponds to the
85724**        deleted row in the child table. If such a row is not found,
85725**        decrement the counter.
85726**
85727**   D.2) For each FK for which the table is the parent table, search
85728**        the child table for rows that correspond to the deleted row
85729**        in the parent table. For each found increment the counter.
85730**
85731** UPDATE operations:
85732**
85733**   An UPDATE command requires that all 4 steps above are taken, but only
85734**   for FK constraints for which the affected columns are actually
85735**   modified (values must be compared at runtime).
85736**
85737** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
85738** This simplifies the implementation a bit.
85739**
85740** For the purposes of immediate FK constraints, the OR REPLACE conflict
85741** resolution is considered to delete rows before the new row is inserted.
85742** If a delete caused by OR REPLACE violates an FK constraint, an exception
85743** is thrown, even if the FK constraint would be satisfied after the new
85744** row is inserted.
85745**
85746** Immediate constraints are usually handled similarly. The only difference
85747** is that the counter used is stored as part of each individual statement
85748** object (struct Vdbe). If, after the statement has run, its immediate
85749** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
85750** and the statement transaction is rolled back. An exception is an INSERT
85751** statement that inserts a single row only (no triggers). In this case,
85752** instead of using a counter, an exception is thrown immediately if the
85753** INSERT violates a foreign key constraint. This is necessary as such
85754** an INSERT does not open a statement transaction.
85755**
85756** TODO: How should dropping a table be handled? How should renaming a
85757** table be handled?
85758**
85759**
85760** Query API Notes
85761** ---------------
85762**
85763** Before coding an UPDATE or DELETE row operation, the code-generator
85764** for those two operations needs to know whether or not the operation
85765** requires any FK processing and, if so, which columns of the original
85766** row are required by the FK processing VDBE code (i.e. if FKs were
85767** implemented using triggers, which of the old.* columns would be
85768** accessed). No information is required by the code-generator before
85769** coding an INSERT operation. The functions used by the UPDATE/DELETE
85770** generation code to query for this information are:
85771**
85772**   sqlite3FkRequired() - Test to see if FK processing is required.
85773**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
85774**
85775**
85776** Externally accessible module functions
85777** --------------------------------------
85778**
85779**   sqlite3FkCheck()    - Check for foreign key violations.
85780**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
85781**   sqlite3FkDelete()   - Delete an FKey structure.
85782*/
85783
85784/*
85785** VDBE Calling Convention
85786** -----------------------
85787**
85788** Example:
85789**
85790**   For the following INSERT statement:
85791**
85792**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
85793**     INSERT INTO t1 VALUES(1, 2, 3.1);
85794**
85795**   Register (x):        2    (type integer)
85796**   Register (x+1):      1    (type integer)
85797**   Register (x+2):      NULL (type NULL)
85798**   Register (x+3):      3.1  (type real)
85799*/
85800
85801/*
85802** A foreign key constraint requires that the key columns in the parent
85803** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
85804** Given that pParent is the parent table for foreign key constraint pFKey,
85805** search the schema a unique index on the parent key columns.
85806**
85807** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
85808** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
85809** is set to point to the unique index.
85810**
85811** If the parent key consists of a single column (the foreign key constraint
85812** is not a composite foreign key), output variable *paiCol is set to NULL.
85813** Otherwise, it is set to point to an allocated array of size N, where
85814** N is the number of columns in the parent key. The first element of the
85815** array is the index of the child table column that is mapped by the FK
85816** constraint to the parent table column stored in the left-most column
85817** of index *ppIdx. The second element of the array is the index of the
85818** child table column that corresponds to the second left-most column of
85819** *ppIdx, and so on.
85820**
85821** If the required index cannot be found, either because:
85822**
85823**   1) The named parent key columns do not exist, or
85824**
85825**   2) The named parent key columns do exist, but are not subject to a
85826**      UNIQUE or PRIMARY KEY constraint, or
85827**
85828**   3) No parent key columns were provided explicitly as part of the
85829**      foreign key definition, and the parent table does not have a
85830**      PRIMARY KEY, or
85831**
85832**   4) No parent key columns were provided explicitly as part of the
85833**      foreign key definition, and the PRIMARY KEY of the parent table
85834**      consists of a a different number of columns to the child key in
85835**      the child table.
85836**
85837** then non-zero is returned, and a "foreign key mismatch" error loaded
85838** into pParse. If an OOM error occurs, non-zero is returned and the
85839** pParse->db->mallocFailed flag is set.
85840*/
85841static int locateFkeyIndex(
85842  Parse *pParse,                  /* Parse context to store any error in */
85843  Table *pParent,                 /* Parent table of FK constraint pFKey */
85844  FKey *pFKey,                    /* Foreign key to find index for */
85845  Index **ppIdx,                  /* OUT: Unique index on parent table */
85846  int **paiCol                    /* OUT: Map of index columns in pFKey */
85847){
85848  Index *pIdx = 0;                    /* Value to return via *ppIdx */
85849  int *aiCol = 0;                     /* Value to return via *paiCol */
85850  int nCol = pFKey->nCol;             /* Number of columns in parent key */
85851  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
85852
85853  /* The caller is responsible for zeroing output parameters. */
85854  assert( ppIdx && *ppIdx==0 );
85855  assert( !paiCol || *paiCol==0 );
85856  assert( pParse );
85857
85858  /* If this is a non-composite (single column) foreign key, check if it
85859  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
85860  ** and *paiCol set to zero and return early.
85861  **
85862  ** Otherwise, for a composite foreign key (more than one column), allocate
85863  ** space for the aiCol array (returned via output parameter *paiCol).
85864  ** Non-composite foreign keys do not require the aiCol array.
85865  */
85866  if( nCol==1 ){
85867    /* The FK maps to the IPK if any of the following are true:
85868    **
85869    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
85870    **      mapped to the primary key of table pParent, or
85871    **   2) The FK is explicitly mapped to a column declared as INTEGER
85872    **      PRIMARY KEY.
85873    */
85874    if( pParent->iPKey>=0 ){
85875      if( !zKey ) return 0;
85876      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
85877    }
85878  }else if( paiCol ){
85879    assert( nCol>1 );
85880    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
85881    if( !aiCol ) return 1;
85882    *paiCol = aiCol;
85883  }
85884
85885  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
85886    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){
85887      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
85888      ** of columns. If each indexed column corresponds to a foreign key
85889      ** column of pFKey, then this index is a winner.  */
85890
85891      if( zKey==0 ){
85892        /* If zKey is NULL, then this foreign key is implicitly mapped to
85893        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
85894        ** identified by the test (Index.autoIndex==2).  */
85895        if( pIdx->autoIndex==2 ){
85896          if( aiCol ){
85897            int i;
85898            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
85899          }
85900          break;
85901        }
85902      }else{
85903        /* If zKey is non-NULL, then this foreign key was declared to
85904        ** map to an explicit list of columns in table pParent. Check if this
85905        ** index matches those columns. Also, check that the index uses
85906        ** the default collation sequences for each column. */
85907        int i, j;
85908        for(i=0; i<nCol; i++){
85909          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
85910          char *zDfltColl;                  /* Def. collation for column */
85911          char *zIdxCol;                    /* Name of indexed column */
85912
85913          /* If the index uses a collation sequence that is different from
85914          ** the default collation sequence for the column, this index is
85915          ** unusable. Bail out early in this case.  */
85916          zDfltColl = pParent->aCol[iCol].zColl;
85917          if( !zDfltColl ){
85918            zDfltColl = "BINARY";
85919          }
85920          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
85921
85922          zIdxCol = pParent->aCol[iCol].zName;
85923          for(j=0; j<nCol; j++){
85924            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
85925              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
85926              break;
85927            }
85928          }
85929          if( j==nCol ) break;
85930        }
85931        if( i==nCol ) break;      /* pIdx is usable */
85932      }
85933    }
85934  }
85935
85936  if( !pIdx ){
85937    if( !pParse->disableTriggers ){
85938      sqlite3ErrorMsg(pParse, "foreign key mismatch");
85939    }
85940    sqlite3DbFree(pParse->db, aiCol);
85941    return 1;
85942  }
85943
85944  *ppIdx = pIdx;
85945  return 0;
85946}
85947
85948/*
85949** This function is called when a row is inserted into or deleted from the
85950** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
85951** on the child table of pFKey, this function is invoked twice for each row
85952** affected - once to "delete" the old row, and then again to "insert" the
85953** new row.
85954**
85955** Each time it is called, this function generates VDBE code to locate the
85956** row in the parent table that corresponds to the row being inserted into
85957** or deleted from the child table. If the parent row can be found, no
85958** special action is taken. Otherwise, if the parent row can *not* be
85959** found in the parent table:
85960**
85961**   Operation | FK type   | Action taken
85962**   --------------------------------------------------------------------------
85963**   INSERT      immediate   Increment the "immediate constraint counter".
85964**
85965**   DELETE      immediate   Decrement the "immediate constraint counter".
85966**
85967**   INSERT      deferred    Increment the "deferred constraint counter".
85968**
85969**   DELETE      deferred    Decrement the "deferred constraint counter".
85970**
85971** These operations are identified in the comment at the top of this file
85972** (fkey.c) as "I.1" and "D.1".
85973*/
85974static void fkLookupParent(
85975  Parse *pParse,        /* Parse context */
85976  int iDb,              /* Index of database housing pTab */
85977  Table *pTab,          /* Parent table of FK pFKey */
85978  Index *pIdx,          /* Unique index on parent key columns in pTab */
85979  FKey *pFKey,          /* Foreign key constraint */
85980  int *aiCol,           /* Map from parent key columns to child table columns */
85981  int regData,          /* Address of array containing child table row */
85982  int nIncr,            /* Increment constraint counter by this */
85983  int isIgnore          /* If true, pretend pTab contains all NULL values */
85984){
85985  int i;                                    /* Iterator variable */
85986  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
85987  int iCur = pParse->nTab - 1;              /* Cursor number to use */
85988  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
85989
85990  /* If nIncr is less than zero, then check at runtime if there are any
85991  ** outstanding constraints to resolve. If there are not, there is no need
85992  ** to check if deleting this row resolves any outstanding violations.
85993  **
85994  ** Check if any of the key columns in the child table row are NULL. If
85995  ** any are, then the constraint is considered satisfied. No need to
85996  ** search for a matching row in the parent table.  */
85997  if( nIncr<0 ){
85998    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
85999  }
86000  for(i=0; i<pFKey->nCol; i++){
86001    int iReg = aiCol[i] + regData + 1;
86002    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
86003  }
86004
86005  if( isIgnore==0 ){
86006    if( pIdx==0 ){
86007      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
86008      ** column of the parent table (table pTab).  */
86009      int iMustBeInt;               /* Address of MustBeInt instruction */
86010      int regTemp = sqlite3GetTempReg(pParse);
86011
86012      /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
86013      ** apply the affinity of the parent key). If this fails, then there
86014      ** is no matching parent key. Before using MustBeInt, make a copy of
86015      ** the value. Otherwise, the value inserted into the child key column
86016      ** will have INTEGER affinity applied to it, which may not be correct.  */
86017      sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
86018      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
86019
86020      /* If the parent table is the same as the child table, and we are about
86021      ** to increment the constraint-counter (i.e. this is an INSERT operation),
86022      ** then check if the row being inserted matches itself. If so, do not
86023      ** increment the constraint-counter.  */
86024      if( pTab==pFKey->pFrom && nIncr==1 ){
86025        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
86026      }
86027
86028      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
86029      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
86030      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
86031      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
86032      sqlite3VdbeJumpHere(v, iMustBeInt);
86033      sqlite3ReleaseTempReg(pParse, regTemp);
86034    }else{
86035      int nCol = pFKey->nCol;
86036      int regTemp = sqlite3GetTempRange(pParse, nCol);
86037      int regRec = sqlite3GetTempReg(pParse);
86038      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
86039
86040      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
86041      sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
86042      for(i=0; i<nCol; i++){
86043        sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
86044      }
86045
86046      /* If the parent table is the same as the child table, and we are about
86047      ** to increment the constraint-counter (i.e. this is an INSERT operation),
86048      ** then check if the row being inserted matches itself. If so, do not
86049      ** increment the constraint-counter.
86050      **
86051      ** If any of the parent-key values are NULL, then the row cannot match
86052      ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
86053      ** of the parent-key values are NULL (at this point it is known that
86054      ** none of the child key values are).
86055      */
86056      if( pTab==pFKey->pFrom && nIncr==1 ){
86057        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
86058        for(i=0; i<nCol; i++){
86059          int iChild = aiCol[i]+1+regData;
86060          int iParent = pIdx->aiColumn[i]+1+regData;
86061          assert( aiCol[i]!=pTab->iPKey );
86062          if( pIdx->aiColumn[i]==pTab->iPKey ){
86063            /* The parent key is a composite key that includes the IPK column */
86064            iParent = regData;
86065          }
86066          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
86067          sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
86068        }
86069        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
86070      }
86071
86072      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
86073      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
86074      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
86075
86076      sqlite3ReleaseTempReg(pParse, regRec);
86077      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
86078    }
86079  }
86080
86081  if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
86082    /* Special case: If this is an INSERT statement that will insert exactly
86083    ** one row into the table, raise a constraint immediately instead of
86084    ** incrementing a counter. This is necessary as the VM code is being
86085    ** generated for will not open a statement transaction.  */
86086    assert( nIncr==1 );
86087    sqlite3HaltConstraint(
86088        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
86089    );
86090  }else{
86091    if( nIncr>0 && pFKey->isDeferred==0 ){
86092      sqlite3ParseToplevel(pParse)->mayAbort = 1;
86093    }
86094    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
86095  }
86096
86097  sqlite3VdbeResolveLabel(v, iOk);
86098  sqlite3VdbeAddOp1(v, OP_Close, iCur);
86099}
86100
86101/*
86102** This function is called to generate code executed when a row is deleted
86103** from the parent table of foreign key constraint pFKey and, if pFKey is
86104** deferred, when a row is inserted into the same table. When generating
86105** code for an SQL UPDATE operation, this function may be called twice -
86106** once to "delete" the old row and once to "insert" the new row.
86107**
86108** The code generated by this function scans through the rows in the child
86109** table that correspond to the parent table row being deleted or inserted.
86110** For each child row found, one of the following actions is taken:
86111**
86112**   Operation | FK type   | Action taken
86113**   --------------------------------------------------------------------------
86114**   DELETE      immediate   Increment the "immediate constraint counter".
86115**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
86116**                           throw a "foreign key constraint failed" exception.
86117**
86118**   INSERT      immediate   Decrement the "immediate constraint counter".
86119**
86120**   DELETE      deferred    Increment the "deferred constraint counter".
86121**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
86122**                           throw a "foreign key constraint failed" exception.
86123**
86124**   INSERT      deferred    Decrement the "deferred constraint counter".
86125**
86126** These operations are identified in the comment at the top of this file
86127** (fkey.c) as "I.2" and "D.2".
86128*/
86129static void fkScanChildren(
86130  Parse *pParse,                  /* Parse context */
86131  SrcList *pSrc,                  /* SrcList containing the table to scan */
86132  Table *pTab,
86133  Index *pIdx,                    /* Foreign key index */
86134  FKey *pFKey,                    /* Foreign key relationship */
86135  int *aiCol,                     /* Map from pIdx cols to child table cols */
86136  int regData,                    /* Referenced table data starts here */
86137  int nIncr                       /* Amount to increment deferred counter by */
86138){
86139  sqlite3 *db = pParse->db;       /* Database handle */
86140  int i;                          /* Iterator variable */
86141  Expr *pWhere = 0;               /* WHERE clause to scan with */
86142  NameContext sNameContext;       /* Context used to resolve WHERE clause */
86143  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
86144  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
86145  Vdbe *v = sqlite3GetVdbe(pParse);
86146
86147  assert( !pIdx || pIdx->pTable==pTab );
86148
86149  if( nIncr<0 ){
86150    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
86151  }
86152
86153  /* Create an Expr object representing an SQL expression like:
86154  **
86155  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
86156  **
86157  ** The collation sequence used for the comparison should be that of
86158  ** the parent key columns. The affinity of the parent key column should
86159  ** be applied to each child key value before the comparison takes place.
86160  */
86161  for(i=0; i<pFKey->nCol; i++){
86162    Expr *pLeft;                  /* Value from parent table row */
86163    Expr *pRight;                 /* Column ref to child table */
86164    Expr *pEq;                    /* Expression (pLeft = pRight) */
86165    int iCol;                     /* Index of column in child table */
86166    const char *zCol;             /* Name of column in child table */
86167
86168    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
86169    if( pLeft ){
86170      /* Set the collation sequence and affinity of the LHS of each TK_EQ
86171      ** expression to the parent key column defaults.  */
86172      if( pIdx ){
86173        Column *pCol;
86174        iCol = pIdx->aiColumn[i];
86175        pCol = &pTab->aCol[iCol];
86176        if( pTab->iPKey==iCol ) iCol = -1;
86177        pLeft->iTable = regData+iCol+1;
86178        pLeft->affinity = pCol->affinity;
86179        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
86180      }else{
86181        pLeft->iTable = regData;
86182        pLeft->affinity = SQLITE_AFF_INTEGER;
86183      }
86184    }
86185    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
86186    assert( iCol>=0 );
86187    zCol = pFKey->pFrom->aCol[iCol].zName;
86188    pRight = sqlite3Expr(db, TK_ID, zCol);
86189    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
86190    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
86191  }
86192
86193  /* If the child table is the same as the parent table, and this scan
86194  ** is taking place as part of a DELETE operation (operation D.2), omit the
86195  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE
86196  ** clause, where $rowid is the rowid of the row being deleted.  */
86197  if( pTab==pFKey->pFrom && nIncr>0 ){
86198    Expr *pEq;                    /* Expression (pLeft = pRight) */
86199    Expr *pLeft;                  /* Value from parent table row */
86200    Expr *pRight;                 /* Column ref to child table */
86201    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
86202    pRight = sqlite3Expr(db, TK_COLUMN, 0);
86203    if( pLeft && pRight ){
86204      pLeft->iTable = regData;
86205      pLeft->affinity = SQLITE_AFF_INTEGER;
86206      pRight->iTable = pSrc->a[0].iCursor;
86207      pRight->iColumn = -1;
86208    }
86209    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
86210    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
86211  }
86212
86213  /* Resolve the references in the WHERE clause. */
86214  memset(&sNameContext, 0, sizeof(NameContext));
86215  sNameContext.pSrcList = pSrc;
86216  sNameContext.pParse = pParse;
86217  sqlite3ResolveExprNames(&sNameContext, pWhere);
86218
86219  /* Create VDBE to loop through the entries in pSrc that match the WHERE
86220  ** clause. If the constraint is not deferred, throw an exception for
86221  ** each row found. Otherwise, for deferred constraints, increment the
86222  ** deferred constraint counter by nIncr for each row selected.  */
86223  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0);
86224  if( nIncr>0 && pFKey->isDeferred==0 ){
86225    sqlite3ParseToplevel(pParse)->mayAbort = 1;
86226  }
86227  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
86228  if( pWInfo ){
86229    sqlite3WhereEnd(pWInfo);
86230  }
86231
86232  /* Clean up the WHERE clause constructed above. */
86233  sqlite3ExprDelete(db, pWhere);
86234  if( iFkIfZero ){
86235    sqlite3VdbeJumpHere(v, iFkIfZero);
86236  }
86237}
86238
86239/*
86240** This function returns a pointer to the head of a linked list of FK
86241** constraints for which table pTab is the parent table. For example,
86242** given the following schema:
86243**
86244**   CREATE TABLE t1(a PRIMARY KEY);
86245**   CREATE TABLE t2(b REFERENCES t1(a);
86246**
86247** Calling this function with table "t1" as an argument returns a pointer
86248** to the FKey structure representing the foreign key constraint on table
86249** "t2". Calling this function with "t2" as the argument would return a
86250** NULL pointer (as there are no FK constraints for which t2 is the parent
86251** table).
86252*/
86253SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
86254  int nName = sqlite3Strlen30(pTab->zName);
86255  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
86256}
86257
86258/*
86259** The second argument is a Trigger structure allocated by the
86260** fkActionTrigger() routine. This function deletes the Trigger structure
86261** and all of its sub-components.
86262**
86263** The Trigger structure or any of its sub-components may be allocated from
86264** the lookaside buffer belonging to database handle dbMem.
86265*/
86266static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
86267  if( p ){
86268    TriggerStep *pStep = p->step_list;
86269    sqlite3ExprDelete(dbMem, pStep->pWhere);
86270    sqlite3ExprListDelete(dbMem, pStep->pExprList);
86271    sqlite3SelectDelete(dbMem, pStep->pSelect);
86272    sqlite3ExprDelete(dbMem, p->pWhen);
86273    sqlite3DbFree(dbMem, p);
86274  }
86275}
86276
86277/*
86278** This function is called to generate code that runs when table pTab is
86279** being dropped from the database. The SrcList passed as the second argument
86280** to this function contains a single entry guaranteed to resolve to
86281** table pTab.
86282**
86283** Normally, no code is required. However, if either
86284**
86285**   (a) The table is the parent table of a FK constraint, or
86286**   (b) The table is the child table of a deferred FK constraint and it is
86287**       determined at runtime that there are outstanding deferred FK
86288**       constraint violations in the database,
86289**
86290** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
86291** the table from the database. Triggers are disabled while running this
86292** DELETE, but foreign key actions are not.
86293*/
86294SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
86295  sqlite3 *db = pParse->db;
86296  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
86297    int iSkip = 0;
86298    Vdbe *v = sqlite3GetVdbe(pParse);
86299
86300    assert( v );                  /* VDBE has already been allocated */
86301    if( sqlite3FkReferences(pTab)==0 ){
86302      /* Search for a deferred foreign key constraint for which this table
86303      ** is the child table. If one cannot be found, return without
86304      ** generating any VDBE code. If one can be found, then jump over
86305      ** the entire DELETE if there are no outstanding deferred constraints
86306      ** when this statement is run.  */
86307      FKey *p;
86308      for(p=pTab->pFKey; p; p=p->pNextFrom){
86309        if( p->isDeferred ) break;
86310      }
86311      if( !p ) return;
86312      iSkip = sqlite3VdbeMakeLabel(v);
86313      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
86314    }
86315
86316    pParse->disableTriggers = 1;
86317    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
86318    pParse->disableTriggers = 0;
86319
86320    /* If the DELETE has generated immediate foreign key constraint
86321    ** violations, halt the VDBE and return an error at this point, before
86322    ** any modifications to the schema are made. This is because statement
86323    ** transactions are not able to rollback schema changes.  */
86324    sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
86325    sqlite3HaltConstraint(
86326        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
86327    );
86328
86329    if( iSkip ){
86330      sqlite3VdbeResolveLabel(v, iSkip);
86331    }
86332  }
86333}
86334
86335/*
86336** This function is called when inserting, deleting or updating a row of
86337** table pTab to generate VDBE code to perform foreign key constraint
86338** processing for the operation.
86339**
86340** For a DELETE operation, parameter regOld is passed the index of the
86341** first register in an array of (pTab->nCol+1) registers containing the
86342** rowid of the row being deleted, followed by each of the column values
86343** of the row being deleted, from left to right. Parameter regNew is passed
86344** zero in this case.
86345**
86346** For an INSERT operation, regOld is passed zero and regNew is passed the
86347** first register of an array of (pTab->nCol+1) registers containing the new
86348** row data.
86349**
86350** For an UPDATE operation, this function is called twice. Once before
86351** the original record is deleted from the table using the calling convention
86352** described for DELETE. Then again after the original record is deleted
86353** but before the new record is inserted using the INSERT convention.
86354*/
86355SQLITE_PRIVATE void sqlite3FkCheck(
86356  Parse *pParse,                  /* Parse context */
86357  Table *pTab,                    /* Row is being deleted from this table */
86358  int regOld,                     /* Previous row data is stored here */
86359  int regNew                      /* New row data is stored here */
86360){
86361  sqlite3 *db = pParse->db;       /* Database handle */
86362  FKey *pFKey;                    /* Used to iterate through FKs */
86363  int iDb;                        /* Index of database containing pTab */
86364  const char *zDb;                /* Name of database containing pTab */
86365  int isIgnoreErrors = pParse->disableTriggers;
86366
86367  /* Exactly one of regOld and regNew should be non-zero. */
86368  assert( (regOld==0)!=(regNew==0) );
86369
86370  /* If foreign-keys are disabled, this function is a no-op. */
86371  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
86372
86373  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
86374  zDb = db->aDb[iDb].zName;
86375
86376  /* Loop through all the foreign key constraints for which pTab is the
86377  ** child table (the table that the foreign key definition is part of).  */
86378  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
86379    Table *pTo;                   /* Parent table of foreign key pFKey */
86380    Index *pIdx = 0;              /* Index on key columns in pTo */
86381    int *aiFree = 0;
86382    int *aiCol;
86383    int iCol;
86384    int i;
86385    int isIgnore = 0;
86386
86387    /* Find the parent table of this foreign key. Also find a unique index
86388    ** on the parent key columns in the parent table. If either of these
86389    ** schema items cannot be located, set an error in pParse and return
86390    ** early.  */
86391    if( pParse->disableTriggers ){
86392      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
86393    }else{
86394      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
86395    }
86396    if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
86397      assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
86398      if( !isIgnoreErrors || db->mallocFailed ) return;
86399      if( pTo==0 ){
86400        /* If isIgnoreErrors is true, then a table is being dropped. In this
86401        ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
86402        ** before actually dropping it in order to check FK constraints.
86403        ** If the parent table of an FK constraint on the current table is
86404        ** missing, behave as if it is empty. i.e. decrement the relevant
86405        ** FK counter for each row of the current table with non-NULL keys.
86406        */
86407        Vdbe *v = sqlite3GetVdbe(pParse);
86408        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
86409        for(i=0; i<pFKey->nCol; i++){
86410          int iReg = pFKey->aCol[i].iFrom + regOld + 1;
86411          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
86412        }
86413        sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
86414      }
86415      continue;
86416    }
86417    assert( pFKey->nCol==1 || (aiFree && pIdx) );
86418
86419    if( aiFree ){
86420      aiCol = aiFree;
86421    }else{
86422      iCol = pFKey->aCol[0].iFrom;
86423      aiCol = &iCol;
86424    }
86425    for(i=0; i<pFKey->nCol; i++){
86426      if( aiCol[i]==pTab->iPKey ){
86427        aiCol[i] = -1;
86428      }
86429#ifndef SQLITE_OMIT_AUTHORIZATION
86430      /* Request permission to read the parent key columns. If the
86431      ** authorization callback returns SQLITE_IGNORE, behave as if any
86432      ** values read from the parent table are NULL. */
86433      if( db->xAuth ){
86434        int rcauth;
86435        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
86436        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
86437        isIgnore = (rcauth==SQLITE_IGNORE);
86438      }
86439#endif
86440    }
86441
86442    /* Take a shared-cache advisory read-lock on the parent table. Allocate
86443    ** a cursor to use to search the unique index on the parent key columns
86444    ** in the parent table.  */
86445    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
86446    pParse->nTab++;
86447
86448    if( regOld!=0 ){
86449      /* A row is being removed from the child table. Search for the parent.
86450      ** If the parent does not exist, removing the child row resolves an
86451      ** outstanding foreign key constraint violation. */
86452      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
86453    }
86454    if( regNew!=0 ){
86455      /* A row is being added to the child table. If a parent row cannot
86456      ** be found, adding the child row has violated the FK constraint. */
86457      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
86458    }
86459
86460    sqlite3DbFree(db, aiFree);
86461  }
86462
86463  /* Loop through all the foreign key constraints that refer to this table */
86464  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
86465    Index *pIdx = 0;              /* Foreign key index for pFKey */
86466    SrcList *pSrc;
86467    int *aiCol = 0;
86468
86469    if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
86470      assert( regOld==0 && regNew!=0 );
86471      /* Inserting a single row into a parent table cannot cause an immediate
86472      ** foreign key violation. So do nothing in this case.  */
86473      continue;
86474    }
86475
86476    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
86477      if( !isIgnoreErrors || db->mallocFailed ) return;
86478      continue;
86479    }
86480    assert( aiCol || pFKey->nCol==1 );
86481
86482    /* Create a SrcList structure containing a single table (the table
86483    ** the foreign key that refers to this table is attached to). This
86484    ** is required for the sqlite3WhereXXX() interface.  */
86485    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
86486    if( pSrc ){
86487      struct SrcList_item *pItem = pSrc->a;
86488      pItem->pTab = pFKey->pFrom;
86489      pItem->zName = pFKey->pFrom->zName;
86490      pItem->pTab->nRef++;
86491      pItem->iCursor = pParse->nTab++;
86492
86493      if( regNew!=0 ){
86494        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
86495      }
86496      if( regOld!=0 ){
86497        /* If there is a RESTRICT action configured for the current operation
86498        ** on the parent table of this FK, then throw an exception
86499        ** immediately if the FK constraint is violated, even if this is a
86500        ** deferred trigger. That's what RESTRICT means. To defer checking
86501        ** the constraint, the FK should specify NO ACTION (represented
86502        ** using OE_None). NO ACTION is the default.  */
86503        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
86504      }
86505      pItem->zName = 0;
86506      sqlite3SrcListDelete(db, pSrc);
86507    }
86508    sqlite3DbFree(db, aiCol);
86509  }
86510}
86511
86512#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
86513
86514/*
86515** This function is called before generating code to update or delete a
86516** row contained in table pTab.
86517*/
86518SQLITE_PRIVATE u32 sqlite3FkOldmask(
86519  Parse *pParse,                  /* Parse context */
86520  Table *pTab                     /* Table being modified */
86521){
86522  u32 mask = 0;
86523  if( pParse->db->flags&SQLITE_ForeignKeys ){
86524    FKey *p;
86525    int i;
86526    for(p=pTab->pFKey; p; p=p->pNextFrom){
86527      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
86528    }
86529    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
86530      Index *pIdx = 0;
86531      locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
86532      if( pIdx ){
86533        for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
86534      }
86535    }
86536  }
86537  return mask;
86538}
86539
86540/*
86541** This function is called before generating code to update or delete a
86542** row contained in table pTab. If the operation is a DELETE, then
86543** parameter aChange is passed a NULL value. For an UPDATE, aChange points
86544** to an array of size N, where N is the number of columns in table pTab.
86545** If the i'th column is not modified by the UPDATE, then the corresponding
86546** entry in the aChange[] array is set to -1. If the column is modified,
86547** the value is 0 or greater. Parameter chngRowid is set to true if the
86548** UPDATE statement modifies the rowid fields of the table.
86549**
86550** If any foreign key processing will be required, this function returns
86551** true. If there is no foreign key related processing, this function
86552** returns false.
86553*/
86554SQLITE_PRIVATE int sqlite3FkRequired(
86555  Parse *pParse,                  /* Parse context */
86556  Table *pTab,                    /* Table being modified */
86557  int *aChange,                   /* Non-NULL for UPDATE operations */
86558  int chngRowid                   /* True for UPDATE that affects rowid */
86559){
86560  if( pParse->db->flags&SQLITE_ForeignKeys ){
86561    if( !aChange ){
86562      /* A DELETE operation. Foreign key processing is required if the
86563      ** table in question is either the child or parent table for any
86564      ** foreign key constraint.  */
86565      return (sqlite3FkReferences(pTab) || pTab->pFKey);
86566    }else{
86567      /* This is an UPDATE. Foreign key processing is only required if the
86568      ** operation modifies one or more child or parent key columns. */
86569      int i;
86570      FKey *p;
86571
86572      /* Check if any child key columns are being modified. */
86573      for(p=pTab->pFKey; p; p=p->pNextFrom){
86574        for(i=0; i<p->nCol; i++){
86575          int iChildKey = p->aCol[i].iFrom;
86576          if( aChange[iChildKey]>=0 ) return 1;
86577          if( iChildKey==pTab->iPKey && chngRowid ) return 1;
86578        }
86579      }
86580
86581      /* Check if any parent key columns are being modified. */
86582      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
86583        for(i=0; i<p->nCol; i++){
86584          char *zKey = p->aCol[i].zCol;
86585          int iKey;
86586          for(iKey=0; iKey<pTab->nCol; iKey++){
86587            Column *pCol = &pTab->aCol[iKey];
86588            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
86589              if( aChange[iKey]>=0 ) return 1;
86590              if( iKey==pTab->iPKey && chngRowid ) return 1;
86591            }
86592          }
86593        }
86594      }
86595    }
86596  }
86597  return 0;
86598}
86599
86600/*
86601** This function is called when an UPDATE or DELETE operation is being
86602** compiled on table pTab, which is the parent table of foreign-key pFKey.
86603** If the current operation is an UPDATE, then the pChanges parameter is
86604** passed a pointer to the list of columns being modified. If it is a
86605** DELETE, pChanges is passed a NULL pointer.
86606**
86607** It returns a pointer to a Trigger structure containing a trigger
86608** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
86609** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
86610** returned (these actions require no special handling by the triggers
86611** sub-system, code for them is created by fkScanChildren()).
86612**
86613** For example, if pFKey is the foreign key and pTab is table "p" in
86614** the following schema:
86615**
86616**   CREATE TABLE p(pk PRIMARY KEY);
86617**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
86618**
86619** then the returned trigger structure is equivalent to:
86620**
86621**   CREATE TRIGGER ... DELETE ON p BEGIN
86622**     DELETE FROM c WHERE ck = old.pk;
86623**   END;
86624**
86625** The returned pointer is cached as part of the foreign key object. It
86626** is eventually freed along with the rest of the foreign key object by
86627** sqlite3FkDelete().
86628*/
86629static Trigger *fkActionTrigger(
86630  Parse *pParse,                  /* Parse context */
86631  Table *pTab,                    /* Table being updated or deleted from */
86632  FKey *pFKey,                    /* Foreign key to get action for */
86633  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
86634){
86635  sqlite3 *db = pParse->db;       /* Database handle */
86636  int action;                     /* One of OE_None, OE_Cascade etc. */
86637  Trigger *pTrigger;              /* Trigger definition to return */
86638  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
86639
86640  action = pFKey->aAction[iAction];
86641  pTrigger = pFKey->apTrigger[iAction];
86642
86643  if( action!=OE_None && !pTrigger ){
86644    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
86645    char const *zFrom;            /* Name of child table */
86646    int nFrom;                    /* Length in bytes of zFrom */
86647    Index *pIdx = 0;              /* Parent key index for this FK */
86648    int *aiCol = 0;               /* child table cols -> parent key cols */
86649    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
86650    Expr *pWhere = 0;             /* WHERE clause of trigger step */
86651    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
86652    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
86653    int i;                        /* Iterator variable */
86654    Expr *pWhen = 0;              /* WHEN clause for the trigger */
86655
86656    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
86657    assert( aiCol || pFKey->nCol==1 );
86658
86659    for(i=0; i<pFKey->nCol; i++){
86660      Token tOld = { "old", 3 };  /* Literal "old" token */
86661      Token tNew = { "new", 3 };  /* Literal "new" token */
86662      Token tFromCol;             /* Name of column in child table */
86663      Token tToCol;               /* Name of column in parent table */
86664      int iFromCol;               /* Idx of column in child table */
86665      Expr *pEq;                  /* tFromCol = OLD.tToCol */
86666
86667      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
86668      assert( iFromCol>=0 );
86669      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
86670      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
86671
86672      tToCol.n = sqlite3Strlen30(tToCol.z);
86673      tFromCol.n = sqlite3Strlen30(tFromCol.z);
86674
86675      /* Create the expression "OLD.zToCol = zFromCol". It is important
86676      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
86677      ** that the affinity and collation sequence associated with the
86678      ** parent table are used for the comparison. */
86679      pEq = sqlite3PExpr(pParse, TK_EQ,
86680          sqlite3PExpr(pParse, TK_DOT,
86681            sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
86682            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
86683          , 0),
86684          sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
86685      , 0);
86686      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
86687
86688      /* For ON UPDATE, construct the next term of the WHEN clause.
86689      ** The final WHEN clause will be like this:
86690      **
86691      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
86692      */
86693      if( pChanges ){
86694        pEq = sqlite3PExpr(pParse, TK_IS,
86695            sqlite3PExpr(pParse, TK_DOT,
86696              sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
86697              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
86698              0),
86699            sqlite3PExpr(pParse, TK_DOT,
86700              sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
86701              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
86702              0),
86703            0);
86704        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
86705      }
86706
86707      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
86708        Expr *pNew;
86709        if( action==OE_Cascade ){
86710          pNew = sqlite3PExpr(pParse, TK_DOT,
86711            sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
86712            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
86713          , 0);
86714        }else if( action==OE_SetDflt ){
86715          Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
86716          if( pDflt ){
86717            pNew = sqlite3ExprDup(db, pDflt, 0);
86718          }else{
86719            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
86720          }
86721        }else{
86722          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
86723        }
86724        pList = sqlite3ExprListAppend(pParse, pList, pNew);
86725        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
86726      }
86727    }
86728    sqlite3DbFree(db, aiCol);
86729
86730    zFrom = pFKey->pFrom->zName;
86731    nFrom = sqlite3Strlen30(zFrom);
86732
86733    if( action==OE_Restrict ){
86734      Token tFrom;
86735      Expr *pRaise;
86736
86737      tFrom.z = zFrom;
86738      tFrom.n = nFrom;
86739      pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
86740      if( pRaise ){
86741        pRaise->affinity = OE_Abort;
86742      }
86743      pSelect = sqlite3SelectNew(pParse,
86744          sqlite3ExprListAppend(pParse, 0, pRaise),
86745          sqlite3SrcListAppend(db, 0, &tFrom, 0),
86746          pWhere,
86747          0, 0, 0, 0, 0, 0
86748      );
86749      pWhere = 0;
86750    }
86751
86752    /* Disable lookaside memory allocation */
86753    enableLookaside = db->lookaside.bEnabled;
86754    db->lookaside.bEnabled = 0;
86755
86756    pTrigger = (Trigger *)sqlite3DbMallocZero(db,
86757        sizeof(Trigger) +         /* struct Trigger */
86758        sizeof(TriggerStep) +     /* Single step in trigger program */
86759        nFrom + 1                 /* Space for pStep->target.z */
86760    );
86761    if( pTrigger ){
86762      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
86763      pStep->target.z = (char *)&pStep[1];
86764      pStep->target.n = nFrom;
86765      memcpy((char *)pStep->target.z, zFrom, nFrom);
86766
86767      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
86768      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
86769      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
86770      if( pWhen ){
86771        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
86772        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
86773      }
86774    }
86775
86776    /* Re-enable the lookaside buffer, if it was disabled earlier. */
86777    db->lookaside.bEnabled = enableLookaside;
86778
86779    sqlite3ExprDelete(db, pWhere);
86780    sqlite3ExprDelete(db, pWhen);
86781    sqlite3ExprListDelete(db, pList);
86782    sqlite3SelectDelete(db, pSelect);
86783    if( db->mallocFailed==1 ){
86784      fkTriggerDelete(db, pTrigger);
86785      return 0;
86786    }
86787
86788    switch( action ){
86789      case OE_Restrict:
86790        pStep->op = TK_SELECT;
86791        break;
86792      case OE_Cascade:
86793        if( !pChanges ){
86794          pStep->op = TK_DELETE;
86795          break;
86796        }
86797      default:
86798        pStep->op = TK_UPDATE;
86799    }
86800    pStep->pTrig = pTrigger;
86801    pTrigger->pSchema = pTab->pSchema;
86802    pTrigger->pTabSchema = pTab->pSchema;
86803    pFKey->apTrigger[iAction] = pTrigger;
86804    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
86805  }
86806
86807  return pTrigger;
86808}
86809
86810/*
86811** This function is called when deleting or updating a row to implement
86812** any required CASCADE, SET NULL or SET DEFAULT actions.
86813*/
86814SQLITE_PRIVATE void sqlite3FkActions(
86815  Parse *pParse,                  /* Parse context */
86816  Table *pTab,                    /* Table being updated or deleted from */
86817  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
86818  int regOld                      /* Address of array containing old row */
86819){
86820  /* If foreign-key support is enabled, iterate through all FKs that
86821  ** refer to table pTab. If there is an action associated with the FK
86822  ** for this operation (either update or delete), invoke the associated
86823  ** trigger sub-program.  */
86824  if( pParse->db->flags&SQLITE_ForeignKeys ){
86825    FKey *pFKey;                  /* Iterator variable */
86826    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
86827      Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
86828      if( pAction ){
86829        sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
86830      }
86831    }
86832  }
86833}
86834
86835#endif /* ifndef SQLITE_OMIT_TRIGGER */
86836
86837/*
86838** Free all memory associated with foreign key definitions attached to
86839** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
86840** hash table.
86841*/
86842SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
86843  FKey *pFKey;                    /* Iterator variable */
86844  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
86845
86846  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
86847  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
86848
86849    /* Remove the FK from the fkeyHash hash table. */
86850    if( !db || db->pnBytesFreed==0 ){
86851      if( pFKey->pPrevTo ){
86852        pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
86853      }else{
86854        void *p = (void *)pFKey->pNextTo;
86855        const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
86856        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
86857      }
86858      if( pFKey->pNextTo ){
86859        pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
86860      }
86861    }
86862
86863    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
86864    ** classified as either immediate or deferred.
86865    */
86866    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
86867
86868    /* Delete any triggers created to implement actions for this FK. */
86869#ifndef SQLITE_OMIT_TRIGGER
86870    fkTriggerDelete(db, pFKey->apTrigger[0]);
86871    fkTriggerDelete(db, pFKey->apTrigger[1]);
86872#endif
86873
86874    pNext = pFKey->pNextFrom;
86875    sqlite3DbFree(db, pFKey);
86876  }
86877}
86878#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
86879
86880/************** End of fkey.c ************************************************/
86881/************** Begin file insert.c ******************************************/
86882/*
86883** 2001 September 15
86884**
86885** The author disclaims copyright to this source code.  In place of
86886** a legal notice, here is a blessing:
86887**
86888**    May you do good and not evil.
86889**    May you find forgiveness for yourself and forgive others.
86890**    May you share freely, never taking more than you give.
86891**
86892*************************************************************************
86893** This file contains C code routines that are called by the parser
86894** to handle INSERT statements in SQLite.
86895*/
86896
86897/*
86898** Generate code that will open a table for reading.
86899*/
86900SQLITE_PRIVATE void sqlite3OpenTable(
86901  Parse *p,       /* Generate code into this VDBE */
86902  int iCur,       /* The cursor number of the table */
86903  int iDb,        /* The database index in sqlite3.aDb[] */
86904  Table *pTab,    /* The table to be opened */
86905  int opcode      /* OP_OpenRead or OP_OpenWrite */
86906){
86907  Vdbe *v;
86908  if( IsVirtual(pTab) ) return;
86909  v = sqlite3GetVdbe(p);
86910  assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
86911  sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
86912  sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
86913  sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
86914  VdbeComment((v, "%s", pTab->zName));
86915}
86916
86917/*
86918** Return a pointer to the column affinity string associated with index
86919** pIdx. A column affinity string has one character for each column in
86920** the table, according to the affinity of the column:
86921**
86922**  Character      Column affinity
86923**  ------------------------------
86924**  'a'            TEXT
86925**  'b'            NONE
86926**  'c'            NUMERIC
86927**  'd'            INTEGER
86928**  'e'            REAL
86929**
86930** An extra 'b' is appended to the end of the string to cover the
86931** rowid that appears as the last column in every index.
86932**
86933** Memory for the buffer containing the column index affinity string
86934** is managed along with the rest of the Index structure. It will be
86935** released when sqlite3DeleteIndex() is called.
86936*/
86937SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
86938  if( !pIdx->zColAff ){
86939    /* The first time a column affinity string for a particular index is
86940    ** required, it is allocated and populated here. It is then stored as
86941    ** a member of the Index structure for subsequent use.
86942    **
86943    ** The column affinity string will eventually be deleted by
86944    ** sqliteDeleteIndex() when the Index structure itself is cleaned
86945    ** up.
86946    */
86947    int n;
86948    Table *pTab = pIdx->pTable;
86949    sqlite3 *db = sqlite3VdbeDb(v);
86950    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
86951    if( !pIdx->zColAff ){
86952      db->mallocFailed = 1;
86953      return 0;
86954    }
86955    for(n=0; n<pIdx->nColumn; n++){
86956      pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
86957    }
86958    pIdx->zColAff[n++] = SQLITE_AFF_NONE;
86959    pIdx->zColAff[n] = 0;
86960  }
86961
86962  return pIdx->zColAff;
86963}
86964
86965/*
86966** Set P4 of the most recently inserted opcode to a column affinity
86967** string for table pTab. A column affinity string has one character
86968** for each column indexed by the index, according to the affinity of the
86969** column:
86970**
86971**  Character      Column affinity
86972**  ------------------------------
86973**  'a'            TEXT
86974**  'b'            NONE
86975**  'c'            NUMERIC
86976**  'd'            INTEGER
86977**  'e'            REAL
86978*/
86979SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
86980  /* The first time a column affinity string for a particular table
86981  ** is required, it is allocated and populated here. It is then
86982  ** stored as a member of the Table structure for subsequent use.
86983  **
86984  ** The column affinity string will eventually be deleted by
86985  ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
86986  */
86987  if( !pTab->zColAff ){
86988    char *zColAff;
86989    int i;
86990    sqlite3 *db = sqlite3VdbeDb(v);
86991
86992    zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
86993    if( !zColAff ){
86994      db->mallocFailed = 1;
86995      return;
86996    }
86997
86998    for(i=0; i<pTab->nCol; i++){
86999      zColAff[i] = pTab->aCol[i].affinity;
87000    }
87001    zColAff[pTab->nCol] = '\0';
87002
87003    pTab->zColAff = zColAff;
87004  }
87005
87006  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
87007}
87008
87009/*
87010** Return non-zero if the table pTab in database iDb or any of its indices
87011** have been opened at any point in the VDBE program beginning at location
87012** iStartAddr throught the end of the program.  This is used to see if
87013** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
87014** run without using temporary table for the results of the SELECT.
87015*/
87016static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
87017  Vdbe *v = sqlite3GetVdbe(p);
87018  int i;
87019  int iEnd = sqlite3VdbeCurrentAddr(v);
87020#ifndef SQLITE_OMIT_VIRTUALTABLE
87021  VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
87022#endif
87023
87024  for(i=iStartAddr; i<iEnd; i++){
87025    VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
87026    assert( pOp!=0 );
87027    if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
87028      Index *pIndex;
87029      int tnum = pOp->p2;
87030      if( tnum==pTab->tnum ){
87031        return 1;
87032      }
87033      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
87034        if( tnum==pIndex->tnum ){
87035          return 1;
87036        }
87037      }
87038    }
87039#ifndef SQLITE_OMIT_VIRTUALTABLE
87040    if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
87041      assert( pOp->p4.pVtab!=0 );
87042      assert( pOp->p4type==P4_VTAB );
87043      return 1;
87044    }
87045#endif
87046  }
87047  return 0;
87048}
87049
87050#ifndef SQLITE_OMIT_AUTOINCREMENT
87051/*
87052** Locate or create an AutoincInfo structure associated with table pTab
87053** which is in database iDb.  Return the register number for the register
87054** that holds the maximum rowid.
87055**
87056** There is at most one AutoincInfo structure per table even if the
87057** same table is autoincremented multiple times due to inserts within
87058** triggers.  A new AutoincInfo structure is created if this is the
87059** first use of table pTab.  On 2nd and subsequent uses, the original
87060** AutoincInfo structure is used.
87061**
87062** Three memory locations are allocated:
87063**
87064**   (1)  Register to hold the name of the pTab table.
87065**   (2)  Register to hold the maximum ROWID of pTab.
87066**   (3)  Register to hold the rowid in sqlite_sequence of pTab
87067**
87068** The 2nd register is the one that is returned.  That is all the
87069** insert routine needs to know about.
87070*/
87071static int autoIncBegin(
87072  Parse *pParse,      /* Parsing context */
87073  int iDb,            /* Index of the database holding pTab */
87074  Table *pTab         /* The table we are writing to */
87075){
87076  int memId = 0;      /* Register holding maximum rowid */
87077  if( pTab->tabFlags & TF_Autoincrement ){
87078    Parse *pToplevel = sqlite3ParseToplevel(pParse);
87079    AutoincInfo *pInfo;
87080
87081    pInfo = pToplevel->pAinc;
87082    while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
87083    if( pInfo==0 ){
87084      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
87085      if( pInfo==0 ) return 0;
87086      pInfo->pNext = pToplevel->pAinc;
87087      pToplevel->pAinc = pInfo;
87088      pInfo->pTab = pTab;
87089      pInfo->iDb = iDb;
87090      pToplevel->nMem++;                  /* Register to hold name of table */
87091      pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
87092      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
87093    }
87094    memId = pInfo->regCtr;
87095  }
87096  return memId;
87097}
87098
87099/*
87100** This routine generates code that will initialize all of the
87101** register used by the autoincrement tracker.
87102*/
87103SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
87104  AutoincInfo *p;            /* Information about an AUTOINCREMENT */
87105  sqlite3 *db = pParse->db;  /* The database connection */
87106  Db *pDb;                   /* Database only autoinc table */
87107  int memId;                 /* Register holding max rowid */
87108  int addr;                  /* A VDBE address */
87109  Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
87110
87111  /* This routine is never called during trigger-generation.  It is
87112  ** only called from the top-level */
87113  assert( pParse->pTriggerTab==0 );
87114  assert( pParse==sqlite3ParseToplevel(pParse) );
87115
87116  assert( v );   /* We failed long ago if this is not so */
87117  for(p = pParse->pAinc; p; p = p->pNext){
87118    pDb = &db->aDb[p->iDb];
87119    memId = p->regCtr;
87120    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
87121    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
87122    addr = sqlite3VdbeCurrentAddr(v);
87123    sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
87124    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
87125    sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
87126    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
87127    sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
87128    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
87129    sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
87130    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
87131    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
87132    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
87133    sqlite3VdbeAddOp0(v, OP_Close);
87134  }
87135}
87136
87137/*
87138** Update the maximum rowid for an autoincrement calculation.
87139**
87140** This routine should be called when the top of the stack holds a
87141** new rowid that is about to be inserted.  If that new rowid is
87142** larger than the maximum rowid in the memId memory cell, then the
87143** memory cell is updated.  The stack is unchanged.
87144*/
87145static void autoIncStep(Parse *pParse, int memId, int regRowid){
87146  if( memId>0 ){
87147    sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
87148  }
87149}
87150
87151/*
87152** This routine generates the code needed to write autoincrement
87153** maximum rowid values back into the sqlite_sequence register.
87154** Every statement that might do an INSERT into an autoincrement
87155** table (either directly or through triggers) needs to call this
87156** routine just before the "exit" code.
87157*/
87158SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
87159  AutoincInfo *p;
87160  Vdbe *v = pParse->pVdbe;
87161  sqlite3 *db = pParse->db;
87162
87163  assert( v );
87164  for(p = pParse->pAinc; p; p = p->pNext){
87165    Db *pDb = &db->aDb[p->iDb];
87166    int j1, j2, j3, j4, j5;
87167    int iRec;
87168    int memId = p->regCtr;
87169
87170    iRec = sqlite3GetTempReg(pParse);
87171    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
87172    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
87173    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
87174    j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
87175    j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
87176    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
87177    sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
87178    sqlite3VdbeJumpHere(v, j2);
87179    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
87180    j5 = sqlite3VdbeAddOp0(v, OP_Goto);
87181    sqlite3VdbeJumpHere(v, j4);
87182    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
87183    sqlite3VdbeJumpHere(v, j1);
87184    sqlite3VdbeJumpHere(v, j5);
87185    sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
87186    sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
87187    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
87188    sqlite3VdbeAddOp0(v, OP_Close);
87189    sqlite3ReleaseTempReg(pParse, iRec);
87190  }
87191}
87192#else
87193/*
87194** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
87195** above are all no-ops
87196*/
87197# define autoIncBegin(A,B,C) (0)
87198# define autoIncStep(A,B,C)
87199#endif /* SQLITE_OMIT_AUTOINCREMENT */
87200
87201
87202/* Forward declaration */
87203static int xferOptimization(
87204  Parse *pParse,        /* Parser context */
87205  Table *pDest,         /* The table we are inserting into */
87206  Select *pSelect,      /* A SELECT statement to use as the data source */
87207  int onError,          /* How to handle constraint errors */
87208  int iDbDest           /* The database of pDest */
87209);
87210
87211/*
87212** This routine is call to handle SQL of the following forms:
87213**
87214**    insert into TABLE (IDLIST) values(EXPRLIST)
87215**    insert into TABLE (IDLIST) select
87216**
87217** The IDLIST following the table name is always optional.  If omitted,
87218** then a list of all columns for the table is substituted.  The IDLIST
87219** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
87220**
87221** The pList parameter holds EXPRLIST in the first form of the INSERT
87222** statement above, and pSelect is NULL.  For the second form, pList is
87223** NULL and pSelect is a pointer to the select statement used to generate
87224** data for the insert.
87225**
87226** The code generated follows one of four templates.  For a simple
87227** select with data coming from a VALUES clause, the code executes
87228** once straight down through.  Pseudo-code follows (we call this
87229** the "1st template"):
87230**
87231**         open write cursor to <table> and its indices
87232**         puts VALUES clause expressions onto the stack
87233**         write the resulting record into <table>
87234**         cleanup
87235**
87236** The three remaining templates assume the statement is of the form
87237**
87238**   INSERT INTO <table> SELECT ...
87239**
87240** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
87241** in other words if the SELECT pulls all columns from a single table
87242** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
87243** if <table2> and <table1> are distinct tables but have identical
87244** schemas, including all the same indices, then a special optimization
87245** is invoked that copies raw records from <table2> over to <table1>.
87246** See the xferOptimization() function for the implementation of this
87247** template.  This is the 2nd template.
87248**
87249**         open a write cursor to <table>
87250**         open read cursor on <table2>
87251**         transfer all records in <table2> over to <table>
87252**         close cursors
87253**         foreach index on <table>
87254**           open a write cursor on the <table> index
87255**           open a read cursor on the corresponding <table2> index
87256**           transfer all records from the read to the write cursors
87257**           close cursors
87258**         end foreach
87259**
87260** The 3rd template is for when the second template does not apply
87261** and the SELECT clause does not read from <table> at any time.
87262** The generated code follows this template:
87263**
87264**         EOF <- 0
87265**         X <- A
87266**         goto B
87267**      A: setup for the SELECT
87268**         loop over the rows in the SELECT
87269**           load values into registers R..R+n
87270**           yield X
87271**         end loop
87272**         cleanup after the SELECT
87273**         EOF <- 1
87274**         yield X
87275**         goto A
87276**      B: open write cursor to <table> and its indices
87277**      C: yield X
87278**         if EOF goto D
87279**         insert the select result into <table> from R..R+n
87280**         goto C
87281**      D: cleanup
87282**
87283** The 4th template is used if the insert statement takes its
87284** values from a SELECT but the data is being inserted into a table
87285** that is also read as part of the SELECT.  In the third form,
87286** we have to use a intermediate table to store the results of
87287** the select.  The template is like this:
87288**
87289**         EOF <- 0
87290**         X <- A
87291**         goto B
87292**      A: setup for the SELECT
87293**         loop over the tables in the SELECT
87294**           load value into register R..R+n
87295**           yield X
87296**         end loop
87297**         cleanup after the SELECT
87298**         EOF <- 1
87299**         yield X
87300**         halt-error
87301**      B: open temp table
87302**      L: yield X
87303**         if EOF goto M
87304**         insert row from R..R+n into temp table
87305**         goto L
87306**      M: open write cursor to <table> and its indices
87307**         rewind temp table
87308**      C: loop over rows of intermediate table
87309**           transfer values form intermediate table into <table>
87310**         end loop
87311**      D: cleanup
87312*/
87313SQLITE_PRIVATE void sqlite3Insert(
87314  Parse *pParse,        /* Parser context */
87315  SrcList *pTabList,    /* Name of table into which we are inserting */
87316  ExprList *pList,      /* List of values to be inserted */
87317  Select *pSelect,      /* A SELECT statement to use as the data source */
87318  IdList *pColumn,      /* Column names corresponding to IDLIST. */
87319  int onError           /* How to handle constraint errors */
87320){
87321  sqlite3 *db;          /* The main database structure */
87322  Table *pTab;          /* The table to insert into.  aka TABLE */
87323  char *zTab;           /* Name of the table into which we are inserting */
87324  const char *zDb;      /* Name of the database holding this table */
87325  int i, j, idx;        /* Loop counters */
87326  Vdbe *v;              /* Generate code into this virtual machine */
87327  Index *pIdx;          /* For looping over indices of the table */
87328  int nColumn;          /* Number of columns in the data */
87329  int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
87330  int baseCur = 0;      /* VDBE Cursor number for pTab */
87331  int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
87332  int endOfLoop;        /* Label for the end of the insertion loop */
87333  int useTempTable = 0; /* Store SELECT results in intermediate table */
87334  int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
87335  int addrInsTop = 0;   /* Jump to label "D" */
87336  int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
87337  int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
87338  SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
87339  int iDb;              /* Index of database holding TABLE */
87340  Db *pDb;              /* The database containing table being inserted into */
87341  int appendFlag = 0;   /* True if the insert is likely to be an append */
87342
87343  /* Register allocations */
87344  int regFromSelect = 0;/* Base register for data coming from SELECT */
87345  int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
87346  int regRowCount = 0;  /* Memory cell used for the row counter */
87347  int regIns;           /* Block of regs holding rowid+data being inserted */
87348  int regRowid;         /* registers holding insert rowid */
87349  int regData;          /* register holding first column to insert */
87350  int regEof = 0;       /* Register recording end of SELECT data */
87351  int *aRegIdx = 0;     /* One register allocated to each index */
87352
87353#ifndef SQLITE_OMIT_TRIGGER
87354  int isView;                 /* True if attempting to insert into a view */
87355  Trigger *pTrigger;          /* List of triggers on pTab, if required */
87356  int tmask;                  /* Mask of trigger times */
87357#endif
87358
87359  db = pParse->db;
87360  memset(&dest, 0, sizeof(dest));
87361  if( pParse->nErr || db->mallocFailed ){
87362    goto insert_cleanup;
87363  }
87364
87365  /* Locate the table into which we will be inserting new information.
87366  */
87367  assert( pTabList->nSrc==1 );
87368  zTab = pTabList->a[0].zName;
87369  if( NEVER(zTab==0) ) goto insert_cleanup;
87370  pTab = sqlite3SrcListLookup(pParse, pTabList);
87371  if( pTab==0 ){
87372    goto insert_cleanup;
87373  }
87374  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
87375  assert( iDb<db->nDb );
87376  pDb = &db->aDb[iDb];
87377  zDb = pDb->zName;
87378  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
87379    goto insert_cleanup;
87380  }
87381
87382  /* Figure out if we have any triggers and if the table being
87383  ** inserted into is a view
87384  */
87385#ifndef SQLITE_OMIT_TRIGGER
87386  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
87387  isView = pTab->pSelect!=0;
87388#else
87389# define pTrigger 0
87390# define tmask 0
87391# define isView 0
87392#endif
87393#ifdef SQLITE_OMIT_VIEW
87394# undef isView
87395# define isView 0
87396#endif
87397  assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
87398
87399  /* If pTab is really a view, make sure it has been initialized.
87400  ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
87401  ** module table).
87402  */
87403  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
87404    goto insert_cleanup;
87405  }
87406
87407  /* Ensure that:
87408  *  (a) the table is not read-only,
87409  *  (b) that if it is a view then ON INSERT triggers exist
87410  */
87411  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
87412    goto insert_cleanup;
87413  }
87414
87415  /* Allocate a VDBE
87416  */
87417  v = sqlite3GetVdbe(pParse);
87418  if( v==0 ) goto insert_cleanup;
87419  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
87420  sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
87421
87422#ifndef SQLITE_OMIT_XFER_OPT
87423  /* If the statement is of the form
87424  **
87425  **       INSERT INTO <table1> SELECT * FROM <table2>;
87426  **
87427  ** Then special optimizations can be applied that make the transfer
87428  ** very fast and which reduce fragmentation of indices.
87429  **
87430  ** This is the 2nd template.
87431  */
87432  if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
87433    assert( !pTrigger );
87434    assert( pList==0 );
87435    goto insert_end;
87436  }
87437#endif /* SQLITE_OMIT_XFER_OPT */
87438
87439  /* If this is an AUTOINCREMENT table, look up the sequence number in the
87440  ** sqlite_sequence table and store it in memory cell regAutoinc.
87441  */
87442  regAutoinc = autoIncBegin(pParse, iDb, pTab);
87443
87444  /* Figure out how many columns of data are supplied.  If the data
87445  ** is coming from a SELECT statement, then generate a co-routine that
87446  ** produces a single row of the SELECT on each invocation.  The
87447  ** co-routine is the common header to the 3rd and 4th templates.
87448  */
87449  if( pSelect ){
87450    /* Data is coming from a SELECT.  Generate code to implement that SELECT
87451    ** as a co-routine.  The code is common to both the 3rd and 4th
87452    ** templates:
87453    **
87454    **         EOF <- 0
87455    **         X <- A
87456    **         goto B
87457    **      A: setup for the SELECT
87458    **         loop over the tables in the SELECT
87459    **           load value into register R..R+n
87460    **           yield X
87461    **         end loop
87462    **         cleanup after the SELECT
87463    **         EOF <- 1
87464    **         yield X
87465    **         halt-error
87466    **
87467    ** On each invocation of the co-routine, it puts a single row of the
87468    ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
87469    ** (These output registers are allocated by sqlite3Select().)  When
87470    ** the SELECT completes, it sets the EOF flag stored in regEof.
87471    */
87472    int rc, j1;
87473
87474    regEof = ++pParse->nMem;
87475    sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
87476    VdbeComment((v, "SELECT eof flag"));
87477    sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
87478    addrSelect = sqlite3VdbeCurrentAddr(v)+2;
87479    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
87480    j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
87481    VdbeComment((v, "Jump over SELECT coroutine"));
87482
87483    /* Resolve the expressions in the SELECT statement and execute it. */
87484    rc = sqlite3Select(pParse, pSelect, &dest);
87485    assert( pParse->nErr==0 || rc );
87486    if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
87487      goto insert_cleanup;
87488    }
87489    sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
87490    sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
87491    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
87492    VdbeComment((v, "End of SELECT coroutine"));
87493    sqlite3VdbeJumpHere(v, j1);                          /* label B: */
87494
87495    regFromSelect = dest.iMem;
87496    assert( pSelect->pEList );
87497    nColumn = pSelect->pEList->nExpr;
87498    assert( dest.nMem==nColumn );
87499
87500    /* Set useTempTable to TRUE if the result of the SELECT statement
87501    ** should be written into a temporary table (template 4).  Set to
87502    ** FALSE if each* row of the SELECT can be written directly into
87503    ** the destination table (template 3).
87504    **
87505    ** A temp table must be used if the table being updated is also one
87506    ** of the tables being read by the SELECT statement.  Also use a
87507    ** temp table in the case of row triggers.
87508    */
87509    if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
87510      useTempTable = 1;
87511    }
87512
87513    if( useTempTable ){
87514      /* Invoke the coroutine to extract information from the SELECT
87515      ** and add it to a transient table srcTab.  The code generated
87516      ** here is from the 4th template:
87517      **
87518      **      B: open temp table
87519      **      L: yield X
87520      **         if EOF goto M
87521      **         insert row from R..R+n into temp table
87522      **         goto L
87523      **      M: ...
87524      */
87525      int regRec;          /* Register to hold packed record */
87526      int regTempRowid;    /* Register to hold temp table ROWID */
87527      int addrTop;         /* Label "L" */
87528      int addrIf;          /* Address of jump to M */
87529
87530      srcTab = pParse->nTab++;
87531      regRec = sqlite3GetTempReg(pParse);
87532      regTempRowid = sqlite3GetTempReg(pParse);
87533      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
87534      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
87535      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
87536      sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
87537      sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
87538      sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
87539      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
87540      sqlite3VdbeJumpHere(v, addrIf);
87541      sqlite3ReleaseTempReg(pParse, regRec);
87542      sqlite3ReleaseTempReg(pParse, regTempRowid);
87543    }
87544  }else{
87545    /* This is the case if the data for the INSERT is coming from a VALUES
87546    ** clause
87547    */
87548    NameContext sNC;
87549    memset(&sNC, 0, sizeof(sNC));
87550    sNC.pParse = pParse;
87551    srcTab = -1;
87552    assert( useTempTable==0 );
87553    nColumn = pList ? pList->nExpr : 0;
87554    for(i=0; i<nColumn; i++){
87555      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
87556        goto insert_cleanup;
87557      }
87558    }
87559  }
87560
87561  /* Make sure the number of columns in the source data matches the number
87562  ** of columns to be inserted into the table.
87563  */
87564  if( IsVirtual(pTab) ){
87565    for(i=0; i<pTab->nCol; i++){
87566      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
87567    }
87568  }
87569  if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
87570    sqlite3ErrorMsg(pParse,
87571       "table %S has %d columns but %d values were supplied",
87572       pTabList, 0, pTab->nCol-nHidden, nColumn);
87573    goto insert_cleanup;
87574  }
87575  if( pColumn!=0 && nColumn!=pColumn->nId ){
87576    sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
87577    goto insert_cleanup;
87578  }
87579
87580  /* If the INSERT statement included an IDLIST term, then make sure
87581  ** all elements of the IDLIST really are columns of the table and
87582  ** remember the column indices.
87583  **
87584  ** If the table has an INTEGER PRIMARY KEY column and that column
87585  ** is named in the IDLIST, then record in the keyColumn variable
87586  ** the index into IDLIST of the primary key column.  keyColumn is
87587  ** the index of the primary key as it appears in IDLIST, not as
87588  ** is appears in the original table.  (The index of the primary
87589  ** key in the original table is pTab->iPKey.)
87590  */
87591  if( pColumn ){
87592    for(i=0; i<pColumn->nId; i++){
87593      pColumn->a[i].idx = -1;
87594    }
87595    for(i=0; i<pColumn->nId; i++){
87596      for(j=0; j<pTab->nCol; j++){
87597        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
87598          pColumn->a[i].idx = j;
87599          if( j==pTab->iPKey ){
87600            keyColumn = i;
87601          }
87602          break;
87603        }
87604      }
87605      if( j>=pTab->nCol ){
87606        if( sqlite3IsRowid(pColumn->a[i].zName) ){
87607          keyColumn = i;
87608        }else{
87609          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
87610              pTabList, 0, pColumn->a[i].zName);
87611          pParse->checkSchema = 1;
87612          goto insert_cleanup;
87613        }
87614      }
87615    }
87616  }
87617
87618  /* If there is no IDLIST term but the table has an integer primary
87619  ** key, the set the keyColumn variable to the primary key column index
87620  ** in the original table definition.
87621  */
87622  if( pColumn==0 && nColumn>0 ){
87623    keyColumn = pTab->iPKey;
87624  }
87625
87626  /* Initialize the count of rows to be inserted
87627  */
87628  if( db->flags & SQLITE_CountRows ){
87629    regRowCount = ++pParse->nMem;
87630    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
87631  }
87632
87633  /* If this is not a view, open the table and and all indices */
87634  if( !isView ){
87635    int nIdx;
87636
87637    baseCur = pParse->nTab;
87638    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
87639    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
87640    if( aRegIdx==0 ){
87641      goto insert_cleanup;
87642    }
87643    for(i=0; i<nIdx; i++){
87644      aRegIdx[i] = ++pParse->nMem;
87645    }
87646  }
87647
87648  /* This is the top of the main insertion loop */
87649  if( useTempTable ){
87650    /* This block codes the top of loop only.  The complete loop is the
87651    ** following pseudocode (template 4):
87652    **
87653    **         rewind temp table
87654    **      C: loop over rows of intermediate table
87655    **           transfer values form intermediate table into <table>
87656    **         end loop
87657    **      D: ...
87658    */
87659    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
87660    addrCont = sqlite3VdbeCurrentAddr(v);
87661  }else if( pSelect ){
87662    /* This block codes the top of loop only.  The complete loop is the
87663    ** following pseudocode (template 3):
87664    **
87665    **      C: yield X
87666    **         if EOF goto D
87667    **         insert the select result into <table> from R..R+n
87668    **         goto C
87669    **      D: ...
87670    */
87671    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
87672    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
87673  }
87674
87675  /* Allocate registers for holding the rowid of the new row,
87676  ** the content of the new row, and the assemblied row record.
87677  */
87678  regRowid = regIns = pParse->nMem+1;
87679  pParse->nMem += pTab->nCol + 1;
87680  if( IsVirtual(pTab) ){
87681    regRowid++;
87682    pParse->nMem++;
87683  }
87684  regData = regRowid+1;
87685
87686  /* Run the BEFORE and INSTEAD OF triggers, if there are any
87687  */
87688  endOfLoop = sqlite3VdbeMakeLabel(v);
87689  if( tmask & TRIGGER_BEFORE ){
87690    int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
87691
87692    /* build the NEW.* reference row.  Note that if there is an INTEGER
87693    ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
87694    ** translated into a unique ID for the row.  But on a BEFORE trigger,
87695    ** we do not know what the unique ID will be (because the insert has
87696    ** not happened yet) so we substitute a rowid of -1
87697    */
87698    if( keyColumn<0 ){
87699      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
87700    }else{
87701      int j1;
87702      if( useTempTable ){
87703        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
87704      }else{
87705        assert( pSelect==0 );  /* Otherwise useTempTable is true */
87706        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
87707      }
87708      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
87709      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
87710      sqlite3VdbeJumpHere(v, j1);
87711      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
87712    }
87713
87714    /* Cannot have triggers on a virtual table. If it were possible,
87715    ** this block would have to account for hidden column.
87716    */
87717    assert( !IsVirtual(pTab) );
87718
87719    /* Create the new column data
87720    */
87721    for(i=0; i<pTab->nCol; i++){
87722      if( pColumn==0 ){
87723        j = i;
87724      }else{
87725        for(j=0; j<pColumn->nId; j++){
87726          if( pColumn->a[j].idx==i ) break;
87727        }
87728      }
87729      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
87730        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
87731      }else if( useTempTable ){
87732        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
87733      }else{
87734        assert( pSelect==0 ); /* Otherwise useTempTable is true */
87735        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
87736      }
87737    }
87738
87739    /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
87740    ** do not attempt any conversions before assembling the record.
87741    ** If this is a real table, attempt conversions as required by the
87742    ** table column affinities.
87743    */
87744    if( !isView ){
87745      sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
87746      sqlite3TableAffinityStr(v, pTab);
87747    }
87748
87749    /* Fire BEFORE or INSTEAD OF triggers */
87750    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
87751        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
87752
87753    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
87754  }
87755
87756  /* Push the record number for the new entry onto the stack.  The
87757  ** record number is a randomly generate integer created by NewRowid
87758  ** except when the table has an INTEGER PRIMARY KEY column, in which
87759  ** case the record number is the same as that column.
87760  */
87761  if( !isView ){
87762    if( IsVirtual(pTab) ){
87763      /* The row that the VUpdate opcode will delete: none */
87764      sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
87765    }
87766    if( keyColumn>=0 ){
87767      if( useTempTable ){
87768        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
87769      }else if( pSelect ){
87770        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
87771      }else{
87772        VdbeOp *pOp;
87773        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
87774        pOp = sqlite3VdbeGetOp(v, -1);
87775        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
87776          appendFlag = 1;
87777          pOp->opcode = OP_NewRowid;
87778          pOp->p1 = baseCur;
87779          pOp->p2 = regRowid;
87780          pOp->p3 = regAutoinc;
87781        }
87782      }
87783      /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
87784      ** to generate a unique primary key value.
87785      */
87786      if( !appendFlag ){
87787        int j1;
87788        if( !IsVirtual(pTab) ){
87789          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
87790          sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
87791          sqlite3VdbeJumpHere(v, j1);
87792        }else{
87793          j1 = sqlite3VdbeCurrentAddr(v);
87794          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
87795        }
87796        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
87797      }
87798    }else if( IsVirtual(pTab) ){
87799      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
87800    }else{
87801      sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
87802      appendFlag = 1;
87803    }
87804    autoIncStep(pParse, regAutoinc, regRowid);
87805
87806    /* Push onto the stack, data for all columns of the new entry, beginning
87807    ** with the first column.
87808    */
87809    nHidden = 0;
87810    for(i=0; i<pTab->nCol; i++){
87811      int iRegStore = regRowid+1+i;
87812      if( i==pTab->iPKey ){
87813        /* The value of the INTEGER PRIMARY KEY column is always a NULL.
87814        ** Whenever this column is read, the record number will be substituted
87815        ** in its place.  So will fill this column with a NULL to avoid
87816        ** taking up data space with information that will never be used. */
87817        sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
87818        continue;
87819      }
87820      if( pColumn==0 ){
87821        if( IsHiddenColumn(&pTab->aCol[i]) ){
87822          assert( IsVirtual(pTab) );
87823          j = -1;
87824          nHidden++;
87825        }else{
87826          j = i - nHidden;
87827        }
87828      }else{
87829        for(j=0; j<pColumn->nId; j++){
87830          if( pColumn->a[j].idx==i ) break;
87831        }
87832      }
87833      if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
87834        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
87835      }else if( useTempTable ){
87836        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
87837      }else if( pSelect ){
87838        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
87839      }else{
87840        sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
87841      }
87842    }
87843
87844    /* Generate code to check constraints and generate index keys and
87845    ** do the insertion.
87846    */
87847#ifndef SQLITE_OMIT_VIRTUALTABLE
87848    if( IsVirtual(pTab) ){
87849      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
87850      sqlite3VtabMakeWritable(pParse, pTab);
87851      sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
87852      sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
87853      sqlite3MayAbort(pParse);
87854    }else
87855#endif
87856    {
87857      int isReplace;    /* Set to true if constraints may cause a replace */
87858      sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
87859          keyColumn>=0, 0, onError, endOfLoop, &isReplace
87860      );
87861      sqlite3FkCheck(pParse, pTab, 0, regIns);
87862      sqlite3CompleteInsertion(
87863          pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
87864      );
87865    }
87866  }
87867
87868  /* Update the count of rows that are inserted
87869  */
87870  if( (db->flags & SQLITE_CountRows)!=0 ){
87871    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
87872  }
87873
87874  if( pTrigger ){
87875    /* Code AFTER triggers */
87876    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
87877        pTab, regData-2-pTab->nCol, onError, endOfLoop);
87878  }
87879
87880  /* The bottom of the main insertion loop, if the data source
87881  ** is a SELECT statement.
87882  */
87883  sqlite3VdbeResolveLabel(v, endOfLoop);
87884  if( useTempTable ){
87885    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
87886    sqlite3VdbeJumpHere(v, addrInsTop);
87887    sqlite3VdbeAddOp1(v, OP_Close, srcTab);
87888  }else if( pSelect ){
87889    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
87890    sqlite3VdbeJumpHere(v, addrInsTop);
87891  }
87892
87893  if( !IsVirtual(pTab) && !isView ){
87894    /* Close all tables opened */
87895    sqlite3VdbeAddOp1(v, OP_Close, baseCur);
87896    for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
87897      sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
87898    }
87899  }
87900
87901insert_end:
87902  /* Update the sqlite_sequence table by storing the content of the
87903  ** maximum rowid counter values recorded while inserting into
87904  ** autoincrement tables.
87905  */
87906  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
87907    sqlite3AutoincrementEnd(pParse);
87908  }
87909
87910  /*
87911  ** Return the number of rows inserted. If this routine is
87912  ** generating code because of a call to sqlite3NestedParse(), do not
87913  ** invoke the callback function.
87914  */
87915  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
87916    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
87917    sqlite3VdbeSetNumCols(v, 1);
87918    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
87919  }
87920
87921insert_cleanup:
87922  sqlite3SrcListDelete(db, pTabList);
87923  sqlite3ExprListDelete(db, pList);
87924  sqlite3SelectDelete(db, pSelect);
87925  sqlite3IdListDelete(db, pColumn);
87926  sqlite3DbFree(db, aRegIdx);
87927}
87928
87929/* Make sure "isView" and other macros defined above are undefined. Otherwise
87930** thely may interfere with compilation of other functions in this file
87931** (or in another file, if this file becomes part of the amalgamation).  */
87932#ifdef isView
87933 #undef isView
87934#endif
87935#ifdef pTrigger
87936 #undef pTrigger
87937#endif
87938#ifdef tmask
87939 #undef tmask
87940#endif
87941
87942
87943/*
87944** Generate code to do constraint checks prior to an INSERT or an UPDATE.
87945**
87946** The input is a range of consecutive registers as follows:
87947**
87948**    1.  The rowid of the row after the update.
87949**
87950**    2.  The data in the first column of the entry after the update.
87951**
87952**    i.  Data from middle columns...
87953**
87954**    N.  The data in the last column of the entry after the update.
87955**
87956** The regRowid parameter is the index of the register containing (1).
87957**
87958** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
87959** the address of a register containing the rowid before the update takes
87960** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
87961** is false, indicating an INSERT statement, then a non-zero rowidChng
87962** indicates that the rowid was explicitly specified as part of the
87963** INSERT statement. If rowidChng is false, it means that  the rowid is
87964** computed automatically in an insert or that the rowid value is not
87965** modified by an update.
87966**
87967** The code generated by this routine store new index entries into
87968** registers identified by aRegIdx[].  No index entry is created for
87969** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
87970** the same as the order of indices on the linked list of indices
87971** attached to the table.
87972**
87973** This routine also generates code to check constraints.  NOT NULL,
87974** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
87975** then the appropriate action is performed.  There are five possible
87976** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
87977**
87978**  Constraint type  Action       What Happens
87979**  ---------------  ----------   ----------------------------------------
87980**  any              ROLLBACK     The current transaction is rolled back and
87981**                                sqlite3_exec() returns immediately with a
87982**                                return code of SQLITE_CONSTRAINT.
87983**
87984**  any              ABORT        Back out changes from the current command
87985**                                only (do not do a complete rollback) then
87986**                                cause sqlite3_exec() to return immediately
87987**                                with SQLITE_CONSTRAINT.
87988**
87989**  any              FAIL         Sqlite_exec() returns immediately with a
87990**                                return code of SQLITE_CONSTRAINT.  The
87991**                                transaction is not rolled back and any
87992**                                prior changes are retained.
87993**
87994**  any              IGNORE       The record number and data is popped from
87995**                                the stack and there is an immediate jump
87996**                                to label ignoreDest.
87997**
87998**  NOT NULL         REPLACE      The NULL value is replace by the default
87999**                                value for that column.  If the default value
88000**                                is NULL, the action is the same as ABORT.
88001**
88002**  UNIQUE           REPLACE      The other row that conflicts with the row
88003**                                being inserted is removed.
88004**
88005**  CHECK            REPLACE      Illegal.  The results in an exception.
88006**
88007** Which action to take is determined by the overrideError parameter.
88008** Or if overrideError==OE_Default, then the pParse->onError parameter
88009** is used.  Or if pParse->onError==OE_Default then the onError value
88010** for the constraint is used.
88011**
88012** The calling routine must open a read/write cursor for pTab with
88013** cursor number "baseCur".  All indices of pTab must also have open
88014** read/write cursors with cursor number baseCur+i for the i-th cursor.
88015** Except, if there is no possibility of a REPLACE action then
88016** cursors do not need to be open for indices where aRegIdx[i]==0.
88017*/
88018SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
88019  Parse *pParse,      /* The parser context */
88020  Table *pTab,        /* the table into which we are inserting */
88021  int baseCur,        /* Index of a read/write cursor pointing at pTab */
88022  int regRowid,       /* Index of the range of input registers */
88023  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
88024  int rowidChng,      /* True if the rowid might collide with existing entry */
88025  int isUpdate,       /* True for UPDATE, False for INSERT */
88026  int overrideError,  /* Override onError to this if not OE_Default */
88027  int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
88028  int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
88029){
88030  int i;              /* loop counter */
88031  Vdbe *v;            /* VDBE under constrution */
88032  int nCol;           /* Number of columns */
88033  int onError;        /* Conflict resolution strategy */
88034  int j1;             /* Addresss of jump instruction */
88035  int j2 = 0, j3;     /* Addresses of jump instructions */
88036  int regData;        /* Register containing first data column */
88037  int iCur;           /* Table cursor number */
88038  Index *pIdx;         /* Pointer to one of the indices */
88039  int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
88040  int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
88041
88042  v = sqlite3GetVdbe(pParse);
88043  assert( v!=0 );
88044  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
88045  nCol = pTab->nCol;
88046  regData = regRowid + 1;
88047
88048  /* Test all NOT NULL constraints.
88049  */
88050  for(i=0; i<nCol; i++){
88051    if( i==pTab->iPKey ){
88052      continue;
88053    }
88054    onError = pTab->aCol[i].notNull;
88055    if( onError==OE_None ) continue;
88056    if( overrideError!=OE_Default ){
88057      onError = overrideError;
88058    }else if( onError==OE_Default ){
88059      onError = OE_Abort;
88060    }
88061    if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
88062      onError = OE_Abort;
88063    }
88064    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
88065        || onError==OE_Ignore || onError==OE_Replace );
88066    switch( onError ){
88067      case OE_Abort:
88068        sqlite3MayAbort(pParse);
88069      case OE_Rollback:
88070      case OE_Fail: {
88071        char *zMsg;
88072        sqlite3VdbeAddOp3(v, OP_HaltIfNull,
88073                                  SQLITE_CONSTRAINT, onError, regData+i);
88074        zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
88075                              pTab->zName, pTab->aCol[i].zName);
88076        sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
88077        break;
88078      }
88079      case OE_Ignore: {
88080        sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
88081        break;
88082      }
88083      default: {
88084        assert( onError==OE_Replace );
88085        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
88086        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
88087        sqlite3VdbeJumpHere(v, j1);
88088        break;
88089      }
88090    }
88091  }
88092
88093  /* Test all CHECK constraints
88094  */
88095#ifndef SQLITE_OMIT_CHECK
88096  if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
88097    int allOk = sqlite3VdbeMakeLabel(v);
88098    pParse->ckBase = regData;
88099    sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
88100    onError = overrideError!=OE_Default ? overrideError : OE_Abort;
88101    if( onError==OE_Ignore ){
88102      sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
88103    }else{
88104      if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
88105      sqlite3HaltConstraint(pParse, onError, 0, 0);
88106    }
88107    sqlite3VdbeResolveLabel(v, allOk);
88108  }
88109#endif /* !defined(SQLITE_OMIT_CHECK) */
88110
88111  /* If we have an INTEGER PRIMARY KEY, make sure the primary key
88112  ** of the new record does not previously exist.  Except, if this
88113  ** is an UPDATE and the primary key is not changing, that is OK.
88114  */
88115  if( rowidChng ){
88116    onError = pTab->keyConf;
88117    if( overrideError!=OE_Default ){
88118      onError = overrideError;
88119    }else if( onError==OE_Default ){
88120      onError = OE_Abort;
88121    }
88122
88123    if( isUpdate ){
88124      j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
88125    }
88126    j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
88127    switch( onError ){
88128      default: {
88129        onError = OE_Abort;
88130        /* Fall thru into the next case */
88131      }
88132      case OE_Rollback:
88133      case OE_Abort:
88134      case OE_Fail: {
88135        sqlite3HaltConstraint(
88136          pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
88137        break;
88138      }
88139      case OE_Replace: {
88140        /* If there are DELETE triggers on this table and the
88141        ** recursive-triggers flag is set, call GenerateRowDelete() to
88142        ** remove the conflicting row from the the table. This will fire
88143        ** the triggers and remove both the table and index b-tree entries.
88144        **
88145        ** Otherwise, if there are no triggers or the recursive-triggers
88146        ** flag is not set, but the table has one or more indexes, call
88147        ** GenerateRowIndexDelete(). This removes the index b-tree entries
88148        ** only. The table b-tree entry will be replaced by the new entry
88149        ** when it is inserted.
88150        **
88151        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
88152        ** also invoke MultiWrite() to indicate that this VDBE may require
88153        ** statement rollback (if the statement is aborted after the delete
88154        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
88155        ** but being more selective here allows statements like:
88156        **
88157        **   REPLACE INTO t(rowid) VALUES($newrowid)
88158        **
88159        ** to run without a statement journal if there are no indexes on the
88160        ** table.
88161        */
88162        Trigger *pTrigger = 0;
88163        if( pParse->db->flags&SQLITE_RecTriggers ){
88164          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
88165        }
88166        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
88167          sqlite3MultiWrite(pParse);
88168          sqlite3GenerateRowDelete(
88169              pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
88170          );
88171        }else if( pTab->pIndex ){
88172          sqlite3MultiWrite(pParse);
88173          sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
88174        }
88175        seenReplace = 1;
88176        break;
88177      }
88178      case OE_Ignore: {
88179        assert( seenReplace==0 );
88180        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
88181        break;
88182      }
88183    }
88184    sqlite3VdbeJumpHere(v, j3);
88185    if( isUpdate ){
88186      sqlite3VdbeJumpHere(v, j2);
88187    }
88188  }
88189
88190  /* Test all UNIQUE constraints by creating entries for each UNIQUE
88191  ** index and making sure that duplicate entries do not already exist.
88192  ** Add the new records to the indices as we go.
88193  */
88194  for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
88195    int regIdx;
88196    int regR;
88197
88198    if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
88199
88200    /* Create a key for accessing the index entry */
88201    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
88202    for(i=0; i<pIdx->nColumn; i++){
88203      int idx = pIdx->aiColumn[i];
88204      if( idx==pTab->iPKey ){
88205        sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
88206      }else{
88207        sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
88208      }
88209    }
88210    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
88211    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
88212    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
88213    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
88214
88215    /* Find out what action to take in case there is an indexing conflict */
88216    onError = pIdx->onError;
88217    if( onError==OE_None ){
88218      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
88219      continue;  /* pIdx is not a UNIQUE index */
88220    }
88221    if( overrideError!=OE_Default ){
88222      onError = overrideError;
88223    }else if( onError==OE_Default ){
88224      onError = OE_Abort;
88225    }
88226    if( seenReplace ){
88227      if( onError==OE_Ignore ) onError = OE_Replace;
88228      else if( onError==OE_Fail ) onError = OE_Abort;
88229    }
88230
88231    /* Check to see if the new index entry will be unique */
88232    regR = sqlite3GetTempReg(pParse);
88233    sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
88234    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
88235                           regR, SQLITE_INT_TO_PTR(regIdx),
88236                           P4_INT32);
88237    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
88238
88239    /* Generate code that executes if the new index entry is not unique */
88240    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
88241        || onError==OE_Ignore || onError==OE_Replace );
88242    switch( onError ){
88243      case OE_Rollback:
88244      case OE_Abort:
88245      case OE_Fail: {
88246        int j;
88247        StrAccum errMsg;
88248        const char *zSep;
88249        char *zErr;
88250
88251        sqlite3StrAccumInit(&errMsg, 0, 0, 200);
88252        errMsg.db = pParse->db;
88253        zSep = pIdx->nColumn>1 ? "columns " : "column ";
88254        for(j=0; j<pIdx->nColumn; j++){
88255          char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
88256          sqlite3StrAccumAppend(&errMsg, zSep, -1);
88257          zSep = ", ";
88258          sqlite3StrAccumAppend(&errMsg, zCol, -1);
88259        }
88260        sqlite3StrAccumAppend(&errMsg,
88261            pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
88262        zErr = sqlite3StrAccumFinish(&errMsg);
88263        sqlite3HaltConstraint(pParse, onError, zErr, 0);
88264        sqlite3DbFree(errMsg.db, zErr);
88265        break;
88266      }
88267      case OE_Ignore: {
88268        assert( seenReplace==0 );
88269        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
88270        break;
88271      }
88272      default: {
88273        Trigger *pTrigger = 0;
88274        assert( onError==OE_Replace );
88275        sqlite3MultiWrite(pParse);
88276        if( pParse->db->flags&SQLITE_RecTriggers ){
88277          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
88278        }
88279        sqlite3GenerateRowDelete(
88280            pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
88281        );
88282        seenReplace = 1;
88283        break;
88284      }
88285    }
88286    sqlite3VdbeJumpHere(v, j3);
88287    sqlite3ReleaseTempReg(pParse, regR);
88288  }
88289
88290  if( pbMayReplace ){
88291    *pbMayReplace = seenReplace;
88292  }
88293}
88294
88295/*
88296** This routine generates code to finish the INSERT or UPDATE operation
88297** that was started by a prior call to sqlite3GenerateConstraintChecks.
88298** A consecutive range of registers starting at regRowid contains the
88299** rowid and the content to be inserted.
88300**
88301** The arguments to this routine should be the same as the first six
88302** arguments to sqlite3GenerateConstraintChecks.
88303*/
88304SQLITE_PRIVATE void sqlite3CompleteInsertion(
88305  Parse *pParse,      /* The parser context */
88306  Table *pTab,        /* the table into which we are inserting */
88307  int baseCur,        /* Index of a read/write cursor pointing at pTab */
88308  int regRowid,       /* Range of content */
88309  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
88310  int isUpdate,       /* True for UPDATE, False for INSERT */
88311  int appendBias,     /* True if this is likely to be an append */
88312  int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
88313){
88314  int i;
88315  Vdbe *v;
88316  int nIdx;
88317  Index *pIdx;
88318  u8 pik_flags;
88319  int regData;
88320  int regRec;
88321
88322  v = sqlite3GetVdbe(pParse);
88323  assert( v!=0 );
88324  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
88325  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
88326  for(i=nIdx-1; i>=0; i--){
88327    if( aRegIdx[i]==0 ) continue;
88328    sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
88329    if( useSeekResult ){
88330      sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
88331    }
88332  }
88333  regData = regRowid + 1;
88334  regRec = sqlite3GetTempReg(pParse);
88335  sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
88336  sqlite3TableAffinityStr(v, pTab);
88337  sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
88338  if( pParse->nested ){
88339    pik_flags = 0;
88340  }else{
88341    pik_flags = OPFLAG_NCHANGE;
88342    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
88343  }
88344  if( appendBias ){
88345    pik_flags |= OPFLAG_APPEND;
88346  }
88347  if( useSeekResult ){
88348    pik_flags |= OPFLAG_USESEEKRESULT;
88349  }
88350  sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
88351  if( !pParse->nested ){
88352    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
88353  }
88354  sqlite3VdbeChangeP5(v, pik_flags);
88355}
88356
88357/*
88358** Generate code that will open cursors for a table and for all
88359** indices of that table.  The "baseCur" parameter is the cursor number used
88360** for the table.  Indices are opened on subsequent cursors.
88361**
88362** Return the number of indices on the table.
88363*/
88364SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
88365  Parse *pParse,   /* Parsing context */
88366  Table *pTab,     /* Table to be opened */
88367  int baseCur,     /* Cursor number assigned to the table */
88368  int op           /* OP_OpenRead or OP_OpenWrite */
88369){
88370  int i;
88371  int iDb;
88372  Index *pIdx;
88373  Vdbe *v;
88374
88375  if( IsVirtual(pTab) ) return 0;
88376  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
88377  v = sqlite3GetVdbe(pParse);
88378  assert( v!=0 );
88379  sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
88380  for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
88381    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
88382    assert( pIdx->pSchema==pTab->pSchema );
88383    sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
88384                      (char*)pKey, P4_KEYINFO_HANDOFF);
88385    VdbeComment((v, "%s", pIdx->zName));
88386  }
88387  if( pParse->nTab<baseCur+i ){
88388    pParse->nTab = baseCur+i;
88389  }
88390  return i-1;
88391}
88392
88393
88394#ifdef SQLITE_TEST
88395/*
88396** The following global variable is incremented whenever the
88397** transfer optimization is used.  This is used for testing
88398** purposes only - to make sure the transfer optimization really
88399** is happening when it is suppose to.
88400*/
88401SQLITE_API int sqlite3_xferopt_count;
88402#endif /* SQLITE_TEST */
88403
88404
88405#ifndef SQLITE_OMIT_XFER_OPT
88406/*
88407** Check to collation names to see if they are compatible.
88408*/
88409static int xferCompatibleCollation(const char *z1, const char *z2){
88410  if( z1==0 ){
88411    return z2==0;
88412  }
88413  if( z2==0 ){
88414    return 0;
88415  }
88416  return sqlite3StrICmp(z1, z2)==0;
88417}
88418
88419
88420/*
88421** Check to see if index pSrc is compatible as a source of data
88422** for index pDest in an insert transfer optimization.  The rules
88423** for a compatible index:
88424**
88425**    *   The index is over the same set of columns
88426**    *   The same DESC and ASC markings occurs on all columns
88427**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
88428**    *   The same collating sequence on each column
88429*/
88430static int xferCompatibleIndex(Index *pDest, Index *pSrc){
88431  int i;
88432  assert( pDest && pSrc );
88433  assert( pDest->pTable!=pSrc->pTable );
88434  if( pDest->nColumn!=pSrc->nColumn ){
88435    return 0;   /* Different number of columns */
88436  }
88437  if( pDest->onError!=pSrc->onError ){
88438    return 0;   /* Different conflict resolution strategies */
88439  }
88440  for(i=0; i<pSrc->nColumn; i++){
88441    if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
88442      return 0;   /* Different columns indexed */
88443    }
88444    if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
88445      return 0;   /* Different sort orders */
88446    }
88447    if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
88448      return 0;   /* Different collating sequences */
88449    }
88450  }
88451
88452  /* If no test above fails then the indices must be compatible */
88453  return 1;
88454}
88455
88456/*
88457** Attempt the transfer optimization on INSERTs of the form
88458**
88459**     INSERT INTO tab1 SELECT * FROM tab2;
88460**
88461** This optimization is only attempted if
88462**
88463**    (1)  tab1 and tab2 have identical schemas including all the
88464**         same indices and constraints
88465**
88466**    (2)  tab1 and tab2 are different tables
88467**
88468**    (3)  There must be no triggers on tab1
88469**
88470**    (4)  The result set of the SELECT statement is "*"
88471**
88472**    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
88473**         or LIMIT clause.
88474**
88475**    (6)  The SELECT statement is a simple (not a compound) select that
88476**         contains only tab2 in its FROM clause
88477**
88478** This method for implementing the INSERT transfers raw records from
88479** tab2 over to tab1.  The columns are not decoded.  Raw records from
88480** the indices of tab2 are transfered to tab1 as well.  In so doing,
88481** the resulting tab1 has much less fragmentation.
88482**
88483** This routine returns TRUE if the optimization is attempted.  If any
88484** of the conditions above fail so that the optimization should not
88485** be attempted, then this routine returns FALSE.
88486*/
88487static int xferOptimization(
88488  Parse *pParse,        /* Parser context */
88489  Table *pDest,         /* The table we are inserting into */
88490  Select *pSelect,      /* A SELECT statement to use as the data source */
88491  int onError,          /* How to handle constraint errors */
88492  int iDbDest           /* The database of pDest */
88493){
88494  ExprList *pEList;                /* The result set of the SELECT */
88495  Table *pSrc;                     /* The table in the FROM clause of SELECT */
88496  Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
88497  struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
88498  int i;                           /* Loop counter */
88499  int iDbSrc;                      /* The database of pSrc */
88500  int iSrc, iDest;                 /* Cursors from source and destination */
88501  int addr1, addr2;                /* Loop addresses */
88502  int emptyDestTest;               /* Address of test for empty pDest */
88503  int emptySrcTest;                /* Address of test for empty pSrc */
88504  Vdbe *v;                         /* The VDBE we are building */
88505  KeyInfo *pKey;                   /* Key information for an index */
88506  int regAutoinc;                  /* Memory register used by AUTOINC */
88507  int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
88508  int regData, regRowid;           /* Registers holding data and rowid */
88509
88510  if( pSelect==0 ){
88511    return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
88512  }
88513  if( sqlite3TriggerList(pParse, pDest) ){
88514    return 0;   /* tab1 must not have triggers */
88515  }
88516#ifndef SQLITE_OMIT_VIRTUALTABLE
88517  if( pDest->tabFlags & TF_Virtual ){
88518    return 0;   /* tab1 must not be a virtual table */
88519  }
88520#endif
88521  if( onError==OE_Default ){
88522    onError = OE_Abort;
88523  }
88524  if( onError!=OE_Abort && onError!=OE_Rollback ){
88525    return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
88526  }
88527  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
88528  if( pSelect->pSrc->nSrc!=1 ){
88529    return 0;   /* FROM clause must have exactly one term */
88530  }
88531  if( pSelect->pSrc->a[0].pSelect ){
88532    return 0;   /* FROM clause cannot contain a subquery */
88533  }
88534  if( pSelect->pWhere ){
88535    return 0;   /* SELECT may not have a WHERE clause */
88536  }
88537  if( pSelect->pOrderBy ){
88538    return 0;   /* SELECT may not have an ORDER BY clause */
88539  }
88540  /* Do not need to test for a HAVING clause.  If HAVING is present but
88541  ** there is no ORDER BY, we will get an error. */
88542  if( pSelect->pGroupBy ){
88543    return 0;   /* SELECT may not have a GROUP BY clause */
88544  }
88545  if( pSelect->pLimit ){
88546    return 0;   /* SELECT may not have a LIMIT clause */
88547  }
88548  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
88549  if( pSelect->pPrior ){
88550    return 0;   /* SELECT may not be a compound query */
88551  }
88552  if( pSelect->selFlags & SF_Distinct ){
88553    return 0;   /* SELECT may not be DISTINCT */
88554  }
88555  pEList = pSelect->pEList;
88556  assert( pEList!=0 );
88557  if( pEList->nExpr!=1 ){
88558    return 0;   /* The result set must have exactly one column */
88559  }
88560  assert( pEList->a[0].pExpr );
88561  if( pEList->a[0].pExpr->op!=TK_ALL ){
88562    return 0;   /* The result set must be the special operator "*" */
88563  }
88564
88565  /* At this point we have established that the statement is of the
88566  ** correct syntactic form to participate in this optimization.  Now
88567  ** we have to check the semantics.
88568  */
88569  pItem = pSelect->pSrc->a;
88570  pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
88571  if( pSrc==0 ){
88572    return 0;   /* FROM clause does not contain a real table */
88573  }
88574  if( pSrc==pDest ){
88575    return 0;   /* tab1 and tab2 may not be the same table */
88576  }
88577#ifndef SQLITE_OMIT_VIRTUALTABLE
88578  if( pSrc->tabFlags & TF_Virtual ){
88579    return 0;   /* tab2 must not be a virtual table */
88580  }
88581#endif
88582  if( pSrc->pSelect ){
88583    return 0;   /* tab2 may not be a view */
88584  }
88585  if( pDest->nCol!=pSrc->nCol ){
88586    return 0;   /* Number of columns must be the same in tab1 and tab2 */
88587  }
88588  if( pDest->iPKey!=pSrc->iPKey ){
88589    return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
88590  }
88591  for(i=0; i<pDest->nCol; i++){
88592    if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
88593      return 0;    /* Affinity must be the same on all columns */
88594    }
88595    if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
88596      return 0;    /* Collating sequence must be the same on all columns */
88597    }
88598    if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
88599      return 0;    /* tab2 must be NOT NULL if tab1 is */
88600    }
88601  }
88602  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
88603    if( pDestIdx->onError!=OE_None ){
88604      destHasUniqueIdx = 1;
88605    }
88606    for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
88607      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
88608    }
88609    if( pSrcIdx==0 ){
88610      return 0;    /* pDestIdx has no corresponding index in pSrc */
88611    }
88612  }
88613#ifndef SQLITE_OMIT_CHECK
88614  if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
88615    return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
88616  }
88617#endif
88618#ifndef SQLITE_OMIT_FOREIGN_KEY
88619  /* Disallow the transfer optimization if the destination table constains
88620  ** any foreign key constraints.  This is more restrictive than necessary.
88621  ** But the main beneficiary of the transfer optimization is the VACUUM
88622  ** command, and the VACUUM command disables foreign key constraints.  So
88623  ** the extra complication to make this rule less restrictive is probably
88624  ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
88625  */
88626  if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
88627    return 0;
88628  }
88629#endif
88630
88631  /* If we get this far, it means either:
88632  **
88633  **    *   We can always do the transfer if the table contains an
88634  **        an integer primary key
88635  **
88636  **    *   We can conditionally do the transfer if the destination
88637  **        table is empty.
88638  */
88639#ifdef SQLITE_TEST
88640  sqlite3_xferopt_count++;
88641#endif
88642  iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
88643  v = sqlite3GetVdbe(pParse);
88644  sqlite3CodeVerifySchema(pParse, iDbSrc);
88645  iSrc = pParse->nTab++;
88646  iDest = pParse->nTab++;
88647  regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
88648  sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
88649  if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
88650    /* If tables do not have an INTEGER PRIMARY KEY and there
88651    ** are indices to be copied and the destination is not empty,
88652    ** we have to disallow the transfer optimization because the
88653    ** the rowids might change which will mess up indexing.
88654    **
88655    ** Or if the destination has a UNIQUE index and is not empty,
88656    ** we also disallow the transfer optimization because we cannot
88657    ** insure that all entries in the union of DEST and SRC will be
88658    ** unique.
88659    */
88660    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
88661    emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
88662    sqlite3VdbeJumpHere(v, addr1);
88663  }else{
88664    emptyDestTest = 0;
88665  }
88666  sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
88667  emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
88668  regData = sqlite3GetTempReg(pParse);
88669  regRowid = sqlite3GetTempReg(pParse);
88670  if( pDest->iPKey>=0 ){
88671    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
88672    addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
88673    sqlite3HaltConstraint(
88674        pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
88675    sqlite3VdbeJumpHere(v, addr2);
88676    autoIncStep(pParse, regAutoinc, regRowid);
88677  }else if( pDest->pIndex==0 ){
88678    addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
88679  }else{
88680    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
88681    assert( (pDest->tabFlags & TF_Autoincrement)==0 );
88682  }
88683  sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
88684  sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
88685  sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
88686  sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
88687  sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
88688  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
88689    for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
88690      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
88691    }
88692    assert( pSrcIdx );
88693    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
88694    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
88695    pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
88696    sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
88697                      (char*)pKey, P4_KEYINFO_HANDOFF);
88698    VdbeComment((v, "%s", pSrcIdx->zName));
88699    pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
88700    sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
88701                      (char*)pKey, P4_KEYINFO_HANDOFF);
88702    VdbeComment((v, "%s", pDestIdx->zName));
88703    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
88704    sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
88705    sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
88706    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
88707    sqlite3VdbeJumpHere(v, addr1);
88708  }
88709  sqlite3VdbeJumpHere(v, emptySrcTest);
88710  sqlite3ReleaseTempReg(pParse, regRowid);
88711  sqlite3ReleaseTempReg(pParse, regData);
88712  sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
88713  sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
88714  if( emptyDestTest ){
88715    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
88716    sqlite3VdbeJumpHere(v, emptyDestTest);
88717    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
88718    return 0;
88719  }else{
88720    return 1;
88721  }
88722}
88723#endif /* SQLITE_OMIT_XFER_OPT */
88724
88725/************** End of insert.c **********************************************/
88726/************** Begin file legacy.c ******************************************/
88727/*
88728** 2001 September 15
88729**
88730** The author disclaims copyright to this source code.  In place of
88731** a legal notice, here is a blessing:
88732**
88733**    May you do good and not evil.
88734**    May you find forgiveness for yourself and forgive others.
88735**    May you share freely, never taking more than you give.
88736**
88737*************************************************************************
88738** Main file for the SQLite library.  The routines in this file
88739** implement the programmer interface to the library.  Routines in
88740** other files are for internal use by SQLite and should not be
88741** accessed by users of the library.
88742*/
88743
88744
88745/*
88746** Execute SQL code.  Return one of the SQLITE_ success/failure
88747** codes.  Also write an error message into memory obtained from
88748** malloc() and make *pzErrMsg point to that message.
88749**
88750** If the SQL is a query, then for each row in the query result
88751** the xCallback() function is called.  pArg becomes the first
88752** argument to xCallback().  If xCallback=NULL then no callback
88753** is invoked, even for queries.
88754*/
88755SQLITE_API int sqlite3_exec(
88756  sqlite3 *db,                /* The database on which the SQL executes */
88757  const char *zSql,           /* The SQL to be executed */
88758  sqlite3_callback xCallback, /* Invoke this callback routine */
88759  void *pArg,                 /* First argument to xCallback() */
88760  char **pzErrMsg             /* Write error messages here */
88761){
88762  int rc = SQLITE_OK;         /* Return code */
88763  const char *zLeftover;      /* Tail of unprocessed SQL */
88764  sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
88765  char **azCols = 0;          /* Names of result columns */
88766  int nRetry = 0;             /* Number of retry attempts */
88767  int callbackIsInit;         /* True if callback data is initialized */
88768
88769  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
88770  if( zSql==0 ) zSql = "";
88771
88772  sqlite3_mutex_enter(db->mutex);
88773  sqlite3Error(db, SQLITE_OK, 0);
88774  while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
88775    int nCol;
88776    char **azVals = 0;
88777
88778    pStmt = 0;
88779    rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
88780    assert( rc==SQLITE_OK || pStmt==0 );
88781    if( rc!=SQLITE_OK ){
88782      continue;
88783    }
88784    if( !pStmt ){
88785      /* this happens for a comment or white-space */
88786      zSql = zLeftover;
88787      continue;
88788    }
88789
88790    callbackIsInit = 0;
88791    nCol = sqlite3_column_count(pStmt);
88792
88793    while( 1 ){
88794      int i;
88795      rc = sqlite3_step(pStmt);
88796
88797      /* Invoke the callback function if required */
88798      if( xCallback && (SQLITE_ROW==rc ||
88799          (SQLITE_DONE==rc && !callbackIsInit
88800                           && db->flags&SQLITE_NullCallback)) ){
88801        if( !callbackIsInit ){
88802          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
88803          if( azCols==0 ){
88804            goto exec_out;
88805          }
88806          for(i=0; i<nCol; i++){
88807            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
88808            /* sqlite3VdbeSetColName() installs column names as UTF8
88809            ** strings so there is no way for sqlite3_column_name() to fail. */
88810            assert( azCols[i]!=0 );
88811          }
88812          callbackIsInit = 1;
88813        }
88814        if( rc==SQLITE_ROW ){
88815          azVals = &azCols[nCol];
88816          for(i=0; i<nCol; i++){
88817            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
88818            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
88819              db->mallocFailed = 1;
88820              goto exec_out;
88821            }
88822          }
88823        }
88824        if( xCallback(pArg, nCol, azVals, azCols) ){
88825          rc = SQLITE_ABORT;
88826          sqlite3VdbeFinalize((Vdbe *)pStmt);
88827          pStmt = 0;
88828          sqlite3Error(db, SQLITE_ABORT, 0);
88829          goto exec_out;
88830        }
88831      }
88832
88833      if( rc!=SQLITE_ROW ){
88834        rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
88835        pStmt = 0;
88836        if( rc!=SQLITE_SCHEMA ){
88837          nRetry = 0;
88838          zSql = zLeftover;
88839          while( sqlite3Isspace(zSql[0]) ) zSql++;
88840        }
88841        break;
88842      }
88843    }
88844
88845    sqlite3DbFree(db, azCols);
88846    azCols = 0;
88847  }
88848
88849exec_out:
88850  if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
88851  sqlite3DbFree(db, azCols);
88852
88853  rc = sqlite3ApiExit(db, rc);
88854  if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
88855    int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
88856    *pzErrMsg = sqlite3Malloc(nErrMsg);
88857    if( *pzErrMsg ){
88858      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
88859    }else{
88860      rc = SQLITE_NOMEM;
88861      sqlite3Error(db, SQLITE_NOMEM, 0);
88862    }
88863  }else if( pzErrMsg ){
88864    *pzErrMsg = 0;
88865  }
88866
88867  assert( (rc&db->errMask)==rc );
88868  sqlite3_mutex_leave(db->mutex);
88869  return rc;
88870}
88871
88872/************** End of legacy.c **********************************************/
88873/************** Begin file loadext.c *****************************************/
88874/*
88875** 2006 June 7
88876**
88877** The author disclaims copyright to this source code.  In place of
88878** a legal notice, here is a blessing:
88879**
88880**    May you do good and not evil.
88881**    May you find forgiveness for yourself and forgive others.
88882**    May you share freely, never taking more than you give.
88883**
88884*************************************************************************
88885** This file contains code used to dynamically load extensions into
88886** the SQLite library.
88887*/
88888
88889#ifndef SQLITE_CORE
88890  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
88891#endif
88892/************** Include sqlite3ext.h in the middle of loadext.c **************/
88893/************** Begin file sqlite3ext.h **************************************/
88894/*
88895** 2006 June 7
88896**
88897** The author disclaims copyright to this source code.  In place of
88898** a legal notice, here is a blessing:
88899**
88900**    May you do good and not evil.
88901**    May you find forgiveness for yourself and forgive others.
88902**    May you share freely, never taking more than you give.
88903**
88904*************************************************************************
88905** This header file defines the SQLite interface for use by
88906** shared libraries that want to be imported as extensions into
88907** an SQLite instance.  Shared libraries that intend to be loaded
88908** as extensions by SQLite should #include this file instead of
88909** sqlite3.h.
88910*/
88911#ifndef _SQLITE3EXT_H_
88912#define _SQLITE3EXT_H_
88913
88914typedef struct sqlite3_api_routines sqlite3_api_routines;
88915
88916/*
88917** The following structure holds pointers to all of the SQLite API
88918** routines.
88919**
88920** WARNING:  In order to maintain backwards compatibility, add new
88921** interfaces to the end of this structure only.  If you insert new
88922** interfaces in the middle of this structure, then older different
88923** versions of SQLite will not be able to load each others' shared
88924** libraries!
88925*/
88926struct sqlite3_api_routines {
88927  void * (*aggregate_context)(sqlite3_context*,int nBytes);
88928  int  (*aggregate_count)(sqlite3_context*);
88929  int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
88930  int  (*bind_double)(sqlite3_stmt*,int,double);
88931  int  (*bind_int)(sqlite3_stmt*,int,int);
88932  int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
88933  int  (*bind_null)(sqlite3_stmt*,int);
88934  int  (*bind_parameter_count)(sqlite3_stmt*);
88935  int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
88936  const char * (*bind_parameter_name)(sqlite3_stmt*,int);
88937  int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
88938  int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
88939  int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
88940  int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
88941  int  (*busy_timeout)(sqlite3*,int ms);
88942  int  (*changes)(sqlite3*);
88943  int  (*close)(sqlite3*);
88944  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
88945  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
88946  const void * (*column_blob)(sqlite3_stmt*,int iCol);
88947  int  (*column_bytes)(sqlite3_stmt*,int iCol);
88948  int  (*column_bytes16)(sqlite3_stmt*,int iCol);
88949  int  (*column_count)(sqlite3_stmt*pStmt);
88950  const char * (*column_database_name)(sqlite3_stmt*,int);
88951  const void * (*column_database_name16)(sqlite3_stmt*,int);
88952  const char * (*column_decltype)(sqlite3_stmt*,int i);
88953  const void * (*column_decltype16)(sqlite3_stmt*,int);
88954  double  (*column_double)(sqlite3_stmt*,int iCol);
88955  int  (*column_int)(sqlite3_stmt*,int iCol);
88956  sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
88957  const char * (*column_name)(sqlite3_stmt*,int);
88958  const void * (*column_name16)(sqlite3_stmt*,int);
88959  const char * (*column_origin_name)(sqlite3_stmt*,int);
88960  const void * (*column_origin_name16)(sqlite3_stmt*,int);
88961  const char * (*column_table_name)(sqlite3_stmt*,int);
88962  const void * (*column_table_name16)(sqlite3_stmt*,int);
88963  const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
88964  const void * (*column_text16)(sqlite3_stmt*,int iCol);
88965  int  (*column_type)(sqlite3_stmt*,int iCol);
88966  sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
88967  void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
88968  int  (*complete)(const char*sql);
88969  int  (*complete16)(const void*sql);
88970  int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
88971  int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
88972  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*));
88973  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*));
88974  int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
88975  int  (*data_count)(sqlite3_stmt*pStmt);
88976  sqlite3 * (*db_handle)(sqlite3_stmt*);
88977  int (*declare_vtab)(sqlite3*,const char*);
88978  int  (*enable_shared_cache)(int);
88979  int  (*errcode)(sqlite3*db);
88980  const char * (*errmsg)(sqlite3*);
88981  const void * (*errmsg16)(sqlite3*);
88982  int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
88983  int  (*expired)(sqlite3_stmt*);
88984  int  (*finalize)(sqlite3_stmt*pStmt);
88985  void  (*free)(void*);
88986  void  (*free_table)(char**result);
88987  int  (*get_autocommit)(sqlite3*);
88988  void * (*get_auxdata)(sqlite3_context*,int);
88989  int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
88990  int  (*global_recover)(void);
88991  void  (*interruptx)(sqlite3*);
88992  sqlite_int64  (*last_insert_rowid)(sqlite3*);
88993  const char * (*libversion)(void);
88994  int  (*libversion_number)(void);
88995  void *(*malloc)(int);
88996  char * (*mprintf)(const char*,...);
88997  int  (*open)(const char*,sqlite3**);
88998  int  (*open16)(const void*,sqlite3**);
88999  int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
89000  int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
89001  void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
89002  void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
89003  void *(*realloc)(void*,int);
89004  int  (*reset)(sqlite3_stmt*pStmt);
89005  void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
89006  void  (*result_double)(sqlite3_context*,double);
89007  void  (*result_error)(sqlite3_context*,const char*,int);
89008  void  (*result_error16)(sqlite3_context*,const void*,int);
89009  void  (*result_int)(sqlite3_context*,int);
89010  void  (*result_int64)(sqlite3_context*,sqlite_int64);
89011  void  (*result_null)(sqlite3_context*);
89012  void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
89013  void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
89014  void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
89015  void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
89016  void  (*result_value)(sqlite3_context*,sqlite3_value*);
89017  void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
89018  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
89019  void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
89020  char * (*snprintf)(int,char*,const char*,...);
89021  int  (*step)(sqlite3_stmt*);
89022  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
89023  void  (*thread_cleanup)(void);
89024  int  (*total_changes)(sqlite3*);
89025  void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
89026  int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
89027  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
89028  void * (*user_data)(sqlite3_context*);
89029  const void * (*value_blob)(sqlite3_value*);
89030  int  (*value_bytes)(sqlite3_value*);
89031  int  (*value_bytes16)(sqlite3_value*);
89032  double  (*value_double)(sqlite3_value*);
89033  int  (*value_int)(sqlite3_value*);
89034  sqlite_int64  (*value_int64)(sqlite3_value*);
89035  int  (*value_numeric_type)(sqlite3_value*);
89036  const unsigned char * (*value_text)(sqlite3_value*);
89037  const void * (*value_text16)(sqlite3_value*);
89038  const void * (*value_text16be)(sqlite3_value*);
89039  const void * (*value_text16le)(sqlite3_value*);
89040  int  (*value_type)(sqlite3_value*);
89041  char *(*vmprintf)(const char*,va_list);
89042  /* Added ??? */
89043  int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
89044  /* Added by 3.3.13 */
89045  int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
89046  int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
89047  int (*clear_bindings)(sqlite3_stmt*);
89048  /* Added by 3.4.1 */
89049  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
89050  /* Added by 3.5.0 */
89051  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
89052  int (*blob_bytes)(sqlite3_blob*);
89053  int (*blob_close)(sqlite3_blob*);
89054  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
89055  int (*blob_read)(sqlite3_blob*,void*,int,int);
89056  int (*blob_write)(sqlite3_blob*,const void*,int,int);
89057  int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
89058  int (*file_control)(sqlite3*,const char*,int,void*);
89059  sqlite3_int64 (*memory_highwater)(int);
89060  sqlite3_int64 (*memory_used)(void);
89061  sqlite3_mutex *(*mutex_alloc)(int);
89062  void (*mutex_enter)(sqlite3_mutex*);
89063  void (*mutex_free)(sqlite3_mutex*);
89064  void (*mutex_leave)(sqlite3_mutex*);
89065  int (*mutex_try)(sqlite3_mutex*);
89066  int (*open_v2)(const char*,sqlite3**,int,const char*);
89067  int (*release_memory)(int);
89068  void (*result_error_nomem)(sqlite3_context*);
89069  void (*result_error_toobig)(sqlite3_context*);
89070  int (*sleep)(int);
89071  void (*soft_heap_limit)(int);
89072  sqlite3_vfs *(*vfs_find)(const char*);
89073  int (*vfs_register)(sqlite3_vfs*,int);
89074  int (*vfs_unregister)(sqlite3_vfs*);
89075  int (*xthreadsafe)(void);
89076  void (*result_zeroblob)(sqlite3_context*,int);
89077  void (*result_error_code)(sqlite3_context*,int);
89078  int (*test_control)(int, ...);
89079  void (*randomness)(int,void*);
89080  sqlite3 *(*context_db_handle)(sqlite3_context*);
89081  int (*extended_result_codes)(sqlite3*,int);
89082  int (*limit)(sqlite3*,int,int);
89083  sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
89084  const char *(*sql)(sqlite3_stmt*);
89085  int (*status)(int,int*,int*,int);
89086  int (*backup_finish)(sqlite3_backup*);
89087  sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
89088  int (*backup_pagecount)(sqlite3_backup*);
89089  int (*backup_remaining)(sqlite3_backup*);
89090  int (*backup_step)(sqlite3_backup*,int);
89091  const char *(*compileoption_get)(int);
89092  int (*compileoption_used)(const char*);
89093  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*));
89094  int (*db_config)(sqlite3*,int,...);
89095  sqlite3_mutex *(*db_mutex)(sqlite3*);
89096  int (*db_status)(sqlite3*,int,int*,int*,int);
89097  int (*extended_errcode)(sqlite3*);
89098  void (*log)(int,const char*,...);
89099  sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
89100  const char *(*sourceid)(void);
89101  int (*stmt_status)(sqlite3_stmt*,int,int);
89102  int (*strnicmp)(const char*,const char*,int);
89103  int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
89104  int (*wal_autocheckpoint)(sqlite3*,int);
89105  int (*wal_checkpoint)(sqlite3*,const char*);
89106  void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
89107  int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
89108  int (*vtab_config)(sqlite3*,int op,...);
89109  int (*vtab_on_conflict)(sqlite3*);
89110};
89111
89112/*
89113** The following macros redefine the API routines so that they are
89114** redirected throught the global sqlite3_api structure.
89115**
89116** This header file is also used by the loadext.c source file
89117** (part of the main SQLite library - not an extension) so that
89118** it can get access to the sqlite3_api_routines structure
89119** definition.  But the main library does not want to redefine
89120** the API.  So the redefinition macros are only valid if the
89121** SQLITE_CORE macros is undefined.
89122*/
89123#ifndef SQLITE_CORE
89124#define sqlite3_aggregate_context      sqlite3_api->aggregate_context
89125#ifndef SQLITE_OMIT_DEPRECATED
89126#define sqlite3_aggregate_count        sqlite3_api->aggregate_count
89127#endif
89128#define sqlite3_bind_blob              sqlite3_api->bind_blob
89129#define sqlite3_bind_double            sqlite3_api->bind_double
89130#define sqlite3_bind_int               sqlite3_api->bind_int
89131#define sqlite3_bind_int64             sqlite3_api->bind_int64
89132#define sqlite3_bind_null              sqlite3_api->bind_null
89133#define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
89134#define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
89135#define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
89136#define sqlite3_bind_text              sqlite3_api->bind_text
89137#define sqlite3_bind_text16            sqlite3_api->bind_text16
89138#define sqlite3_bind_value             sqlite3_api->bind_value
89139#define sqlite3_busy_handler           sqlite3_api->busy_handler
89140#define sqlite3_busy_timeout           sqlite3_api->busy_timeout
89141#define sqlite3_changes                sqlite3_api->changes
89142#define sqlite3_close                  sqlite3_api->close
89143#define sqlite3_collation_needed       sqlite3_api->collation_needed
89144#define sqlite3_collation_needed16     sqlite3_api->collation_needed16
89145#define sqlite3_column_blob            sqlite3_api->column_blob
89146#define sqlite3_column_bytes           sqlite3_api->column_bytes
89147#define sqlite3_column_bytes16         sqlite3_api->column_bytes16
89148#define sqlite3_column_count           sqlite3_api->column_count
89149#define sqlite3_column_database_name   sqlite3_api->column_database_name
89150#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
89151#define sqlite3_column_decltype        sqlite3_api->column_decltype
89152#define sqlite3_column_decltype16      sqlite3_api->column_decltype16
89153#define sqlite3_column_double          sqlite3_api->column_double
89154#define sqlite3_column_int             sqlite3_api->column_int
89155#define sqlite3_column_int64           sqlite3_api->column_int64
89156#define sqlite3_column_name            sqlite3_api->column_name
89157#define sqlite3_column_name16          sqlite3_api->column_name16
89158#define sqlite3_column_origin_name     sqlite3_api->column_origin_name
89159#define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
89160#define sqlite3_column_table_name      sqlite3_api->column_table_name
89161#define sqlite3_column_table_name16    sqlite3_api->column_table_name16
89162#define sqlite3_column_text            sqlite3_api->column_text
89163#define sqlite3_column_text16          sqlite3_api->column_text16
89164#define sqlite3_column_type            sqlite3_api->column_type
89165#define sqlite3_column_value           sqlite3_api->column_value
89166#define sqlite3_commit_hook            sqlite3_api->commit_hook
89167#define sqlite3_complete               sqlite3_api->complete
89168#define sqlite3_complete16             sqlite3_api->complete16
89169#define sqlite3_create_collation       sqlite3_api->create_collation
89170#define sqlite3_create_collation16     sqlite3_api->create_collation16
89171#define sqlite3_create_function        sqlite3_api->create_function
89172#define sqlite3_create_function16      sqlite3_api->create_function16
89173#define sqlite3_create_module          sqlite3_api->create_module
89174#define sqlite3_create_module_v2       sqlite3_api->create_module_v2
89175#define sqlite3_data_count             sqlite3_api->data_count
89176#define sqlite3_db_handle              sqlite3_api->db_handle
89177#define sqlite3_declare_vtab           sqlite3_api->declare_vtab
89178#define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
89179#define sqlite3_errcode                sqlite3_api->errcode
89180#define sqlite3_errmsg                 sqlite3_api->errmsg
89181#define sqlite3_errmsg16               sqlite3_api->errmsg16
89182#define sqlite3_exec                   sqlite3_api->exec
89183#ifndef SQLITE_OMIT_DEPRECATED
89184#define sqlite3_expired                sqlite3_api->expired
89185#endif
89186#define sqlite3_finalize               sqlite3_api->finalize
89187#define sqlite3_free                   sqlite3_api->free
89188#define sqlite3_free_table             sqlite3_api->free_table
89189#define sqlite3_get_autocommit         sqlite3_api->get_autocommit
89190#define sqlite3_get_auxdata            sqlite3_api->get_auxdata
89191#define sqlite3_get_table              sqlite3_api->get_table
89192#ifndef SQLITE_OMIT_DEPRECATED
89193#define sqlite3_global_recover         sqlite3_api->global_recover
89194#endif
89195#define sqlite3_interrupt              sqlite3_api->interruptx
89196#define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
89197#define sqlite3_libversion             sqlite3_api->libversion
89198#define sqlite3_libversion_number      sqlite3_api->libversion_number
89199#define sqlite3_malloc                 sqlite3_api->malloc
89200#define sqlite3_mprintf                sqlite3_api->mprintf
89201#define sqlite3_open                   sqlite3_api->open
89202#define sqlite3_open16                 sqlite3_api->open16
89203#define sqlite3_prepare                sqlite3_api->prepare
89204#define sqlite3_prepare16              sqlite3_api->prepare16
89205#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
89206#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
89207#define sqlite3_profile                sqlite3_api->profile
89208#define sqlite3_progress_handler       sqlite3_api->progress_handler
89209#define sqlite3_realloc                sqlite3_api->realloc
89210#define sqlite3_reset                  sqlite3_api->reset
89211#define sqlite3_result_blob            sqlite3_api->result_blob
89212#define sqlite3_result_double          sqlite3_api->result_double
89213#define sqlite3_result_error           sqlite3_api->result_error
89214#define sqlite3_result_error16         sqlite3_api->result_error16
89215#define sqlite3_result_int             sqlite3_api->result_int
89216#define sqlite3_result_int64           sqlite3_api->result_int64
89217#define sqlite3_result_null            sqlite3_api->result_null
89218#define sqlite3_result_text            sqlite3_api->result_text
89219#define sqlite3_result_text16          sqlite3_api->result_text16
89220#define sqlite3_result_text16be        sqlite3_api->result_text16be
89221#define sqlite3_result_text16le        sqlite3_api->result_text16le
89222#define sqlite3_result_value           sqlite3_api->result_value
89223#define sqlite3_rollback_hook          sqlite3_api->rollback_hook
89224#define sqlite3_set_authorizer         sqlite3_api->set_authorizer
89225#define sqlite3_set_auxdata            sqlite3_api->set_auxdata
89226#define sqlite3_snprintf               sqlite3_api->snprintf
89227#define sqlite3_step                   sqlite3_api->step
89228#define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
89229#define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
89230#define sqlite3_total_changes          sqlite3_api->total_changes
89231#define sqlite3_trace                  sqlite3_api->trace
89232#ifndef SQLITE_OMIT_DEPRECATED
89233#define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
89234#endif
89235#define sqlite3_update_hook            sqlite3_api->update_hook
89236#define sqlite3_user_data              sqlite3_api->user_data
89237#define sqlite3_value_blob             sqlite3_api->value_blob
89238#define sqlite3_value_bytes            sqlite3_api->value_bytes
89239#define sqlite3_value_bytes16          sqlite3_api->value_bytes16
89240#define sqlite3_value_double           sqlite3_api->value_double
89241#define sqlite3_value_int              sqlite3_api->value_int
89242#define sqlite3_value_int64            sqlite3_api->value_int64
89243#define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
89244#define sqlite3_value_text             sqlite3_api->value_text
89245#define sqlite3_value_text16           sqlite3_api->value_text16
89246#define sqlite3_value_text16be         sqlite3_api->value_text16be
89247#define sqlite3_value_text16le         sqlite3_api->value_text16le
89248#define sqlite3_value_type             sqlite3_api->value_type
89249#define sqlite3_vmprintf               sqlite3_api->vmprintf
89250#define sqlite3_overload_function      sqlite3_api->overload_function
89251#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
89252#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
89253#define sqlite3_clear_bindings         sqlite3_api->clear_bindings
89254#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
89255#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
89256#define sqlite3_blob_close             sqlite3_api->blob_close
89257#define sqlite3_blob_open              sqlite3_api->blob_open
89258#define sqlite3_blob_read              sqlite3_api->blob_read
89259#define sqlite3_blob_write             sqlite3_api->blob_write
89260#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
89261#define sqlite3_file_control           sqlite3_api->file_control
89262#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
89263#define sqlite3_memory_used            sqlite3_api->memory_used
89264#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
89265#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
89266#define sqlite3_mutex_free             sqlite3_api->mutex_free
89267#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
89268#define sqlite3_mutex_try              sqlite3_api->mutex_try
89269#define sqlite3_open_v2                sqlite3_api->open_v2
89270#define sqlite3_release_memory         sqlite3_api->release_memory
89271#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
89272#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
89273#define sqlite3_sleep                  sqlite3_api->sleep
89274#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
89275#define sqlite3_vfs_find               sqlite3_api->vfs_find
89276#define sqlite3_vfs_register           sqlite3_api->vfs_register
89277#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
89278#define sqlite3_threadsafe             sqlite3_api->xthreadsafe
89279#define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
89280#define sqlite3_result_error_code      sqlite3_api->result_error_code
89281#define sqlite3_test_control           sqlite3_api->test_control
89282#define sqlite3_randomness             sqlite3_api->randomness
89283#define sqlite3_context_db_handle      sqlite3_api->context_db_handle
89284#define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
89285#define sqlite3_limit                  sqlite3_api->limit
89286#define sqlite3_next_stmt              sqlite3_api->next_stmt
89287#define sqlite3_sql                    sqlite3_api->sql
89288#define sqlite3_status                 sqlite3_api->status
89289#define sqlite3_backup_finish          sqlite3_api->backup_finish
89290#define sqlite3_backup_init            sqlite3_api->backup_init
89291#define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
89292#define sqlite3_backup_remaining       sqlite3_api->backup_remaining
89293#define sqlite3_backup_step            sqlite3_api->backup_step
89294#define sqlite3_compileoption_get      sqlite3_api->compileoption_get
89295#define sqlite3_compileoption_used     sqlite3_api->compileoption_used
89296#define sqlite3_create_function_v2     sqlite3_api->create_function_v2
89297#define sqlite3_db_config              sqlite3_api->db_config
89298#define sqlite3_db_mutex               sqlite3_api->db_mutex
89299#define sqlite3_db_status              sqlite3_api->db_status
89300#define sqlite3_extended_errcode       sqlite3_api->extended_errcode
89301#define sqlite3_log                    sqlite3_api->log
89302#define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
89303#define sqlite3_sourceid               sqlite3_api->sourceid
89304#define sqlite3_stmt_status            sqlite3_api->stmt_status
89305#define sqlite3_strnicmp               sqlite3_api->strnicmp
89306#define sqlite3_unlock_notify          sqlite3_api->unlock_notify
89307#define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
89308#define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
89309#define sqlite3_wal_hook               sqlite3_api->wal_hook
89310#define sqlite3_blob_reopen            sqlite3_api->blob_reopen
89311#define sqlite3_vtab_config            sqlite3_api->vtab_config
89312#define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
89313#endif /* SQLITE_CORE */
89314
89315#define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
89316#define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
89317
89318#endif /* _SQLITE3EXT_H_ */
89319
89320/************** End of sqlite3ext.h ******************************************/
89321/************** Continuing where we left off in loadext.c ********************/
89322/* #include <string.h> */
89323
89324#ifndef SQLITE_OMIT_LOAD_EXTENSION
89325
89326/*
89327** Some API routines are omitted when various features are
89328** excluded from a build of SQLite.  Substitute a NULL pointer
89329** for any missing APIs.
89330*/
89331#ifndef SQLITE_ENABLE_COLUMN_METADATA
89332# define sqlite3_column_database_name   0
89333# define sqlite3_column_database_name16 0
89334# define sqlite3_column_table_name      0
89335# define sqlite3_column_table_name16    0
89336# define sqlite3_column_origin_name     0
89337# define sqlite3_column_origin_name16   0
89338# define sqlite3_table_column_metadata  0
89339#endif
89340
89341#ifdef SQLITE_OMIT_AUTHORIZATION
89342# define sqlite3_set_authorizer         0
89343#endif
89344
89345#ifdef SQLITE_OMIT_UTF16
89346# define sqlite3_bind_text16            0
89347# define sqlite3_collation_needed16     0
89348# define sqlite3_column_decltype16      0
89349# define sqlite3_column_name16          0
89350# define sqlite3_column_text16          0
89351# define sqlite3_complete16             0
89352# define sqlite3_create_collation16     0
89353# define sqlite3_create_function16      0
89354# define sqlite3_errmsg16               0
89355# define sqlite3_open16                 0
89356# define sqlite3_prepare16              0
89357# define sqlite3_prepare16_v2           0
89358# define sqlite3_result_error16         0
89359# define sqlite3_result_text16          0
89360# define sqlite3_result_text16be        0
89361# define sqlite3_result_text16le        0
89362# define sqlite3_value_text16           0
89363# define sqlite3_value_text16be         0
89364# define sqlite3_value_text16le         0
89365# define sqlite3_column_database_name16 0
89366# define sqlite3_column_table_name16    0
89367# define sqlite3_column_origin_name16   0
89368#endif
89369
89370#ifdef SQLITE_OMIT_COMPLETE
89371# define sqlite3_complete 0
89372# define sqlite3_complete16 0
89373#endif
89374
89375#ifdef SQLITE_OMIT_DECLTYPE
89376# define sqlite3_column_decltype16      0
89377# define sqlite3_column_decltype        0
89378#endif
89379
89380#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
89381# define sqlite3_progress_handler 0
89382#endif
89383
89384#ifdef SQLITE_OMIT_VIRTUALTABLE
89385# define sqlite3_create_module 0
89386# define sqlite3_create_module_v2 0
89387# define sqlite3_declare_vtab 0
89388# define sqlite3_vtab_config 0
89389# define sqlite3_vtab_on_conflict 0
89390#endif
89391
89392#ifdef SQLITE_OMIT_SHARED_CACHE
89393# define sqlite3_enable_shared_cache 0
89394#endif
89395
89396#ifdef SQLITE_OMIT_TRACE
89397# define sqlite3_profile       0
89398# define sqlite3_trace         0
89399#endif
89400
89401#ifdef SQLITE_OMIT_GET_TABLE
89402# define sqlite3_free_table    0
89403# define sqlite3_get_table     0
89404#endif
89405
89406#ifdef SQLITE_OMIT_INCRBLOB
89407#define sqlite3_bind_zeroblob  0
89408#define sqlite3_blob_bytes     0
89409#define sqlite3_blob_close     0
89410#define sqlite3_blob_open      0
89411#define sqlite3_blob_read      0
89412#define sqlite3_blob_write     0
89413#define sqlite3_blob_reopen    0
89414#endif
89415
89416/*
89417** The following structure contains pointers to all SQLite API routines.
89418** A pointer to this structure is passed into extensions when they are
89419** loaded so that the extension can make calls back into the SQLite
89420** library.
89421**
89422** When adding new APIs, add them to the bottom of this structure
89423** in order to preserve backwards compatibility.
89424**
89425** Extensions that use newer APIs should first call the
89426** sqlite3_libversion_number() to make sure that the API they
89427** intend to use is supported by the library.  Extensions should
89428** also check to make sure that the pointer to the function is
89429** not NULL before calling it.
89430*/
89431static const sqlite3_api_routines sqlite3Apis = {
89432  sqlite3_aggregate_context,
89433#ifndef SQLITE_OMIT_DEPRECATED
89434  sqlite3_aggregate_count,
89435#else
89436  0,
89437#endif
89438  sqlite3_bind_blob,
89439  sqlite3_bind_double,
89440  sqlite3_bind_int,
89441  sqlite3_bind_int64,
89442  sqlite3_bind_null,
89443  sqlite3_bind_parameter_count,
89444  sqlite3_bind_parameter_index,
89445  sqlite3_bind_parameter_name,
89446  sqlite3_bind_text,
89447  sqlite3_bind_text16,
89448  sqlite3_bind_value,
89449  sqlite3_busy_handler,
89450  sqlite3_busy_timeout,
89451  sqlite3_changes,
89452  sqlite3_close,
89453  sqlite3_collation_needed,
89454  sqlite3_collation_needed16,
89455  sqlite3_column_blob,
89456  sqlite3_column_bytes,
89457  sqlite3_column_bytes16,
89458  sqlite3_column_count,
89459  sqlite3_column_database_name,
89460  sqlite3_column_database_name16,
89461  sqlite3_column_decltype,
89462  sqlite3_column_decltype16,
89463  sqlite3_column_double,
89464  sqlite3_column_int,
89465  sqlite3_column_int64,
89466  sqlite3_column_name,
89467  sqlite3_column_name16,
89468  sqlite3_column_origin_name,
89469  sqlite3_column_origin_name16,
89470  sqlite3_column_table_name,
89471  sqlite3_column_table_name16,
89472  sqlite3_column_text,
89473  sqlite3_column_text16,
89474  sqlite3_column_type,
89475  sqlite3_column_value,
89476  sqlite3_commit_hook,
89477  sqlite3_complete,
89478  sqlite3_complete16,
89479  sqlite3_create_collation,
89480  sqlite3_create_collation16,
89481  sqlite3_create_function,
89482  sqlite3_create_function16,
89483  sqlite3_create_module,
89484  sqlite3_data_count,
89485  sqlite3_db_handle,
89486  sqlite3_declare_vtab,
89487  sqlite3_enable_shared_cache,
89488  sqlite3_errcode,
89489  sqlite3_errmsg,
89490  sqlite3_errmsg16,
89491  sqlite3_exec,
89492#ifndef SQLITE_OMIT_DEPRECATED
89493  sqlite3_expired,
89494#else
89495  0,
89496#endif
89497  sqlite3_finalize,
89498  sqlite3_free,
89499  sqlite3_free_table,
89500  sqlite3_get_autocommit,
89501  sqlite3_get_auxdata,
89502  sqlite3_get_table,
89503  0,     /* Was sqlite3_global_recover(), but that function is deprecated */
89504  sqlite3_interrupt,
89505  sqlite3_last_insert_rowid,
89506  sqlite3_libversion,
89507  sqlite3_libversion_number,
89508  sqlite3_malloc,
89509  sqlite3_mprintf,
89510  sqlite3_open,
89511  sqlite3_open16,
89512  sqlite3_prepare,
89513  sqlite3_prepare16,
89514  sqlite3_profile,
89515  sqlite3_progress_handler,
89516  sqlite3_realloc,
89517  sqlite3_reset,
89518  sqlite3_result_blob,
89519  sqlite3_result_double,
89520  sqlite3_result_error,
89521  sqlite3_result_error16,
89522  sqlite3_result_int,
89523  sqlite3_result_int64,
89524  sqlite3_result_null,
89525  sqlite3_result_text,
89526  sqlite3_result_text16,
89527  sqlite3_result_text16be,
89528  sqlite3_result_text16le,
89529  sqlite3_result_value,
89530  sqlite3_rollback_hook,
89531  sqlite3_set_authorizer,
89532  sqlite3_set_auxdata,
89533  sqlite3_snprintf,
89534  sqlite3_step,
89535  sqlite3_table_column_metadata,
89536#ifndef SQLITE_OMIT_DEPRECATED
89537  sqlite3_thread_cleanup,
89538#else
89539  0,
89540#endif
89541  sqlite3_total_changes,
89542  sqlite3_trace,
89543#ifndef SQLITE_OMIT_DEPRECATED
89544  sqlite3_transfer_bindings,
89545#else
89546  0,
89547#endif
89548  sqlite3_update_hook,
89549  sqlite3_user_data,
89550  sqlite3_value_blob,
89551  sqlite3_value_bytes,
89552  sqlite3_value_bytes16,
89553  sqlite3_value_double,
89554  sqlite3_value_int,
89555  sqlite3_value_int64,
89556  sqlite3_value_numeric_type,
89557  sqlite3_value_text,
89558  sqlite3_value_text16,
89559  sqlite3_value_text16be,
89560  sqlite3_value_text16le,
89561  sqlite3_value_type,
89562  sqlite3_vmprintf,
89563  /*
89564  ** The original API set ends here.  All extensions can call any
89565  ** of the APIs above provided that the pointer is not NULL.  But
89566  ** before calling APIs that follow, extension should check the
89567  ** sqlite3_libversion_number() to make sure they are dealing with
89568  ** a library that is new enough to support that API.
89569  *************************************************************************
89570  */
89571  sqlite3_overload_function,
89572
89573  /*
89574  ** Added after 3.3.13
89575  */
89576  sqlite3_prepare_v2,
89577  sqlite3_prepare16_v2,
89578  sqlite3_clear_bindings,
89579
89580  /*
89581  ** Added for 3.4.1
89582  */
89583  sqlite3_create_module_v2,
89584
89585  /*
89586  ** Added for 3.5.0
89587  */
89588  sqlite3_bind_zeroblob,
89589  sqlite3_blob_bytes,
89590  sqlite3_blob_close,
89591  sqlite3_blob_open,
89592  sqlite3_blob_read,
89593  sqlite3_blob_write,
89594  sqlite3_create_collation_v2,
89595  sqlite3_file_control,
89596  sqlite3_memory_highwater,
89597  sqlite3_memory_used,
89598#ifdef SQLITE_MUTEX_OMIT
89599  0,
89600  0,
89601  0,
89602  0,
89603  0,
89604#else
89605  sqlite3_mutex_alloc,
89606  sqlite3_mutex_enter,
89607  sqlite3_mutex_free,
89608  sqlite3_mutex_leave,
89609  sqlite3_mutex_try,
89610#endif
89611  sqlite3_open_v2,
89612  sqlite3_release_memory,
89613  sqlite3_result_error_nomem,
89614  sqlite3_result_error_toobig,
89615  sqlite3_sleep,
89616  sqlite3_soft_heap_limit,
89617  sqlite3_vfs_find,
89618  sqlite3_vfs_register,
89619  sqlite3_vfs_unregister,
89620
89621  /*
89622  ** Added for 3.5.8
89623  */
89624  sqlite3_threadsafe,
89625  sqlite3_result_zeroblob,
89626  sqlite3_result_error_code,
89627  sqlite3_test_control,
89628  sqlite3_randomness,
89629  sqlite3_context_db_handle,
89630
89631  /*
89632  ** Added for 3.6.0
89633  */
89634  sqlite3_extended_result_codes,
89635  sqlite3_limit,
89636  sqlite3_next_stmt,
89637  sqlite3_sql,
89638  sqlite3_status,
89639
89640  /*
89641  ** Added for 3.7.4
89642  */
89643  sqlite3_backup_finish,
89644  sqlite3_backup_init,
89645  sqlite3_backup_pagecount,
89646  sqlite3_backup_remaining,
89647  sqlite3_backup_step,
89648#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
89649  sqlite3_compileoption_get,
89650  sqlite3_compileoption_used,
89651#else
89652  0,
89653  0,
89654#endif
89655  sqlite3_create_function_v2,
89656  sqlite3_db_config,
89657  sqlite3_db_mutex,
89658  sqlite3_db_status,
89659  sqlite3_extended_errcode,
89660  sqlite3_log,
89661  sqlite3_soft_heap_limit64,
89662  sqlite3_sourceid,
89663  sqlite3_stmt_status,
89664  sqlite3_strnicmp,
89665#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
89666  sqlite3_unlock_notify,
89667#else
89668  0,
89669#endif
89670#ifndef SQLITE_OMIT_WAL
89671  sqlite3_wal_autocheckpoint,
89672  sqlite3_wal_checkpoint,
89673  sqlite3_wal_hook,
89674#else
89675  0,
89676  0,
89677  0,
89678#endif
89679  sqlite3_blob_reopen,
89680  sqlite3_vtab_config,
89681  sqlite3_vtab_on_conflict,
89682};
89683
89684/*
89685** Attempt to load an SQLite extension library contained in the file
89686** zFile.  The entry point is zProc.  zProc may be 0 in which case a
89687** default entry point name (sqlite3_extension_init) is used.  Use
89688** of the default name is recommended.
89689**
89690** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
89691**
89692** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
89693** error message text.  The calling function should free this memory
89694** by calling sqlite3DbFree(db, ).
89695*/
89696static int sqlite3LoadExtension(
89697  sqlite3 *db,          /* Load the extension into this database connection */
89698  const char *zFile,    /* Name of the shared library containing extension */
89699  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
89700  char **pzErrMsg       /* Put error message here if not 0 */
89701){
89702  sqlite3_vfs *pVfs = db->pVfs;
89703  void *handle;
89704  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
89705  char *zErrmsg = 0;
89706  void **aHandle;
89707  const int nMsg = 300;
89708
89709  if( pzErrMsg ) *pzErrMsg = 0;
89710
89711  /* Ticket #1863.  To avoid a creating security problems for older
89712  ** applications that relink against newer versions of SQLite, the
89713  ** ability to run load_extension is turned off by default.  One
89714  ** must call sqlite3_enable_load_extension() to turn on extension
89715  ** loading.  Otherwise you get the following error.
89716  */
89717  if( (db->flags & SQLITE_LoadExtension)==0 ){
89718    if( pzErrMsg ){
89719      *pzErrMsg = sqlite3_mprintf("not authorized");
89720    }
89721    return SQLITE_ERROR;
89722  }
89723
89724  if( zProc==0 ){
89725    zProc = "sqlite3_extension_init";
89726  }
89727
89728  handle = sqlite3OsDlOpen(pVfs, zFile);
89729  if( handle==0 ){
89730    if( pzErrMsg ){
89731      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
89732      if( zErrmsg ){
89733        sqlite3_snprintf(nMsg, zErrmsg,
89734            "unable to open shared library [%s]", zFile);
89735        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
89736      }
89737    }
89738    return SQLITE_ERROR;
89739  }
89740  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
89741                   sqlite3OsDlSym(pVfs, handle, zProc);
89742  if( xInit==0 ){
89743    if( pzErrMsg ){
89744      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
89745      if( zErrmsg ){
89746        sqlite3_snprintf(nMsg, zErrmsg,
89747            "no entry point [%s] in shared library [%s]", zProc,zFile);
89748        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
89749      }
89750      sqlite3OsDlClose(pVfs, handle);
89751    }
89752    return SQLITE_ERROR;
89753  }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
89754    if( pzErrMsg ){
89755      *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
89756    }
89757    sqlite3_free(zErrmsg);
89758    sqlite3OsDlClose(pVfs, handle);
89759    return SQLITE_ERROR;
89760  }
89761
89762  /* Append the new shared library handle to the db->aExtension array. */
89763  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
89764  if( aHandle==0 ){
89765    return SQLITE_NOMEM;
89766  }
89767  if( db->nExtension>0 ){
89768    memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
89769  }
89770  sqlite3DbFree(db, db->aExtension);
89771  db->aExtension = aHandle;
89772
89773  db->aExtension[db->nExtension++] = handle;
89774  return SQLITE_OK;
89775}
89776SQLITE_API int sqlite3_load_extension(
89777  sqlite3 *db,          /* Load the extension into this database connection */
89778  const char *zFile,    /* Name of the shared library containing extension */
89779  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
89780  char **pzErrMsg       /* Put error message here if not 0 */
89781){
89782  int rc;
89783  sqlite3_mutex_enter(db->mutex);
89784  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
89785  rc = sqlite3ApiExit(db, rc);
89786  sqlite3_mutex_leave(db->mutex);
89787  return rc;
89788}
89789
89790/*
89791** Call this routine when the database connection is closing in order
89792** to clean up loaded extensions
89793*/
89794SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
89795  int i;
89796  assert( sqlite3_mutex_held(db->mutex) );
89797  for(i=0; i<db->nExtension; i++){
89798    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
89799  }
89800  sqlite3DbFree(db, db->aExtension);
89801}
89802
89803/*
89804** Enable or disable extension loading.  Extension loading is disabled by
89805** default so as not to open security holes in older applications.
89806*/
89807SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
89808  sqlite3_mutex_enter(db->mutex);
89809  if( onoff ){
89810    db->flags |= SQLITE_LoadExtension;
89811  }else{
89812    db->flags &= ~SQLITE_LoadExtension;
89813  }
89814  sqlite3_mutex_leave(db->mutex);
89815  return SQLITE_OK;
89816}
89817
89818#endif /* SQLITE_OMIT_LOAD_EXTENSION */
89819
89820/*
89821** The auto-extension code added regardless of whether or not extension
89822** loading is supported.  We need a dummy sqlite3Apis pointer for that
89823** code if regular extension loading is not available.  This is that
89824** dummy pointer.
89825*/
89826#ifdef SQLITE_OMIT_LOAD_EXTENSION
89827static const sqlite3_api_routines sqlite3Apis = { 0 };
89828#endif
89829
89830
89831/*
89832** The following object holds the list of automatically loaded
89833** extensions.
89834**
89835** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
89836** mutex must be held while accessing this list.
89837*/
89838typedef struct sqlite3AutoExtList sqlite3AutoExtList;
89839static SQLITE_WSD struct sqlite3AutoExtList {
89840  int nExt;              /* Number of entries in aExt[] */
89841  void (**aExt)(void);   /* Pointers to the extension init functions */
89842} sqlite3Autoext = { 0, 0 };
89843
89844/* The "wsdAutoext" macro will resolve to the autoextension
89845** state vector.  If writable static data is unsupported on the target,
89846** we have to locate the state vector at run-time.  In the more common
89847** case where writable static data is supported, wsdStat can refer directly
89848** to the "sqlite3Autoext" state vector declared above.
89849*/
89850#ifdef SQLITE_OMIT_WSD
89851# define wsdAutoextInit \
89852  sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
89853# define wsdAutoext x[0]
89854#else
89855# define wsdAutoextInit
89856# define wsdAutoext sqlite3Autoext
89857#endif
89858
89859
89860/*
89861** Register a statically linked extension that is automatically
89862** loaded by every new database connection.
89863*/
89864SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
89865  int rc = SQLITE_OK;
89866#ifndef SQLITE_OMIT_AUTOINIT
89867  rc = sqlite3_initialize();
89868  if( rc ){
89869    return rc;
89870  }else
89871#endif
89872  {
89873    int i;
89874#if SQLITE_THREADSAFE
89875    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
89876#endif
89877    wsdAutoextInit;
89878    sqlite3_mutex_enter(mutex);
89879    for(i=0; i<wsdAutoext.nExt; i++){
89880      if( wsdAutoext.aExt[i]==xInit ) break;
89881    }
89882    if( i==wsdAutoext.nExt ){
89883      int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
89884      void (**aNew)(void);
89885      aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
89886      if( aNew==0 ){
89887        rc = SQLITE_NOMEM;
89888      }else{
89889        wsdAutoext.aExt = aNew;
89890        wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
89891        wsdAutoext.nExt++;
89892      }
89893    }
89894    sqlite3_mutex_leave(mutex);
89895    assert( (rc&0xff)==rc );
89896    return rc;
89897  }
89898}
89899
89900/*
89901** Reset the automatic extension loading mechanism.
89902*/
89903SQLITE_API void sqlite3_reset_auto_extension(void){
89904#ifndef SQLITE_OMIT_AUTOINIT
89905  if( sqlite3_initialize()==SQLITE_OK )
89906#endif
89907  {
89908#if SQLITE_THREADSAFE
89909    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
89910#endif
89911    wsdAutoextInit;
89912    sqlite3_mutex_enter(mutex);
89913    sqlite3_free(wsdAutoext.aExt);
89914    wsdAutoext.aExt = 0;
89915    wsdAutoext.nExt = 0;
89916    sqlite3_mutex_leave(mutex);
89917  }
89918}
89919
89920/*
89921** Load all automatic extensions.
89922**
89923** If anything goes wrong, set an error in the database connection.
89924*/
89925SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
89926  int i;
89927  int go = 1;
89928  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
89929
89930  wsdAutoextInit;
89931  if( wsdAutoext.nExt==0 ){
89932    /* Common case: early out without every having to acquire a mutex */
89933    return;
89934  }
89935  for(i=0; go; i++){
89936    char *zErrmsg;
89937#if SQLITE_THREADSAFE
89938    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
89939#endif
89940    sqlite3_mutex_enter(mutex);
89941    if( i>=wsdAutoext.nExt ){
89942      xInit = 0;
89943      go = 0;
89944    }else{
89945      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
89946              wsdAutoext.aExt[i];
89947    }
89948    sqlite3_mutex_leave(mutex);
89949    zErrmsg = 0;
89950    if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
89951      sqlite3Error(db, SQLITE_ERROR,
89952            "automatic extension loading failed: %s", zErrmsg);
89953      go = 0;
89954    }
89955    sqlite3_free(zErrmsg);
89956  }
89957}
89958
89959/************** End of loadext.c *********************************************/
89960/************** Begin file pragma.c ******************************************/
89961/*
89962** 2003 April 6
89963**
89964** The author disclaims copyright to this source code.  In place of
89965** a legal notice, here is a blessing:
89966**
89967**    May you do good and not evil.
89968**    May you find forgiveness for yourself and forgive others.
89969**    May you share freely, never taking more than you give.
89970**
89971*************************************************************************
89972** This file contains code used to implement the PRAGMA command.
89973*/
89974
89975/*
89976** Interpret the given string as a safety level.  Return 0 for OFF,
89977** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
89978** unrecognized string argument.
89979**
89980** Note that the values returned are one less that the values that
89981** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
89982** to support legacy SQL code.  The safety level used to be boolean
89983** and older scripts may have used numbers 0 for OFF and 1 for ON.
89984*/
89985static u8 getSafetyLevel(const char *z){
89986                             /* 123456789 123456789 */
89987  static const char zText[] = "onoffalseyestruefull";
89988  static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
89989  static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
89990  static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
89991  int i, n;
89992  if( sqlite3Isdigit(*z) ){
89993    return (u8)sqlite3Atoi(z);
89994  }
89995  n = sqlite3Strlen30(z);
89996  for(i=0; i<ArraySize(iLength); i++){
89997    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
89998      return iValue[i];
89999    }
90000  }
90001  return 1;
90002}
90003
90004/*
90005** Interpret the given string as a boolean value.
90006*/
90007SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z){
90008  return getSafetyLevel(z)&1;
90009}
90010
90011/* The sqlite3GetBoolean() function is used by other modules but the
90012** remainder of this file is specific to PRAGMA processing.  So omit
90013** the rest of the file if PRAGMAs are omitted from the build.
90014*/
90015#if !defined(SQLITE_OMIT_PRAGMA)
90016
90017/*
90018** Interpret the given string as a locking mode value.
90019*/
90020static int getLockingMode(const char *z){
90021  if( z ){
90022    if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
90023    if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
90024  }
90025  return PAGER_LOCKINGMODE_QUERY;
90026}
90027
90028#ifndef SQLITE_OMIT_AUTOVACUUM
90029/*
90030** Interpret the given string as an auto-vacuum mode value.
90031**
90032** The following strings, "none", "full" and "incremental" are
90033** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
90034*/
90035static int getAutoVacuum(const char *z){
90036  int i;
90037  if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
90038  if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
90039  if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
90040  i = sqlite3Atoi(z);
90041  return (u8)((i>=0&&i<=2)?i:0);
90042}
90043#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
90044
90045#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90046/*
90047** Interpret the given string as a temp db location. Return 1 for file
90048** backed temporary databases, 2 for the Red-Black tree in memory database
90049** and 0 to use the compile-time default.
90050*/
90051static int getTempStore(const char *z){
90052  if( z[0]>='0' && z[0]<='2' ){
90053    return z[0] - '0';
90054  }else if( sqlite3StrICmp(z, "file")==0 ){
90055    return 1;
90056  }else if( sqlite3StrICmp(z, "memory")==0 ){
90057    return 2;
90058  }else{
90059    return 0;
90060  }
90061}
90062#endif /* SQLITE_PAGER_PRAGMAS */
90063
90064#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90065/*
90066** Invalidate temp storage, either when the temp storage is changed
90067** from default, or when 'file' and the temp_store_directory has changed
90068*/
90069static int invalidateTempStorage(Parse *pParse){
90070  sqlite3 *db = pParse->db;
90071  if( db->aDb[1].pBt!=0 ){
90072    if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
90073      sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
90074        "from within a transaction");
90075      return SQLITE_ERROR;
90076    }
90077    sqlite3BtreeClose(db->aDb[1].pBt);
90078    db->aDb[1].pBt = 0;
90079    sqlite3ResetInternalSchema(db, -1);
90080  }
90081  return SQLITE_OK;
90082}
90083#endif /* SQLITE_PAGER_PRAGMAS */
90084
90085#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90086/*
90087** If the TEMP database is open, close it and mark the database schema
90088** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
90089** or DEFAULT_TEMP_STORE pragmas.
90090*/
90091static int changeTempStorage(Parse *pParse, const char *zStorageType){
90092  int ts = getTempStore(zStorageType);
90093  sqlite3 *db = pParse->db;
90094  if( db->temp_store==ts ) return SQLITE_OK;
90095  if( invalidateTempStorage( pParse ) != SQLITE_OK ){
90096    return SQLITE_ERROR;
90097  }
90098  db->temp_store = (u8)ts;
90099  return SQLITE_OK;
90100}
90101#endif /* SQLITE_PAGER_PRAGMAS */
90102
90103/*
90104** Generate code to return a single integer value.
90105*/
90106static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
90107  Vdbe *v = sqlite3GetVdbe(pParse);
90108  int mem = ++pParse->nMem;
90109  i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
90110  if( pI64 ){
90111    memcpy(pI64, &value, sizeof(value));
90112  }
90113  sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
90114  sqlite3VdbeSetNumCols(v, 1);
90115  sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
90116  sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
90117}
90118
90119#ifndef SQLITE_OMIT_FLAG_PRAGMAS
90120/*
90121** Check to see if zRight and zLeft refer to a pragma that queries
90122** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
90123** Also, implement the pragma.
90124*/
90125static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
90126  static const struct sPragmaType {
90127    const char *zName;  /* Name of the pragma */
90128    int mask;           /* Mask for the db->flags value */
90129  } aPragma[] = {
90130    { "full_column_names",        SQLITE_FullColNames  },
90131    { "short_column_names",       SQLITE_ShortColNames },
90132    { "count_changes",            SQLITE_CountRows     },
90133    { "empty_result_callbacks",   SQLITE_NullCallback  },
90134    { "legacy_file_format",       SQLITE_LegacyFileFmt },
90135    { "fullfsync",                SQLITE_FullFSync     },
90136    { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
90137    { "reverse_unordered_selects", SQLITE_ReverseOrder  },
90138#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
90139    { "automatic_index",          SQLITE_AutoIndex     },
90140#endif
90141#ifdef SQLITE_DEBUG
90142    { "sql_trace",                SQLITE_SqlTrace      },
90143    { "vdbe_listing",             SQLITE_VdbeListing   },
90144    { "vdbe_trace",               SQLITE_VdbeTrace     },
90145#endif
90146#ifndef SQLITE_OMIT_CHECK
90147    { "ignore_check_constraints", SQLITE_IgnoreChecks  },
90148#endif
90149    /* The following is VERY experimental */
90150    { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
90151    { "omit_readlock",            SQLITE_NoReadlock    },
90152
90153    /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
90154    ** flag if there are any active statements. */
90155    { "read_uncommitted",         SQLITE_ReadUncommitted },
90156    { "recursive_triggers",       SQLITE_RecTriggers },
90157
90158    /* This flag may only be set if both foreign-key and trigger support
90159    ** are present in the build.  */
90160#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
90161    { "foreign_keys",             SQLITE_ForeignKeys },
90162#endif
90163  };
90164  int i;
90165  const struct sPragmaType *p;
90166  for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
90167    if( sqlite3StrICmp(zLeft, p->zName)==0 ){
90168      sqlite3 *db = pParse->db;
90169      Vdbe *v;
90170      v = sqlite3GetVdbe(pParse);
90171      assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
90172      if( ALWAYS(v) ){
90173        if( zRight==0 ){
90174          returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
90175        }else{
90176          int mask = p->mask;          /* Mask of bits to set or clear. */
90177          if( db->autoCommit==0 ){
90178            /* Foreign key support may not be enabled or disabled while not
90179            ** in auto-commit mode.  */
90180            mask &= ~(SQLITE_ForeignKeys);
90181          }
90182
90183          if( sqlite3GetBoolean(zRight) ){
90184            db->flags |= mask;
90185          }else{
90186            db->flags &= ~mask;
90187          }
90188
90189          /* Many of the flag-pragmas modify the code generated by the SQL
90190          ** compiler (eg. count_changes). So add an opcode to expire all
90191          ** compiled SQL statements after modifying a pragma value.
90192          */
90193          sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
90194        }
90195      }
90196
90197      return 1;
90198    }
90199  }
90200  return 0;
90201}
90202#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
90203
90204/*
90205** Return a human-readable name for a constraint resolution action.
90206*/
90207#ifndef SQLITE_OMIT_FOREIGN_KEY
90208static const char *actionName(u8 action){
90209  const char *zName;
90210  switch( action ){
90211    case OE_SetNull:  zName = "SET NULL";        break;
90212    case OE_SetDflt:  zName = "SET DEFAULT";     break;
90213    case OE_Cascade:  zName = "CASCADE";         break;
90214    case OE_Restrict: zName = "RESTRICT";        break;
90215    default:          zName = "NO ACTION";
90216                      assert( action==OE_None ); break;
90217  }
90218  return zName;
90219}
90220#endif
90221
90222
90223/*
90224** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
90225** defined in pager.h. This function returns the associated lowercase
90226** journal-mode name.
90227*/
90228SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
90229  static char * const azModeName[] = {
90230    "delete", "persist", "off", "truncate", "memory"
90231#ifndef SQLITE_OMIT_WAL
90232     , "wal"
90233#endif
90234  };
90235  assert( PAGER_JOURNALMODE_DELETE==0 );
90236  assert( PAGER_JOURNALMODE_PERSIST==1 );
90237  assert( PAGER_JOURNALMODE_OFF==2 );
90238  assert( PAGER_JOURNALMODE_TRUNCATE==3 );
90239  assert( PAGER_JOURNALMODE_MEMORY==4 );
90240  assert( PAGER_JOURNALMODE_WAL==5 );
90241  assert( eMode>=0 && eMode<=ArraySize(azModeName) );
90242
90243  if( eMode==ArraySize(azModeName) ) return 0;
90244  return azModeName[eMode];
90245}
90246
90247/*
90248** Process a pragma statement.
90249**
90250** Pragmas are of this form:
90251**
90252**      PRAGMA [database.]id [= value]
90253**
90254** The identifier might also be a string.  The value is a string, and
90255** identifier, or a number.  If minusFlag is true, then the value is
90256** a number that was preceded by a minus sign.
90257**
90258** If the left side is "database.id" then pId1 is the database name
90259** and pId2 is the id.  If the left side is just "id" then pId1 is the
90260** id and pId2 is any empty string.
90261*/
90262SQLITE_PRIVATE void sqlite3Pragma(
90263  Parse *pParse,
90264  Token *pId1,        /* First part of [database.]id field */
90265  Token *pId2,        /* Second part of [database.]id field, or NULL */
90266  Token *pValue,      /* Token for <value>, or NULL */
90267  int minusFlag       /* True if a '-' sign preceded <value> */
90268){
90269  char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
90270  char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
90271  const char *zDb = 0;   /* The database name */
90272  Token *pId;            /* Pointer to <id> token */
90273  int iDb;               /* Database index for <database> */
90274  sqlite3 *db = pParse->db;
90275  Db *pDb;
90276  Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
90277  if( v==0 ) return;
90278  sqlite3VdbeRunOnlyOnce(v);
90279  pParse->nMem = 2;
90280
90281  /* Interpret the [database.] part of the pragma statement. iDb is the
90282  ** index of the database this pragma is being applied to in db.aDb[]. */
90283  iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
90284  if( iDb<0 ) return;
90285  pDb = &db->aDb[iDb];
90286
90287  /* If the temp database has been explicitly named as part of the
90288  ** pragma, make sure it is open.
90289  */
90290  if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
90291    return;
90292  }
90293
90294  zLeft = sqlite3NameFromToken(db, pId);
90295  if( !zLeft ) return;
90296  if( minusFlag ){
90297    zRight = sqlite3MPrintf(db, "-%T", pValue);
90298  }else{
90299    zRight = sqlite3NameFromToken(db, pValue);
90300  }
90301
90302  assert( pId2 );
90303  zDb = pId2->n>0 ? pDb->zName : 0;
90304  if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
90305    goto pragma_out;
90306  }
90307
90308#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90309  /*
90310  **  PRAGMA [database.]default_cache_size
90311  **  PRAGMA [database.]default_cache_size=N
90312  **
90313  ** The first form reports the current persistent setting for the
90314  ** page cache size.  The value returned is the maximum number of
90315  ** pages in the page cache.  The second form sets both the current
90316  ** page cache size value and the persistent page cache size value
90317  ** stored in the database file.
90318  **
90319  ** Older versions of SQLite would set the default cache size to a
90320  ** negative number to indicate synchronous=OFF.  These days, synchronous
90321  ** is always on by default regardless of the sign of the default cache
90322  ** size.  But continue to take the absolute value of the default cache
90323  ** size of historical compatibility.
90324  */
90325  if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
90326    static const VdbeOpList getCacheSize[] = {
90327      { OP_Transaction, 0, 0,        0},                         /* 0 */
90328      { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
90329      { OP_IfPos,       1, 7,        0},
90330      { OP_Integer,     0, 2,        0},
90331      { OP_Subtract,    1, 2,        1},
90332      { OP_IfPos,       1, 7,        0},
90333      { OP_Integer,     0, 1,        0},                         /* 6 */
90334      { OP_ResultRow,   1, 1,        0},
90335    };
90336    int addr;
90337    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90338    sqlite3VdbeUsesBtree(v, iDb);
90339    if( !zRight ){
90340      sqlite3VdbeSetNumCols(v, 1);
90341      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
90342      pParse->nMem += 2;
90343      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
90344      sqlite3VdbeChangeP1(v, addr, iDb);
90345      sqlite3VdbeChangeP1(v, addr+1, iDb);
90346      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
90347    }else{
90348      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
90349      sqlite3BeginWriteOperation(pParse, 0, iDb);
90350      sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
90351      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
90352      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90353      pDb->pSchema->cache_size = size;
90354      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
90355    }
90356  }else
90357
90358  /*
90359  **  PRAGMA [database.]page_size
90360  **  PRAGMA [database.]page_size=N
90361  **
90362  ** The first form reports the current setting for the
90363  ** database page size in bytes.  The second form sets the
90364  ** database page size value.  The value can only be set if
90365  ** the database has not yet been created.
90366  */
90367  if( sqlite3StrICmp(zLeft,"page_size")==0 ){
90368    Btree *pBt = pDb->pBt;
90369    assert( pBt!=0 );
90370    if( !zRight ){
90371      int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
90372      returnSingleInt(pParse, "page_size", size);
90373    }else{
90374      /* Malloc may fail when setting the page-size, as there is an internal
90375      ** buffer that the pager module resizes using sqlite3_realloc().
90376      */
90377      db->nextPagesize = sqlite3Atoi(zRight);
90378      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
90379        db->mallocFailed = 1;
90380      }
90381    }
90382  }else
90383
90384  /*
90385  **  PRAGMA [database.]secure_delete
90386  **  PRAGMA [database.]secure_delete=ON/OFF
90387  **
90388  ** The first form reports the current setting for the
90389  ** secure_delete flag.  The second form changes the secure_delete
90390  ** flag setting and reports thenew value.
90391  */
90392  if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
90393    Btree *pBt = pDb->pBt;
90394    int b = -1;
90395    assert( pBt!=0 );
90396    if( zRight ){
90397      b = sqlite3GetBoolean(zRight);
90398    }
90399    if( pId2->n==0 && b>=0 ){
90400      int ii;
90401      for(ii=0; ii<db->nDb; ii++){
90402        sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
90403      }
90404    }
90405    b = sqlite3BtreeSecureDelete(pBt, b);
90406    returnSingleInt(pParse, "secure_delete", b);
90407  }else
90408
90409  /*
90410  **  PRAGMA [database.]max_page_count
90411  **  PRAGMA [database.]max_page_count=N
90412  **
90413  ** The first form reports the current setting for the
90414  ** maximum number of pages in the database file.  The
90415  ** second form attempts to change this setting.  Both
90416  ** forms return the current setting.
90417  **
90418  **  PRAGMA [database.]page_count
90419  **
90420  ** Return the number of pages in the specified database.
90421  */
90422  if( sqlite3StrICmp(zLeft,"page_count")==0
90423   || sqlite3StrICmp(zLeft,"max_page_count")==0
90424  ){
90425    int iReg;
90426    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90427    sqlite3CodeVerifySchema(pParse, iDb);
90428    iReg = ++pParse->nMem;
90429    if( zLeft[0]=='p' ){
90430      sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
90431    }else{
90432      sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
90433    }
90434    sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
90435    sqlite3VdbeSetNumCols(v, 1);
90436    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
90437  }else
90438
90439  /*
90440  **  PRAGMA [database.]locking_mode
90441  **  PRAGMA [database.]locking_mode = (normal|exclusive)
90442  */
90443  if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
90444    const char *zRet = "normal";
90445    int eMode = getLockingMode(zRight);
90446
90447    if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
90448      /* Simple "PRAGMA locking_mode;" statement. This is a query for
90449      ** the current default locking mode (which may be different to
90450      ** the locking-mode of the main database).
90451      */
90452      eMode = db->dfltLockMode;
90453    }else{
90454      Pager *pPager;
90455      if( pId2->n==0 ){
90456        /* This indicates that no database name was specified as part
90457        ** of the PRAGMA command. In this case the locking-mode must be
90458        ** set on all attached databases, as well as the main db file.
90459        **
90460        ** Also, the sqlite3.dfltLockMode variable is set so that
90461        ** any subsequently attached databases also use the specified
90462        ** locking mode.
90463        */
90464        int ii;
90465        assert(pDb==&db->aDb[0]);
90466        for(ii=2; ii<db->nDb; ii++){
90467          pPager = sqlite3BtreePager(db->aDb[ii].pBt);
90468          sqlite3PagerLockingMode(pPager, eMode);
90469        }
90470        db->dfltLockMode = (u8)eMode;
90471      }
90472      pPager = sqlite3BtreePager(pDb->pBt);
90473      eMode = sqlite3PagerLockingMode(pPager, eMode);
90474    }
90475
90476    assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
90477    if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
90478      zRet = "exclusive";
90479    }
90480    sqlite3VdbeSetNumCols(v, 1);
90481    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
90482    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
90483    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
90484  }else
90485
90486  /*
90487  **  PRAGMA [database.]journal_mode
90488  **  PRAGMA [database.]journal_mode =
90489  **                      (delete|persist|off|truncate|memory|wal|off)
90490  */
90491  if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
90492    int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
90493    int ii;           /* Loop counter */
90494
90495    /* Force the schema to be loaded on all databases.  This cases all
90496    ** database files to be opened and the journal_modes set. */
90497    if( sqlite3ReadSchema(pParse) ){
90498      goto pragma_out;
90499    }
90500
90501    sqlite3VdbeSetNumCols(v, 1);
90502    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
90503
90504    if( zRight==0 ){
90505      /* If there is no "=MODE" part of the pragma, do a query for the
90506      ** current mode */
90507      eMode = PAGER_JOURNALMODE_QUERY;
90508    }else{
90509      const char *zMode;
90510      int n = sqlite3Strlen30(zRight);
90511      for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
90512        if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
90513      }
90514      if( !zMode ){
90515        /* If the "=MODE" part does not match any known journal mode,
90516        ** then do a query */
90517        eMode = PAGER_JOURNALMODE_QUERY;
90518      }
90519    }
90520    if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
90521      /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
90522      iDb = 0;
90523      pId2->n = 1;
90524    }
90525    for(ii=db->nDb-1; ii>=0; ii--){
90526      if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
90527        sqlite3VdbeUsesBtree(v, ii);
90528        sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
90529      }
90530    }
90531    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
90532  }else
90533
90534  /*
90535  **  PRAGMA [database.]journal_size_limit
90536  **  PRAGMA [database.]journal_size_limit=N
90537  **
90538  ** Get or set the size limit on rollback journal files.
90539  */
90540  if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
90541    Pager *pPager = sqlite3BtreePager(pDb->pBt);
90542    i64 iLimit = -2;
90543    if( zRight ){
90544      sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
90545      if( iLimit<-1 ) iLimit = -1;
90546    }
90547    iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
90548    returnSingleInt(pParse, "journal_size_limit", iLimit);
90549  }else
90550
90551#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
90552
90553  /*
90554  **  PRAGMA [database.]auto_vacuum
90555  **  PRAGMA [database.]auto_vacuum=N
90556  **
90557  ** Get or set the value of the database 'auto-vacuum' parameter.
90558  ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
90559  */
90560#ifndef SQLITE_OMIT_AUTOVACUUM
90561  if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
90562    Btree *pBt = pDb->pBt;
90563    assert( pBt!=0 );
90564    if( sqlite3ReadSchema(pParse) ){
90565      goto pragma_out;
90566    }
90567    if( !zRight ){
90568      int auto_vacuum;
90569      if( ALWAYS(pBt) ){
90570         auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
90571      }else{
90572         auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
90573      }
90574      returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
90575    }else{
90576      int eAuto = getAutoVacuum(zRight);
90577      assert( eAuto>=0 && eAuto<=2 );
90578      db->nextAutovac = (u8)eAuto;
90579      if( ALWAYS(eAuto>=0) ){
90580        /* Call SetAutoVacuum() to set initialize the internal auto and
90581        ** incr-vacuum flags. This is required in case this connection
90582        ** creates the database file. It is important that it is created
90583        ** as an auto-vacuum capable db.
90584        */
90585        int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
90586        if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
90587          /* When setting the auto_vacuum mode to either "full" or
90588          ** "incremental", write the value of meta[6] in the database
90589          ** file. Before writing to meta[6], check that meta[3] indicates
90590          ** that this really is an auto-vacuum capable database.
90591          */
90592          static const VdbeOpList setMeta6[] = {
90593            { OP_Transaction,    0,         1,                 0},    /* 0 */
90594            { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
90595            { OP_If,             1,         0,                 0},    /* 2 */
90596            { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
90597            { OP_Integer,        0,         1,                 0},    /* 4 */
90598            { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
90599          };
90600          int iAddr;
90601          iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
90602          sqlite3VdbeChangeP1(v, iAddr, iDb);
90603          sqlite3VdbeChangeP1(v, iAddr+1, iDb);
90604          sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
90605          sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
90606          sqlite3VdbeChangeP1(v, iAddr+5, iDb);
90607          sqlite3VdbeUsesBtree(v, iDb);
90608        }
90609      }
90610    }
90611  }else
90612#endif
90613
90614  /*
90615  **  PRAGMA [database.]incremental_vacuum(N)
90616  **
90617  ** Do N steps of incremental vacuuming on a database.
90618  */
90619#ifndef SQLITE_OMIT_AUTOVACUUM
90620  if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
90621    int iLimit, addr;
90622    if( sqlite3ReadSchema(pParse) ){
90623      goto pragma_out;
90624    }
90625    if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
90626      iLimit = 0x7fffffff;
90627    }
90628    sqlite3BeginWriteOperation(pParse, 0, iDb);
90629    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
90630    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
90631    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
90632    sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
90633    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
90634    sqlite3VdbeJumpHere(v, addr);
90635  }else
90636#endif
90637
90638#ifndef SQLITE_OMIT_PAGER_PRAGMAS
90639  /*
90640  **  PRAGMA [database.]cache_size
90641  **  PRAGMA [database.]cache_size=N
90642  **
90643  ** The first form reports the current local setting for the
90644  ** page cache size.  The local setting can be different from
90645  ** the persistent cache size value that is stored in the database
90646  ** file itself.  The value returned is the maximum number of
90647  ** pages in the page cache.  The second form sets the local
90648  ** page cache size value.  It does not change the persistent
90649  ** cache size stored on the disk so the cache size will revert
90650  ** to its default value when the database is closed and reopened.
90651  ** N should be a positive integer.
90652  */
90653  if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
90654    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90655    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90656    if( !zRight ){
90657      returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
90658    }else{
90659      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
90660      pDb->pSchema->cache_size = size;
90661      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
90662    }
90663  }else
90664
90665  /*
90666  **   PRAGMA temp_store
90667  **   PRAGMA temp_store = "default"|"memory"|"file"
90668  **
90669  ** Return or set the local value of the temp_store flag.  Changing
90670  ** the local value does not make changes to the disk file and the default
90671  ** value will be restored the next time the database is opened.
90672  **
90673  ** Note that it is possible for the library compile-time options to
90674  ** override this setting
90675  */
90676  if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
90677    if( !zRight ){
90678      returnSingleInt(pParse, "temp_store", db->temp_store);
90679    }else{
90680      changeTempStorage(pParse, zRight);
90681    }
90682  }else
90683
90684  /*
90685  **   PRAGMA temp_store_directory
90686  **   PRAGMA temp_store_directory = ""|"directory_name"
90687  **
90688  ** Return or set the local value of the temp_store_directory flag.  Changing
90689  ** the value sets a specific directory to be used for temporary files.
90690  ** Setting to a null string reverts to the default temporary directory search.
90691  ** If temporary directory is changed, then invalidateTempStorage.
90692  **
90693  */
90694  if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
90695    if( !zRight ){
90696      if( sqlite3_temp_directory ){
90697        sqlite3VdbeSetNumCols(v, 1);
90698        sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
90699            "temp_store_directory", SQLITE_STATIC);
90700        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
90701        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
90702      }
90703    }else{
90704#ifndef SQLITE_OMIT_WSD
90705      if( zRight[0] ){
90706        int rc;
90707        int res;
90708        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
90709        if( rc!=SQLITE_OK || res==0 ){
90710          sqlite3ErrorMsg(pParse, "not a writable directory");
90711          goto pragma_out;
90712        }
90713      }
90714      if( SQLITE_TEMP_STORE==0
90715       || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
90716       || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
90717      ){
90718        invalidateTempStorage(pParse);
90719      }
90720      sqlite3_free(sqlite3_temp_directory);
90721      if( zRight[0] ){
90722        sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
90723      }else{
90724        sqlite3_temp_directory = 0;
90725      }
90726#endif /* SQLITE_OMIT_WSD */
90727    }
90728  }else
90729
90730#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
90731#  if defined(__APPLE__)
90732#    define SQLITE_ENABLE_LOCKING_STYLE 1
90733#  else
90734#    define SQLITE_ENABLE_LOCKING_STYLE 0
90735#  endif
90736#endif
90737#if SQLITE_ENABLE_LOCKING_STYLE
90738  /*
90739   **   PRAGMA [database.]lock_proxy_file
90740   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
90741   **
90742   ** Return or set the value of the lock_proxy_file flag.  Changing
90743   ** the value sets a specific file to be used for database access locks.
90744   **
90745   */
90746  if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
90747    if( !zRight ){
90748      Pager *pPager = sqlite3BtreePager(pDb->pBt);
90749      char *proxy_file_path = NULL;
90750      sqlite3_file *pFile = sqlite3PagerFile(pPager);
90751      sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
90752                           &proxy_file_path);
90753
90754      if( proxy_file_path ){
90755        sqlite3VdbeSetNumCols(v, 1);
90756        sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
90757                              "lock_proxy_file", SQLITE_STATIC);
90758        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
90759        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
90760      }
90761    }else{
90762      Pager *pPager = sqlite3BtreePager(pDb->pBt);
90763      sqlite3_file *pFile = sqlite3PagerFile(pPager);
90764      int res;
90765      if( zRight[0] ){
90766        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
90767                                     zRight);
90768      } else {
90769        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
90770                                     NULL);
90771      }
90772      if( res!=SQLITE_OK ){
90773        sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
90774        goto pragma_out;
90775      }
90776    }
90777  }else
90778#endif /* SQLITE_ENABLE_LOCKING_STYLE */
90779
90780  /*
90781  **   PRAGMA [database.]synchronous
90782  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
90783  **
90784  ** Return or set the local value of the synchronous flag.  Changing
90785  ** the local value does not make changes to the disk file and the
90786  ** default value will be restored the next time the database is
90787  ** opened.
90788  */
90789  if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
90790    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90791    if( !zRight ){
90792      returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
90793    }else{
90794      if( !db->autoCommit ){
90795        sqlite3ErrorMsg(pParse,
90796            "Safety level may not be changed inside a transaction");
90797      }else{
90798        pDb->safety_level = getSafetyLevel(zRight)+1;
90799      }
90800    }
90801  }else
90802#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
90803
90804#ifndef SQLITE_OMIT_FLAG_PRAGMAS
90805  if( flagPragma(pParse, zLeft, zRight) ){
90806    /* The flagPragma() subroutine also generates any necessary code
90807    ** there is nothing more to do here */
90808  }else
90809#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
90810
90811#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
90812  /*
90813  **   PRAGMA table_info(<table>)
90814  **
90815  ** Return a single row for each column of the named table. The columns of
90816  ** the returned data set are:
90817  **
90818  ** cid:        Column id (numbered from left to right, starting at 0)
90819  ** name:       Column name
90820  ** type:       Column declaration type.
90821  ** notnull:    True if 'NOT NULL' is part of column declaration
90822  ** dflt_value: The default value for the column, if any.
90823  */
90824  if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
90825    Table *pTab;
90826    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90827    pTab = sqlite3FindTable(db, zRight, zDb);
90828    if( pTab ){
90829      int i;
90830      int nHidden = 0;
90831      Column *pCol;
90832      sqlite3VdbeSetNumCols(v, 6);
90833      pParse->nMem = 6;
90834      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
90835      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
90836      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
90837      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
90838      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
90839      sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
90840      sqlite3ViewGetColumnNames(pParse, pTab);
90841      for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
90842        if( IsHiddenColumn(pCol) ){
90843          nHidden++;
90844          continue;
90845        }
90846        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
90847        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
90848        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
90849           pCol->zType ? pCol->zType : "", 0);
90850        sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
90851        if( pCol->zDflt ){
90852          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
90853        }else{
90854          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
90855        }
90856        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
90857        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
90858      }
90859    }
90860  }else
90861
90862  if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
90863    Index *pIdx;
90864    Table *pTab;
90865    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90866    pIdx = sqlite3FindIndex(db, zRight, zDb);
90867    if( pIdx ){
90868      int i;
90869      pTab = pIdx->pTable;
90870      sqlite3VdbeSetNumCols(v, 3);
90871      pParse->nMem = 3;
90872      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
90873      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
90874      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
90875      for(i=0; i<pIdx->nColumn; i++){
90876        int cnum = pIdx->aiColumn[i];
90877        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
90878        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
90879        assert( pTab->nCol>cnum );
90880        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
90881        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
90882      }
90883    }
90884  }else
90885
90886  if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
90887    Index *pIdx;
90888    Table *pTab;
90889    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90890    pTab = sqlite3FindTable(db, zRight, zDb);
90891    if( pTab ){
90892      v = sqlite3GetVdbe(pParse);
90893      pIdx = pTab->pIndex;
90894      if( pIdx ){
90895        int i = 0;
90896        sqlite3VdbeSetNumCols(v, 3);
90897        pParse->nMem = 3;
90898        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
90899        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
90900        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
90901        while(pIdx){
90902          sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
90903          sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
90904          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
90905          sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
90906          ++i;
90907          pIdx = pIdx->pNext;
90908        }
90909      }
90910    }
90911  }else
90912
90913  if( sqlite3StrICmp(zLeft, "database_list")==0 ){
90914    int i;
90915    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90916    sqlite3VdbeSetNumCols(v, 3);
90917    pParse->nMem = 3;
90918    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
90919    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
90920    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
90921    for(i=0; i<db->nDb; i++){
90922      if( db->aDb[i].pBt==0 ) continue;
90923      assert( db->aDb[i].zName!=0 );
90924      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
90925      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
90926      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
90927           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
90928      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
90929    }
90930  }else
90931
90932  if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
90933    int i = 0;
90934    HashElem *p;
90935    sqlite3VdbeSetNumCols(v, 2);
90936    pParse->nMem = 2;
90937    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
90938    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
90939    for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
90940      CollSeq *pColl = (CollSeq *)sqliteHashData(p);
90941      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
90942      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
90943      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
90944    }
90945  }else
90946#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
90947
90948#ifndef SQLITE_OMIT_FOREIGN_KEY
90949  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
90950    FKey *pFK;
90951    Table *pTab;
90952    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90953    pTab = sqlite3FindTable(db, zRight, zDb);
90954    if( pTab ){
90955      v = sqlite3GetVdbe(pParse);
90956      pFK = pTab->pFKey;
90957      if( pFK ){
90958        int i = 0;
90959        sqlite3VdbeSetNumCols(v, 8);
90960        pParse->nMem = 8;
90961        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
90962        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
90963        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
90964        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
90965        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
90966        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
90967        sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
90968        sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
90969        while(pFK){
90970          int j;
90971          for(j=0; j<pFK->nCol; j++){
90972            char *zCol = pFK->aCol[j].zCol;
90973            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
90974            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
90975            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
90976            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
90977            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
90978            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
90979                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
90980            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
90981            sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
90982            sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
90983            sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
90984            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
90985          }
90986          ++i;
90987          pFK = pFK->pNextFrom;
90988        }
90989      }
90990    }
90991  }else
90992#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
90993
90994#ifndef NDEBUG
90995  if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
90996    if( zRight ){
90997      if( sqlite3GetBoolean(zRight) ){
90998        sqlite3ParserTrace(stderr, "parser: ");
90999      }else{
91000        sqlite3ParserTrace(0, 0);
91001      }
91002    }
91003  }else
91004#endif
91005
91006  /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
91007  ** used will be case sensitive or not depending on the RHS.
91008  */
91009  if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
91010    if( zRight ){
91011      sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight));
91012    }
91013  }else
91014
91015#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
91016# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
91017#endif
91018
91019#ifndef SQLITE_OMIT_INTEGRITY_CHECK
91020  /* Pragma "quick_check" is an experimental reduced version of
91021  ** integrity_check designed to detect most database corruption
91022  ** without most of the overhead of a full integrity-check.
91023  */
91024  if( sqlite3StrICmp(zLeft, "integrity_check")==0
91025   || sqlite3StrICmp(zLeft, "quick_check")==0
91026  ){
91027    int i, j, addr, mxErr;
91028
91029    /* Code that appears at the end of the integrity check.  If no error
91030    ** messages have been generated, output OK.  Otherwise output the
91031    ** error message
91032    */
91033    static const VdbeOpList endCode[] = {
91034      { OP_AddImm,      1, 0,        0},    /* 0 */
91035      { OP_IfNeg,       1, 0,        0},    /* 1 */
91036      { OP_String8,     0, 3,        0},    /* 2 */
91037      { OP_ResultRow,   3, 1,        0},
91038    };
91039
91040    int isQuick = (zLeft[0]=='q');
91041
91042    /* Initialize the VDBE program */
91043    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91044    pParse->nMem = 6;
91045    sqlite3VdbeSetNumCols(v, 1);
91046    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
91047
91048    /* Set the maximum error count */
91049    mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
91050    if( zRight ){
91051      sqlite3GetInt32(zRight, &mxErr);
91052      if( mxErr<=0 ){
91053        mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
91054      }
91055    }
91056    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
91057
91058    /* Do an integrity check on each database file */
91059    for(i=0; i<db->nDb; i++){
91060      HashElem *x;
91061      Hash *pTbls;
91062      int cnt = 0;
91063
91064      if( OMIT_TEMPDB && i==1 ) continue;
91065
91066      sqlite3CodeVerifySchema(pParse, i);
91067      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
91068      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
91069      sqlite3VdbeJumpHere(v, addr);
91070
91071      /* Do an integrity check of the B-Tree
91072      **
91073      ** Begin by filling registers 2, 3, ... with the root pages numbers
91074      ** for all tables and indices in the database.
91075      */
91076      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
91077      pTbls = &db->aDb[i].pSchema->tblHash;
91078      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
91079        Table *pTab = sqliteHashData(x);
91080        Index *pIdx;
91081        sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
91082        cnt++;
91083        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
91084          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
91085          cnt++;
91086        }
91087      }
91088
91089      /* Make sure sufficient number of registers have been allocated */
91090      if( pParse->nMem < cnt+4 ){
91091        pParse->nMem = cnt+4;
91092      }
91093
91094      /* Do the b-tree integrity checks */
91095      sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
91096      sqlite3VdbeChangeP5(v, (u8)i);
91097      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
91098      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
91099         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
91100         P4_DYNAMIC);
91101      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
91102      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
91103      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
91104      sqlite3VdbeJumpHere(v, addr);
91105
91106      /* Make sure all the indices are constructed correctly.
91107      */
91108      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
91109        Table *pTab = sqliteHashData(x);
91110        Index *pIdx;
91111        int loopTop;
91112
91113        if( pTab->pIndex==0 ) continue;
91114        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
91115        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
91116        sqlite3VdbeJumpHere(v, addr);
91117        sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
91118        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
91119        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
91120        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
91121        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
91122          int jmp2;
91123          int r1;
91124          static const VdbeOpList idxErr[] = {
91125            { OP_AddImm,      1, -1,  0},
91126            { OP_String8,     0,  3,  0},    /* 1 */
91127            { OP_Rowid,       1,  4,  0},
91128            { OP_String8,     0,  5,  0},    /* 3 */
91129            { OP_String8,     0,  6,  0},    /* 4 */
91130            { OP_Concat,      4,  3,  3},
91131            { OP_Concat,      5,  3,  3},
91132            { OP_Concat,      6,  3,  3},
91133            { OP_ResultRow,   3,  1,  0},
91134            { OP_IfPos,       1,  0,  0},    /* 9 */
91135            { OP_Halt,        0,  0,  0},
91136          };
91137          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
91138          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
91139          addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
91140          sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
91141          sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
91142          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
91143          sqlite3VdbeJumpHere(v, addr+9);
91144          sqlite3VdbeJumpHere(v, jmp2);
91145        }
91146        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
91147        sqlite3VdbeJumpHere(v, loopTop);
91148        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
91149          static const VdbeOpList cntIdx[] = {
91150             { OP_Integer,      0,  3,  0},
91151             { OP_Rewind,       0,  0,  0},  /* 1 */
91152             { OP_AddImm,       3,  1,  0},
91153             { OP_Next,         0,  0,  0},  /* 3 */
91154             { OP_Eq,           2,  0,  3},  /* 4 */
91155             { OP_AddImm,       1, -1,  0},
91156             { OP_String8,      0,  2,  0},  /* 6 */
91157             { OP_String8,      0,  3,  0},  /* 7 */
91158             { OP_Concat,       3,  2,  2},
91159             { OP_ResultRow,    2,  1,  0},
91160          };
91161          addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
91162          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
91163          sqlite3VdbeJumpHere(v, addr);
91164          addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
91165          sqlite3VdbeChangeP1(v, addr+1, j+2);
91166          sqlite3VdbeChangeP2(v, addr+1, addr+4);
91167          sqlite3VdbeChangeP1(v, addr+3, j+2);
91168          sqlite3VdbeChangeP2(v, addr+3, addr+2);
91169          sqlite3VdbeJumpHere(v, addr+4);
91170          sqlite3VdbeChangeP4(v, addr+6,
91171                     "wrong # of entries in index ", P4_STATIC);
91172          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
91173        }
91174      }
91175    }
91176    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
91177    sqlite3VdbeChangeP2(v, addr, -mxErr);
91178    sqlite3VdbeJumpHere(v, addr+1);
91179    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
91180  }else
91181#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
91182
91183#ifndef SQLITE_OMIT_UTF16
91184  /*
91185  **   PRAGMA encoding
91186  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
91187  **
91188  ** In its first form, this pragma returns the encoding of the main
91189  ** database. If the database is not initialized, it is initialized now.
91190  **
91191  ** The second form of this pragma is a no-op if the main database file
91192  ** has not already been initialized. In this case it sets the default
91193  ** encoding that will be used for the main database file if a new file
91194  ** is created. If an existing main database file is opened, then the
91195  ** default text encoding for the existing database is used.
91196  **
91197  ** In all cases new databases created using the ATTACH command are
91198  ** created to use the same default text encoding as the main database. If
91199  ** the main database has not been initialized and/or created when ATTACH
91200  ** is executed, this is done before the ATTACH operation.
91201  **
91202  ** In the second form this pragma sets the text encoding to be used in
91203  ** new database files created using this database handle. It is only
91204  ** useful if invoked immediately after the main database i
91205  */
91206  if( sqlite3StrICmp(zLeft, "encoding")==0 ){
91207    static const struct EncName {
91208      char *zName;
91209      u8 enc;
91210    } encnames[] = {
91211      { "UTF8",     SQLITE_UTF8        },
91212      { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
91213      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
91214      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
91215      { "UTF16le",  SQLITE_UTF16LE     },
91216      { "UTF16be",  SQLITE_UTF16BE     },
91217      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
91218      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
91219      { 0, 0 }
91220    };
91221    const struct EncName *pEnc;
91222    if( !zRight ){    /* "PRAGMA encoding" */
91223      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91224      sqlite3VdbeSetNumCols(v, 1);
91225      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
91226      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
91227      assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
91228      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
91229      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
91230      sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
91231      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
91232    }else{                        /* "PRAGMA encoding = XXX" */
91233      /* Only change the value of sqlite.enc if the database handle is not
91234      ** initialized. If the main database exists, the new sqlite.enc value
91235      ** will be overwritten when the schema is next loaded. If it does not
91236      ** already exists, it will be created to use the new encoding value.
91237      */
91238      if(
91239        !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
91240        DbHasProperty(db, 0, DB_Empty)
91241      ){
91242        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
91243          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
91244            ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
91245            break;
91246          }
91247        }
91248        if( !pEnc->zName ){
91249          sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
91250        }
91251      }
91252    }
91253  }else
91254#endif /* SQLITE_OMIT_UTF16 */
91255
91256#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
91257  /*
91258  **   PRAGMA [database.]schema_version
91259  **   PRAGMA [database.]schema_version = <integer>
91260  **
91261  **   PRAGMA [database.]user_version
91262  **   PRAGMA [database.]user_version = <integer>
91263  **
91264  ** The pragma's schema_version and user_version are used to set or get
91265  ** the value of the schema-version and user-version, respectively. Both
91266  ** the schema-version and the user-version are 32-bit signed integers
91267  ** stored in the database header.
91268  **
91269  ** The schema-cookie is usually only manipulated internally by SQLite. It
91270  ** is incremented by SQLite whenever the database schema is modified (by
91271  ** creating or dropping a table or index). The schema version is used by
91272  ** SQLite each time a query is executed to ensure that the internal cache
91273  ** of the schema used when compiling the SQL query matches the schema of
91274  ** the database against which the compiled query is actually executed.
91275  ** Subverting this mechanism by using "PRAGMA schema_version" to modify
91276  ** the schema-version is potentially dangerous and may lead to program
91277  ** crashes or database corruption. Use with caution!
91278  **
91279  ** The user-version is not used internally by SQLite. It may be used by
91280  ** applications for any purpose.
91281  */
91282  if( sqlite3StrICmp(zLeft, "schema_version")==0
91283   || sqlite3StrICmp(zLeft, "user_version")==0
91284   || sqlite3StrICmp(zLeft, "freelist_count")==0
91285  ){
91286    int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
91287    sqlite3VdbeUsesBtree(v, iDb);
91288    switch( zLeft[0] ){
91289      case 'f': case 'F':
91290        iCookie = BTREE_FREE_PAGE_COUNT;
91291        break;
91292      case 's': case 'S':
91293        iCookie = BTREE_SCHEMA_VERSION;
91294        break;
91295      default:
91296        iCookie = BTREE_USER_VERSION;
91297        break;
91298    }
91299
91300    if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
91301      /* Write the specified cookie value */
91302      static const VdbeOpList setCookie[] = {
91303        { OP_Transaction,    0,  1,  0},    /* 0 */
91304        { OP_Integer,        0,  1,  0},    /* 1 */
91305        { OP_SetCookie,      0,  0,  1},    /* 2 */
91306      };
91307      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
91308      sqlite3VdbeChangeP1(v, addr, iDb);
91309      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
91310      sqlite3VdbeChangeP1(v, addr+2, iDb);
91311      sqlite3VdbeChangeP2(v, addr+2, iCookie);
91312    }else{
91313      /* Read the specified cookie value */
91314      static const VdbeOpList readCookie[] = {
91315        { OP_Transaction,     0,  0,  0},    /* 0 */
91316        { OP_ReadCookie,      0,  1,  0},    /* 1 */
91317        { OP_ResultRow,       1,  1,  0}
91318      };
91319      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
91320      sqlite3VdbeChangeP1(v, addr, iDb);
91321      sqlite3VdbeChangeP1(v, addr+1, iDb);
91322      sqlite3VdbeChangeP3(v, addr+1, iCookie);
91323      sqlite3VdbeSetNumCols(v, 1);
91324      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
91325    }
91326  }else
91327#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
91328
91329#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
91330  /*
91331  **   PRAGMA compile_options
91332  **
91333  ** Return the names of all compile-time options used in this build,
91334  ** one option per row.
91335  */
91336  if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
91337    int i = 0;
91338    const char *zOpt;
91339    sqlite3VdbeSetNumCols(v, 1);
91340    pParse->nMem = 1;
91341    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
91342    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
91343      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
91344      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
91345    }
91346  }else
91347#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
91348
91349#ifndef SQLITE_OMIT_WAL
91350  /*
91351  **   PRAGMA [database.]wal_checkpoint = passive|full|restart
91352  **
91353  ** Checkpoint the database.
91354  */
91355  if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
91356    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
91357    int eMode = SQLITE_CHECKPOINT_PASSIVE;
91358    if( zRight ){
91359      if( sqlite3StrICmp(zRight, "full")==0 ){
91360        eMode = SQLITE_CHECKPOINT_FULL;
91361      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
91362        eMode = SQLITE_CHECKPOINT_RESTART;
91363      }
91364    }
91365    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91366    sqlite3VdbeSetNumCols(v, 3);
91367    pParse->nMem = 3;
91368    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
91369    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
91370    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
91371
91372    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
91373    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
91374  }else
91375
91376  /*
91377  **   PRAGMA wal_autocheckpoint
91378  **   PRAGMA wal_autocheckpoint = N
91379  **
91380  ** Configure a database connection to automatically checkpoint a database
91381  ** after accumulating N frames in the log. Or query for the current value
91382  ** of N.
91383  */
91384  if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
91385    if( zRight ){
91386      sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
91387    }
91388    returnSingleInt(pParse, "wal_autocheckpoint",
91389       db->xWalCallback==sqlite3WalDefaultHook ?
91390           SQLITE_PTR_TO_INT(db->pWalArg) : 0);
91391  }else
91392#endif
91393
91394#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
91395  /*
91396  ** Report the current state of file logs for all databases
91397  */
91398  if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
91399    static const char *const azLockName[] = {
91400      "unlocked", "shared", "reserved", "pending", "exclusive"
91401    };
91402    int i;
91403    sqlite3VdbeSetNumCols(v, 2);
91404    pParse->nMem = 2;
91405    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
91406    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
91407    for(i=0; i<db->nDb; i++){
91408      Btree *pBt;
91409      Pager *pPager;
91410      const char *zState = "unknown";
91411      int j;
91412      if( db->aDb[i].zName==0 ) continue;
91413      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
91414      pBt = db->aDb[i].pBt;
91415      if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
91416        zState = "closed";
91417      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
91418                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
91419         zState = azLockName[j];
91420      }
91421      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
91422      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
91423    }
91424
91425  }else
91426#endif
91427
91428#ifdef SQLITE_HAS_CODEC
91429  if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
91430    sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
91431  }else
91432  if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
91433    sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
91434  }else
91435  if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
91436                 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
91437    int i, h1, h2;
91438    char zKey[40];
91439    for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
91440      h1 += 9*(1&(h1>>6));
91441      h2 += 9*(1&(h2>>6));
91442      zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
91443    }
91444    if( (zLeft[3] & 0xf)==0xb ){
91445      sqlite3_key(db, zKey, i/2);
91446    }else{
91447      sqlite3_rekey(db, zKey, i/2);
91448    }
91449  }else
91450#endif
91451#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
91452  if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
91453#ifdef SQLITE_HAS_CODEC
91454    if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
91455      sqlite3_activate_see(&zRight[4]);
91456    }
91457#endif
91458#ifdef SQLITE_ENABLE_CEROD
91459    if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
91460      sqlite3_activate_cerod(&zRight[6]);
91461    }
91462#endif
91463  }else
91464#endif
91465
91466
91467  {/* Empty ELSE clause */}
91468
91469  /*
91470  ** Reset the safety level, in case the fullfsync flag or synchronous
91471  ** setting changed.
91472  */
91473#ifndef SQLITE_OMIT_PAGER_PRAGMAS
91474  if( db->autoCommit ){
91475    sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
91476               (db->flags&SQLITE_FullFSync)!=0,
91477               (db->flags&SQLITE_CkptFullFSync)!=0);
91478  }
91479#endif
91480pragma_out:
91481  sqlite3DbFree(db, zLeft);
91482  sqlite3DbFree(db, zRight);
91483}
91484
91485#endif /* SQLITE_OMIT_PRAGMA */
91486
91487/************** End of pragma.c **********************************************/
91488/************** Begin file prepare.c *****************************************/
91489/*
91490** 2005 May 25
91491**
91492** The author disclaims copyright to this source code.  In place of
91493** a legal notice, here is a blessing:
91494**
91495**    May you do good and not evil.
91496**    May you find forgiveness for yourself and forgive others.
91497**    May you share freely, never taking more than you give.
91498**
91499*************************************************************************
91500** This file contains the implementation of the sqlite3_prepare()
91501** interface, and routines that contribute to loading the database schema
91502** from disk.
91503*/
91504
91505/*
91506** Fill the InitData structure with an error message that indicates
91507** that the database is corrupt.
91508*/
91509static void corruptSchema(
91510  InitData *pData,     /* Initialization context */
91511  const char *zObj,    /* Object being parsed at the point of error */
91512  const char *zExtra   /* Error information */
91513){
91514  sqlite3 *db = pData->db;
91515  if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
91516    if( zObj==0 ) zObj = "?";
91517    sqlite3SetString(pData->pzErrMsg, db,
91518      "malformed database schema (%s)", zObj);
91519    if( zExtra ){
91520      *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
91521                                 "%s - %s", *pData->pzErrMsg, zExtra);
91522    }
91523  }
91524  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
91525}
91526
91527/*
91528** This is the callback routine for the code that initializes the
91529** database.  See sqlite3Init() below for additional information.
91530** This routine is also called from the OP_ParseSchema opcode of the VDBE.
91531**
91532** Each callback contains the following information:
91533**
91534**     argv[0] = name of thing being created
91535**     argv[1] = root page number for table or index. 0 for trigger or view.
91536**     argv[2] = SQL text for the CREATE statement.
91537**
91538*/
91539SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
91540  InitData *pData = (InitData*)pInit;
91541  sqlite3 *db = pData->db;
91542  int iDb = pData->iDb;
91543
91544  assert( argc==3 );
91545  UNUSED_PARAMETER2(NotUsed, argc);
91546  assert( sqlite3_mutex_held(db->mutex) );
91547  DbClearProperty(db, iDb, DB_Empty);
91548  if( db->mallocFailed ){
91549    corruptSchema(pData, argv[0], 0);
91550    return 1;
91551  }
91552
91553  assert( iDb>=0 && iDb<db->nDb );
91554  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
91555  if( argv[1]==0 ){
91556    corruptSchema(pData, argv[0], 0);
91557  }else if( argv[2] && argv[2][0] ){
91558    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
91559    ** But because db->init.busy is set to 1, no VDBE code is generated
91560    ** or executed.  All the parser does is build the internal data
91561    ** structures that describe the table, index, or view.
91562    */
91563    int rc;
91564    sqlite3_stmt *pStmt;
91565    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
91566
91567    assert( db->init.busy );
91568    db->init.iDb = iDb;
91569    db->init.newTnum = sqlite3Atoi(argv[1]);
91570    db->init.orphanTrigger = 0;
91571    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
91572    rc = db->errCode;
91573    assert( (rc&0xFF)==(rcp&0xFF) );
91574    db->init.iDb = 0;
91575    if( SQLITE_OK!=rc ){
91576      if( db->init.orphanTrigger ){
91577        assert( iDb==1 );
91578      }else{
91579        pData->rc = rc;
91580        if( rc==SQLITE_NOMEM ){
91581          db->mallocFailed = 1;
91582        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
91583          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
91584        }
91585      }
91586    }
91587    sqlite3_finalize(pStmt);
91588  }else if( argv[0]==0 ){
91589    corruptSchema(pData, 0, 0);
91590  }else{
91591    /* If the SQL column is blank it means this is an index that
91592    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
91593    ** constraint for a CREATE TABLE.  The index should have already
91594    ** been created when we processed the CREATE TABLE.  All we have
91595    ** to do here is record the root page number for that index.
91596    */
91597    Index *pIndex;
91598    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
91599    if( pIndex==0 ){
91600      /* This can occur if there exists an index on a TEMP table which
91601      ** has the same name as another index on a permanent index.  Since
91602      ** the permanent table is hidden by the TEMP table, we can also
91603      ** safely ignore the index on the permanent table.
91604      */
91605      /* Do Nothing */;
91606    }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
91607      corruptSchema(pData, argv[0], "invalid rootpage");
91608    }
91609  }
91610  return 0;
91611}
91612
91613/*
91614** Attempt to read the database schema and initialize internal
91615** data structures for a single database file.  The index of the
91616** database file is given by iDb.  iDb==0 is used for the main
91617** database.  iDb==1 should never be used.  iDb>=2 is used for
91618** auxiliary databases.  Return one of the SQLITE_ error codes to
91619** indicate success or failure.
91620*/
91621static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
91622  int rc;
91623  int i;
91624  int size;
91625  Table *pTab;
91626  Db *pDb;
91627  char const *azArg[4];
91628  int meta[5];
91629  InitData initData;
91630  char const *zMasterSchema;
91631  char const *zMasterName;
91632  int openedTransaction = 0;
91633
91634  /*
91635  ** The master database table has a structure like this
91636  */
91637  static const char master_schema[] =
91638     "CREATE TABLE sqlite_master(\n"
91639     "  type text,\n"
91640     "  name text,\n"
91641     "  tbl_name text,\n"
91642     "  rootpage integer,\n"
91643     "  sql text\n"
91644     ")"
91645  ;
91646#ifndef SQLITE_OMIT_TEMPDB
91647  static const char temp_master_schema[] =
91648     "CREATE TEMP TABLE sqlite_temp_master(\n"
91649     "  type text,\n"
91650     "  name text,\n"
91651     "  tbl_name text,\n"
91652     "  rootpage integer,\n"
91653     "  sql text\n"
91654     ")"
91655  ;
91656#else
91657  #define temp_master_schema 0
91658#endif
91659
91660  assert( iDb>=0 && iDb<db->nDb );
91661  assert( db->aDb[iDb].pSchema );
91662  assert( sqlite3_mutex_held(db->mutex) );
91663  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
91664
91665  /* zMasterSchema and zInitScript are set to point at the master schema
91666  ** and initialisation script appropriate for the database being
91667  ** initialised. zMasterName is the name of the master table.
91668  */
91669  if( !OMIT_TEMPDB && iDb==1 ){
91670    zMasterSchema = temp_master_schema;
91671  }else{
91672    zMasterSchema = master_schema;
91673  }
91674  zMasterName = SCHEMA_TABLE(iDb);
91675
91676  /* Construct the schema tables.  */
91677  azArg[0] = zMasterName;
91678  azArg[1] = "1";
91679  azArg[2] = zMasterSchema;
91680  azArg[3] = 0;
91681  initData.db = db;
91682  initData.iDb = iDb;
91683  initData.rc = SQLITE_OK;
91684  initData.pzErrMsg = pzErrMsg;
91685  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
91686  if( initData.rc ){
91687    rc = initData.rc;
91688    goto error_out;
91689  }
91690  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
91691  if( ALWAYS(pTab) ){
91692    pTab->tabFlags |= TF_Readonly;
91693  }
91694
91695  /* Create a cursor to hold the database open
91696  */
91697  pDb = &db->aDb[iDb];
91698  if( pDb->pBt==0 ){
91699    if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
91700      DbSetProperty(db, 1, DB_SchemaLoaded);
91701    }
91702    return SQLITE_OK;
91703  }
91704
91705  /* If there is not already a read-only (or read-write) transaction opened
91706  ** on the b-tree database, open one now. If a transaction is opened, it
91707  ** will be closed before this function returns.  */
91708  sqlite3BtreeEnter(pDb->pBt);
91709  if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
91710    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
91711    if( rc!=SQLITE_OK ){
91712      sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
91713      goto initone_error_out;
91714    }
91715    openedTransaction = 1;
91716  }
91717
91718  /* Get the database meta information.
91719  **
91720  ** Meta values are as follows:
91721  **    meta[0]   Schema cookie.  Changes with each schema change.
91722  **    meta[1]   File format of schema layer.
91723  **    meta[2]   Size of the page cache.
91724  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
91725  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
91726  **    meta[5]   User version
91727  **    meta[6]   Incremental vacuum mode
91728  **    meta[7]   unused
91729  **    meta[8]   unused
91730  **    meta[9]   unused
91731  **
91732  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
91733  ** the possible values of meta[4].
91734  */
91735  for(i=0; i<ArraySize(meta); i++){
91736    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
91737  }
91738  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
91739
91740  /* If opening a non-empty database, check the text encoding. For the
91741  ** main database, set sqlite3.enc to the encoding of the main database.
91742  ** For an attached db, it is an error if the encoding is not the same
91743  ** as sqlite3.enc.
91744  */
91745  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
91746    if( iDb==0 ){
91747      u8 encoding;
91748      /* If opening the main database, set ENC(db). */
91749      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
91750      if( encoding==0 ) encoding = SQLITE_UTF8;
91751      ENC(db) = encoding;
91752      db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
91753    }else{
91754      /* If opening an attached database, the encoding much match ENC(db) */
91755      if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
91756        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
91757            " text encoding as main database");
91758        rc = SQLITE_ERROR;
91759        goto initone_error_out;
91760      }
91761    }
91762  }else{
91763    DbSetProperty(db, iDb, DB_Empty);
91764  }
91765  pDb->pSchema->enc = ENC(db);
91766
91767  if( pDb->pSchema->cache_size==0 ){
91768    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
91769    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
91770    pDb->pSchema->cache_size = size;
91771    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
91772  }
91773
91774  /*
91775  ** file_format==1    Version 3.0.0.
91776  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
91777  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
91778  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
91779  */
91780  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
91781  if( pDb->pSchema->file_format==0 ){
91782    pDb->pSchema->file_format = 1;
91783  }
91784  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
91785    sqlite3SetString(pzErrMsg, db, "unsupported file format");
91786    rc = SQLITE_ERROR;
91787    goto initone_error_out;
91788  }
91789
91790  /* Ticket #2804:  When we open a database in the newer file format,
91791  ** clear the legacy_file_format pragma flag so that a VACUUM will
91792  ** not downgrade the database and thus invalidate any descending
91793  ** indices that the user might have created.
91794  */
91795  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
91796    db->flags &= ~SQLITE_LegacyFileFmt;
91797  }
91798
91799  /* Read the schema information out of the schema tables
91800  */
91801  assert( db->init.busy );
91802  {
91803    char *zSql;
91804    zSql = sqlite3MPrintf(db,
91805        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
91806        db->aDb[iDb].zName, zMasterName);
91807#ifndef SQLITE_OMIT_AUTHORIZATION
91808    {
91809      int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
91810      xAuth = db->xAuth;
91811      db->xAuth = 0;
91812#endif
91813      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
91814#ifndef SQLITE_OMIT_AUTHORIZATION
91815      db->xAuth = xAuth;
91816    }
91817#endif
91818    if( rc==SQLITE_OK ) rc = initData.rc;
91819    sqlite3DbFree(db, zSql);
91820#ifndef SQLITE_OMIT_ANALYZE
91821    if( rc==SQLITE_OK ){
91822      sqlite3AnalysisLoad(db, iDb);
91823    }
91824#endif
91825  }
91826  if( db->mallocFailed ){
91827    rc = SQLITE_NOMEM;
91828    sqlite3ResetInternalSchema(db, -1);
91829  }
91830  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
91831    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
91832    ** the schema loaded, even if errors occurred. In this situation the
91833    ** current sqlite3_prepare() operation will fail, but the following one
91834    ** will attempt to compile the supplied statement against whatever subset
91835    ** of the schema was loaded before the error occurred. The primary
91836    ** purpose of this is to allow access to the sqlite_master table
91837    ** even when its contents have been corrupted.
91838    */
91839    DbSetProperty(db, iDb, DB_SchemaLoaded);
91840    rc = SQLITE_OK;
91841  }
91842
91843  /* Jump here for an error that occurs after successfully allocating
91844  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
91845  ** before that point, jump to error_out.
91846  */
91847initone_error_out:
91848  if( openedTransaction ){
91849    sqlite3BtreeCommit(pDb->pBt);
91850  }
91851  sqlite3BtreeLeave(pDb->pBt);
91852
91853error_out:
91854  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
91855    db->mallocFailed = 1;
91856  }
91857  return rc;
91858}
91859
91860/*
91861** Initialize all database files - the main database file, the file
91862** used to store temporary tables, and any additional database files
91863** created using ATTACH statements.  Return a success code.  If an
91864** error occurs, write an error message into *pzErrMsg.
91865**
91866** After a database is initialized, the DB_SchemaLoaded bit is set
91867** bit is set in the flags field of the Db structure. If the database
91868** file was of zero-length, then the DB_Empty flag is also set.
91869*/
91870SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
91871  int i, rc;
91872  int commit_internal = !(db->flags&SQLITE_InternChanges);
91873
91874  assert( sqlite3_mutex_held(db->mutex) );
91875  rc = SQLITE_OK;
91876  db->init.busy = 1;
91877  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
91878    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
91879    rc = sqlite3InitOne(db, i, pzErrMsg);
91880    if( rc ){
91881      sqlite3ResetInternalSchema(db, i);
91882    }
91883  }
91884
91885  /* Once all the other databases have been initialised, load the schema
91886  ** for the TEMP database. This is loaded last, as the TEMP database
91887  ** schema may contain references to objects in other databases.
91888  */
91889#ifndef SQLITE_OMIT_TEMPDB
91890  if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
91891                    && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
91892    rc = sqlite3InitOne(db, 1, pzErrMsg);
91893    if( rc ){
91894      sqlite3ResetInternalSchema(db, 1);
91895    }
91896  }
91897#endif
91898
91899  db->init.busy = 0;
91900  if( rc==SQLITE_OK && commit_internal ){
91901    sqlite3CommitInternalChanges(db);
91902  }
91903
91904  return rc;
91905}
91906
91907/*
91908** This routine is a no-op if the database schema is already initialised.
91909** Otherwise, the schema is loaded. An error code is returned.
91910*/
91911SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
91912  int rc = SQLITE_OK;
91913  sqlite3 *db = pParse->db;
91914  assert( sqlite3_mutex_held(db->mutex) );
91915  if( !db->init.busy ){
91916    rc = sqlite3Init(db, &pParse->zErrMsg);
91917  }
91918  if( rc!=SQLITE_OK ){
91919    pParse->rc = rc;
91920    pParse->nErr++;
91921  }
91922  return rc;
91923}
91924
91925
91926/*
91927** Check schema cookies in all databases.  If any cookie is out
91928** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
91929** make no changes to pParse->rc.
91930*/
91931static void schemaIsValid(Parse *pParse){
91932  sqlite3 *db = pParse->db;
91933  int iDb;
91934  int rc;
91935  int cookie;
91936
91937  assert( pParse->checkSchema );
91938  assert( sqlite3_mutex_held(db->mutex) );
91939  for(iDb=0; iDb<db->nDb; iDb++){
91940    int openedTransaction = 0;         /* True if a transaction is opened */
91941    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
91942    if( pBt==0 ) continue;
91943
91944    /* If there is not already a read-only (or read-write) transaction opened
91945    ** on the b-tree database, open one now. If a transaction is opened, it
91946    ** will be closed immediately after reading the meta-value. */
91947    if( !sqlite3BtreeIsInReadTrans(pBt) ){
91948      rc = sqlite3BtreeBeginTrans(pBt, 0);
91949      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
91950        db->mallocFailed = 1;
91951      }
91952      if( rc!=SQLITE_OK ) return;
91953      openedTransaction = 1;
91954    }
91955
91956    /* Read the schema cookie from the database. If it does not match the
91957    ** value stored as part of the in-memory schema representation,
91958    ** set Parse.rc to SQLITE_SCHEMA. */
91959    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
91960    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
91961    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
91962      sqlite3ResetInternalSchema(db, iDb);
91963      pParse->rc = SQLITE_SCHEMA;
91964    }
91965
91966    /* Close the transaction, if one was opened. */
91967    if( openedTransaction ){
91968      sqlite3BtreeCommit(pBt);
91969    }
91970  }
91971}
91972
91973/*
91974** Convert a schema pointer into the iDb index that indicates
91975** which database file in db->aDb[] the schema refers to.
91976**
91977** If the same database is attached more than once, the first
91978** attached database is returned.
91979*/
91980SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
91981  int i = -1000000;
91982
91983  /* If pSchema is NULL, then return -1000000. This happens when code in
91984  ** expr.c is trying to resolve a reference to a transient table (i.e. one
91985  ** created by a sub-select). In this case the return value of this
91986  ** function should never be used.
91987  **
91988  ** We return -1000000 instead of the more usual -1 simply because using
91989  ** -1000000 as the incorrect index into db->aDb[] is much
91990  ** more likely to cause a segfault than -1 (of course there are assert()
91991  ** statements too, but it never hurts to play the odds).
91992  */
91993  assert( sqlite3_mutex_held(db->mutex) );
91994  if( pSchema ){
91995    for(i=0; ALWAYS(i<db->nDb); i++){
91996      if( db->aDb[i].pSchema==pSchema ){
91997        break;
91998      }
91999    }
92000    assert( i>=0 && i<db->nDb );
92001  }
92002  return i;
92003}
92004
92005/*
92006** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
92007*/
92008static int sqlite3Prepare(
92009  sqlite3 *db,              /* Database handle. */
92010  const char *zSql,         /* UTF-8 encoded SQL statement. */
92011  int nBytes,               /* Length of zSql in bytes. */
92012  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
92013  Vdbe *pReprepare,         /* VM being reprepared */
92014  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92015  const char **pzTail       /* OUT: End of parsed string */
92016){
92017  Parse *pParse;            /* Parsing context */
92018  char *zErrMsg = 0;        /* Error message */
92019  int rc = SQLITE_OK;       /* Result code */
92020  int i;                    /* Loop counter */
92021
92022  /* Allocate the parsing context */
92023  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
92024  if( pParse==0 ){
92025    rc = SQLITE_NOMEM;
92026    goto end_prepare;
92027  }
92028  pParse->pReprepare = pReprepare;
92029  assert( ppStmt && *ppStmt==0 );
92030  assert( !db->mallocFailed );
92031  assert( sqlite3_mutex_held(db->mutex) );
92032
92033  /* Check to verify that it is possible to get a read lock on all
92034  ** database schemas.  The inability to get a read lock indicates that
92035  ** some other database connection is holding a write-lock, which in
92036  ** turn means that the other connection has made uncommitted changes
92037  ** to the schema.
92038  **
92039  ** Were we to proceed and prepare the statement against the uncommitted
92040  ** schema changes and if those schema changes are subsequently rolled
92041  ** back and different changes are made in their place, then when this
92042  ** prepared statement goes to run the schema cookie would fail to detect
92043  ** the schema change.  Disaster would follow.
92044  **
92045  ** This thread is currently holding mutexes on all Btrees (because
92046  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
92047  ** is not possible for another thread to start a new schema change
92048  ** while this routine is running.  Hence, we do not need to hold
92049  ** locks on the schema, we just need to make sure nobody else is
92050  ** holding them.
92051  **
92052  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
92053  ** but it does *not* override schema lock detection, so this all still
92054  ** works even if READ_UNCOMMITTED is set.
92055  */
92056  for(i=0; i<db->nDb; i++) {
92057    Btree *pBt = db->aDb[i].pBt;
92058    if( pBt ){
92059      assert( sqlite3BtreeHoldsMutex(pBt) );
92060      rc = sqlite3BtreeSchemaLocked(pBt);
92061      if( rc ){
92062        const char *zDb = db->aDb[i].zName;
92063        sqlite3Error(db, rc, "database schema is locked: %s", zDb);
92064        testcase( db->flags & SQLITE_ReadUncommitted );
92065        goto end_prepare;
92066      }
92067    }
92068  }
92069
92070  sqlite3VtabUnlockList(db);
92071
92072  pParse->db = db;
92073  pParse->nQueryLoop = (double)1;
92074  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
92075    char *zSqlCopy;
92076    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
92077    testcase( nBytes==mxLen );
92078    testcase( nBytes==mxLen+1 );
92079    if( nBytes>mxLen ){
92080      sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
92081      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
92082      goto end_prepare;
92083    }
92084    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
92085    if( zSqlCopy ){
92086      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
92087      sqlite3DbFree(db, zSqlCopy);
92088      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
92089    }else{
92090      pParse->zTail = &zSql[nBytes];
92091    }
92092  }else{
92093    sqlite3RunParser(pParse, zSql, &zErrMsg);
92094  }
92095  assert( 1==(int)pParse->nQueryLoop );
92096
92097  if( db->mallocFailed ){
92098    pParse->rc = SQLITE_NOMEM;
92099  }
92100  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
92101  if( pParse->checkSchema ){
92102    schemaIsValid(pParse);
92103  }
92104  if( db->mallocFailed ){
92105    pParse->rc = SQLITE_NOMEM;
92106  }
92107  if( pzTail ){
92108    *pzTail = pParse->zTail;
92109  }
92110  rc = pParse->rc;
92111
92112#ifndef SQLITE_OMIT_EXPLAIN
92113  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
92114    static const char * const azColName[] = {
92115       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
92116       "selectid", "order", "from", "detail"
92117    };
92118    int iFirst, mx;
92119    if( pParse->explain==2 ){
92120      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
92121      iFirst = 8;
92122      mx = 12;
92123    }else{
92124      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
92125      iFirst = 0;
92126      mx = 8;
92127    }
92128    for(i=iFirst; i<mx; i++){
92129      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
92130                            azColName[i], SQLITE_STATIC);
92131    }
92132  }
92133#endif
92134
92135  assert( db->init.busy==0 || saveSqlFlag==0 );
92136  if( db->init.busy==0 ){
92137    Vdbe *pVdbe = pParse->pVdbe;
92138    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
92139  }
92140  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
92141    sqlite3VdbeFinalize(pParse->pVdbe);
92142    assert(!(*ppStmt));
92143  }else{
92144    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
92145  }
92146
92147  if( zErrMsg ){
92148    sqlite3Error(db, rc, "%s", zErrMsg);
92149    sqlite3DbFree(db, zErrMsg);
92150  }else{
92151    sqlite3Error(db, rc, 0);
92152  }
92153
92154  /* Delete any TriggerPrg structures allocated while parsing this statement. */
92155  while( pParse->pTriggerPrg ){
92156    TriggerPrg *pT = pParse->pTriggerPrg;
92157    pParse->pTriggerPrg = pT->pNext;
92158    sqlite3DbFree(db, pT);
92159  }
92160
92161end_prepare:
92162
92163  sqlite3StackFree(db, pParse);
92164  rc = sqlite3ApiExit(db, rc);
92165  assert( (rc&db->errMask)==rc );
92166  return rc;
92167}
92168static int sqlite3LockAndPrepare(
92169  sqlite3 *db,              /* Database handle. */
92170  const char *zSql,         /* UTF-8 encoded SQL statement. */
92171  int nBytes,               /* Length of zSql in bytes. */
92172  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
92173  Vdbe *pOld,               /* VM being reprepared */
92174  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92175  const char **pzTail       /* OUT: End of parsed string */
92176){
92177  int rc;
92178  assert( ppStmt!=0 );
92179  *ppStmt = 0;
92180  if( !sqlite3SafetyCheckOk(db) ){
92181    return SQLITE_MISUSE_BKPT;
92182  }
92183  sqlite3_mutex_enter(db->mutex);
92184  sqlite3BtreeEnterAll(db);
92185  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
92186  if( rc==SQLITE_SCHEMA ){
92187    sqlite3_finalize(*ppStmt);
92188    rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
92189  }
92190  sqlite3BtreeLeaveAll(db);
92191  sqlite3_mutex_leave(db->mutex);
92192  return rc;
92193}
92194
92195/*
92196** Rerun the compilation of a statement after a schema change.
92197**
92198** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
92199** if the statement cannot be recompiled because another connection has
92200** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
92201** occurs, return SQLITE_SCHEMA.
92202*/
92203SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
92204  int rc;
92205  sqlite3_stmt *pNew;
92206  const char *zSql;
92207  sqlite3 *db;
92208
92209  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
92210  zSql = sqlite3_sql((sqlite3_stmt *)p);
92211  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
92212  db = sqlite3VdbeDb(p);
92213  assert( sqlite3_mutex_held(db->mutex) );
92214  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
92215  if( rc ){
92216    if( rc==SQLITE_NOMEM ){
92217      db->mallocFailed = 1;
92218    }
92219    assert( pNew==0 );
92220    return rc;
92221  }else{
92222    assert( pNew!=0 );
92223  }
92224  sqlite3VdbeSwap((Vdbe*)pNew, p);
92225  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
92226  sqlite3VdbeResetStepResult((Vdbe*)pNew);
92227  sqlite3VdbeFinalize((Vdbe*)pNew);
92228  return SQLITE_OK;
92229}
92230
92231
92232/*
92233** Two versions of the official API.  Legacy and new use.  In the legacy
92234** version, the original SQL text is not saved in the prepared statement
92235** and so if a schema change occurs, SQLITE_SCHEMA is returned by
92236** sqlite3_step().  In the new version, the original SQL text is retained
92237** and the statement is automatically recompiled if an schema change
92238** occurs.
92239*/
92240SQLITE_API int sqlite3_prepare(
92241  sqlite3 *db,              /* Database handle. */
92242  const char *zSql,         /* UTF-8 encoded SQL statement. */
92243  int nBytes,               /* Length of zSql in bytes. */
92244  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92245  const char **pzTail       /* OUT: End of parsed string */
92246){
92247  int rc;
92248  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
92249  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
92250  return rc;
92251}
92252SQLITE_API int sqlite3_prepare_v2(
92253  sqlite3 *db,              /* Database handle. */
92254  const char *zSql,         /* UTF-8 encoded SQL statement. */
92255  int nBytes,               /* Length of zSql in bytes. */
92256  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92257  const char **pzTail       /* OUT: End of parsed string */
92258){
92259  int rc;
92260  rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
92261  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
92262  return rc;
92263}
92264
92265
92266#ifndef SQLITE_OMIT_UTF16
92267/*
92268** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
92269*/
92270static int sqlite3Prepare16(
92271  sqlite3 *db,              /* Database handle. */
92272  const void *zSql,         /* UTF-16 encoded SQL statement. */
92273  int nBytes,               /* Length of zSql in bytes. */
92274  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
92275  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92276  const void **pzTail       /* OUT: End of parsed string */
92277){
92278  /* This function currently works by first transforming the UTF-16
92279  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
92280  ** tricky bit is figuring out the pointer to return in *pzTail.
92281  */
92282  char *zSql8;
92283  const char *zTail8 = 0;
92284  int rc = SQLITE_OK;
92285
92286  assert( ppStmt );
92287  *ppStmt = 0;
92288  if( !sqlite3SafetyCheckOk(db) ){
92289    return SQLITE_MISUSE_BKPT;
92290  }
92291  sqlite3_mutex_enter(db->mutex);
92292  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
92293  if( zSql8 ){
92294    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
92295  }
92296
92297  if( zTail8 && pzTail ){
92298    /* If sqlite3_prepare returns a tail pointer, we calculate the
92299    ** equivalent pointer into the UTF-16 string by counting the unicode
92300    ** characters between zSql8 and zTail8, and then returning a pointer
92301    ** the same number of characters into the UTF-16 string.
92302    */
92303    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
92304    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
92305  }
92306  sqlite3DbFree(db, zSql8);
92307  rc = sqlite3ApiExit(db, rc);
92308  sqlite3_mutex_leave(db->mutex);
92309  return rc;
92310}
92311
92312/*
92313** Two versions of the official API.  Legacy and new use.  In the legacy
92314** version, the original SQL text is not saved in the prepared statement
92315** and so if a schema change occurs, SQLITE_SCHEMA is returned by
92316** sqlite3_step().  In the new version, the original SQL text is retained
92317** and the statement is automatically recompiled if an schema change
92318** occurs.
92319*/
92320SQLITE_API int sqlite3_prepare16(
92321  sqlite3 *db,              /* Database handle. */
92322  const void *zSql,         /* UTF-16 encoded SQL statement. */
92323  int nBytes,               /* Length of zSql in bytes. */
92324  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92325  const void **pzTail       /* OUT: End of parsed string */
92326){
92327  int rc;
92328  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
92329  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
92330  return rc;
92331}
92332SQLITE_API int sqlite3_prepare16_v2(
92333  sqlite3 *db,              /* Database handle. */
92334  const void *zSql,         /* UTF-16 encoded SQL statement. */
92335  int nBytes,               /* Length of zSql in bytes. */
92336  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
92337  const void **pzTail       /* OUT: End of parsed string */
92338){
92339  int rc;
92340  rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
92341  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
92342  return rc;
92343}
92344
92345#endif /* SQLITE_OMIT_UTF16 */
92346
92347/************** End of prepare.c *********************************************/
92348/************** Begin file select.c ******************************************/
92349/*
92350** 2001 September 15
92351**
92352** The author disclaims copyright to this source code.  In place of
92353** a legal notice, here is a blessing:
92354**
92355**    May you do good and not evil.
92356**    May you find forgiveness for yourself and forgive others.
92357**    May you share freely, never taking more than you give.
92358**
92359*************************************************************************
92360** This file contains C code routines that are called by the parser
92361** to handle SELECT statements in SQLite.
92362*/
92363
92364
92365/*
92366** Delete all the content of a Select structure but do not deallocate
92367** the select structure itself.
92368*/
92369static void clearSelect(sqlite3 *db, Select *p){
92370  sqlite3ExprListDelete(db, p->pEList);
92371  sqlite3SrcListDelete(db, p->pSrc);
92372  sqlite3ExprDelete(db, p->pWhere);
92373  sqlite3ExprListDelete(db, p->pGroupBy);
92374  sqlite3ExprDelete(db, p->pHaving);
92375  sqlite3ExprListDelete(db, p->pOrderBy);
92376  sqlite3SelectDelete(db, p->pPrior);
92377  sqlite3ExprDelete(db, p->pLimit);
92378  sqlite3ExprDelete(db, p->pOffset);
92379}
92380
92381/*
92382** Initialize a SelectDest structure.
92383*/
92384SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
92385  pDest->eDest = (u8)eDest;
92386  pDest->iParm = iParm;
92387  pDest->affinity = 0;
92388  pDest->iMem = 0;
92389  pDest->nMem = 0;
92390}
92391
92392
92393/*
92394** Allocate a new Select structure and return a pointer to that
92395** structure.
92396*/
92397SQLITE_PRIVATE Select *sqlite3SelectNew(
92398  Parse *pParse,        /* Parsing context */
92399  ExprList *pEList,     /* which columns to include in the result */
92400  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
92401  Expr *pWhere,         /* the WHERE clause */
92402  ExprList *pGroupBy,   /* the GROUP BY clause */
92403  Expr *pHaving,        /* the HAVING clause */
92404  ExprList *pOrderBy,   /* the ORDER BY clause */
92405  int isDistinct,       /* true if the DISTINCT keyword is present */
92406  Expr *pLimit,         /* LIMIT value.  NULL means not used */
92407  Expr *pOffset         /* OFFSET value.  NULL means no offset */
92408){
92409  Select *pNew;
92410  Select standin;
92411  sqlite3 *db = pParse->db;
92412  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
92413  assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
92414  if( pNew==0 ){
92415    pNew = &standin;
92416    memset(pNew, 0, sizeof(*pNew));
92417  }
92418  if( pEList==0 ){
92419    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
92420  }
92421  pNew->pEList = pEList;
92422  pNew->pSrc = pSrc;
92423  pNew->pWhere = pWhere;
92424  pNew->pGroupBy = pGroupBy;
92425  pNew->pHaving = pHaving;
92426  pNew->pOrderBy = pOrderBy;
92427  pNew->selFlags = isDistinct ? SF_Distinct : 0;
92428  pNew->op = TK_SELECT;
92429  pNew->pLimit = pLimit;
92430  pNew->pOffset = pOffset;
92431  assert( pOffset==0 || pLimit!=0 );
92432  pNew->addrOpenEphm[0] = -1;
92433  pNew->addrOpenEphm[1] = -1;
92434  pNew->addrOpenEphm[2] = -1;
92435  if( db->mallocFailed ) {
92436    clearSelect(db, pNew);
92437    if( pNew!=&standin ) sqlite3DbFree(db, pNew);
92438    pNew = 0;
92439  }else{
92440    assert( pNew->pSrc!=0 || pParse->nErr>0 );
92441  }
92442  return pNew;
92443}
92444
92445/*
92446** Delete the given Select structure and all of its substructures.
92447*/
92448SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
92449  if( p ){
92450    clearSelect(db, p);
92451    sqlite3DbFree(db, p);
92452  }
92453}
92454
92455/*
92456** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
92457** type of join.  Return an integer constant that expresses that type
92458** in terms of the following bit values:
92459**
92460**     JT_INNER
92461**     JT_CROSS
92462**     JT_OUTER
92463**     JT_NATURAL
92464**     JT_LEFT
92465**     JT_RIGHT
92466**
92467** A full outer join is the combination of JT_LEFT and JT_RIGHT.
92468**
92469** If an illegal or unsupported join type is seen, then still return
92470** a join type, but put an error in the pParse structure.
92471*/
92472SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
92473  int jointype = 0;
92474  Token *apAll[3];
92475  Token *p;
92476                             /*   0123456789 123456789 123456789 123 */
92477  static const char zKeyText[] = "naturaleftouterightfullinnercross";
92478  static const struct {
92479    u8 i;        /* Beginning of keyword text in zKeyText[] */
92480    u8 nChar;    /* Length of the keyword in characters */
92481    u8 code;     /* Join type mask */
92482  } aKeyword[] = {
92483    /* natural */ { 0,  7, JT_NATURAL                },
92484    /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
92485    /* outer   */ { 10, 5, JT_OUTER                  },
92486    /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
92487    /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
92488    /* inner   */ { 23, 5, JT_INNER                  },
92489    /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
92490  };
92491  int i, j;
92492  apAll[0] = pA;
92493  apAll[1] = pB;
92494  apAll[2] = pC;
92495  for(i=0; i<3 && apAll[i]; i++){
92496    p = apAll[i];
92497    for(j=0; j<ArraySize(aKeyword); j++){
92498      if( p->n==aKeyword[j].nChar
92499          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
92500        jointype |= aKeyword[j].code;
92501        break;
92502      }
92503    }
92504    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
92505    if( j>=ArraySize(aKeyword) ){
92506      jointype |= JT_ERROR;
92507      break;
92508    }
92509  }
92510  if(
92511     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
92512     (jointype & JT_ERROR)!=0
92513  ){
92514    const char *zSp = " ";
92515    assert( pB!=0 );
92516    if( pC==0 ){ zSp++; }
92517    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
92518       "%T %T%s%T", pA, pB, zSp, pC);
92519    jointype = JT_INNER;
92520  }else if( (jointype & JT_OUTER)!=0
92521         && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
92522    sqlite3ErrorMsg(pParse,
92523      "RIGHT and FULL OUTER JOINs are not currently supported");
92524    jointype = JT_INNER;
92525  }
92526  return jointype;
92527}
92528
92529/*
92530** Return the index of a column in a table.  Return -1 if the column
92531** is not contained in the table.
92532*/
92533static int columnIndex(Table *pTab, const char *zCol){
92534  int i;
92535  for(i=0; i<pTab->nCol; i++){
92536    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
92537  }
92538  return -1;
92539}
92540
92541/*
92542** Search the first N tables in pSrc, from left to right, looking for a
92543** table that has a column named zCol.
92544**
92545** When found, set *piTab and *piCol to the table index and column index
92546** of the matching column and return TRUE.
92547**
92548** If not found, return FALSE.
92549*/
92550static int tableAndColumnIndex(
92551  SrcList *pSrc,       /* Array of tables to search */
92552  int N,               /* Number of tables in pSrc->a[] to search */
92553  const char *zCol,    /* Name of the column we are looking for */
92554  int *piTab,          /* Write index of pSrc->a[] here */
92555  int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
92556){
92557  int i;               /* For looping over tables in pSrc */
92558  int iCol;            /* Index of column matching zCol */
92559
92560  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
92561  for(i=0; i<N; i++){
92562    iCol = columnIndex(pSrc->a[i].pTab, zCol);
92563    if( iCol>=0 ){
92564      if( piTab ){
92565        *piTab = i;
92566        *piCol = iCol;
92567      }
92568      return 1;
92569    }
92570  }
92571  return 0;
92572}
92573
92574/*
92575** This function is used to add terms implied by JOIN syntax to the
92576** WHERE clause expression of a SELECT statement. The new term, which
92577** is ANDed with the existing WHERE clause, is of the form:
92578**
92579**    (tab1.col1 = tab2.col2)
92580**
92581** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
92582** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
92583** column iColRight of tab2.
92584*/
92585static void addWhereTerm(
92586  Parse *pParse,                  /* Parsing context */
92587  SrcList *pSrc,                  /* List of tables in FROM clause */
92588  int iLeft,                      /* Index of first table to join in pSrc */
92589  int iColLeft,                   /* Index of column in first table */
92590  int iRight,                     /* Index of second table in pSrc */
92591  int iColRight,                  /* Index of column in second table */
92592  int isOuterJoin,                /* True if this is an OUTER join */
92593  Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
92594){
92595  sqlite3 *db = pParse->db;
92596  Expr *pE1;
92597  Expr *pE2;
92598  Expr *pEq;
92599
92600  assert( iLeft<iRight );
92601  assert( pSrc->nSrc>iRight );
92602  assert( pSrc->a[iLeft].pTab );
92603  assert( pSrc->a[iRight].pTab );
92604
92605  pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
92606  pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
92607
92608  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
92609  if( pEq && isOuterJoin ){
92610    ExprSetProperty(pEq, EP_FromJoin);
92611    assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
92612    ExprSetIrreducible(pEq);
92613    pEq->iRightJoinTable = (i16)pE2->iTable;
92614  }
92615  *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
92616}
92617
92618/*
92619** Set the EP_FromJoin property on all terms of the given expression.
92620** And set the Expr.iRightJoinTable to iTable for every term in the
92621** expression.
92622**
92623** The EP_FromJoin property is used on terms of an expression to tell
92624** the LEFT OUTER JOIN processing logic that this term is part of the
92625** join restriction specified in the ON or USING clause and not a part
92626** of the more general WHERE clause.  These terms are moved over to the
92627** WHERE clause during join processing but we need to remember that they
92628** originated in the ON or USING clause.
92629**
92630** The Expr.iRightJoinTable tells the WHERE clause processing that the
92631** expression depends on table iRightJoinTable even if that table is not
92632** explicitly mentioned in the expression.  That information is needed
92633** for cases like this:
92634**
92635**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
92636**
92637** The where clause needs to defer the handling of the t1.x=5
92638** term until after the t2 loop of the join.  In that way, a
92639** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
92640** defer the handling of t1.x=5, it will be processed immediately
92641** after the t1 loop and rows with t1.x!=5 will never appear in
92642** the output, which is incorrect.
92643*/
92644static void setJoinExpr(Expr *p, int iTable){
92645  while( p ){
92646    ExprSetProperty(p, EP_FromJoin);
92647    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
92648    ExprSetIrreducible(p);
92649    p->iRightJoinTable = (i16)iTable;
92650    setJoinExpr(p->pLeft, iTable);
92651    p = p->pRight;
92652  }
92653}
92654
92655/*
92656** This routine processes the join information for a SELECT statement.
92657** ON and USING clauses are converted into extra terms of the WHERE clause.
92658** NATURAL joins also create extra WHERE clause terms.
92659**
92660** The terms of a FROM clause are contained in the Select.pSrc structure.
92661** The left most table is the first entry in Select.pSrc.  The right-most
92662** table is the last entry.  The join operator is held in the entry to
92663** the left.  Thus entry 0 contains the join operator for the join between
92664** entries 0 and 1.  Any ON or USING clauses associated with the join are
92665** also attached to the left entry.
92666**
92667** This routine returns the number of errors encountered.
92668*/
92669static int sqliteProcessJoin(Parse *pParse, Select *p){
92670  SrcList *pSrc;                  /* All tables in the FROM clause */
92671  int i, j;                       /* Loop counters */
92672  struct SrcList_item *pLeft;     /* Left table being joined */
92673  struct SrcList_item *pRight;    /* Right table being joined */
92674
92675  pSrc = p->pSrc;
92676  pLeft = &pSrc->a[0];
92677  pRight = &pLeft[1];
92678  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
92679    Table *pLeftTab = pLeft->pTab;
92680    Table *pRightTab = pRight->pTab;
92681    int isOuter;
92682
92683    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
92684    isOuter = (pRight->jointype & JT_OUTER)!=0;
92685
92686    /* When the NATURAL keyword is present, add WHERE clause terms for
92687    ** every column that the two tables have in common.
92688    */
92689    if( pRight->jointype & JT_NATURAL ){
92690      if( pRight->pOn || pRight->pUsing ){
92691        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
92692           "an ON or USING clause", 0);
92693        return 1;
92694      }
92695      for(j=0; j<pRightTab->nCol; j++){
92696        char *zName;   /* Name of column in the right table */
92697        int iLeft;     /* Matching left table */
92698        int iLeftCol;  /* Matching column in the left table */
92699
92700        zName = pRightTab->aCol[j].zName;
92701        if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
92702          addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
92703                       isOuter, &p->pWhere);
92704        }
92705      }
92706    }
92707
92708    /* Disallow both ON and USING clauses in the same join
92709    */
92710    if( pRight->pOn && pRight->pUsing ){
92711      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
92712        "clauses in the same join");
92713      return 1;
92714    }
92715
92716    /* Add the ON clause to the end of the WHERE clause, connected by
92717    ** an AND operator.
92718    */
92719    if( pRight->pOn ){
92720      if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
92721      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
92722      pRight->pOn = 0;
92723    }
92724
92725    /* Create extra terms on the WHERE clause for each column named
92726    ** in the USING clause.  Example: If the two tables to be joined are
92727    ** A and B and the USING clause names X, Y, and Z, then add this
92728    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
92729    ** Report an error if any column mentioned in the USING clause is
92730    ** not contained in both tables to be joined.
92731    */
92732    if( pRight->pUsing ){
92733      IdList *pList = pRight->pUsing;
92734      for(j=0; j<pList->nId; j++){
92735        char *zName;     /* Name of the term in the USING clause */
92736        int iLeft;       /* Table on the left with matching column name */
92737        int iLeftCol;    /* Column number of matching column on the left */
92738        int iRightCol;   /* Column number of matching column on the right */
92739
92740        zName = pList->a[j].zName;
92741        iRightCol = columnIndex(pRightTab, zName);
92742        if( iRightCol<0
92743         || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
92744        ){
92745          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
92746            "not present in both tables", zName);
92747          return 1;
92748        }
92749        addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
92750                     isOuter, &p->pWhere);
92751      }
92752    }
92753  }
92754  return 0;
92755}
92756
92757/*
92758** Insert code into "v" that will push the record on the top of the
92759** stack into the sorter.
92760*/
92761static void pushOntoSorter(
92762  Parse *pParse,         /* Parser context */
92763  ExprList *pOrderBy,    /* The ORDER BY clause */
92764  Select *pSelect,       /* The whole SELECT statement */
92765  int regData            /* Register holding data to be sorted */
92766){
92767  Vdbe *v = pParse->pVdbe;
92768  int nExpr = pOrderBy->nExpr;
92769  int regBase = sqlite3GetTempRange(pParse, nExpr+2);
92770  int regRecord = sqlite3GetTempReg(pParse);
92771  int op;
92772  sqlite3ExprCacheClear(pParse);
92773  sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
92774  sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
92775  sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
92776  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
92777  if( pSelect->selFlags & SF_UseSorter ){
92778    op = OP_SorterInsert;
92779  }else{
92780    op = OP_IdxInsert;
92781  }
92782  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
92783  sqlite3ReleaseTempReg(pParse, regRecord);
92784  sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
92785  if( pSelect->iLimit ){
92786    int addr1, addr2;
92787    int iLimit;
92788    if( pSelect->iOffset ){
92789      iLimit = pSelect->iOffset+1;
92790    }else{
92791      iLimit = pSelect->iLimit;
92792    }
92793    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
92794    sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
92795    addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
92796    sqlite3VdbeJumpHere(v, addr1);
92797    sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
92798    sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
92799    sqlite3VdbeJumpHere(v, addr2);
92800  }
92801}
92802
92803/*
92804** Add code to implement the OFFSET
92805*/
92806static void codeOffset(
92807  Vdbe *v,          /* Generate code into this VM */
92808  Select *p,        /* The SELECT statement being coded */
92809  int iContinue     /* Jump here to skip the current record */
92810){
92811  if( p->iOffset && iContinue!=0 ){
92812    int addr;
92813    sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
92814    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
92815    sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
92816    VdbeComment((v, "skip OFFSET records"));
92817    sqlite3VdbeJumpHere(v, addr);
92818  }
92819}
92820
92821/*
92822** Add code that will check to make sure the N registers starting at iMem
92823** form a distinct entry.  iTab is a sorting index that holds previously
92824** seen combinations of the N values.  A new entry is made in iTab
92825** if the current N values are new.
92826**
92827** A jump to addrRepeat is made and the N+1 values are popped from the
92828** stack if the top N elements are not distinct.
92829*/
92830static void codeDistinct(
92831  Parse *pParse,     /* Parsing and code generating context */
92832  int iTab,          /* A sorting index used to test for distinctness */
92833  int addrRepeat,    /* Jump to here if not distinct */
92834  int N,             /* Number of elements */
92835  int iMem           /* First element */
92836){
92837  Vdbe *v;
92838  int r1;
92839
92840  v = pParse->pVdbe;
92841  r1 = sqlite3GetTempReg(pParse);
92842  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
92843  sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
92844  sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
92845  sqlite3ReleaseTempReg(pParse, r1);
92846}
92847
92848#ifndef SQLITE_OMIT_SUBQUERY
92849/*
92850** Generate an error message when a SELECT is used within a subexpression
92851** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
92852** column.  We do this in a subroutine because the error used to occur
92853** in multiple places.  (The error only occurs in one place now, but we
92854** retain the subroutine to minimize code disruption.)
92855*/
92856static int checkForMultiColumnSelectError(
92857  Parse *pParse,       /* Parse context. */
92858  SelectDest *pDest,   /* Destination of SELECT results */
92859  int nExpr            /* Number of result columns returned by SELECT */
92860){
92861  int eDest = pDest->eDest;
92862  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
92863    sqlite3ErrorMsg(pParse, "only a single result allowed for "
92864       "a SELECT that is part of an expression");
92865    return 1;
92866  }else{
92867    return 0;
92868  }
92869}
92870#endif
92871
92872/*
92873** This routine generates the code for the inside of the inner loop
92874** of a SELECT.
92875**
92876** If srcTab and nColumn are both zero, then the pEList expressions
92877** are evaluated in order to get the data for this row.  If nColumn>0
92878** then data is pulled from srcTab and pEList is used only to get the
92879** datatypes for each column.
92880*/
92881static void selectInnerLoop(
92882  Parse *pParse,          /* The parser context */
92883  Select *p,              /* The complete select statement being coded */
92884  ExprList *pEList,       /* List of values being extracted */
92885  int srcTab,             /* Pull data from this table */
92886  int nColumn,            /* Number of columns in the source table */
92887  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
92888  int distinct,           /* If >=0, make sure results are distinct */
92889  SelectDest *pDest,      /* How to dispose of the results */
92890  int iContinue,          /* Jump here to continue with next row */
92891  int iBreak              /* Jump here to break out of the inner loop */
92892){
92893  Vdbe *v = pParse->pVdbe;
92894  int i;
92895  int hasDistinct;        /* True if the DISTINCT keyword is present */
92896  int regResult;              /* Start of memory holding result set */
92897  int eDest = pDest->eDest;   /* How to dispose of results */
92898  int iParm = pDest->iParm;   /* First argument to disposal method */
92899  int nResultCol;             /* Number of result columns */
92900
92901  assert( v );
92902  if( NEVER(v==0) ) return;
92903  assert( pEList!=0 );
92904  hasDistinct = distinct>=0;
92905  if( pOrderBy==0 && !hasDistinct ){
92906    codeOffset(v, p, iContinue);
92907  }
92908
92909  /* Pull the requested columns.
92910  */
92911  if( nColumn>0 ){
92912    nResultCol = nColumn;
92913  }else{
92914    nResultCol = pEList->nExpr;
92915  }
92916  if( pDest->iMem==0 ){
92917    pDest->iMem = pParse->nMem+1;
92918    pDest->nMem = nResultCol;
92919    pParse->nMem += nResultCol;
92920  }else{
92921    assert( pDest->nMem==nResultCol );
92922  }
92923  regResult = pDest->iMem;
92924  if( nColumn>0 ){
92925    for(i=0; i<nColumn; i++){
92926      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
92927    }
92928  }else if( eDest!=SRT_Exists ){
92929    /* If the destination is an EXISTS(...) expression, the actual
92930    ** values returned by the SELECT are not required.
92931    */
92932    sqlite3ExprCacheClear(pParse);
92933    sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
92934  }
92935  nColumn = nResultCol;
92936
92937  /* If the DISTINCT keyword was present on the SELECT statement
92938  ** and this row has been seen before, then do not make this row
92939  ** part of the result.
92940  */
92941  if( hasDistinct ){
92942    assert( pEList!=0 );
92943    assert( pEList->nExpr==nColumn );
92944    codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
92945    if( pOrderBy==0 ){
92946      codeOffset(v, p, iContinue);
92947    }
92948  }
92949
92950  switch( eDest ){
92951    /* In this mode, write each query result to the key of the temporary
92952    ** table iParm.
92953    */
92954#ifndef SQLITE_OMIT_COMPOUND_SELECT
92955    case SRT_Union: {
92956      int r1;
92957      r1 = sqlite3GetTempReg(pParse);
92958      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
92959      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
92960      sqlite3ReleaseTempReg(pParse, r1);
92961      break;
92962    }
92963
92964    /* Construct a record from the query result, but instead of
92965    ** saving that record, use it as a key to delete elements from
92966    ** the temporary table iParm.
92967    */
92968    case SRT_Except: {
92969      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
92970      break;
92971    }
92972#endif
92973
92974    /* Store the result as data using a unique key.
92975    */
92976    case SRT_Table:
92977    case SRT_EphemTab: {
92978      int r1 = sqlite3GetTempReg(pParse);
92979      testcase( eDest==SRT_Table );
92980      testcase( eDest==SRT_EphemTab );
92981      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
92982      if( pOrderBy ){
92983        pushOntoSorter(pParse, pOrderBy, p, r1);
92984      }else{
92985        int r2 = sqlite3GetTempReg(pParse);
92986        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
92987        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
92988        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
92989        sqlite3ReleaseTempReg(pParse, r2);
92990      }
92991      sqlite3ReleaseTempReg(pParse, r1);
92992      break;
92993    }
92994
92995#ifndef SQLITE_OMIT_SUBQUERY
92996    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
92997    ** then there should be a single item on the stack.  Write this
92998    ** item into the set table with bogus data.
92999    */
93000    case SRT_Set: {
93001      assert( nColumn==1 );
93002      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
93003      if( pOrderBy ){
93004        /* At first glance you would think we could optimize out the
93005        ** ORDER BY in this case since the order of entries in the set
93006        ** does not matter.  But there might be a LIMIT clause, in which
93007        ** case the order does matter */
93008        pushOntoSorter(pParse, pOrderBy, p, regResult);
93009      }else{
93010        int r1 = sqlite3GetTempReg(pParse);
93011        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
93012        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
93013        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
93014        sqlite3ReleaseTempReg(pParse, r1);
93015      }
93016      break;
93017    }
93018
93019    /* If any row exist in the result set, record that fact and abort.
93020    */
93021    case SRT_Exists: {
93022      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
93023      /* The LIMIT clause will terminate the loop for us */
93024      break;
93025    }
93026
93027    /* If this is a scalar select that is part of an expression, then
93028    ** store the results in the appropriate memory cell and break out
93029    ** of the scan loop.
93030    */
93031    case SRT_Mem: {
93032      assert( nColumn==1 );
93033      if( pOrderBy ){
93034        pushOntoSorter(pParse, pOrderBy, p, regResult);
93035      }else{
93036        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
93037        /* The LIMIT clause will jump out of the loop for us */
93038      }
93039      break;
93040    }
93041#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
93042
93043    /* Send the data to the callback function or to a subroutine.  In the
93044    ** case of a subroutine, the subroutine itself is responsible for
93045    ** popping the data from the stack.
93046    */
93047    case SRT_Coroutine:
93048    case SRT_Output: {
93049      testcase( eDest==SRT_Coroutine );
93050      testcase( eDest==SRT_Output );
93051      if( pOrderBy ){
93052        int r1 = sqlite3GetTempReg(pParse);
93053        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
93054        pushOntoSorter(pParse, pOrderBy, p, r1);
93055        sqlite3ReleaseTempReg(pParse, r1);
93056      }else if( eDest==SRT_Coroutine ){
93057        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
93058      }else{
93059        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
93060        sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
93061      }
93062      break;
93063    }
93064
93065#if !defined(SQLITE_OMIT_TRIGGER)
93066    /* Discard the results.  This is used for SELECT statements inside
93067    ** the body of a TRIGGER.  The purpose of such selects is to call
93068    ** user-defined functions that have side effects.  We do not care
93069    ** about the actual results of the select.
93070    */
93071    default: {
93072      assert( eDest==SRT_Discard );
93073      break;
93074    }
93075#endif
93076  }
93077
93078  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
93079  ** there is a sorter, in which case the sorter has already limited
93080  ** the output for us.
93081  */
93082  if( pOrderBy==0 && p->iLimit ){
93083    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
93084  }
93085}
93086
93087/*
93088** Given an expression list, generate a KeyInfo structure that records
93089** the collating sequence for each expression in that expression list.
93090**
93091** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
93092** KeyInfo structure is appropriate for initializing a virtual index to
93093** implement that clause.  If the ExprList is the result set of a SELECT
93094** then the KeyInfo structure is appropriate for initializing a virtual
93095** index to implement a DISTINCT test.
93096**
93097** Space to hold the KeyInfo structure is obtain from malloc.  The calling
93098** function is responsible for seeing that this structure is eventually
93099** freed.  Add the KeyInfo structure to the P4 field of an opcode using
93100** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
93101*/
93102static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
93103  sqlite3 *db = pParse->db;
93104  int nExpr;
93105  KeyInfo *pInfo;
93106  struct ExprList_item *pItem;
93107  int i;
93108
93109  nExpr = pList->nExpr;
93110  pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
93111  if( pInfo ){
93112    pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
93113    pInfo->nField = (u16)nExpr;
93114    pInfo->enc = ENC(db);
93115    pInfo->db = db;
93116    for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
93117      CollSeq *pColl;
93118      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
93119      if( !pColl ){
93120        pColl = db->pDfltColl;
93121      }
93122      pInfo->aColl[i] = pColl;
93123      pInfo->aSortOrder[i] = pItem->sortOrder;
93124    }
93125  }
93126  return pInfo;
93127}
93128
93129#ifndef SQLITE_OMIT_COMPOUND_SELECT
93130/*
93131** Name of the connection operator, used for error messages.
93132*/
93133static const char *selectOpName(int id){
93134  char *z;
93135  switch( id ){
93136    case TK_ALL:       z = "UNION ALL";   break;
93137    case TK_INTERSECT: z = "INTERSECT";   break;
93138    case TK_EXCEPT:    z = "EXCEPT";      break;
93139    default:           z = "UNION";       break;
93140  }
93141  return z;
93142}
93143#endif /* SQLITE_OMIT_COMPOUND_SELECT */
93144
93145#ifndef SQLITE_OMIT_EXPLAIN
93146/*
93147** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
93148** is a no-op. Otherwise, it adds a single row of output to the EQP result,
93149** where the caption is of the form:
93150**
93151**   "USE TEMP B-TREE FOR xxx"
93152**
93153** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
93154** is determined by the zUsage argument.
93155*/
93156static void explainTempTable(Parse *pParse, const char *zUsage){
93157  if( pParse->explain==2 ){
93158    Vdbe *v = pParse->pVdbe;
93159    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
93160    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
93161  }
93162}
93163
93164/*
93165** Assign expression b to lvalue a. A second, no-op, version of this macro
93166** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
93167** in sqlite3Select() to assign values to structure member variables that
93168** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
93169** code with #ifndef directives.
93170*/
93171# define explainSetInteger(a, b) a = b
93172
93173#else
93174/* No-op versions of the explainXXX() functions and macros. */
93175# define explainTempTable(y,z)
93176# define explainSetInteger(y,z)
93177#endif
93178
93179#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
93180/*
93181** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
93182** is a no-op. Otherwise, it adds a single row of output to the EQP result,
93183** where the caption is of one of the two forms:
93184**
93185**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
93186**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
93187**
93188** where iSub1 and iSub2 are the integers passed as the corresponding
93189** function parameters, and op is the text representation of the parameter
93190** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
93191** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
93192** false, or the second form if it is true.
93193*/
93194static void explainComposite(
93195  Parse *pParse,                  /* Parse context */
93196  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
93197  int iSub1,                      /* Subquery id 1 */
93198  int iSub2,                      /* Subquery id 2 */
93199  int bUseTmp                     /* True if a temp table was used */
93200){
93201  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
93202  if( pParse->explain==2 ){
93203    Vdbe *v = pParse->pVdbe;
93204    char *zMsg = sqlite3MPrintf(
93205        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
93206        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
93207    );
93208    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
93209  }
93210}
93211#else
93212/* No-op versions of the explainXXX() functions and macros. */
93213# define explainComposite(v,w,x,y,z)
93214#endif
93215
93216/*
93217** If the inner loop was generated using a non-null pOrderBy argument,
93218** then the results were placed in a sorter.  After the loop is terminated
93219** we need to run the sorter and output the results.  The following
93220** routine generates the code needed to do that.
93221*/
93222static void generateSortTail(
93223  Parse *pParse,    /* Parsing context */
93224  Select *p,        /* The SELECT statement */
93225  Vdbe *v,          /* Generate code into this VDBE */
93226  int nColumn,      /* Number of columns of data */
93227  SelectDest *pDest /* Write the sorted results here */
93228){
93229  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
93230  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
93231  int addr;
93232  int iTab;
93233  int pseudoTab = 0;
93234  ExprList *pOrderBy = p->pOrderBy;
93235
93236  int eDest = pDest->eDest;
93237  int iParm = pDest->iParm;
93238
93239  int regRow;
93240  int regRowid;
93241
93242  iTab = pOrderBy->iECursor;
93243  regRow = sqlite3GetTempReg(pParse);
93244  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
93245    pseudoTab = pParse->nTab++;
93246    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
93247    regRowid = 0;
93248  }else{
93249    regRowid = sqlite3GetTempReg(pParse);
93250  }
93251  if( p->selFlags & SF_UseSorter ){
93252    int regSortOut = ++pParse->nMem;
93253    int ptab2 = pParse->nTab++;
93254    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
93255    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
93256    codeOffset(v, p, addrContinue);
93257    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
93258    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
93259    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
93260  }else{
93261    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
93262    codeOffset(v, p, addrContinue);
93263    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
93264  }
93265  switch( eDest ){
93266    case SRT_Table:
93267    case SRT_EphemTab: {
93268      testcase( eDest==SRT_Table );
93269      testcase( eDest==SRT_EphemTab );
93270      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
93271      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
93272      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
93273      break;
93274    }
93275#ifndef SQLITE_OMIT_SUBQUERY
93276    case SRT_Set: {
93277      assert( nColumn==1 );
93278      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
93279      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
93280      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
93281      break;
93282    }
93283    case SRT_Mem: {
93284      assert( nColumn==1 );
93285      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
93286      /* The LIMIT clause will terminate the loop for us */
93287      break;
93288    }
93289#endif
93290    default: {
93291      int i;
93292      assert( eDest==SRT_Output || eDest==SRT_Coroutine );
93293      testcase( eDest==SRT_Output );
93294      testcase( eDest==SRT_Coroutine );
93295      for(i=0; i<nColumn; i++){
93296        assert( regRow!=pDest->iMem+i );
93297        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
93298        if( i==0 ){
93299          sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
93300        }
93301      }
93302      if( eDest==SRT_Output ){
93303        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
93304        sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
93305      }else{
93306        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
93307      }
93308      break;
93309    }
93310  }
93311  sqlite3ReleaseTempReg(pParse, regRow);
93312  sqlite3ReleaseTempReg(pParse, regRowid);
93313
93314  /* The bottom of the loop
93315  */
93316  sqlite3VdbeResolveLabel(v, addrContinue);
93317  if( p->selFlags & SF_UseSorter ){
93318    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
93319  }else{
93320    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
93321  }
93322  sqlite3VdbeResolveLabel(v, addrBreak);
93323  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
93324    sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
93325  }
93326}
93327
93328/*
93329** Return a pointer to a string containing the 'declaration type' of the
93330** expression pExpr. The string may be treated as static by the caller.
93331**
93332** The declaration type is the exact datatype definition extracted from the
93333** original CREATE TABLE statement if the expression is a column. The
93334** declaration type for a ROWID field is INTEGER. Exactly when an expression
93335** is considered a column can be complex in the presence of subqueries. The
93336** result-set expression in all of the following SELECT statements is
93337** considered a column by this function.
93338**
93339**   SELECT col FROM tbl;
93340**   SELECT (SELECT col FROM tbl;
93341**   SELECT (SELECT col FROM tbl);
93342**   SELECT abc FROM (SELECT col AS abc FROM tbl);
93343**
93344** The declaration type for any expression other than a column is NULL.
93345*/
93346static const char *columnType(
93347  NameContext *pNC,
93348  Expr *pExpr,
93349  const char **pzOriginDb,
93350  const char **pzOriginTab,
93351  const char **pzOriginCol
93352){
93353  char const *zType = 0;
93354  char const *zOriginDb = 0;
93355  char const *zOriginTab = 0;
93356  char const *zOriginCol = 0;
93357  int j;
93358  if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
93359
93360  switch( pExpr->op ){
93361    case TK_AGG_COLUMN:
93362    case TK_COLUMN: {
93363      /* The expression is a column. Locate the table the column is being
93364      ** extracted from in NameContext.pSrcList. This table may be real
93365      ** database table or a subquery.
93366      */
93367      Table *pTab = 0;            /* Table structure column is extracted from */
93368      Select *pS = 0;             /* Select the column is extracted from */
93369      int iCol = pExpr->iColumn;  /* Index of column in pTab */
93370      testcase( pExpr->op==TK_AGG_COLUMN );
93371      testcase( pExpr->op==TK_COLUMN );
93372      while( pNC && !pTab ){
93373        SrcList *pTabList = pNC->pSrcList;
93374        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
93375        if( j<pTabList->nSrc ){
93376          pTab = pTabList->a[j].pTab;
93377          pS = pTabList->a[j].pSelect;
93378        }else{
93379          pNC = pNC->pNext;
93380        }
93381      }
93382
93383      if( pTab==0 ){
93384        /* At one time, code such as "SELECT new.x" within a trigger would
93385        ** cause this condition to run.  Since then, we have restructured how
93386        ** trigger code is generated and so this condition is no longer
93387        ** possible. However, it can still be true for statements like
93388        ** the following:
93389        **
93390        **   CREATE TABLE t1(col INTEGER);
93391        **   SELECT (SELECT t1.col) FROM FROM t1;
93392        **
93393        ** when columnType() is called on the expression "t1.col" in the
93394        ** sub-select. In this case, set the column type to NULL, even
93395        ** though it should really be "INTEGER".
93396        **
93397        ** This is not a problem, as the column type of "t1.col" is never
93398        ** used. When columnType() is called on the expression
93399        ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
93400        ** branch below.  */
93401        break;
93402      }
93403
93404      assert( pTab && pExpr->pTab==pTab );
93405      if( pS ){
93406        /* The "table" is actually a sub-select or a view in the FROM clause
93407        ** of the SELECT statement. Return the declaration type and origin
93408        ** data for the result-set column of the sub-select.
93409        */
93410        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
93411          /* If iCol is less than zero, then the expression requests the
93412          ** rowid of the sub-select or view. This expression is legal (see
93413          ** test case misc2.2.2) - it always evaluates to NULL.
93414          */
93415          NameContext sNC;
93416          Expr *p = pS->pEList->a[iCol].pExpr;
93417          sNC.pSrcList = pS->pSrc;
93418          sNC.pNext = pNC;
93419          sNC.pParse = pNC->pParse;
93420          zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
93421        }
93422      }else if( ALWAYS(pTab->pSchema) ){
93423        /* A real table */
93424        assert( !pS );
93425        if( iCol<0 ) iCol = pTab->iPKey;
93426        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
93427        if( iCol<0 ){
93428          zType = "INTEGER";
93429          zOriginCol = "rowid";
93430        }else{
93431          zType = pTab->aCol[iCol].zType;
93432          zOriginCol = pTab->aCol[iCol].zName;
93433        }
93434        zOriginTab = pTab->zName;
93435        if( pNC->pParse ){
93436          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
93437          zOriginDb = pNC->pParse->db->aDb[iDb].zName;
93438        }
93439      }
93440      break;
93441    }
93442#ifndef SQLITE_OMIT_SUBQUERY
93443    case TK_SELECT: {
93444      /* The expression is a sub-select. Return the declaration type and
93445      ** origin info for the single column in the result set of the SELECT
93446      ** statement.
93447      */
93448      NameContext sNC;
93449      Select *pS = pExpr->x.pSelect;
93450      Expr *p = pS->pEList->a[0].pExpr;
93451      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
93452      sNC.pSrcList = pS->pSrc;
93453      sNC.pNext = pNC;
93454      sNC.pParse = pNC->pParse;
93455      zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
93456      break;
93457    }
93458#endif
93459  }
93460
93461  if( pzOriginDb ){
93462    assert( pzOriginTab && pzOriginCol );
93463    *pzOriginDb = zOriginDb;
93464    *pzOriginTab = zOriginTab;
93465    *pzOriginCol = zOriginCol;
93466  }
93467  return zType;
93468}
93469
93470/*
93471** Generate code that will tell the VDBE the declaration types of columns
93472** in the result set.
93473*/
93474static void generateColumnTypes(
93475  Parse *pParse,      /* Parser context */
93476  SrcList *pTabList,  /* List of tables */
93477  ExprList *pEList    /* Expressions defining the result set */
93478){
93479#ifndef SQLITE_OMIT_DECLTYPE
93480  Vdbe *v = pParse->pVdbe;
93481  int i;
93482  NameContext sNC;
93483  sNC.pSrcList = pTabList;
93484  sNC.pParse = pParse;
93485  for(i=0; i<pEList->nExpr; i++){
93486    Expr *p = pEList->a[i].pExpr;
93487    const char *zType;
93488#ifdef SQLITE_ENABLE_COLUMN_METADATA
93489    const char *zOrigDb = 0;
93490    const char *zOrigTab = 0;
93491    const char *zOrigCol = 0;
93492    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
93493
93494    /* The vdbe must make its own copy of the column-type and other
93495    ** column specific strings, in case the schema is reset before this
93496    ** virtual machine is deleted.
93497    */
93498    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
93499    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
93500    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
93501#else
93502    zType = columnType(&sNC, p, 0, 0, 0);
93503#endif
93504    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
93505  }
93506#endif /* SQLITE_OMIT_DECLTYPE */
93507}
93508
93509/*
93510** Generate code that will tell the VDBE the names of columns
93511** in the result set.  This information is used to provide the
93512** azCol[] values in the callback.
93513*/
93514static void generateColumnNames(
93515  Parse *pParse,      /* Parser context */
93516  SrcList *pTabList,  /* List of tables */
93517  ExprList *pEList    /* Expressions defining the result set */
93518){
93519  Vdbe *v = pParse->pVdbe;
93520  int i, j;
93521  sqlite3 *db = pParse->db;
93522  int fullNames, shortNames;
93523
93524#ifndef SQLITE_OMIT_EXPLAIN
93525  /* If this is an EXPLAIN, skip this step */
93526  if( pParse->explain ){
93527    return;
93528  }
93529#endif
93530
93531  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
93532  pParse->colNamesSet = 1;
93533  fullNames = (db->flags & SQLITE_FullColNames)!=0;
93534  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
93535  sqlite3VdbeSetNumCols(v, pEList->nExpr);
93536  for(i=0; i<pEList->nExpr; i++){
93537    Expr *p;
93538    p = pEList->a[i].pExpr;
93539    if( NEVER(p==0) ) continue;
93540    if( pEList->a[i].zName ){
93541      char *zName = pEList->a[i].zName;
93542      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
93543    }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
93544      Table *pTab;
93545      char *zCol;
93546      int iCol = p->iColumn;
93547      for(j=0; ALWAYS(j<pTabList->nSrc); j++){
93548        if( pTabList->a[j].iCursor==p->iTable ) break;
93549      }
93550      assert( j<pTabList->nSrc );
93551      pTab = pTabList->a[j].pTab;
93552      if( iCol<0 ) iCol = pTab->iPKey;
93553      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
93554      if( iCol<0 ){
93555        zCol = "rowid";
93556      }else{
93557        zCol = pTab->aCol[iCol].zName;
93558      }
93559      if( !shortNames && !fullNames ){
93560        sqlite3VdbeSetColName(v, i, COLNAME_NAME,
93561            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
93562      }else if( fullNames ){
93563        char *zName = 0;
93564        zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
93565        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
93566      }else{
93567        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
93568      }
93569    }else{
93570      sqlite3VdbeSetColName(v, i, COLNAME_NAME,
93571          sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
93572    }
93573  }
93574  generateColumnTypes(pParse, pTabList, pEList);
93575}
93576
93577/*
93578** Given a an expression list (which is really the list of expressions
93579** that form the result set of a SELECT statement) compute appropriate
93580** column names for a table that would hold the expression list.
93581**
93582** All column names will be unique.
93583**
93584** Only the column names are computed.  Column.zType, Column.zColl,
93585** and other fields of Column are zeroed.
93586**
93587** Return SQLITE_OK on success.  If a memory allocation error occurs,
93588** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
93589*/
93590static int selectColumnsFromExprList(
93591  Parse *pParse,          /* Parsing context */
93592  ExprList *pEList,       /* Expr list from which to derive column names */
93593  int *pnCol,             /* Write the number of columns here */
93594  Column **paCol          /* Write the new column list here */
93595){
93596  sqlite3 *db = pParse->db;   /* Database connection */
93597  int i, j;                   /* Loop counters */
93598  int cnt;                    /* Index added to make the name unique */
93599  Column *aCol, *pCol;        /* For looping over result columns */
93600  int nCol;                   /* Number of columns in the result set */
93601  Expr *p;                    /* Expression for a single result column */
93602  char *zName;                /* Column name */
93603  int nName;                  /* Size of name in zName[] */
93604
93605  *pnCol = nCol = pEList->nExpr;
93606  aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
93607  if( aCol==0 ) return SQLITE_NOMEM;
93608  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
93609    /* Get an appropriate name for the column
93610    */
93611    p = pEList->a[i].pExpr;
93612    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
93613               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
93614    if( (zName = pEList->a[i].zName)!=0 ){
93615      /* If the column contains an "AS <name>" phrase, use <name> as the name */
93616      zName = sqlite3DbStrDup(db, zName);
93617    }else{
93618      Expr *pColExpr = p;  /* The expression that is the result column name */
93619      Table *pTab;         /* Table associated with this expression */
93620      while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
93621      if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
93622        /* For columns use the column name name */
93623        int iCol = pColExpr->iColumn;
93624        pTab = pColExpr->pTab;
93625        if( iCol<0 ) iCol = pTab->iPKey;
93626        zName = sqlite3MPrintf(db, "%s",
93627                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
93628      }else if( pColExpr->op==TK_ID ){
93629        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
93630        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
93631      }else{
93632        /* Use the original text of the column expression as its name */
93633        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
93634      }
93635    }
93636    if( db->mallocFailed ){
93637      sqlite3DbFree(db, zName);
93638      break;
93639    }
93640
93641    /* Make sure the column name is unique.  If the name is not unique,
93642    ** append a integer to the name so that it becomes unique.
93643    */
93644    nName = sqlite3Strlen30(zName);
93645    for(j=cnt=0; j<i; j++){
93646      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
93647        char *zNewName;
93648        zName[nName] = 0;
93649        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
93650        sqlite3DbFree(db, zName);
93651        zName = zNewName;
93652        j = -1;
93653        if( zName==0 ) break;
93654      }
93655    }
93656    pCol->zName = zName;
93657  }
93658  if( db->mallocFailed ){
93659    for(j=0; j<i; j++){
93660      sqlite3DbFree(db, aCol[j].zName);
93661    }
93662    sqlite3DbFree(db, aCol);
93663    *paCol = 0;
93664    *pnCol = 0;
93665    return SQLITE_NOMEM;
93666  }
93667  return SQLITE_OK;
93668}
93669
93670/*
93671** Add type and collation information to a column list based on
93672** a SELECT statement.
93673**
93674** The column list presumably came from selectColumnNamesFromExprList().
93675** The column list has only names, not types or collations.  This
93676** routine goes through and adds the types and collations.
93677**
93678** This routine requires that all identifiers in the SELECT
93679** statement be resolved.
93680*/
93681static void selectAddColumnTypeAndCollation(
93682  Parse *pParse,        /* Parsing contexts */
93683  int nCol,             /* Number of columns */
93684  Column *aCol,         /* List of columns */
93685  Select *pSelect       /* SELECT used to determine types and collations */
93686){
93687  sqlite3 *db = pParse->db;
93688  NameContext sNC;
93689  Column *pCol;
93690  CollSeq *pColl;
93691  int i;
93692  Expr *p;
93693  struct ExprList_item *a;
93694
93695  assert( pSelect!=0 );
93696  assert( (pSelect->selFlags & SF_Resolved)!=0 );
93697  assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
93698  if( db->mallocFailed ) return;
93699  memset(&sNC, 0, sizeof(sNC));
93700  sNC.pSrcList = pSelect->pSrc;
93701  a = pSelect->pEList->a;
93702  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
93703    p = a[i].pExpr;
93704    pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
93705    pCol->affinity = sqlite3ExprAffinity(p);
93706    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
93707    pColl = sqlite3ExprCollSeq(pParse, p);
93708    if( pColl ){
93709      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
93710    }
93711  }
93712}
93713
93714/*
93715** Given a SELECT statement, generate a Table structure that describes
93716** the result set of that SELECT.
93717*/
93718SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
93719  Table *pTab;
93720  sqlite3 *db = pParse->db;
93721  int savedFlags;
93722
93723  savedFlags = db->flags;
93724  db->flags &= ~SQLITE_FullColNames;
93725  db->flags |= SQLITE_ShortColNames;
93726  sqlite3SelectPrep(pParse, pSelect, 0);
93727  if( pParse->nErr ) return 0;
93728  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
93729  db->flags = savedFlags;
93730  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
93731  if( pTab==0 ){
93732    return 0;
93733  }
93734  /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
93735  ** is disabled */
93736  assert( db->lookaside.bEnabled==0 );
93737  pTab->nRef = 1;
93738  pTab->zName = 0;
93739  pTab->nRowEst = 1000000;
93740  selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
93741  selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
93742  pTab->iPKey = -1;
93743  if( db->mallocFailed ){
93744    sqlite3DeleteTable(db, pTab);
93745    return 0;
93746  }
93747  return pTab;
93748}
93749
93750/*
93751** Get a VDBE for the given parser context.  Create a new one if necessary.
93752** If an error occurs, return NULL and leave a message in pParse.
93753*/
93754SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
93755  Vdbe *v = pParse->pVdbe;
93756  if( v==0 ){
93757    v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
93758#ifndef SQLITE_OMIT_TRACE
93759    if( v ){
93760      sqlite3VdbeAddOp0(v, OP_Trace);
93761    }
93762#endif
93763  }
93764  return v;
93765}
93766
93767
93768/*
93769** Compute the iLimit and iOffset fields of the SELECT based on the
93770** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
93771** that appear in the original SQL statement after the LIMIT and OFFSET
93772** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
93773** are the integer memory register numbers for counters used to compute
93774** the limit and offset.  If there is no limit and/or offset, then
93775** iLimit and iOffset are negative.
93776**
93777** This routine changes the values of iLimit and iOffset only if
93778** a limit or offset is defined by pLimit and pOffset.  iLimit and
93779** iOffset should have been preset to appropriate default values
93780** (usually but not always -1) prior to calling this routine.
93781** Only if pLimit!=0 or pOffset!=0 do the limit registers get
93782** redefined.  The UNION ALL operator uses this property to force
93783** the reuse of the same limit and offset registers across multiple
93784** SELECT statements.
93785*/
93786static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
93787  Vdbe *v = 0;
93788  int iLimit = 0;
93789  int iOffset;
93790  int addr1, n;
93791  if( p->iLimit ) return;
93792
93793  /*
93794  ** "LIMIT -1" always shows all rows.  There is some
93795  ** contraversy about what the correct behavior should be.
93796  ** The current implementation interprets "LIMIT 0" to mean
93797  ** no rows.
93798  */
93799  sqlite3ExprCacheClear(pParse);
93800  assert( p->pOffset==0 || p->pLimit!=0 );
93801  if( p->pLimit ){
93802    p->iLimit = iLimit = ++pParse->nMem;
93803    v = sqlite3GetVdbe(pParse);
93804    if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
93805    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
93806      sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
93807      VdbeComment((v, "LIMIT counter"));
93808      if( n==0 ){
93809        sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
93810      }else{
93811        if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
93812      }
93813    }else{
93814      sqlite3ExprCode(pParse, p->pLimit, iLimit);
93815      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
93816      VdbeComment((v, "LIMIT counter"));
93817      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
93818    }
93819    if( p->pOffset ){
93820      p->iOffset = iOffset = ++pParse->nMem;
93821      pParse->nMem++;   /* Allocate an extra register for limit+offset */
93822      sqlite3ExprCode(pParse, p->pOffset, iOffset);
93823      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
93824      VdbeComment((v, "OFFSET counter"));
93825      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
93826      sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
93827      sqlite3VdbeJumpHere(v, addr1);
93828      sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
93829      VdbeComment((v, "LIMIT+OFFSET"));
93830      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
93831      sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
93832      sqlite3VdbeJumpHere(v, addr1);
93833    }
93834  }
93835}
93836
93837#ifndef SQLITE_OMIT_COMPOUND_SELECT
93838/*
93839** Return the appropriate collating sequence for the iCol-th column of
93840** the result set for the compound-select statement "p".  Return NULL if
93841** the column has no default collating sequence.
93842**
93843** The collating sequence for the compound select is taken from the
93844** left-most term of the select that has a collating sequence.
93845*/
93846static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
93847  CollSeq *pRet;
93848  if( p->pPrior ){
93849    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
93850  }else{
93851    pRet = 0;
93852  }
93853  assert( iCol>=0 );
93854  if( pRet==0 && iCol<p->pEList->nExpr ){
93855    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
93856  }
93857  return pRet;
93858}
93859#endif /* SQLITE_OMIT_COMPOUND_SELECT */
93860
93861/* Forward reference */
93862static int multiSelectOrderBy(
93863  Parse *pParse,        /* Parsing context */
93864  Select *p,            /* The right-most of SELECTs to be coded */
93865  SelectDest *pDest     /* What to do with query results */
93866);
93867
93868
93869#ifndef SQLITE_OMIT_COMPOUND_SELECT
93870/*
93871** This routine is called to process a compound query form from
93872** two or more separate queries using UNION, UNION ALL, EXCEPT, or
93873** INTERSECT
93874**
93875** "p" points to the right-most of the two queries.  the query on the
93876** left is p->pPrior.  The left query could also be a compound query
93877** in which case this routine will be called recursively.
93878**
93879** The results of the total query are to be written into a destination
93880** of type eDest with parameter iParm.
93881**
93882** Example 1:  Consider a three-way compound SQL statement.
93883**
93884**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
93885**
93886** This statement is parsed up as follows:
93887**
93888**     SELECT c FROM t3
93889**      |
93890**      `----->  SELECT b FROM t2
93891**                |
93892**                `------>  SELECT a FROM t1
93893**
93894** The arrows in the diagram above represent the Select.pPrior pointer.
93895** So if this routine is called with p equal to the t3 query, then
93896** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
93897**
93898** Notice that because of the way SQLite parses compound SELECTs, the
93899** individual selects always group from left to right.
93900*/
93901static int multiSelect(
93902  Parse *pParse,        /* Parsing context */
93903  Select *p,            /* The right-most of SELECTs to be coded */
93904  SelectDest *pDest     /* What to do with query results */
93905){
93906  int rc = SQLITE_OK;   /* Success code from a subroutine */
93907  Select *pPrior;       /* Another SELECT immediately to our left */
93908  Vdbe *v;              /* Generate code to this VDBE */
93909  SelectDest dest;      /* Alternative data destination */
93910  Select *pDelete = 0;  /* Chain of simple selects to delete */
93911  sqlite3 *db;          /* Database connection */
93912#ifndef SQLITE_OMIT_EXPLAIN
93913  int iSub1;            /* EQP id of left-hand query */
93914  int iSub2;            /* EQP id of right-hand query */
93915#endif
93916
93917  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
93918  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
93919  */
93920  assert( p && p->pPrior );  /* Calling function guarantees this much */
93921  db = pParse->db;
93922  pPrior = p->pPrior;
93923  assert( pPrior->pRightmost!=pPrior );
93924  assert( pPrior->pRightmost==p->pRightmost );
93925  dest = *pDest;
93926  if( pPrior->pOrderBy ){
93927    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
93928      selectOpName(p->op));
93929    rc = 1;
93930    goto multi_select_end;
93931  }
93932  if( pPrior->pLimit ){
93933    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
93934      selectOpName(p->op));
93935    rc = 1;
93936    goto multi_select_end;
93937  }
93938
93939  v = sqlite3GetVdbe(pParse);
93940  assert( v!=0 );  /* The VDBE already created by calling function */
93941
93942  /* Create the destination temporary table if necessary
93943  */
93944  if( dest.eDest==SRT_EphemTab ){
93945    assert( p->pEList );
93946    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
93947    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
93948    dest.eDest = SRT_Table;
93949  }
93950
93951  /* Make sure all SELECTs in the statement have the same number of elements
93952  ** in their result sets.
93953  */
93954  assert( p->pEList && pPrior->pEList );
93955  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
93956    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
93957      " do not have the same number of result columns", selectOpName(p->op));
93958    rc = 1;
93959    goto multi_select_end;
93960  }
93961
93962  /* Compound SELECTs that have an ORDER BY clause are handled separately.
93963  */
93964  if( p->pOrderBy ){
93965    return multiSelectOrderBy(pParse, p, pDest);
93966  }
93967
93968  /* Generate code for the left and right SELECT statements.
93969  */
93970  switch( p->op ){
93971    case TK_ALL: {
93972      int addr = 0;
93973      int nLimit;
93974      assert( !pPrior->pLimit );
93975      pPrior->pLimit = p->pLimit;
93976      pPrior->pOffset = p->pOffset;
93977      explainSetInteger(iSub1, pParse->iNextSelectId);
93978      rc = sqlite3Select(pParse, pPrior, &dest);
93979      p->pLimit = 0;
93980      p->pOffset = 0;
93981      if( rc ){
93982        goto multi_select_end;
93983      }
93984      p->pPrior = 0;
93985      p->iLimit = pPrior->iLimit;
93986      p->iOffset = pPrior->iOffset;
93987      if( p->iLimit ){
93988        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
93989        VdbeComment((v, "Jump ahead if LIMIT reached"));
93990      }
93991      explainSetInteger(iSub2, pParse->iNextSelectId);
93992      rc = sqlite3Select(pParse, p, &dest);
93993      testcase( rc!=SQLITE_OK );
93994      pDelete = p->pPrior;
93995      p->pPrior = pPrior;
93996      p->nSelectRow += pPrior->nSelectRow;
93997      if( pPrior->pLimit
93998       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
93999       && p->nSelectRow > (double)nLimit
94000      ){
94001        p->nSelectRow = (double)nLimit;
94002      }
94003      if( addr ){
94004        sqlite3VdbeJumpHere(v, addr);
94005      }
94006      break;
94007    }
94008    case TK_EXCEPT:
94009    case TK_UNION: {
94010      int unionTab;    /* Cursor number of the temporary table holding result */
94011      u8 op = 0;       /* One of the SRT_ operations to apply to self */
94012      int priorOp;     /* The SRT_ operation to apply to prior selects */
94013      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
94014      int addr;
94015      SelectDest uniondest;
94016
94017      testcase( p->op==TK_EXCEPT );
94018      testcase( p->op==TK_UNION );
94019      priorOp = SRT_Union;
94020      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
94021        /* We can reuse a temporary table generated by a SELECT to our
94022        ** right.
94023        */
94024        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
94025                                     ** of a 3-way or more compound */
94026        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
94027        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
94028        unionTab = dest.iParm;
94029      }else{
94030        /* We will need to create our own temporary table to hold the
94031        ** intermediate results.
94032        */
94033        unionTab = pParse->nTab++;
94034        assert( p->pOrderBy==0 );
94035        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
94036        assert( p->addrOpenEphm[0] == -1 );
94037        p->addrOpenEphm[0] = addr;
94038        p->pRightmost->selFlags |= SF_UsesEphemeral;
94039        assert( p->pEList );
94040      }
94041
94042      /* Code the SELECT statements to our left
94043      */
94044      assert( !pPrior->pOrderBy );
94045      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
94046      explainSetInteger(iSub1, pParse->iNextSelectId);
94047      rc = sqlite3Select(pParse, pPrior, &uniondest);
94048      if( rc ){
94049        goto multi_select_end;
94050      }
94051
94052      /* Code the current SELECT statement
94053      */
94054      if( p->op==TK_EXCEPT ){
94055        op = SRT_Except;
94056      }else{
94057        assert( p->op==TK_UNION );
94058        op = SRT_Union;
94059      }
94060      p->pPrior = 0;
94061      pLimit = p->pLimit;
94062      p->pLimit = 0;
94063      pOffset = p->pOffset;
94064      p->pOffset = 0;
94065      uniondest.eDest = op;
94066      explainSetInteger(iSub2, pParse->iNextSelectId);
94067      rc = sqlite3Select(pParse, p, &uniondest);
94068      testcase( rc!=SQLITE_OK );
94069      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
94070      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
94071      sqlite3ExprListDelete(db, p->pOrderBy);
94072      pDelete = p->pPrior;
94073      p->pPrior = pPrior;
94074      p->pOrderBy = 0;
94075      if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
94076      sqlite3ExprDelete(db, p->pLimit);
94077      p->pLimit = pLimit;
94078      p->pOffset = pOffset;
94079      p->iLimit = 0;
94080      p->iOffset = 0;
94081
94082      /* Convert the data in the temporary table into whatever form
94083      ** it is that we currently need.
94084      */
94085      assert( unionTab==dest.iParm || dest.eDest!=priorOp );
94086      if( dest.eDest!=priorOp ){
94087        int iCont, iBreak, iStart;
94088        assert( p->pEList );
94089        if( dest.eDest==SRT_Output ){
94090          Select *pFirst = p;
94091          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
94092          generateColumnNames(pParse, 0, pFirst->pEList);
94093        }
94094        iBreak = sqlite3VdbeMakeLabel(v);
94095        iCont = sqlite3VdbeMakeLabel(v);
94096        computeLimitRegisters(pParse, p, iBreak);
94097        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
94098        iStart = sqlite3VdbeCurrentAddr(v);
94099        selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
94100                        0, -1, &dest, iCont, iBreak);
94101        sqlite3VdbeResolveLabel(v, iCont);
94102        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
94103        sqlite3VdbeResolveLabel(v, iBreak);
94104        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
94105      }
94106      break;
94107    }
94108    default: assert( p->op==TK_INTERSECT ); {
94109      int tab1, tab2;
94110      int iCont, iBreak, iStart;
94111      Expr *pLimit, *pOffset;
94112      int addr;
94113      SelectDest intersectdest;
94114      int r1;
94115
94116      /* INTERSECT is different from the others since it requires
94117      ** two temporary tables.  Hence it has its own case.  Begin
94118      ** by allocating the tables we will need.
94119      */
94120      tab1 = pParse->nTab++;
94121      tab2 = pParse->nTab++;
94122      assert( p->pOrderBy==0 );
94123
94124      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
94125      assert( p->addrOpenEphm[0] == -1 );
94126      p->addrOpenEphm[0] = addr;
94127      p->pRightmost->selFlags |= SF_UsesEphemeral;
94128      assert( p->pEList );
94129
94130      /* Code the SELECTs to our left into temporary table "tab1".
94131      */
94132      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
94133      explainSetInteger(iSub1, pParse->iNextSelectId);
94134      rc = sqlite3Select(pParse, pPrior, &intersectdest);
94135      if( rc ){
94136        goto multi_select_end;
94137      }
94138
94139      /* Code the current SELECT into temporary table "tab2"
94140      */
94141      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
94142      assert( p->addrOpenEphm[1] == -1 );
94143      p->addrOpenEphm[1] = addr;
94144      p->pPrior = 0;
94145      pLimit = p->pLimit;
94146      p->pLimit = 0;
94147      pOffset = p->pOffset;
94148      p->pOffset = 0;
94149      intersectdest.iParm = tab2;
94150      explainSetInteger(iSub2, pParse->iNextSelectId);
94151      rc = sqlite3Select(pParse, p, &intersectdest);
94152      testcase( rc!=SQLITE_OK );
94153      pDelete = p->pPrior;
94154      p->pPrior = pPrior;
94155      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
94156      sqlite3ExprDelete(db, p->pLimit);
94157      p->pLimit = pLimit;
94158      p->pOffset = pOffset;
94159
94160      /* Generate code to take the intersection of the two temporary
94161      ** tables.
94162      */
94163      assert( p->pEList );
94164      if( dest.eDest==SRT_Output ){
94165        Select *pFirst = p;
94166        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
94167        generateColumnNames(pParse, 0, pFirst->pEList);
94168      }
94169      iBreak = sqlite3VdbeMakeLabel(v);
94170      iCont = sqlite3VdbeMakeLabel(v);
94171      computeLimitRegisters(pParse, p, iBreak);
94172      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
94173      r1 = sqlite3GetTempReg(pParse);
94174      iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
94175      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
94176      sqlite3ReleaseTempReg(pParse, r1);
94177      selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
94178                      0, -1, &dest, iCont, iBreak);
94179      sqlite3VdbeResolveLabel(v, iCont);
94180      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
94181      sqlite3VdbeResolveLabel(v, iBreak);
94182      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
94183      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
94184      break;
94185    }
94186  }
94187
94188  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
94189
94190  /* Compute collating sequences used by
94191  ** temporary tables needed to implement the compound select.
94192  ** Attach the KeyInfo structure to all temporary tables.
94193  **
94194  ** This section is run by the right-most SELECT statement only.
94195  ** SELECT statements to the left always skip this part.  The right-most
94196  ** SELECT might also skip this part if it has no ORDER BY clause and
94197  ** no temp tables are required.
94198  */
94199  if( p->selFlags & SF_UsesEphemeral ){
94200    int i;                        /* Loop counter */
94201    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
94202    Select *pLoop;                /* For looping through SELECT statements */
94203    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
94204    int nCol;                     /* Number of columns in result set */
94205
94206    assert( p->pRightmost==p );
94207    nCol = p->pEList->nExpr;
94208    pKeyInfo = sqlite3DbMallocZero(db,
94209                       sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
94210    if( !pKeyInfo ){
94211      rc = SQLITE_NOMEM;
94212      goto multi_select_end;
94213    }
94214
94215    pKeyInfo->enc = ENC(db);
94216    pKeyInfo->nField = (u16)nCol;
94217
94218    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
94219      *apColl = multiSelectCollSeq(pParse, p, i);
94220      if( 0==*apColl ){
94221        *apColl = db->pDfltColl;
94222      }
94223    }
94224
94225    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
94226      for(i=0; i<2; i++){
94227        int addr = pLoop->addrOpenEphm[i];
94228        if( addr<0 ){
94229          /* If [0] is unused then [1] is also unused.  So we can
94230          ** always safely abort as soon as the first unused slot is found */
94231          assert( pLoop->addrOpenEphm[1]<0 );
94232          break;
94233        }
94234        sqlite3VdbeChangeP2(v, addr, nCol);
94235        sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
94236        pLoop->addrOpenEphm[i] = -1;
94237      }
94238    }
94239    sqlite3DbFree(db, pKeyInfo);
94240  }
94241
94242multi_select_end:
94243  pDest->iMem = dest.iMem;
94244  pDest->nMem = dest.nMem;
94245  sqlite3SelectDelete(db, pDelete);
94246  return rc;
94247}
94248#endif /* SQLITE_OMIT_COMPOUND_SELECT */
94249
94250/*
94251** Code an output subroutine for a coroutine implementation of a
94252** SELECT statment.
94253**
94254** The data to be output is contained in pIn->iMem.  There are
94255** pIn->nMem columns to be output.  pDest is where the output should
94256** be sent.
94257**
94258** regReturn is the number of the register holding the subroutine
94259** return address.
94260**
94261** If regPrev>0 then it is the first register in a vector that
94262** records the previous output.  mem[regPrev] is a flag that is false
94263** if there has been no previous output.  If regPrev>0 then code is
94264** generated to suppress duplicates.  pKeyInfo is used for comparing
94265** keys.
94266**
94267** If the LIMIT found in p->iLimit is reached, jump immediately to
94268** iBreak.
94269*/
94270static int generateOutputSubroutine(
94271  Parse *pParse,          /* Parsing context */
94272  Select *p,              /* The SELECT statement */
94273  SelectDest *pIn,        /* Coroutine supplying data */
94274  SelectDest *pDest,      /* Where to send the data */
94275  int regReturn,          /* The return address register */
94276  int regPrev,            /* Previous result register.  No uniqueness if 0 */
94277  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
94278  int p4type,             /* The p4 type for pKeyInfo */
94279  int iBreak              /* Jump here if we hit the LIMIT */
94280){
94281  Vdbe *v = pParse->pVdbe;
94282  int iContinue;
94283  int addr;
94284
94285  addr = sqlite3VdbeCurrentAddr(v);
94286  iContinue = sqlite3VdbeMakeLabel(v);
94287
94288  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
94289  */
94290  if( regPrev ){
94291    int j1, j2;
94292    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
94293    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
94294                              (char*)pKeyInfo, p4type);
94295    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
94296    sqlite3VdbeJumpHere(v, j1);
94297    sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
94298    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
94299  }
94300  if( pParse->db->mallocFailed ) return 0;
94301
94302  /* Suppress the the first OFFSET entries if there is an OFFSET clause
94303  */
94304  codeOffset(v, p, iContinue);
94305
94306  switch( pDest->eDest ){
94307    /* Store the result as data using a unique key.
94308    */
94309    case SRT_Table:
94310    case SRT_EphemTab: {
94311      int r1 = sqlite3GetTempReg(pParse);
94312      int r2 = sqlite3GetTempReg(pParse);
94313      testcase( pDest->eDest==SRT_Table );
94314      testcase( pDest->eDest==SRT_EphemTab );
94315      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
94316      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
94317      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
94318      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
94319      sqlite3ReleaseTempReg(pParse, r2);
94320      sqlite3ReleaseTempReg(pParse, r1);
94321      break;
94322    }
94323
94324#ifndef SQLITE_OMIT_SUBQUERY
94325    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
94326    ** then there should be a single item on the stack.  Write this
94327    ** item into the set table with bogus data.
94328    */
94329    case SRT_Set: {
94330      int r1;
94331      assert( pIn->nMem==1 );
94332      p->affinity =
94333         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
94334      r1 = sqlite3GetTempReg(pParse);
94335      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
94336      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
94337      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
94338      sqlite3ReleaseTempReg(pParse, r1);
94339      break;
94340    }
94341
94342#if 0  /* Never occurs on an ORDER BY query */
94343    /* If any row exist in the result set, record that fact and abort.
94344    */
94345    case SRT_Exists: {
94346      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
94347      /* The LIMIT clause will terminate the loop for us */
94348      break;
94349    }
94350#endif
94351
94352    /* If this is a scalar select that is part of an expression, then
94353    ** store the results in the appropriate memory cell and break out
94354    ** of the scan loop.
94355    */
94356    case SRT_Mem: {
94357      assert( pIn->nMem==1 );
94358      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
94359      /* The LIMIT clause will jump out of the loop for us */
94360      break;
94361    }
94362#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
94363
94364    /* The results are stored in a sequence of registers
94365    ** starting at pDest->iMem.  Then the co-routine yields.
94366    */
94367    case SRT_Coroutine: {
94368      if( pDest->iMem==0 ){
94369        pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
94370        pDest->nMem = pIn->nMem;
94371      }
94372      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
94373      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
94374      break;
94375    }
94376
94377    /* If none of the above, then the result destination must be
94378    ** SRT_Output.  This routine is never called with any other
94379    ** destination other than the ones handled above or SRT_Output.
94380    **
94381    ** For SRT_Output, results are stored in a sequence of registers.
94382    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
94383    ** return the next row of result.
94384    */
94385    default: {
94386      assert( pDest->eDest==SRT_Output );
94387      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
94388      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
94389      break;
94390    }
94391  }
94392
94393  /* Jump to the end of the loop if the LIMIT is reached.
94394  */
94395  if( p->iLimit ){
94396    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
94397  }
94398
94399  /* Generate the subroutine return
94400  */
94401  sqlite3VdbeResolveLabel(v, iContinue);
94402  sqlite3VdbeAddOp1(v, OP_Return, regReturn);
94403
94404  return addr;
94405}
94406
94407/*
94408** Alternative compound select code generator for cases when there
94409** is an ORDER BY clause.
94410**
94411** We assume a query of the following form:
94412**
94413**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
94414**
94415** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
94416** is to code both <selectA> and <selectB> with the ORDER BY clause as
94417** co-routines.  Then run the co-routines in parallel and merge the results
94418** into the output.  In addition to the two coroutines (called selectA and
94419** selectB) there are 7 subroutines:
94420**
94421**    outA:    Move the output of the selectA coroutine into the output
94422**             of the compound query.
94423**
94424**    outB:    Move the output of the selectB coroutine into the output
94425**             of the compound query.  (Only generated for UNION and
94426**             UNION ALL.  EXCEPT and INSERTSECT never output a row that
94427**             appears only in B.)
94428**
94429**    AltB:    Called when there is data from both coroutines and A<B.
94430**
94431**    AeqB:    Called when there is data from both coroutines and A==B.
94432**
94433**    AgtB:    Called when there is data from both coroutines and A>B.
94434**
94435**    EofA:    Called when data is exhausted from selectA.
94436**
94437**    EofB:    Called when data is exhausted from selectB.
94438**
94439** The implementation of the latter five subroutines depend on which
94440** <operator> is used:
94441**
94442**
94443**             UNION ALL         UNION            EXCEPT          INTERSECT
94444**          -------------  -----------------  --------------  -----------------
94445**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
94446**
94447**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
94448**
94449**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
94450**
94451**   EofA:   outB, nextB      outB, nextB          halt             halt
94452**
94453**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
94454**
94455** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
94456** causes an immediate jump to EofA and an EOF on B following nextB causes
94457** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
94458** following nextX causes a jump to the end of the select processing.
94459**
94460** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
94461** within the output subroutine.  The regPrev register set holds the previously
94462** output value.  A comparison is made against this value and the output
94463** is skipped if the next results would be the same as the previous.
94464**
94465** The implementation plan is to implement the two coroutines and seven
94466** subroutines first, then put the control logic at the bottom.  Like this:
94467**
94468**          goto Init
94469**     coA: coroutine for left query (A)
94470**     coB: coroutine for right query (B)
94471**    outA: output one row of A
94472**    outB: output one row of B (UNION and UNION ALL only)
94473**    EofA: ...
94474**    EofB: ...
94475**    AltB: ...
94476**    AeqB: ...
94477**    AgtB: ...
94478**    Init: initialize coroutine registers
94479**          yield coA
94480**          if eof(A) goto EofA
94481**          yield coB
94482**          if eof(B) goto EofB
94483**    Cmpr: Compare A, B
94484**          Jump AltB, AeqB, AgtB
94485**     End: ...
94486**
94487** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
94488** actually called using Gosub and they do not Return.  EofA and EofB loop
94489** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
94490** and AgtB jump to either L2 or to one of EofA or EofB.
94491*/
94492#ifndef SQLITE_OMIT_COMPOUND_SELECT
94493static int multiSelectOrderBy(
94494  Parse *pParse,        /* Parsing context */
94495  Select *p,            /* The right-most of SELECTs to be coded */
94496  SelectDest *pDest     /* What to do with query results */
94497){
94498  int i, j;             /* Loop counters */
94499  Select *pPrior;       /* Another SELECT immediately to our left */
94500  Vdbe *v;              /* Generate code to this VDBE */
94501  SelectDest destA;     /* Destination for coroutine A */
94502  SelectDest destB;     /* Destination for coroutine B */
94503  int regAddrA;         /* Address register for select-A coroutine */
94504  int regEofA;          /* Flag to indicate when select-A is complete */
94505  int regAddrB;         /* Address register for select-B coroutine */
94506  int regEofB;          /* Flag to indicate when select-B is complete */
94507  int addrSelectA;      /* Address of the select-A coroutine */
94508  int addrSelectB;      /* Address of the select-B coroutine */
94509  int regOutA;          /* Address register for the output-A subroutine */
94510  int regOutB;          /* Address register for the output-B subroutine */
94511  int addrOutA;         /* Address of the output-A subroutine */
94512  int addrOutB = 0;     /* Address of the output-B subroutine */
94513  int addrEofA;         /* Address of the select-A-exhausted subroutine */
94514  int addrEofB;         /* Address of the select-B-exhausted subroutine */
94515  int addrAltB;         /* Address of the A<B subroutine */
94516  int addrAeqB;         /* Address of the A==B subroutine */
94517  int addrAgtB;         /* Address of the A>B subroutine */
94518  int regLimitA;        /* Limit register for select-A */
94519  int regLimitB;        /* Limit register for select-A */
94520  int regPrev;          /* A range of registers to hold previous output */
94521  int savedLimit;       /* Saved value of p->iLimit */
94522  int savedOffset;      /* Saved value of p->iOffset */
94523  int labelCmpr;        /* Label for the start of the merge algorithm */
94524  int labelEnd;         /* Label for the end of the overall SELECT stmt */
94525  int j1;               /* Jump instructions that get retargetted */
94526  int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
94527  KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
94528  KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
94529  sqlite3 *db;          /* Database connection */
94530  ExprList *pOrderBy;   /* The ORDER BY clause */
94531  int nOrderBy;         /* Number of terms in the ORDER BY clause */
94532  int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
94533#ifndef SQLITE_OMIT_EXPLAIN
94534  int iSub1;            /* EQP id of left-hand query */
94535  int iSub2;            /* EQP id of right-hand query */
94536#endif
94537
94538  assert( p->pOrderBy!=0 );
94539  assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
94540  db = pParse->db;
94541  v = pParse->pVdbe;
94542  assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
94543  labelEnd = sqlite3VdbeMakeLabel(v);
94544  labelCmpr = sqlite3VdbeMakeLabel(v);
94545
94546
94547  /* Patch up the ORDER BY clause
94548  */
94549  op = p->op;
94550  pPrior = p->pPrior;
94551  assert( pPrior->pOrderBy==0 );
94552  pOrderBy = p->pOrderBy;
94553  assert( pOrderBy );
94554  nOrderBy = pOrderBy->nExpr;
94555
94556  /* For operators other than UNION ALL we have to make sure that
94557  ** the ORDER BY clause covers every term of the result set.  Add
94558  ** terms to the ORDER BY clause as necessary.
94559  */
94560  if( op!=TK_ALL ){
94561    for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
94562      struct ExprList_item *pItem;
94563      for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
94564        assert( pItem->iCol>0 );
94565        if( pItem->iCol==i ) break;
94566      }
94567      if( j==nOrderBy ){
94568        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
94569        if( pNew==0 ) return SQLITE_NOMEM;
94570        pNew->flags |= EP_IntValue;
94571        pNew->u.iValue = i;
94572        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
94573        pOrderBy->a[nOrderBy++].iCol = (u16)i;
94574      }
94575    }
94576  }
94577
94578  /* Compute the comparison permutation and keyinfo that is used with
94579  ** the permutation used to determine if the next
94580  ** row of results comes from selectA or selectB.  Also add explicit
94581  ** collations to the ORDER BY clause terms so that when the subqueries
94582  ** to the right and the left are evaluated, they use the correct
94583  ** collation.
94584  */
94585  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
94586  if( aPermute ){
94587    struct ExprList_item *pItem;
94588    for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
94589      assert( pItem->iCol>0  && pItem->iCol<=p->pEList->nExpr );
94590      aPermute[i] = pItem->iCol - 1;
94591    }
94592    pKeyMerge =
94593      sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
94594    if( pKeyMerge ){
94595      pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
94596      pKeyMerge->nField = (u16)nOrderBy;
94597      pKeyMerge->enc = ENC(db);
94598      for(i=0; i<nOrderBy; i++){
94599        CollSeq *pColl;
94600        Expr *pTerm = pOrderBy->a[i].pExpr;
94601        if( pTerm->flags & EP_ExpCollate ){
94602          pColl = pTerm->pColl;
94603        }else{
94604          pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
94605          pTerm->flags |= EP_ExpCollate;
94606          pTerm->pColl = pColl;
94607        }
94608        pKeyMerge->aColl[i] = pColl;
94609        pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
94610      }
94611    }
94612  }else{
94613    pKeyMerge = 0;
94614  }
94615
94616  /* Reattach the ORDER BY clause to the query.
94617  */
94618  p->pOrderBy = pOrderBy;
94619  pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
94620
94621  /* Allocate a range of temporary registers and the KeyInfo needed
94622  ** for the logic that removes duplicate result rows when the
94623  ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
94624  */
94625  if( op==TK_ALL ){
94626    regPrev = 0;
94627  }else{
94628    int nExpr = p->pEList->nExpr;
94629    assert( nOrderBy>=nExpr || db->mallocFailed );
94630    regPrev = sqlite3GetTempRange(pParse, nExpr+1);
94631    sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
94632    pKeyDup = sqlite3DbMallocZero(db,
94633                  sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
94634    if( pKeyDup ){
94635      pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
94636      pKeyDup->nField = (u16)nExpr;
94637      pKeyDup->enc = ENC(db);
94638      for(i=0; i<nExpr; i++){
94639        pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
94640        pKeyDup->aSortOrder[i] = 0;
94641      }
94642    }
94643  }
94644
94645  /* Separate the left and the right query from one another
94646  */
94647  p->pPrior = 0;
94648  sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
94649  if( pPrior->pPrior==0 ){
94650    sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
94651  }
94652
94653  /* Compute the limit registers */
94654  computeLimitRegisters(pParse, p, labelEnd);
94655  if( p->iLimit && op==TK_ALL ){
94656    regLimitA = ++pParse->nMem;
94657    regLimitB = ++pParse->nMem;
94658    sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
94659                                  regLimitA);
94660    sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
94661  }else{
94662    regLimitA = regLimitB = 0;
94663  }
94664  sqlite3ExprDelete(db, p->pLimit);
94665  p->pLimit = 0;
94666  sqlite3ExprDelete(db, p->pOffset);
94667  p->pOffset = 0;
94668
94669  regAddrA = ++pParse->nMem;
94670  regEofA = ++pParse->nMem;
94671  regAddrB = ++pParse->nMem;
94672  regEofB = ++pParse->nMem;
94673  regOutA = ++pParse->nMem;
94674  regOutB = ++pParse->nMem;
94675  sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
94676  sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
94677
94678  /* Jump past the various subroutines and coroutines to the main
94679  ** merge loop
94680  */
94681  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
94682  addrSelectA = sqlite3VdbeCurrentAddr(v);
94683
94684
94685  /* Generate a coroutine to evaluate the SELECT statement to the
94686  ** left of the compound operator - the "A" select.
94687  */
94688  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
94689  pPrior->iLimit = regLimitA;
94690  explainSetInteger(iSub1, pParse->iNextSelectId);
94691  sqlite3Select(pParse, pPrior, &destA);
94692  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
94693  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
94694  VdbeNoopComment((v, "End coroutine for left SELECT"));
94695
94696  /* Generate a coroutine to evaluate the SELECT statement on
94697  ** the right - the "B" select
94698  */
94699  addrSelectB = sqlite3VdbeCurrentAddr(v);
94700  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
94701  savedLimit = p->iLimit;
94702  savedOffset = p->iOffset;
94703  p->iLimit = regLimitB;
94704  p->iOffset = 0;
94705  explainSetInteger(iSub2, pParse->iNextSelectId);
94706  sqlite3Select(pParse, p, &destB);
94707  p->iLimit = savedLimit;
94708  p->iOffset = savedOffset;
94709  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
94710  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
94711  VdbeNoopComment((v, "End coroutine for right SELECT"));
94712
94713  /* Generate a subroutine that outputs the current row of the A
94714  ** select as the next output row of the compound select.
94715  */
94716  VdbeNoopComment((v, "Output routine for A"));
94717  addrOutA = generateOutputSubroutine(pParse,
94718                 p, &destA, pDest, regOutA,
94719                 regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
94720
94721  /* Generate a subroutine that outputs the current row of the B
94722  ** select as the next output row of the compound select.
94723  */
94724  if( op==TK_ALL || op==TK_UNION ){
94725    VdbeNoopComment((v, "Output routine for B"));
94726    addrOutB = generateOutputSubroutine(pParse,
94727                 p, &destB, pDest, regOutB,
94728                 regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
94729  }
94730
94731  /* Generate a subroutine to run when the results from select A
94732  ** are exhausted and only data in select B remains.
94733  */
94734  VdbeNoopComment((v, "eof-A subroutine"));
94735  if( op==TK_EXCEPT || op==TK_INTERSECT ){
94736    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
94737  }else{
94738    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
94739    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
94740    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
94741    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
94742    p->nSelectRow += pPrior->nSelectRow;
94743  }
94744
94745  /* Generate a subroutine to run when the results from select B
94746  ** are exhausted and only data in select A remains.
94747  */
94748  if( op==TK_INTERSECT ){
94749    addrEofB = addrEofA;
94750    if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
94751  }else{
94752    VdbeNoopComment((v, "eof-B subroutine"));
94753    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
94754    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
94755    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
94756    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
94757  }
94758
94759  /* Generate code to handle the case of A<B
94760  */
94761  VdbeNoopComment((v, "A-lt-B subroutine"));
94762  addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
94763  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
94764  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
94765  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
94766
94767  /* Generate code to handle the case of A==B
94768  */
94769  if( op==TK_ALL ){
94770    addrAeqB = addrAltB;
94771  }else if( op==TK_INTERSECT ){
94772    addrAeqB = addrAltB;
94773    addrAltB++;
94774  }else{
94775    VdbeNoopComment((v, "A-eq-B subroutine"));
94776    addrAeqB =
94777    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
94778    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
94779    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
94780  }
94781
94782  /* Generate code to handle the case of A>B
94783  */
94784  VdbeNoopComment((v, "A-gt-B subroutine"));
94785  addrAgtB = sqlite3VdbeCurrentAddr(v);
94786  if( op==TK_ALL || op==TK_UNION ){
94787    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
94788  }
94789  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
94790  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
94791  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
94792
94793  /* This code runs once to initialize everything.
94794  */
94795  sqlite3VdbeJumpHere(v, j1);
94796  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
94797  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
94798  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
94799  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
94800  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
94801  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
94802
94803  /* Implement the main merge loop
94804  */
94805  sqlite3VdbeResolveLabel(v, labelCmpr);
94806  sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
94807  sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
94808                         (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
94809  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
94810
94811  /* Release temporary registers
94812  */
94813  if( regPrev ){
94814    sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
94815  }
94816
94817  /* Jump to the this point in order to terminate the query.
94818  */
94819  sqlite3VdbeResolveLabel(v, labelEnd);
94820
94821  /* Set the number of output columns
94822  */
94823  if( pDest->eDest==SRT_Output ){
94824    Select *pFirst = pPrior;
94825    while( pFirst->pPrior ) pFirst = pFirst->pPrior;
94826    generateColumnNames(pParse, 0, pFirst->pEList);
94827  }
94828
94829  /* Reassembly the compound query so that it will be freed correctly
94830  ** by the calling function */
94831  if( p->pPrior ){
94832    sqlite3SelectDelete(db, p->pPrior);
94833  }
94834  p->pPrior = pPrior;
94835
94836  /*** TBD:  Insert subroutine calls to close cursors on incomplete
94837  **** subqueries ****/
94838  explainComposite(pParse, p->op, iSub1, iSub2, 0);
94839  return SQLITE_OK;
94840}
94841#endif
94842
94843#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
94844/* Forward Declarations */
94845static void substExprList(sqlite3*, ExprList*, int, ExprList*);
94846static void substSelect(sqlite3*, Select *, int, ExprList *);
94847
94848/*
94849** Scan through the expression pExpr.  Replace every reference to
94850** a column in table number iTable with a copy of the iColumn-th
94851** entry in pEList.  (But leave references to the ROWID column
94852** unchanged.)
94853**
94854** This routine is part of the flattening procedure.  A subquery
94855** whose result set is defined by pEList appears as entry in the
94856** FROM clause of a SELECT such that the VDBE cursor assigned to that
94857** FORM clause entry is iTable.  This routine make the necessary
94858** changes to pExpr so that it refers directly to the source table
94859** of the subquery rather the result set of the subquery.
94860*/
94861static Expr *substExpr(
94862  sqlite3 *db,        /* Report malloc errors to this connection */
94863  Expr *pExpr,        /* Expr in which substitution occurs */
94864  int iTable,         /* Table to be substituted */
94865  ExprList *pEList    /* Substitute expressions */
94866){
94867  if( pExpr==0 ) return 0;
94868  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
94869    if( pExpr->iColumn<0 ){
94870      pExpr->op = TK_NULL;
94871    }else{
94872      Expr *pNew;
94873      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
94874      assert( pExpr->pLeft==0 && pExpr->pRight==0 );
94875      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
94876      if( pNew && pExpr->pColl ){
94877        pNew->pColl = pExpr->pColl;
94878      }
94879      sqlite3ExprDelete(db, pExpr);
94880      pExpr = pNew;
94881    }
94882  }else{
94883    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
94884    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
94885    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
94886      substSelect(db, pExpr->x.pSelect, iTable, pEList);
94887    }else{
94888      substExprList(db, pExpr->x.pList, iTable, pEList);
94889    }
94890  }
94891  return pExpr;
94892}
94893static void substExprList(
94894  sqlite3 *db,         /* Report malloc errors here */
94895  ExprList *pList,     /* List to scan and in which to make substitutes */
94896  int iTable,          /* Table to be substituted */
94897  ExprList *pEList     /* Substitute values */
94898){
94899  int i;
94900  if( pList==0 ) return;
94901  for(i=0; i<pList->nExpr; i++){
94902    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
94903  }
94904}
94905static void substSelect(
94906  sqlite3 *db,         /* Report malloc errors here */
94907  Select *p,           /* SELECT statement in which to make substitutions */
94908  int iTable,          /* Table to be replaced */
94909  ExprList *pEList     /* Substitute values */
94910){
94911  SrcList *pSrc;
94912  struct SrcList_item *pItem;
94913  int i;
94914  if( !p ) return;
94915  substExprList(db, p->pEList, iTable, pEList);
94916  substExprList(db, p->pGroupBy, iTable, pEList);
94917  substExprList(db, p->pOrderBy, iTable, pEList);
94918  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
94919  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
94920  substSelect(db, p->pPrior, iTable, pEList);
94921  pSrc = p->pSrc;
94922  assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
94923  if( ALWAYS(pSrc) ){
94924    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
94925      substSelect(db, pItem->pSelect, iTable, pEList);
94926    }
94927  }
94928}
94929#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
94930
94931#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
94932/*
94933** This routine attempts to flatten subqueries in order to speed
94934** execution.  It returns 1 if it makes changes and 0 if no flattening
94935** occurs.
94936**
94937** To understand the concept of flattening, consider the following
94938** query:
94939**
94940**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
94941**
94942** The default way of implementing this query is to execute the
94943** subquery first and store the results in a temporary table, then
94944** run the outer query on that temporary table.  This requires two
94945** passes over the data.  Furthermore, because the temporary table
94946** has no indices, the WHERE clause on the outer query cannot be
94947** optimized.
94948**
94949** This routine attempts to rewrite queries such as the above into
94950** a single flat select, like this:
94951**
94952**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
94953**
94954** The code generated for this simpification gives the same result
94955** but only has to scan the data once.  And because indices might
94956** exist on the table t1, a complete scan of the data might be
94957** avoided.
94958**
94959** Flattening is only attempted if all of the following are true:
94960**
94961**   (1)  The subquery and the outer query do not both use aggregates.
94962**
94963**   (2)  The subquery is not an aggregate or the outer query is not a join.
94964**
94965**   (3)  The subquery is not the right operand of a left outer join
94966**        (Originally ticket #306.  Strengthened by ticket #3300)
94967**
94968**   (4)  The subquery is not DISTINCT.
94969**
94970**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
94971**        sub-queries that were excluded from this optimization. Restriction
94972**        (4) has since been expanded to exclude all DISTINCT subqueries.
94973**
94974**   (6)  The subquery does not use aggregates or the outer query is not
94975**        DISTINCT.
94976**
94977**   (7)  The subquery has a FROM clause.
94978**
94979**   (8)  The subquery does not use LIMIT or the outer query is not a join.
94980**
94981**   (9)  The subquery does not use LIMIT or the outer query does not use
94982**        aggregates.
94983**
94984**  (10)  The subquery does not use aggregates or the outer query does not
94985**        use LIMIT.
94986**
94987**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
94988**
94989**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
94990**        a separate restriction deriving from ticket #350.
94991**
94992**  (13)  The subquery and outer query do not both use LIMIT.
94993**
94994**  (14)  The subquery does not use OFFSET.
94995**
94996**  (15)  The outer query is not part of a compound select or the
94997**        subquery does not have a LIMIT clause.
94998**        (See ticket #2339 and ticket [02a8e81d44]).
94999**
95000**  (16)  The outer query is not an aggregate or the subquery does
95001**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
95002**        until we introduced the group_concat() function.
95003**
95004**  (17)  The sub-query is not a compound select, or it is a UNION ALL
95005**        compound clause made up entirely of non-aggregate queries, and
95006**        the parent query:
95007**
95008**          * is not itself part of a compound select,
95009**          * is not an aggregate or DISTINCT query, and
95010**          * has no other tables or sub-selects in the FROM clause.
95011**
95012**        The parent and sub-query may contain WHERE clauses. Subject to
95013**        rules (11), (13) and (14), they may also contain ORDER BY,
95014**        LIMIT and OFFSET clauses.
95015**
95016**  (18)  If the sub-query is a compound select, then all terms of the
95017**        ORDER by clause of the parent must be simple references to
95018**        columns of the sub-query.
95019**
95020**  (19)  The subquery does not use LIMIT or the outer query does not
95021**        have a WHERE clause.
95022**
95023**  (20)  If the sub-query is a compound select, then it must not use
95024**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
95025**        somewhat by saying that the terms of the ORDER BY clause must
95026**        appear as unmodified result columns in the outer query.  But
95027**        have other optimizations in mind to deal with that case.
95028**
95029**  (21)  The subquery does not use LIMIT or the outer query is not
95030**        DISTINCT.  (See ticket [752e1646fc]).
95031**
95032** In this routine, the "p" parameter is a pointer to the outer query.
95033** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
95034** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
95035**
95036** If flattening is not attempted, this routine is a no-op and returns 0.
95037** If flattening is attempted this routine returns 1.
95038**
95039** All of the expression analysis must occur on both the outer query and
95040** the subquery before this routine runs.
95041*/
95042static int flattenSubquery(
95043  Parse *pParse,       /* Parsing context */
95044  Select *p,           /* The parent or outer SELECT statement */
95045  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
95046  int isAgg,           /* True if outer SELECT uses aggregate functions */
95047  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
95048){
95049  const char *zSavedAuthContext = pParse->zAuthContext;
95050  Select *pParent;
95051  Select *pSub;       /* The inner query or "subquery" */
95052  Select *pSub1;      /* Pointer to the rightmost select in sub-query */
95053  SrcList *pSrc;      /* The FROM clause of the outer query */
95054  SrcList *pSubSrc;   /* The FROM clause of the subquery */
95055  ExprList *pList;    /* The result set of the outer query */
95056  int iParent;        /* VDBE cursor number of the pSub result set temp table */
95057  int i;              /* Loop counter */
95058  Expr *pWhere;                    /* The WHERE clause */
95059  struct SrcList_item *pSubitem;   /* The subquery */
95060  sqlite3 *db = pParse->db;
95061
95062  /* Check to see if flattening is permitted.  Return 0 if not.
95063  */
95064  assert( p!=0 );
95065  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
95066  if( db->flags & SQLITE_QueryFlattener ) return 0;
95067  pSrc = p->pSrc;
95068  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
95069  pSubitem = &pSrc->a[iFrom];
95070  iParent = pSubitem->iCursor;
95071  pSub = pSubitem->pSelect;
95072  assert( pSub!=0 );
95073  if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
95074  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
95075  pSubSrc = pSub->pSrc;
95076  assert( pSubSrc );
95077  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
95078  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
95079  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
95080  ** became arbitrary expressions, we were forced to add restrictions (13)
95081  ** and (14). */
95082  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
95083  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
95084  if( p->pRightmost && pSub->pLimit ){
95085    return 0;                                            /* Restriction (15) */
95086  }
95087  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
95088  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
95089  if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
95090     return 0;         /* Restrictions (8)(9) */
95091  }
95092  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
95093     return 0;         /* Restriction (6)  */
95094  }
95095  if( p->pOrderBy && pSub->pOrderBy ){
95096     return 0;                                           /* Restriction (11) */
95097  }
95098  if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
95099  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
95100  if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
95101     return 0;         /* Restriction (21) */
95102  }
95103
95104  /* OBSOLETE COMMENT 1:
95105  ** Restriction 3:  If the subquery is a join, make sure the subquery is
95106  ** not used as the right operand of an outer join.  Examples of why this
95107  ** is not allowed:
95108  **
95109  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
95110  **
95111  ** If we flatten the above, we would get
95112  **
95113  **         (t1 LEFT OUTER JOIN t2) JOIN t3
95114  **
95115  ** which is not at all the same thing.
95116  **
95117  ** OBSOLETE COMMENT 2:
95118  ** Restriction 12:  If the subquery is the right operand of a left outer
95119  ** join, make sure the subquery has no WHERE clause.
95120  ** An examples of why this is not allowed:
95121  **
95122  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
95123  **
95124  ** If we flatten the above, we would get
95125  **
95126  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
95127  **
95128  ** But the t2.x>0 test will always fail on a NULL row of t2, which
95129  ** effectively converts the OUTER JOIN into an INNER JOIN.
95130  **
95131  ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
95132  ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
95133  ** is fraught with danger.  Best to avoid the whole thing.  If the
95134  ** subquery is the right term of a LEFT JOIN, then do not flatten.
95135  */
95136  if( (pSubitem->jointype & JT_OUTER)!=0 ){
95137    return 0;
95138  }
95139
95140  /* Restriction 17: If the sub-query is a compound SELECT, then it must
95141  ** use only the UNION ALL operator. And none of the simple select queries
95142  ** that make up the compound SELECT are allowed to be aggregate or distinct
95143  ** queries.
95144  */
95145  if( pSub->pPrior ){
95146    if( pSub->pOrderBy ){
95147      return 0;  /* Restriction 20 */
95148    }
95149    if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
95150      return 0;
95151    }
95152    for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
95153      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
95154      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
95155      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
95156       || (pSub1->pPrior && pSub1->op!=TK_ALL)
95157       || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
95158      ){
95159        return 0;
95160      }
95161    }
95162
95163    /* Restriction 18. */
95164    if( p->pOrderBy ){
95165      int ii;
95166      for(ii=0; ii<p->pOrderBy->nExpr; ii++){
95167        if( p->pOrderBy->a[ii].iCol==0 ) return 0;
95168      }
95169    }
95170  }
95171
95172  /***** If we reach this point, flattening is permitted. *****/
95173
95174  /* Authorize the subquery */
95175  pParse->zAuthContext = pSubitem->zName;
95176  sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
95177  pParse->zAuthContext = zSavedAuthContext;
95178
95179  /* If the sub-query is a compound SELECT statement, then (by restrictions
95180  ** 17 and 18 above) it must be a UNION ALL and the parent query must
95181  ** be of the form:
95182  **
95183  **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
95184  **
95185  ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
95186  ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
95187  ** OFFSET clauses and joins them to the left-hand-side of the original
95188  ** using UNION ALL operators. In this case N is the number of simple
95189  ** select statements in the compound sub-query.
95190  **
95191  ** Example:
95192  **
95193  **     SELECT a+1 FROM (
95194  **        SELECT x FROM tab
95195  **        UNION ALL
95196  **        SELECT y FROM tab
95197  **        UNION ALL
95198  **        SELECT abs(z*2) FROM tab2
95199  **     ) WHERE a!=5 ORDER BY 1
95200  **
95201  ** Transformed into:
95202  **
95203  **     SELECT x+1 FROM tab WHERE x+1!=5
95204  **     UNION ALL
95205  **     SELECT y+1 FROM tab WHERE y+1!=5
95206  **     UNION ALL
95207  **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
95208  **     ORDER BY 1
95209  **
95210  ** We call this the "compound-subquery flattening".
95211  */
95212  for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
95213    Select *pNew;
95214    ExprList *pOrderBy = p->pOrderBy;
95215    Expr *pLimit = p->pLimit;
95216    Select *pPrior = p->pPrior;
95217    p->pOrderBy = 0;
95218    p->pSrc = 0;
95219    p->pPrior = 0;
95220    p->pLimit = 0;
95221    pNew = sqlite3SelectDup(db, p, 0);
95222    p->pLimit = pLimit;
95223    p->pOrderBy = pOrderBy;
95224    p->pSrc = pSrc;
95225    p->op = TK_ALL;
95226    p->pRightmost = 0;
95227    if( pNew==0 ){
95228      pNew = pPrior;
95229    }else{
95230      pNew->pPrior = pPrior;
95231      pNew->pRightmost = 0;
95232    }
95233    p->pPrior = pNew;
95234    if( db->mallocFailed ) return 1;
95235  }
95236
95237  /* Begin flattening the iFrom-th entry of the FROM clause
95238  ** in the outer query.
95239  */
95240  pSub = pSub1 = pSubitem->pSelect;
95241
95242  /* Delete the transient table structure associated with the
95243  ** subquery
95244  */
95245  sqlite3DbFree(db, pSubitem->zDatabase);
95246  sqlite3DbFree(db, pSubitem->zName);
95247  sqlite3DbFree(db, pSubitem->zAlias);
95248  pSubitem->zDatabase = 0;
95249  pSubitem->zName = 0;
95250  pSubitem->zAlias = 0;
95251  pSubitem->pSelect = 0;
95252
95253  /* Defer deleting the Table object associated with the
95254  ** subquery until code generation is
95255  ** complete, since there may still exist Expr.pTab entries that
95256  ** refer to the subquery even after flattening.  Ticket #3346.
95257  **
95258  ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
95259  */
95260  if( ALWAYS(pSubitem->pTab!=0) ){
95261    Table *pTabToDel = pSubitem->pTab;
95262    if( pTabToDel->nRef==1 ){
95263      Parse *pToplevel = sqlite3ParseToplevel(pParse);
95264      pTabToDel->pNextZombie = pToplevel->pZombieTab;
95265      pToplevel->pZombieTab = pTabToDel;
95266    }else{
95267      pTabToDel->nRef--;
95268    }
95269    pSubitem->pTab = 0;
95270  }
95271
95272  /* The following loop runs once for each term in a compound-subquery
95273  ** flattening (as described above).  If we are doing a different kind
95274  ** of flattening - a flattening other than a compound-subquery flattening -
95275  ** then this loop only runs once.
95276  **
95277  ** This loop moves all of the FROM elements of the subquery into the
95278  ** the FROM clause of the outer query.  Before doing this, remember
95279  ** the cursor number for the original outer query FROM element in
95280  ** iParent.  The iParent cursor will never be used.  Subsequent code
95281  ** will scan expressions looking for iParent references and replace
95282  ** those references with expressions that resolve to the subquery FROM
95283  ** elements we are now copying in.
95284  */
95285  for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
95286    int nSubSrc;
95287    u8 jointype = 0;
95288    pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
95289    nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
95290    pSrc = pParent->pSrc;     /* FROM clause of the outer query */
95291
95292    if( pSrc ){
95293      assert( pParent==p );  /* First time through the loop */
95294      jointype = pSubitem->jointype;
95295    }else{
95296      assert( pParent!=p );  /* 2nd and subsequent times through the loop */
95297      pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
95298      if( pSrc==0 ){
95299        assert( db->mallocFailed );
95300        break;
95301      }
95302    }
95303
95304    /* The subquery uses a single slot of the FROM clause of the outer
95305    ** query.  If the subquery has more than one element in its FROM clause,
95306    ** then expand the outer query to make space for it to hold all elements
95307    ** of the subquery.
95308    **
95309    ** Example:
95310    **
95311    **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
95312    **
95313    ** The outer query has 3 slots in its FROM clause.  One slot of the
95314    ** outer query (the middle slot) is used by the subquery.  The next
95315    ** block of code will expand the out query to 4 slots.  The middle
95316    ** slot is expanded to two slots in order to make space for the
95317    ** two elements in the FROM clause of the subquery.
95318    */
95319    if( nSubSrc>1 ){
95320      pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
95321      if( db->mallocFailed ){
95322        break;
95323      }
95324    }
95325
95326    /* Transfer the FROM clause terms from the subquery into the
95327    ** outer query.
95328    */
95329    for(i=0; i<nSubSrc; i++){
95330      sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
95331      pSrc->a[i+iFrom] = pSubSrc->a[i];
95332      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
95333    }
95334    pSrc->a[iFrom].jointype = jointype;
95335
95336    /* Now begin substituting subquery result set expressions for
95337    ** references to the iParent in the outer query.
95338    **
95339    ** Example:
95340    **
95341    **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
95342    **   \                     \_____________ subquery __________/          /
95343    **    \_____________________ outer query ______________________________/
95344    **
95345    ** We look at every expression in the outer query and every place we see
95346    ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
95347    */
95348    pList = pParent->pEList;
95349    for(i=0; i<pList->nExpr; i++){
95350      if( pList->a[i].zName==0 ){
95351        const char *zSpan = pList->a[i].zSpan;
95352        if( ALWAYS(zSpan) ){
95353          pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
95354        }
95355      }
95356    }
95357    substExprList(db, pParent->pEList, iParent, pSub->pEList);
95358    if( isAgg ){
95359      substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
95360      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
95361    }
95362    if( pSub->pOrderBy ){
95363      assert( pParent->pOrderBy==0 );
95364      pParent->pOrderBy = pSub->pOrderBy;
95365      pSub->pOrderBy = 0;
95366    }else if( pParent->pOrderBy ){
95367      substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
95368    }
95369    if( pSub->pWhere ){
95370      pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
95371    }else{
95372      pWhere = 0;
95373    }
95374    if( subqueryIsAgg ){
95375      assert( pParent->pHaving==0 );
95376      pParent->pHaving = pParent->pWhere;
95377      pParent->pWhere = pWhere;
95378      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
95379      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
95380                                  sqlite3ExprDup(db, pSub->pHaving, 0));
95381      assert( pParent->pGroupBy==0 );
95382      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
95383    }else{
95384      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
95385      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
95386    }
95387
95388    /* The flattened query is distinct if either the inner or the
95389    ** outer query is distinct.
95390    */
95391    pParent->selFlags |= pSub->selFlags & SF_Distinct;
95392
95393    /*
95394    ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
95395    **
95396    ** One is tempted to try to add a and b to combine the limits.  But this
95397    ** does not work if either limit is negative.
95398    */
95399    if( pSub->pLimit ){
95400      pParent->pLimit = pSub->pLimit;
95401      pSub->pLimit = 0;
95402    }
95403  }
95404
95405  /* Finially, delete what is left of the subquery and return
95406  ** success.
95407  */
95408  sqlite3SelectDelete(db, pSub1);
95409
95410  return 1;
95411}
95412#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
95413
95414/*
95415** Analyze the SELECT statement passed as an argument to see if it
95416** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if
95417** it is, or 0 otherwise. At present, a query is considered to be
95418** a min()/max() query if:
95419**
95420**   1. There is a single object in the FROM clause.
95421**
95422**   2. There is a single expression in the result set, and it is
95423**      either min(x) or max(x), where x is a column reference.
95424*/
95425static u8 minMaxQuery(Select *p){
95426  Expr *pExpr;
95427  ExprList *pEList = p->pEList;
95428
95429  if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
95430  pExpr = pEList->a[0].pExpr;
95431  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
95432  if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
95433  pEList = pExpr->x.pList;
95434  if( pEList==0 || pEList->nExpr!=1 ) return 0;
95435  if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
95436  assert( !ExprHasProperty(pExpr, EP_IntValue) );
95437  if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
95438    return WHERE_ORDERBY_MIN;
95439  }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
95440    return WHERE_ORDERBY_MAX;
95441  }
95442  return WHERE_ORDERBY_NORMAL;
95443}
95444
95445/*
95446** The select statement passed as the first argument is an aggregate query.
95447** The second argment is the associated aggregate-info object. This
95448** function tests if the SELECT is of the form:
95449**
95450**   SELECT count(*) FROM <tbl>
95451**
95452** where table is a database table, not a sub-select or view. If the query
95453** does match this pattern, then a pointer to the Table object representing
95454** <tbl> is returned. Otherwise, 0 is returned.
95455*/
95456static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
95457  Table *pTab;
95458  Expr *pExpr;
95459
95460  assert( !p->pGroupBy );
95461
95462  if( p->pWhere || p->pEList->nExpr!=1
95463   || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
95464  ){
95465    return 0;
95466  }
95467  pTab = p->pSrc->a[0].pTab;
95468  pExpr = p->pEList->a[0].pExpr;
95469  assert( pTab && !pTab->pSelect && pExpr );
95470
95471  if( IsVirtual(pTab) ) return 0;
95472  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
95473  if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
95474  if( pExpr->flags&EP_Distinct ) return 0;
95475
95476  return pTab;
95477}
95478
95479/*
95480** If the source-list item passed as an argument was augmented with an
95481** INDEXED BY clause, then try to locate the specified index. If there
95482** was such a clause and the named index cannot be found, return
95483** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
95484** pFrom->pIndex and return SQLITE_OK.
95485*/
95486SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
95487  if( pFrom->pTab && pFrom->zIndex ){
95488    Table *pTab = pFrom->pTab;
95489    char *zIndex = pFrom->zIndex;
95490    Index *pIdx;
95491    for(pIdx=pTab->pIndex;
95492        pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
95493        pIdx=pIdx->pNext
95494    );
95495    if( !pIdx ){
95496      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
95497      pParse->checkSchema = 1;
95498      return SQLITE_ERROR;
95499    }
95500    pFrom->pIndex = pIdx;
95501  }
95502  return SQLITE_OK;
95503}
95504
95505/*
95506** This routine is a Walker callback for "expanding" a SELECT statement.
95507** "Expanding" means to do the following:
95508**
95509**    (1)  Make sure VDBE cursor numbers have been assigned to every
95510**         element of the FROM clause.
95511**
95512**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
95513**         defines FROM clause.  When views appear in the FROM clause,
95514**         fill pTabList->a[].pSelect with a copy of the SELECT statement
95515**         that implements the view.  A copy is made of the view's SELECT
95516**         statement so that we can freely modify or delete that statement
95517**         without worrying about messing up the presistent representation
95518**         of the view.
95519**
95520**    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
95521**         on joins and the ON and USING clause of joins.
95522**
95523**    (4)  Scan the list of columns in the result set (pEList) looking
95524**         for instances of the "*" operator or the TABLE.* operator.
95525**         If found, expand each "*" to be every column in every table
95526**         and TABLE.* to be every column in TABLE.
95527**
95528*/
95529static int selectExpander(Walker *pWalker, Select *p){
95530  Parse *pParse = pWalker->pParse;
95531  int i, j, k;
95532  SrcList *pTabList;
95533  ExprList *pEList;
95534  struct SrcList_item *pFrom;
95535  sqlite3 *db = pParse->db;
95536
95537  if( db->mallocFailed  ){
95538    return WRC_Abort;
95539  }
95540  if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
95541    return WRC_Prune;
95542  }
95543  p->selFlags |= SF_Expanded;
95544  pTabList = p->pSrc;
95545  pEList = p->pEList;
95546
95547  /* Make sure cursor numbers have been assigned to all entries in
95548  ** the FROM clause of the SELECT statement.
95549  */
95550  sqlite3SrcListAssignCursors(pParse, pTabList);
95551
95552  /* Look up every table named in the FROM clause of the select.  If
95553  ** an entry of the FROM clause is a subquery instead of a table or view,
95554  ** then create a transient table structure to describe the subquery.
95555  */
95556  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
95557    Table *pTab;
95558    if( pFrom->pTab!=0 ){
95559      /* This statement has already been prepared.  There is no need
95560      ** to go further. */
95561      assert( i==0 );
95562      return WRC_Prune;
95563    }
95564    if( pFrom->zName==0 ){
95565#ifndef SQLITE_OMIT_SUBQUERY
95566      Select *pSel = pFrom->pSelect;
95567      /* A sub-query in the FROM clause of a SELECT */
95568      assert( pSel!=0 );
95569      assert( pFrom->pTab==0 );
95570      sqlite3WalkSelect(pWalker, pSel);
95571      pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
95572      if( pTab==0 ) return WRC_Abort;
95573      pTab->nRef = 1;
95574      pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
95575      while( pSel->pPrior ){ pSel = pSel->pPrior; }
95576      selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
95577      pTab->iPKey = -1;
95578      pTab->nRowEst = 1000000;
95579      pTab->tabFlags |= TF_Ephemeral;
95580#endif
95581    }else{
95582      /* An ordinary table or view name in the FROM clause */
95583      assert( pFrom->pTab==0 );
95584      pFrom->pTab = pTab =
95585        sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
95586      if( pTab==0 ) return WRC_Abort;
95587      pTab->nRef++;
95588#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
95589      if( pTab->pSelect || IsVirtual(pTab) ){
95590        /* We reach here if the named table is a really a view */
95591        if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
95592        assert( pFrom->pSelect==0 );
95593        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
95594        sqlite3WalkSelect(pWalker, pFrom->pSelect);
95595      }
95596#endif
95597    }
95598
95599    /* Locate the index named by the INDEXED BY clause, if any. */
95600    if( sqlite3IndexedByLookup(pParse, pFrom) ){
95601      return WRC_Abort;
95602    }
95603  }
95604
95605  /* Process NATURAL keywords, and ON and USING clauses of joins.
95606  */
95607  if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
95608    return WRC_Abort;
95609  }
95610
95611  /* For every "*" that occurs in the column list, insert the names of
95612  ** all columns in all tables.  And for every TABLE.* insert the names
95613  ** of all columns in TABLE.  The parser inserted a special expression
95614  ** with the TK_ALL operator for each "*" that it found in the column list.
95615  ** The following code just has to locate the TK_ALL expressions and expand
95616  ** each one to the list of all columns in all tables.
95617  **
95618  ** The first loop just checks to see if there are any "*" operators
95619  ** that need expanding.
95620  */
95621  for(k=0; k<pEList->nExpr; k++){
95622    Expr *pE = pEList->a[k].pExpr;
95623    if( pE->op==TK_ALL ) break;
95624    assert( pE->op!=TK_DOT || pE->pRight!=0 );
95625    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
95626    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
95627  }
95628  if( k<pEList->nExpr ){
95629    /*
95630    ** If we get here it means the result set contains one or more "*"
95631    ** operators that need to be expanded.  Loop through each expression
95632    ** in the result set and expand them one by one.
95633    */
95634    struct ExprList_item *a = pEList->a;
95635    ExprList *pNew = 0;
95636    int flags = pParse->db->flags;
95637    int longNames = (flags & SQLITE_FullColNames)!=0
95638                      && (flags & SQLITE_ShortColNames)==0;
95639
95640    for(k=0; k<pEList->nExpr; k++){
95641      Expr *pE = a[k].pExpr;
95642      assert( pE->op!=TK_DOT || pE->pRight!=0 );
95643      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
95644        /* This particular expression does not need to be expanded.
95645        */
95646        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
95647        if( pNew ){
95648          pNew->a[pNew->nExpr-1].zName = a[k].zName;
95649          pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
95650          a[k].zName = 0;
95651          a[k].zSpan = 0;
95652        }
95653        a[k].pExpr = 0;
95654      }else{
95655        /* This expression is a "*" or a "TABLE.*" and needs to be
95656        ** expanded. */
95657        int tableSeen = 0;      /* Set to 1 when TABLE matches */
95658        char *zTName;            /* text of name of TABLE */
95659        if( pE->op==TK_DOT ){
95660          assert( pE->pLeft!=0 );
95661          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
95662          zTName = pE->pLeft->u.zToken;
95663        }else{
95664          zTName = 0;
95665        }
95666        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
95667          Table *pTab = pFrom->pTab;
95668          char *zTabName = pFrom->zAlias;
95669          if( zTabName==0 ){
95670            zTabName = pTab->zName;
95671          }
95672          if( db->mallocFailed ) break;
95673          if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
95674            continue;
95675          }
95676          tableSeen = 1;
95677          for(j=0; j<pTab->nCol; j++){
95678            Expr *pExpr, *pRight;
95679            char *zName = pTab->aCol[j].zName;
95680            char *zColname;  /* The computed column name */
95681            char *zToFree;   /* Malloced string that needs to be freed */
95682            Token sColname;  /* Computed column name as a token */
95683
95684            /* If a column is marked as 'hidden' (currently only possible
95685            ** for virtual tables), do not include it in the expanded
95686            ** result-set list.
95687            */
95688            if( IsHiddenColumn(&pTab->aCol[j]) ){
95689              assert(IsVirtual(pTab));
95690              continue;
95691            }
95692
95693            if( i>0 && zTName==0 ){
95694              if( (pFrom->jointype & JT_NATURAL)!=0
95695                && tableAndColumnIndex(pTabList, i, zName, 0, 0)
95696              ){
95697                /* In a NATURAL join, omit the join columns from the
95698                ** table to the right of the join */
95699                continue;
95700              }
95701              if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
95702                /* In a join with a USING clause, omit columns in the
95703                ** using clause from the table on the right. */
95704                continue;
95705              }
95706            }
95707            pRight = sqlite3Expr(db, TK_ID, zName);
95708            zColname = zName;
95709            zToFree = 0;
95710            if( longNames || pTabList->nSrc>1 ){
95711              Expr *pLeft;
95712              pLeft = sqlite3Expr(db, TK_ID, zTabName);
95713              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
95714              if( longNames ){
95715                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
95716                zToFree = zColname;
95717              }
95718            }else{
95719              pExpr = pRight;
95720            }
95721            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
95722            sColname.z = zColname;
95723            sColname.n = sqlite3Strlen30(zColname);
95724            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
95725            sqlite3DbFree(db, zToFree);
95726          }
95727        }
95728        if( !tableSeen ){
95729          if( zTName ){
95730            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
95731          }else{
95732            sqlite3ErrorMsg(pParse, "no tables specified");
95733          }
95734        }
95735      }
95736    }
95737    sqlite3ExprListDelete(db, pEList);
95738    p->pEList = pNew;
95739  }
95740#if SQLITE_MAX_COLUMN
95741  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
95742    sqlite3ErrorMsg(pParse, "too many columns in result set");
95743  }
95744#endif
95745  return WRC_Continue;
95746}
95747
95748/*
95749** No-op routine for the parse-tree walker.
95750**
95751** When this routine is the Walker.xExprCallback then expression trees
95752** are walked without any actions being taken at each node.  Presumably,
95753** when this routine is used for Walker.xExprCallback then
95754** Walker.xSelectCallback is set to do something useful for every
95755** subquery in the parser tree.
95756*/
95757static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
95758  UNUSED_PARAMETER2(NotUsed, NotUsed2);
95759  return WRC_Continue;
95760}
95761
95762/*
95763** This routine "expands" a SELECT statement and all of its subqueries.
95764** For additional information on what it means to "expand" a SELECT
95765** statement, see the comment on the selectExpand worker callback above.
95766**
95767** Expanding a SELECT statement is the first step in processing a
95768** SELECT statement.  The SELECT statement must be expanded before
95769** name resolution is performed.
95770**
95771** If anything goes wrong, an error message is written into pParse.
95772** The calling function can detect the problem by looking at pParse->nErr
95773** and/or pParse->db->mallocFailed.
95774*/
95775static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
95776  Walker w;
95777  w.xSelectCallback = selectExpander;
95778  w.xExprCallback = exprWalkNoop;
95779  w.pParse = pParse;
95780  sqlite3WalkSelect(&w, pSelect);
95781}
95782
95783
95784#ifndef SQLITE_OMIT_SUBQUERY
95785/*
95786** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
95787** interface.
95788**
95789** For each FROM-clause subquery, add Column.zType and Column.zColl
95790** information to the Table structure that represents the result set
95791** of that subquery.
95792**
95793** The Table structure that represents the result set was constructed
95794** by selectExpander() but the type and collation information was omitted
95795** at that point because identifiers had not yet been resolved.  This
95796** routine is called after identifier resolution.
95797*/
95798static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
95799  Parse *pParse;
95800  int i;
95801  SrcList *pTabList;
95802  struct SrcList_item *pFrom;
95803
95804  assert( p->selFlags & SF_Resolved );
95805  if( (p->selFlags & SF_HasTypeInfo)==0 ){
95806    p->selFlags |= SF_HasTypeInfo;
95807    pParse = pWalker->pParse;
95808    pTabList = p->pSrc;
95809    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
95810      Table *pTab = pFrom->pTab;
95811      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
95812        /* A sub-query in the FROM clause of a SELECT */
95813        Select *pSel = pFrom->pSelect;
95814        assert( pSel );
95815        while( pSel->pPrior ) pSel = pSel->pPrior;
95816        selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
95817      }
95818    }
95819  }
95820  return WRC_Continue;
95821}
95822#endif
95823
95824
95825/*
95826** This routine adds datatype and collating sequence information to
95827** the Table structures of all FROM-clause subqueries in a
95828** SELECT statement.
95829**
95830** Use this routine after name resolution.
95831*/
95832static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
95833#ifndef SQLITE_OMIT_SUBQUERY
95834  Walker w;
95835  w.xSelectCallback = selectAddSubqueryTypeInfo;
95836  w.xExprCallback = exprWalkNoop;
95837  w.pParse = pParse;
95838  sqlite3WalkSelect(&w, pSelect);
95839#endif
95840}
95841
95842
95843/*
95844** This routine sets of a SELECT statement for processing.  The
95845** following is accomplished:
95846**
95847**     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
95848**     *  Ephemeral Table objects are created for all FROM-clause subqueries.
95849**     *  ON and USING clauses are shifted into WHERE statements
95850**     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
95851**     *  Identifiers in expression are matched to tables.
95852**
95853** This routine acts recursively on all subqueries within the SELECT.
95854*/
95855SQLITE_PRIVATE void sqlite3SelectPrep(
95856  Parse *pParse,         /* The parser context */
95857  Select *p,             /* The SELECT statement being coded. */
95858  NameContext *pOuterNC  /* Name context for container */
95859){
95860  sqlite3 *db;
95861  if( NEVER(p==0) ) return;
95862  db = pParse->db;
95863  if( p->selFlags & SF_HasTypeInfo ) return;
95864  sqlite3SelectExpand(pParse, p);
95865  if( pParse->nErr || db->mallocFailed ) return;
95866  sqlite3ResolveSelectNames(pParse, p, pOuterNC);
95867  if( pParse->nErr || db->mallocFailed ) return;
95868  sqlite3SelectAddTypeInfo(pParse, p);
95869}
95870
95871/*
95872** Reset the aggregate accumulator.
95873**
95874** The aggregate accumulator is a set of memory cells that hold
95875** intermediate results while calculating an aggregate.  This
95876** routine simply stores NULLs in all of those memory cells.
95877*/
95878static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
95879  Vdbe *v = pParse->pVdbe;
95880  int i;
95881  struct AggInfo_func *pFunc;
95882  if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
95883    return;
95884  }
95885  for(i=0; i<pAggInfo->nColumn; i++){
95886    sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
95887  }
95888  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
95889    sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
95890    if( pFunc->iDistinct>=0 ){
95891      Expr *pE = pFunc->pExpr;
95892      assert( !ExprHasProperty(pE, EP_xIsSelect) );
95893      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
95894        sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
95895           "argument");
95896        pFunc->iDistinct = -1;
95897      }else{
95898        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
95899        sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
95900                          (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
95901      }
95902    }
95903  }
95904}
95905
95906/*
95907** Invoke the OP_AggFinalize opcode for every aggregate function
95908** in the AggInfo structure.
95909*/
95910static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
95911  Vdbe *v = pParse->pVdbe;
95912  int i;
95913  struct AggInfo_func *pF;
95914  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
95915    ExprList *pList = pF->pExpr->x.pList;
95916    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
95917    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
95918                      (void*)pF->pFunc, P4_FUNCDEF);
95919  }
95920}
95921
95922/*
95923** Update the accumulator memory cells for an aggregate based on
95924** the current cursor position.
95925*/
95926static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
95927  Vdbe *v = pParse->pVdbe;
95928  int i;
95929  struct AggInfo_func *pF;
95930  struct AggInfo_col *pC;
95931
95932  pAggInfo->directMode = 1;
95933  sqlite3ExprCacheClear(pParse);
95934  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
95935    int nArg;
95936    int addrNext = 0;
95937    int regAgg;
95938    ExprList *pList = pF->pExpr->x.pList;
95939    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
95940    if( pList ){
95941      nArg = pList->nExpr;
95942      regAgg = sqlite3GetTempRange(pParse, nArg);
95943      sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
95944    }else{
95945      nArg = 0;
95946      regAgg = 0;
95947    }
95948    if( pF->iDistinct>=0 ){
95949      addrNext = sqlite3VdbeMakeLabel(v);
95950      assert( nArg==1 );
95951      codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
95952    }
95953    if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
95954      CollSeq *pColl = 0;
95955      struct ExprList_item *pItem;
95956      int j;
95957      assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
95958      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
95959        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
95960      }
95961      if( !pColl ){
95962        pColl = pParse->db->pDfltColl;
95963      }
95964      sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
95965    }
95966    sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
95967                      (void*)pF->pFunc, P4_FUNCDEF);
95968    sqlite3VdbeChangeP5(v, (u8)nArg);
95969    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
95970    sqlite3ReleaseTempRange(pParse, regAgg, nArg);
95971    if( addrNext ){
95972      sqlite3VdbeResolveLabel(v, addrNext);
95973      sqlite3ExprCacheClear(pParse);
95974    }
95975  }
95976
95977  /* Before populating the accumulator registers, clear the column cache.
95978  ** Otherwise, if any of the required column values are already present
95979  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
95980  ** to pC->iMem. But by the time the value is used, the original register
95981  ** may have been used, invalidating the underlying buffer holding the
95982  ** text or blob value. See ticket [883034dcb5].
95983  **
95984  ** Another solution would be to change the OP_SCopy used to copy cached
95985  ** values to an OP_Copy.
95986  */
95987  sqlite3ExprCacheClear(pParse);
95988  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
95989    sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
95990  }
95991  pAggInfo->directMode = 0;
95992  sqlite3ExprCacheClear(pParse);
95993}
95994
95995/*
95996** Add a single OP_Explain instruction to the VDBE to explain a simple
95997** count(*) query ("SELECT count(*) FROM pTab").
95998*/
95999#ifndef SQLITE_OMIT_EXPLAIN
96000static void explainSimpleCount(
96001  Parse *pParse,                  /* Parse context */
96002  Table *pTab,                    /* Table being queried */
96003  Index *pIdx                     /* Index used to optimize scan, or NULL */
96004){
96005  if( pParse->explain==2 ){
96006    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
96007        pTab->zName,
96008        pIdx ? "USING COVERING INDEX " : "",
96009        pIdx ? pIdx->zName : "",
96010        pTab->nRowEst
96011    );
96012    sqlite3VdbeAddOp4(
96013        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
96014    );
96015  }
96016}
96017#else
96018# define explainSimpleCount(a,b,c)
96019#endif
96020
96021/*
96022** Generate code for the SELECT statement given in the p argument.
96023**
96024** The results are distributed in various ways depending on the
96025** contents of the SelectDest structure pointed to by argument pDest
96026** as follows:
96027**
96028**     pDest->eDest    Result
96029**     ------------    -------------------------------------------
96030**     SRT_Output      Generate a row of output (using the OP_ResultRow
96031**                     opcode) for each row in the result set.
96032**
96033**     SRT_Mem         Only valid if the result is a single column.
96034**                     Store the first column of the first result row
96035**                     in register pDest->iParm then abandon the rest
96036**                     of the query.  This destination implies "LIMIT 1".
96037**
96038**     SRT_Set         The result must be a single column.  Store each
96039**                     row of result as the key in table pDest->iParm.
96040**                     Apply the affinity pDest->affinity before storing
96041**                     results.  Used to implement "IN (SELECT ...)".
96042**
96043**     SRT_Union       Store results as a key in a temporary table pDest->iParm.
96044**
96045**     SRT_Except      Remove results from the temporary table pDest->iParm.
96046**
96047**     SRT_Table       Store results in temporary table pDest->iParm.
96048**                     This is like SRT_EphemTab except that the table
96049**                     is assumed to already be open.
96050**
96051**     SRT_EphemTab    Create an temporary table pDest->iParm and store
96052**                     the result there. The cursor is left open after
96053**                     returning.  This is like SRT_Table except that
96054**                     this destination uses OP_OpenEphemeral to create
96055**                     the table first.
96056**
96057**     SRT_Coroutine   Generate a co-routine that returns a new row of
96058**                     results each time it is invoked.  The entry point
96059**                     of the co-routine is stored in register pDest->iParm.
96060**
96061**     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
96062**                     set is not empty.
96063**
96064**     SRT_Discard     Throw the results away.  This is used by SELECT
96065**                     statements within triggers whose only purpose is
96066**                     the side-effects of functions.
96067**
96068** This routine returns the number of errors.  If any errors are
96069** encountered, then an appropriate error message is left in
96070** pParse->zErrMsg.
96071**
96072** This routine does NOT free the Select structure passed in.  The
96073** calling function needs to do that.
96074*/
96075SQLITE_PRIVATE int sqlite3Select(
96076  Parse *pParse,         /* The parser context */
96077  Select *p,             /* The SELECT statement being coded. */
96078  SelectDest *pDest      /* What to do with the query results */
96079){
96080  int i, j;              /* Loop counters */
96081  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
96082  Vdbe *v;               /* The virtual machine under construction */
96083  int isAgg;             /* True for select lists like "count(*)" */
96084  ExprList *pEList;      /* List of columns to extract. */
96085  SrcList *pTabList;     /* List of tables to select from */
96086  Expr *pWhere;          /* The WHERE clause.  May be NULL */
96087  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
96088  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
96089  Expr *pHaving;         /* The HAVING clause.  May be NULL */
96090  int isDistinct;        /* True if the DISTINCT keyword is present */
96091  int distinct;          /* Table to use for the distinct set */
96092  int rc = 1;            /* Value to return from this function */
96093  int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
96094  int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
96095  AggInfo sAggInfo;      /* Information used by aggregate queries */
96096  int iEnd;              /* Address of the end of the query */
96097  sqlite3 *db;           /* The database connection */
96098
96099#ifndef SQLITE_OMIT_EXPLAIN
96100  int iRestoreSelectId = pParse->iSelectId;
96101  pParse->iSelectId = pParse->iNextSelectId++;
96102#endif
96103
96104  db = pParse->db;
96105  if( p==0 || db->mallocFailed || pParse->nErr ){
96106    return 1;
96107  }
96108  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
96109  memset(&sAggInfo, 0, sizeof(sAggInfo));
96110
96111  if( IgnorableOrderby(pDest) ){
96112    assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
96113           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
96114    /* If ORDER BY makes no difference in the output then neither does
96115    ** DISTINCT so it can be removed too. */
96116    sqlite3ExprListDelete(db, p->pOrderBy);
96117    p->pOrderBy = 0;
96118    p->selFlags &= ~SF_Distinct;
96119  }
96120  sqlite3SelectPrep(pParse, p, 0);
96121  pOrderBy = p->pOrderBy;
96122  pTabList = p->pSrc;
96123  pEList = p->pEList;
96124  if( pParse->nErr || db->mallocFailed ){
96125    goto select_end;
96126  }
96127  isAgg = (p->selFlags & SF_Aggregate)!=0;
96128  assert( pEList!=0 );
96129
96130  /* Begin generating code.
96131  */
96132  v = sqlite3GetVdbe(pParse);
96133  if( v==0 ) goto select_end;
96134
96135  /* If writing to memory or generating a set
96136  ** only a single column may be output.
96137  */
96138#ifndef SQLITE_OMIT_SUBQUERY
96139  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
96140    goto select_end;
96141  }
96142#endif
96143
96144  /* Generate code for all sub-queries in the FROM clause
96145  */
96146#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
96147  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
96148    struct SrcList_item *pItem = &pTabList->a[i];
96149    SelectDest dest;
96150    Select *pSub = pItem->pSelect;
96151    int isAggSub;
96152
96153    if( pSub==0 ) continue;
96154    if( pItem->addrFillSub ){
96155      sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
96156      continue;
96157    }
96158
96159    /* Increment Parse.nHeight by the height of the largest expression
96160    ** tree refered to by this, the parent select. The child select
96161    ** may contain expression trees of at most
96162    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
96163    ** more conservative than necessary, but much easier than enforcing
96164    ** an exact limit.
96165    */
96166    pParse->nHeight += sqlite3SelectExprHeight(p);
96167
96168    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
96169    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
96170      /* This subquery can be absorbed into its parent. */
96171      if( isAggSub ){
96172        isAgg = 1;
96173        p->selFlags |= SF_Aggregate;
96174      }
96175      i = -1;
96176    }else{
96177      /* Generate a subroutine that will fill an ephemeral table with
96178      ** the content of this subquery.  pItem->addrFillSub will point
96179      ** to the address of the generated subroutine.  pItem->regReturn
96180      ** is a register allocated to hold the subroutine return address
96181      */
96182      int topAddr;
96183      int onceAddr = 0;
96184      int retAddr;
96185      assert( pItem->addrFillSub==0 );
96186      pItem->regReturn = ++pParse->nMem;
96187      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
96188      pItem->addrFillSub = topAddr+1;
96189      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
96190      if( pItem->isCorrelated==0 && pParse->pTriggerTab==0 ){
96191        /* If the subquery is no correlated and if we are not inside of
96192        ** a trigger, then we only need to compute the value of the subquery
96193        ** once. */
96194        int regOnce = ++pParse->nMem;
96195        onceAddr = sqlite3VdbeAddOp1(v, OP_Once, regOnce);
96196      }
96197      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
96198      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
96199      sqlite3Select(pParse, pSub, &dest);
96200      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
96201      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
96202      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
96203      VdbeComment((v, "end %s", pItem->pTab->zName));
96204      sqlite3VdbeChangeP1(v, topAddr, retAddr);
96205
96206    }
96207    if( /*pParse->nErr ||*/ db->mallocFailed ){
96208      goto select_end;
96209    }
96210    pParse->nHeight -= sqlite3SelectExprHeight(p);
96211    pTabList = p->pSrc;
96212    if( !IgnorableOrderby(pDest) ){
96213      pOrderBy = p->pOrderBy;
96214    }
96215  }
96216  pEList = p->pEList;
96217#endif
96218  pWhere = p->pWhere;
96219  pGroupBy = p->pGroupBy;
96220  pHaving = p->pHaving;
96221  isDistinct = (p->selFlags & SF_Distinct)!=0;
96222
96223#ifndef SQLITE_OMIT_COMPOUND_SELECT
96224  /* If there is are a sequence of queries, do the earlier ones first.
96225  */
96226  if( p->pPrior ){
96227    if( p->pRightmost==0 ){
96228      Select *pLoop, *pRight = 0;
96229      int cnt = 0;
96230      int mxSelect;
96231      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
96232        pLoop->pRightmost = p;
96233        pLoop->pNext = pRight;
96234        pRight = pLoop;
96235      }
96236      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
96237      if( mxSelect && cnt>mxSelect ){
96238        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
96239        goto select_end;
96240      }
96241    }
96242    rc = multiSelect(pParse, p, pDest);
96243    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
96244    return rc;
96245  }
96246#endif
96247
96248  /* If there is both a GROUP BY and an ORDER BY clause and they are
96249  ** identical, then disable the ORDER BY clause since the GROUP BY
96250  ** will cause elements to come out in the correct order.  This is
96251  ** an optimization - the correct answer should result regardless.
96252  ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
96253  ** to disable this optimization for testing purposes.
96254  */
96255  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
96256         && (db->flags & SQLITE_GroupByOrder)==0 ){
96257    pOrderBy = 0;
96258  }
96259
96260  /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
96261  ** if the select-list is the same as the ORDER BY list, then this query
96262  ** can be rewritten as a GROUP BY. In other words, this:
96263  **
96264  **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
96265  **
96266  ** is transformed to:
96267  **
96268  **     SELECT xyz FROM ... GROUP BY xyz
96269  **
96270  ** The second form is preferred as a single index (or temp-table) may be
96271  ** used for both the ORDER BY and DISTINCT processing. As originally
96272  ** written the query must use a temp-table for at least one of the ORDER
96273  ** BY and DISTINCT, and an index or separate temp-table for the other.
96274  */
96275  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
96276   && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
96277  ){
96278    p->selFlags &= ~SF_Distinct;
96279    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
96280    pGroupBy = p->pGroupBy;
96281    pOrderBy = 0;
96282  }
96283
96284  /* If there is an ORDER BY clause, then this sorting
96285  ** index might end up being unused if the data can be
96286  ** extracted in pre-sorted order.  If that is the case, then the
96287  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
96288  ** we figure out that the sorting index is not needed.  The addrSortIndex
96289  ** variable is used to facilitate that change.
96290  */
96291  if( pOrderBy ){
96292    KeyInfo *pKeyInfo;
96293    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
96294    pOrderBy->iECursor = pParse->nTab++;
96295    p->addrOpenEphm[2] = addrSortIndex =
96296      sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
96297                           pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
96298                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
96299  }else{
96300    addrSortIndex = -1;
96301  }
96302
96303  /* If the output is destined for a temporary table, open that table.
96304  */
96305  if( pDest->eDest==SRT_EphemTab ){
96306    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
96307  }
96308
96309  /* Set the limiter.
96310  */
96311  iEnd = sqlite3VdbeMakeLabel(v);
96312  p->nSelectRow = (double)LARGEST_INT64;
96313  computeLimitRegisters(pParse, p, iEnd);
96314  if( p->iLimit==0 && addrSortIndex>=0 ){
96315    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
96316    p->selFlags |= SF_UseSorter;
96317  }
96318
96319  /* Open a virtual index to use for the distinct set.
96320  */
96321  if( p->selFlags & SF_Distinct ){
96322    KeyInfo *pKeyInfo;
96323    distinct = pParse->nTab++;
96324    pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
96325    addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
96326        (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
96327    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
96328  }else{
96329    distinct = addrDistinctIndex = -1;
96330  }
96331
96332  /* Aggregate and non-aggregate queries are handled differently */
96333  if( !isAgg && pGroupBy==0 ){
96334    ExprList *pDist = (isDistinct ? p->pEList : 0);
96335
96336    /* Begin the database scan. */
96337    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0);
96338    if( pWInfo==0 ) goto select_end;
96339    if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
96340
96341    /* If sorting index that was created by a prior OP_OpenEphemeral
96342    ** instruction ended up not being needed, then change the OP_OpenEphemeral
96343    ** into an OP_Noop.
96344    */
96345    if( addrSortIndex>=0 && pOrderBy==0 ){
96346      sqlite3VdbeChangeToNoop(v, addrSortIndex);
96347      p->addrOpenEphm[2] = -1;
96348    }
96349
96350    if( pWInfo->eDistinct ){
96351      VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
96352
96353      assert( addrDistinctIndex>=0 );
96354      pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
96355
96356      assert( isDistinct );
96357      assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
96358           || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
96359      );
96360      distinct = -1;
96361      if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
96362        int iJump;
96363        int iExpr;
96364        int iFlag = ++pParse->nMem;
96365        int iBase = pParse->nMem+1;
96366        int iBase2 = iBase + pEList->nExpr;
96367        pParse->nMem += (pEList->nExpr*2);
96368
96369        /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
96370        ** OP_Integer initializes the "first row" flag.  */
96371        pOp->opcode = OP_Integer;
96372        pOp->p1 = 1;
96373        pOp->p2 = iFlag;
96374
96375        sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
96376        iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
96377        sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
96378        for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
96379          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
96380          sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
96381          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
96382          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
96383        }
96384        sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
96385
96386        sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
96387        assert( sqlite3VdbeCurrentAddr(v)==iJump );
96388        sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
96389      }else{
96390        pOp->opcode = OP_Noop;
96391      }
96392    }
96393
96394    /* Use the standard inner loop. */
96395    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
96396                    pWInfo->iContinue, pWInfo->iBreak);
96397
96398    /* End the database scan loop.
96399    */
96400    sqlite3WhereEnd(pWInfo);
96401  }else{
96402    /* This is the processing for aggregate queries */
96403    NameContext sNC;    /* Name context for processing aggregate information */
96404    int iAMem;          /* First Mem address for storing current GROUP BY */
96405    int iBMem;          /* First Mem address for previous GROUP BY */
96406    int iUseFlag;       /* Mem address holding flag indicating that at least
96407                        ** one row of the input to the aggregator has been
96408                        ** processed */
96409    int iAbortFlag;     /* Mem address which causes query abort if positive */
96410    int groupBySort;    /* Rows come from source in GROUP BY order */
96411    int addrEnd;        /* End of processing for this SELECT */
96412    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
96413    int sortOut = 0;    /* Output register from the sorter */
96414
96415    /* Remove any and all aliases between the result set and the
96416    ** GROUP BY clause.
96417    */
96418    if( pGroupBy ){
96419      int k;                        /* Loop counter */
96420      struct ExprList_item *pItem;  /* For looping over expression in a list */
96421
96422      for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
96423        pItem->iAlias = 0;
96424      }
96425      for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
96426        pItem->iAlias = 0;
96427      }
96428      if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
96429    }else{
96430      p->nSelectRow = (double)1;
96431    }
96432
96433
96434    /* Create a label to jump to when we want to abort the query */
96435    addrEnd = sqlite3VdbeMakeLabel(v);
96436
96437    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
96438    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
96439    ** SELECT statement.
96440    */
96441    memset(&sNC, 0, sizeof(sNC));
96442    sNC.pParse = pParse;
96443    sNC.pSrcList = pTabList;
96444    sNC.pAggInfo = &sAggInfo;
96445    sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
96446    sAggInfo.pGroupBy = pGroupBy;
96447    sqlite3ExprAnalyzeAggList(&sNC, pEList);
96448    sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
96449    if( pHaving ){
96450      sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
96451    }
96452    sAggInfo.nAccumulator = sAggInfo.nColumn;
96453    for(i=0; i<sAggInfo.nFunc; i++){
96454      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
96455      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
96456    }
96457    if( db->mallocFailed ) goto select_end;
96458
96459    /* Processing for aggregates with GROUP BY is very different and
96460    ** much more complex than aggregates without a GROUP BY.
96461    */
96462    if( pGroupBy ){
96463      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
96464      int j1;             /* A-vs-B comparision jump */
96465      int addrOutputRow;  /* Start of subroutine that outputs a result row */
96466      int regOutputRow;   /* Return address register for output subroutine */
96467      int addrSetAbort;   /* Set the abort flag and return */
96468      int addrTopOfLoop;  /* Top of the input loop */
96469      int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
96470      int addrReset;      /* Subroutine for resetting the accumulator */
96471      int regReset;       /* Return address register for reset subroutine */
96472
96473      /* If there is a GROUP BY clause we might need a sorting index to
96474      ** implement it.  Allocate that sorting index now.  If it turns out
96475      ** that we do not need it after all, the OP_SorterOpen instruction
96476      ** will be converted into a Noop.
96477      */
96478      sAggInfo.sortingIdx = pParse->nTab++;
96479      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
96480      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
96481          sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
96482          0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
96483
96484      /* Initialize memory locations used by GROUP BY aggregate processing
96485      */
96486      iUseFlag = ++pParse->nMem;
96487      iAbortFlag = ++pParse->nMem;
96488      regOutputRow = ++pParse->nMem;
96489      addrOutputRow = sqlite3VdbeMakeLabel(v);
96490      regReset = ++pParse->nMem;
96491      addrReset = sqlite3VdbeMakeLabel(v);
96492      iAMem = pParse->nMem + 1;
96493      pParse->nMem += pGroupBy->nExpr;
96494      iBMem = pParse->nMem + 1;
96495      pParse->nMem += pGroupBy->nExpr;
96496      sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
96497      VdbeComment((v, "clear abort flag"));
96498      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
96499      VdbeComment((v, "indicate accumulator empty"));
96500
96501      /* Begin a loop that will extract all source rows in GROUP BY order.
96502      ** This might involve two separate loops with an OP_Sort in between, or
96503      ** it might be a single loop that uses an index to extract information
96504      ** in the right order to begin with.
96505      */
96506      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
96507      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0);
96508      if( pWInfo==0 ) goto select_end;
96509      if( pGroupBy==0 ){
96510        /* The optimizer is able to deliver rows in group by order so
96511        ** we do not have to sort.  The OP_OpenEphemeral table will be
96512        ** cancelled later because we still need to use the pKeyInfo
96513        */
96514        pGroupBy = p->pGroupBy;
96515        groupBySort = 0;
96516      }else{
96517        /* Rows are coming out in undetermined order.  We have to push
96518        ** each row into a sorting index, terminate the first loop,
96519        ** then loop over the sorting index in order to get the output
96520        ** in sorted order
96521        */
96522        int regBase;
96523        int regRecord;
96524        int nCol;
96525        int nGroupBy;
96526
96527        explainTempTable(pParse,
96528            isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
96529
96530        groupBySort = 1;
96531        nGroupBy = pGroupBy->nExpr;
96532        nCol = nGroupBy + 1;
96533        j = nGroupBy+1;
96534        for(i=0; i<sAggInfo.nColumn; i++){
96535          if( sAggInfo.aCol[i].iSorterColumn>=j ){
96536            nCol++;
96537            j++;
96538          }
96539        }
96540        regBase = sqlite3GetTempRange(pParse, nCol);
96541        sqlite3ExprCacheClear(pParse);
96542        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
96543        sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
96544        j = nGroupBy+1;
96545        for(i=0; i<sAggInfo.nColumn; i++){
96546          struct AggInfo_col *pCol = &sAggInfo.aCol[i];
96547          if( pCol->iSorterColumn>=j ){
96548            int r1 = j + regBase;
96549            int r2;
96550
96551            r2 = sqlite3ExprCodeGetColumn(pParse,
96552                               pCol->pTab, pCol->iColumn, pCol->iTable, r1);
96553            if( r1!=r2 ){
96554              sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
96555            }
96556            j++;
96557          }
96558        }
96559        regRecord = sqlite3GetTempReg(pParse);
96560        sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
96561        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
96562        sqlite3ReleaseTempReg(pParse, regRecord);
96563        sqlite3ReleaseTempRange(pParse, regBase, nCol);
96564        sqlite3WhereEnd(pWInfo);
96565        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
96566        sortOut = sqlite3GetTempReg(pParse);
96567        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
96568        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
96569        VdbeComment((v, "GROUP BY sort"));
96570        sAggInfo.useSortingIdx = 1;
96571        sqlite3ExprCacheClear(pParse);
96572      }
96573
96574      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
96575      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
96576      ** Then compare the current GROUP BY terms against the GROUP BY terms
96577      ** from the previous row currently stored in a0, a1, a2...
96578      */
96579      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
96580      sqlite3ExprCacheClear(pParse);
96581      if( groupBySort ){
96582        sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
96583      }
96584      for(j=0; j<pGroupBy->nExpr; j++){
96585        if( groupBySort ){
96586          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
96587          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
96588        }else{
96589          sAggInfo.directMode = 1;
96590          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
96591        }
96592      }
96593      sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
96594                          (char*)pKeyInfo, P4_KEYINFO);
96595      j1 = sqlite3VdbeCurrentAddr(v);
96596      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
96597
96598      /* Generate code that runs whenever the GROUP BY changes.
96599      ** Changes in the GROUP BY are detected by the previous code
96600      ** block.  If there were no changes, this block is skipped.
96601      **
96602      ** This code copies current group by terms in b0,b1,b2,...
96603      ** over to a0,a1,a2.  It then calls the output subroutine
96604      ** and resets the aggregate accumulator registers in preparation
96605      ** for the next GROUP BY batch.
96606      */
96607      sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
96608      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
96609      VdbeComment((v, "output one row"));
96610      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
96611      VdbeComment((v, "check abort flag"));
96612      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
96613      VdbeComment((v, "reset accumulator"));
96614
96615      /* Update the aggregate accumulators based on the content of
96616      ** the current row
96617      */
96618      sqlite3VdbeJumpHere(v, j1);
96619      updateAccumulator(pParse, &sAggInfo);
96620      sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
96621      VdbeComment((v, "indicate data in accumulator"));
96622
96623      /* End of the loop
96624      */
96625      if( groupBySort ){
96626        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
96627      }else{
96628        sqlite3WhereEnd(pWInfo);
96629        sqlite3VdbeChangeToNoop(v, addrSortingIdx);
96630      }
96631
96632      /* Output the final row of result
96633      */
96634      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
96635      VdbeComment((v, "output final row"));
96636
96637      /* Jump over the subroutines
96638      */
96639      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
96640
96641      /* Generate a subroutine that outputs a single row of the result
96642      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
96643      ** is less than or equal to zero, the subroutine is a no-op.  If
96644      ** the processing calls for the query to abort, this subroutine
96645      ** increments the iAbortFlag memory location before returning in
96646      ** order to signal the caller to abort.
96647      */
96648      addrSetAbort = sqlite3VdbeCurrentAddr(v);
96649      sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
96650      VdbeComment((v, "set abort flag"));
96651      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
96652      sqlite3VdbeResolveLabel(v, addrOutputRow);
96653      addrOutputRow = sqlite3VdbeCurrentAddr(v);
96654      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
96655      VdbeComment((v, "Groupby result generator entry point"));
96656      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
96657      finalizeAggFunctions(pParse, &sAggInfo);
96658      sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
96659      selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
96660                      distinct, pDest,
96661                      addrOutputRow+1, addrSetAbort);
96662      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
96663      VdbeComment((v, "end groupby result generator"));
96664
96665      /* Generate a subroutine that will reset the group-by accumulator
96666      */
96667      sqlite3VdbeResolveLabel(v, addrReset);
96668      resetAccumulator(pParse, &sAggInfo);
96669      sqlite3VdbeAddOp1(v, OP_Return, regReset);
96670
96671    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
96672    else {
96673      ExprList *pDel = 0;
96674#ifndef SQLITE_OMIT_BTREECOUNT
96675      Table *pTab;
96676      if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
96677        /* If isSimpleCount() returns a pointer to a Table structure, then
96678        ** the SQL statement is of the form:
96679        **
96680        **   SELECT count(*) FROM <tbl>
96681        **
96682        ** where the Table structure returned represents table <tbl>.
96683        **
96684        ** This statement is so common that it is optimized specially. The
96685        ** OP_Count instruction is executed either on the intkey table that
96686        ** contains the data for table <tbl> or on one of its indexes. It
96687        ** is better to execute the op on an index, as indexes are almost
96688        ** always spread across less pages than their corresponding tables.
96689        */
96690        const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
96691        const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
96692        Index *pIdx;                         /* Iterator variable */
96693        KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
96694        Index *pBest = 0;                    /* Best index found so far */
96695        int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
96696
96697        sqlite3CodeVerifySchema(pParse, iDb);
96698        sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
96699
96700        /* Search for the index that has the least amount of columns. If
96701        ** there is such an index, and it has less columns than the table
96702        ** does, then we can assume that it consumes less space on disk and
96703        ** will therefore be cheaper to scan to determine the query result.
96704        ** In this case set iRoot to the root page number of the index b-tree
96705        ** and pKeyInfo to the KeyInfo structure required to navigate the
96706        ** index.
96707        **
96708        ** (2011-04-15) Do not do a full scan of an unordered index.
96709        **
96710        ** In practice the KeyInfo structure will not be used. It is only
96711        ** passed to keep OP_OpenRead happy.
96712        */
96713        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
96714          if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
96715            pBest = pIdx;
96716          }
96717        }
96718        if( pBest && pBest->nColumn<pTab->nCol ){
96719          iRoot = pBest->tnum;
96720          pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
96721        }
96722
96723        /* Open a read-only cursor, execute the OP_Count, close the cursor. */
96724        sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
96725        if( pKeyInfo ){
96726          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
96727        }
96728        sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
96729        sqlite3VdbeAddOp1(v, OP_Close, iCsr);
96730        explainSimpleCount(pParse, pTab, pBest);
96731      }else
96732#endif /* SQLITE_OMIT_BTREECOUNT */
96733      {
96734        /* Check if the query is of one of the following forms:
96735        **
96736        **   SELECT min(x) FROM ...
96737        **   SELECT max(x) FROM ...
96738        **
96739        ** If it is, then ask the code in where.c to attempt to sort results
96740        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
96741        ** If where.c is able to produce results sorted in this order, then
96742        ** add vdbe code to break out of the processing loop after the
96743        ** first iteration (since the first iteration of the loop is
96744        ** guaranteed to operate on the row with the minimum or maximum
96745        ** value of x, the only row required).
96746        **
96747        ** A special flag must be passed to sqlite3WhereBegin() to slightly
96748        ** modify behaviour as follows:
96749        **
96750        **   + If the query is a "SELECT min(x)", then the loop coded by
96751        **     where.c should not iterate over any values with a NULL value
96752        **     for x.
96753        **
96754        **   + The optimizer code in where.c (the thing that decides which
96755        **     index or indices to use) should place a different priority on
96756        **     satisfying the 'ORDER BY' clause than it does in other cases.
96757        **     Refer to code and comments in where.c for details.
96758        */
96759        ExprList *pMinMax = 0;
96760        u8 flag = minMaxQuery(p);
96761        if( flag ){
96762          assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
96763          pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
96764          pDel = pMinMax;
96765          if( pMinMax && !db->mallocFailed ){
96766            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
96767            pMinMax->a[0].pExpr->op = TK_COLUMN;
96768          }
96769        }
96770
96771        /* This case runs if the aggregate has no GROUP BY clause.  The
96772        ** processing is much simpler since there is only a single row
96773        ** of output.
96774        */
96775        resetAccumulator(pParse, &sAggInfo);
96776        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, 0, flag);
96777        if( pWInfo==0 ){
96778          sqlite3ExprListDelete(db, pDel);
96779          goto select_end;
96780        }
96781        updateAccumulator(pParse, &sAggInfo);
96782        if( !pMinMax && flag ){
96783          sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
96784          VdbeComment((v, "%s() by index",
96785                (flag==WHERE_ORDERBY_MIN?"min":"max")));
96786        }
96787        sqlite3WhereEnd(pWInfo);
96788        finalizeAggFunctions(pParse, &sAggInfo);
96789      }
96790
96791      pOrderBy = 0;
96792      sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
96793      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
96794                      pDest, addrEnd, addrEnd);
96795      sqlite3ExprListDelete(db, pDel);
96796    }
96797    sqlite3VdbeResolveLabel(v, addrEnd);
96798
96799  } /* endif aggregate query */
96800
96801  if( distinct>=0 ){
96802    explainTempTable(pParse, "DISTINCT");
96803  }
96804
96805  /* If there is an ORDER BY clause, then we need to sort the results
96806  ** and send them to the callback one by one.
96807  */
96808  if( pOrderBy ){
96809    explainTempTable(pParse, "ORDER BY");
96810    generateSortTail(pParse, p, v, pEList->nExpr, pDest);
96811  }
96812
96813  /* Jump here to skip this query
96814  */
96815  sqlite3VdbeResolveLabel(v, iEnd);
96816
96817  /* The SELECT was successfully coded.   Set the return code to 0
96818  ** to indicate no errors.
96819  */
96820  rc = 0;
96821
96822  /* Control jumps to here if an error is encountered above, or upon
96823  ** successful coding of the SELECT.
96824  */
96825select_end:
96826  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
96827
96828  /* Identify column names if results of the SELECT are to be output.
96829  */
96830  if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
96831    generateColumnNames(pParse, pTabList, pEList);
96832  }
96833
96834  sqlite3DbFree(db, sAggInfo.aCol);
96835  sqlite3DbFree(db, sAggInfo.aFunc);
96836  return rc;
96837}
96838
96839#if defined(SQLITE_DEBUG)
96840/*
96841*******************************************************************************
96842** The following code is used for testing and debugging only.  The code
96843** that follows does not appear in normal builds.
96844**
96845** These routines are used to print out the content of all or part of a
96846** parse structures such as Select or Expr.  Such printouts are useful
96847** for helping to understand what is happening inside the code generator
96848** during the execution of complex SELECT statements.
96849**
96850** These routine are not called anywhere from within the normal
96851** code base.  Then are intended to be called from within the debugger
96852** or from temporary "printf" statements inserted for debugging.
96853*/
96854SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
96855  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
96856    sqlite3DebugPrintf("(%s", p->u.zToken);
96857  }else{
96858    sqlite3DebugPrintf("(%d", p->op);
96859  }
96860  if( p->pLeft ){
96861    sqlite3DebugPrintf(" ");
96862    sqlite3PrintExpr(p->pLeft);
96863  }
96864  if( p->pRight ){
96865    sqlite3DebugPrintf(" ");
96866    sqlite3PrintExpr(p->pRight);
96867  }
96868  sqlite3DebugPrintf(")");
96869}
96870SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
96871  int i;
96872  for(i=0; i<pList->nExpr; i++){
96873    sqlite3PrintExpr(pList->a[i].pExpr);
96874    if( i<pList->nExpr-1 ){
96875      sqlite3DebugPrintf(", ");
96876    }
96877  }
96878}
96879SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
96880  sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
96881  sqlite3PrintExprList(p->pEList);
96882  sqlite3DebugPrintf("\n");
96883  if( p->pSrc ){
96884    char *zPrefix;
96885    int i;
96886    zPrefix = "FROM";
96887    for(i=0; i<p->pSrc->nSrc; i++){
96888      struct SrcList_item *pItem = &p->pSrc->a[i];
96889      sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
96890      zPrefix = "";
96891      if( pItem->pSelect ){
96892        sqlite3DebugPrintf("(\n");
96893        sqlite3PrintSelect(pItem->pSelect, indent+10);
96894        sqlite3DebugPrintf("%*s)", indent+8, "");
96895      }else if( pItem->zName ){
96896        sqlite3DebugPrintf("%s", pItem->zName);
96897      }
96898      if( pItem->pTab ){
96899        sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
96900      }
96901      if( pItem->zAlias ){
96902        sqlite3DebugPrintf(" AS %s", pItem->zAlias);
96903      }
96904      if( i<p->pSrc->nSrc-1 ){
96905        sqlite3DebugPrintf(",");
96906      }
96907      sqlite3DebugPrintf("\n");
96908    }
96909  }
96910  if( p->pWhere ){
96911    sqlite3DebugPrintf("%*s WHERE ", indent, "");
96912    sqlite3PrintExpr(p->pWhere);
96913    sqlite3DebugPrintf("\n");
96914  }
96915  if( p->pGroupBy ){
96916    sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
96917    sqlite3PrintExprList(p->pGroupBy);
96918    sqlite3DebugPrintf("\n");
96919  }
96920  if( p->pHaving ){
96921    sqlite3DebugPrintf("%*s HAVING ", indent, "");
96922    sqlite3PrintExpr(p->pHaving);
96923    sqlite3DebugPrintf("\n");
96924  }
96925  if( p->pOrderBy ){
96926    sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
96927    sqlite3PrintExprList(p->pOrderBy);
96928    sqlite3DebugPrintf("\n");
96929  }
96930}
96931/* End of the structure debug printing code
96932*****************************************************************************/
96933#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
96934
96935/************** End of select.c **********************************************/
96936/************** Begin file table.c *******************************************/
96937/*
96938** 2001 September 15
96939**
96940** The author disclaims copyright to this source code.  In place of
96941** a legal notice, here is a blessing:
96942**
96943**    May you do good and not evil.
96944**    May you find forgiveness for yourself and forgive others.
96945**    May you share freely, never taking more than you give.
96946**
96947*************************************************************************
96948** This file contains the sqlite3_get_table() and sqlite3_free_table()
96949** interface routines.  These are just wrappers around the main
96950** interface routine of sqlite3_exec().
96951**
96952** These routines are in a separate files so that they will not be linked
96953** if they are not used.
96954*/
96955/* #include <stdlib.h> */
96956/* #include <string.h> */
96957
96958#ifndef SQLITE_OMIT_GET_TABLE
96959
96960/*
96961** This structure is used to pass data from sqlite3_get_table() through
96962** to the callback function is uses to build the result.
96963*/
96964typedef struct TabResult {
96965  char **azResult;   /* Accumulated output */
96966  char *zErrMsg;     /* Error message text, if an error occurs */
96967  int nAlloc;        /* Slots allocated for azResult[] */
96968  int nRow;          /* Number of rows in the result */
96969  int nColumn;       /* Number of columns in the result */
96970  int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
96971  int rc;            /* Return code from sqlite3_exec() */
96972} TabResult;
96973
96974/*
96975** This routine is called once for each row in the result table.  Its job
96976** is to fill in the TabResult structure appropriately, allocating new
96977** memory as necessary.
96978*/
96979static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
96980  TabResult *p = (TabResult*)pArg;  /* Result accumulator */
96981  int need;                         /* Slots needed in p->azResult[] */
96982  int i;                            /* Loop counter */
96983  char *z;                          /* A single column of result */
96984
96985  /* Make sure there is enough space in p->azResult to hold everything
96986  ** we need to remember from this invocation of the callback.
96987  */
96988  if( p->nRow==0 && argv!=0 ){
96989    need = nCol*2;
96990  }else{
96991    need = nCol;
96992  }
96993  if( p->nData + need > p->nAlloc ){
96994    char **azNew;
96995    p->nAlloc = p->nAlloc*2 + need;
96996    azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
96997    if( azNew==0 ) goto malloc_failed;
96998    p->azResult = azNew;
96999  }
97000
97001  /* If this is the first row, then generate an extra row containing
97002  ** the names of all columns.
97003  */
97004  if( p->nRow==0 ){
97005    p->nColumn = nCol;
97006    for(i=0; i<nCol; i++){
97007      z = sqlite3_mprintf("%s", colv[i]);
97008      if( z==0 ) goto malloc_failed;
97009      p->azResult[p->nData++] = z;
97010    }
97011  }else if( p->nColumn!=nCol ){
97012    sqlite3_free(p->zErrMsg);
97013    p->zErrMsg = sqlite3_mprintf(
97014       "sqlite3_get_table() called with two or more incompatible queries"
97015    );
97016    p->rc = SQLITE_ERROR;
97017    return 1;
97018  }
97019
97020  /* Copy over the row data
97021  */
97022  if( argv!=0 ){
97023    for(i=0; i<nCol; i++){
97024      if( argv[i]==0 ){
97025        z = 0;
97026      }else{
97027        int n = sqlite3Strlen30(argv[i])+1;
97028        z = sqlite3_malloc( n );
97029        if( z==0 ) goto malloc_failed;
97030        memcpy(z, argv[i], n);
97031      }
97032      p->azResult[p->nData++] = z;
97033    }
97034    p->nRow++;
97035  }
97036  return 0;
97037
97038malloc_failed:
97039  p->rc = SQLITE_NOMEM;
97040  return 1;
97041}
97042
97043/*
97044** Query the database.  But instead of invoking a callback for each row,
97045** malloc() for space to hold the result and return the entire results
97046** at the conclusion of the call.
97047**
97048** The result that is written to ***pazResult is held in memory obtained
97049** from malloc().  But the caller cannot free this memory directly.
97050** Instead, the entire table should be passed to sqlite3_free_table() when
97051** the calling procedure is finished using it.
97052*/
97053SQLITE_API int sqlite3_get_table(
97054  sqlite3 *db,                /* The database on which the SQL executes */
97055  const char *zSql,           /* The SQL to be executed */
97056  char ***pazResult,          /* Write the result table here */
97057  int *pnRow,                 /* Write the number of rows in the result here */
97058  int *pnColumn,              /* Write the number of columns of result here */
97059  char **pzErrMsg             /* Write error messages here */
97060){
97061  int rc;
97062  TabResult res;
97063
97064  *pazResult = 0;
97065  if( pnColumn ) *pnColumn = 0;
97066  if( pnRow ) *pnRow = 0;
97067  if( pzErrMsg ) *pzErrMsg = 0;
97068  res.zErrMsg = 0;
97069  res.nRow = 0;
97070  res.nColumn = 0;
97071  res.nData = 1;
97072  res.nAlloc = 20;
97073  res.rc = SQLITE_OK;
97074  res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
97075  if( res.azResult==0 ){
97076     db->errCode = SQLITE_NOMEM;
97077     return SQLITE_NOMEM;
97078  }
97079  res.azResult[0] = 0;
97080  rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
97081  assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
97082  res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
97083  if( (rc&0xff)==SQLITE_ABORT ){
97084    sqlite3_free_table(&res.azResult[1]);
97085    if( res.zErrMsg ){
97086      if( pzErrMsg ){
97087        sqlite3_free(*pzErrMsg);
97088        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
97089      }
97090      sqlite3_free(res.zErrMsg);
97091    }
97092    db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
97093    return res.rc;
97094  }
97095  sqlite3_free(res.zErrMsg);
97096  if( rc!=SQLITE_OK ){
97097    sqlite3_free_table(&res.azResult[1]);
97098    return rc;
97099  }
97100  if( res.nAlloc>res.nData ){
97101    char **azNew;
97102    azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
97103    if( azNew==0 ){
97104      sqlite3_free_table(&res.azResult[1]);
97105      db->errCode = SQLITE_NOMEM;
97106      return SQLITE_NOMEM;
97107    }
97108    res.azResult = azNew;
97109  }
97110  *pazResult = &res.azResult[1];
97111  if( pnColumn ) *pnColumn = res.nColumn;
97112  if( pnRow ) *pnRow = res.nRow;
97113  return rc;
97114}
97115
97116/*
97117** This routine frees the space the sqlite3_get_table() malloced.
97118*/
97119SQLITE_API void sqlite3_free_table(
97120  char **azResult            /* Result returned from from sqlite3_get_table() */
97121){
97122  if( azResult ){
97123    int i, n;
97124    azResult--;
97125    assert( azResult!=0 );
97126    n = SQLITE_PTR_TO_INT(azResult[0]);
97127    for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
97128    sqlite3_free(azResult);
97129  }
97130}
97131
97132#endif /* SQLITE_OMIT_GET_TABLE */
97133
97134/************** End of table.c ***********************************************/
97135/************** Begin file trigger.c *****************************************/
97136/*
97137**
97138** The author disclaims copyright to this source code.  In place of
97139** a legal notice, here is a blessing:
97140**
97141**    May you do good and not evil.
97142**    May you find forgiveness for yourself and forgive others.
97143**    May you share freely, never taking more than you give.
97144**
97145*************************************************************************
97146** This file contains the implementation for TRIGGERs
97147*/
97148
97149#ifndef SQLITE_OMIT_TRIGGER
97150/*
97151** Delete a linked list of TriggerStep structures.
97152*/
97153SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
97154  while( pTriggerStep ){
97155    TriggerStep * pTmp = pTriggerStep;
97156    pTriggerStep = pTriggerStep->pNext;
97157
97158    sqlite3ExprDelete(db, pTmp->pWhere);
97159    sqlite3ExprListDelete(db, pTmp->pExprList);
97160    sqlite3SelectDelete(db, pTmp->pSelect);
97161    sqlite3IdListDelete(db, pTmp->pIdList);
97162
97163    sqlite3DbFree(db, pTmp);
97164  }
97165}
97166
97167/*
97168** Given table pTab, return a list of all the triggers attached to
97169** the table. The list is connected by Trigger.pNext pointers.
97170**
97171** All of the triggers on pTab that are in the same database as pTab
97172** are already attached to pTab->pTrigger.  But there might be additional
97173** triggers on pTab in the TEMP schema.  This routine prepends all
97174** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
97175** and returns the combined list.
97176**
97177** To state it another way:  This routine returns a list of all triggers
97178** that fire off of pTab.  The list will include any TEMP triggers on
97179** pTab as well as the triggers lised in pTab->pTrigger.
97180*/
97181SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
97182  Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
97183  Trigger *pList = 0;                  /* List of triggers to return */
97184
97185  if( pParse->disableTriggers ){
97186    return 0;
97187  }
97188
97189  if( pTmpSchema!=pTab->pSchema ){
97190    HashElem *p;
97191    assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
97192    for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
97193      Trigger *pTrig = (Trigger *)sqliteHashData(p);
97194      if( pTrig->pTabSchema==pTab->pSchema
97195       && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
97196      ){
97197        pTrig->pNext = (pList ? pList : pTab->pTrigger);
97198        pList = pTrig;
97199      }
97200    }
97201  }
97202
97203  return (pList ? pList : pTab->pTrigger);
97204}
97205
97206/*
97207** This is called by the parser when it sees a CREATE TRIGGER statement
97208** up to the point of the BEGIN before the trigger actions.  A Trigger
97209** structure is generated based on the information available and stored
97210** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
97211** sqlite3FinishTrigger() function is called to complete the trigger
97212** construction process.
97213*/
97214SQLITE_PRIVATE void sqlite3BeginTrigger(
97215  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
97216  Token *pName1,      /* The name of the trigger */
97217  Token *pName2,      /* The name of the trigger */
97218  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
97219  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
97220  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
97221  SrcList *pTableName,/* The name of the table/view the trigger applies to */
97222  Expr *pWhen,        /* WHEN clause */
97223  int isTemp,         /* True if the TEMPORARY keyword is present */
97224  int noErr           /* Suppress errors if the trigger already exists */
97225){
97226  Trigger *pTrigger = 0;  /* The new trigger */
97227  Table *pTab;            /* Table that the trigger fires off of */
97228  char *zName = 0;        /* Name of the trigger */
97229  sqlite3 *db = pParse->db;  /* The database connection */
97230  int iDb;                /* The database to store the trigger in */
97231  Token *pName;           /* The unqualified db name */
97232  DbFixer sFix;           /* State vector for the DB fixer */
97233  int iTabDb;             /* Index of the database holding pTab */
97234
97235  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
97236  assert( pName2!=0 );
97237  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
97238  assert( op>0 && op<0xff );
97239  if( isTemp ){
97240    /* If TEMP was specified, then the trigger name may not be qualified. */
97241    if( pName2->n>0 ){
97242      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
97243      goto trigger_cleanup;
97244    }
97245    iDb = 1;
97246    pName = pName1;
97247  }else{
97248    /* Figure out the db that the the trigger will be created in */
97249    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
97250    if( iDb<0 ){
97251      goto trigger_cleanup;
97252    }
97253  }
97254  if( !pTableName || db->mallocFailed ){
97255    goto trigger_cleanup;
97256  }
97257
97258  /* A long-standing parser bug is that this syntax was allowed:
97259  **
97260  **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
97261  **                                                 ^^^^^^^^
97262  **
97263  ** To maintain backwards compatibility, ignore the database
97264  ** name on pTableName if we are reparsing our of SQLITE_MASTER.
97265  */
97266  if( db->init.busy && iDb!=1 ){
97267    sqlite3DbFree(db, pTableName->a[0].zDatabase);
97268    pTableName->a[0].zDatabase = 0;
97269  }
97270
97271  /* If the trigger name was unqualified, and the table is a temp table,
97272  ** then set iDb to 1 to create the trigger in the temporary database.
97273  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
97274  ** exist, the error is caught by the block below.
97275  */
97276  pTab = sqlite3SrcListLookup(pParse, pTableName);
97277  if( db->init.busy==0 && pName2->n==0 && pTab
97278        && pTab->pSchema==db->aDb[1].pSchema ){
97279    iDb = 1;
97280  }
97281
97282  /* Ensure the table name matches database name and that the table exists */
97283  if( db->mallocFailed ) goto trigger_cleanup;
97284  assert( pTableName->nSrc==1 );
97285  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
97286      sqlite3FixSrcList(&sFix, pTableName) ){
97287    goto trigger_cleanup;
97288  }
97289  pTab = sqlite3SrcListLookup(pParse, pTableName);
97290  if( !pTab ){
97291    /* The table does not exist. */
97292    if( db->init.iDb==1 ){
97293      /* Ticket #3810.
97294      ** Normally, whenever a table is dropped, all associated triggers are
97295      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
97296      ** and the table is dropped by a different database connection, the
97297      ** trigger is not visible to the database connection that does the
97298      ** drop so the trigger cannot be dropped.  This results in an
97299      ** "orphaned trigger" - a trigger whose associated table is missing.
97300      */
97301      db->init.orphanTrigger = 1;
97302    }
97303    goto trigger_cleanup;
97304  }
97305  if( IsVirtual(pTab) ){
97306    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
97307    goto trigger_cleanup;
97308  }
97309
97310  /* Check that the trigger name is not reserved and that no trigger of the
97311  ** specified name exists */
97312  zName = sqlite3NameFromToken(db, pName);
97313  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
97314    goto trigger_cleanup;
97315  }
97316  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97317  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
97318                      zName, sqlite3Strlen30(zName)) ){
97319    if( !noErr ){
97320      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
97321    }else{
97322      assert( !db->init.busy );
97323      sqlite3CodeVerifySchema(pParse, iDb);
97324    }
97325    goto trigger_cleanup;
97326  }
97327
97328  /* Do not create a trigger on a system table */
97329  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
97330    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
97331    pParse->nErr++;
97332    goto trigger_cleanup;
97333  }
97334
97335  /* INSTEAD of triggers are only for views and views only support INSTEAD
97336  ** of triggers.
97337  */
97338  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
97339    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
97340        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
97341    goto trigger_cleanup;
97342  }
97343  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
97344    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
97345        " trigger on table: %S", pTableName, 0);
97346    goto trigger_cleanup;
97347  }
97348  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97349
97350#ifndef SQLITE_OMIT_AUTHORIZATION
97351  {
97352    int code = SQLITE_CREATE_TRIGGER;
97353    const char *zDb = db->aDb[iTabDb].zName;
97354    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
97355    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
97356    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
97357      goto trigger_cleanup;
97358    }
97359    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
97360      goto trigger_cleanup;
97361    }
97362  }
97363#endif
97364
97365  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
97366  ** cannot appear on views.  So we might as well translate every
97367  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
97368  ** elsewhere.
97369  */
97370  if (tr_tm == TK_INSTEAD){
97371    tr_tm = TK_BEFORE;
97372  }
97373
97374  /* Build the Trigger object */
97375  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
97376  if( pTrigger==0 ) goto trigger_cleanup;
97377  pTrigger->zName = zName;
97378  zName = 0;
97379  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
97380  pTrigger->pSchema = db->aDb[iDb].pSchema;
97381  pTrigger->pTabSchema = pTab->pSchema;
97382  pTrigger->op = (u8)op;
97383  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
97384  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
97385  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
97386  assert( pParse->pNewTrigger==0 );
97387  pParse->pNewTrigger = pTrigger;
97388
97389trigger_cleanup:
97390  sqlite3DbFree(db, zName);
97391  sqlite3SrcListDelete(db, pTableName);
97392  sqlite3IdListDelete(db, pColumns);
97393  sqlite3ExprDelete(db, pWhen);
97394  if( !pParse->pNewTrigger ){
97395    sqlite3DeleteTrigger(db, pTrigger);
97396  }else{
97397    assert( pParse->pNewTrigger==pTrigger );
97398  }
97399}
97400
97401/*
97402** This routine is called after all of the trigger actions have been parsed
97403** in order to complete the process of building the trigger.
97404*/
97405SQLITE_PRIVATE void sqlite3FinishTrigger(
97406  Parse *pParse,          /* Parser context */
97407  TriggerStep *pStepList, /* The triggered program */
97408  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
97409){
97410  Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
97411  char *zName;                            /* Name of trigger */
97412  sqlite3 *db = pParse->db;               /* The database */
97413  DbFixer sFix;                           /* Fixer object */
97414  int iDb;                                /* Database containing the trigger */
97415  Token nameToken;                        /* Trigger name for error reporting */
97416
97417  pParse->pNewTrigger = 0;
97418  if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
97419  zName = pTrig->zName;
97420  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
97421  pTrig->step_list = pStepList;
97422  while( pStepList ){
97423    pStepList->pTrig = pTrig;
97424    pStepList = pStepList->pNext;
97425  }
97426  nameToken.z = pTrig->zName;
97427  nameToken.n = sqlite3Strlen30(nameToken.z);
97428  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken)
97429          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
97430    goto triggerfinish_cleanup;
97431  }
97432
97433  /* if we are not initializing,
97434  ** build the sqlite_master entry
97435  */
97436  if( !db->init.busy ){
97437    Vdbe *v;
97438    char *z;
97439
97440    /* Make an entry in the sqlite_master table */
97441    v = sqlite3GetVdbe(pParse);
97442    if( v==0 ) goto triggerfinish_cleanup;
97443    sqlite3BeginWriteOperation(pParse, 0, iDb);
97444    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
97445    sqlite3NestedParse(pParse,
97446       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
97447       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
97448       pTrig->table, z);
97449    sqlite3DbFree(db, z);
97450    sqlite3ChangeCookie(pParse, iDb);
97451    sqlite3VdbeAddParseSchemaOp(v, iDb,
97452        sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
97453  }
97454
97455  if( db->init.busy ){
97456    Trigger *pLink = pTrig;
97457    Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
97458    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97459    pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
97460    if( pTrig ){
97461      db->mallocFailed = 1;
97462    }else if( pLink->pSchema==pLink->pTabSchema ){
97463      Table *pTab;
97464      int n = sqlite3Strlen30(pLink->table);
97465      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
97466      assert( pTab!=0 );
97467      pLink->pNext = pTab->pTrigger;
97468      pTab->pTrigger = pLink;
97469    }
97470  }
97471
97472triggerfinish_cleanup:
97473  sqlite3DeleteTrigger(db, pTrig);
97474  assert( !pParse->pNewTrigger );
97475  sqlite3DeleteTriggerStep(db, pStepList);
97476}
97477
97478/*
97479** Turn a SELECT statement (that the pSelect parameter points to) into
97480** a trigger step.  Return a pointer to a TriggerStep structure.
97481**
97482** The parser calls this routine when it finds a SELECT statement in
97483** body of a TRIGGER.
97484*/
97485SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
97486  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
97487  if( pTriggerStep==0 ) {
97488    sqlite3SelectDelete(db, pSelect);
97489    return 0;
97490  }
97491  pTriggerStep->op = TK_SELECT;
97492  pTriggerStep->pSelect = pSelect;
97493  pTriggerStep->orconf = OE_Default;
97494  return pTriggerStep;
97495}
97496
97497/*
97498** Allocate space to hold a new trigger step.  The allocated space
97499** holds both the TriggerStep object and the TriggerStep.target.z string.
97500**
97501** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
97502*/
97503static TriggerStep *triggerStepAllocate(
97504  sqlite3 *db,                /* Database connection */
97505  u8 op,                      /* Trigger opcode */
97506  Token *pName                /* The target name */
97507){
97508  TriggerStep *pTriggerStep;
97509
97510  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
97511  if( pTriggerStep ){
97512    char *z = (char*)&pTriggerStep[1];
97513    memcpy(z, pName->z, pName->n);
97514    pTriggerStep->target.z = z;
97515    pTriggerStep->target.n = pName->n;
97516    pTriggerStep->op = op;
97517  }
97518  return pTriggerStep;
97519}
97520
97521/*
97522** Build a trigger step out of an INSERT statement.  Return a pointer
97523** to the new trigger step.
97524**
97525** The parser calls this routine when it sees an INSERT inside the
97526** body of a trigger.
97527*/
97528SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
97529  sqlite3 *db,        /* The database connection */
97530  Token *pTableName,  /* Name of the table into which we insert */
97531  IdList *pColumn,    /* List of columns in pTableName to insert into */
97532  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
97533  Select *pSelect,    /* A SELECT statement that supplies values */
97534  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
97535){
97536  TriggerStep *pTriggerStep;
97537
97538  assert(pEList == 0 || pSelect == 0);
97539  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
97540
97541  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
97542  if( pTriggerStep ){
97543    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
97544    pTriggerStep->pIdList = pColumn;
97545    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
97546    pTriggerStep->orconf = orconf;
97547  }else{
97548    sqlite3IdListDelete(db, pColumn);
97549  }
97550  sqlite3ExprListDelete(db, pEList);
97551  sqlite3SelectDelete(db, pSelect);
97552
97553  return pTriggerStep;
97554}
97555
97556/*
97557** Construct a trigger step that implements an UPDATE statement and return
97558** a pointer to that trigger step.  The parser calls this routine when it
97559** sees an UPDATE statement inside the body of a CREATE TRIGGER.
97560*/
97561SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
97562  sqlite3 *db,         /* The database connection */
97563  Token *pTableName,   /* Name of the table to be updated */
97564  ExprList *pEList,    /* The SET clause: list of column and new values */
97565  Expr *pWhere,        /* The WHERE clause */
97566  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
97567){
97568  TriggerStep *pTriggerStep;
97569
97570  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
97571  if( pTriggerStep ){
97572    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
97573    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
97574    pTriggerStep->orconf = orconf;
97575  }
97576  sqlite3ExprListDelete(db, pEList);
97577  sqlite3ExprDelete(db, pWhere);
97578  return pTriggerStep;
97579}
97580
97581/*
97582** Construct a trigger step that implements a DELETE statement and return
97583** a pointer to that trigger step.  The parser calls this routine when it
97584** sees a DELETE statement inside the body of a CREATE TRIGGER.
97585*/
97586SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
97587  sqlite3 *db,            /* Database connection */
97588  Token *pTableName,      /* The table from which rows are deleted */
97589  Expr *pWhere            /* The WHERE clause */
97590){
97591  TriggerStep *pTriggerStep;
97592
97593  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
97594  if( pTriggerStep ){
97595    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
97596    pTriggerStep->orconf = OE_Default;
97597  }
97598  sqlite3ExprDelete(db, pWhere);
97599  return pTriggerStep;
97600}
97601
97602/*
97603** Recursively delete a Trigger structure
97604*/
97605SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
97606  if( pTrigger==0 ) return;
97607  sqlite3DeleteTriggerStep(db, pTrigger->step_list);
97608  sqlite3DbFree(db, pTrigger->zName);
97609  sqlite3DbFree(db, pTrigger->table);
97610  sqlite3ExprDelete(db, pTrigger->pWhen);
97611  sqlite3IdListDelete(db, pTrigger->pColumns);
97612  sqlite3DbFree(db, pTrigger);
97613}
97614
97615/*
97616** This function is called to drop a trigger from the database schema.
97617**
97618** This may be called directly from the parser and therefore identifies
97619** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
97620** same job as this routine except it takes a pointer to the trigger
97621** instead of the trigger name.
97622**/
97623SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
97624  Trigger *pTrigger = 0;
97625  int i;
97626  const char *zDb;
97627  const char *zName;
97628  int nName;
97629  sqlite3 *db = pParse->db;
97630
97631  if( db->mallocFailed ) goto drop_trigger_cleanup;
97632  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
97633    goto drop_trigger_cleanup;
97634  }
97635
97636  assert( pName->nSrc==1 );
97637  zDb = pName->a[0].zDatabase;
97638  zName = pName->a[0].zName;
97639  nName = sqlite3Strlen30(zName);
97640  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
97641  for(i=OMIT_TEMPDB; i<db->nDb; i++){
97642    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
97643    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
97644    assert( sqlite3SchemaMutexHeld(db, j, 0) );
97645    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
97646    if( pTrigger ) break;
97647  }
97648  if( !pTrigger ){
97649    if( !noErr ){
97650      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
97651    }else{
97652      sqlite3CodeVerifyNamedSchema(pParse, zDb);
97653    }
97654    pParse->checkSchema = 1;
97655    goto drop_trigger_cleanup;
97656  }
97657  sqlite3DropTriggerPtr(pParse, pTrigger);
97658
97659drop_trigger_cleanup:
97660  sqlite3SrcListDelete(db, pName);
97661}
97662
97663/*
97664** Return a pointer to the Table structure for the table that a trigger
97665** is set on.
97666*/
97667static Table *tableOfTrigger(Trigger *pTrigger){
97668  int n = sqlite3Strlen30(pTrigger->table);
97669  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
97670}
97671
97672
97673/*
97674** Drop a trigger given a pointer to that trigger.
97675*/
97676SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
97677  Table   *pTable;
97678  Vdbe *v;
97679  sqlite3 *db = pParse->db;
97680  int iDb;
97681
97682  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
97683  assert( iDb>=0 && iDb<db->nDb );
97684  pTable = tableOfTrigger(pTrigger);
97685  assert( pTable );
97686  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
97687#ifndef SQLITE_OMIT_AUTHORIZATION
97688  {
97689    int code = SQLITE_DROP_TRIGGER;
97690    const char *zDb = db->aDb[iDb].zName;
97691    const char *zTab = SCHEMA_TABLE(iDb);
97692    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
97693    if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
97694      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
97695      return;
97696    }
97697  }
97698#endif
97699
97700  /* Generate code to destroy the database record of the trigger.
97701  */
97702  assert( pTable!=0 );
97703  if( (v = sqlite3GetVdbe(pParse))!=0 ){
97704    int base;
97705    static const VdbeOpList dropTrigger[] = {
97706      { OP_Rewind,     0, ADDR(9),  0},
97707      { OP_String8,    0, 1,        0}, /* 1 */
97708      { OP_Column,     0, 1,        2},
97709      { OP_Ne,         2, ADDR(8),  1},
97710      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
97711      { OP_Column,     0, 0,        2},
97712      { OP_Ne,         2, ADDR(8),  1},
97713      { OP_Delete,     0, 0,        0},
97714      { OP_Next,       0, ADDR(1),  0}, /* 8 */
97715    };
97716
97717    sqlite3BeginWriteOperation(pParse, 0, iDb);
97718    sqlite3OpenMasterTable(pParse, iDb);
97719    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
97720    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
97721    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
97722    sqlite3ChangeCookie(pParse, iDb);
97723    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
97724    sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
97725    if( pParse->nMem<3 ){
97726      pParse->nMem = 3;
97727    }
97728  }
97729}
97730
97731/*
97732** Remove a trigger from the hash tables of the sqlite* pointer.
97733*/
97734SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
97735  Trigger *pTrigger;
97736  Hash *pHash;
97737
97738  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97739  pHash = &(db->aDb[iDb].pSchema->trigHash);
97740  pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
97741  if( ALWAYS(pTrigger) ){
97742    if( pTrigger->pSchema==pTrigger->pTabSchema ){
97743      Table *pTab = tableOfTrigger(pTrigger);
97744      Trigger **pp;
97745      for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
97746      *pp = (*pp)->pNext;
97747    }
97748    sqlite3DeleteTrigger(db, pTrigger);
97749    db->flags |= SQLITE_InternChanges;
97750  }
97751}
97752
97753/*
97754** pEList is the SET clause of an UPDATE statement.  Each entry
97755** in pEList is of the format <id>=<expr>.  If any of the entries
97756** in pEList have an <id> which matches an identifier in pIdList,
97757** then return TRUE.  If pIdList==NULL, then it is considered a
97758** wildcard that matches anything.  Likewise if pEList==NULL then
97759** it matches anything so always return true.  Return false only
97760** if there is no match.
97761*/
97762static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
97763  int e;
97764  if( pIdList==0 || NEVER(pEList==0) ) return 1;
97765  for(e=0; e<pEList->nExpr; e++){
97766    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
97767  }
97768  return 0;
97769}
97770
97771/*
97772** Return a list of all triggers on table pTab if there exists at least
97773** one trigger that must be fired when an operation of type 'op' is
97774** performed on the table, and, if that operation is an UPDATE, if at
97775** least one of the columns in pChanges is being modified.
97776*/
97777SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
97778  Parse *pParse,          /* Parse context */
97779  Table *pTab,            /* The table the contains the triggers */
97780  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
97781  ExprList *pChanges,     /* Columns that change in an UPDATE statement */
97782  int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
97783){
97784  int mask = 0;
97785  Trigger *pList = 0;
97786  Trigger *p;
97787
97788  if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
97789    pList = sqlite3TriggerList(pParse, pTab);
97790  }
97791  assert( pList==0 || IsVirtual(pTab)==0 );
97792  for(p=pList; p; p=p->pNext){
97793    if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
97794      mask |= p->tr_tm;
97795    }
97796  }
97797  if( pMask ){
97798    *pMask = mask;
97799  }
97800  return (mask ? pList : 0);
97801}
97802
97803/*
97804** Convert the pStep->target token into a SrcList and return a pointer
97805** to that SrcList.
97806**
97807** This routine adds a specific database name, if needed, to the target when
97808** forming the SrcList.  This prevents a trigger in one database from
97809** referring to a target in another database.  An exception is when the
97810** trigger is in TEMP in which case it can refer to any other database it
97811** wants.
97812*/
97813static SrcList *targetSrcList(
97814  Parse *pParse,       /* The parsing context */
97815  TriggerStep *pStep   /* The trigger containing the target token */
97816){
97817  int iDb;             /* Index of the database to use */
97818  SrcList *pSrc;       /* SrcList to be returned */
97819
97820  pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
97821  if( pSrc ){
97822    assert( pSrc->nSrc>0 );
97823    assert( pSrc->a!=0 );
97824    iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
97825    if( iDb==0 || iDb>=2 ){
97826      sqlite3 *db = pParse->db;
97827      assert( iDb<pParse->db->nDb );
97828      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
97829    }
97830  }
97831  return pSrc;
97832}
97833
97834/*
97835** Generate VDBE code for the statements inside the body of a single
97836** trigger.
97837*/
97838static int codeTriggerProgram(
97839  Parse *pParse,            /* The parser context */
97840  TriggerStep *pStepList,   /* List of statements inside the trigger body */
97841  int orconf                /* Conflict algorithm. (OE_Abort, etc) */
97842){
97843  TriggerStep *pStep;
97844  Vdbe *v = pParse->pVdbe;
97845  sqlite3 *db = pParse->db;
97846
97847  assert( pParse->pTriggerTab && pParse->pToplevel );
97848  assert( pStepList );
97849  assert( v!=0 );
97850  for(pStep=pStepList; pStep; pStep=pStep->pNext){
97851    /* Figure out the ON CONFLICT policy that will be used for this step
97852    ** of the trigger program. If the statement that caused this trigger
97853    ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
97854    ** the ON CONFLICT policy that was specified as part of the trigger
97855    ** step statement. Example:
97856    **
97857    **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
97858    **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
97859    **   END;
97860    **
97861    **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
97862    **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
97863    */
97864    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
97865
97866    switch( pStep->op ){
97867      case TK_UPDATE: {
97868        sqlite3Update(pParse,
97869          targetSrcList(pParse, pStep),
97870          sqlite3ExprListDup(db, pStep->pExprList, 0),
97871          sqlite3ExprDup(db, pStep->pWhere, 0),
97872          pParse->eOrconf
97873        );
97874        break;
97875      }
97876      case TK_INSERT: {
97877        sqlite3Insert(pParse,
97878          targetSrcList(pParse, pStep),
97879          sqlite3ExprListDup(db, pStep->pExprList, 0),
97880          sqlite3SelectDup(db, pStep->pSelect, 0),
97881          sqlite3IdListDup(db, pStep->pIdList),
97882          pParse->eOrconf
97883        );
97884        break;
97885      }
97886      case TK_DELETE: {
97887        sqlite3DeleteFrom(pParse,
97888          targetSrcList(pParse, pStep),
97889          sqlite3ExprDup(db, pStep->pWhere, 0)
97890        );
97891        break;
97892      }
97893      default: assert( pStep->op==TK_SELECT ); {
97894        SelectDest sDest;
97895        Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
97896        sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
97897        sqlite3Select(pParse, pSelect, &sDest);
97898        sqlite3SelectDelete(db, pSelect);
97899        break;
97900      }
97901    }
97902    if( pStep->op!=TK_SELECT ){
97903      sqlite3VdbeAddOp0(v, OP_ResetCount);
97904    }
97905  }
97906
97907  return 0;
97908}
97909
97910#ifdef SQLITE_DEBUG
97911/*
97912** This function is used to add VdbeComment() annotations to a VDBE
97913** program. It is not used in production code, only for debugging.
97914*/
97915static const char *onErrorText(int onError){
97916  switch( onError ){
97917    case OE_Abort:    return "abort";
97918    case OE_Rollback: return "rollback";
97919    case OE_Fail:     return "fail";
97920    case OE_Replace:  return "replace";
97921    case OE_Ignore:   return "ignore";
97922    case OE_Default:  return "default";
97923  }
97924  return "n/a";
97925}
97926#endif
97927
97928/*
97929** Parse context structure pFrom has just been used to create a sub-vdbe
97930** (trigger program). If an error has occurred, transfer error information
97931** from pFrom to pTo.
97932*/
97933static void transferParseError(Parse *pTo, Parse *pFrom){
97934  assert( pFrom->zErrMsg==0 || pFrom->nErr );
97935  assert( pTo->zErrMsg==0 || pTo->nErr );
97936  if( pTo->nErr==0 ){
97937    pTo->zErrMsg = pFrom->zErrMsg;
97938    pTo->nErr = pFrom->nErr;
97939  }else{
97940    sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
97941  }
97942}
97943
97944/*
97945** Create and populate a new TriggerPrg object with a sub-program
97946** implementing trigger pTrigger with ON CONFLICT policy orconf.
97947*/
97948static TriggerPrg *codeRowTrigger(
97949  Parse *pParse,       /* Current parse context */
97950  Trigger *pTrigger,   /* Trigger to code */
97951  Table *pTab,         /* The table pTrigger is attached to */
97952  int orconf           /* ON CONFLICT policy to code trigger program with */
97953){
97954  Parse *pTop = sqlite3ParseToplevel(pParse);
97955  sqlite3 *db = pParse->db;   /* Database handle */
97956  TriggerPrg *pPrg;           /* Value to return */
97957  Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
97958  Vdbe *v;                    /* Temporary VM */
97959  NameContext sNC;            /* Name context for sub-vdbe */
97960  SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
97961  Parse *pSubParse;           /* Parse context for sub-vdbe */
97962  int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
97963
97964  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
97965  assert( pTop->pVdbe );
97966
97967  /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
97968  ** are freed if an error occurs, link them into the Parse.pTriggerPrg
97969  ** list of the top-level Parse object sooner rather than later.  */
97970  pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
97971  if( !pPrg ) return 0;
97972  pPrg->pNext = pTop->pTriggerPrg;
97973  pTop->pTriggerPrg = pPrg;
97974  pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
97975  if( !pProgram ) return 0;
97976  sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
97977  pPrg->pTrigger = pTrigger;
97978  pPrg->orconf = orconf;
97979  pPrg->aColmask[0] = 0xffffffff;
97980  pPrg->aColmask[1] = 0xffffffff;
97981
97982  /* Allocate and populate a new Parse context to use for coding the
97983  ** trigger sub-program.  */
97984  pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
97985  if( !pSubParse ) return 0;
97986  memset(&sNC, 0, sizeof(sNC));
97987  sNC.pParse = pSubParse;
97988  pSubParse->db = db;
97989  pSubParse->pTriggerTab = pTab;
97990  pSubParse->pToplevel = pTop;
97991  pSubParse->zAuthContext = pTrigger->zName;
97992  pSubParse->eTriggerOp = pTrigger->op;
97993  pSubParse->nQueryLoop = pParse->nQueryLoop;
97994
97995  v = sqlite3GetVdbe(pSubParse);
97996  if( v ){
97997    VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
97998      pTrigger->zName, onErrorText(orconf),
97999      (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
98000        (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
98001        (pTrigger->op==TK_INSERT ? "INSERT" : ""),
98002        (pTrigger->op==TK_DELETE ? "DELETE" : ""),
98003      pTab->zName
98004    ));
98005#ifndef SQLITE_OMIT_TRACE
98006    sqlite3VdbeChangeP4(v, -1,
98007      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
98008    );
98009#endif
98010
98011    /* If one was specified, code the WHEN clause. If it evaluates to false
98012    ** (or NULL) the sub-vdbe is immediately halted by jumping to the
98013    ** OP_Halt inserted at the end of the program.  */
98014    if( pTrigger->pWhen ){
98015      pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
98016      if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
98017       && db->mallocFailed==0
98018      ){
98019        iEndTrigger = sqlite3VdbeMakeLabel(v);
98020        sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
98021      }
98022      sqlite3ExprDelete(db, pWhen);
98023    }
98024
98025    /* Code the trigger program into the sub-vdbe. */
98026    codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
98027
98028    /* Insert an OP_Halt at the end of the sub-program. */
98029    if( iEndTrigger ){
98030      sqlite3VdbeResolveLabel(v, iEndTrigger);
98031    }
98032    sqlite3VdbeAddOp0(v, OP_Halt);
98033    VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
98034
98035    transferParseError(pParse, pSubParse);
98036    if( db->mallocFailed==0 ){
98037      pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
98038    }
98039    pProgram->nMem = pSubParse->nMem;
98040    pProgram->nCsr = pSubParse->nTab;
98041    pProgram->token = (void *)pTrigger;
98042    pPrg->aColmask[0] = pSubParse->oldmask;
98043    pPrg->aColmask[1] = pSubParse->newmask;
98044    sqlite3VdbeDelete(v);
98045  }
98046
98047  assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
98048  assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
98049  sqlite3StackFree(db, pSubParse);
98050
98051  return pPrg;
98052}
98053
98054/*
98055** Return a pointer to a TriggerPrg object containing the sub-program for
98056** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
98057** TriggerPrg object exists, a new object is allocated and populated before
98058** being returned.
98059*/
98060static TriggerPrg *getRowTrigger(
98061  Parse *pParse,       /* Current parse context */
98062  Trigger *pTrigger,   /* Trigger to code */
98063  Table *pTab,         /* The table trigger pTrigger is attached to */
98064  int orconf           /* ON CONFLICT algorithm. */
98065){
98066  Parse *pRoot = sqlite3ParseToplevel(pParse);
98067  TriggerPrg *pPrg;
98068
98069  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
98070
98071  /* It may be that this trigger has already been coded (or is in the
98072  ** process of being coded). If this is the case, then an entry with
98073  ** a matching TriggerPrg.pTrigger field will be present somewhere
98074  ** in the Parse.pTriggerPrg list. Search for such an entry.  */
98075  for(pPrg=pRoot->pTriggerPrg;
98076      pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
98077      pPrg=pPrg->pNext
98078  );
98079
98080  /* If an existing TriggerPrg could not be located, create a new one. */
98081  if( !pPrg ){
98082    pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
98083  }
98084
98085  return pPrg;
98086}
98087
98088/*
98089** Generate code for the trigger program associated with trigger p on
98090** table pTab. The reg, orconf and ignoreJump parameters passed to this
98091** function are the same as those described in the header function for
98092** sqlite3CodeRowTrigger()
98093*/
98094SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
98095  Parse *pParse,       /* Parse context */
98096  Trigger *p,          /* Trigger to code */
98097  Table *pTab,         /* The table to code triggers from */
98098  int reg,             /* Reg array containing OLD.* and NEW.* values */
98099  int orconf,          /* ON CONFLICT policy */
98100  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
98101){
98102  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
98103  TriggerPrg *pPrg;
98104  pPrg = getRowTrigger(pParse, p, pTab, orconf);
98105  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
98106
98107  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
98108  ** is a pointer to the sub-vdbe containing the trigger program.  */
98109  if( pPrg ){
98110    int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
98111
98112    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
98113    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
98114    VdbeComment(
98115        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
98116
98117    /* Set the P5 operand of the OP_Program instruction to non-zero if
98118    ** recursive invocation of this trigger program is disallowed. Recursive
98119    ** invocation is disallowed if (a) the sub-program is really a trigger,
98120    ** not a foreign key action, and (b) the flag to enable recursive triggers
98121    ** is clear.  */
98122    sqlite3VdbeChangeP5(v, (u8)bRecursive);
98123  }
98124}
98125
98126/*
98127** This is called to code the required FOR EACH ROW triggers for an operation
98128** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
98129** is given by the op paramater. The tr_tm parameter determines whether the
98130** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
98131** parameter pChanges is passed the list of columns being modified.
98132**
98133** If there are no triggers that fire at the specified time for the specified
98134** operation on pTab, this function is a no-op.
98135**
98136** The reg argument is the address of the first in an array of registers
98137** that contain the values substituted for the new.* and old.* references
98138** in the trigger program. If N is the number of columns in table pTab
98139** (a copy of pTab->nCol), then registers are populated as follows:
98140**
98141**   Register       Contains
98142**   ------------------------------------------------------
98143**   reg+0          OLD.rowid
98144**   reg+1          OLD.* value of left-most column of pTab
98145**   ...            ...
98146**   reg+N          OLD.* value of right-most column of pTab
98147**   reg+N+1        NEW.rowid
98148**   reg+N+2        OLD.* value of left-most column of pTab
98149**   ...            ...
98150**   reg+N+N+1      NEW.* value of right-most column of pTab
98151**
98152** For ON DELETE triggers, the registers containing the NEW.* values will
98153** never be accessed by the trigger program, so they are not allocated or
98154** populated by the caller (there is no data to populate them with anyway).
98155** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
98156** are never accessed, and so are not allocated by the caller. So, for an
98157** ON INSERT trigger, the value passed to this function as parameter reg
98158** is not a readable register, although registers (reg+N) through
98159** (reg+N+N+1) are.
98160**
98161** Parameter orconf is the default conflict resolution algorithm for the
98162** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
98163** is the instruction that control should jump to if a trigger program
98164** raises an IGNORE exception.
98165*/
98166SQLITE_PRIVATE void sqlite3CodeRowTrigger(
98167  Parse *pParse,       /* Parse context */
98168  Trigger *pTrigger,   /* List of triggers on table pTab */
98169  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
98170  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
98171  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
98172  Table *pTab,         /* The table to code triggers from */
98173  int reg,             /* The first in an array of registers (see above) */
98174  int orconf,          /* ON CONFLICT policy */
98175  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
98176){
98177  Trigger *p;          /* Used to iterate through pTrigger list */
98178
98179  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
98180  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
98181  assert( (op==TK_UPDATE)==(pChanges!=0) );
98182
98183  for(p=pTrigger; p; p=p->pNext){
98184
98185    /* Sanity checking:  The schema for the trigger and for the table are
98186    ** always defined.  The trigger must be in the same schema as the table
98187    ** or else it must be a TEMP trigger. */
98188    assert( p->pSchema!=0 );
98189    assert( p->pTabSchema!=0 );
98190    assert( p->pSchema==p->pTabSchema
98191         || p->pSchema==pParse->db->aDb[1].pSchema );
98192
98193    /* Determine whether we should code this trigger */
98194    if( p->op==op
98195     && p->tr_tm==tr_tm
98196     && checkColumnOverlap(p->pColumns, pChanges)
98197    ){
98198      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
98199    }
98200  }
98201}
98202
98203/*
98204** Triggers may access values stored in the old.* or new.* pseudo-table.
98205** This function returns a 32-bit bitmask indicating which columns of the
98206** old.* or new.* tables actually are used by triggers. This information
98207** may be used by the caller, for example, to avoid having to load the entire
98208** old.* record into memory when executing an UPDATE or DELETE command.
98209**
98210** Bit 0 of the returned mask is set if the left-most column of the
98211** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
98212** the second leftmost column value is required, and so on. If there
98213** are more than 32 columns in the table, and at least one of the columns
98214** with an index greater than 32 may be accessed, 0xffffffff is returned.
98215**
98216** It is not possible to determine if the old.rowid or new.rowid column is
98217** accessed by triggers. The caller must always assume that it is.
98218**
98219** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
98220** applies to the old.* table. If 1, the new.* table.
98221**
98222** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
98223** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
98224** included in the returned mask if the TRIGGER_BEFORE bit is set in the
98225** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
98226** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
98227*/
98228SQLITE_PRIVATE u32 sqlite3TriggerColmask(
98229  Parse *pParse,       /* Parse context */
98230  Trigger *pTrigger,   /* List of triggers on table pTab */
98231  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
98232  int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
98233  int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
98234  Table *pTab,         /* The table to code triggers from */
98235  int orconf           /* Default ON CONFLICT policy for trigger steps */
98236){
98237  const int op = pChanges ? TK_UPDATE : TK_DELETE;
98238  u32 mask = 0;
98239  Trigger *p;
98240
98241  assert( isNew==1 || isNew==0 );
98242  for(p=pTrigger; p; p=p->pNext){
98243    if( p->op==op && (tr_tm&p->tr_tm)
98244     && checkColumnOverlap(p->pColumns,pChanges)
98245    ){
98246      TriggerPrg *pPrg;
98247      pPrg = getRowTrigger(pParse, p, pTab, orconf);
98248      if( pPrg ){
98249        mask |= pPrg->aColmask[isNew];
98250      }
98251    }
98252  }
98253
98254  return mask;
98255}
98256
98257#endif /* !defined(SQLITE_OMIT_TRIGGER) */
98258
98259/************** End of trigger.c *********************************************/
98260/************** Begin file update.c ******************************************/
98261/*
98262** 2001 September 15
98263**
98264** The author disclaims copyright to this source code.  In place of
98265** a legal notice, here is a blessing:
98266**
98267**    May you do good and not evil.
98268**    May you find forgiveness for yourself and forgive others.
98269**    May you share freely, never taking more than you give.
98270**
98271*************************************************************************
98272** This file contains C code routines that are called by the parser
98273** to handle UPDATE statements.
98274*/
98275
98276#ifndef SQLITE_OMIT_VIRTUALTABLE
98277/* Forward declaration */
98278static void updateVirtualTable(
98279  Parse *pParse,       /* The parsing context */
98280  SrcList *pSrc,       /* The virtual table to be modified */
98281  Table *pTab,         /* The virtual table */
98282  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
98283  Expr *pRowidExpr,    /* Expression used to recompute the rowid */
98284  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
98285  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
98286  int onError          /* ON CONFLICT strategy */
98287);
98288#endif /* SQLITE_OMIT_VIRTUALTABLE */
98289
98290/*
98291** The most recently coded instruction was an OP_Column to retrieve the
98292** i-th column of table pTab. This routine sets the P4 parameter of the
98293** OP_Column to the default value, if any.
98294**
98295** The default value of a column is specified by a DEFAULT clause in the
98296** column definition. This was either supplied by the user when the table
98297** was created, or added later to the table definition by an ALTER TABLE
98298** command. If the latter, then the row-records in the table btree on disk
98299** may not contain a value for the column and the default value, taken
98300** from the P4 parameter of the OP_Column instruction, is returned instead.
98301** If the former, then all row-records are guaranteed to include a value
98302** for the column and the P4 value is not required.
98303**
98304** Column definitions created by an ALTER TABLE command may only have
98305** literal default values specified: a number, null or a string. (If a more
98306** complicated default expression value was provided, it is evaluated
98307** when the ALTER TABLE is executed and one of the literal values written
98308** into the sqlite_master table.)
98309**
98310** Therefore, the P4 parameter is only required if the default value for
98311** the column is a literal number, string or null. The sqlite3ValueFromExpr()
98312** function is capable of transforming these types of expressions into
98313** sqlite3_value objects.
98314**
98315** If parameter iReg is not negative, code an OP_RealAffinity instruction
98316** on register iReg. This is used when an equivalent integer value is
98317** stored in place of an 8-byte floating point value in order to save
98318** space.
98319*/
98320SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
98321  assert( pTab!=0 );
98322  if( !pTab->pSelect ){
98323    sqlite3_value *pValue;
98324    u8 enc = ENC(sqlite3VdbeDb(v));
98325    Column *pCol = &pTab->aCol[i];
98326    VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
98327    assert( i<pTab->nCol );
98328    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
98329                         pCol->affinity, &pValue);
98330    if( pValue ){
98331      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
98332    }
98333#ifndef SQLITE_OMIT_FLOATING_POINT
98334    if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
98335      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
98336    }
98337#endif
98338  }
98339}
98340
98341/*
98342** Process an UPDATE statement.
98343**
98344**   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
98345**          \_______/ \________/     \______/       \________________/
98346*            onError   pTabList      pChanges             pWhere
98347*/
98348SQLITE_PRIVATE void sqlite3Update(
98349  Parse *pParse,         /* The parser context */
98350  SrcList *pTabList,     /* The table in which we should change things */
98351  ExprList *pChanges,    /* Things to be changed */
98352  Expr *pWhere,          /* The WHERE clause.  May be null */
98353  int onError            /* How to handle constraint errors */
98354){
98355  int i, j;              /* Loop counters */
98356  Table *pTab;           /* The table to be updated */
98357  int addr = 0;          /* VDBE instruction address of the start of the loop */
98358  WhereInfo *pWInfo;     /* Information about the WHERE clause */
98359  Vdbe *v;               /* The virtual database engine */
98360  Index *pIdx;           /* For looping over indices */
98361  int nIdx;              /* Number of indices that need updating */
98362  int iCur;              /* VDBE Cursor number of pTab */
98363  sqlite3 *db;           /* The database structure */
98364  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
98365  int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
98366                         ** an expression for the i-th column of the table.
98367                         ** aXRef[i]==-1 if the i-th column is not changed. */
98368  int chngRowid;         /* True if the record number is being changed */
98369  Expr *pRowidExpr = 0;  /* Expression defining the new record number */
98370  int openAll = 0;       /* True if all indices need to be opened */
98371  AuthContext sContext;  /* The authorization context */
98372  NameContext sNC;       /* The name-context to resolve expressions in */
98373  int iDb;               /* Database containing the table being updated */
98374  int okOnePass;         /* True for one-pass algorithm without the FIFO */
98375  int hasFK;             /* True if foreign key processing is required */
98376
98377#ifndef SQLITE_OMIT_TRIGGER
98378  int isView;            /* True when updating a view (INSTEAD OF trigger) */
98379  Trigger *pTrigger;     /* List of triggers on pTab, if required */
98380  int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
98381#endif
98382  int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
98383
98384  /* Register Allocations */
98385  int regRowCount = 0;   /* A count of rows changed */
98386  int regOldRowid;       /* The old rowid */
98387  int regNewRowid;       /* The new rowid */
98388  int regNew;
98389  int regOld = 0;
98390  int regRowSet = 0;     /* Rowset of rows to be updated */
98391
98392  memset(&sContext, 0, sizeof(sContext));
98393  db = pParse->db;
98394  if( pParse->nErr || db->mallocFailed ){
98395    goto update_cleanup;
98396  }
98397  assert( pTabList->nSrc==1 );
98398
98399  /* Locate the table which we want to update.
98400  */
98401  pTab = sqlite3SrcListLookup(pParse, pTabList);
98402  if( pTab==0 ) goto update_cleanup;
98403  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
98404
98405  /* Figure out if we have any triggers and if the table being
98406  ** updated is a view.
98407  */
98408#ifndef SQLITE_OMIT_TRIGGER
98409  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
98410  isView = pTab->pSelect!=0;
98411  assert( pTrigger || tmask==0 );
98412#else
98413# define pTrigger 0
98414# define isView 0
98415# define tmask 0
98416#endif
98417#ifdef SQLITE_OMIT_VIEW
98418# undef isView
98419# define isView 0
98420#endif
98421
98422  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
98423    goto update_cleanup;
98424  }
98425  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
98426    goto update_cleanup;
98427  }
98428  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
98429  if( aXRef==0 ) goto update_cleanup;
98430  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
98431
98432  /* Allocate a cursors for the main database table and for all indices.
98433  ** The index cursors might not be used, but if they are used they
98434  ** need to occur right after the database cursor.  So go ahead and
98435  ** allocate enough space, just in case.
98436  */
98437  pTabList->a[0].iCursor = iCur = pParse->nTab++;
98438  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98439    pParse->nTab++;
98440  }
98441
98442  /* Initialize the name-context */
98443  memset(&sNC, 0, sizeof(sNC));
98444  sNC.pParse = pParse;
98445  sNC.pSrcList = pTabList;
98446
98447  /* Resolve the column names in all the expressions of the
98448  ** of the UPDATE statement.  Also find the column index
98449  ** for each column to be updated in the pChanges array.  For each
98450  ** column to be updated, make sure we have authorization to change
98451  ** that column.
98452  */
98453  chngRowid = 0;
98454  for(i=0; i<pChanges->nExpr; i++){
98455    if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
98456      goto update_cleanup;
98457    }
98458    for(j=0; j<pTab->nCol; j++){
98459      if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
98460        if( j==pTab->iPKey ){
98461          chngRowid = 1;
98462          pRowidExpr = pChanges->a[i].pExpr;
98463        }
98464        aXRef[j] = i;
98465        break;
98466      }
98467    }
98468    if( j>=pTab->nCol ){
98469      if( sqlite3IsRowid(pChanges->a[i].zName) ){
98470        chngRowid = 1;
98471        pRowidExpr = pChanges->a[i].pExpr;
98472      }else{
98473        sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
98474        pParse->checkSchema = 1;
98475        goto update_cleanup;
98476      }
98477    }
98478#ifndef SQLITE_OMIT_AUTHORIZATION
98479    {
98480      int rc;
98481      rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
98482                           pTab->aCol[j].zName, db->aDb[iDb].zName);
98483      if( rc==SQLITE_DENY ){
98484        goto update_cleanup;
98485      }else if( rc==SQLITE_IGNORE ){
98486        aXRef[j] = -1;
98487      }
98488    }
98489#endif
98490  }
98491
98492  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
98493
98494  /* Allocate memory for the array aRegIdx[].  There is one entry in the
98495  ** array for each index associated with table being updated.  Fill in
98496  ** the value with a register number for indices that are to be used
98497  ** and with zero for unused indices.
98498  */
98499  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
98500  if( nIdx>0 ){
98501    aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
98502    if( aRegIdx==0 ) goto update_cleanup;
98503  }
98504  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98505    int reg;
98506    if( hasFK || chngRowid ){
98507      reg = ++pParse->nMem;
98508    }else{
98509      reg = 0;
98510      for(i=0; i<pIdx->nColumn; i++){
98511        if( aXRef[pIdx->aiColumn[i]]>=0 ){
98512          reg = ++pParse->nMem;
98513          break;
98514        }
98515      }
98516    }
98517    aRegIdx[j] = reg;
98518  }
98519
98520  /* Begin generating code. */
98521  v = sqlite3GetVdbe(pParse);
98522  if( v==0 ) goto update_cleanup;
98523  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
98524  sqlite3BeginWriteOperation(pParse, 1, iDb);
98525
98526#ifndef SQLITE_OMIT_VIRTUALTABLE
98527  /* Virtual tables must be handled separately */
98528  if( IsVirtual(pTab) ){
98529    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
98530                       pWhere, onError);
98531    pWhere = 0;
98532    pTabList = 0;
98533    goto update_cleanup;
98534  }
98535#endif
98536
98537  /* Allocate required registers. */
98538  regOldRowid = regNewRowid = ++pParse->nMem;
98539  if( pTrigger || hasFK ){
98540    regOld = pParse->nMem + 1;
98541    pParse->nMem += pTab->nCol;
98542  }
98543  if( chngRowid || pTrigger || hasFK ){
98544    regNewRowid = ++pParse->nMem;
98545  }
98546  regNew = pParse->nMem + 1;
98547  pParse->nMem += pTab->nCol;
98548
98549  /* Start the view context. */
98550  if( isView ){
98551    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
98552  }
98553
98554  /* If we are trying to update a view, realize that view into
98555  ** a ephemeral table.
98556  */
98557#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
98558  if( isView ){
98559    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
98560  }
98561#endif
98562
98563  /* Resolve the column names in all the expressions in the
98564  ** WHERE clause.
98565  */
98566  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
98567    goto update_cleanup;
98568  }
98569
98570  /* Begin the database scan
98571  */
98572  sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
98573  pWInfo = sqlite3WhereBegin(
98574      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
98575  );
98576  if( pWInfo==0 ) goto update_cleanup;
98577  okOnePass = pWInfo->okOnePass;
98578
98579  /* Remember the rowid of every item to be updated.
98580  */
98581  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
98582  if( !okOnePass ){
98583    regRowSet = ++pParse->nMem;
98584    sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
98585  }
98586
98587  /* End the database scan loop.
98588  */
98589  sqlite3WhereEnd(pWInfo);
98590
98591  /* Initialize the count of updated rows
98592  */
98593  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
98594    regRowCount = ++pParse->nMem;
98595    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
98596  }
98597
98598  if( !isView ){
98599    /*
98600    ** Open every index that needs updating.  Note that if any
98601    ** index could potentially invoke a REPLACE conflict resolution
98602    ** action, then we need to open all indices because we might need
98603    ** to be deleting some records.
98604    */
98605    if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
98606    if( onError==OE_Replace ){
98607      openAll = 1;
98608    }else{
98609      openAll = 0;
98610      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98611        if( pIdx->onError==OE_Replace ){
98612          openAll = 1;
98613          break;
98614        }
98615      }
98616    }
98617    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
98618      if( openAll || aRegIdx[i]>0 ){
98619        KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
98620        sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
98621                       (char*)pKey, P4_KEYINFO_HANDOFF);
98622        assert( pParse->nTab>iCur+i+1 );
98623      }
98624    }
98625  }
98626
98627  /* Top of the update loop */
98628  if( okOnePass ){
98629    int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
98630    addr = sqlite3VdbeAddOp0(v, OP_Goto);
98631    sqlite3VdbeJumpHere(v, a1);
98632  }else{
98633    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
98634  }
98635
98636  /* Make cursor iCur point to the record that is being updated. If
98637  ** this record does not exist for some reason (deleted by a trigger,
98638  ** for example, then jump to the next iteration of the RowSet loop.  */
98639  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
98640
98641  /* If the record number will change, set register regNewRowid to
98642  ** contain the new value. If the record number is not being modified,
98643  ** then regNewRowid is the same register as regOldRowid, which is
98644  ** already populated.  */
98645  assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
98646  if( chngRowid ){
98647    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
98648    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
98649  }
98650
98651  /* If there are triggers on this table, populate an array of registers
98652  ** with the required old.* column data.  */
98653  if( hasFK || pTrigger ){
98654    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
98655    oldmask |= sqlite3TriggerColmask(pParse,
98656        pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
98657    );
98658    for(i=0; i<pTab->nCol; i++){
98659      if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
98660        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
98661      }else{
98662        sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
98663      }
98664    }
98665    if( chngRowid==0 ){
98666      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
98667    }
98668  }
98669
98670  /* Populate the array of registers beginning at regNew with the new
98671  ** row data. This array is used to check constaints, create the new
98672  ** table and index records, and as the values for any new.* references
98673  ** made by triggers.
98674  **
98675  ** If there are one or more BEFORE triggers, then do not populate the
98676  ** registers associated with columns that are (a) not modified by
98677  ** this UPDATE statement and (b) not accessed by new.* references. The
98678  ** values for registers not modified by the UPDATE must be reloaded from
98679  ** the database after the BEFORE triggers are fired anyway (as the trigger
98680  ** may have modified them). So not loading those that are not going to
98681  ** be used eliminates some redundant opcodes.
98682  */
98683  newmask = sqlite3TriggerColmask(
98684      pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
98685  );
98686  for(i=0; i<pTab->nCol; i++){
98687    if( i==pTab->iPKey ){
98688      sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
98689    }else{
98690      j = aXRef[i];
98691      if( j>=0 ){
98692        sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
98693      }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
98694        /* This branch loads the value of a column that will not be changed
98695        ** into a register. This is done if there are no BEFORE triggers, or
98696        ** if there are one or more BEFORE triggers that use this value via
98697        ** a new.* reference in a trigger program.
98698        */
98699        testcase( i==31 );
98700        testcase( i==32 );
98701        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
98702        sqlite3ColumnDefault(v, pTab, i, regNew+i);
98703      }
98704    }
98705  }
98706
98707  /* Fire any BEFORE UPDATE triggers. This happens before constraints are
98708  ** verified. One could argue that this is wrong.
98709  */
98710  if( tmask&TRIGGER_BEFORE ){
98711    sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
98712    sqlite3TableAffinityStr(v, pTab);
98713    sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
98714        TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
98715
98716    /* The row-trigger may have deleted the row being updated. In this
98717    ** case, jump to the next row. No updates or AFTER triggers are
98718    ** required. This behaviour - what happens when the row being updated
98719    ** is deleted or renamed by a BEFORE trigger - is left undefined in the
98720    ** documentation.
98721    */
98722    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
98723
98724    /* If it did not delete it, the row-trigger may still have modified
98725    ** some of the columns of the row being updated. Load the values for
98726    ** all columns not modified by the update statement into their
98727    ** registers in case this has happened.
98728    */
98729    for(i=0; i<pTab->nCol; i++){
98730      if( aXRef[i]<0 && i!=pTab->iPKey ){
98731        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
98732        sqlite3ColumnDefault(v, pTab, i, regNew+i);
98733      }
98734    }
98735  }
98736
98737  if( !isView ){
98738    int j1;                       /* Address of jump instruction */
98739
98740    /* Do constraint checks. */
98741    sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
98742        aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
98743
98744    /* Do FK constraint checks. */
98745    if( hasFK ){
98746      sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
98747    }
98748
98749    /* Delete the index entries associated with the current record.  */
98750    j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
98751    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
98752
98753    /* If changing the record number, delete the old record.  */
98754    if( hasFK || chngRowid ){
98755      sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
98756    }
98757    sqlite3VdbeJumpHere(v, j1);
98758
98759    if( hasFK ){
98760      sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
98761    }
98762
98763    /* Insert the new index entries and the new record. */
98764    sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
98765
98766    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
98767    ** handle rows (possibly in other tables) that refer via a foreign key
98768    ** to the row just updated. */
98769    if( hasFK ){
98770      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
98771    }
98772  }
98773
98774  /* Increment the row counter
98775  */
98776  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
98777    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
98778  }
98779
98780  sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
98781      TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
98782
98783  /* Repeat the above with the next record to be updated, until
98784  ** all record selected by the WHERE clause have been updated.
98785  */
98786  sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
98787  sqlite3VdbeJumpHere(v, addr);
98788
98789  /* Close all tables */
98790  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
98791    if( openAll || aRegIdx[i]>0 ){
98792      sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
98793    }
98794  }
98795  sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
98796
98797  /* Update the sqlite_sequence table by storing the content of the
98798  ** maximum rowid counter values recorded while inserting into
98799  ** autoincrement tables.
98800  */
98801  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
98802    sqlite3AutoincrementEnd(pParse);
98803  }
98804
98805  /*
98806  ** Return the number of rows that were changed. If this routine is
98807  ** generating code because of a call to sqlite3NestedParse(), do not
98808  ** invoke the callback function.
98809  */
98810  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
98811    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
98812    sqlite3VdbeSetNumCols(v, 1);
98813    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
98814  }
98815
98816update_cleanup:
98817  sqlite3AuthContextPop(&sContext);
98818  sqlite3DbFree(db, aRegIdx);
98819  sqlite3DbFree(db, aXRef);
98820  sqlite3SrcListDelete(db, pTabList);
98821  sqlite3ExprListDelete(db, pChanges);
98822  sqlite3ExprDelete(db, pWhere);
98823  return;
98824}
98825/* Make sure "isView" and other macros defined above are undefined. Otherwise
98826** thely may interfere with compilation of other functions in this file
98827** (or in another file, if this file becomes part of the amalgamation).  */
98828#ifdef isView
98829 #undef isView
98830#endif
98831#ifdef pTrigger
98832 #undef pTrigger
98833#endif
98834
98835#ifndef SQLITE_OMIT_VIRTUALTABLE
98836/*
98837** Generate code for an UPDATE of a virtual table.
98838**
98839** The strategy is that we create an ephemerial table that contains
98840** for each row to be changed:
98841**
98842**   (A)  The original rowid of that row.
98843**   (B)  The revised rowid for the row. (note1)
98844**   (C)  The content of every column in the row.
98845**
98846** Then we loop over this ephemeral table and for each row in
98847** the ephermeral table call VUpdate.
98848**
98849** When finished, drop the ephemeral table.
98850**
98851** (note1) Actually, if we know in advance that (A) is always the same
98852** as (B) we only store (A), then duplicate (A) when pulling
98853** it out of the ephemeral table before calling VUpdate.
98854*/
98855static void updateVirtualTable(
98856  Parse *pParse,       /* The parsing context */
98857  SrcList *pSrc,       /* The virtual table to be modified */
98858  Table *pTab,         /* The virtual table */
98859  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
98860  Expr *pRowid,        /* Expression used to recompute the rowid */
98861  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
98862  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
98863  int onError          /* ON CONFLICT strategy */
98864){
98865  Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
98866  ExprList *pEList = 0;     /* The result set of the SELECT statement */
98867  Select *pSelect = 0;      /* The SELECT statement */
98868  Expr *pExpr;              /* Temporary expression */
98869  int ephemTab;             /* Table holding the result of the SELECT */
98870  int i;                    /* Loop counter */
98871  int addr;                 /* Address of top of loop */
98872  int iReg;                 /* First register in set passed to OP_VUpdate */
98873  sqlite3 *db = pParse->db; /* Database connection */
98874  const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
98875  SelectDest dest;
98876
98877  /* Construct the SELECT statement that will find the new values for
98878  ** all updated rows.
98879  */
98880  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
98881  if( pRowid ){
98882    pEList = sqlite3ExprListAppend(pParse, pEList,
98883                                   sqlite3ExprDup(db, pRowid, 0));
98884  }
98885  assert( pTab->iPKey<0 );
98886  for(i=0; i<pTab->nCol; i++){
98887    if( aXRef[i]>=0 ){
98888      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
98889    }else{
98890      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
98891    }
98892    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
98893  }
98894  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
98895
98896  /* Create the ephemeral table into which the update results will
98897  ** be stored.
98898  */
98899  assert( v );
98900  ephemTab = pParse->nTab++;
98901  sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
98902  sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
98903
98904  /* fill the ephemeral table
98905  */
98906  sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
98907  sqlite3Select(pParse, pSelect, &dest);
98908
98909  /* Generate code to scan the ephemeral table and call VUpdate. */
98910  iReg = ++pParse->nMem;
98911  pParse->nMem += pTab->nCol+1;
98912  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
98913  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
98914  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
98915  for(i=0; i<pTab->nCol; i++){
98916    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
98917  }
98918  sqlite3VtabMakeWritable(pParse, pTab);
98919  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
98920  sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
98921  sqlite3MayAbort(pParse);
98922  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
98923  sqlite3VdbeJumpHere(v, addr);
98924  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
98925
98926  /* Cleanup */
98927  sqlite3SelectDelete(db, pSelect);
98928}
98929#endif /* SQLITE_OMIT_VIRTUALTABLE */
98930
98931/************** End of update.c **********************************************/
98932/************** Begin file vacuum.c ******************************************/
98933/*
98934** 2003 April 6
98935**
98936** The author disclaims copyright to this source code.  In place of
98937** a legal notice, here is a blessing:
98938**
98939**    May you do good and not evil.
98940**    May you find forgiveness for yourself and forgive others.
98941**    May you share freely, never taking more than you give.
98942**
98943*************************************************************************
98944** This file contains code used to implement the VACUUM command.
98945**
98946** Most of the code in this file may be omitted by defining the
98947** SQLITE_OMIT_VACUUM macro.
98948*/
98949
98950#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
98951/*
98952** Finalize a prepared statement.  If there was an error, store the
98953** text of the error message in *pzErrMsg.  Return the result code.
98954*/
98955static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
98956  int rc;
98957  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
98958  if( rc ){
98959    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
98960  }
98961  return rc;
98962}
98963
98964/*
98965** Execute zSql on database db. Return an error code.
98966*/
98967static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
98968  sqlite3_stmt *pStmt;
98969  VVA_ONLY( int rc; )
98970  if( !zSql ){
98971    return SQLITE_NOMEM;
98972  }
98973  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
98974    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
98975    return sqlite3_errcode(db);
98976  }
98977  VVA_ONLY( rc = ) sqlite3_step(pStmt);
98978  assert( rc!=SQLITE_ROW );
98979  return vacuumFinalize(db, pStmt, pzErrMsg);
98980}
98981
98982/*
98983** Execute zSql on database db. The statement returns exactly
98984** one column. Execute this as SQL on the same database.
98985*/
98986static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
98987  sqlite3_stmt *pStmt;
98988  int rc;
98989
98990  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
98991  if( rc!=SQLITE_OK ) return rc;
98992
98993  while( SQLITE_ROW==sqlite3_step(pStmt) ){
98994    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
98995    if( rc!=SQLITE_OK ){
98996      vacuumFinalize(db, pStmt, pzErrMsg);
98997      return rc;
98998    }
98999  }
99000
99001  return vacuumFinalize(db, pStmt, pzErrMsg);
99002}
99003
99004/*
99005** The non-standard VACUUM command is used to clean up the database,
99006** collapse free space, etc.  It is modelled after the VACUUM command
99007** in PostgreSQL.
99008**
99009** In version 1.0.x of SQLite, the VACUUM command would call
99010** gdbm_reorganize() on all the database tables.  But beginning
99011** with 2.0.0, SQLite no longer uses GDBM so this command has
99012** become a no-op.
99013*/
99014SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
99015  Vdbe *v = sqlite3GetVdbe(pParse);
99016  if( v ){
99017    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
99018  }
99019  return;
99020}
99021
99022/*
99023** This routine implements the OP_Vacuum opcode of the VDBE.
99024*/
99025SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
99026  int rc = SQLITE_OK;     /* Return code from service routines */
99027  Btree *pMain;           /* The database being vacuumed */
99028  Btree *pTemp;           /* The temporary database we vacuum into */
99029  char *zSql = 0;         /* SQL statements */
99030  int saved_flags;        /* Saved value of the db->flags */
99031  int saved_nChange;      /* Saved value of db->nChange */
99032  int saved_nTotalChange; /* Saved value of db->nTotalChange */
99033  void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
99034  Db *pDb = 0;            /* Database to detach at end of vacuum */
99035  int isMemDb;            /* True if vacuuming a :memory: database */
99036  int nRes;               /* Bytes of reserved space at the end of each page */
99037  int nDb;                /* Number of attached databases */
99038
99039  if( !db->autoCommit ){
99040    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
99041    return SQLITE_ERROR;
99042  }
99043  if( db->activeVdbeCnt>1 ){
99044    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
99045    return SQLITE_ERROR;
99046  }
99047
99048  /* Save the current value of the database flags so that it can be
99049  ** restored before returning. Then set the writable-schema flag, and
99050  ** disable CHECK and foreign key constraints.  */
99051  saved_flags = db->flags;
99052  saved_nChange = db->nChange;
99053  saved_nTotalChange = db->nTotalChange;
99054  saved_xTrace = db->xTrace;
99055  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
99056  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
99057  db->xTrace = 0;
99058
99059  pMain = db->aDb[0].pBt;
99060  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
99061
99062  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
99063  ** can be set to 'off' for this file, as it is not recovered if a crash
99064  ** occurs anyway. The integrity of the database is maintained by a
99065  ** (possibly synchronous) transaction opened on the main database before
99066  ** sqlite3BtreeCopyFile() is called.
99067  **
99068  ** An optimisation would be to use a non-journaled pager.
99069  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
99070  ** that actually made the VACUUM run slower.  Very little journalling
99071  ** actually occurs when doing a vacuum since the vacuum_db is initially
99072  ** empty.  Only the journal header is written.  Apparently it takes more
99073  ** time to parse and run the PRAGMA to turn journalling off than it does
99074  ** to write the journal header file.
99075  */
99076  nDb = db->nDb;
99077  if( sqlite3TempInMemory(db) ){
99078    zSql = "ATTACH ':memory:' AS vacuum_db;";
99079  }else{
99080    zSql = "ATTACH '' AS vacuum_db;";
99081  }
99082  rc = execSql(db, pzErrMsg, zSql);
99083  if( db->nDb>nDb ){
99084    pDb = &db->aDb[db->nDb-1];
99085    assert( strcmp(pDb->zName,"vacuum_db")==0 );
99086  }
99087  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99088  pTemp = db->aDb[db->nDb-1].pBt;
99089
99090  /* The call to execSql() to attach the temp database has left the file
99091  ** locked (as there was more than one active statement when the transaction
99092  ** to read the schema was concluded. Unlock it here so that this doesn't
99093  ** cause problems for the call to BtreeSetPageSize() below.  */
99094  sqlite3BtreeCommit(pTemp);
99095
99096  nRes = sqlite3BtreeGetReserve(pMain);
99097
99098  /* A VACUUM cannot change the pagesize of an encrypted database. */
99099#ifdef SQLITE_HAS_CODEC
99100  if( db->nextPagesize ){
99101    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
99102    int nKey;
99103    char *zKey;
99104    sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
99105    if( nKey ) db->nextPagesize = 0;
99106  }
99107#endif
99108
99109  /* Do not attempt to change the page size for a WAL database */
99110  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
99111                                               ==PAGER_JOURNALMODE_WAL ){
99112    db->nextPagesize = 0;
99113  }
99114
99115  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
99116   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
99117   || NEVER(db->mallocFailed)
99118  ){
99119    rc = SQLITE_NOMEM;
99120    goto end_of_vacuum;
99121  }
99122  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
99123  if( rc!=SQLITE_OK ){
99124    goto end_of_vacuum;
99125  }
99126
99127#ifndef SQLITE_OMIT_AUTOVACUUM
99128  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
99129                                           sqlite3BtreeGetAutoVacuum(pMain));
99130#endif
99131
99132  /* Begin a transaction */
99133  rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
99134  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99135
99136  /* Query the schema of the main database. Create a mirror schema
99137  ** in the temporary database.
99138  */
99139  rc = execExecSql(db, pzErrMsg,
99140      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
99141      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
99142      "   AND rootpage>0"
99143  );
99144  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99145  rc = execExecSql(db, pzErrMsg,
99146      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
99147      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
99148  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99149  rc = execExecSql(db, pzErrMsg,
99150      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
99151      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
99152  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99153
99154  /* Loop through the tables in the main database. For each, do
99155  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
99156  ** the contents to the temporary database.
99157  */
99158  rc = execExecSql(db, pzErrMsg,
99159      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
99160      "|| ' SELECT * FROM main.' || quote(name) || ';'"
99161      "FROM main.sqlite_master "
99162      "WHERE type = 'table' AND name!='sqlite_sequence' "
99163      "  AND rootpage>0"
99164  );
99165  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99166
99167  /* Copy over the sequence table
99168  */
99169  rc = execExecSql(db, pzErrMsg,
99170      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
99171      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
99172  );
99173  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99174  rc = execExecSql(db, pzErrMsg,
99175      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
99176      "|| ' SELECT * FROM main.' || quote(name) || ';' "
99177      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
99178  );
99179  if( rc!=SQLITE_OK ) goto end_of_vacuum;
99180
99181
99182  /* Copy the triggers, views, and virtual tables from the main database
99183  ** over to the temporary database.  None of these objects has any
99184  ** associated storage, so all we have to do is copy their entries
99185  ** from the SQLITE_MASTER table.
99186  */
99187  rc = execSql(db, pzErrMsg,
99188      "INSERT INTO vacuum_db.sqlite_master "
99189      "  SELECT type, name, tbl_name, rootpage, sql"
99190      "    FROM main.sqlite_master"
99191      "   WHERE type='view' OR type='trigger'"
99192      "      OR (type='table' AND rootpage=0)"
99193  );
99194  if( rc ) goto end_of_vacuum;
99195
99196  /* At this point, unless the main db was completely empty, there is now a
99197  ** transaction open on the vacuum database, but not on the main database.
99198  ** Open a btree level transaction on the main database. This allows a
99199  ** call to sqlite3BtreeCopyFile(). The main database btree level
99200  ** transaction is then committed, so the SQL level never knows it was
99201  ** opened for writing. This way, the SQL transaction used to create the
99202  ** temporary database never needs to be committed.
99203  */
99204  {
99205    u32 meta;
99206    int i;
99207
99208    /* This array determines which meta meta values are preserved in the
99209    ** vacuum.  Even entries are the meta value number and odd entries
99210    ** are an increment to apply to the meta value after the vacuum.
99211    ** The increment is used to increase the schema cookie so that other
99212    ** connections to the same database will know to reread the schema.
99213    */
99214    static const unsigned char aCopy[] = {
99215       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
99216       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
99217       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
99218       BTREE_USER_VERSION,       0,  /* Preserve the user version */
99219    };
99220
99221    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
99222    assert( 1==sqlite3BtreeIsInTrans(pMain) );
99223
99224    /* Copy Btree meta values */
99225    for(i=0; i<ArraySize(aCopy); i+=2){
99226      /* GetMeta() and UpdateMeta() cannot fail in this context because
99227      ** we already have page 1 loaded into cache and marked dirty. */
99228      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
99229      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
99230      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
99231    }
99232
99233    rc = sqlite3BtreeCopyFile(pMain, pTemp);
99234    if( rc!=SQLITE_OK ) goto end_of_vacuum;
99235    rc = sqlite3BtreeCommit(pTemp);
99236    if( rc!=SQLITE_OK ) goto end_of_vacuum;
99237#ifndef SQLITE_OMIT_AUTOVACUUM
99238    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
99239#endif
99240  }
99241
99242  assert( rc==SQLITE_OK );
99243  rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
99244
99245end_of_vacuum:
99246  /* Restore the original value of db->flags */
99247  db->flags = saved_flags;
99248  db->nChange = saved_nChange;
99249  db->nTotalChange = saved_nTotalChange;
99250  db->xTrace = saved_xTrace;
99251  sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
99252
99253  /* Currently there is an SQL level transaction open on the vacuum
99254  ** database. No locks are held on any other files (since the main file
99255  ** was committed at the btree level). So it safe to end the transaction
99256  ** by manually setting the autoCommit flag to true and detaching the
99257  ** vacuum database. The vacuum_db journal file is deleted when the pager
99258  ** is closed by the DETACH.
99259  */
99260  db->autoCommit = 1;
99261
99262  if( pDb ){
99263    sqlite3BtreeClose(pDb->pBt);
99264    pDb->pBt = 0;
99265    pDb->pSchema = 0;
99266  }
99267
99268  /* This both clears the schemas and reduces the size of the db->aDb[]
99269  ** array. */
99270  sqlite3ResetInternalSchema(db, -1);
99271
99272  return rc;
99273}
99274
99275#endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
99276
99277/************** End of vacuum.c **********************************************/
99278/************** Begin file vtab.c ********************************************/
99279/*
99280** 2006 June 10
99281**
99282** The author disclaims copyright to this source code.  In place of
99283** a legal notice, here is a blessing:
99284**
99285**    May you do good and not evil.
99286**    May you find forgiveness for yourself and forgive others.
99287**    May you share freely, never taking more than you give.
99288**
99289*************************************************************************
99290** This file contains code used to help implement virtual tables.
99291*/
99292#ifndef SQLITE_OMIT_VIRTUALTABLE
99293
99294/*
99295** Before a virtual table xCreate() or xConnect() method is invoked, the
99296** sqlite3.pVtabCtx member variable is set to point to an instance of
99297** this struct allocated on the stack. It is used by the implementation of
99298** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
99299** are invoked only from within xCreate and xConnect methods.
99300*/
99301struct VtabCtx {
99302  Table *pTab;
99303  VTable *pVTable;
99304};
99305
99306/*
99307** The actual function that does the work of creating a new module.
99308** This function implements the sqlite3_create_module() and
99309** sqlite3_create_module_v2() interfaces.
99310*/
99311static int createModule(
99312  sqlite3 *db,                    /* Database in which module is registered */
99313  const char *zName,              /* Name assigned to this module */
99314  const sqlite3_module *pModule,  /* The definition of the module */
99315  void *pAux,                     /* Context pointer for xCreate/xConnect */
99316  void (*xDestroy)(void *)        /* Module destructor function */
99317){
99318  int rc, nName;
99319  Module *pMod;
99320
99321  sqlite3_mutex_enter(db->mutex);
99322  nName = sqlite3Strlen30(zName);
99323  pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
99324  if( pMod ){
99325    Module *pDel;
99326    char *zCopy = (char *)(&pMod[1]);
99327    memcpy(zCopy, zName, nName+1);
99328    pMod->zName = zCopy;
99329    pMod->pModule = pModule;
99330    pMod->pAux = pAux;
99331    pMod->xDestroy = xDestroy;
99332    pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
99333    if( pDel && pDel->xDestroy ){
99334      sqlite3ResetInternalSchema(db, -1);
99335      pDel->xDestroy(pDel->pAux);
99336    }
99337    sqlite3DbFree(db, pDel);
99338    if( pDel==pMod ){
99339      db->mallocFailed = 1;
99340    }
99341  }else if( xDestroy ){
99342    xDestroy(pAux);
99343  }
99344  rc = sqlite3ApiExit(db, SQLITE_OK);
99345  sqlite3_mutex_leave(db->mutex);
99346  return rc;
99347}
99348
99349
99350/*
99351** External API function used to create a new virtual-table module.
99352*/
99353SQLITE_API int sqlite3_create_module(
99354  sqlite3 *db,                    /* Database in which module is registered */
99355  const char *zName,              /* Name assigned to this module */
99356  const sqlite3_module *pModule,  /* The definition of the module */
99357  void *pAux                      /* Context pointer for xCreate/xConnect */
99358){
99359  return createModule(db, zName, pModule, pAux, 0);
99360}
99361
99362/*
99363** External API function used to create a new virtual-table module.
99364*/
99365SQLITE_API int sqlite3_create_module_v2(
99366  sqlite3 *db,                    /* Database in which module is registered */
99367  const char *zName,              /* Name assigned to this module */
99368  const sqlite3_module *pModule,  /* The definition of the module */
99369  void *pAux,                     /* Context pointer for xCreate/xConnect */
99370  void (*xDestroy)(void *)        /* Module destructor function */
99371){
99372  return createModule(db, zName, pModule, pAux, xDestroy);
99373}
99374
99375/*
99376** Lock the virtual table so that it cannot be disconnected.
99377** Locks nest.  Every lock should have a corresponding unlock.
99378** If an unlock is omitted, resources leaks will occur.
99379**
99380** If a disconnect is attempted while a virtual table is locked,
99381** the disconnect is deferred until all locks have been removed.
99382*/
99383SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
99384  pVTab->nRef++;
99385}
99386
99387
99388/*
99389** pTab is a pointer to a Table structure representing a virtual-table.
99390** Return a pointer to the VTable object used by connection db to access
99391** this virtual-table, if one has been created, or NULL otherwise.
99392*/
99393SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
99394  VTable *pVtab;
99395  assert( IsVirtual(pTab) );
99396  for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
99397  return pVtab;
99398}
99399
99400/*
99401** Decrement the ref-count on a virtual table object. When the ref-count
99402** reaches zero, call the xDisconnect() method to delete the object.
99403*/
99404SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
99405  sqlite3 *db = pVTab->db;
99406
99407  assert( db );
99408  assert( pVTab->nRef>0 );
99409  assert( sqlite3SafetyCheckOk(db) );
99410
99411  pVTab->nRef--;
99412  if( pVTab->nRef==0 ){
99413    sqlite3_vtab *p = pVTab->pVtab;
99414    if( p ){
99415      p->pModule->xDisconnect(p);
99416    }
99417    sqlite3DbFree(db, pVTab);
99418  }
99419}
99420
99421/*
99422** Table p is a virtual table. This function moves all elements in the
99423** p->pVTable list to the sqlite3.pDisconnect lists of their associated
99424** database connections to be disconnected at the next opportunity.
99425** Except, if argument db is not NULL, then the entry associated with
99426** connection db is left in the p->pVTable list.
99427*/
99428static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
99429  VTable *pRet = 0;
99430  VTable *pVTable = p->pVTable;
99431  p->pVTable = 0;
99432
99433  /* Assert that the mutex (if any) associated with the BtShared database
99434  ** that contains table p is held by the caller. See header comments
99435  ** above function sqlite3VtabUnlockList() for an explanation of why
99436  ** this makes it safe to access the sqlite3.pDisconnect list of any
99437  ** database connection that may have an entry in the p->pVTable list.
99438  */
99439  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
99440
99441  while( pVTable ){
99442    sqlite3 *db2 = pVTable->db;
99443    VTable *pNext = pVTable->pNext;
99444    assert( db2 );
99445    if( db2==db ){
99446      pRet = pVTable;
99447      p->pVTable = pRet;
99448      pRet->pNext = 0;
99449    }else{
99450      pVTable->pNext = db2->pDisconnect;
99451      db2->pDisconnect = pVTable;
99452    }
99453    pVTable = pNext;
99454  }
99455
99456  assert( !db || pRet );
99457  return pRet;
99458}
99459
99460
99461/*
99462** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
99463**
99464** This function may only be called when the mutexes associated with all
99465** shared b-tree databases opened using connection db are held by the
99466** caller. This is done to protect the sqlite3.pDisconnect list. The
99467** sqlite3.pDisconnect list is accessed only as follows:
99468**
99469**   1) By this function. In this case, all BtShared mutexes and the mutex
99470**      associated with the database handle itself must be held.
99471**
99472**   2) By function vtabDisconnectAll(), when it adds a VTable entry to
99473**      the sqlite3.pDisconnect list. In this case either the BtShared mutex
99474**      associated with the database the virtual table is stored in is held
99475**      or, if the virtual table is stored in a non-sharable database, then
99476**      the database handle mutex is held.
99477**
99478** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
99479** by multiple threads. It is thread-safe.
99480*/
99481SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
99482  VTable *p = db->pDisconnect;
99483  db->pDisconnect = 0;
99484
99485  assert( sqlite3BtreeHoldsAllMutexes(db) );
99486  assert( sqlite3_mutex_held(db->mutex) );
99487
99488  if( p ){
99489    sqlite3ExpirePreparedStatements(db);
99490    do {
99491      VTable *pNext = p->pNext;
99492      sqlite3VtabUnlock(p);
99493      p = pNext;
99494    }while( p );
99495  }
99496}
99497
99498/*
99499** Clear any and all virtual-table information from the Table record.
99500** This routine is called, for example, just before deleting the Table
99501** record.
99502**
99503** Since it is a virtual-table, the Table structure contains a pointer
99504** to the head of a linked list of VTable structures. Each VTable
99505** structure is associated with a single sqlite3* user of the schema.
99506** The reference count of the VTable structure associated with database
99507** connection db is decremented immediately (which may lead to the
99508** structure being xDisconnected and free). Any other VTable structures
99509** in the list are moved to the sqlite3.pDisconnect list of the associated
99510** database connection.
99511*/
99512SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
99513  if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
99514  if( p->azModuleArg ){
99515    int i;
99516    for(i=0; i<p->nModuleArg; i++){
99517      sqlite3DbFree(db, p->azModuleArg[i]);
99518    }
99519    sqlite3DbFree(db, p->azModuleArg);
99520  }
99521}
99522
99523/*
99524** Add a new module argument to pTable->azModuleArg[].
99525** The string is not copied - the pointer is stored.  The
99526** string will be freed automatically when the table is
99527** deleted.
99528*/
99529static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
99530  int i = pTable->nModuleArg++;
99531  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
99532  char **azModuleArg;
99533  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
99534  if( azModuleArg==0 ){
99535    int j;
99536    for(j=0; j<i; j++){
99537      sqlite3DbFree(db, pTable->azModuleArg[j]);
99538    }
99539    sqlite3DbFree(db, zArg);
99540    sqlite3DbFree(db, pTable->azModuleArg);
99541    pTable->nModuleArg = 0;
99542  }else{
99543    azModuleArg[i] = zArg;
99544    azModuleArg[i+1] = 0;
99545  }
99546  pTable->azModuleArg = azModuleArg;
99547}
99548
99549/*
99550** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
99551** statement.  The module name has been parsed, but the optional list
99552** of parameters that follow the module name are still pending.
99553*/
99554SQLITE_PRIVATE void sqlite3VtabBeginParse(
99555  Parse *pParse,        /* Parsing context */
99556  Token *pName1,        /* Name of new table, or database name */
99557  Token *pName2,        /* Name of new table or NULL */
99558  Token *pModuleName    /* Name of the module for the virtual table */
99559){
99560  int iDb;              /* The database the table is being created in */
99561  Table *pTable;        /* The new virtual table */
99562  sqlite3 *db;          /* Database connection */
99563
99564  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
99565  pTable = pParse->pNewTable;
99566  if( pTable==0 ) return;
99567  assert( 0==pTable->pIndex );
99568
99569  db = pParse->db;
99570  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
99571  assert( iDb>=0 );
99572
99573  pTable->tabFlags |= TF_Virtual;
99574  pTable->nModuleArg = 0;
99575  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
99576  addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
99577  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
99578  pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
99579
99580#ifndef SQLITE_OMIT_AUTHORIZATION
99581  /* Creating a virtual table invokes the authorization callback twice.
99582  ** The first invocation, to obtain permission to INSERT a row into the
99583  ** sqlite_master table, has already been made by sqlite3StartTable().
99584  ** The second call, to obtain permission to create the table, is made now.
99585  */
99586  if( pTable->azModuleArg ){
99587    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
99588            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
99589  }
99590#endif
99591}
99592
99593/*
99594** This routine takes the module argument that has been accumulating
99595** in pParse->zArg[] and appends it to the list of arguments on the
99596** virtual table currently under construction in pParse->pTable.
99597*/
99598static void addArgumentToVtab(Parse *pParse){
99599  if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
99600    const char *z = (const char*)pParse->sArg.z;
99601    int n = pParse->sArg.n;
99602    sqlite3 *db = pParse->db;
99603    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
99604  }
99605}
99606
99607/*
99608** The parser calls this routine after the CREATE VIRTUAL TABLE statement
99609** has been completely parsed.
99610*/
99611SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
99612  Table *pTab = pParse->pNewTable;  /* The table being constructed */
99613  sqlite3 *db = pParse->db;         /* The database connection */
99614
99615  if( pTab==0 ) return;
99616  addArgumentToVtab(pParse);
99617  pParse->sArg.z = 0;
99618  if( pTab->nModuleArg<1 ) return;
99619
99620  /* If the CREATE VIRTUAL TABLE statement is being entered for the
99621  ** first time (in other words if the virtual table is actually being
99622  ** created now instead of just being read out of sqlite_master) then
99623  ** do additional initialization work and store the statement text
99624  ** in the sqlite_master table.
99625  */
99626  if( !db->init.busy ){
99627    char *zStmt;
99628    char *zWhere;
99629    int iDb;
99630    Vdbe *v;
99631
99632    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
99633    if( pEnd ){
99634      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
99635    }
99636    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
99637
99638    /* A slot for the record has already been allocated in the
99639    ** SQLITE_MASTER table.  We just need to update that slot with all
99640    ** the information we've collected.
99641    **
99642    ** The VM register number pParse->regRowid holds the rowid of an
99643    ** entry in the sqlite_master table tht was created for this vtab
99644    ** by sqlite3StartTable().
99645    */
99646    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
99647    sqlite3NestedParse(pParse,
99648      "UPDATE %Q.%s "
99649         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
99650       "WHERE rowid=#%d",
99651      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
99652      pTab->zName,
99653      pTab->zName,
99654      zStmt,
99655      pParse->regRowid
99656    );
99657    sqlite3DbFree(db, zStmt);
99658    v = sqlite3GetVdbe(pParse);
99659    sqlite3ChangeCookie(pParse, iDb);
99660
99661    sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
99662    zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
99663    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
99664    sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
99665                         pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
99666  }
99667
99668  /* If we are rereading the sqlite_master table create the in-memory
99669  ** record of the table. The xConnect() method is not called until
99670  ** the first time the virtual table is used in an SQL statement. This
99671  ** allows a schema that contains virtual tables to be loaded before
99672  ** the required virtual table implementations are registered.  */
99673  else {
99674    Table *pOld;
99675    Schema *pSchema = pTab->pSchema;
99676    const char *zName = pTab->zName;
99677    int nName = sqlite3Strlen30(zName);
99678    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
99679    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
99680    if( pOld ){
99681      db->mallocFailed = 1;
99682      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
99683      return;
99684    }
99685    pParse->pNewTable = 0;
99686  }
99687}
99688
99689/*
99690** The parser calls this routine when it sees the first token
99691** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
99692*/
99693SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
99694  addArgumentToVtab(pParse);
99695  pParse->sArg.z = 0;
99696  pParse->sArg.n = 0;
99697}
99698
99699/*
99700** The parser calls this routine for each token after the first token
99701** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
99702*/
99703SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
99704  Token *pArg = &pParse->sArg;
99705  if( pArg->z==0 ){
99706    pArg->z = p->z;
99707    pArg->n = p->n;
99708  }else{
99709    assert(pArg->z < p->z);
99710    pArg->n = (int)(&p->z[p->n] - pArg->z);
99711  }
99712}
99713
99714/*
99715** Invoke a virtual table constructor (either xCreate or xConnect). The
99716** pointer to the function to invoke is passed as the fourth parameter
99717** to this procedure.
99718*/
99719static int vtabCallConstructor(
99720  sqlite3 *db,
99721  Table *pTab,
99722  Module *pMod,
99723  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
99724  char **pzErr
99725){
99726  VtabCtx sCtx;
99727  VTable *pVTable;
99728  int rc;
99729  const char *const*azArg = (const char *const*)pTab->azModuleArg;
99730  int nArg = pTab->nModuleArg;
99731  char *zErr = 0;
99732  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
99733
99734  if( !zModuleName ){
99735    return SQLITE_NOMEM;
99736  }
99737
99738  pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
99739  if( !pVTable ){
99740    sqlite3DbFree(db, zModuleName);
99741    return SQLITE_NOMEM;
99742  }
99743  pVTable->db = db;
99744  pVTable->pMod = pMod;
99745
99746  /* Invoke the virtual table constructor */
99747  assert( &db->pVtabCtx );
99748  assert( xConstruct );
99749  sCtx.pTab = pTab;
99750  sCtx.pVTable = pVTable;
99751  db->pVtabCtx = &sCtx;
99752  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
99753  db->pVtabCtx = 0;
99754  if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
99755
99756  if( SQLITE_OK!=rc ){
99757    if( zErr==0 ){
99758      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
99759    }else {
99760      *pzErr = sqlite3MPrintf(db, "%s", zErr);
99761      sqlite3_free(zErr);
99762    }
99763    sqlite3DbFree(db, pVTable);
99764  }else if( ALWAYS(pVTable->pVtab) ){
99765    /* Justification of ALWAYS():  A correct vtab constructor must allocate
99766    ** the sqlite3_vtab object if successful.  */
99767    pVTable->pVtab->pModule = pMod->pModule;
99768    pVTable->nRef = 1;
99769    if( sCtx.pTab ){
99770      const char *zFormat = "vtable constructor did not declare schema: %s";
99771      *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
99772      sqlite3VtabUnlock(pVTable);
99773      rc = SQLITE_ERROR;
99774    }else{
99775      int iCol;
99776      /* If everything went according to plan, link the new VTable structure
99777      ** into the linked list headed by pTab->pVTable. Then loop through the
99778      ** columns of the table to see if any of them contain the token "hidden".
99779      ** If so, set the Column.isHidden flag and remove the token from
99780      ** the type string.  */
99781      pVTable->pNext = pTab->pVTable;
99782      pTab->pVTable = pVTable;
99783
99784      for(iCol=0; iCol<pTab->nCol; iCol++){
99785        char *zType = pTab->aCol[iCol].zType;
99786        int nType;
99787        int i = 0;
99788        if( !zType ) continue;
99789        nType = sqlite3Strlen30(zType);
99790        if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
99791          for(i=0; i<nType; i++){
99792            if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
99793             && (zType[i+7]=='\0' || zType[i+7]==' ')
99794            ){
99795              i++;
99796              break;
99797            }
99798          }
99799        }
99800        if( i<nType ){
99801          int j;
99802          int nDel = 6 + (zType[i+6] ? 1 : 0);
99803          for(j=i; (j+nDel)<=nType; j++){
99804            zType[j] = zType[j+nDel];
99805          }
99806          if( zType[i]=='\0' && i>0 ){
99807            assert(zType[i-1]==' ');
99808            zType[i-1] = '\0';
99809          }
99810          pTab->aCol[iCol].isHidden = 1;
99811        }
99812      }
99813    }
99814  }
99815
99816  sqlite3DbFree(db, zModuleName);
99817  return rc;
99818}
99819
99820/*
99821** This function is invoked by the parser to call the xConnect() method
99822** of the virtual table pTab. If an error occurs, an error code is returned
99823** and an error left in pParse.
99824**
99825** This call is a no-op if table pTab is not a virtual table.
99826*/
99827SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
99828  sqlite3 *db = pParse->db;
99829  const char *zMod;
99830  Module *pMod;
99831  int rc;
99832
99833  assert( pTab );
99834  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
99835    return SQLITE_OK;
99836  }
99837
99838  /* Locate the required virtual table module */
99839  zMod = pTab->azModuleArg[0];
99840  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
99841
99842  if( !pMod ){
99843    const char *zModule = pTab->azModuleArg[0];
99844    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
99845    rc = SQLITE_ERROR;
99846  }else{
99847    char *zErr = 0;
99848    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
99849    if( rc!=SQLITE_OK ){
99850      sqlite3ErrorMsg(pParse, "%s", zErr);
99851    }
99852    sqlite3DbFree(db, zErr);
99853  }
99854
99855  return rc;
99856}
99857/*
99858** Grow the db->aVTrans[] array so that there is room for at least one
99859** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
99860*/
99861static int growVTrans(sqlite3 *db){
99862  const int ARRAY_INCR = 5;
99863
99864  /* Grow the sqlite3.aVTrans array if required */
99865  if( (db->nVTrans%ARRAY_INCR)==0 ){
99866    VTable **aVTrans;
99867    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
99868    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
99869    if( !aVTrans ){
99870      return SQLITE_NOMEM;
99871    }
99872    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
99873    db->aVTrans = aVTrans;
99874  }
99875
99876  return SQLITE_OK;
99877}
99878
99879/*
99880** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
99881** have already been reserved using growVTrans().
99882*/
99883static void addToVTrans(sqlite3 *db, VTable *pVTab){
99884  /* Add pVtab to the end of sqlite3.aVTrans */
99885  db->aVTrans[db->nVTrans++] = pVTab;
99886  sqlite3VtabLock(pVTab);
99887}
99888
99889/*
99890** This function is invoked by the vdbe to call the xCreate method
99891** of the virtual table named zTab in database iDb.
99892**
99893** If an error occurs, *pzErr is set to point an an English language
99894** description of the error and an SQLITE_XXX error code is returned.
99895** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
99896*/
99897SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
99898  int rc = SQLITE_OK;
99899  Table *pTab;
99900  Module *pMod;
99901  const char *zMod;
99902
99903  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
99904  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
99905
99906  /* Locate the required virtual table module */
99907  zMod = pTab->azModuleArg[0];
99908  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
99909
99910  /* If the module has been registered and includes a Create method,
99911  ** invoke it now. If the module has not been registered, return an
99912  ** error. Otherwise, do nothing.
99913  */
99914  if( !pMod ){
99915    *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
99916    rc = SQLITE_ERROR;
99917  }else{
99918    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
99919  }
99920
99921  /* Justification of ALWAYS():  The xConstructor method is required to
99922  ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
99923  if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
99924    rc = growVTrans(db);
99925    if( rc==SQLITE_OK ){
99926      addToVTrans(db, sqlite3GetVTable(db, pTab));
99927    }
99928  }
99929
99930  return rc;
99931}
99932
99933/*
99934** This function is used to set the schema of a virtual table.  It is only
99935** valid to call this function from within the xCreate() or xConnect() of a
99936** virtual table module.
99937*/
99938SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
99939  Parse *pParse;
99940
99941  int rc = SQLITE_OK;
99942  Table *pTab;
99943  char *zErr = 0;
99944
99945  sqlite3_mutex_enter(db->mutex);
99946  if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
99947    sqlite3Error(db, SQLITE_MISUSE, 0);
99948    sqlite3_mutex_leave(db->mutex);
99949    return SQLITE_MISUSE_BKPT;
99950  }
99951  assert( (pTab->tabFlags & TF_Virtual)!=0 );
99952
99953  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
99954  if( pParse==0 ){
99955    rc = SQLITE_NOMEM;
99956  }else{
99957    pParse->declareVtab = 1;
99958    pParse->db = db;
99959    pParse->nQueryLoop = 1;
99960
99961    if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
99962     && pParse->pNewTable
99963     && !db->mallocFailed
99964     && !pParse->pNewTable->pSelect
99965     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
99966    ){
99967      if( !pTab->aCol ){
99968        pTab->aCol = pParse->pNewTable->aCol;
99969        pTab->nCol = pParse->pNewTable->nCol;
99970        pParse->pNewTable->nCol = 0;
99971        pParse->pNewTable->aCol = 0;
99972      }
99973      db->pVtabCtx->pTab = 0;
99974    }else{
99975      sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
99976      sqlite3DbFree(db, zErr);
99977      rc = SQLITE_ERROR;
99978    }
99979    pParse->declareVtab = 0;
99980
99981    if( pParse->pVdbe ){
99982      sqlite3VdbeFinalize(pParse->pVdbe);
99983    }
99984    sqlite3DeleteTable(db, pParse->pNewTable);
99985    sqlite3StackFree(db, pParse);
99986  }
99987
99988  assert( (rc&0xff)==rc );
99989  rc = sqlite3ApiExit(db, rc);
99990  sqlite3_mutex_leave(db->mutex);
99991  return rc;
99992}
99993
99994/*
99995** This function is invoked by the vdbe to call the xDestroy method
99996** of the virtual table named zTab in database iDb. This occurs
99997** when a DROP TABLE is mentioned.
99998**
99999** This call is a no-op if zTab is not a virtual table.
100000*/
100001SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
100002  int rc = SQLITE_OK;
100003  Table *pTab;
100004
100005  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
100006  if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
100007    VTable *p = vtabDisconnectAll(db, pTab);
100008
100009    assert( rc==SQLITE_OK );
100010    rc = p->pMod->pModule->xDestroy(p->pVtab);
100011
100012    /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
100013    if( rc==SQLITE_OK ){
100014      assert( pTab->pVTable==p && p->pNext==0 );
100015      p->pVtab = 0;
100016      pTab->pVTable = 0;
100017      sqlite3VtabUnlock(p);
100018    }
100019  }
100020
100021  return rc;
100022}
100023
100024/*
100025** This function invokes either the xRollback or xCommit method
100026** of each of the virtual tables in the sqlite3.aVTrans array. The method
100027** called is identified by the second argument, "offset", which is
100028** the offset of the method to call in the sqlite3_module structure.
100029**
100030** The array is cleared after invoking the callbacks.
100031*/
100032static void callFinaliser(sqlite3 *db, int offset){
100033  int i;
100034  if( db->aVTrans ){
100035    for(i=0; i<db->nVTrans; i++){
100036      VTable *pVTab = db->aVTrans[i];
100037      sqlite3_vtab *p = pVTab->pVtab;
100038      if( p ){
100039        int (*x)(sqlite3_vtab *);
100040        x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
100041        if( x ) x(p);
100042      }
100043      pVTab->iSavepoint = 0;
100044      sqlite3VtabUnlock(pVTab);
100045    }
100046    sqlite3DbFree(db, db->aVTrans);
100047    db->nVTrans = 0;
100048    db->aVTrans = 0;
100049  }
100050}
100051
100052/*
100053** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
100054** array. Return the error code for the first error that occurs, or
100055** SQLITE_OK if all xSync operations are successful.
100056**
100057** Set *pzErrmsg to point to a buffer that should be released using
100058** sqlite3DbFree() containing an error message, if one is available.
100059*/
100060SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
100061  int i;
100062  int rc = SQLITE_OK;
100063  VTable **aVTrans = db->aVTrans;
100064
100065  db->aVTrans = 0;
100066  for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
100067    int (*x)(sqlite3_vtab *);
100068    sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
100069    if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
100070      rc = x(pVtab);
100071      sqlite3DbFree(db, *pzErrmsg);
100072      *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
100073      sqlite3_free(pVtab->zErrMsg);
100074    }
100075  }
100076  db->aVTrans = aVTrans;
100077  return rc;
100078}
100079
100080/*
100081** Invoke the xRollback method of all virtual tables in the
100082** sqlite3.aVTrans array. Then clear the array itself.
100083*/
100084SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
100085  callFinaliser(db, offsetof(sqlite3_module,xRollback));
100086  return SQLITE_OK;
100087}
100088
100089/*
100090** Invoke the xCommit method of all virtual tables in the
100091** sqlite3.aVTrans array. Then clear the array itself.
100092*/
100093SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
100094  callFinaliser(db, offsetof(sqlite3_module,xCommit));
100095  return SQLITE_OK;
100096}
100097
100098/*
100099** If the virtual table pVtab supports the transaction interface
100100** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
100101** not currently open, invoke the xBegin method now.
100102**
100103** If the xBegin call is successful, place the sqlite3_vtab pointer
100104** in the sqlite3.aVTrans array.
100105*/
100106SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
100107  int rc = SQLITE_OK;
100108  const sqlite3_module *pModule;
100109
100110  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
100111  ** than zero, then this function is being called from within a
100112  ** virtual module xSync() callback. It is illegal to write to
100113  ** virtual module tables in this case, so return SQLITE_LOCKED.
100114  */
100115  if( sqlite3VtabInSync(db) ){
100116    return SQLITE_LOCKED;
100117  }
100118  if( !pVTab ){
100119    return SQLITE_OK;
100120  }
100121  pModule = pVTab->pVtab->pModule;
100122
100123  if( pModule->xBegin ){
100124    int i;
100125
100126    /* If pVtab is already in the aVTrans array, return early */
100127    for(i=0; i<db->nVTrans; i++){
100128      if( db->aVTrans[i]==pVTab ){
100129        return SQLITE_OK;
100130      }
100131    }
100132
100133    /* Invoke the xBegin method. If successful, add the vtab to the
100134    ** sqlite3.aVTrans[] array. */
100135    rc = growVTrans(db);
100136    if( rc==SQLITE_OK ){
100137      rc = pModule->xBegin(pVTab->pVtab);
100138      if( rc==SQLITE_OK ){
100139        addToVTrans(db, pVTab);
100140      }
100141    }
100142  }
100143  return rc;
100144}
100145
100146/*
100147** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
100148** virtual tables that currently have an open transaction. Pass iSavepoint
100149** as the second argument to the virtual table method invoked.
100150**
100151** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
100152** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
100153** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
100154** an open transaction is invoked.
100155**
100156** If any virtual table method returns an error code other than SQLITE_OK,
100157** processing is abandoned and the error returned to the caller of this
100158** function immediately. If all calls to virtual table methods are successful,
100159** SQLITE_OK is returned.
100160*/
100161SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
100162  int rc = SQLITE_OK;
100163
100164  assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
100165  assert( iSavepoint>=0 );
100166  if( db->aVTrans ){
100167    int i;
100168    for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
100169      VTable *pVTab = db->aVTrans[i];
100170      const sqlite3_module *pMod = pVTab->pMod->pModule;
100171      if( pMod->iVersion>=2 ){
100172        int (*xMethod)(sqlite3_vtab *, int);
100173        switch( op ){
100174          case SAVEPOINT_BEGIN:
100175            xMethod = pMod->xSavepoint;
100176            pVTab->iSavepoint = iSavepoint+1;
100177            break;
100178          case SAVEPOINT_ROLLBACK:
100179            xMethod = pMod->xRollbackTo;
100180            break;
100181          default:
100182            xMethod = pMod->xRelease;
100183            break;
100184        }
100185        if( xMethod && pVTab->iSavepoint>iSavepoint ){
100186          rc = xMethod(db->aVTrans[i]->pVtab, iSavepoint);
100187        }
100188      }
100189    }
100190  }
100191  return rc;
100192}
100193
100194/*
100195** The first parameter (pDef) is a function implementation.  The
100196** second parameter (pExpr) is the first argument to this function.
100197** If pExpr is a column in a virtual table, then let the virtual
100198** table implementation have an opportunity to overload the function.
100199**
100200** This routine is used to allow virtual table implementations to
100201** overload MATCH, LIKE, GLOB, and REGEXP operators.
100202**
100203** Return either the pDef argument (indicating no change) or a
100204** new FuncDef structure that is marked as ephemeral using the
100205** SQLITE_FUNC_EPHEM flag.
100206*/
100207SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
100208  sqlite3 *db,    /* Database connection for reporting malloc problems */
100209  FuncDef *pDef,  /* Function to possibly overload */
100210  int nArg,       /* Number of arguments to the function */
100211  Expr *pExpr     /* First argument to the function */
100212){
100213  Table *pTab;
100214  sqlite3_vtab *pVtab;
100215  sqlite3_module *pMod;
100216  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
100217  void *pArg = 0;
100218  FuncDef *pNew;
100219  int rc = 0;
100220  char *zLowerName;
100221  unsigned char *z;
100222
100223
100224  /* Check to see the left operand is a column in a virtual table */
100225  if( NEVER(pExpr==0) ) return pDef;
100226  if( pExpr->op!=TK_COLUMN ) return pDef;
100227  pTab = pExpr->pTab;
100228  if( NEVER(pTab==0) ) return pDef;
100229  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
100230  pVtab = sqlite3GetVTable(db, pTab)->pVtab;
100231  assert( pVtab!=0 );
100232  assert( pVtab->pModule!=0 );
100233  pMod = (sqlite3_module *)pVtab->pModule;
100234  if( pMod->xFindFunction==0 ) return pDef;
100235
100236  /* Call the xFindFunction method on the virtual table implementation
100237  ** to see if the implementation wants to overload this function
100238  */
100239  zLowerName = sqlite3DbStrDup(db, pDef->zName);
100240  if( zLowerName ){
100241    for(z=(unsigned char*)zLowerName; *z; z++){
100242      *z = sqlite3UpperToLower[*z];
100243    }
100244    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
100245    sqlite3DbFree(db, zLowerName);
100246  }
100247  if( rc==0 ){
100248    return pDef;
100249  }
100250
100251  /* Create a new ephemeral function definition for the overloaded
100252  ** function */
100253  pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
100254                             + sqlite3Strlen30(pDef->zName) + 1);
100255  if( pNew==0 ){
100256    return pDef;
100257  }
100258  *pNew = *pDef;
100259  pNew->zName = (char *)&pNew[1];
100260  memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
100261  pNew->xFunc = xFunc;
100262  pNew->pUserData = pArg;
100263  pNew->flags |= SQLITE_FUNC_EPHEM;
100264  return pNew;
100265}
100266
100267/*
100268** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
100269** array so that an OP_VBegin will get generated for it.  Add pTab to the
100270** array if it is missing.  If pTab is already in the array, this routine
100271** is a no-op.
100272*/
100273SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
100274  Parse *pToplevel = sqlite3ParseToplevel(pParse);
100275  int i, n;
100276  Table **apVtabLock;
100277
100278  assert( IsVirtual(pTab) );
100279  for(i=0; i<pToplevel->nVtabLock; i++){
100280    if( pTab==pToplevel->apVtabLock[i] ) return;
100281  }
100282  n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
100283  apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
100284  if( apVtabLock ){
100285    pToplevel->apVtabLock = apVtabLock;
100286    pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
100287  }else{
100288    pToplevel->db->mallocFailed = 1;
100289  }
100290}
100291
100292/*
100293** Return the ON CONFLICT resolution mode in effect for the virtual
100294** table update operation currently in progress.
100295**
100296** The results of this routine are undefined unless it is called from
100297** within an xUpdate method.
100298*/
100299SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
100300  static const unsigned char aMap[] = {
100301    SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
100302  };
100303  assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
100304  assert( OE_Ignore==4 && OE_Replace==5 );
100305  assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
100306  return (int)aMap[db->vtabOnConflict-1];
100307}
100308
100309/*
100310** Call from within the xCreate() or xConnect() methods to provide
100311** the SQLite core with additional information about the behavior
100312** of the virtual table being implemented.
100313*/
100314SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
100315  va_list ap;
100316  int rc = SQLITE_OK;
100317
100318  sqlite3_mutex_enter(db->mutex);
100319
100320  va_start(ap, op);
100321  switch( op ){
100322    case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
100323      VtabCtx *p = db->pVtabCtx;
100324      if( !p ){
100325        rc = SQLITE_MISUSE_BKPT;
100326      }else{
100327        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
100328        p->pVTable->bConstraint = (u8)va_arg(ap, int);
100329      }
100330      break;
100331    }
100332    default:
100333      rc = SQLITE_MISUSE_BKPT;
100334      break;
100335  }
100336  va_end(ap);
100337
100338  if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
100339  sqlite3_mutex_leave(db->mutex);
100340  return rc;
100341}
100342
100343#endif /* SQLITE_OMIT_VIRTUALTABLE */
100344
100345/************** End of vtab.c ************************************************/
100346/************** Begin file where.c *******************************************/
100347/*
100348** 2001 September 15
100349**
100350** The author disclaims copyright to this source code.  In place of
100351** a legal notice, here is a blessing:
100352**
100353**    May you do good and not evil.
100354**    May you find forgiveness for yourself and forgive others.
100355**    May you share freely, never taking more than you give.
100356**
100357*************************************************************************
100358** This module contains C code that generates VDBE code used to process
100359** the WHERE clause of SQL statements.  This module is responsible for
100360** generating the code that loops through a table looking for applicable
100361** rows.  Indices are selected and used to speed the search when doing
100362** so is applicable.  Because this module is responsible for selecting
100363** indices, you might also think of this module as the "query optimizer".
100364*/
100365
100366
100367/*
100368** Trace output macros
100369*/
100370#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
100371SQLITE_PRIVATE int sqlite3WhereTrace = 0;
100372#endif
100373#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
100374# define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
100375#else
100376# define WHERETRACE(X)
100377#endif
100378
100379/* Forward reference
100380*/
100381typedef struct WhereClause WhereClause;
100382typedef struct WhereMaskSet WhereMaskSet;
100383typedef struct WhereOrInfo WhereOrInfo;
100384typedef struct WhereAndInfo WhereAndInfo;
100385typedef struct WhereCost WhereCost;
100386
100387/*
100388** The query generator uses an array of instances of this structure to
100389** help it analyze the subexpressions of the WHERE clause.  Each WHERE
100390** clause subexpression is separated from the others by AND operators,
100391** usually, or sometimes subexpressions separated by OR.
100392**
100393** All WhereTerms are collected into a single WhereClause structure.
100394** The following identity holds:
100395**
100396**        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
100397**
100398** When a term is of the form:
100399**
100400**              X <op> <expr>
100401**
100402** where X is a column name and <op> is one of certain operators,
100403** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
100404** cursor number and column number for X.  WhereTerm.eOperator records
100405** the <op> using a bitmask encoding defined by WO_xxx below.  The
100406** use of a bitmask encoding for the operator allows us to search
100407** quickly for terms that match any of several different operators.
100408**
100409** A WhereTerm might also be two or more subterms connected by OR:
100410**
100411**         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
100412**
100413** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
100414** and the WhereTerm.u.pOrInfo field points to auxiliary information that
100415** is collected about the
100416**
100417** If a term in the WHERE clause does not match either of the two previous
100418** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
100419** to the original subexpression content and wtFlags is set up appropriately
100420** but no other fields in the WhereTerm object are meaningful.
100421**
100422** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
100423** but they do so indirectly.  A single WhereMaskSet structure translates
100424** cursor number into bits and the translated bit is stored in the prereq
100425** fields.  The translation is used in order to maximize the number of
100426** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
100427** spread out over the non-negative integers.  For example, the cursor
100428** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
100429** translates these sparse cursor numbers into consecutive integers
100430** beginning with 0 in order to make the best possible use of the available
100431** bits in the Bitmask.  So, in the example above, the cursor numbers
100432** would be mapped into integers 0 through 7.
100433**
100434** The number of terms in a join is limited by the number of bits
100435** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
100436** is only able to process joins with 64 or fewer tables.
100437*/
100438typedef struct WhereTerm WhereTerm;
100439struct WhereTerm {
100440  Expr *pExpr;            /* Pointer to the subexpression that is this term */
100441  int iParent;            /* Disable pWC->a[iParent] when this term disabled */
100442  int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
100443  union {
100444    int leftColumn;         /* Column number of X in "X <op> <expr>" */
100445    WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
100446    WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
100447  } u;
100448  u16 eOperator;          /* A WO_xx value describing <op> */
100449  u8 wtFlags;             /* TERM_xxx bit flags.  See below */
100450  u8 nChild;              /* Number of children that must disable us */
100451  WhereClause *pWC;       /* The clause this term is part of */
100452  Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
100453  Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
100454};
100455
100456/*
100457** Allowed values of WhereTerm.wtFlags
100458*/
100459#define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
100460#define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
100461#define TERM_CODED      0x04   /* This term is already coded */
100462#define TERM_COPIED     0x08   /* Has a child */
100463#define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
100464#define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
100465#define TERM_OR_OK      0x40   /* Used during OR-clause processing */
100466#ifdef SQLITE_ENABLE_STAT2
100467#  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
100468#else
100469#  define TERM_VNULL    0x00   /* Disabled if not using stat2 */
100470#endif
100471
100472/*
100473** An instance of the following structure holds all information about a
100474** WHERE clause.  Mostly this is a container for one or more WhereTerms.
100475*/
100476struct WhereClause {
100477  Parse *pParse;           /* The parser context */
100478  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
100479  Bitmask vmask;           /* Bitmask identifying virtual table cursors */
100480  u8 op;                   /* Split operator.  TK_AND or TK_OR */
100481  int nTerm;               /* Number of terms */
100482  int nSlot;               /* Number of entries in a[] */
100483  WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
100484#if defined(SQLITE_SMALL_STACK)
100485  WhereTerm aStatic[1];    /* Initial static space for a[] */
100486#else
100487  WhereTerm aStatic[8];    /* Initial static space for a[] */
100488#endif
100489};
100490
100491/*
100492** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
100493** a dynamically allocated instance of the following structure.
100494*/
100495struct WhereOrInfo {
100496  WhereClause wc;          /* Decomposition into subterms */
100497  Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
100498};
100499
100500/*
100501** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
100502** a dynamically allocated instance of the following structure.
100503*/
100504struct WhereAndInfo {
100505  WhereClause wc;          /* The subexpression broken out */
100506};
100507
100508/*
100509** An instance of the following structure keeps track of a mapping
100510** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
100511**
100512** The VDBE cursor numbers are small integers contained in
100513** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
100514** clause, the cursor numbers might not begin with 0 and they might
100515** contain gaps in the numbering sequence.  But we want to make maximum
100516** use of the bits in our bitmasks.  This structure provides a mapping
100517** from the sparse cursor numbers into consecutive integers beginning
100518** with 0.
100519**
100520** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
100521** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
100522**
100523** For example, if the WHERE clause expression used these VDBE
100524** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
100525** would map those cursor numbers into bits 0 through 5.
100526**
100527** Note that the mapping is not necessarily ordered.  In the example
100528** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
100529** 57->5, 73->4.  Or one of 719 other combinations might be used. It
100530** does not really matter.  What is important is that sparse cursor
100531** numbers all get mapped into bit numbers that begin with 0 and contain
100532** no gaps.
100533*/
100534struct WhereMaskSet {
100535  int n;                        /* Number of assigned cursor values */
100536  int ix[BMS];                  /* Cursor assigned to each bit */
100537};
100538
100539/*
100540** A WhereCost object records a lookup strategy and the estimated
100541** cost of pursuing that strategy.
100542*/
100543struct WhereCost {
100544  WherePlan plan;    /* The lookup strategy */
100545  double rCost;      /* Overall cost of pursuing this search strategy */
100546  Bitmask used;      /* Bitmask of cursors used by this plan */
100547};
100548
100549/*
100550** Bitmasks for the operators that indices are able to exploit.  An
100551** OR-ed combination of these values can be used when searching for
100552** terms in the where clause.
100553*/
100554#define WO_IN     0x001
100555#define WO_EQ     0x002
100556#define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
100557#define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
100558#define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
100559#define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
100560#define WO_MATCH  0x040
100561#define WO_ISNULL 0x080
100562#define WO_OR     0x100       /* Two or more OR-connected terms */
100563#define WO_AND    0x200       /* Two or more AND-connected terms */
100564#define WO_NOOP   0x800       /* This term does not restrict search space */
100565
100566#define WO_ALL    0xfff       /* Mask of all possible WO_* values */
100567#define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
100568
100569/*
100570** Value for wsFlags returned by bestIndex() and stored in
100571** WhereLevel.wsFlags.  These flags determine which search
100572** strategies are appropriate.
100573**
100574** The least significant 12 bits is reserved as a mask for WO_ values above.
100575** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
100576** But if the table is the right table of a left join, WhereLevel.wsFlags
100577** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
100578** the "op" parameter to findTerm when we are resolving equality constraints.
100579** ISNULL constraints will then not be used on the right table of a left
100580** join.  Tickets #2177 and #2189.
100581*/
100582#define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
100583#define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
100584#define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
100585#define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
100586#define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
100587#define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
100588#define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
100589#define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
100590#define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
100591#define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
100592#define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
100593#define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
100594#define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
100595#define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
100596#define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
100597#define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
100598#define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
100599#define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
100600#define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
100601#define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
100602
100603/*
100604** Initialize a preallocated WhereClause structure.
100605*/
100606static void whereClauseInit(
100607  WhereClause *pWC,        /* The WhereClause to be initialized */
100608  Parse *pParse,           /* The parsing context */
100609  WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
100610){
100611  pWC->pParse = pParse;
100612  pWC->pMaskSet = pMaskSet;
100613  pWC->nTerm = 0;
100614  pWC->nSlot = ArraySize(pWC->aStatic);
100615  pWC->a = pWC->aStatic;
100616  pWC->vmask = 0;
100617}
100618
100619/* Forward reference */
100620static void whereClauseClear(WhereClause*);
100621
100622/*
100623** Deallocate all memory associated with a WhereOrInfo object.
100624*/
100625static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
100626  whereClauseClear(&p->wc);
100627  sqlite3DbFree(db, p);
100628}
100629
100630/*
100631** Deallocate all memory associated with a WhereAndInfo object.
100632*/
100633static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
100634  whereClauseClear(&p->wc);
100635  sqlite3DbFree(db, p);
100636}
100637
100638/*
100639** Deallocate a WhereClause structure.  The WhereClause structure
100640** itself is not freed.  This routine is the inverse of whereClauseInit().
100641*/
100642static void whereClauseClear(WhereClause *pWC){
100643  int i;
100644  WhereTerm *a;
100645  sqlite3 *db = pWC->pParse->db;
100646  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
100647    if( a->wtFlags & TERM_DYNAMIC ){
100648      sqlite3ExprDelete(db, a->pExpr);
100649    }
100650    if( a->wtFlags & TERM_ORINFO ){
100651      whereOrInfoDelete(db, a->u.pOrInfo);
100652    }else if( a->wtFlags & TERM_ANDINFO ){
100653      whereAndInfoDelete(db, a->u.pAndInfo);
100654    }
100655  }
100656  if( pWC->a!=pWC->aStatic ){
100657    sqlite3DbFree(db, pWC->a);
100658  }
100659}
100660
100661/*
100662** Add a single new WhereTerm entry to the WhereClause object pWC.
100663** The new WhereTerm object is constructed from Expr p and with wtFlags.
100664** The index in pWC->a[] of the new WhereTerm is returned on success.
100665** 0 is returned if the new WhereTerm could not be added due to a memory
100666** allocation error.  The memory allocation failure will be recorded in
100667** the db->mallocFailed flag so that higher-level functions can detect it.
100668**
100669** This routine will increase the size of the pWC->a[] array as necessary.
100670**
100671** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
100672** for freeing the expression p is assumed by the WhereClause object pWC.
100673** This is true even if this routine fails to allocate a new WhereTerm.
100674**
100675** WARNING:  This routine might reallocate the space used to store
100676** WhereTerms.  All pointers to WhereTerms should be invalidated after
100677** calling this routine.  Such pointers may be reinitialized by referencing
100678** the pWC->a[] array.
100679*/
100680static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
100681  WhereTerm *pTerm;
100682  int idx;
100683  testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
100684  if( pWC->nTerm>=pWC->nSlot ){
100685    WhereTerm *pOld = pWC->a;
100686    sqlite3 *db = pWC->pParse->db;
100687    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
100688    if( pWC->a==0 ){
100689      if( wtFlags & TERM_DYNAMIC ){
100690        sqlite3ExprDelete(db, p);
100691      }
100692      pWC->a = pOld;
100693      return 0;
100694    }
100695    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
100696    if( pOld!=pWC->aStatic ){
100697      sqlite3DbFree(db, pOld);
100698    }
100699    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
100700  }
100701  pTerm = &pWC->a[idx = pWC->nTerm++];
100702  pTerm->pExpr = p;
100703  pTerm->wtFlags = wtFlags;
100704  pTerm->pWC = pWC;
100705  pTerm->iParent = -1;
100706  return idx;
100707}
100708
100709/*
100710** This routine identifies subexpressions in the WHERE clause where
100711** each subexpression is separated by the AND operator or some other
100712** operator specified in the op parameter.  The WhereClause structure
100713** is filled with pointers to subexpressions.  For example:
100714**
100715**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
100716**           \________/     \_______________/     \________________/
100717**            slot[0]            slot[1]               slot[2]
100718**
100719** The original WHERE clause in pExpr is unaltered.  All this routine
100720** does is make slot[] entries point to substructure within pExpr.
100721**
100722** In the previous sentence and in the diagram, "slot[]" refers to
100723** the WhereClause.a[] array.  The slot[] array grows as needed to contain
100724** all terms of the WHERE clause.
100725*/
100726static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
100727  pWC->op = (u8)op;
100728  if( pExpr==0 ) return;
100729  if( pExpr->op!=op ){
100730    whereClauseInsert(pWC, pExpr, 0);
100731  }else{
100732    whereSplit(pWC, pExpr->pLeft, op);
100733    whereSplit(pWC, pExpr->pRight, op);
100734  }
100735}
100736
100737/*
100738** Initialize an expression mask set (a WhereMaskSet object)
100739*/
100740#define initMaskSet(P)  memset(P, 0, sizeof(*P))
100741
100742/*
100743** Return the bitmask for the given cursor number.  Return 0 if
100744** iCursor is not in the set.
100745*/
100746static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
100747  int i;
100748  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
100749  for(i=0; i<pMaskSet->n; i++){
100750    if( pMaskSet->ix[i]==iCursor ){
100751      return ((Bitmask)1)<<i;
100752    }
100753  }
100754  return 0;
100755}
100756
100757/*
100758** Create a new mask for cursor iCursor.
100759**
100760** There is one cursor per table in the FROM clause.  The number of
100761** tables in the FROM clause is limited by a test early in the
100762** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
100763** array will never overflow.
100764*/
100765static void createMask(WhereMaskSet *pMaskSet, int iCursor){
100766  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
100767  pMaskSet->ix[pMaskSet->n++] = iCursor;
100768}
100769
100770/*
100771** This routine walks (recursively) an expression tree and generates
100772** a bitmask indicating which tables are used in that expression
100773** tree.
100774**
100775** In order for this routine to work, the calling function must have
100776** previously invoked sqlite3ResolveExprNames() on the expression.  See
100777** the header comment on that routine for additional information.
100778** The sqlite3ResolveExprNames() routines looks for column names and
100779** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
100780** the VDBE cursor number of the table.  This routine just has to
100781** translate the cursor numbers into bitmask values and OR all
100782** the bitmasks together.
100783*/
100784static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
100785static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
100786static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
100787  Bitmask mask = 0;
100788  if( p==0 ) return 0;
100789  if( p->op==TK_COLUMN ){
100790    mask = getMask(pMaskSet, p->iTable);
100791    return mask;
100792  }
100793  mask = exprTableUsage(pMaskSet, p->pRight);
100794  mask |= exprTableUsage(pMaskSet, p->pLeft);
100795  if( ExprHasProperty(p, EP_xIsSelect) ){
100796    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
100797  }else{
100798    mask |= exprListTableUsage(pMaskSet, p->x.pList);
100799  }
100800  return mask;
100801}
100802static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
100803  int i;
100804  Bitmask mask = 0;
100805  if( pList ){
100806    for(i=0; i<pList->nExpr; i++){
100807      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
100808    }
100809  }
100810  return mask;
100811}
100812static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
100813  Bitmask mask = 0;
100814  while( pS ){
100815    SrcList *pSrc = pS->pSrc;
100816    mask |= exprListTableUsage(pMaskSet, pS->pEList);
100817    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
100818    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
100819    mask |= exprTableUsage(pMaskSet, pS->pWhere);
100820    mask |= exprTableUsage(pMaskSet, pS->pHaving);
100821    if( ALWAYS(pSrc!=0) ){
100822      int i;
100823      for(i=0; i<pSrc->nSrc; i++){
100824        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
100825        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
100826      }
100827    }
100828    pS = pS->pPrior;
100829  }
100830  return mask;
100831}
100832
100833/*
100834** Return TRUE if the given operator is one of the operators that is
100835** allowed for an indexable WHERE clause term.  The allowed operators are
100836** "=", "<", ">", "<=", ">=", and "IN".
100837**
100838** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
100839** of one of the following forms: column = expression column > expression
100840** column >= expression column < expression column <= expression
100841** expression = column expression > column expression >= column
100842** expression < column expression <= column column IN
100843** (expression-list) column IN (subquery) column IS NULL
100844*/
100845static int allowedOp(int op){
100846  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
100847  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
100848  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
100849  assert( TK_GE==TK_EQ+4 );
100850  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
100851}
100852
100853/*
100854** Swap two objects of type TYPE.
100855*/
100856#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
100857
100858/*
100859** Commute a comparison operator.  Expressions of the form "X op Y"
100860** are converted into "Y op X".
100861**
100862** If a collation sequence is associated with either the left or right
100863** side of the comparison, it remains associated with the same side after
100864** the commutation. So "Y collate NOCASE op X" becomes
100865** "X collate NOCASE op Y". This is because any collation sequence on
100866** the left hand side of a comparison overrides any collation sequence
100867** attached to the right. For the same reason the EP_ExpCollate flag
100868** is not commuted.
100869*/
100870static void exprCommute(Parse *pParse, Expr *pExpr){
100871  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
100872  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
100873  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
100874  pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
100875  pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
100876  SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
100877  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
100878  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
100879  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
100880  if( pExpr->op>=TK_GT ){
100881    assert( TK_LT==TK_GT+2 );
100882    assert( TK_GE==TK_LE+2 );
100883    assert( TK_GT>TK_EQ );
100884    assert( TK_GT<TK_LE );
100885    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
100886    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
100887  }
100888}
100889
100890/*
100891** Translate from TK_xx operator to WO_xx bitmask.
100892*/
100893static u16 operatorMask(int op){
100894  u16 c;
100895  assert( allowedOp(op) );
100896  if( op==TK_IN ){
100897    c = WO_IN;
100898  }else if( op==TK_ISNULL ){
100899    c = WO_ISNULL;
100900  }else{
100901    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
100902    c = (u16)(WO_EQ<<(op-TK_EQ));
100903  }
100904  assert( op!=TK_ISNULL || c==WO_ISNULL );
100905  assert( op!=TK_IN || c==WO_IN );
100906  assert( op!=TK_EQ || c==WO_EQ );
100907  assert( op!=TK_LT || c==WO_LT );
100908  assert( op!=TK_LE || c==WO_LE );
100909  assert( op!=TK_GT || c==WO_GT );
100910  assert( op!=TK_GE || c==WO_GE );
100911  return c;
100912}
100913
100914/*
100915** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
100916** where X is a reference to the iColumn of table iCur and <op> is one of
100917** the WO_xx operator codes specified by the op parameter.
100918** Return a pointer to the term.  Return 0 if not found.
100919*/
100920static WhereTerm *findTerm(
100921  WhereClause *pWC,     /* The WHERE clause to be searched */
100922  int iCur,             /* Cursor number of LHS */
100923  int iColumn,          /* Column number of LHS */
100924  Bitmask notReady,     /* RHS must not overlap with this mask */
100925  u32 op,               /* Mask of WO_xx values describing operator */
100926  Index *pIdx           /* Must be compatible with this index, if not NULL */
100927){
100928  WhereTerm *pTerm;
100929  int k;
100930  assert( iCur>=0 );
100931  op &= WO_ALL;
100932  for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
100933    if( pTerm->leftCursor==iCur
100934       && (pTerm->prereqRight & notReady)==0
100935       && pTerm->u.leftColumn==iColumn
100936       && (pTerm->eOperator & op)!=0
100937    ){
100938      if( pIdx && pTerm->eOperator!=WO_ISNULL ){
100939        Expr *pX = pTerm->pExpr;
100940        CollSeq *pColl;
100941        char idxaff;
100942        int j;
100943        Parse *pParse = pWC->pParse;
100944
100945        idxaff = pIdx->pTable->aCol[iColumn].affinity;
100946        if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
100947
100948        /* Figure out the collation sequence required from an index for
100949        ** it to be useful for optimising expression pX. Store this
100950        ** value in variable pColl.
100951        */
100952        assert(pX->pLeft);
100953        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
100954        assert(pColl || pParse->nErr);
100955
100956        for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
100957          if( NEVER(j>=pIdx->nColumn) ) return 0;
100958        }
100959        if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
100960      }
100961      return pTerm;
100962    }
100963  }
100964  return 0;
100965}
100966
100967/* Forward reference */
100968static void exprAnalyze(SrcList*, WhereClause*, int);
100969
100970/*
100971** Call exprAnalyze on all terms in a WHERE clause.
100972**
100973**
100974*/
100975static void exprAnalyzeAll(
100976  SrcList *pTabList,       /* the FROM clause */
100977  WhereClause *pWC         /* the WHERE clause to be analyzed */
100978){
100979  int i;
100980  for(i=pWC->nTerm-1; i>=0; i--){
100981    exprAnalyze(pTabList, pWC, i);
100982  }
100983}
100984
100985#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
100986/*
100987** Check to see if the given expression is a LIKE or GLOB operator that
100988** can be optimized using inequality constraints.  Return TRUE if it is
100989** so and false if not.
100990**
100991** In order for the operator to be optimizible, the RHS must be a string
100992** literal that does not begin with a wildcard.
100993*/
100994static int isLikeOrGlob(
100995  Parse *pParse,    /* Parsing and code generating context */
100996  Expr *pExpr,      /* Test this expression */
100997  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
100998  int *pisComplete, /* True if the only wildcard is % in the last character */
100999  int *pnoCase      /* True if uppercase is equivalent to lowercase */
101000){
101001  const char *z = 0;         /* String on RHS of LIKE operator */
101002  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
101003  ExprList *pList;           /* List of operands to the LIKE operator */
101004  int c;                     /* One character in z[] */
101005  int cnt;                   /* Number of non-wildcard prefix characters */
101006  char wc[3];                /* Wildcard characters */
101007  sqlite3 *db = pParse->db;  /* Database connection */
101008  sqlite3_value *pVal = 0;
101009  int op;                    /* Opcode of pRight */
101010
101011  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
101012    return 0;
101013  }
101014#ifdef SQLITE_EBCDIC
101015  if( *pnoCase ) return 0;
101016#endif
101017  pList = pExpr->x.pList;
101018  pLeft = pList->a[1].pExpr;
101019  if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
101020    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
101021    ** be the name of an indexed column with TEXT affinity. */
101022    return 0;
101023  }
101024  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
101025
101026  pRight = pList->a[0].pExpr;
101027  op = pRight->op;
101028  if( op==TK_REGISTER ){
101029    op = pRight->op2;
101030  }
101031  if( op==TK_VARIABLE ){
101032    Vdbe *pReprepare = pParse->pReprepare;
101033    int iCol = pRight->iColumn;
101034    pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
101035    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
101036      z = (char *)sqlite3_value_text(pVal);
101037    }
101038    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
101039    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
101040  }else if( op==TK_STRING ){
101041    z = pRight->u.zToken;
101042  }
101043  if( z ){
101044    cnt = 0;
101045    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
101046      cnt++;
101047    }
101048    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
101049      Expr *pPrefix;
101050      *pisComplete = c==wc[0] && z[cnt+1]==0;
101051      pPrefix = sqlite3Expr(db, TK_STRING, z);
101052      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
101053      *ppPrefix = pPrefix;
101054      if( op==TK_VARIABLE ){
101055        Vdbe *v = pParse->pVdbe;
101056        sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
101057        if( *pisComplete && pRight->u.zToken[1] ){
101058          /* If the rhs of the LIKE expression is a variable, and the current
101059          ** value of the variable means there is no need to invoke the LIKE
101060          ** function, then no OP_Variable will be added to the program.
101061          ** This causes problems for the sqlite3_bind_parameter_name()
101062          ** API. To workaround them, add a dummy OP_Variable here.
101063          */
101064          int r1 = sqlite3GetTempReg(pParse);
101065          sqlite3ExprCodeTarget(pParse, pRight, r1);
101066          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
101067          sqlite3ReleaseTempReg(pParse, r1);
101068        }
101069      }
101070    }else{
101071      z = 0;
101072    }
101073  }
101074
101075  sqlite3ValueFree(pVal);
101076  return (z!=0);
101077}
101078#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
101079
101080
101081#ifndef SQLITE_OMIT_VIRTUALTABLE
101082/*
101083** Check to see if the given expression is of the form
101084**
101085**         column MATCH expr
101086**
101087** If it is then return TRUE.  If not, return FALSE.
101088*/
101089static int isMatchOfColumn(
101090  Expr *pExpr      /* Test this expression */
101091){
101092  ExprList *pList;
101093
101094  if( pExpr->op!=TK_FUNCTION ){
101095    return 0;
101096  }
101097  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
101098    return 0;
101099  }
101100  pList = pExpr->x.pList;
101101  if( pList->nExpr!=2 ){
101102    return 0;
101103  }
101104  if( pList->a[1].pExpr->op != TK_COLUMN ){
101105    return 0;
101106  }
101107  return 1;
101108}
101109#endif /* SQLITE_OMIT_VIRTUALTABLE */
101110
101111/*
101112** If the pBase expression originated in the ON or USING clause of
101113** a join, then transfer the appropriate markings over to derived.
101114*/
101115static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
101116  pDerived->flags |= pBase->flags & EP_FromJoin;
101117  pDerived->iRightJoinTable = pBase->iRightJoinTable;
101118}
101119
101120#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
101121/*
101122** Analyze a term that consists of two or more OR-connected
101123** subterms.  So in:
101124**
101125**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
101126**                          ^^^^^^^^^^^^^^^^^^^^
101127**
101128** This routine analyzes terms such as the middle term in the above example.
101129** A WhereOrTerm object is computed and attached to the term under
101130** analysis, regardless of the outcome of the analysis.  Hence:
101131**
101132**     WhereTerm.wtFlags   |=  TERM_ORINFO
101133**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
101134**
101135** The term being analyzed must have two or more of OR-connected subterms.
101136** A single subterm might be a set of AND-connected sub-subterms.
101137** Examples of terms under analysis:
101138**
101139**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
101140**     (B)     x=expr1 OR expr2=x OR x=expr3
101141**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
101142**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
101143**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
101144**
101145** CASE 1:
101146**
101147** If all subterms are of the form T.C=expr for some single column of C
101148** a single table T (as shown in example B above) then create a new virtual
101149** term that is an equivalent IN expression.  In other words, if the term
101150** being analyzed is:
101151**
101152**      x = expr1  OR  expr2 = x  OR  x = expr3
101153**
101154** then create a new virtual term like this:
101155**
101156**      x IN (expr1,expr2,expr3)
101157**
101158** CASE 2:
101159**
101160** If all subterms are indexable by a single table T, then set
101161**
101162**     WhereTerm.eOperator              =  WO_OR
101163**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
101164**
101165** A subterm is "indexable" if it is of the form
101166** "T.C <op> <expr>" where C is any column of table T and
101167** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
101168** A subterm is also indexable if it is an AND of two or more
101169** subsubterms at least one of which is indexable.  Indexable AND
101170** subterms have their eOperator set to WO_AND and they have
101171** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
101172**
101173** From another point of view, "indexable" means that the subterm could
101174** potentially be used with an index if an appropriate index exists.
101175** This analysis does not consider whether or not the index exists; that
101176** is something the bestIndex() routine will determine.  This analysis
101177** only looks at whether subterms appropriate for indexing exist.
101178**
101179** All examples A through E above all satisfy case 2.  But if a term
101180** also statisfies case 1 (such as B) we know that the optimizer will
101181** always prefer case 1, so in that case we pretend that case 2 is not
101182** satisfied.
101183**
101184** It might be the case that multiple tables are indexable.  For example,
101185** (E) above is indexable on tables P, Q, and R.
101186**
101187** Terms that satisfy case 2 are candidates for lookup by using
101188** separate indices to find rowids for each subterm and composing
101189** the union of all rowids using a RowSet object.  This is similar
101190** to "bitmap indices" in other database engines.
101191**
101192** OTHERWISE:
101193**
101194** If neither case 1 nor case 2 apply, then leave the eOperator set to
101195** zero.  This term is not useful for search.
101196*/
101197static void exprAnalyzeOrTerm(
101198  SrcList *pSrc,            /* the FROM clause */
101199  WhereClause *pWC,         /* the complete WHERE clause */
101200  int idxTerm               /* Index of the OR-term to be analyzed */
101201){
101202  Parse *pParse = pWC->pParse;            /* Parser context */
101203  sqlite3 *db = pParse->db;               /* Database connection */
101204  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
101205  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
101206  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
101207  int i;                                  /* Loop counters */
101208  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
101209  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
101210  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
101211  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
101212  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
101213
101214  /*
101215  ** Break the OR clause into its separate subterms.  The subterms are
101216  ** stored in a WhereClause structure containing within the WhereOrInfo
101217  ** object that is attached to the original OR clause term.
101218  */
101219  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
101220  assert( pExpr->op==TK_OR );
101221  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
101222  if( pOrInfo==0 ) return;
101223  pTerm->wtFlags |= TERM_ORINFO;
101224  pOrWc = &pOrInfo->wc;
101225  whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
101226  whereSplit(pOrWc, pExpr, TK_OR);
101227  exprAnalyzeAll(pSrc, pOrWc);
101228  if( db->mallocFailed ) return;
101229  assert( pOrWc->nTerm>=2 );
101230
101231  /*
101232  ** Compute the set of tables that might satisfy cases 1 or 2.
101233  */
101234  indexable = ~(Bitmask)0;
101235  chngToIN = ~(pWC->vmask);
101236  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
101237    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
101238      WhereAndInfo *pAndInfo;
101239      assert( pOrTerm->eOperator==0 );
101240      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
101241      chngToIN = 0;
101242      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
101243      if( pAndInfo ){
101244        WhereClause *pAndWC;
101245        WhereTerm *pAndTerm;
101246        int j;
101247        Bitmask b = 0;
101248        pOrTerm->u.pAndInfo = pAndInfo;
101249        pOrTerm->wtFlags |= TERM_ANDINFO;
101250        pOrTerm->eOperator = WO_AND;
101251        pAndWC = &pAndInfo->wc;
101252        whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
101253        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
101254        exprAnalyzeAll(pSrc, pAndWC);
101255        testcase( db->mallocFailed );
101256        if( !db->mallocFailed ){
101257          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
101258            assert( pAndTerm->pExpr );
101259            if( allowedOp(pAndTerm->pExpr->op) ){
101260              b |= getMask(pMaskSet, pAndTerm->leftCursor);
101261            }
101262          }
101263        }
101264        indexable &= b;
101265      }
101266    }else if( pOrTerm->wtFlags & TERM_COPIED ){
101267      /* Skip this term for now.  We revisit it when we process the
101268      ** corresponding TERM_VIRTUAL term */
101269    }else{
101270      Bitmask b;
101271      b = getMask(pMaskSet, pOrTerm->leftCursor);
101272      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
101273        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
101274        b |= getMask(pMaskSet, pOther->leftCursor);
101275      }
101276      indexable &= b;
101277      if( pOrTerm->eOperator!=WO_EQ ){
101278        chngToIN = 0;
101279      }else{
101280        chngToIN &= b;
101281      }
101282    }
101283  }
101284
101285  /*
101286  ** Record the set of tables that satisfy case 2.  The set might be
101287  ** empty.
101288  */
101289  pOrInfo->indexable = indexable;
101290  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
101291
101292  /*
101293  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
101294  ** we have to do some additional checking to see if case 1 really
101295  ** is satisfied.
101296  **
101297  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
101298  ** that there is no possibility of transforming the OR clause into an
101299  ** IN operator because one or more terms in the OR clause contain
101300  ** something other than == on a column in the single table.  The 1-bit
101301  ** case means that every term of the OR clause is of the form
101302  ** "table.column=expr" for some single table.  The one bit that is set
101303  ** will correspond to the common table.  We still need to check to make
101304  ** sure the same column is used on all terms.  The 2-bit case is when
101305  ** the all terms are of the form "table1.column=table2.column".  It
101306  ** might be possible to form an IN operator with either table1.column
101307  ** or table2.column as the LHS if either is common to every term of
101308  ** the OR clause.
101309  **
101310  ** Note that terms of the form "table.column1=table.column2" (the
101311  ** same table on both sizes of the ==) cannot be optimized.
101312  */
101313  if( chngToIN ){
101314    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
101315    int iColumn = -1;         /* Column index on lhs of IN operator */
101316    int iCursor = -1;         /* Table cursor common to all terms */
101317    int j = 0;                /* Loop counter */
101318
101319    /* Search for a table and column that appears on one side or the
101320    ** other of the == operator in every subterm.  That table and column
101321    ** will be recorded in iCursor and iColumn.  There might not be any
101322    ** such table and column.  Set okToChngToIN if an appropriate table
101323    ** and column is found but leave okToChngToIN false if not found.
101324    */
101325    for(j=0; j<2 && !okToChngToIN; j++){
101326      pOrTerm = pOrWc->a;
101327      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
101328        assert( pOrTerm->eOperator==WO_EQ );
101329        pOrTerm->wtFlags &= ~TERM_OR_OK;
101330        if( pOrTerm->leftCursor==iCursor ){
101331          /* This is the 2-bit case and we are on the second iteration and
101332          ** current term is from the first iteration.  So skip this term. */
101333          assert( j==1 );
101334          continue;
101335        }
101336        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
101337          /* This term must be of the form t1.a==t2.b where t2 is in the
101338          ** chngToIN set but t1 is not.  This term will be either preceeded
101339          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
101340          ** and use its inversion. */
101341          testcase( pOrTerm->wtFlags & TERM_COPIED );
101342          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
101343          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
101344          continue;
101345        }
101346        iColumn = pOrTerm->u.leftColumn;
101347        iCursor = pOrTerm->leftCursor;
101348        break;
101349      }
101350      if( i<0 ){
101351        /* No candidate table+column was found.  This can only occur
101352        ** on the second iteration */
101353        assert( j==1 );
101354        assert( (chngToIN&(chngToIN-1))==0 );
101355        assert( chngToIN==getMask(pMaskSet, iCursor) );
101356        break;
101357      }
101358      testcase( j==1 );
101359
101360      /* We have found a candidate table and column.  Check to see if that
101361      ** table and column is common to every term in the OR clause */
101362      okToChngToIN = 1;
101363      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
101364        assert( pOrTerm->eOperator==WO_EQ );
101365        if( pOrTerm->leftCursor!=iCursor ){
101366          pOrTerm->wtFlags &= ~TERM_OR_OK;
101367        }else if( pOrTerm->u.leftColumn!=iColumn ){
101368          okToChngToIN = 0;
101369        }else{
101370          int affLeft, affRight;
101371          /* If the right-hand side is also a column, then the affinities
101372          ** of both right and left sides must be such that no type
101373          ** conversions are required on the right.  (Ticket #2249)
101374          */
101375          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
101376          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
101377          if( affRight!=0 && affRight!=affLeft ){
101378            okToChngToIN = 0;
101379          }else{
101380            pOrTerm->wtFlags |= TERM_OR_OK;
101381          }
101382        }
101383      }
101384    }
101385
101386    /* At this point, okToChngToIN is true if original pTerm satisfies
101387    ** case 1.  In that case, construct a new virtual term that is
101388    ** pTerm converted into an IN operator.
101389    **
101390    ** EV: R-00211-15100
101391    */
101392    if( okToChngToIN ){
101393      Expr *pDup;            /* A transient duplicate expression */
101394      ExprList *pList = 0;   /* The RHS of the IN operator */
101395      Expr *pLeft = 0;       /* The LHS of the IN operator */
101396      Expr *pNew;            /* The complete IN operator */
101397
101398      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
101399        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
101400        assert( pOrTerm->eOperator==WO_EQ );
101401        assert( pOrTerm->leftCursor==iCursor );
101402        assert( pOrTerm->u.leftColumn==iColumn );
101403        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
101404        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
101405        pLeft = pOrTerm->pExpr->pLeft;
101406      }
101407      assert( pLeft!=0 );
101408      pDup = sqlite3ExprDup(db, pLeft, 0);
101409      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
101410      if( pNew ){
101411        int idxNew;
101412        transferJoinMarkings(pNew, pExpr);
101413        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
101414        pNew->x.pList = pList;
101415        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
101416        testcase( idxNew==0 );
101417        exprAnalyze(pSrc, pWC, idxNew);
101418        pTerm = &pWC->a[idxTerm];
101419        pWC->a[idxNew].iParent = idxTerm;
101420        pTerm->nChild = 1;
101421      }else{
101422        sqlite3ExprListDelete(db, pList);
101423      }
101424      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
101425    }
101426  }
101427}
101428#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
101429
101430
101431/*
101432** The input to this routine is an WhereTerm structure with only the
101433** "pExpr" field filled in.  The job of this routine is to analyze the
101434** subexpression and populate all the other fields of the WhereTerm
101435** structure.
101436**
101437** If the expression is of the form "<expr> <op> X" it gets commuted
101438** to the standard form of "X <op> <expr>".
101439**
101440** If the expression is of the form "X <op> Y" where both X and Y are
101441** columns, then the original expression is unchanged and a new virtual
101442** term of the form "Y <op> X" is added to the WHERE clause and
101443** analyzed separately.  The original term is marked with TERM_COPIED
101444** and the new term is marked with TERM_DYNAMIC (because it's pExpr
101445** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
101446** is a commuted copy of a prior term.)  The original term has nChild=1
101447** and the copy has idxParent set to the index of the original term.
101448*/
101449static void exprAnalyze(
101450  SrcList *pSrc,            /* the FROM clause */
101451  WhereClause *pWC,         /* the WHERE clause */
101452  int idxTerm               /* Index of the term to be analyzed */
101453){
101454  WhereTerm *pTerm;                /* The term to be analyzed */
101455  WhereMaskSet *pMaskSet;          /* Set of table index masks */
101456  Expr *pExpr;                     /* The expression to be analyzed */
101457  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
101458  Bitmask prereqAll;               /* Prerequesites of pExpr */
101459  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
101460  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
101461  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
101462  int noCase = 0;                  /* LIKE/GLOB distinguishes case */
101463  int op;                          /* Top-level operator.  pExpr->op */
101464  Parse *pParse = pWC->pParse;     /* Parsing context */
101465  sqlite3 *db = pParse->db;        /* Database connection */
101466
101467  if( db->mallocFailed ){
101468    return;
101469  }
101470  pTerm = &pWC->a[idxTerm];
101471  pMaskSet = pWC->pMaskSet;
101472  pExpr = pTerm->pExpr;
101473  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
101474  op = pExpr->op;
101475  if( op==TK_IN ){
101476    assert( pExpr->pRight==0 );
101477    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
101478      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
101479    }else{
101480      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
101481    }
101482  }else if( op==TK_ISNULL ){
101483    pTerm->prereqRight = 0;
101484  }else{
101485    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
101486  }
101487  prereqAll = exprTableUsage(pMaskSet, pExpr);
101488  if( ExprHasProperty(pExpr, EP_FromJoin) ){
101489    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
101490    prereqAll |= x;
101491    extraRight = x-1;  /* ON clause terms may not be used with an index
101492                       ** on left table of a LEFT JOIN.  Ticket #3015 */
101493  }
101494  pTerm->prereqAll = prereqAll;
101495  pTerm->leftCursor = -1;
101496  pTerm->iParent = -1;
101497  pTerm->eOperator = 0;
101498  if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
101499    Expr *pLeft = pExpr->pLeft;
101500    Expr *pRight = pExpr->pRight;
101501    if( pLeft->op==TK_COLUMN ){
101502      pTerm->leftCursor = pLeft->iTable;
101503      pTerm->u.leftColumn = pLeft->iColumn;
101504      pTerm->eOperator = operatorMask(op);
101505    }
101506    if( pRight && pRight->op==TK_COLUMN ){
101507      WhereTerm *pNew;
101508      Expr *pDup;
101509      if( pTerm->leftCursor>=0 ){
101510        int idxNew;
101511        pDup = sqlite3ExprDup(db, pExpr, 0);
101512        if( db->mallocFailed ){
101513          sqlite3ExprDelete(db, pDup);
101514          return;
101515        }
101516        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
101517        if( idxNew==0 ) return;
101518        pNew = &pWC->a[idxNew];
101519        pNew->iParent = idxTerm;
101520        pTerm = &pWC->a[idxTerm];
101521        pTerm->nChild = 1;
101522        pTerm->wtFlags |= TERM_COPIED;
101523      }else{
101524        pDup = pExpr;
101525        pNew = pTerm;
101526      }
101527      exprCommute(pParse, pDup);
101528      pLeft = pDup->pLeft;
101529      pNew->leftCursor = pLeft->iTable;
101530      pNew->u.leftColumn = pLeft->iColumn;
101531      testcase( (prereqLeft | extraRight) != prereqLeft );
101532      pNew->prereqRight = prereqLeft | extraRight;
101533      pNew->prereqAll = prereqAll;
101534      pNew->eOperator = operatorMask(pDup->op);
101535    }
101536  }
101537
101538#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
101539  /* If a term is the BETWEEN operator, create two new virtual terms
101540  ** that define the range that the BETWEEN implements.  For example:
101541  **
101542  **      a BETWEEN b AND c
101543  **
101544  ** is converted into:
101545  **
101546  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
101547  **
101548  ** The two new terms are added onto the end of the WhereClause object.
101549  ** The new terms are "dynamic" and are children of the original BETWEEN
101550  ** term.  That means that if the BETWEEN term is coded, the children are
101551  ** skipped.  Or, if the children are satisfied by an index, the original
101552  ** BETWEEN term is skipped.
101553  */
101554  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
101555    ExprList *pList = pExpr->x.pList;
101556    int i;
101557    static const u8 ops[] = {TK_GE, TK_LE};
101558    assert( pList!=0 );
101559    assert( pList->nExpr==2 );
101560    for(i=0; i<2; i++){
101561      Expr *pNewExpr;
101562      int idxNew;
101563      pNewExpr = sqlite3PExpr(pParse, ops[i],
101564                             sqlite3ExprDup(db, pExpr->pLeft, 0),
101565                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
101566      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
101567      testcase( idxNew==0 );
101568      exprAnalyze(pSrc, pWC, idxNew);
101569      pTerm = &pWC->a[idxTerm];
101570      pWC->a[idxNew].iParent = idxTerm;
101571    }
101572    pTerm->nChild = 2;
101573  }
101574#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
101575
101576#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
101577  /* Analyze a term that is composed of two or more subterms connected by
101578  ** an OR operator.
101579  */
101580  else if( pExpr->op==TK_OR ){
101581    assert( pWC->op==TK_AND );
101582    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
101583    pTerm = &pWC->a[idxTerm];
101584  }
101585#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
101586
101587#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
101588  /* Add constraints to reduce the search space on a LIKE or GLOB
101589  ** operator.
101590  **
101591  ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
101592  **
101593  **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
101594  **
101595  ** The last character of the prefix "abc" is incremented to form the
101596  ** termination condition "abd".
101597  */
101598  if( pWC->op==TK_AND
101599   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
101600  ){
101601    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
101602    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
101603    Expr *pNewExpr1;
101604    Expr *pNewExpr2;
101605    int idxNew1;
101606    int idxNew2;
101607    CollSeq *pColl;    /* Collating sequence to use */
101608
101609    pLeft = pExpr->x.pList->a[1].pExpr;
101610    pStr2 = sqlite3ExprDup(db, pStr1, 0);
101611    if( !db->mallocFailed ){
101612      u8 c, *pC;       /* Last character before the first wildcard */
101613      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
101614      c = *pC;
101615      if( noCase ){
101616        /* The point is to increment the last character before the first
101617        ** wildcard.  But if we increment '@', that will push it into the
101618        ** alphabetic range where case conversions will mess up the
101619        ** inequality.  To avoid this, make sure to also run the full
101620        ** LIKE on all candidate expressions by clearing the isComplete flag
101621        */
101622        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
101623
101624
101625        c = sqlite3UpperToLower[c];
101626      }
101627      *pC = c + 1;
101628    }
101629    pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
101630    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
101631                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
101632                     pStr1, 0);
101633    idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
101634    testcase( idxNew1==0 );
101635    exprAnalyze(pSrc, pWC, idxNew1);
101636    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
101637                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
101638                     pStr2, 0);
101639    idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
101640    testcase( idxNew2==0 );
101641    exprAnalyze(pSrc, pWC, idxNew2);
101642    pTerm = &pWC->a[idxTerm];
101643    if( isComplete ){
101644      pWC->a[idxNew1].iParent = idxTerm;
101645      pWC->a[idxNew2].iParent = idxTerm;
101646      pTerm->nChild = 2;
101647    }
101648  }
101649#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
101650
101651#ifndef SQLITE_OMIT_VIRTUALTABLE
101652  /* Add a WO_MATCH auxiliary term to the constraint set if the
101653  ** current expression is of the form:  column MATCH expr.
101654  ** This information is used by the xBestIndex methods of
101655  ** virtual tables.  The native query optimizer does not attempt
101656  ** to do anything with MATCH functions.
101657  */
101658  if( isMatchOfColumn(pExpr) ){
101659    int idxNew;
101660    Expr *pRight, *pLeft;
101661    WhereTerm *pNewTerm;
101662    Bitmask prereqColumn, prereqExpr;
101663
101664    pRight = pExpr->x.pList->a[0].pExpr;
101665    pLeft = pExpr->x.pList->a[1].pExpr;
101666    prereqExpr = exprTableUsage(pMaskSet, pRight);
101667    prereqColumn = exprTableUsage(pMaskSet, pLeft);
101668    if( (prereqExpr & prereqColumn)==0 ){
101669      Expr *pNewExpr;
101670      pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
101671                              0, sqlite3ExprDup(db, pRight, 0), 0);
101672      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
101673      testcase( idxNew==0 );
101674      pNewTerm = &pWC->a[idxNew];
101675      pNewTerm->prereqRight = prereqExpr;
101676      pNewTerm->leftCursor = pLeft->iTable;
101677      pNewTerm->u.leftColumn = pLeft->iColumn;
101678      pNewTerm->eOperator = WO_MATCH;
101679      pNewTerm->iParent = idxTerm;
101680      pTerm = &pWC->a[idxTerm];
101681      pTerm->nChild = 1;
101682      pTerm->wtFlags |= TERM_COPIED;
101683      pNewTerm->prereqAll = pTerm->prereqAll;
101684    }
101685  }
101686#endif /* SQLITE_OMIT_VIRTUALTABLE */
101687
101688#ifdef SQLITE_ENABLE_STAT2
101689  /* When sqlite_stat2 histogram data is available an operator of the
101690  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101691  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
101692  ** virtual term of that form.
101693  **
101694  ** Note that the virtual term must be tagged with TERM_VNULL.  This
101695  ** TERM_VNULL tag will suppress the not-null check at the beginning
101696  ** of the loop.  Without the TERM_VNULL flag, the not-null check at
101697  ** the start of the loop will prevent any results from being returned.
101698  */
101699  if( pExpr->op==TK_NOTNULL
101700   && pExpr->pLeft->op==TK_COLUMN
101701   && pExpr->pLeft->iColumn>=0
101702  ){
101703    Expr *pNewExpr;
101704    Expr *pLeft = pExpr->pLeft;
101705    int idxNew;
101706    WhereTerm *pNewTerm;
101707
101708    pNewExpr = sqlite3PExpr(pParse, TK_GT,
101709                            sqlite3ExprDup(db, pLeft, 0),
101710                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
101711
101712    idxNew = whereClauseInsert(pWC, pNewExpr,
101713                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
101714    if( idxNew ){
101715      pNewTerm = &pWC->a[idxNew];
101716      pNewTerm->prereqRight = 0;
101717      pNewTerm->leftCursor = pLeft->iTable;
101718      pNewTerm->u.leftColumn = pLeft->iColumn;
101719      pNewTerm->eOperator = WO_GT;
101720      pNewTerm->iParent = idxTerm;
101721      pTerm = &pWC->a[idxTerm];
101722      pTerm->nChild = 1;
101723      pTerm->wtFlags |= TERM_COPIED;
101724      pNewTerm->prereqAll = pTerm->prereqAll;
101725    }
101726  }
101727#endif /* SQLITE_ENABLE_STAT2 */
101728
101729  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
101730  ** an index for tables to the left of the join.
101731  */
101732  pTerm->prereqRight |= extraRight;
101733}
101734
101735/*
101736** Return TRUE if any of the expressions in pList->a[iFirst...] contain
101737** a reference to any table other than the iBase table.
101738*/
101739static int referencesOtherTables(
101740  ExprList *pList,          /* Search expressions in ths list */
101741  WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
101742  int iFirst,               /* Be searching with the iFirst-th expression */
101743  int iBase                 /* Ignore references to this table */
101744){
101745  Bitmask allowed = ~getMask(pMaskSet, iBase);
101746  while( iFirst<pList->nExpr ){
101747    if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
101748      return 1;
101749    }
101750  }
101751  return 0;
101752}
101753
101754/*
101755** This function searches the expression list passed as the second argument
101756** for an expression of type TK_COLUMN that refers to the same column and
101757** uses the same collation sequence as the iCol'th column of index pIdx.
101758** Argument iBase is the cursor number used for the table that pIdx refers
101759** to.
101760**
101761** If such an expression is found, its index in pList->a[] is returned. If
101762** no expression is found, -1 is returned.
101763*/
101764static int findIndexCol(
101765  Parse *pParse,                  /* Parse context */
101766  ExprList *pList,                /* Expression list to search */
101767  int iBase,                      /* Cursor for table associated with pIdx */
101768  Index *pIdx,                    /* Index to match column of */
101769  int iCol                        /* Column of index to match */
101770){
101771  int i;
101772  const char *zColl = pIdx->azColl[iCol];
101773
101774  for(i=0; i<pList->nExpr; i++){
101775    Expr *p = pList->a[i].pExpr;
101776    if( p->op==TK_COLUMN
101777     && p->iColumn==pIdx->aiColumn[iCol]
101778     && p->iTable==iBase
101779    ){
101780      CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
101781      if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
101782        return i;
101783      }
101784    }
101785  }
101786
101787  return -1;
101788}
101789
101790/*
101791** This routine determines if pIdx can be used to assist in processing a
101792** DISTINCT qualifier. In other words, it tests whether or not using this
101793** index for the outer loop guarantees that rows with equal values for
101794** all expressions in the pDistinct list are delivered grouped together.
101795**
101796** For example, the query
101797**
101798**   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
101799**
101800** can benefit from any index on columns "b" and "c".
101801*/
101802static int isDistinctIndex(
101803  Parse *pParse,                  /* Parsing context */
101804  WhereClause *pWC,               /* The WHERE clause */
101805  Index *pIdx,                    /* The index being considered */
101806  int base,                       /* Cursor number for the table pIdx is on */
101807  ExprList *pDistinct,            /* The DISTINCT expressions */
101808  int nEqCol                      /* Number of index columns with == */
101809){
101810  Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
101811  int i;                          /* Iterator variable */
101812
101813  if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
101814  testcase( pDistinct->nExpr==BMS-1 );
101815
101816  /* Loop through all the expressions in the distinct list. If any of them
101817  ** are not simple column references, return early. Otherwise, test if the
101818  ** WHERE clause contains a "col=X" clause. If it does, the expression
101819  ** can be ignored. If it does not, and the column does not belong to the
101820  ** same table as index pIdx, return early. Finally, if there is no
101821  ** matching "col=X" expression and the column is on the same table as pIdx,
101822  ** set the corresponding bit in variable mask.
101823  */
101824  for(i=0; i<pDistinct->nExpr; i++){
101825    WhereTerm *pTerm;
101826    Expr *p = pDistinct->a[i].pExpr;
101827    if( p->op!=TK_COLUMN ) return 0;
101828    pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
101829    if( pTerm ){
101830      Expr *pX = pTerm->pExpr;
101831      CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
101832      CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
101833      if( p1==p2 ) continue;
101834    }
101835    if( p->iTable!=base ) return 0;
101836    mask |= (((Bitmask)1) << i);
101837  }
101838
101839  for(i=nEqCol; mask && i<pIdx->nColumn; i++){
101840    int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
101841    if( iExpr<0 ) break;
101842    mask &= ~(((Bitmask)1) << iExpr);
101843  }
101844
101845  return (mask==0);
101846}
101847
101848
101849/*
101850** Return true if the DISTINCT expression-list passed as the third argument
101851** is redundant. A DISTINCT list is redundant if the database contains a
101852** UNIQUE index that guarantees that the result of the query will be distinct
101853** anyway.
101854*/
101855static int isDistinctRedundant(
101856  Parse *pParse,
101857  SrcList *pTabList,
101858  WhereClause *pWC,
101859  ExprList *pDistinct
101860){
101861  Table *pTab;
101862  Index *pIdx;
101863  int i;
101864  int iBase;
101865
101866  /* If there is more than one table or sub-select in the FROM clause of
101867  ** this query, then it will not be possible to show that the DISTINCT
101868  ** clause is redundant. */
101869  if( pTabList->nSrc!=1 ) return 0;
101870  iBase = pTabList->a[0].iCursor;
101871  pTab = pTabList->a[0].pTab;
101872
101873  /* If any of the expressions is an IPK column on table iBase, then return
101874  ** true. Note: The (p->iTable==iBase) part of this test may be false if the
101875  ** current SELECT is a correlated sub-query.
101876  */
101877  for(i=0; i<pDistinct->nExpr; i++){
101878    Expr *p = pDistinct->a[i].pExpr;
101879    if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
101880  }
101881
101882  /* Loop through all indices on the table, checking each to see if it makes
101883  ** the DISTINCT qualifier redundant. It does so if:
101884  **
101885  **   1. The index is itself UNIQUE, and
101886  **
101887  **   2. All of the columns in the index are either part of the pDistinct
101888  **      list, or else the WHERE clause contains a term of the form "col=X",
101889  **      where X is a constant value. The collation sequences of the
101890  **      comparison and select-list expressions must match those of the index.
101891  */
101892  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
101893    if( pIdx->onError==OE_None ) continue;
101894    for(i=0; i<pIdx->nColumn; i++){
101895      int iCol = pIdx->aiColumn[i];
101896      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx)
101897       && 0>findIndexCol(pParse, pDistinct, iBase, pIdx, i)
101898      ){
101899        break;
101900      }
101901    }
101902    if( i==pIdx->nColumn ){
101903      /* This index implies that the DISTINCT qualifier is redundant. */
101904      return 1;
101905    }
101906  }
101907
101908  return 0;
101909}
101910
101911/*
101912** This routine decides if pIdx can be used to satisfy the ORDER BY
101913** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
101914** ORDER BY clause, this routine returns 0.
101915**
101916** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
101917** left-most table in the FROM clause of that same SELECT statement and
101918** the table has a cursor number of "base".  pIdx is an index on pTab.
101919**
101920** nEqCol is the number of columns of pIdx that are used as equality
101921** constraints.  Any of these columns may be missing from the ORDER BY
101922** clause and the match can still be a success.
101923**
101924** All terms of the ORDER BY that match against the index must be either
101925** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
101926** index do not need to satisfy this constraint.)  The *pbRev value is
101927** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
101928** the ORDER BY clause is all ASC.
101929*/
101930static int isSortingIndex(
101931  Parse *pParse,          /* Parsing context */
101932  WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
101933  Index *pIdx,            /* The index we are testing */
101934  int base,               /* Cursor number for the table to be sorted */
101935  ExprList *pOrderBy,     /* The ORDER BY clause */
101936  int nEqCol,             /* Number of index columns with == constraints */
101937  int wsFlags,            /* Index usages flags */
101938  int *pbRev              /* Set to 1 if ORDER BY is DESC */
101939){
101940  int i, j;                       /* Loop counters */
101941  int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
101942  int nTerm;                      /* Number of ORDER BY terms */
101943  struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
101944  sqlite3 *db = pParse->db;
101945
101946  if( !pOrderBy ) return 0;
101947  if( wsFlags & WHERE_COLUMN_IN ) return 0;
101948  if( pIdx->bUnordered ) return 0;
101949
101950  nTerm = pOrderBy->nExpr;
101951  assert( nTerm>0 );
101952
101953  /* Argument pIdx must either point to a 'real' named index structure,
101954  ** or an index structure allocated on the stack by bestBtreeIndex() to
101955  ** represent the rowid index that is part of every table.  */
101956  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
101957
101958  /* Match terms of the ORDER BY clause against columns of
101959  ** the index.
101960  **
101961  ** Note that indices have pIdx->nColumn regular columns plus
101962  ** one additional column containing the rowid.  The rowid column
101963  ** of the index is also allowed to match against the ORDER BY
101964  ** clause.
101965  */
101966  for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
101967    Expr *pExpr;       /* The expression of the ORDER BY pTerm */
101968    CollSeq *pColl;    /* The collating sequence of pExpr */
101969    int termSortOrder; /* Sort order for this term */
101970    int iColumn;       /* The i-th column of the index.  -1 for rowid */
101971    int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
101972    const char *zColl; /* Name of the collating sequence for i-th index term */
101973
101974    pExpr = pTerm->pExpr;
101975    if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
101976      /* Can not use an index sort on anything that is not a column in the
101977      ** left-most table of the FROM clause */
101978      break;
101979    }
101980    pColl = sqlite3ExprCollSeq(pParse, pExpr);
101981    if( !pColl ){
101982      pColl = db->pDfltColl;
101983    }
101984    if( pIdx->zName && i<pIdx->nColumn ){
101985      iColumn = pIdx->aiColumn[i];
101986      if( iColumn==pIdx->pTable->iPKey ){
101987        iColumn = -1;
101988      }
101989      iSortOrder = pIdx->aSortOrder[i];
101990      zColl = pIdx->azColl[i];
101991    }else{
101992      iColumn = -1;
101993      iSortOrder = 0;
101994      zColl = pColl->zName;
101995    }
101996    if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
101997      /* Term j of the ORDER BY clause does not match column i of the index */
101998      if( i<nEqCol ){
101999        /* If an index column that is constrained by == fails to match an
102000        ** ORDER BY term, that is OK.  Just ignore that column of the index
102001        */
102002        continue;
102003      }else if( i==pIdx->nColumn ){
102004        /* Index column i is the rowid.  All other terms match. */
102005        break;
102006      }else{
102007        /* If an index column fails to match and is not constrained by ==
102008        ** then the index cannot satisfy the ORDER BY constraint.
102009        */
102010        return 0;
102011      }
102012    }
102013    assert( pIdx->aSortOrder!=0 || iColumn==-1 );
102014    assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
102015    assert( iSortOrder==0 || iSortOrder==1 );
102016    termSortOrder = iSortOrder ^ pTerm->sortOrder;
102017    if( i>nEqCol ){
102018      if( termSortOrder!=sortOrder ){
102019        /* Indices can only be used if all ORDER BY terms past the
102020        ** equality constraints are all either DESC or ASC. */
102021        return 0;
102022      }
102023    }else{
102024      sortOrder = termSortOrder;
102025    }
102026    j++;
102027    pTerm++;
102028    if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
102029      /* If the indexed column is the primary key and everything matches
102030      ** so far and none of the ORDER BY terms to the right reference other
102031      ** tables in the join, then we are assured that the index can be used
102032      ** to sort because the primary key is unique and so none of the other
102033      ** columns will make any difference
102034      */
102035      j = nTerm;
102036    }
102037  }
102038
102039  *pbRev = sortOrder!=0;
102040  if( j>=nTerm ){
102041    /* All terms of the ORDER BY clause are covered by this index so
102042    ** this index can be used for sorting. */
102043    return 1;
102044  }
102045  if( pIdx->onError!=OE_None && i==pIdx->nColumn
102046      && (wsFlags & WHERE_COLUMN_NULL)==0
102047      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
102048    /* All terms of this index match some prefix of the ORDER BY clause
102049    ** and the index is UNIQUE and no terms on the tail of the ORDER BY
102050    ** clause reference other tables in a join.  If this is all true then
102051    ** the order by clause is superfluous.  Not that if the matching
102052    ** condition is IS NULL then the result is not necessarily unique
102053    ** even on a UNIQUE index, so disallow those cases. */
102054    return 1;
102055  }
102056  return 0;
102057}
102058
102059/*
102060** Prepare a crude estimate of the logarithm of the input value.
102061** The results need not be exact.  This is only used for estimating
102062** the total cost of performing operations with O(logN) or O(NlogN)
102063** complexity.  Because N is just a guess, it is no great tragedy if
102064** logN is a little off.
102065*/
102066static double estLog(double N){
102067  double logN = 1;
102068  double x = 10;
102069  while( N>x ){
102070    logN += 1;
102071    x *= 10;
102072  }
102073  return logN;
102074}
102075
102076/*
102077** Two routines for printing the content of an sqlite3_index_info
102078** structure.  Used for testing and debugging only.  If neither
102079** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
102080** are no-ops.
102081*/
102082#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
102083static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
102084  int i;
102085  if( !sqlite3WhereTrace ) return;
102086  for(i=0; i<p->nConstraint; i++){
102087    sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
102088       i,
102089       p->aConstraint[i].iColumn,
102090       p->aConstraint[i].iTermOffset,
102091       p->aConstraint[i].op,
102092       p->aConstraint[i].usable);
102093  }
102094  for(i=0; i<p->nOrderBy; i++){
102095    sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
102096       i,
102097       p->aOrderBy[i].iColumn,
102098       p->aOrderBy[i].desc);
102099  }
102100}
102101static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
102102  int i;
102103  if( !sqlite3WhereTrace ) return;
102104  for(i=0; i<p->nConstraint; i++){
102105    sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
102106       i,
102107       p->aConstraintUsage[i].argvIndex,
102108       p->aConstraintUsage[i].omit);
102109  }
102110  sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
102111  sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
102112  sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
102113  sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
102114}
102115#else
102116#define TRACE_IDX_INPUTS(A)
102117#define TRACE_IDX_OUTPUTS(A)
102118#endif
102119
102120/*
102121** Required because bestIndex() is called by bestOrClauseIndex()
102122*/
102123static void bestIndex(
102124    Parse*, WhereClause*, struct SrcList_item*,
102125    Bitmask, Bitmask, ExprList*, WhereCost*);
102126
102127/*
102128** This routine attempts to find an scanning strategy that can be used
102129** to optimize an 'OR' expression that is part of a WHERE clause.
102130**
102131** The table associated with FROM clause term pSrc may be either a
102132** regular B-Tree table or a virtual table.
102133*/
102134static void bestOrClauseIndex(
102135  Parse *pParse,              /* The parsing context */
102136  WhereClause *pWC,           /* The WHERE clause */
102137  struct SrcList_item *pSrc,  /* The FROM clause term to search */
102138  Bitmask notReady,           /* Mask of cursors not available for indexing */
102139  Bitmask notValid,           /* Cursors not available for any purpose */
102140  ExprList *pOrderBy,         /* The ORDER BY clause */
102141  WhereCost *pCost            /* Lowest cost query plan */
102142){
102143#ifndef SQLITE_OMIT_OR_OPTIMIZATION
102144  const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
102145  const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
102146  WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
102147  WhereTerm *pTerm;                 /* A single term of the WHERE clause */
102148
102149  /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
102150  ** are used */
102151  if( pSrc->notIndexed || pSrc->pIndex!=0 ){
102152    return;
102153  }
102154
102155  /* Search the WHERE clause terms for a usable WO_OR term. */
102156  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102157    if( pTerm->eOperator==WO_OR
102158     && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
102159     && (pTerm->u.pOrInfo->indexable & maskSrc)!=0
102160    ){
102161      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
102162      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
102163      WhereTerm *pOrTerm;
102164      int flags = WHERE_MULTI_OR;
102165      double rTotal = 0;
102166      double nRow = 0;
102167      Bitmask used = 0;
102168
102169      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
102170        WhereCost sTermCost;
102171        WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
102172          (pOrTerm - pOrWC->a), (pTerm - pWC->a)
102173        ));
102174        if( pOrTerm->eOperator==WO_AND ){
102175          WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
102176          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
102177        }else if( pOrTerm->leftCursor==iCur ){
102178          WhereClause tempWC;
102179          tempWC.pParse = pWC->pParse;
102180          tempWC.pMaskSet = pWC->pMaskSet;
102181          tempWC.op = TK_AND;
102182          tempWC.a = pOrTerm;
102183          tempWC.nTerm = 1;
102184          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
102185        }else{
102186          continue;
102187        }
102188        rTotal += sTermCost.rCost;
102189        nRow += sTermCost.plan.nRow;
102190        used |= sTermCost.used;
102191        if( rTotal>=pCost->rCost ) break;
102192      }
102193
102194      /* If there is an ORDER BY clause, increase the scan cost to account
102195      ** for the cost of the sort. */
102196      if( pOrderBy!=0 ){
102197        WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
102198                    rTotal, rTotal+nRow*estLog(nRow)));
102199        rTotal += nRow*estLog(nRow);
102200      }
102201
102202      /* If the cost of scanning using this OR term for optimization is
102203      ** less than the current cost stored in pCost, replace the contents
102204      ** of pCost. */
102205      WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
102206      if( rTotal<pCost->rCost ){
102207        pCost->rCost = rTotal;
102208        pCost->used = used;
102209        pCost->plan.nRow = nRow;
102210        pCost->plan.wsFlags = flags;
102211        pCost->plan.u.pTerm = pTerm;
102212      }
102213    }
102214  }
102215#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
102216}
102217
102218#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
102219/*
102220** Return TRUE if the WHERE clause term pTerm is of a form where it
102221** could be used with an index to access pSrc, assuming an appropriate
102222** index existed.
102223*/
102224static int termCanDriveIndex(
102225  WhereTerm *pTerm,              /* WHERE clause term to check */
102226  struct SrcList_item *pSrc,     /* Table we are trying to access */
102227  Bitmask notReady               /* Tables in outer loops of the join */
102228){
102229  char aff;
102230  if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
102231  if( pTerm->eOperator!=WO_EQ ) return 0;
102232  if( (pTerm->prereqRight & notReady)!=0 ) return 0;
102233  aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
102234  if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
102235  return 1;
102236}
102237#endif
102238
102239#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
102240/*
102241** If the query plan for pSrc specified in pCost is a full table scan
102242** and indexing is allows (if there is no NOT INDEXED clause) and it
102243** possible to construct a transient index that would perform better
102244** than a full table scan even when the cost of constructing the index
102245** is taken into account, then alter the query plan to use the
102246** transient index.
102247*/
102248static void bestAutomaticIndex(
102249  Parse *pParse,              /* The parsing context */
102250  WhereClause *pWC,           /* The WHERE clause */
102251  struct SrcList_item *pSrc,  /* The FROM clause term to search */
102252  Bitmask notReady,           /* Mask of cursors that are not available */
102253  WhereCost *pCost            /* Lowest cost query plan */
102254){
102255  double nTableRow;           /* Rows in the input table */
102256  double logN;                /* log(nTableRow) */
102257  double costTempIdx;         /* per-query cost of the transient index */
102258  WhereTerm *pTerm;           /* A single term of the WHERE clause */
102259  WhereTerm *pWCEnd;          /* End of pWC->a[] */
102260  Table *pTable;              /* Table tht might be indexed */
102261
102262  if( pParse->nQueryLoop<=(double)1 ){
102263    /* There is no point in building an automatic index for a single scan */
102264    return;
102265  }
102266  if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
102267    /* Automatic indices are disabled at run-time */
102268    return;
102269  }
102270  if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
102271    /* We already have some kind of index in use for this query. */
102272    return;
102273  }
102274  if( pSrc->notIndexed ){
102275    /* The NOT INDEXED clause appears in the SQL. */
102276    return;
102277  }
102278  if( pSrc->isCorrelated ){
102279    /* The source is a correlated sub-query. No point in indexing it. */
102280    return;
102281  }
102282
102283  assert( pParse->nQueryLoop >= (double)1 );
102284  pTable = pSrc->pTab;
102285  nTableRow = pTable->nRowEst;
102286  logN = estLog(nTableRow);
102287  costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
102288  if( costTempIdx>=pCost->rCost ){
102289    /* The cost of creating the transient table would be greater than
102290    ** doing the full table scan */
102291    return;
102292  }
102293
102294  /* Search for any equality comparison term */
102295  pWCEnd = &pWC->a[pWC->nTerm];
102296  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102297    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
102298      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
102299                    pCost->rCost, costTempIdx));
102300      pCost->rCost = costTempIdx;
102301      pCost->plan.nRow = logN + 1;
102302      pCost->plan.wsFlags = WHERE_TEMP_INDEX;
102303      pCost->used = pTerm->prereqRight;
102304      break;
102305    }
102306  }
102307}
102308#else
102309# define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
102310#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
102311
102312
102313#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
102314/*
102315** Generate code to construct the Index object for an automatic index
102316** and to set up the WhereLevel object pLevel so that the code generator
102317** makes use of the automatic index.
102318*/
102319static void constructAutomaticIndex(
102320  Parse *pParse,              /* The parsing context */
102321  WhereClause *pWC,           /* The WHERE clause */
102322  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
102323  Bitmask notReady,           /* Mask of cursors that are not available */
102324  WhereLevel *pLevel          /* Write new index here */
102325){
102326  int nColumn;                /* Number of columns in the constructed index */
102327  WhereTerm *pTerm;           /* A single term of the WHERE clause */
102328  WhereTerm *pWCEnd;          /* End of pWC->a[] */
102329  int nByte;                  /* Byte of memory needed for pIdx */
102330  Index *pIdx;                /* Object describing the transient index */
102331  Vdbe *v;                    /* Prepared statement under construction */
102332  int regIsInit;              /* Register set by initialization */
102333  int addrInit;               /* Address of the initialization bypass jump */
102334  Table *pTable;              /* The table being indexed */
102335  KeyInfo *pKeyinfo;          /* Key information for the index */
102336  int addrTop;                /* Top of the index fill loop */
102337  int regRecord;              /* Register holding an index record */
102338  int n;                      /* Column counter */
102339  int i;                      /* Loop counter */
102340  int mxBitCol;               /* Maximum column in pSrc->colUsed */
102341  CollSeq *pColl;             /* Collating sequence to on a column */
102342  Bitmask idxCols;            /* Bitmap of columns used for indexing */
102343  Bitmask extraCols;          /* Bitmap of additional columns */
102344
102345  /* Generate code to skip over the creation and initialization of the
102346  ** transient index on 2nd and subsequent iterations of the loop. */
102347  v = pParse->pVdbe;
102348  assert( v!=0 );
102349  regIsInit = ++pParse->nMem;
102350  addrInit = sqlite3VdbeAddOp1(v, OP_Once, regIsInit);
102351
102352  /* Count the number of columns that will be added to the index
102353  ** and used to match WHERE clause constraints */
102354  nColumn = 0;
102355  pTable = pSrc->pTab;
102356  pWCEnd = &pWC->a[pWC->nTerm];
102357  idxCols = 0;
102358  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102359    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
102360      int iCol = pTerm->u.leftColumn;
102361      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
102362      testcase( iCol==BMS );
102363      testcase( iCol==BMS-1 );
102364      if( (idxCols & cMask)==0 ){
102365        nColumn++;
102366        idxCols |= cMask;
102367      }
102368    }
102369  }
102370  assert( nColumn>0 );
102371  pLevel->plan.nEq = nColumn;
102372
102373  /* Count the number of additional columns needed to create a
102374  ** covering index.  A "covering index" is an index that contains all
102375  ** columns that are needed by the query.  With a covering index, the
102376  ** original table never needs to be accessed.  Automatic indices must
102377  ** be a covering index because the index will not be updated if the
102378  ** original table changes and the index and table cannot both be used
102379  ** if they go out of sync.
102380  */
102381  extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
102382  mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
102383  testcase( pTable->nCol==BMS-1 );
102384  testcase( pTable->nCol==BMS-2 );
102385  for(i=0; i<mxBitCol; i++){
102386    if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
102387  }
102388  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
102389    nColumn += pTable->nCol - BMS + 1;
102390  }
102391  pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
102392
102393  /* Construct the Index object to describe this index */
102394  nByte = sizeof(Index);
102395  nByte += nColumn*sizeof(int);     /* Index.aiColumn */
102396  nByte += nColumn*sizeof(char*);   /* Index.azColl */
102397  nByte += nColumn;                 /* Index.aSortOrder */
102398  pIdx = sqlite3DbMallocZero(pParse->db, nByte);
102399  if( pIdx==0 ) return;
102400  pLevel->plan.u.pIdx = pIdx;
102401  pIdx->azColl = (char**)&pIdx[1];
102402  pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
102403  pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
102404  pIdx->zName = "auto-index";
102405  pIdx->nColumn = nColumn;
102406  pIdx->pTable = pTable;
102407  n = 0;
102408  idxCols = 0;
102409  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102410    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
102411      int iCol = pTerm->u.leftColumn;
102412      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
102413      if( (idxCols & cMask)==0 ){
102414        Expr *pX = pTerm->pExpr;
102415        idxCols |= cMask;
102416        pIdx->aiColumn[n] = pTerm->u.leftColumn;
102417        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
102418        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
102419        n++;
102420      }
102421    }
102422  }
102423  assert( (u32)n==pLevel->plan.nEq );
102424
102425  /* Add additional columns needed to make the automatic index into
102426  ** a covering index */
102427  for(i=0; i<mxBitCol; i++){
102428    if( extraCols & (((Bitmask)1)<<i) ){
102429      pIdx->aiColumn[n] = i;
102430      pIdx->azColl[n] = "BINARY";
102431      n++;
102432    }
102433  }
102434  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
102435    for(i=BMS-1; i<pTable->nCol; i++){
102436      pIdx->aiColumn[n] = i;
102437      pIdx->azColl[n] = "BINARY";
102438      n++;
102439    }
102440  }
102441  assert( n==nColumn );
102442
102443  /* Create the automatic index */
102444  pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
102445  assert( pLevel->iIdxCur>=0 );
102446  sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
102447                    (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
102448  VdbeComment((v, "for %s", pTable->zName));
102449
102450  /* Fill the automatic index with content */
102451  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
102452  regRecord = sqlite3GetTempReg(pParse);
102453  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
102454  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
102455  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
102456  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
102457  sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
102458  sqlite3VdbeJumpHere(v, addrTop);
102459  sqlite3ReleaseTempReg(pParse, regRecord);
102460
102461  /* Jump here when skipping the initialization */
102462  sqlite3VdbeJumpHere(v, addrInit);
102463}
102464#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
102465
102466#ifndef SQLITE_OMIT_VIRTUALTABLE
102467/*
102468** Allocate and populate an sqlite3_index_info structure. It is the
102469** responsibility of the caller to eventually release the structure
102470** by passing the pointer returned by this function to sqlite3_free().
102471*/
102472static sqlite3_index_info *allocateIndexInfo(
102473  Parse *pParse,
102474  WhereClause *pWC,
102475  struct SrcList_item *pSrc,
102476  ExprList *pOrderBy
102477){
102478  int i, j;
102479  int nTerm;
102480  struct sqlite3_index_constraint *pIdxCons;
102481  struct sqlite3_index_orderby *pIdxOrderBy;
102482  struct sqlite3_index_constraint_usage *pUsage;
102483  WhereTerm *pTerm;
102484  int nOrderBy;
102485  sqlite3_index_info *pIdxInfo;
102486
102487  WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
102488
102489  /* Count the number of possible WHERE clause constraints referring
102490  ** to this virtual table */
102491  for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
102492    if( pTerm->leftCursor != pSrc->iCursor ) continue;
102493    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
102494    testcase( pTerm->eOperator==WO_IN );
102495    testcase( pTerm->eOperator==WO_ISNULL );
102496    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
102497    if( pTerm->wtFlags & TERM_VNULL ) continue;
102498    nTerm++;
102499  }
102500
102501  /* If the ORDER BY clause contains only columns in the current
102502  ** virtual table then allocate space for the aOrderBy part of
102503  ** the sqlite3_index_info structure.
102504  */
102505  nOrderBy = 0;
102506  if( pOrderBy ){
102507    for(i=0; i<pOrderBy->nExpr; i++){
102508      Expr *pExpr = pOrderBy->a[i].pExpr;
102509      if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
102510    }
102511    if( i==pOrderBy->nExpr ){
102512      nOrderBy = pOrderBy->nExpr;
102513    }
102514  }
102515
102516  /* Allocate the sqlite3_index_info structure
102517  */
102518  pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
102519                           + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
102520                           + sizeof(*pIdxOrderBy)*nOrderBy );
102521  if( pIdxInfo==0 ){
102522    sqlite3ErrorMsg(pParse, "out of memory");
102523    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
102524    return 0;
102525  }
102526
102527  /* Initialize the structure.  The sqlite3_index_info structure contains
102528  ** many fields that are declared "const" to prevent xBestIndex from
102529  ** changing them.  We have to do some funky casting in order to
102530  ** initialize those fields.
102531  */
102532  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
102533  pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
102534  pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
102535  *(int*)&pIdxInfo->nConstraint = nTerm;
102536  *(int*)&pIdxInfo->nOrderBy = nOrderBy;
102537  *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
102538  *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
102539  *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
102540                                                                   pUsage;
102541
102542  for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
102543    if( pTerm->leftCursor != pSrc->iCursor ) continue;
102544    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
102545    testcase( pTerm->eOperator==WO_IN );
102546    testcase( pTerm->eOperator==WO_ISNULL );
102547    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
102548    if( pTerm->wtFlags & TERM_VNULL ) continue;
102549    pIdxCons[j].iColumn = pTerm->u.leftColumn;
102550    pIdxCons[j].iTermOffset = i;
102551    pIdxCons[j].op = (u8)pTerm->eOperator;
102552    /* The direct assignment in the previous line is possible only because
102553    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
102554    ** following asserts verify this fact. */
102555    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
102556    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
102557    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
102558    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
102559    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
102560    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
102561    assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
102562    j++;
102563  }
102564  for(i=0; i<nOrderBy; i++){
102565    Expr *pExpr = pOrderBy->a[i].pExpr;
102566    pIdxOrderBy[i].iColumn = pExpr->iColumn;
102567    pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
102568  }
102569
102570  return pIdxInfo;
102571}
102572
102573/*
102574** The table object reference passed as the second argument to this function
102575** must represent a virtual table. This function invokes the xBestIndex()
102576** method of the virtual table with the sqlite3_index_info pointer passed
102577** as the argument.
102578**
102579** If an error occurs, pParse is populated with an error message and a
102580** non-zero value is returned. Otherwise, 0 is returned and the output
102581** part of the sqlite3_index_info structure is left populated.
102582**
102583** Whether or not an error is returned, it is the responsibility of the
102584** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
102585** that this is required.
102586*/
102587static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
102588  sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
102589  int i;
102590  int rc;
102591
102592  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
102593  TRACE_IDX_INPUTS(p);
102594  rc = pVtab->pModule->xBestIndex(pVtab, p);
102595  TRACE_IDX_OUTPUTS(p);
102596
102597  if( rc!=SQLITE_OK ){
102598    if( rc==SQLITE_NOMEM ){
102599      pParse->db->mallocFailed = 1;
102600    }else if( !pVtab->zErrMsg ){
102601      sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
102602    }else{
102603      sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
102604    }
102605  }
102606  sqlite3_free(pVtab->zErrMsg);
102607  pVtab->zErrMsg = 0;
102608
102609  for(i=0; i<p->nConstraint; i++){
102610    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
102611      sqlite3ErrorMsg(pParse,
102612          "table %s: xBestIndex returned an invalid plan", pTab->zName);
102613    }
102614  }
102615
102616  return pParse->nErr;
102617}
102618
102619
102620/*
102621** Compute the best index for a virtual table.
102622**
102623** The best index is computed by the xBestIndex method of the virtual
102624** table module.  This routine is really just a wrapper that sets up
102625** the sqlite3_index_info structure that is used to communicate with
102626** xBestIndex.
102627**
102628** In a join, this routine might be called multiple times for the
102629** same virtual table.  The sqlite3_index_info structure is created
102630** and initialized on the first invocation and reused on all subsequent
102631** invocations.  The sqlite3_index_info structure is also used when
102632** code is generated to access the virtual table.  The whereInfoDelete()
102633** routine takes care of freeing the sqlite3_index_info structure after
102634** everybody has finished with it.
102635*/
102636static void bestVirtualIndex(
102637  Parse *pParse,                  /* The parsing context */
102638  WhereClause *pWC,               /* The WHERE clause */
102639  struct SrcList_item *pSrc,      /* The FROM clause term to search */
102640  Bitmask notReady,               /* Mask of cursors not available for index */
102641  Bitmask notValid,               /* Cursors not valid for any purpose */
102642  ExprList *pOrderBy,             /* The order by clause */
102643  WhereCost *pCost,               /* Lowest cost query plan */
102644  sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
102645){
102646  Table *pTab = pSrc->pTab;
102647  sqlite3_index_info *pIdxInfo;
102648  struct sqlite3_index_constraint *pIdxCons;
102649  struct sqlite3_index_constraint_usage *pUsage;
102650  WhereTerm *pTerm;
102651  int i, j;
102652  int nOrderBy;
102653  double rCost;
102654
102655  /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
102656  ** malloc in allocateIndexInfo() fails and this function returns leaving
102657  ** wsFlags in an uninitialized state, the caller may behave unpredictably.
102658  */
102659  memset(pCost, 0, sizeof(*pCost));
102660  pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
102661
102662  /* If the sqlite3_index_info structure has not been previously
102663  ** allocated and initialized, then allocate and initialize it now.
102664  */
102665  pIdxInfo = *ppIdxInfo;
102666  if( pIdxInfo==0 ){
102667    *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
102668  }
102669  if( pIdxInfo==0 ){
102670    return;
102671  }
102672
102673  /* At this point, the sqlite3_index_info structure that pIdxInfo points
102674  ** to will have been initialized, either during the current invocation or
102675  ** during some prior invocation.  Now we just have to customize the
102676  ** details of pIdxInfo for the current invocation and pass it to
102677  ** xBestIndex.
102678  */
102679
102680  /* The module name must be defined. Also, by this point there must
102681  ** be a pointer to an sqlite3_vtab structure. Otherwise
102682  ** sqlite3ViewGetColumnNames() would have picked up the error.
102683  */
102684  assert( pTab->azModuleArg && pTab->azModuleArg[0] );
102685  assert( sqlite3GetVTable(pParse->db, pTab) );
102686
102687  /* Set the aConstraint[].usable fields and initialize all
102688  ** output variables to zero.
102689  **
102690  ** aConstraint[].usable is true for constraints where the right-hand
102691  ** side contains only references to tables to the left of the current
102692  ** table.  In other words, if the constraint is of the form:
102693  **
102694  **           column = expr
102695  **
102696  ** and we are evaluating a join, then the constraint on column is
102697  ** only valid if all tables referenced in expr occur to the left
102698  ** of the table containing column.
102699  **
102700  ** The aConstraints[] array contains entries for all constraints
102701  ** on the current table.  That way we only have to compute it once
102702  ** even though we might try to pick the best index multiple times.
102703  ** For each attempt at picking an index, the order of tables in the
102704  ** join might be different so we have to recompute the usable flag
102705  ** each time.
102706  */
102707  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
102708  pUsage = pIdxInfo->aConstraintUsage;
102709  for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
102710    j = pIdxCons->iTermOffset;
102711    pTerm = &pWC->a[j];
102712    pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
102713  }
102714  memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
102715  if( pIdxInfo->needToFreeIdxStr ){
102716    sqlite3_free(pIdxInfo->idxStr);
102717  }
102718  pIdxInfo->idxStr = 0;
102719  pIdxInfo->idxNum = 0;
102720  pIdxInfo->needToFreeIdxStr = 0;
102721  pIdxInfo->orderByConsumed = 0;
102722  /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
102723  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
102724  nOrderBy = pIdxInfo->nOrderBy;
102725  if( !pOrderBy ){
102726    pIdxInfo->nOrderBy = 0;
102727  }
102728
102729  if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
102730    return;
102731  }
102732
102733  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
102734  for(i=0; i<pIdxInfo->nConstraint; i++){
102735    if( pUsage[i].argvIndex>0 ){
102736      pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
102737    }
102738  }
102739
102740  /* If there is an ORDER BY clause, and the selected virtual table index
102741  ** does not satisfy it, increase the cost of the scan accordingly. This
102742  ** matches the processing for non-virtual tables in bestBtreeIndex().
102743  */
102744  rCost = pIdxInfo->estimatedCost;
102745  if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
102746    rCost += estLog(rCost)*rCost;
102747  }
102748
102749  /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
102750  ** inital value of lowestCost in this loop. If it is, then the
102751  ** (cost<lowestCost) test below will never be true.
102752  **
102753  ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
102754  ** is defined.
102755  */
102756  if( (SQLITE_BIG_DBL/((double)2))<rCost ){
102757    pCost->rCost = (SQLITE_BIG_DBL/((double)2));
102758  }else{
102759    pCost->rCost = rCost;
102760  }
102761  pCost->plan.u.pVtabIdx = pIdxInfo;
102762  if( pIdxInfo->orderByConsumed ){
102763    pCost->plan.wsFlags |= WHERE_ORDERBY;
102764  }
102765  pCost->plan.nEq = 0;
102766  pIdxInfo->nOrderBy = nOrderBy;
102767
102768  /* Try to find a more efficient access pattern by using multiple indexes
102769  ** to optimize an OR expression within the WHERE clause.
102770  */
102771  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102772}
102773#endif /* SQLITE_OMIT_VIRTUALTABLE */
102774
102775/*
102776** Argument pIdx is a pointer to an index structure that has an array of
102777** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102778** stored in Index.aSample. These samples divide the domain of values stored
102779** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102780** Region 0 contains all values less than the first sample value. Region
102781** 1 contains values between the first and second samples.  Region 2 contains
102782** values between samples 2 and 3.  And so on.  Region SQLITE_INDEX_SAMPLES
102783** contains values larger than the last sample.
102784**
102785** If the index contains many duplicates of a single value, then it is
102786** possible that two or more adjacent samples can hold the same value.
102787** When that is the case, the smallest possible region code is returned
102788** when roundUp is false and the largest possible region code is returned
102789** when roundUp is true.
102790**
102791** If successful, this function determines which of the regions value
102792** pVal lies in, sets *piRegion to the region index (a value between 0
102793** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102794** Or, if an OOM occurs while converting text values between encodings,
102795** SQLITE_NOMEM is returned and *piRegion is undefined.
102796*/
102797#ifdef SQLITE_ENABLE_STAT2
102798static int whereRangeRegion(
102799  Parse *pParse,              /* Database connection */
102800  Index *pIdx,                /* Index to consider domain of */
102801  sqlite3_value *pVal,        /* Value to consider */
102802  int roundUp,                /* Return largest valid region if true */
102803  int *piRegion               /* OUT: Region of domain in which value lies */
102804){
102805  assert( roundUp==0 || roundUp==1 );
102806  if( ALWAYS(pVal) ){
102807    IndexSample *aSample = pIdx->aSample;
102808    int i = 0;
102809    int eType = sqlite3_value_type(pVal);
102810
102811    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102812      double r = sqlite3_value_double(pVal);
102813      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102814        if( aSample[i].eType==SQLITE_NULL ) continue;
102815        if( aSample[i].eType>=SQLITE_TEXT ) break;
102816        if( roundUp ){
102817          if( aSample[i].u.r>r ) break;
102818        }else{
102819          if( aSample[i].u.r>=r ) break;
102820        }
102821      }
102822    }else if( eType==SQLITE_NULL ){
102823      i = 0;
102824      if( roundUp ){
102825        while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102826      }
102827    }else{
102828      sqlite3 *db = pParse->db;
102829      CollSeq *pColl;
102830      const u8 *z;
102831      int n;
102832
102833      /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102834      assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102835
102836      if( eType==SQLITE_BLOB ){
102837        z = (const u8 *)sqlite3_value_blob(pVal);
102838        pColl = db->pDfltColl;
102839        assert( pColl->enc==SQLITE_UTF8 );
102840      }else{
102841        pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
102842        if( pColl==0 ){
102843          sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
102844                          *pIdx->azColl);
102845          return SQLITE_ERROR;
102846        }
102847        z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
102848        if( !z ){
102849          return SQLITE_NOMEM;
102850        }
102851        assert( z && pColl && pColl->xCmp );
102852      }
102853      n = sqlite3ValueBytes(pVal, pColl->enc);
102854
102855      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102856        int c;
102857        int eSampletype = aSample[i].eType;
102858        if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102859        if( (eSampletype!=eType) ) break;
102860#ifndef SQLITE_OMIT_UTF16
102861        if( pColl->enc!=SQLITE_UTF8 ){
102862          int nSample;
102863          char *zSample = sqlite3Utf8to16(
102864              db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
102865          );
102866          if( !zSample ){
102867            assert( db->mallocFailed );
102868            return SQLITE_NOMEM;
102869          }
102870          c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
102871          sqlite3DbFree(db, zSample);
102872        }else
102873#endif
102874        {
102875          c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102876        }
102877        if( c-roundUp>=0 ) break;
102878      }
102879    }
102880
102881    assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102882    *piRegion = i;
102883  }
102884  return SQLITE_OK;
102885}
102886#endif   /* #ifdef SQLITE_ENABLE_STAT2 */
102887
102888/*
102889** If expression pExpr represents a literal value, set *pp to point to
102890** an sqlite3_value structure containing the same value, with affinity
102891** aff applied to it, before returning. It is the responsibility of the
102892** caller to eventually release this structure by passing it to
102893** sqlite3ValueFree().
102894**
102895** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
102896** is an SQL variable that currently has a non-NULL value bound to it,
102897** create an sqlite3_value structure containing this value, again with
102898** affinity aff applied to it, instead.
102899**
102900** If neither of the above apply, set *pp to NULL.
102901**
102902** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102903*/
102904#ifdef SQLITE_ENABLE_STAT2
102905static int valueFromExpr(
102906  Parse *pParse,
102907  Expr *pExpr,
102908  u8 aff,
102909  sqlite3_value **pp
102910){
102911  if( pExpr->op==TK_VARIABLE
102912   || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
102913  ){
102914    int iVar = pExpr->iColumn;
102915    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
102916    *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
102917    return SQLITE_OK;
102918  }
102919  return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
102920}
102921#endif
102922
102923/*
102924** This function is used to estimate the number of rows that will be visited
102925** by scanning an index for a range of values. The range may have an upper
102926** bound, a lower bound, or both. The WHERE clause terms that set the upper
102927** and lower bounds are represented by pLower and pUpper respectively. For
102928** example, assuming that index p is on t1(a):
102929**
102930**   ... FROM t1 WHERE a > ? AND a < ? ...
102931**                    |_____|   |_____|
102932**                       |         |
102933**                     pLower    pUpper
102934**
102935** If either of the upper or lower bound is not present, then NULL is passed in
102936** place of the corresponding WhereTerm.
102937**
102938** The nEq parameter is passed the index of the index column subject to the
102939** range constraint. Or, equivalently, the number of equality constraints
102940** optimized by the proposed index scan. For example, assuming index p is
102941** on t1(a, b), and the SQL query is:
102942**
102943**   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
102944**
102945** then nEq should be passed the value 1 (as the range restricted column,
102946** b, is the second left-most column of the index). Or, if the query is:
102947**
102948**   ... FROM t1 WHERE a > ? AND a < ? ...
102949**
102950** then nEq should be passed 0.
102951**
102952** The returned value is an integer between 1 and 100, inclusive. A return
102953** value of 1 indicates that the proposed range scan is expected to visit
102954** approximately 1/100th (1%) of the rows selected by the nEq equality
102955** constraints (if any). A return value of 100 indicates that it is expected
102956** that the range scan will visit every row (100%) selected by the equality
102957** constraints.
102958**
102959** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102960** reduces the search space by 3/4ths.  Hence a single constraint (x>?)
102961** results in a return of 25 and a range constraint (x>? AND x<?) results
102962** in a return of 6.
102963*/
102964static int whereRangeScanEst(
102965  Parse *pParse,       /* Parsing & code generating context */
102966  Index *p,            /* The index containing the range-compared column; "x" */
102967  int nEq,             /* index into p->aCol[] of the range-compared column */
102968  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
102969  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
102970  int *piEst           /* OUT: Return value */
102971){
102972  int rc = SQLITE_OK;
102973
102974#ifdef SQLITE_ENABLE_STAT2
102975
102976  if( nEq==0 && p->aSample ){
102977    sqlite3_value *pLowerVal = 0;
102978    sqlite3_value *pUpperVal = 0;
102979    int iEst;
102980    int iLower = 0;
102981    int iUpper = SQLITE_INDEX_SAMPLES;
102982    int roundUpUpper = 0;
102983    int roundUpLower = 0;
102984    u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102985
102986    if( pLower ){
102987      Expr *pExpr = pLower->pExpr->pRight;
102988      rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
102989      assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102990      roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
102991    }
102992    if( rc==SQLITE_OK && pUpper ){
102993      Expr *pExpr = pUpper->pExpr->pRight;
102994      rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
102995      assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102996      roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102997    }
102998
102999    if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
103000      sqlite3ValueFree(pLowerVal);
103001      sqlite3ValueFree(pUpperVal);
103002      goto range_est_fallback;
103003    }else if( pLowerVal==0 ){
103004      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103005      if( pLower ) iLower = iUpper/2;
103006    }else if( pUpperVal==0 ){
103007      rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103008      if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
103009    }else{
103010      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103011      if( rc==SQLITE_OK ){
103012        rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103013      }
103014    }
103015    WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
103016
103017    iEst = iUpper - iLower;
103018    testcase( iEst==SQLITE_INDEX_SAMPLES );
103019    assert( iEst<=SQLITE_INDEX_SAMPLES );
103020    if( iEst<1 ){
103021      *piEst = 50/SQLITE_INDEX_SAMPLES;
103022    }else{
103023      *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103024    }
103025    sqlite3ValueFree(pLowerVal);
103026    sqlite3ValueFree(pUpperVal);
103027    return rc;
103028  }
103029range_est_fallback:
103030#else
103031  UNUSED_PARAMETER(pParse);
103032  UNUSED_PARAMETER(p);
103033  UNUSED_PARAMETER(nEq);
103034#endif
103035  assert( pLower || pUpper );
103036  *piEst = 100;
103037  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103038  if( pUpper ) *piEst /= 4;
103039  return rc;
103040}
103041
103042#ifdef SQLITE_ENABLE_STAT2
103043/*
103044** Estimate the number of rows that will be returned based on
103045** an equality constraint x=VALUE and where that VALUE occurs in
103046** the histogram data.  This only works when x is the left-most
103047** column of an index and sqlite_stat2 histogram data is available
103048** for that index.  When pExpr==NULL that means the constraint is
103049** "x IS NULL" instead of "x=VALUE".
103050**
103051** Write the estimated row count into *pnRow and return SQLITE_OK.
103052** If unable to make an estimate, leave *pnRow unchanged and return
103053** non-zero.
103054**
103055** This routine can fail if it is unable to load a collating sequence
103056** required for string comparison, or if unable to allocate memory
103057** for a UTF conversion required for comparison.  The error is stored
103058** in the pParse structure.
103059*/
103060static int whereEqualScanEst(
103061  Parse *pParse,       /* Parsing & code generating context */
103062  Index *p,            /* The index whose left-most column is pTerm */
103063  Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
103064  double *pnRow        /* Write the revised row estimate here */
103065){
103066  sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
103067  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
103068  u8 aff;                   /* Column affinity */
103069  int rc;                   /* Subfunction return code */
103070  double nRowEst;           /* New estimate of the number of rows */
103071
103072  assert( p->aSample!=0 );
103073  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103074  if( pExpr ){
103075    rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
103076    if( rc ) goto whereEqualScanEst_cancel;
103077  }else{
103078    pRhs = sqlite3ValueNew(pParse->db);
103079  }
103080  if( pRhs==0 ) return SQLITE_NOTFOUND;
103081  rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103082  if( rc ) goto whereEqualScanEst_cancel;
103083  rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103084  if( rc ) goto whereEqualScanEst_cancel;
103085  WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103086  if( iLower>=iUpper ){
103087    nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103088    if( nRowEst<*pnRow ) *pnRow = nRowEst;
103089  }else{
103090    nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103091    *pnRow = nRowEst;
103092  }
103093
103094whereEqualScanEst_cancel:
103095  sqlite3ValueFree(pRhs);
103096  return rc;
103097}
103098#endif /* defined(SQLITE_ENABLE_STAT2) */
103099
103100#ifdef SQLITE_ENABLE_STAT2
103101/*
103102** Estimate the number of rows that will be returned based on
103103** an IN constraint where the right-hand side of the IN operator
103104** is a list of values.  Example:
103105**
103106**        WHERE x IN (1,2,3,4)
103107**
103108** Write the estimated row count into *pnRow and return SQLITE_OK.
103109** If unable to make an estimate, leave *pnRow unchanged and return
103110** non-zero.
103111**
103112** This routine can fail if it is unable to load a collating sequence
103113** required for string comparison, or if unable to allocate memory
103114** for a UTF conversion required for comparison.  The error is stored
103115** in the pParse structure.
103116*/
103117static int whereInScanEst(
103118  Parse *pParse,       /* Parsing & code generating context */
103119  Index *p,            /* The index whose left-most column is pTerm */
103120  ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103121  double *pnRow        /* Write the revised row estimate here */
103122){
103123  sqlite3_value *pVal = 0;  /* One value from list */
103124  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
103125  u8 aff;                   /* Column affinity */
103126  int rc = SQLITE_OK;       /* Subfunction return code */
103127  double nRowEst;           /* New estimate of the number of rows */
103128  int nSpan = 0;            /* Number of histogram regions spanned */
103129  int nSingle = 0;          /* Histogram regions hit by a single value */
103130  int nNotFound = 0;        /* Count of values that are not constants */
103131  int i;                               /* Loop counter */
103132  u8 aSpan[SQLITE_INDEX_SAMPLES+1];    /* Histogram regions that are spanned */
103133  u8 aSingle[SQLITE_INDEX_SAMPLES+1];  /* Histogram regions hit once */
103134
103135  assert( p->aSample!=0 );
103136  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103137  memset(aSpan, 0, sizeof(aSpan));
103138  memset(aSingle, 0, sizeof(aSingle));
103139  for(i=0; i<pList->nExpr; i++){
103140    sqlite3ValueFree(pVal);
103141    rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103142    if( rc ) break;
103143    if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103144      nNotFound++;
103145      continue;
103146    }
103147    rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103148    if( rc ) break;
103149    rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103150    if( rc ) break;
103151    if( iLower>=iUpper ){
103152      aSingle[iLower] = 1;
103153    }else{
103154      assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103155      while( iLower<iUpper ) aSpan[iLower++] = 1;
103156    }
103157  }
103158  if( rc==SQLITE_OK ){
103159    for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103160      if( aSpan[i] ){
103161        nSpan++;
103162      }else if( aSingle[i] ){
103163        nSingle++;
103164      }
103165    }
103166    nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103167               + nNotFound*p->aiRowEst[1];
103168    if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103169    *pnRow = nRowEst;
103170    WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103171                 nSpan, nSingle, nNotFound, nRowEst));
103172  }
103173  sqlite3ValueFree(pVal);
103174  return rc;
103175}
103176#endif /* defined(SQLITE_ENABLE_STAT2) */
103177
103178
103179/*
103180** Find the best query plan for accessing a particular table.  Write the
103181** best query plan and its cost into the WhereCost object supplied as the
103182** last parameter.
103183**
103184** The lowest cost plan wins.  The cost is an estimate of the amount of
103185** CPU and disk I/O needed to process the requested result.
103186** Factors that influence cost include:
103187**
103188**    *  The estimated number of rows that will be retrieved.  (The
103189**       fewer the better.)
103190**
103191**    *  Whether or not sorting must occur.
103192**
103193**    *  Whether or not there must be separate lookups in the
103194**       index and in the main table.
103195**
103196** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
103197** the SQL statement, then this function only considers plans using the
103198** named index. If no such plan is found, then the returned cost is
103199** SQLITE_BIG_DBL. If a plan is found that uses the named index,
103200** then the cost is calculated in the usual way.
103201**
103202** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
103203** in the SELECT statement, then no indexes are considered. However, the
103204** selected plan may still take advantage of the built-in rowid primary key
103205** index.
103206*/
103207static void bestBtreeIndex(
103208  Parse *pParse,              /* The parsing context */
103209  WhereClause *pWC,           /* The WHERE clause */
103210  struct SrcList_item *pSrc,  /* The FROM clause term to search */
103211  Bitmask notReady,           /* Mask of cursors not available for indexing */
103212  Bitmask notValid,           /* Cursors not available for any purpose */
103213  ExprList *pOrderBy,         /* The ORDER BY clause */
103214  ExprList *pDistinct,        /* The select-list if query is DISTINCT */
103215  WhereCost *pCost            /* Lowest cost query plan */
103216){
103217  int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
103218  Index *pProbe;              /* An index we are evaluating */
103219  Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
103220  int eqTermMask;             /* Current mask of valid equality operators */
103221  int idxEqTermMask;          /* Index mask of valid equality operators */
103222  Index sPk;                  /* A fake index object for the primary key */
103223  unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103224  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
103225  int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
103226
103227  /* Initialize the cost to a worst-case value */
103228  memset(pCost, 0, sizeof(*pCost));
103229  pCost->rCost = SQLITE_BIG_DBL;
103230
103231  /* If the pSrc table is the right table of a LEFT JOIN then we may not
103232  ** use an index to satisfy IS NULL constraints on that table.  This is
103233  ** because columns might end up being NULL if the table does not match -
103234  ** a circumstance which the index cannot help us discover.  Ticket #2177.
103235  */
103236  if( pSrc->jointype & JT_LEFT ){
103237    idxEqTermMask = WO_EQ|WO_IN;
103238  }else{
103239    idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
103240  }
103241
103242  if( pSrc->pIndex ){
103243    /* An INDEXED BY clause specifies a particular index to use */
103244    pIdx = pProbe = pSrc->pIndex;
103245    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
103246    eqTermMask = idxEqTermMask;
103247  }else{
103248    /* There is no INDEXED BY clause.  Create a fake Index object in local
103249    ** variable sPk to represent the rowid primary key index.  Make this
103250    ** fake index the first in a chain of Index objects with all of the real
103251    ** indices to follow */
103252    Index *pFirst;                  /* First of real indices on the table */
103253    memset(&sPk, 0, sizeof(Index));
103254    sPk.nColumn = 1;
103255    sPk.aiColumn = &aiColumnPk;
103256    sPk.aiRowEst = aiRowEstPk;
103257    sPk.onError = OE_Replace;
103258    sPk.pTable = pSrc->pTab;
103259    aiRowEstPk[0] = pSrc->pTab->nRowEst;
103260    aiRowEstPk[1] = 1;
103261    pFirst = pSrc->pTab->pIndex;
103262    if( pSrc->notIndexed==0 ){
103263      /* The real indices of the table are only considered if the
103264      ** NOT INDEXED qualifier is omitted from the FROM clause */
103265      sPk.pNext = pFirst;
103266    }
103267    pProbe = &sPk;
103268    wsFlagMask = ~(
103269        WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
103270    );
103271    eqTermMask = WO_EQ|WO_IN;
103272    pIdx = 0;
103273  }
103274
103275  /* Loop over all indices looking for the best one to use
103276  */
103277  for(; pProbe; pIdx=pProbe=pProbe->pNext){
103278    const unsigned int * const aiRowEst = pProbe->aiRowEst;
103279    double cost;                /* Cost of using pProbe */
103280    double nRow;                /* Estimated number of rows in result set */
103281    double log10N;              /* base-10 logarithm of nRow (inexact) */
103282    int rev;                    /* True to scan in reverse order */
103283    int wsFlags = 0;
103284    Bitmask used = 0;
103285
103286    /* The following variables are populated based on the properties of
103287    ** index being evaluated. They are then used to determine the expected
103288    ** cost and number of rows returned.
103289    **
103290    **  nEq:
103291    **    Number of equality terms that can be implemented using the index.
103292    **    In other words, the number of initial fields in the index that
103293    **    are used in == or IN or NOT NULL constraints of the WHERE clause.
103294    **
103295    **  nInMul:
103296    **    The "in-multiplier". This is an estimate of how many seek operations
103297    **    SQLite must perform on the index in question. For example, if the
103298    **    WHERE clause is:
103299    **
103300    **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
103301    **
103302    **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is
103303    **    set to 9. Given the same schema and either of the following WHERE
103304    **    clauses:
103305    **
103306    **      WHERE a =  1
103307    **      WHERE a >= 2
103308    **
103309    **    nInMul is set to 1.
103310    **
103311    **    If there exists a WHERE term of the form "x IN (SELECT ...)", then
103312    **    the sub-select is assumed to return 25 rows for the purposes of
103313    **    determining nInMul.
103314    **
103315    **  bInEst:
103316    **    Set to true if there was at least one "x IN (SELECT ...)" term used
103317    **    in determining the value of nInMul.  Note that the RHS of the
103318    **    IN operator must be a SELECT, not a value list, for this variable
103319    **    to be true.
103320    **
103321    **  estBound:
103322    **    An estimate on the amount of the table that must be searched.  A
103323    **    value of 100 means the entire table is searched.  Range constraints
103324    **    might reduce this to a value less than 100 to indicate that only
103325    **    a fraction of the table needs searching.  In the absence of
103326    **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
103327    **    space to 1/4rd its original size.  So an x>? constraint reduces
103328    **    estBound to 25.  Two constraints (x>? AND x<?) reduce estBound to 6.
103329    **
103330    **  bSort:
103331    **    Boolean. True if there is an ORDER BY clause that will require an
103332    **    external sort (i.e. scanning the index being evaluated will not
103333    **    correctly order records).
103334    **
103335    **  bLookup:
103336    **    Boolean. True if a table lookup is required for each index entry
103337    **    visited.  In other words, true if this is not a covering index.
103338    **    This is always false for the rowid primary key index of a table.
103339    **    For other indexes, it is true unless all the columns of the table
103340    **    used by the SELECT statement are present in the index (such an
103341    **    index is sometimes described as a covering index).
103342    **    For example, given the index on (a, b), the second of the following
103343    **    two queries requires table b-tree lookups in order to find the value
103344    **    of column c, but the first does not because columns a and b are
103345    **    both available in the index.
103346    **
103347    **             SELECT a, b    FROM tbl WHERE a = 1;
103348    **             SELECT a, b, c FROM tbl WHERE a = 1;
103349    */
103350    int nEq;                      /* Number of == or IN terms matching index */
103351    int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
103352    int nInMul = 1;               /* Number of distinct equalities to lookup */
103353    int estBound = 100;           /* Estimated reduction in search space */
103354    int nBound = 0;               /* Number of range constraints seen */
103355    int bSort = !!pOrderBy;       /* True if external sort required */
103356    int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
103357    int bLookup = 0;              /* True if not a covering index */
103358    WhereTerm *pTerm;             /* A single term of the WHERE clause */
103359#ifdef SQLITE_ENABLE_STAT2
103360    WhereTerm *pFirstTerm = 0;    /* First term matching the index */
103361#endif
103362
103363    /* Determine the values of nEq and nInMul */
103364    for(nEq=0; nEq<pProbe->nColumn; nEq++){
103365      int j = pProbe->aiColumn[nEq];
103366      pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
103367      if( pTerm==0 ) break;
103368      wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
103369      if( pTerm->eOperator & WO_IN ){
103370        Expr *pExpr = pTerm->pExpr;
103371        wsFlags |= WHERE_COLUMN_IN;
103372        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103373          /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
103374          nInMul *= 25;
103375          bInEst = 1;
103376        }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
103377          /* "x IN (value, value, ...)" */
103378          nInMul *= pExpr->x.pList->nExpr;
103379        }
103380      }else if( pTerm->eOperator & WO_ISNULL ){
103381        wsFlags |= WHERE_COLUMN_NULL;
103382      }
103383#ifdef SQLITE_ENABLE_STAT2
103384      if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103385#endif
103386      used |= pTerm->prereqRight;
103387    }
103388
103389    /* Determine the value of estBound. */
103390    if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103391      int j = pProbe->aiColumn[nEq];
103392      if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103393        WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103394        WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103395        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
103396        if( pTop ){
103397          nBound = 1;
103398          wsFlags |= WHERE_TOP_LIMIT;
103399          used |= pTop->prereqRight;
103400        }
103401        if( pBtm ){
103402          nBound++;
103403          wsFlags |= WHERE_BTM_LIMIT;
103404          used |= pBtm->prereqRight;
103405        }
103406        wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103407      }
103408    }else if( pProbe->onError!=OE_None ){
103409      testcase( wsFlags & WHERE_COLUMN_IN );
103410      testcase( wsFlags & WHERE_COLUMN_NULL );
103411      if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
103412        wsFlags |= WHERE_UNIQUE;
103413      }
103414    }
103415
103416    /* If there is an ORDER BY clause and the index being considered will
103417    ** naturally scan rows in the required order, set the appropriate flags
103418    ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
103419    ** will scan rows in a different order, set the bSort variable.  */
103420    if( isSortingIndex(
103421          pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
103422    ){
103423      bSort = 0;
103424      wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
103425      wsFlags |= (rev ? WHERE_REVERSE : 0);
103426    }
103427
103428    /* If there is a DISTINCT qualifier and this index will scan rows in
103429    ** order of the DISTINCT expressions, clear bDist and set the appropriate
103430    ** flags in wsFlags. */
103431    if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq) ){
103432      bDist = 0;
103433      wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
103434    }
103435
103436    /* If currently calculating the cost of using an index (not the IPK
103437    ** index), determine if all required column data may be obtained without
103438    ** using the main table (i.e. if the index is a covering
103439    ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
103440    ** wsFlags. Otherwise, set the bLookup variable to true.  */
103441    if( pIdx && wsFlags ){
103442      Bitmask m = pSrc->colUsed;
103443      int j;
103444      for(j=0; j<pIdx->nColumn; j++){
103445        int x = pIdx->aiColumn[j];
103446        if( x<BMS-1 ){
103447          m &= ~(((Bitmask)1)<<x);
103448        }
103449      }
103450      if( m==0 ){
103451        wsFlags |= WHERE_IDX_ONLY;
103452      }else{
103453        bLookup = 1;
103454      }
103455    }
103456
103457    /*
103458    ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
103459    ** constraint, do not let the estimate exceed half the rows in the table.
103460    */
103461    nRow = (double)(aiRowEst[nEq] * nInMul);
103462    if( bInEst && nRow*2>aiRowEst[0] ){
103463      nRow = aiRowEst[0]/2;
103464      nInMul = (int)(nRow / aiRowEst[nEq]);
103465    }
103466
103467#ifdef SQLITE_ENABLE_STAT2
103468    /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103469    ** and we do not think that values of x are unique and if histogram
103470    ** data is available for column x, then it might be possible
103471    ** to get a better estimate on the number of rows based on
103472    ** VALUE and how common that value is according to the histogram.
103473    */
103474    if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
103475      if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
103476        testcase( pFirstTerm->eOperator==WO_EQ );
103477        testcase( pFirstTerm->eOperator==WO_ISNULL );
103478        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103479      }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
103480        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103481      }
103482    }
103483#endif /* SQLITE_ENABLE_STAT2 */
103484
103485    /* Adjust the number of output rows and downward to reflect rows
103486    ** that are excluded by range constraints.
103487    */
103488    nRow = (nRow * (double)estBound) / (double)100;
103489    if( nRow<1 ) nRow = 1;
103490
103491    /* Experiments run on real SQLite databases show that the time needed
103492    ** to do a binary search to locate a row in a table or index is roughly
103493    ** log10(N) times the time to move from one row to the next row within
103494    ** a table or index.  The actual times can vary, with the size of
103495    ** records being an important factor.  Both moves and searches are
103496    ** slower with larger records, presumably because fewer records fit
103497    ** on one page and hence more pages have to be fetched.
103498    **
103499    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
103500    ** not give us data on the relative sizes of table and index records.
103501    ** So this computation assumes table records are about twice as big
103502    ** as index records
103503    */
103504    if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
103505      /* The cost of a full table scan is a number of move operations equal
103506      ** to the number of rows in the table.
103507      **
103508      ** We add an additional 4x penalty to full table scans.  This causes
103509      ** the cost function to err on the side of choosing an index over
103510      ** choosing a full scan.  This 4x full-scan penalty is an arguable
103511      ** decision and one which we expect to revisit in the future.  But
103512      ** it seems to be working well enough at the moment.
103513      */
103514      cost = aiRowEst[0]*4;
103515    }else{
103516      log10N = estLog(aiRowEst[0]);
103517      cost = nRow;
103518      if( pIdx ){
103519        if( bLookup ){
103520          /* For an index lookup followed by a table lookup:
103521          **    nInMul index searches to find the start of each index range
103522          **  + nRow steps through the index
103523          **  + nRow table searches to lookup the table entry using the rowid
103524          */
103525          cost += (nInMul + nRow)*log10N;
103526        }else{
103527          /* For a covering index:
103528          **     nInMul index searches to find the initial entry
103529          **   + nRow steps through the index
103530          */
103531          cost += nInMul*log10N;
103532        }
103533      }else{
103534        /* For a rowid primary key lookup:
103535        **    nInMult table searches to find the initial entry for each range
103536        **  + nRow steps through the table
103537        */
103538        cost += nInMul*log10N;
103539      }
103540    }
103541
103542    /* Add in the estimated cost of sorting the result.  Actual experimental
103543    ** measurements of sorting performance in SQLite show that sorting time
103544    ** adds C*N*log10(N) to the cost, where N is the number of rows to be
103545    ** sorted and C is a factor between 1.95 and 4.3.  We will split the
103546    ** difference and select C of 3.0.
103547    */
103548    if( bSort ){
103549      cost += nRow*estLog(nRow)*3;
103550    }
103551    if( bDist ){
103552      cost += nRow*estLog(nRow)*3;
103553    }
103554
103555    /**** Cost of using this index has now been computed ****/
103556
103557    /* If there are additional constraints on this table that cannot
103558    ** be used with the current index, but which might lower the number
103559    ** of output rows, adjust the nRow value accordingly.  This only
103560    ** matters if the current index is the least costly, so do not bother
103561    ** with this step if we already know this index will not be chosen.
103562    ** Also, never reduce the output row count below 2 using this step.
103563    **
103564    ** It is critical that the notValid mask be used here instead of
103565    ** the notReady mask.  When computing an "optimal" index, the notReady
103566    ** mask will only have one bit set - the bit for the current table.
103567    ** The notValid mask, on the other hand, always has all bits set for
103568    ** tables that are not in outer loops.  If notReady is used here instead
103569    ** of notValid, then a optimal index that depends on inner joins loops
103570    ** might be selected even when there exists an optimal index that has
103571    ** no such dependency.
103572    */
103573    if( nRow>2 && cost<=pCost->rCost ){
103574      int k;                       /* Loop counter */
103575      int nSkipEq = nEq;           /* Number of == constraints to skip */
103576      int nSkipRange = nBound;     /* Number of < constraints to skip */
103577      Bitmask thisTab;             /* Bitmap for pSrc */
103578
103579      thisTab = getMask(pWC->pMaskSet, iCur);
103580      for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
103581        if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
103582        if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
103583        if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
103584          if( nSkipEq ){
103585            /* Ignore the first nEq equality matches since the index
103586            ** has already accounted for these */
103587            nSkipEq--;
103588          }else{
103589            /* Assume each additional equality match reduces the result
103590            ** set size by a factor of 10 */
103591            nRow /= 10;
103592          }
103593        }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
103594          if( nSkipRange ){
103595            /* Ignore the first nSkipRange range constraints since the index
103596            ** has already accounted for these */
103597            nSkipRange--;
103598          }else{
103599            /* Assume each additional range constraint reduces the result
103600            ** set size by a factor of 3.  Indexed range constraints reduce
103601            ** the search space by a larger factor: 4.  We make indexed range
103602            ** more selective intentionally because of the subjective
103603            ** observation that indexed range constraints really are more
103604            ** selective in practice, on average. */
103605            nRow /= 3;
103606          }
103607        }else if( pTerm->eOperator!=WO_NOOP ){
103608          /* Any other expression lowers the output row count by half */
103609          nRow /= 2;
103610        }
103611      }
103612      if( nRow<2 ) nRow = 2;
103613    }
103614
103615
103616    WHERETRACE((
103617      "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103618      "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103619      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103620      nEq, nInMul, estBound, bSort, bLookup, wsFlags,
103621      notReady, log10N, nRow, cost, used
103622    ));
103623
103624    /* If this index is the best we have seen so far, then record this
103625    ** index and its cost in the pCost structure.
103626    */
103627    if( (!pIdx || wsFlags)
103628     && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
103629    ){
103630      pCost->rCost = cost;
103631      pCost->used = used;
103632      pCost->plan.nRow = nRow;
103633      pCost->plan.wsFlags = (wsFlags&wsFlagMask);
103634      pCost->plan.nEq = nEq;
103635      pCost->plan.u.pIdx = pIdx;
103636    }
103637
103638    /* If there was an INDEXED BY clause, then only that one index is
103639    ** considered. */
103640    if( pSrc->pIndex ) break;
103641
103642    /* Reset masks for the next index in the loop */
103643    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
103644    eqTermMask = idxEqTermMask;
103645  }
103646
103647  /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
103648  ** is set, then reverse the order that the index will be scanned
103649  ** in. This is used for application testing, to help find cases
103650  ** where application behaviour depends on the (undefined) order that
103651  ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
103652  if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
103653    pCost->plan.wsFlags |= WHERE_REVERSE;
103654  }
103655
103656  assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
103657  assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
103658  assert( pSrc->pIndex==0
103659       || pCost->plan.u.pIdx==0
103660       || pCost->plan.u.pIdx==pSrc->pIndex
103661  );
103662
103663  WHERETRACE(("best index is: %s\n",
103664    ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
103665         pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
103666  ));
103667
103668  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
103669  bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
103670  pCost->plan.wsFlags |= eqTermMask;
103671}
103672
103673/*
103674** Find the query plan for accessing table pSrc->pTab. Write the
103675** best query plan and its cost into the WhereCost object supplied
103676** as the last parameter. This function may calculate the cost of
103677** both real and virtual table scans.
103678*/
103679static void bestIndex(
103680  Parse *pParse,              /* The parsing context */
103681  WhereClause *pWC,           /* The WHERE clause */
103682  struct SrcList_item *pSrc,  /* The FROM clause term to search */
103683  Bitmask notReady,           /* Mask of cursors not available for indexing */
103684  Bitmask notValid,           /* Cursors not available for any purpose */
103685  ExprList *pOrderBy,         /* The ORDER BY clause */
103686  WhereCost *pCost            /* Lowest cost query plan */
103687){
103688#ifndef SQLITE_OMIT_VIRTUALTABLE
103689  if( IsVirtual(pSrc->pTab) ){
103690    sqlite3_index_info *p = 0;
103691    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
103692    if( p->needToFreeIdxStr ){
103693      sqlite3_free(p->idxStr);
103694    }
103695    sqlite3DbFree(pParse->db, p);
103696  }else
103697#endif
103698  {
103699    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
103700  }
103701}
103702
103703/*
103704** Disable a term in the WHERE clause.  Except, do not disable the term
103705** if it controls a LEFT OUTER JOIN and it did not originate in the ON
103706** or USING clause of that join.
103707**
103708** Consider the term t2.z='ok' in the following queries:
103709**
103710**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
103711**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
103712**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
103713**
103714** The t2.z='ok' is disabled in the in (2) because it originates
103715** in the ON clause.  The term is disabled in (3) because it is not part
103716** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
103717**
103718** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
103719** completely satisfied by indices.
103720**
103721** Disabling a term causes that term to not be tested in the inner loop
103722** of the join.  Disabling is an optimization.  When terms are satisfied
103723** by indices, we disable them to prevent redundant tests in the inner
103724** loop.  We would get the correct results if nothing were ever disabled,
103725** but joins might run a little slower.  The trick is to disable as much
103726** as we can without disabling too much.  If we disabled in (1), we'd get
103727** the wrong answer.  See ticket #813.
103728*/
103729static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
103730  if( pTerm
103731      && (pTerm->wtFlags & TERM_CODED)==0
103732      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
103733  ){
103734    pTerm->wtFlags |= TERM_CODED;
103735    if( pTerm->iParent>=0 ){
103736      WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
103737      if( (--pOther->nChild)==0 ){
103738        disableTerm(pLevel, pOther);
103739      }
103740    }
103741  }
103742}
103743
103744/*
103745** Code an OP_Affinity opcode to apply the column affinity string zAff
103746** to the n registers starting at base.
103747**
103748** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
103749** beginning and end of zAff are ignored.  If all entries in zAff are
103750** SQLITE_AFF_NONE, then no code gets generated.
103751**
103752** This routine makes its own copy of zAff so that the caller is free
103753** to modify zAff after this routine returns.
103754*/
103755static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
103756  Vdbe *v = pParse->pVdbe;
103757  if( zAff==0 ){
103758    assert( pParse->db->mallocFailed );
103759    return;
103760  }
103761  assert( v!=0 );
103762
103763  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
103764  ** and end of the affinity string.
103765  */
103766  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
103767    n--;
103768    base++;
103769    zAff++;
103770  }
103771  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
103772    n--;
103773  }
103774
103775  /* Code the OP_Affinity opcode if there is anything left to do. */
103776  if( n>0 ){
103777    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
103778    sqlite3VdbeChangeP4(v, -1, zAff, n);
103779    sqlite3ExprCacheAffinityChange(pParse, base, n);
103780  }
103781}
103782
103783
103784/*
103785** Generate code for a single equality term of the WHERE clause.  An equality
103786** term can be either X=expr or X IN (...).   pTerm is the term to be
103787** coded.
103788**
103789** The current value for the constraint is left in register iReg.
103790**
103791** For a constraint of the form X=expr, the expression is evaluated and its
103792** result is left on the stack.  For constraints of the form X IN (...)
103793** this routine sets up a loop that will iterate over all values of X.
103794*/
103795static int codeEqualityTerm(
103796  Parse *pParse,      /* The parsing context */
103797  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
103798  WhereLevel *pLevel, /* When level of the FROM clause we are working on */
103799  int iTarget         /* Attempt to leave results in this register */
103800){
103801  Expr *pX = pTerm->pExpr;
103802  Vdbe *v = pParse->pVdbe;
103803  int iReg;                  /* Register holding results */
103804
103805  assert( iTarget>0 );
103806  if( pX->op==TK_EQ ){
103807    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
103808  }else if( pX->op==TK_ISNULL ){
103809    iReg = iTarget;
103810    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
103811#ifndef SQLITE_OMIT_SUBQUERY
103812  }else{
103813    int eType;
103814    int iTab;
103815    struct InLoop *pIn;
103816
103817    assert( pX->op==TK_IN );
103818    iReg = iTarget;
103819    eType = sqlite3FindInIndex(pParse, pX, 0);
103820    iTab = pX->iTable;
103821    sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
103822    assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
103823    if( pLevel->u.in.nIn==0 ){
103824      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
103825    }
103826    pLevel->u.in.nIn++;
103827    pLevel->u.in.aInLoop =
103828       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
103829                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
103830    pIn = pLevel->u.in.aInLoop;
103831    if( pIn ){
103832      pIn += pLevel->u.in.nIn - 1;
103833      pIn->iCur = iTab;
103834      if( eType==IN_INDEX_ROWID ){
103835        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
103836      }else{
103837        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
103838      }
103839      sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
103840    }else{
103841      pLevel->u.in.nIn = 0;
103842    }
103843#endif
103844  }
103845  disableTerm(pLevel, pTerm);
103846  return iReg;
103847}
103848
103849/*
103850** Generate code that will evaluate all == and IN constraints for an
103851** index.
103852**
103853** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
103854** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
103855** The index has as many as three equality constraints, but in this
103856** example, the third "c" value is an inequality.  So only two
103857** constraints are coded.  This routine will generate code to evaluate
103858** a==5 and b IN (1,2,3).  The current values for a and b will be stored
103859** in consecutive registers and the index of the first register is returned.
103860**
103861** In the example above nEq==2.  But this subroutine works for any value
103862** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
103863** The only thing it does is allocate the pLevel->iMem memory cell and
103864** compute the affinity string.
103865**
103866** This routine always allocates at least one memory cell and returns
103867** the index of that memory cell. The code that
103868** calls this routine will use that memory cell to store the termination
103869** key value of the loop.  If one or more IN operators appear, then
103870** this routine allocates an additional nEq memory cells for internal
103871** use.
103872**
103873** Before returning, *pzAff is set to point to a buffer containing a
103874** copy of the column affinity string of the index allocated using
103875** sqlite3DbMalloc(). Except, entries in the copy of the string associated
103876** with equality constraints that use NONE affinity are set to
103877** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
103878**
103879**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
103880**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
103881**
103882** In the example above, the index on t1(a) has TEXT affinity. But since
103883** the right hand side of the equality constraint (t2.b) has NONE affinity,
103884** no conversion should be attempted before using a t2.b value as part of
103885** a key to search the index. Hence the first byte in the returned affinity
103886** string in this example would be set to SQLITE_AFF_NONE.
103887*/
103888static int codeAllEqualityTerms(
103889  Parse *pParse,        /* Parsing context */
103890  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
103891  WhereClause *pWC,     /* The WHERE clause */
103892  Bitmask notReady,     /* Which parts of FROM have not yet been coded */
103893  int nExtraReg,        /* Number of extra registers to allocate */
103894  char **pzAff          /* OUT: Set to point to affinity string */
103895){
103896  int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
103897  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
103898  Index *pIdx;                  /* The index being used for this loop */
103899  int iCur = pLevel->iTabCur;   /* The cursor of the table */
103900  WhereTerm *pTerm;             /* A single constraint term */
103901  int j;                        /* Loop counter */
103902  int regBase;                  /* Base register */
103903  int nReg;                     /* Number of registers to allocate */
103904  char *zAff;                   /* Affinity string to return */
103905
103906  /* This module is only called on query plans that use an index. */
103907  assert( pLevel->plan.wsFlags & WHERE_INDEXED );
103908  pIdx = pLevel->plan.u.pIdx;
103909
103910  /* Figure out how many memory cells we will need then allocate them.
103911  */
103912  regBase = pParse->nMem + 1;
103913  nReg = pLevel->plan.nEq + nExtraReg;
103914  pParse->nMem += nReg;
103915
103916  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
103917  if( !zAff ){
103918    pParse->db->mallocFailed = 1;
103919  }
103920
103921  /* Evaluate the equality constraints
103922  */
103923  assert( pIdx->nColumn>=nEq );
103924  for(j=0; j<nEq; j++){
103925    int r1;
103926    int k = pIdx->aiColumn[j];
103927    pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
103928    if( NEVER(pTerm==0) ) break;
103929    /* The following true for indices with redundant columns.
103930    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
103931    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
103932    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
103933    r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
103934    if( r1!=regBase+j ){
103935      if( nReg==1 ){
103936        sqlite3ReleaseTempReg(pParse, regBase);
103937        regBase = r1;
103938      }else{
103939        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
103940      }
103941    }
103942    testcase( pTerm->eOperator & WO_ISNULL );
103943    testcase( pTerm->eOperator & WO_IN );
103944    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
103945      Expr *pRight = pTerm->pExpr->pRight;
103946      sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
103947      if( zAff ){
103948        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
103949          zAff[j] = SQLITE_AFF_NONE;
103950        }
103951        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
103952          zAff[j] = SQLITE_AFF_NONE;
103953        }
103954      }
103955    }
103956  }
103957  *pzAff = zAff;
103958  return regBase;
103959}
103960
103961#ifndef SQLITE_OMIT_EXPLAIN
103962/*
103963** This routine is a helper for explainIndexRange() below
103964**
103965** pStr holds the text of an expression that we are building up one term
103966** at a time.  This routine adds a new term to the end of the expression.
103967** Terms are separated by AND so add the "AND" text for second and subsequent
103968** terms only.
103969*/
103970static void explainAppendTerm(
103971  StrAccum *pStr,             /* The text expression being built */
103972  int iTerm,                  /* Index of this term.  First is zero */
103973  const char *zColumn,        /* Name of the column */
103974  const char *zOp             /* Name of the operator */
103975){
103976  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
103977  sqlite3StrAccumAppend(pStr, zColumn, -1);
103978  sqlite3StrAccumAppend(pStr, zOp, 1);
103979  sqlite3StrAccumAppend(pStr, "?", 1);
103980}
103981
103982/*
103983** Argument pLevel describes a strategy for scanning table pTab. This
103984** function returns a pointer to a string buffer containing a description
103985** of the subset of table rows scanned by the strategy in the form of an
103986** SQL expression. Or, if all rows are scanned, NULL is returned.
103987**
103988** For example, if the query:
103989**
103990**   SELECT * FROM t1 WHERE a=1 AND b>2;
103991**
103992** is run and there is an index on (a, b), then this function returns a
103993** string similar to:
103994**
103995**   "a=? AND b>?"
103996**
103997** The returned pointer points to memory obtained from sqlite3DbMalloc().
103998** It is the responsibility of the caller to free the buffer when it is
103999** no longer required.
104000*/
104001static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
104002  WherePlan *pPlan = &pLevel->plan;
104003  Index *pIndex = pPlan->u.pIdx;
104004  int nEq = pPlan->nEq;
104005  int i, j;
104006  Column *aCol = pTab->aCol;
104007  int *aiColumn = pIndex->aiColumn;
104008  StrAccum txt;
104009
104010  if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
104011    return 0;
104012  }
104013  sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
104014  txt.db = db;
104015  sqlite3StrAccumAppend(&txt, " (", 2);
104016  for(i=0; i<nEq; i++){
104017    explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
104018  }
104019
104020  j = i;
104021  if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
104022    explainAppendTerm(&txt, i++, aCol[aiColumn[j]].zName, ">");
104023  }
104024  if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
104025    explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<");
104026  }
104027  sqlite3StrAccumAppend(&txt, ")", 1);
104028  return sqlite3StrAccumFinish(&txt);
104029}
104030
104031/*
104032** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
104033** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
104034** record is added to the output to describe the table scan strategy in
104035** pLevel.
104036*/
104037static void explainOneScan(
104038  Parse *pParse,                  /* Parse context */
104039  SrcList *pTabList,              /* Table list this loop refers to */
104040  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
104041  int iLevel,                     /* Value for "level" column of output */
104042  int iFrom,                      /* Value for "from" column of output */
104043  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
104044){
104045  if( pParse->explain==2 ){
104046    u32 flags = pLevel->plan.wsFlags;
104047    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
104048    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
104049    sqlite3 *db = pParse->db;     /* Database handle */
104050    char *zMsg;                   /* Text to add to EQP output */
104051    sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
104052    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
104053    int isSearch;                 /* True for a SEARCH. False for SCAN. */
104054
104055    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
104056
104057    isSearch = (pLevel->plan.nEq>0)
104058             || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
104059             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
104060
104061    zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
104062    if( pItem->pSelect ){
104063      zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
104064    }else{
104065      zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
104066    }
104067
104068    if( pItem->zAlias ){
104069      zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
104070    }
104071    if( (flags & WHERE_INDEXED)!=0 ){
104072      char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
104073      zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg,
104074          ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
104075          ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
104076          ((flags & WHERE_TEMP_INDEX)?"":" "),
104077          ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
104078          zWhere
104079      );
104080      sqlite3DbFree(db, zWhere);
104081    }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
104082      zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
104083
104084      if( flags&WHERE_ROWID_EQ ){
104085        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
104086      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
104087        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
104088      }else if( flags&WHERE_BTM_LIMIT ){
104089        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
104090      }else if( flags&WHERE_TOP_LIMIT ){
104091        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
104092      }
104093    }
104094#ifndef SQLITE_OMIT_VIRTUALTABLE
104095    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
104096      sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
104097      zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
104098                  pVtabIdx->idxNum, pVtabIdx->idxStr);
104099    }
104100#endif
104101    if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
104102      testcase( wctrlFlags & WHERE_ORDERBY_MIN );
104103      nRow = 1;
104104    }else{
104105      nRow = (sqlite3_int64)pLevel->plan.nRow;
104106    }
104107    zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
104108    sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
104109  }
104110}
104111#else
104112# define explainOneScan(u,v,w,x,y,z)
104113#endif /* SQLITE_OMIT_EXPLAIN */
104114
104115
104116/*
104117** Generate code for the start of the iLevel-th loop in the WHERE clause
104118** implementation described by pWInfo.
104119*/
104120static Bitmask codeOneLoopStart(
104121  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
104122  int iLevel,          /* Which level of pWInfo->a[] should be coded */
104123  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
104124  Bitmask notReady     /* Which tables are currently available */
104125){
104126  int j, k;            /* Loop counters */
104127  int iCur;            /* The VDBE cursor for the table */
104128  int addrNxt;         /* Where to jump to continue with the next IN case */
104129  int omitTable;       /* True if we use the index only */
104130  int bRev;            /* True if we need to scan in reverse order */
104131  WhereLevel *pLevel;  /* The where level to be coded */
104132  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
104133  WhereTerm *pTerm;               /* A WHERE clause term */
104134  Parse *pParse;                  /* Parsing context */
104135  Vdbe *v;                        /* The prepared stmt under constructions */
104136  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
104137  int addrBrk;                    /* Jump here to break out of the loop */
104138  int addrCont;                   /* Jump here to continue with next cycle */
104139  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
104140  int iReleaseReg = 0;      /* Temp register to free before returning */
104141
104142  pParse = pWInfo->pParse;
104143  v = pParse->pVdbe;
104144  pWC = pWInfo->pWC;
104145  pLevel = &pWInfo->a[iLevel];
104146  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
104147  iCur = pTabItem->iCursor;
104148  bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
104149  omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
104150           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
104151
104152  /* Create labels for the "break" and "continue" instructions
104153  ** for the current loop.  Jump to addrBrk to break out of a loop.
104154  ** Jump to cont to go immediately to the next iteration of the
104155  ** loop.
104156  **
104157  ** When there is an IN operator, we also have a "addrNxt" label that
104158  ** means to continue with the next IN value combination.  When
104159  ** there are no IN operators in the constraints, the "addrNxt" label
104160  ** is the same as "addrBrk".
104161  */
104162  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
104163  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
104164
104165  /* If this is the right table of a LEFT OUTER JOIN, allocate and
104166  ** initialize a memory cell that records if this table matches any
104167  ** row of the left table of the join.
104168  */
104169  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
104170    pLevel->iLeftJoin = ++pParse->nMem;
104171    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
104172    VdbeComment((v, "init LEFT JOIN no-match flag"));
104173  }
104174
104175#ifndef SQLITE_OMIT_VIRTUALTABLE
104176  if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
104177    /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
104178    **          to access the data.
104179    */
104180    int iReg;   /* P3 Value for OP_VFilter */
104181    sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
104182    int nConstraint = pVtabIdx->nConstraint;
104183    struct sqlite3_index_constraint_usage *aUsage =
104184                                                pVtabIdx->aConstraintUsage;
104185    const struct sqlite3_index_constraint *aConstraint =
104186                                                pVtabIdx->aConstraint;
104187
104188    sqlite3ExprCachePush(pParse);
104189    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
104190    for(j=1; j<=nConstraint; j++){
104191      for(k=0; k<nConstraint; k++){
104192        if( aUsage[k].argvIndex==j ){
104193          int iTerm = aConstraint[k].iTermOffset;
104194          sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
104195          break;
104196        }
104197      }
104198      if( k==nConstraint ) break;
104199    }
104200    sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
104201    sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
104202    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
104203                      pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
104204    pVtabIdx->needToFreeIdxStr = 0;
104205    for(j=0; j<nConstraint; j++){
104206      if( aUsage[j].omit ){
104207        int iTerm = aConstraint[j].iTermOffset;
104208        disableTerm(pLevel, &pWC->a[iTerm]);
104209      }
104210    }
104211    pLevel->op = OP_VNext;
104212    pLevel->p1 = iCur;
104213    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
104214    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
104215    sqlite3ExprCachePop(pParse, 1);
104216  }else
104217#endif /* SQLITE_OMIT_VIRTUALTABLE */
104218
104219  if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
104220    /* Case 1:  We can directly reference a single row using an
104221    **          equality comparison against the ROWID field.  Or
104222    **          we reference multiple rows using a "rowid IN (...)"
104223    **          construct.
104224    */
104225    iReleaseReg = sqlite3GetTempReg(pParse);
104226    pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
104227    assert( pTerm!=0 );
104228    assert( pTerm->pExpr!=0 );
104229    assert( pTerm->leftCursor==iCur );
104230    assert( omitTable==0 );
104231    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
104232    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
104233    addrNxt = pLevel->addrNxt;
104234    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
104235    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
104236    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
104237    VdbeComment((v, "pk"));
104238    pLevel->op = OP_Noop;
104239  }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
104240    /* Case 2:  We have an inequality comparison against the ROWID field.
104241    */
104242    int testOp = OP_Noop;
104243    int start;
104244    int memEndValue = 0;
104245    WhereTerm *pStart, *pEnd;
104246
104247    assert( omitTable==0 );
104248    pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
104249    pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
104250    if( bRev ){
104251      pTerm = pStart;
104252      pStart = pEnd;
104253      pEnd = pTerm;
104254    }
104255    if( pStart ){
104256      Expr *pX;             /* The expression that defines the start bound */
104257      int r1, rTemp;        /* Registers for holding the start boundary */
104258
104259      /* The following constant maps TK_xx codes into corresponding
104260      ** seek opcodes.  It depends on a particular ordering of TK_xx
104261      */
104262      const u8 aMoveOp[] = {
104263           /* TK_GT */  OP_SeekGt,
104264           /* TK_LE */  OP_SeekLe,
104265           /* TK_LT */  OP_SeekLt,
104266           /* TK_GE */  OP_SeekGe
104267      };
104268      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
104269      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
104270      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
104271
104272      testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
104273      pX = pStart->pExpr;
104274      assert( pX!=0 );
104275      assert( pStart->leftCursor==iCur );
104276      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
104277      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
104278      VdbeComment((v, "pk"));
104279      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
104280      sqlite3ReleaseTempReg(pParse, rTemp);
104281      disableTerm(pLevel, pStart);
104282    }else{
104283      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
104284    }
104285    if( pEnd ){
104286      Expr *pX;
104287      pX = pEnd->pExpr;
104288      assert( pX!=0 );
104289      assert( pEnd->leftCursor==iCur );
104290      testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
104291      memEndValue = ++pParse->nMem;
104292      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
104293      if( pX->op==TK_LT || pX->op==TK_GT ){
104294        testOp = bRev ? OP_Le : OP_Ge;
104295      }else{
104296        testOp = bRev ? OP_Lt : OP_Gt;
104297      }
104298      disableTerm(pLevel, pEnd);
104299    }
104300    start = sqlite3VdbeCurrentAddr(v);
104301    pLevel->op = bRev ? OP_Prev : OP_Next;
104302    pLevel->p1 = iCur;
104303    pLevel->p2 = start;
104304    if( pStart==0 && pEnd==0 ){
104305      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
104306    }else{
104307      assert( pLevel->p5==0 );
104308    }
104309    if( testOp!=OP_Noop ){
104310      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
104311      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
104312      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
104313      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
104314      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
104315    }
104316  }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
104317    /* Case 3: A scan using an index.
104318    **
104319    **         The WHERE clause may contain zero or more equality
104320    **         terms ("==" or "IN" operators) that refer to the N
104321    **         left-most columns of the index. It may also contain
104322    **         inequality constraints (>, <, >= or <=) on the indexed
104323    **         column that immediately follows the N equalities. Only
104324    **         the right-most column can be an inequality - the rest must
104325    **         use the "==" and "IN" operators. For example, if the
104326    **         index is on (x,y,z), then the following clauses are all
104327    **         optimized:
104328    **
104329    **            x=5
104330    **            x=5 AND y=10
104331    **            x=5 AND y<10
104332    **            x=5 AND y>5 AND y<10
104333    **            x=5 AND y=5 AND z<=10
104334    **
104335    **         The z<10 term of the following cannot be used, only
104336    **         the x=5 term:
104337    **
104338    **            x=5 AND z<10
104339    **
104340    **         N may be zero if there are inequality constraints.
104341    **         If there are no inequality constraints, then N is at
104342    **         least one.
104343    **
104344    **         This case is also used when there are no WHERE clause
104345    **         constraints but an index is selected anyway, in order
104346    **         to force the output order to conform to an ORDER BY.
104347    */
104348    static const u8 aStartOp[] = {
104349      0,
104350      0,
104351      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
104352      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
104353      OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
104354      OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
104355      OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
104356      OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
104357    };
104358    static const u8 aEndOp[] = {
104359      OP_Noop,             /* 0: (!end_constraints) */
104360      OP_IdxGE,            /* 1: (end_constraints && !bRev) */
104361      OP_IdxLT             /* 2: (end_constraints && bRev) */
104362    };
104363    int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
104364    int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
104365    int regBase;                 /* Base register holding constraint values */
104366    int r1;                      /* Temp register */
104367    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
104368    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
104369    int startEq;                 /* True if range start uses ==, >= or <= */
104370    int endEq;                   /* True if range end uses ==, >= or <= */
104371    int start_constraints;       /* Start of range is constrained */
104372    int nConstraint;             /* Number of constraint terms */
104373    Index *pIdx;                 /* The index we will be using */
104374    int iIdxCur;                 /* The VDBE cursor for the index */
104375    int nExtraReg = 0;           /* Number of extra registers needed */
104376    int op;                      /* Instruction opcode */
104377    char *zStartAff;             /* Affinity for start of range constraint */
104378    char *zEndAff;               /* Affinity for end of range constraint */
104379
104380    pIdx = pLevel->plan.u.pIdx;
104381    iIdxCur = pLevel->iIdxCur;
104382    k = pIdx->aiColumn[nEq];     /* Column for inequality constraints */
104383
104384    /* If this loop satisfies a sort order (pOrderBy) request that
104385    ** was passed to this function to implement a "SELECT min(x) ..."
104386    ** query, then the caller will only allow the loop to run for
104387    ** a single iteration. This means that the first row returned
104388    ** should not have a NULL value stored in 'x'. If column 'x' is
104389    ** the first one after the nEq equality constraints in the index,
104390    ** this requires some special handling.
104391    */
104392    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
104393     && (pLevel->plan.wsFlags&WHERE_ORDERBY)
104394     && (pIdx->nColumn>nEq)
104395    ){
104396      /* assert( pOrderBy->nExpr==1 ); */
104397      /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
104398      isMinQuery = 1;
104399      nExtraReg = 1;
104400    }
104401
104402    /* Find any inequality constraint terms for the start and end
104403    ** of the range.
104404    */
104405    if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
104406      pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
104407      nExtraReg = 1;
104408    }
104409    if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
104410      pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
104411      nExtraReg = 1;
104412    }
104413
104414    /* Generate code to evaluate all constraint terms using == or IN
104415    ** and store the values of those terms in an array of registers
104416    ** starting at regBase.
104417    */
104418    regBase = codeAllEqualityTerms(
104419        pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
104420    );
104421    zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
104422    addrNxt = pLevel->addrNxt;
104423
104424    /* If we are doing a reverse order scan on an ascending index, or
104425    ** a forward order scan on a descending index, interchange the
104426    ** start and end terms (pRangeStart and pRangeEnd).
104427    */
104428    if( nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
104429      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
104430    }
104431
104432    testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
104433    testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
104434    testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
104435    testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
104436    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
104437    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
104438    start_constraints = pRangeStart || nEq>0;
104439
104440    /* Seek the index cursor to the start of the range. */
104441    nConstraint = nEq;
104442    if( pRangeStart ){
104443      Expr *pRight = pRangeStart->pExpr->pRight;
104444      sqlite3ExprCode(pParse, pRight, regBase+nEq);
104445      if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
104446        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
104447      }
104448      if( zStartAff ){
104449        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
104450          /* Since the comparison is to be performed with no conversions
104451          ** applied to the operands, set the affinity to apply to pRight to
104452          ** SQLITE_AFF_NONE.  */
104453          zStartAff[nEq] = SQLITE_AFF_NONE;
104454        }
104455        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
104456          zStartAff[nEq] = SQLITE_AFF_NONE;
104457        }
104458      }
104459      nConstraint++;
104460      testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
104461    }else if( isMinQuery ){
104462      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
104463      nConstraint++;
104464      startEq = 0;
104465      start_constraints = 1;
104466    }
104467    codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
104468    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
104469    assert( op!=0 );
104470    testcase( op==OP_Rewind );
104471    testcase( op==OP_Last );
104472    testcase( op==OP_SeekGt );
104473    testcase( op==OP_SeekGe );
104474    testcase( op==OP_SeekLe );
104475    testcase( op==OP_SeekLt );
104476    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
104477
104478    /* Load the value for the inequality constraint at the end of the
104479    ** range (if any).
104480    */
104481    nConstraint = nEq;
104482    if( pRangeEnd ){
104483      Expr *pRight = pRangeEnd->pExpr->pRight;
104484      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
104485      sqlite3ExprCode(pParse, pRight, regBase+nEq);
104486      if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
104487        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
104488      }
104489      if( zEndAff ){
104490        if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
104491          /* Since the comparison is to be performed with no conversions
104492          ** applied to the operands, set the affinity to apply to pRight to
104493          ** SQLITE_AFF_NONE.  */
104494          zEndAff[nEq] = SQLITE_AFF_NONE;
104495        }
104496        if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
104497          zEndAff[nEq] = SQLITE_AFF_NONE;
104498        }
104499      }
104500      codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
104501      nConstraint++;
104502      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
104503    }
104504    sqlite3DbFree(pParse->db, zStartAff);
104505    sqlite3DbFree(pParse->db, zEndAff);
104506
104507    /* Top of the loop body */
104508    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
104509
104510    /* Check if the index cursor is past the end of the range. */
104511    op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
104512    testcase( op==OP_Noop );
104513    testcase( op==OP_IdxGE );
104514    testcase( op==OP_IdxLT );
104515    if( op!=OP_Noop ){
104516      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
104517      sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
104518    }
104519
104520    /* If there are inequality constraints, check that the value
104521    ** of the table column that the inequality contrains is not NULL.
104522    ** If it is, jump to the next iteration of the loop.
104523    */
104524    r1 = sqlite3GetTempReg(pParse);
104525    testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
104526    testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
104527    if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
104528      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
104529      sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
104530    }
104531    sqlite3ReleaseTempReg(pParse, r1);
104532
104533    /* Seek the table cursor, if required */
104534    disableTerm(pLevel, pRangeStart);
104535    disableTerm(pLevel, pRangeEnd);
104536    if( !omitTable ){
104537      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
104538      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
104539      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
104540      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
104541    }
104542
104543    /* Record the instruction used to terminate the loop. Disable
104544    ** WHERE clause terms made redundant by the index range scan.
104545    */
104546    if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
104547      pLevel->op = OP_Noop;
104548    }else if( bRev ){
104549      pLevel->op = OP_Prev;
104550    }else{
104551      pLevel->op = OP_Next;
104552    }
104553    pLevel->p1 = iIdxCur;
104554  }else
104555
104556#ifndef SQLITE_OMIT_OR_OPTIMIZATION
104557  if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
104558    /* Case 4:  Two or more separately indexed terms connected by OR
104559    **
104560    ** Example:
104561    **
104562    **   CREATE TABLE t1(a,b,c,d);
104563    **   CREATE INDEX i1 ON t1(a);
104564    **   CREATE INDEX i2 ON t1(b);
104565    **   CREATE INDEX i3 ON t1(c);
104566    **
104567    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
104568    **
104569    ** In the example, there are three indexed terms connected by OR.
104570    ** The top of the loop looks like this:
104571    **
104572    **          Null       1                # Zero the rowset in reg 1
104573    **
104574    ** Then, for each indexed term, the following. The arguments to
104575    ** RowSetTest are such that the rowid of the current row is inserted
104576    ** into the RowSet. If it is already present, control skips the
104577    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
104578    **
104579    **        sqlite3WhereBegin(<term>)
104580    **          RowSetTest                  # Insert rowid into rowset
104581    **          Gosub      2 A
104582    **        sqlite3WhereEnd()
104583    **
104584    ** Following the above, code to terminate the loop. Label A, the target
104585    ** of the Gosub above, jumps to the instruction right after the Goto.
104586    **
104587    **          Null       1                # Zero the rowset in reg 1
104588    **          Goto       B                # The loop is finished.
104589    **
104590    **       A: <loop body>                 # Return data, whatever.
104591    **
104592    **          Return     2                # Jump back to the Gosub
104593    **
104594    **       B: <after the loop>
104595    **
104596    */
104597    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
104598    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
104599
104600    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
104601    int regRowset = 0;                        /* Register for RowSet object */
104602    int regRowid = 0;                         /* Register holding rowid */
104603    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
104604    int iRetInit;                             /* Address of regReturn init */
104605    int untestedTerms = 0;             /* Some terms not completely tested */
104606    int ii;
104607
104608    pTerm = pLevel->plan.u.pTerm;
104609    assert( pTerm!=0 );
104610    assert( pTerm->eOperator==WO_OR );
104611    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
104612    pOrWc = &pTerm->u.pOrInfo->wc;
104613    pLevel->op = OP_Return;
104614    pLevel->p1 = regReturn;
104615
104616    /* Set up a new SrcList ni pOrTab containing the table being scanned
104617    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
104618    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
104619    */
104620    if( pWInfo->nLevel>1 ){
104621      int nNotReady;                 /* The number of notReady tables */
104622      struct SrcList_item *origSrc;     /* Original list of tables */
104623      nNotReady = pWInfo->nLevel - iLevel - 1;
104624      pOrTab = sqlite3StackAllocRaw(pParse->db,
104625                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
104626      if( pOrTab==0 ) return notReady;
104627      pOrTab->nAlloc = (i16)(nNotReady + 1);
104628      pOrTab->nSrc = pOrTab->nAlloc;
104629      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
104630      origSrc = pWInfo->pTabList->a;
104631      for(k=1; k<=nNotReady; k++){
104632        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
104633      }
104634    }else{
104635      pOrTab = pWInfo->pTabList;
104636    }
104637
104638    /* Initialize the rowset register to contain NULL. An SQL NULL is
104639    ** equivalent to an empty rowset.
104640    **
104641    ** Also initialize regReturn to contain the address of the instruction
104642    ** immediately following the OP_Return at the bottom of the loop. This
104643    ** is required in a few obscure LEFT JOIN cases where control jumps
104644    ** over the top of the loop into the body of it. In this case the
104645    ** correct response for the end-of-loop code (the OP_Return) is to
104646    ** fall through to the next instruction, just as an OP_Next does if
104647    ** called on an uninitialized cursor.
104648    */
104649    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
104650      regRowset = ++pParse->nMem;
104651      regRowid = ++pParse->nMem;
104652      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
104653    }
104654    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
104655
104656    for(ii=0; ii<pOrWc->nTerm; ii++){
104657      WhereTerm *pOrTerm = &pOrWc->a[ii];
104658      if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
104659        WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
104660        /* Loop through table entries that match term pOrTerm. */
104661        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0,
104662                        WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
104663                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
104664        if( pSubWInfo ){
104665          explainOneScan(
104666              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
104667          );
104668          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
104669            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
104670            int r;
104671            r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
104672                                         regRowid);
104673            sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
104674                                 sqlite3VdbeCurrentAddr(v)+2, r, iSet);
104675          }
104676          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
104677
104678          /* The pSubWInfo->untestedTerms flag means that this OR term
104679          ** contained one or more AND term from a notReady table.  The
104680          ** terms from the notReady table could not be tested and will
104681          ** need to be tested later.
104682          */
104683          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
104684
104685          /* Finish the loop through table entries that match term pOrTerm. */
104686          sqlite3WhereEnd(pSubWInfo);
104687        }
104688      }
104689    }
104690    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
104691    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
104692    sqlite3VdbeResolveLabel(v, iLoopBody);
104693
104694    if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
104695    if( !untestedTerms ) disableTerm(pLevel, pTerm);
104696  }else
104697#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
104698
104699  {
104700    /* Case 5:  There is no usable index.  We must do a complete
104701    **          scan of the entire table.
104702    */
104703    static const u8 aStep[] = { OP_Next, OP_Prev };
104704    static const u8 aStart[] = { OP_Rewind, OP_Last };
104705    assert( bRev==0 || bRev==1 );
104706    assert( omitTable==0 );
104707    pLevel->op = aStep[bRev];
104708    pLevel->p1 = iCur;
104709    pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
104710    pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
104711  }
104712  notReady &= ~getMask(pWC->pMaskSet, iCur);
104713
104714  /* Insert code to test every subexpression that can be completely
104715  ** computed using the current set of tables.
104716  **
104717  ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
104718  ** the use of indices become tests that are evaluated against each row of
104719  ** the relevant input tables.
104720  */
104721  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
104722    Expr *pE;
104723    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
104724    testcase( pTerm->wtFlags & TERM_CODED );
104725    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
104726    if( (pTerm->prereqAll & notReady)!=0 ){
104727      testcase( pWInfo->untestedTerms==0
104728               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
104729      pWInfo->untestedTerms = 1;
104730      continue;
104731    }
104732    pE = pTerm->pExpr;
104733    assert( pE!=0 );
104734    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
104735      continue;
104736    }
104737    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
104738    pTerm->wtFlags |= TERM_CODED;
104739  }
104740
104741  /* For a LEFT OUTER JOIN, generate code that will record the fact that
104742  ** at least one row of the right table has matched the left table.
104743  */
104744  if( pLevel->iLeftJoin ){
104745    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
104746    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
104747    VdbeComment((v, "record LEFT JOIN hit"));
104748    sqlite3ExprCacheClear(pParse);
104749    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
104750      testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
104751      testcase( pTerm->wtFlags & TERM_CODED );
104752      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
104753      if( (pTerm->prereqAll & notReady)!=0 ){
104754        assert( pWInfo->untestedTerms );
104755        continue;
104756      }
104757      assert( pTerm->pExpr );
104758      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
104759      pTerm->wtFlags |= TERM_CODED;
104760    }
104761  }
104762  sqlite3ReleaseTempReg(pParse, iReleaseReg);
104763
104764  return notReady;
104765}
104766
104767#if defined(SQLITE_TEST)
104768/*
104769** The following variable holds a text description of query plan generated
104770** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
104771** overwrites the previous.  This information is used for testing and
104772** analysis only.
104773*/
104774SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
104775static int nQPlan = 0;              /* Next free slow in _query_plan[] */
104776
104777#endif /* SQLITE_TEST */
104778
104779
104780/*
104781** Free a WhereInfo structure
104782*/
104783static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
104784  if( ALWAYS(pWInfo) ){
104785    int i;
104786    for(i=0; i<pWInfo->nLevel; i++){
104787      sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
104788      if( pInfo ){
104789        /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
104790        if( pInfo->needToFreeIdxStr ){
104791          sqlite3_free(pInfo->idxStr);
104792        }
104793        sqlite3DbFree(db, pInfo);
104794      }
104795      if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
104796        Index *pIdx = pWInfo->a[i].plan.u.pIdx;
104797        if( pIdx ){
104798          sqlite3DbFree(db, pIdx->zColAff);
104799          sqlite3DbFree(db, pIdx);
104800        }
104801      }
104802    }
104803    whereClauseClear(pWInfo->pWC);
104804    sqlite3DbFree(db, pWInfo);
104805  }
104806}
104807
104808
104809/*
104810** Generate the beginning of the loop used for WHERE clause processing.
104811** The return value is a pointer to an opaque structure that contains
104812** information needed to terminate the loop.  Later, the calling routine
104813** should invoke sqlite3WhereEnd() with the return value of this function
104814** in order to complete the WHERE clause processing.
104815**
104816** If an error occurs, this routine returns NULL.
104817**
104818** The basic idea is to do a nested loop, one loop for each table in
104819** the FROM clause of a select.  (INSERT and UPDATE statements are the
104820** same as a SELECT with only a single table in the FROM clause.)  For
104821** example, if the SQL is this:
104822**
104823**       SELECT * FROM t1, t2, t3 WHERE ...;
104824**
104825** Then the code generated is conceptually like the following:
104826**
104827**      foreach row1 in t1 do       \    Code generated
104828**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
104829**          foreach row3 in t3 do   /
104830**            ...
104831**          end                     \    Code generated
104832**        end                        |-- by sqlite3WhereEnd()
104833**      end                         /
104834**
104835** Note that the loops might not be nested in the order in which they
104836** appear in the FROM clause if a different order is better able to make
104837** use of indices.  Note also that when the IN operator appears in
104838** the WHERE clause, it might result in additional nested loops for
104839** scanning through all values on the right-hand side of the IN.
104840**
104841** There are Btree cursors associated with each table.  t1 uses cursor
104842** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
104843** And so forth.  This routine generates code to open those VDBE cursors
104844** and sqlite3WhereEnd() generates the code to close them.
104845**
104846** The code that sqlite3WhereBegin() generates leaves the cursors named
104847** in pTabList pointing at their appropriate entries.  The [...] code
104848** can use OP_Column and OP_Rowid opcodes on these cursors to extract
104849** data from the various tables of the loop.
104850**
104851** If the WHERE clause is empty, the foreach loops must each scan their
104852** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
104853** the tables have indices and there are terms in the WHERE clause that
104854** refer to those indices, a complete table scan can be avoided and the
104855** code will run much faster.  Most of the work of this routine is checking
104856** to see if there are indices that can be used to speed up the loop.
104857**
104858** Terms of the WHERE clause are also used to limit which rows actually
104859** make it to the "..." in the middle of the loop.  After each "foreach",
104860** terms of the WHERE clause that use only terms in that loop and outer
104861** loops are evaluated and if false a jump is made around all subsequent
104862** inner loops (or around the "..." if the test occurs within the inner-
104863** most loop)
104864**
104865** OUTER JOINS
104866**
104867** An outer join of tables t1 and t2 is conceptally coded as follows:
104868**
104869**    foreach row1 in t1 do
104870**      flag = 0
104871**      foreach row2 in t2 do
104872**        start:
104873**          ...
104874**          flag = 1
104875**      end
104876**      if flag==0 then
104877**        move the row2 cursor to a null row
104878**        goto start
104879**      fi
104880**    end
104881**
104882** ORDER BY CLAUSE PROCESSING
104883**
104884** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
104885** if there is one.  If there is no ORDER BY clause or if this routine
104886** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
104887**
104888** If an index can be used so that the natural output order of the table
104889** scan is correct for the ORDER BY clause, then that index is used and
104890** *ppOrderBy is set to NULL.  This is an optimization that prevents an
104891** unnecessary sort of the result set if an index appropriate for the
104892** ORDER BY clause already exists.
104893**
104894** If the where clause loops cannot be arranged to provide the correct
104895** output order, then the *ppOrderBy is unchanged.
104896*/
104897SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
104898  Parse *pParse,        /* The parser context */
104899  SrcList *pTabList,    /* A list of all tables to be scanned */
104900  Expr *pWhere,         /* The WHERE clause */
104901  ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
104902  ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
104903  u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
104904){
104905  int i;                     /* Loop counter */
104906  int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
104907  int nTabList;              /* Number of elements in pTabList */
104908  WhereInfo *pWInfo;         /* Will become the return value of this function */
104909  Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
104910  Bitmask notReady;          /* Cursors that are not yet positioned */
104911  WhereMaskSet *pMaskSet;    /* The expression mask set */
104912  WhereClause *pWC;               /* Decomposition of the WHERE clause */
104913  struct SrcList_item *pTabItem;  /* A single entry from pTabList */
104914  WhereLevel *pLevel;             /* A single level in the pWInfo list */
104915  int iFrom;                      /* First unused FROM clause element */
104916  int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
104917  sqlite3 *db;               /* Database connection */
104918
104919  /* The number of tables in the FROM clause is limited by the number of
104920  ** bits in a Bitmask
104921  */
104922  testcase( pTabList->nSrc==BMS );
104923  if( pTabList->nSrc>BMS ){
104924    sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
104925    return 0;
104926  }
104927
104928  /* This function normally generates a nested loop for all tables in
104929  ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
104930  ** only generate code for the first table in pTabList and assume that
104931  ** any cursors associated with subsequent tables are uninitialized.
104932  */
104933  nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
104934
104935  /* Allocate and initialize the WhereInfo structure that will become the
104936  ** return value. A single allocation is used to store the WhereInfo
104937  ** struct, the contents of WhereInfo.a[], the WhereClause structure
104938  ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
104939  ** field (type Bitmask) it must be aligned on an 8-byte boundary on
104940  ** some architectures. Hence the ROUND8() below.
104941  */
104942  db = pParse->db;
104943  nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
104944  pWInfo = sqlite3DbMallocZero(db,
104945      nByteWInfo +
104946      sizeof(WhereClause) +
104947      sizeof(WhereMaskSet)
104948  );
104949  if( db->mallocFailed ){
104950    sqlite3DbFree(db, pWInfo);
104951    pWInfo = 0;
104952    goto whereBeginError;
104953  }
104954  pWInfo->nLevel = nTabList;
104955  pWInfo->pParse = pParse;
104956  pWInfo->pTabList = pTabList;
104957  pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
104958  pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
104959  pWInfo->wctrlFlags = wctrlFlags;
104960  pWInfo->savedNQueryLoop = pParse->nQueryLoop;
104961  pMaskSet = (WhereMaskSet*)&pWC[1];
104962
104963  /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
104964  ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
104965  if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
104966
104967  /* Split the WHERE clause into separate subexpressions where each
104968  ** subexpression is separated by an AND operator.
104969  */
104970  initMaskSet(pMaskSet);
104971  whereClauseInit(pWC, pParse, pMaskSet);
104972  sqlite3ExprCodeConstants(pParse, pWhere);
104973  whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
104974
104975  /* Special case: a WHERE clause that is constant.  Evaluate the
104976  ** expression and either jump over all of the code or fall thru.
104977  */
104978  if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
104979    sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
104980    pWhere = 0;
104981  }
104982
104983  /* Assign a bit from the bitmask to every term in the FROM clause.
104984  **
104985  ** When assigning bitmask values to FROM clause cursors, it must be
104986  ** the case that if X is the bitmask for the N-th FROM clause term then
104987  ** the bitmask for all FROM clause terms to the left of the N-th term
104988  ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
104989  ** its Expr.iRightJoinTable value to find the bitmask of the right table
104990  ** of the join.  Subtracting one from the right table bitmask gives a
104991  ** bitmask for all tables to the left of the join.  Knowing the bitmask
104992  ** for all tables to the left of a left join is important.  Ticket #3015.
104993  **
104994  ** Configure the WhereClause.vmask variable so that bits that correspond
104995  ** to virtual table cursors are set. This is used to selectively disable
104996  ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful
104997  ** with virtual tables.
104998  **
104999  ** Note that bitmasks are created for all pTabList->nSrc tables in
105000  ** pTabList, not just the first nTabList tables.  nTabList is normally
105001  ** equal to pTabList->nSrc but might be shortened to 1 if the
105002  ** WHERE_ONETABLE_ONLY flag is set.
105003  */
105004  assert( pWC->vmask==0 && pMaskSet->n==0 );
105005  for(i=0; i<pTabList->nSrc; i++){
105006    createMask(pMaskSet, pTabList->a[i].iCursor);
105007#ifndef SQLITE_OMIT_VIRTUALTABLE
105008    if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
105009      pWC->vmask |= ((Bitmask)1 << i);
105010    }
105011#endif
105012  }
105013#ifndef NDEBUG
105014  {
105015    Bitmask toTheLeft = 0;
105016    for(i=0; i<pTabList->nSrc; i++){
105017      Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
105018      assert( (m-1)==toTheLeft );
105019      toTheLeft |= m;
105020    }
105021  }
105022#endif
105023
105024  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
105025  ** add new virtual terms onto the end of the WHERE clause.  We do not
105026  ** want to analyze these virtual terms, so start analyzing at the end
105027  ** and work forward so that the added virtual terms are never processed.
105028  */
105029  exprAnalyzeAll(pTabList, pWC);
105030  if( db->mallocFailed ){
105031    goto whereBeginError;
105032  }
105033
105034  /* Check if the DISTINCT qualifier, if there is one, is redundant.
105035  ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
105036  ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
105037  */
105038  if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
105039    pDistinct = 0;
105040    pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
105041  }
105042
105043  /* Chose the best index to use for each table in the FROM clause.
105044  **
105045  ** This loop fills in the following fields:
105046  **
105047  **   pWInfo->a[].pIdx      The index to use for this level of the loop.
105048  **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
105049  **   pWInfo->a[].nEq       The number of == and IN constraints
105050  **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
105051  **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
105052  **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
105053  **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
105054  **
105055  ** This loop also figures out the nesting order of tables in the FROM
105056  ** clause.
105057  */
105058  notReady = ~(Bitmask)0;
105059  andFlags = ~0;
105060  WHERETRACE(("*** Optimizer Start ***\n"));
105061  for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
105062    WhereCost bestPlan;         /* Most efficient plan seen so far */
105063    Index *pIdx;                /* Index for FROM table at pTabItem */
105064    int j;                      /* For looping over FROM tables */
105065    int bestJ = -1;             /* The value of j */
105066    Bitmask m;                  /* Bitmask value for j or bestJ */
105067    int isOptimal;              /* Iterator for optimal/non-optimal search */
105068    int nUnconstrained;         /* Number tables without INDEXED BY */
105069    Bitmask notIndexed;         /* Mask of tables that cannot use an index */
105070
105071    memset(&bestPlan, 0, sizeof(bestPlan));
105072    bestPlan.rCost = SQLITE_BIG_DBL;
105073    WHERETRACE(("*** Begin search for loop %d ***\n", i));
105074
105075    /* Loop through the remaining entries in the FROM clause to find the
105076    ** next nested loop. The loop tests all FROM clause entries
105077    ** either once or twice.
105078    **
105079    ** The first test is always performed if there are two or more entries
105080    ** remaining and never performed if there is only one FROM clause entry
105081    ** to choose from.  The first test looks for an "optimal" scan.  In
105082    ** this context an optimal scan is one that uses the same strategy
105083    ** for the given FROM clause entry as would be selected if the entry
105084    ** were used as the innermost nested loop.  In other words, a table
105085    ** is chosen such that the cost of running that table cannot be reduced
105086    ** by waiting for other tables to run first.  This "optimal" test works
105087    ** by first assuming that the FROM clause is on the inner loop and finding
105088    ** its query plan, then checking to see if that query plan uses any
105089    ** other FROM clause terms that are notReady.  If no notReady terms are
105090    ** used then the "optimal" query plan works.
105091    **
105092    ** Note that the WhereCost.nRow parameter for an optimal scan might
105093    ** not be as small as it would be if the table really were the innermost
105094    ** join.  The nRow value can be reduced by WHERE clause constraints
105095    ** that do not use indices.  But this nRow reduction only happens if the
105096    ** table really is the innermost join.
105097    **
105098    ** The second loop iteration is only performed if no optimal scan
105099    ** strategies were found by the first iteration. This second iteration
105100    ** is used to search for the lowest cost scan overall.
105101    **
105102    ** Previous versions of SQLite performed only the second iteration -
105103    ** the next outermost loop was always that with the lowest overall
105104    ** cost. However, this meant that SQLite could select the wrong plan
105105    ** for scripts such as the following:
105106    **
105107    **   CREATE TABLE t1(a, b);
105108    **   CREATE TABLE t2(c, d);
105109    **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
105110    **
105111    ** The best strategy is to iterate through table t1 first. However it
105112    ** is not possible to determine this with a simple greedy algorithm.
105113    ** Since the cost of a linear scan through table t2 is the same
105114    ** as the cost of a linear scan through table t1, a simple greedy
105115    ** algorithm may choose to use t2 for the outer loop, which is a much
105116    ** costlier approach.
105117    */
105118    nUnconstrained = 0;
105119    notIndexed = 0;
105120    for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
105121      Bitmask mask;             /* Mask of tables not yet ready */
105122      for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
105123        int doNotReorder;    /* True if this table should not be reordered */
105124        WhereCost sCost;     /* Cost information from best[Virtual]Index() */
105125        ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
105126        ExprList *pDist;     /* DISTINCT clause for index to optimize */
105127
105128        doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
105129        if( j!=iFrom && doNotReorder ) break;
105130        m = getMask(pMaskSet, pTabItem->iCursor);
105131        if( (m & notReady)==0 ){
105132          if( j==iFrom ) iFrom++;
105133          continue;
105134        }
105135        mask = (isOptimal ? m : notReady);
105136        pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
105137        pDist = (i==0 ? pDistinct : 0);
105138        if( pTabItem->pIndex==0 ) nUnconstrained++;
105139
105140        WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
105141                    j, isOptimal));
105142        assert( pTabItem->pTab );
105143#ifndef SQLITE_OMIT_VIRTUALTABLE
105144        if( IsVirtual(pTabItem->pTab) ){
105145          sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
105146          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
105147                           &sCost, pp);
105148        }else
105149#endif
105150        {
105151          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
105152              pDist, &sCost);
105153        }
105154        assert( isOptimal || (sCost.used&notReady)==0 );
105155
105156        /* If an INDEXED BY clause is present, then the plan must use that
105157        ** index if it uses any index at all */
105158        assert( pTabItem->pIndex==0
105159                  || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
105160                  || sCost.plan.u.pIdx==pTabItem->pIndex );
105161
105162        if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
105163          notIndexed |= m;
105164        }
105165
105166        /* Conditions under which this table becomes the best so far:
105167        **
105168        **   (1) The table must not depend on other tables that have not
105169        **       yet run.
105170        **
105171        **   (2) A full-table-scan plan cannot supercede indexed plan unless
105172        **       the full-table-scan is an "optimal" plan as defined above.
105173        **
105174        **   (3) All tables have an INDEXED BY clause or this table lacks an
105175        **       INDEXED BY clause or this table uses the specific
105176        **       index specified by its INDEXED BY clause.  This rule ensures
105177        **       that a best-so-far is always selected even if an impossible
105178        **       combination of INDEXED BY clauses are given.  The error
105179        **       will be detected and relayed back to the application later.
105180        **       The NEVER() comes about because rule (2) above prevents
105181        **       An indexable full-table-scan from reaching rule (3).
105182        **
105183        **   (4) The plan cost must be lower than prior plans or else the
105184        **       cost must be the same and the number of rows must be lower.
105185        */
105186        if( (sCost.used&notReady)==0                       /* (1) */
105187            && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
105188                || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
105189                || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
105190            && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
105191                || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
105192            && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
105193                || (sCost.rCost<=bestPlan.rCost
105194                 && sCost.plan.nRow<bestPlan.plan.nRow))
105195        ){
105196          WHERETRACE(("=== table %d is best so far"
105197                      " with cost=%g and nRow=%g\n",
105198                      j, sCost.rCost, sCost.plan.nRow));
105199          bestPlan = sCost;
105200          bestJ = j;
105201        }
105202        if( doNotReorder ) break;
105203      }
105204    }
105205    assert( bestJ>=0 );
105206    assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
105207    WHERETRACE(("*** Optimizer selects table %d for loop %d"
105208                " with cost=%g and nRow=%g\n",
105209                bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
105210    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
105211      *ppOrderBy = 0;
105212    }
105213    if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
105214      assert( pWInfo->eDistinct==0 );
105215      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
105216    }
105217    andFlags &= bestPlan.plan.wsFlags;
105218    pLevel->plan = bestPlan.plan;
105219    testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
105220    testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
105221    if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
105222      pLevel->iIdxCur = pParse->nTab++;
105223    }else{
105224      pLevel->iIdxCur = -1;
105225    }
105226    notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
105227    pLevel->iFrom = (u8)bestJ;
105228    if( bestPlan.plan.nRow>=(double)1 ){
105229      pParse->nQueryLoop *= bestPlan.plan.nRow;
105230    }
105231
105232    /* Check that if the table scanned by this loop iteration had an
105233    ** INDEXED BY clause attached to it, that the named index is being
105234    ** used for the scan. If not, then query compilation has failed.
105235    ** Return an error.
105236    */
105237    pIdx = pTabList->a[bestJ].pIndex;
105238    if( pIdx ){
105239      if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
105240        sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
105241        goto whereBeginError;
105242      }else{
105243        /* If an INDEXED BY clause is used, the bestIndex() function is
105244        ** guaranteed to find the index specified in the INDEXED BY clause
105245        ** if it find an index at all. */
105246        assert( bestPlan.plan.u.pIdx==pIdx );
105247      }
105248    }
105249  }
105250  WHERETRACE(("*** Optimizer Finished ***\n"));
105251  if( pParse->nErr || db->mallocFailed ){
105252    goto whereBeginError;
105253  }
105254
105255  /* If the total query only selects a single row, then the ORDER BY
105256  ** clause is irrelevant.
105257  */
105258  if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
105259    *ppOrderBy = 0;
105260  }
105261
105262  /* If the caller is an UPDATE or DELETE statement that is requesting
105263  ** to use a one-pass algorithm, determine if this is appropriate.
105264  ** The one-pass algorithm only works if the WHERE clause constraints
105265  ** the statement to update a single row.
105266  */
105267  assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
105268  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
105269    pWInfo->okOnePass = 1;
105270    pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
105271  }
105272
105273  /* Open all tables in the pTabList and any indices selected for
105274  ** searching those tables.
105275  */
105276  sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
105277  notReady = ~(Bitmask)0;
105278  pWInfo->nRowOut = (double)1;
105279  for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
105280    Table *pTab;     /* Table to open */
105281    int iDb;         /* Index of database containing table/index */
105282
105283    pTabItem = &pTabList->a[pLevel->iFrom];
105284    pTab = pTabItem->pTab;
105285    pLevel->iTabCur = pTabItem->iCursor;
105286    pWInfo->nRowOut *= pLevel->plan.nRow;
105287    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105288    if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
105289      /* Do nothing */
105290    }else
105291#ifndef SQLITE_OMIT_VIRTUALTABLE
105292    if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
105293      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
105294      int iCur = pTabItem->iCursor;
105295      sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
105296    }else
105297#endif
105298    if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105299         && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
105300      int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
105301      sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
105302      testcase( pTab->nCol==BMS-1 );
105303      testcase( pTab->nCol==BMS );
105304      if( !pWInfo->okOnePass && pTab->nCol<BMS ){
105305        Bitmask b = pTabItem->colUsed;
105306        int n = 0;
105307        for(; b; b=b>>1, n++){}
105308        sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
105309                            SQLITE_INT_TO_PTR(n), P4_INT32);
105310        assert( n<=pTab->nCol );
105311      }
105312    }else{
105313      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
105314    }
105315#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
105316    if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
105317      constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
105318    }else
105319#endif
105320    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
105321      Index *pIx = pLevel->plan.u.pIdx;
105322      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
105323      int iIdxCur = pLevel->iIdxCur;
105324      assert( pIx->pSchema==pTab->pSchema );
105325      assert( iIdxCur>=0 );
105326      sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
105327                        (char*)pKey, P4_KEYINFO_HANDOFF);
105328      VdbeComment((v, "%s", pIx->zName));
105329    }
105330    sqlite3CodeVerifySchema(pParse, iDb);
105331    notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
105332  }
105333  pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
105334  if( db->mallocFailed ) goto whereBeginError;
105335
105336  /* Generate the code to do the search.  Each iteration of the for
105337  ** loop below generates code for a single nested loop of the VM
105338  ** program.
105339  */
105340  notReady = ~(Bitmask)0;
105341  for(i=0; i<nTabList; i++){
105342    pLevel = &pWInfo->a[i];
105343    explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
105344    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
105345    pWInfo->iContinue = pLevel->addrCont;
105346  }
105347
105348#ifdef SQLITE_TEST  /* For testing and debugging use only */
105349  /* Record in the query plan information about the current table
105350  ** and the index used to access it (if any).  If the table itself
105351  ** is not used, its name is just '{}'.  If no index is used
105352  ** the index is listed as "{}".  If the primary key is used the
105353  ** index name is '*'.
105354  */
105355  for(i=0; i<nTabList; i++){
105356    char *z;
105357    int n;
105358    pLevel = &pWInfo->a[i];
105359    pTabItem = &pTabList->a[pLevel->iFrom];
105360    z = pTabItem->zAlias;
105361    if( z==0 ) z = pTabItem->pTab->zName;
105362    n = sqlite3Strlen30(z);
105363    if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
105364      if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
105365        memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
105366        nQPlan += 2;
105367      }else{
105368        memcpy(&sqlite3_query_plan[nQPlan], z, n);
105369        nQPlan += n;
105370      }
105371      sqlite3_query_plan[nQPlan++] = ' ';
105372    }
105373    testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
105374    testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
105375    if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
105376      memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
105377      nQPlan += 2;
105378    }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
105379      n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
105380      if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
105381        memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
105382        nQPlan += n;
105383        sqlite3_query_plan[nQPlan++] = ' ';
105384      }
105385    }else{
105386      memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
105387      nQPlan += 3;
105388    }
105389  }
105390  while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
105391    sqlite3_query_plan[--nQPlan] = 0;
105392  }
105393  sqlite3_query_plan[nQPlan] = 0;
105394  nQPlan = 0;
105395#endif /* SQLITE_TEST // Testing and debugging use only */
105396
105397  /* Record the continuation address in the WhereInfo structure.  Then
105398  ** clean up and return.
105399  */
105400  return pWInfo;
105401
105402  /* Jump here if malloc fails */
105403whereBeginError:
105404  if( pWInfo ){
105405    pParse->nQueryLoop = pWInfo->savedNQueryLoop;
105406    whereInfoFree(db, pWInfo);
105407  }
105408  return 0;
105409}
105410
105411/*
105412** Generate the end of the WHERE loop.  See comments on
105413** sqlite3WhereBegin() for additional information.
105414*/
105415SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
105416  Parse *pParse = pWInfo->pParse;
105417  Vdbe *v = pParse->pVdbe;
105418  int i;
105419  WhereLevel *pLevel;
105420  SrcList *pTabList = pWInfo->pTabList;
105421  sqlite3 *db = pParse->db;
105422
105423  /* Generate loop termination code.
105424  */
105425  sqlite3ExprCacheClear(pParse);
105426  for(i=pWInfo->nLevel-1; i>=0; i--){
105427    pLevel = &pWInfo->a[i];
105428    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
105429    if( pLevel->op!=OP_Noop ){
105430      sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
105431      sqlite3VdbeChangeP5(v, pLevel->p5);
105432    }
105433    if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
105434      struct InLoop *pIn;
105435      int j;
105436      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
105437      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
105438        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
105439        sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
105440        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
105441      }
105442      sqlite3DbFree(db, pLevel->u.in.aInLoop);
105443    }
105444    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
105445    if( pLevel->iLeftJoin ){
105446      int addr;
105447      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
105448      assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105449           || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
105450      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
105451        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
105452      }
105453      if( pLevel->iIdxCur>=0 ){
105454        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
105455      }
105456      if( pLevel->op==OP_Return ){
105457        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
105458      }else{
105459        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
105460      }
105461      sqlite3VdbeJumpHere(v, addr);
105462    }
105463  }
105464
105465  /* The "break" point is here, just past the end of the outer loop.
105466  ** Set it.
105467  */
105468  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
105469
105470  /* Close all of the cursors that were opened by sqlite3WhereBegin.
105471  */
105472  assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
105473  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
105474    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
105475    Table *pTab = pTabItem->pTab;
105476    assert( pTab!=0 );
105477    if( (pTab->tabFlags & TF_Ephemeral)==0
105478     && pTab->pSelect==0
105479     && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
105480    ){
105481      int ws = pLevel->plan.wsFlags;
105482      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
105483        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
105484      }
105485      if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
105486        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
105487      }
105488    }
105489
105490    /* If this scan uses an index, make code substitutions to read data
105491    ** from the index in preference to the table. Sometimes, this means
105492    ** the table need never be read from. This is a performance boost,
105493    ** as the vdbe level waits until the table is read before actually
105494    ** seeking the table cursor to the record corresponding to the current
105495    ** position in the index.
105496    **
105497    ** Calls to the code generator in between sqlite3WhereBegin and
105498    ** sqlite3WhereEnd will have created code that references the table
105499    ** directly.  This loop scans all that code looking for opcodes
105500    ** that reference the table and converts them into opcodes that
105501    ** reference the index.
105502    */
105503    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
105504      int k, j, last;
105505      VdbeOp *pOp;
105506      Index *pIdx = pLevel->plan.u.pIdx;
105507
105508      assert( pIdx!=0 );
105509      pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
105510      last = sqlite3VdbeCurrentAddr(v);
105511      for(k=pWInfo->iTop; k<last; k++, pOp++){
105512        if( pOp->p1!=pLevel->iTabCur ) continue;
105513        if( pOp->opcode==OP_Column ){
105514          for(j=0; j<pIdx->nColumn; j++){
105515            if( pOp->p2==pIdx->aiColumn[j] ){
105516              pOp->p2 = j;
105517              pOp->p1 = pLevel->iIdxCur;
105518              break;
105519            }
105520          }
105521          assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105522               || j<pIdx->nColumn );
105523        }else if( pOp->opcode==OP_Rowid ){
105524          pOp->p1 = pLevel->iIdxCur;
105525          pOp->opcode = OP_IdxRowid;
105526        }
105527      }
105528    }
105529  }
105530
105531  /* Final cleanup
105532  */
105533  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
105534  whereInfoFree(db, pWInfo);
105535  return;
105536}
105537
105538/************** End of where.c ***********************************************/
105539/************** Begin file parse.c *******************************************/
105540/* Driver template for the LEMON parser generator.
105541** The author disclaims copyright to this source code.
105542**
105543** This version of "lempar.c" is modified, slightly, for use by SQLite.
105544** The only modifications are the addition of a couple of NEVER()
105545** macros to disable tests that are needed in the case of a general
105546** LALR(1) grammar but which are always false in the
105547** specific grammar used by SQLite.
105548*/
105549/* First off, code is included that follows the "include" declaration
105550** in the input grammar file. */
105551/* #include <stdio.h> */
105552
105553
105554/*
105555** Disable all error recovery processing in the parser push-down
105556** automaton.
105557*/
105558#define YYNOERRORRECOVERY 1
105559
105560/*
105561** Make yytestcase() the same as testcase()
105562*/
105563#define yytestcase(X) testcase(X)
105564
105565/*
105566** An instance of this structure holds information about the
105567** LIMIT clause of a SELECT statement.
105568*/
105569struct LimitVal {
105570  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
105571  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
105572};
105573
105574/*
105575** An instance of this structure is used to store the LIKE,
105576** GLOB, NOT LIKE, and NOT GLOB operators.
105577*/
105578struct LikeOp {
105579  Token eOperator;  /* "like" or "glob" or "regexp" */
105580  int not;         /* True if the NOT keyword is present */
105581};
105582
105583/*
105584** An instance of the following structure describes the event of a
105585** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
105586** TK_DELETE, or TK_INSTEAD.  If the event is of the form
105587**
105588**      UPDATE ON (a,b,c)
105589**
105590** Then the "b" IdList records the list "a,b,c".
105591*/
105592struct TrigEvent { int a; IdList * b; };
105593
105594/*
105595** An instance of this structure holds the ATTACH key and the key type.
105596*/
105597struct AttachKey { int type;  Token key; };
105598
105599
105600  /* This is a utility routine used to set the ExprSpan.zStart and
105601  ** ExprSpan.zEnd values of pOut so that the span covers the complete
105602  ** range of text beginning with pStart and going to the end of pEnd.
105603  */
105604  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
105605    pOut->zStart = pStart->z;
105606    pOut->zEnd = &pEnd->z[pEnd->n];
105607  }
105608
105609  /* Construct a new Expr object from a single identifier.  Use the
105610  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
105611  ** that created the expression.
105612  */
105613  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
105614    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
105615    pOut->zStart = pValue->z;
105616    pOut->zEnd = &pValue->z[pValue->n];
105617  }
105618
105619  /* This routine constructs a binary expression node out of two ExprSpan
105620  ** objects and uses the result to populate a new ExprSpan object.
105621  */
105622  static void spanBinaryExpr(
105623    ExprSpan *pOut,     /* Write the result here */
105624    Parse *pParse,      /* The parsing context.  Errors accumulate here */
105625    int op,             /* The binary operation */
105626    ExprSpan *pLeft,    /* The left operand */
105627    ExprSpan *pRight    /* The right operand */
105628  ){
105629    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
105630    pOut->zStart = pLeft->zStart;
105631    pOut->zEnd = pRight->zEnd;
105632  }
105633
105634  /* Construct an expression node for a unary postfix operator
105635  */
105636  static void spanUnaryPostfix(
105637    ExprSpan *pOut,        /* Write the new expression node here */
105638    Parse *pParse,         /* Parsing context to record errors */
105639    int op,                /* The operator */
105640    ExprSpan *pOperand,    /* The operand */
105641    Token *pPostOp         /* The operand token for setting the span */
105642  ){
105643    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
105644    pOut->zStart = pOperand->zStart;
105645    pOut->zEnd = &pPostOp->z[pPostOp->n];
105646  }
105647
105648  /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
105649  ** unary TK_ISNULL or TK_NOTNULL expression. */
105650  static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
105651    sqlite3 *db = pParse->db;
105652    if( db->mallocFailed==0 && pY->op==TK_NULL ){
105653      pA->op = (u8)op;
105654      sqlite3ExprDelete(db, pA->pRight);
105655      pA->pRight = 0;
105656    }
105657  }
105658
105659  /* Construct an expression node for a unary prefix operator
105660  */
105661  static void spanUnaryPrefix(
105662    ExprSpan *pOut,        /* Write the new expression node here */
105663    Parse *pParse,         /* Parsing context to record errors */
105664    int op,                /* The operator */
105665    ExprSpan *pOperand,    /* The operand */
105666    Token *pPreOp         /* The operand token for setting the span */
105667  ){
105668    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
105669    pOut->zStart = pPreOp->z;
105670    pOut->zEnd = pOperand->zEnd;
105671  }
105672/* Next is all token values, in a form suitable for use by makeheaders.
105673** This section will be null unless lemon is run with the -m switch.
105674*/
105675/*
105676** These constants (all generated automatically by the parser generator)
105677** specify the various kinds of tokens (terminals) that the parser
105678** understands.
105679**
105680** Each symbol here is a terminal symbol in the grammar.
105681*/
105682/* Make sure the INTERFACE macro is defined.
105683*/
105684#ifndef INTERFACE
105685# define INTERFACE 1
105686#endif
105687/* The next thing included is series of defines which control
105688** various aspects of the generated parser.
105689**    YYCODETYPE         is the data type used for storing terminal
105690**                       and nonterminal numbers.  "unsigned char" is
105691**                       used if there are fewer than 250 terminals
105692**                       and nonterminals.  "int" is used otherwise.
105693**    YYNOCODE           is a number of type YYCODETYPE which corresponds
105694**                       to no legal terminal or nonterminal number.  This
105695**                       number is used to fill in empty slots of the hash
105696**                       table.
105697**    YYFALLBACK         If defined, this indicates that one or more tokens
105698**                       have fall-back values which should be used if the
105699**                       original value of the token will not parse.
105700**    YYACTIONTYPE       is the data type used for storing terminal
105701**                       and nonterminal numbers.  "unsigned char" is
105702**                       used if there are fewer than 250 rules and
105703**                       states combined.  "int" is used otherwise.
105704**    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given
105705**                       directly to the parser from the tokenizer.
105706**    YYMINORTYPE        is the data type used for all minor tokens.
105707**                       This is typically a union of many types, one of
105708**                       which is sqlite3ParserTOKENTYPE.  The entry in the union
105709**                       for base tokens is called "yy0".
105710**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
105711**                       zero the stack is dynamically sized using realloc()
105712**    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
105713**    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
105714**    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
105715**    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
105716**    YYNSTATE           the combined number of states.
105717**    YYNRULE            the number of rules in the grammar
105718**    YYERRORSYMBOL      is the code number of the error symbol.  If not
105719**                       defined, then do no error processing.
105720*/
105721#define YYCODETYPE unsigned char
105722#define YYNOCODE 253
105723#define YYACTIONTYPE unsigned short int
105724#define YYWILDCARD 67
105725#define sqlite3ParserTOKENTYPE Token
105726typedef union {
105727  int yyinit;
105728  sqlite3ParserTOKENTYPE yy0;
105729  int yy4;
105730  struct TrigEvent yy90;
105731  ExprSpan yy118;
105732  TriggerStep* yy203;
105733  u8 yy210;
105734  struct {int value; int mask;} yy215;
105735  SrcList* yy259;
105736  struct LimitVal yy292;
105737  Expr* yy314;
105738  ExprList* yy322;
105739  struct LikeOp yy342;
105740  IdList* yy384;
105741  Select* yy387;
105742} YYMINORTYPE;
105743#ifndef YYSTACKDEPTH
105744#define YYSTACKDEPTH 100
105745#endif
105746#define sqlite3ParserARG_SDECL Parse *pParse;
105747#define sqlite3ParserARG_PDECL ,Parse *pParse
105748#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
105749#define sqlite3ParserARG_STORE yypParser->pParse = pParse
105750#define YYNSTATE 630
105751#define YYNRULE 329
105752#define YYFALLBACK 1
105753#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
105754#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
105755#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
105756
105757/* The yyzerominor constant is used to initialize instances of
105758** YYMINORTYPE objects to zero. */
105759static const YYMINORTYPE yyzerominor = { 0 };
105760
105761/* Define the yytestcase() macro to be a no-op if is not already defined
105762** otherwise.
105763**
105764** Applications can choose to define yytestcase() in the %include section
105765** to a macro that can assist in verifying code coverage.  For production
105766** code the yytestcase() macro should be turned off.  But it is useful
105767** for testing.
105768*/
105769#ifndef yytestcase
105770# define yytestcase(X)
105771#endif
105772
105773
105774/* Next are the tables used to determine what action to take based on the
105775** current state and lookahead token.  These tables are used to implement
105776** functions that take a state number and lookahead value and return an
105777** action integer.
105778**
105779** Suppose the action integer is N.  Then the action is determined as
105780** follows
105781**
105782**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
105783**                                      token onto the stack and goto state N.
105784**
105785**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
105786**
105787**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
105788**
105789**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
105790**
105791**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
105792**                                      slots in the yy_action[] table.
105793**
105794** The action table is constructed as a single large table named yy_action[].
105795** Given state S and lookahead X, the action is computed as
105796**
105797**      yy_action[ yy_shift_ofst[S] + X ]
105798**
105799** If the index value yy_shift_ofst[S]+X is out of range or if the value
105800** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
105801** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
105802** and that yy_default[S] should be used instead.
105803**
105804** The formula above is for computing the action when the lookahead is
105805** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
105806** a reduce action) then the yy_reduce_ofst[] array is used in place of
105807** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
105808** YY_SHIFT_USE_DFLT.
105809**
105810** The following are the tables generated in this section:
105811**
105812**  yy_action[]        A single table containing all actions.
105813**  yy_lookahead[]     A table containing the lookahead for each entry in
105814**                     yy_action.  Used to detect hash collisions.
105815**  yy_shift_ofst[]    For each state, the offset into yy_action for
105816**                     shifting terminals.
105817**  yy_reduce_ofst[]   For each state, the offset into yy_action for
105818**                     shifting non-terminals after a reduce.
105819**  yy_default[]       Default action for each state.
105820*/
105821#define YY_ACTTAB_COUNT (1557)
105822static const YYACTIONTYPE yy_action[] = {
105823 /*     0 */   313,  960,  186,  419,    2,  172,  627,  597,   55,   55,
105824 /*    10 */    55,   55,   48,   53,   53,   53,   53,   52,   52,   51,
105825 /*    20 */    51,   51,   50,  238,  302,  283,  623,  622,  516,  515,
105826 /*    30 */   590,  584,   55,   55,   55,   55,  282,   53,   53,   53,
105827 /*    40 */    53,   52,   52,   51,   51,   51,   50,  238,    6,   56,
105828 /*    50 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
105829 /*    60 */    55,   55,  608,   53,   53,   53,   53,   52,   52,   51,
105830 /*    70 */    51,   51,   50,  238,  313,  597,  409,  330,  579,  579,
105831 /*    80 */    32,   53,   53,   53,   53,   52,   52,   51,   51,   51,
105832 /*    90 */    50,  238,  330,  217,  620,  619,  166,  411,  624,  382,
105833 /*   100 */   379,  378,    7,  491,  590,  584,  200,  199,  198,   58,
105834 /*   110 */   377,  300,  414,  621,  481,   66,  623,  622,  621,  580,
105835 /*   120 */   254,  601,   94,   56,   57,   47,  582,  581,  583,  583,
105836 /*   130 */    54,   54,   55,   55,   55,   55,  671,   53,   53,   53,
105837 /*   140 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  532,
105838 /*   150 */   226,  506,  507,  133,  177,  139,  284,  385,  279,  384,
105839 /*   160 */   169,  197,  342,  398,  251,  226,  253,  275,  388,  167,
105840 /*   170 */   139,  284,  385,  279,  384,  169,  570,  236,  590,  584,
105841 /*   180 */   672,  240,  275,  157,  620,  619,  554,  437,   51,   51,
105842 /*   190 */    51,   50,  238,  343,  439,  553,  438,   56,   57,   47,
105843 /*   200 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
105844 /*   210 */   465,   53,   53,   53,   53,   52,   52,   51,   51,   51,
105845 /*   220 */    50,  238,  313,  390,   52,   52,   51,   51,   51,   50,
105846 /*   230 */   238,  391,  166,  491,  566,  382,  379,  378,  409,  440,
105847 /*   240 */   579,  579,  252,  440,  607,   66,  377,  513,  621,   49,
105848 /*   250 */    46,  147,  590,  584,  621,   16,  466,  189,  621,  441,
105849 /*   260 */   442,  673,  526,  441,  340,  577,  595,   64,  194,  482,
105850 /*   270 */   434,   56,   57,   47,  582,  581,  583,  583,   54,   54,
105851 /*   280 */    55,   55,   55,   55,   30,   53,   53,   53,   53,   52,
105852 /*   290 */    52,   51,   51,   51,   50,  238,  313,  593,  593,  593,
105853 /*   300 */   387,  578,  606,  493,  259,  351,  258,  411,    1,  623,
105854 /*   310 */   622,  496,  623,  622,   65,  240,  623,  622,  597,  443,
105855 /*   320 */   237,  239,  414,  341,  237,  602,  590,  584,   18,  603,
105856 /*   330 */   166,  601,   87,  382,  379,  378,   67,  623,  622,   38,
105857 /*   340 */   623,  622,  176,  270,  377,   56,   57,   47,  582,  581,
105858 /*   350 */   583,  583,   54,   54,   55,   55,   55,   55,  175,   53,
105859 /*   360 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
105860 /*   370 */   313,  396,  233,  411,  531,  565,  317,  620,  619,   44,
105861 /*   380 */   620,  619,  240,  206,  620,  619,  597,  266,  414,  268,
105862 /*   390 */   409,  597,  579,  579,  352,  184,  505,  601,   73,  533,
105863 /*   400 */   590,  584,  466,  548,  190,  620,  619,  576,  620,  619,
105864 /*   410 */   547,  383,  551,   35,  332,  575,  574,  600,  504,   56,
105865 /*   420 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
105866 /*   430 */    55,   55,  567,   53,   53,   53,   53,   52,   52,   51,
105867 /*   440 */    51,   51,   50,  238,  313,  411,  561,  561,  528,  364,
105868 /*   450 */   259,  351,  258,  183,  361,  549,  524,  374,  411,  597,
105869 /*   460 */   414,  240,  560,  560,  409,  604,  579,  579,  328,  601,
105870 /*   470 */    93,  623,  622,  414,  590,  584,  237,  564,  559,  559,
105871 /*   480 */   520,  402,  601,   87,  409,  210,  579,  579,  168,  421,
105872 /*   490 */   950,  519,  950,   56,   57,   47,  582,  581,  583,  583,
105873 /*   500 */    54,   54,   55,   55,   55,   55,  192,   53,   53,   53,
105874 /*   510 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  600,
105875 /*   520 */   293,  563,  511,  234,  357,  146,  475,  475,  367,  411,
105876 /*   530 */   562,  411,  358,  542,  425,  171,  411,  215,  144,  620,
105877 /*   540 */   619,  544,  318,  353,  414,  203,  414,  275,  590,  584,
105878 /*   550 */   549,  414,  174,  601,   94,  601,   79,  558,  471,   61,
105879 /*   560 */   601,   79,  421,  949,  350,  949,   34,   56,   57,   47,
105880 /*   570 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
105881 /*   580 */   535,   53,   53,   53,   53,   52,   52,   51,   51,   51,
105882 /*   590 */    50,  238,  313,  307,  424,  394,  272,   49,   46,  147,
105883 /*   600 */   349,  322,    4,  411,  491,  312,  321,  425,  568,  492,
105884 /*   610 */   216,  264,  407,  575,  574,  429,   66,  549,  414,  621,
105885 /*   620 */   540,  602,  590,  584,   13,  603,  621,  601,   72,   12,
105886 /*   630 */   618,  617,  616,  202,  210,  621,  546,  469,  422,  319,
105887 /*   640 */   148,   56,   57,   47,  582,  581,  583,  583,   54,   54,
105888 /*   650 */    55,   55,   55,   55,  338,   53,   53,   53,   53,   52,
105889 /*   660 */    52,   51,   51,   51,   50,  238,  313,  600,  600,  411,
105890 /*   670 */    39,   21,   37,  170,  237,  875,  411,  572,  572,  201,
105891 /*   680 */   144,  473,  538,  331,  414,  474,  143,  146,  630,  628,
105892 /*   690 */   334,  414,  353,  601,   68,  168,  590,  584,  132,  365,
105893 /*   700 */   601,   96,  307,  423,  530,  336,   49,   46,  147,  568,
105894 /*   710 */   406,  216,  549,  360,  529,   56,   57,   47,  582,  581,
105895 /*   720 */   583,  583,   54,   54,   55,   55,   55,   55,  411,   53,
105896 /*   730 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
105897 /*   740 */   313,  411,  605,  414,  484,  510,  172,  422,  597,  318,
105898 /*   750 */   496,  485,  601,   99,  411,  142,  414,  411,  231,  411,
105899 /*   760 */   540,  411,  359,  629,    2,  601,   97,  426,  308,  414,
105900 /*   770 */   590,  584,  414,   20,  414,  621,  414,  621,  601,  106,
105901 /*   780 */   503,  601,  105,  601,  108,  601,  109,  204,   28,   56,
105902 /*   790 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
105903 /*   800 */    55,   55,  411,   53,   53,   53,   53,   52,   52,   51,
105904 /*   810 */    51,   51,   50,  238,  313,  411,  597,  414,  411,  276,
105905 /*   820 */   214,  600,  411,  366,  213,  381,  601,  134,  274,  500,
105906 /*   830 */   414,  167,  130,  414,  621,  411,  354,  414,  376,  601,
105907 /*   840 */   135,  129,  601,  100,  590,  584,  601,  104,  522,  521,
105908 /*   850 */   414,  621,  224,  273,  600,  167,  327,  282,  600,  601,
105909 /*   860 */   103,  468,  521,   56,   57,   47,  582,  581,  583,  583,
105910 /*   870 */    54,   54,   55,   55,   55,   55,  411,   53,   53,   53,
105911 /*   880 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  411,
105912 /*   890 */    27,  414,  411,  375,  276,  167,  359,  544,   50,  238,
105913 /*   900 */   601,   95,  128,  223,  414,  411,  165,  414,  411,  621,
105914 /*   910 */   411,  621,  612,  601,  102,  372,  601,   76,  590,  584,
105915 /*   920 */   414,  570,  236,  414,  470,  414,  167,  621,  188,  601,
105916 /*   930 */    98,  225,  601,  138,  601,  137,  232,   56,   45,   47,
105917 /*   940 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
105918 /*   950 */   411,   53,   53,   53,   53,   52,   52,   51,   51,   51,
105919 /*   960 */    50,  238,  313,  276,  276,  414,  411,  276,  544,  459,
105920 /*   970 */   359,  171,  209,  479,  601,  136,  628,  334,  621,  621,
105921 /*   980 */   125,  414,  621,  368,  411,  621,  257,  540,  589,  588,
105922 /*   990 */   601,   75,  590,  584,  458,  446,   23,   23,  124,  414,
105923 /*  1000 */   326,  325,  621,  427,  324,  309,  600,  288,  601,   92,
105924 /*  1010 */   586,  585,   57,   47,  582,  581,  583,  583,   54,   54,
105925 /*  1020 */    55,   55,   55,   55,  411,   53,   53,   53,   53,   52,
105926 /*  1030 */    52,   51,   51,   51,   50,  238,  313,  587,  411,  414,
105927 /*  1040 */   411,  207,  611,  476,  171,  472,  160,  123,  601,   91,
105928 /*  1050 */   323,  261,   15,  414,  464,  414,  411,  621,  411,  354,
105929 /*  1060 */   222,  411,  601,   74,  601,   90,  590,  584,  159,  264,
105930 /*  1070 */   158,  414,  461,  414,  621,  600,  414,  121,  120,   25,
105931 /*  1080 */   601,   89,  601,  101,  621,  601,   88,   47,  582,  581,
105932 /*  1090 */   583,  583,   54,   54,   55,   55,   55,   55,  544,   53,
105933 /*  1100 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
105934 /*  1110 */    43,  405,  263,    3,  610,  264,  140,  415,  622,   24,
105935 /*  1120 */   410,   11,  456,  594,  118,  155,  219,  452,  408,  621,
105936 /*  1130 */   621,  621,  156,   43,  405,  621,    3,  286,  621,  113,
105937 /*  1140 */   415,  622,  111,  445,  411,  400,  557,  403,  545,   10,
105938 /*  1150 */   411,  408,  264,  110,  205,  436,  541,  566,  453,  414,
105939 /*  1160 */   621,  621,   63,  621,  435,  414,  411,  621,  601,   94,
105940 /*  1170 */   403,  621,  411,  337,  601,   86,  150,   40,   41,  534,
105941 /*  1180 */   566,  414,  242,  264,   42,  413,  412,  414,  600,  595,
105942 /*  1190 */   601,   85,  191,  333,  107,  451,  601,   84,  621,  539,
105943 /*  1200 */    40,   41,  420,  230,  411,  149,  316,   42,  413,  412,
105944 /*  1210 */   398,  127,  595,  315,  621,  399,  278,  625,  181,  414,
105945 /*  1220 */   593,  593,  593,  592,  591,   14,  450,  411,  601,   71,
105946 /*  1230 */   240,  621,   43,  405,  264,    3,  615,  180,  264,  415,
105947 /*  1240 */   622,  614,  414,  593,  593,  593,  592,  591,   14,  621,
105948 /*  1250 */   408,  601,   70,  621,  417,   33,  405,  613,    3,  411,
105949 /*  1260 */   264,  411,  415,  622,  418,  626,  178,  509,    8,  403,
105950 /*  1270 */   241,  416,  126,  408,  414,  621,  414,  449,  208,  566,
105951 /*  1280 */   240,  221,  621,  601,   83,  601,   82,  599,  297,  277,
105952 /*  1290 */   296,   30,  403,   31,  395,  264,  295,  397,  489,   40,
105953 /*  1300 */    41,  411,  566,  220,  621,  294,   42,  413,  412,  271,
105954 /*  1310 */   621,  595,  600,  621,   59,   60,  414,  269,  267,  623,
105955 /*  1320 */   622,   36,   40,   41,  621,  601,   81,  598,  235,   42,
105956 /*  1330 */   413,  412,  621,  621,  595,  265,  344,  411,  248,  556,
105957 /*  1340 */   173,  185,  593,  593,  593,  592,  591,   14,  218,   29,
105958 /*  1350 */   621,  543,  414,  305,  304,  303,  179,  301,  411,  566,
105959 /*  1360 */   454,  601,   80,  289,  335,  593,  593,  593,  592,  591,
105960 /*  1370 */    14,  411,  287,  414,  151,  392,  246,  260,  411,  196,
105961 /*  1380 */   195,  523,  601,   69,  411,  245,  414,  526,  537,  285,
105962 /*  1390 */   389,  595,  621,  414,  536,  601,   17,  362,  153,  414,
105963 /*  1400 */   466,  463,  601,   78,  154,  414,  462,  152,  601,   77,
105964 /*  1410 */   355,  255,  621,  455,  601,    9,  621,  386,  444,  517,
105965 /*  1420 */   247,  621,  593,  593,  593,  621,  621,  244,  621,  243,
105966 /*  1430 */   430,  518,  292,  621,  329,  621,  145,  393,  280,  513,
105967 /*  1440 */   291,  131,  621,  514,  621,  621,  311,  621,  259,  346,
105968 /*  1450 */   249,  621,  621,  229,  314,  621,  228,  512,  227,  240,
105969 /*  1460 */   494,  488,  310,  164,  487,  486,  373,  480,  163,  262,
105970 /*  1470 */   369,  371,  162,   26,  212,  478,  477,  161,  141,  363,
105971 /*  1480 */   467,  122,  339,  187,  119,  348,  347,  117,  116,  115,
105972 /*  1490 */   114,  112,  182,  457,  320,   22,  433,  432,  448,   19,
105973 /*  1500 */   609,  431,  428,   62,  193,  596,  573,  298,  555,  552,
105974 /*  1510 */   571,  404,  290,  380,  498,  510,  495,  306,  281,  499,
105975 /*  1520 */   250,    5,  497,  460,  345,  447,  569,  550,  238,  299,
105976 /*  1530 */   527,  525,  508,  961,  502,  501,  961,  401,  961,  211,
105977 /*  1540 */   490,  356,  256,  961,  483,  961,  961,  961,  961,  961,
105978 /*  1550 */   961,  961,  961,  961,  961,  961,  370,
105979};
105980static const YYCODETYPE yy_lookahead[] = {
105981 /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
105982 /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
105983 /*    20 */    89,   90,   91,   92,   15,   98,   26,   27,    7,    8,
105984 /*    30 */    49,   50,   77,   78,   79,   80,  109,   82,   83,   84,
105985 /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   22,   68,
105986 /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
105987 /*    60 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
105988 /*    70 */    89,   90,   91,   92,   19,   94,  112,   19,  114,  115,
105989 /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
105990 /*    90 */    91,   92,   19,   22,   94,   95,   96,  150,  150,   99,
105991 /*   100 */   100,  101,   76,  150,   49,   50,  105,  106,  107,   54,
105992 /*   110 */   110,  158,  165,  165,  161,  162,   26,   27,  165,  113,
105993 /*   120 */    16,  174,  175,   68,   69,   70,   71,   72,   73,   74,
105994 /*   130 */    75,   76,   77,   78,   79,   80,  118,   82,   83,   84,
105995 /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   23,
105996 /*   150 */    92,   97,   98,   24,   96,   97,   98,   99,  100,  101,
105997 /*   160 */   102,   25,   97,  216,   60,   92,   62,  109,  221,   25,
105998 /*   170 */    97,   98,   99,  100,  101,  102,   86,   87,   49,   50,
105999 /*   180 */   118,  116,  109,   25,   94,   95,   32,   97,   88,   89,
106000 /*   190 */    90,   91,   92,  128,  104,   41,  106,   68,   69,   70,
106001 /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
106002 /*   210 */    11,   82,   83,   84,   85,   86,   87,   88,   89,   90,
106003 /*   220 */    91,   92,   19,   19,   86,   87,   88,   89,   90,   91,
106004 /*   230 */    92,   27,   96,  150,   66,   99,  100,  101,  112,  150,
106005 /*   240 */   114,  115,  138,  150,  161,  162,  110,  103,  165,  222,
106006 /*   250 */   223,  224,   49,   50,  165,   22,   57,   24,  165,  170,
106007 /*   260 */   171,  118,   94,  170,  171,   23,   98,   25,  185,  186,
106008 /*   270 */   243,   68,   69,   70,   71,   72,   73,   74,   75,   76,
106009 /*   280 */    77,   78,   79,   80,  126,   82,   83,   84,   85,   86,
106010 /*   290 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  131,
106011 /*   300 */    88,   23,  172,  173,  105,  106,  107,  150,   22,   26,
106012 /*   310 */    27,  181,   26,   27,   22,  116,   26,   27,   26,  230,
106013 /*   320 */   231,  197,  165,  230,  231,  113,   49,   50,  204,  117,
106014 /*   330 */    96,  174,  175,   99,  100,  101,   22,   26,   27,  136,
106015 /*   340 */    26,   27,  118,   16,  110,   68,   69,   70,   71,   72,
106016 /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  118,   82,
106017 /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
106018 /*   370 */    19,  214,  215,  150,   23,   23,  155,   94,   95,   22,
106019 /*   380 */    94,   95,  116,  160,   94,   95,   94,   60,  165,   62,
106020 /*   390 */   112,   26,  114,  115,  128,   23,   36,  174,  175,   88,
106021 /*   400 */    49,   50,   57,  120,   22,   94,   95,   23,   94,   95,
106022 /*   410 */   120,   51,   25,  136,  169,  170,  171,  194,   58,   68,
106023 /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
106024 /*   430 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
106025 /*   440 */    89,   90,   91,   92,   19,  150,   12,   12,   23,  228,
106026 /*   450 */   105,  106,  107,   23,  233,   25,  165,   19,  150,   94,
106027 /*   460 */   165,  116,   28,   28,  112,  174,  114,  115,  108,  174,
106028 /*   470 */   175,   26,   27,  165,   49,   50,  231,   11,   44,   44,
106029 /*   480 */    46,   46,  174,  175,  112,  160,  114,  115,   50,   22,
106030 /*   490 */    23,   57,   25,   68,   69,   70,   71,   72,   73,   74,
106031 /*   500 */    75,   76,   77,   78,   79,   80,  119,   82,   83,   84,
106032 /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  194,
106033 /*   520 */   225,   23,   23,  215,   19,   95,  105,  106,  107,  150,
106034 /*   530 */    23,  150,   27,   23,   67,   25,  150,  206,  207,   94,
106035 /*   540 */    95,  166,  104,  218,  165,   22,  165,  109,   49,   50,
106036 /*   550 */   120,  165,   25,  174,  175,  174,  175,   23,   21,  234,
106037 /*   560 */   174,  175,   22,   23,  239,   25,   25,   68,   69,   70,
106038 /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
106039 /*   580 */   205,   82,   83,   84,   85,   86,   87,   88,   89,   90,
106040 /*   590 */    91,   92,   19,   22,   23,  216,   23,  222,  223,  224,
106041 /*   600 */    63,  220,   35,  150,  150,  163,  220,   67,  166,  167,
106042 /*   610 */   168,  150,  169,  170,  171,  161,  162,   25,  165,  165,
106043 /*   620 */   150,  113,   49,   50,   25,  117,  165,  174,  175,   35,
106044 /*   630 */     7,    8,    9,  160,  160,  165,  120,  100,   67,  247,
106045 /*   640 */   248,   68,   69,   70,   71,   72,   73,   74,   75,   76,
106046 /*   650 */    77,   78,   79,   80,  193,   82,   83,   84,   85,   86,
106047 /*   660 */    87,   88,   89,   90,   91,   92,   19,  194,  194,  150,
106048 /*   670 */   135,   24,  137,   35,  231,  138,  150,  129,  130,  206,
106049 /*   680 */   207,   30,   27,  213,  165,   34,  118,   95,    0,    1,
106050 /*   690 */     2,  165,  218,  174,  175,   50,   49,   50,   22,   48,
106051 /*   700 */   174,  175,   22,   23,   23,  244,  222,  223,  224,  166,
106052 /*   710 */   167,  168,  120,  239,   23,   68,   69,   70,   71,   72,
106053 /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
106054 /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
106055 /*   740 */    19,  150,  173,  165,  181,  182,   24,   67,   26,  104,
106056 /*   750 */   181,  188,  174,  175,  150,   39,  165,  150,   52,  150,
106057 /*   760 */   150,  150,  150,  144,  145,  174,  175,  249,  250,  165,
106058 /*   770 */    49,   50,  165,   52,  165,  165,  165,  165,  174,  175,
106059 /*   780 */    29,  174,  175,  174,  175,  174,  175,  160,   22,   68,
106060 /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
106061 /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
106062 /*   810 */    89,   90,   91,   92,   19,  150,   94,  165,  150,  150,
106063 /*   820 */   160,  194,  150,  213,  160,   52,  174,  175,   23,   23,
106064 /*   830 */   165,   25,   22,  165,  165,  150,  150,  165,   52,  174,
106065 /*   840 */   175,   22,  174,  175,   49,   50,  174,  175,  190,  191,
106066 /*   850 */   165,  165,  240,   23,  194,   25,  187,  109,  194,  174,
106067 /*   860 */   175,  190,  191,   68,   69,   70,   71,   72,   73,   74,
106068 /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
106069 /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
106070 /*   890 */    22,  165,  150,   23,  150,   25,  150,  166,   91,   92,
106071 /*   900 */   174,  175,   22,  217,  165,  150,  102,  165,  150,  165,
106072 /*   910 */   150,  165,  150,  174,  175,   19,  174,  175,   49,   50,
106073 /*   920 */   165,   86,   87,  165,   23,  165,   25,  165,   24,  174,
106074 /*   930 */   175,  187,  174,  175,  174,  175,  205,   68,   69,   70,
106075 /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
106076 /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
106077 /*   960 */    91,   92,   19,  150,  150,  165,  150,  150,  166,   23,
106078 /*   970 */   150,   25,  160,   20,  174,  175,    1,    2,  165,  165,
106079 /*   980 */   104,  165,  165,   43,  150,  165,  240,  150,   49,   50,
106080 /*   990 */   174,  175,   49,   50,   23,   23,   25,   25,   53,  165,
106081 /*  1000 */   187,  187,  165,   23,  187,   25,  194,  205,  174,  175,
106082 /*  1010 */    71,   72,   69,   70,   71,   72,   73,   74,   75,   76,
106083 /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
106084 /*  1030 */    87,   88,   89,   90,   91,   92,   19,   98,  150,  165,
106085 /*  1040 */   150,  160,  150,   59,   25,   53,  104,   22,  174,  175,
106086 /*  1050 */   213,  138,    5,  165,    1,  165,  150,  165,  150,  150,
106087 /*  1060 */   240,  150,  174,  175,  174,  175,   49,   50,  118,  150,
106088 /*  1070 */    35,  165,   27,  165,  165,  194,  165,  108,  127,   76,
106089 /*  1080 */   174,  175,  174,  175,  165,  174,  175,   70,   71,   72,
106090 /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  166,   82,
106091 /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
106092 /*  1110 */    19,   20,  193,   22,  150,  150,  150,   26,   27,   76,
106093 /*  1120 */   150,   22,    1,  150,  119,  121,  217,   20,   37,  165,
106094 /*  1130 */   165,  165,   16,   19,   20,  165,   22,  205,  165,  119,
106095 /*  1140 */    26,   27,  108,  128,  150,  150,  150,   56,  150,   22,
106096 /*  1150 */   150,   37,  150,  127,  160,   23,  150,   66,  193,  165,
106097 /*  1160 */   165,  165,   16,  165,   23,  165,  150,  165,  174,  175,
106098 /*  1170 */    56,  165,  150,   65,  174,  175,   15,   86,   87,   88,
106099 /*  1180 */    66,  165,  140,  150,   93,   94,   95,  165,  194,   98,
106100 /*  1190 */   174,  175,   22,    3,  164,  193,  174,  175,  165,  150,
106101 /*  1200 */    86,   87,    4,  180,  150,  248,  251,   93,   94,   95,
106102 /*  1210 */   216,  180,   98,  251,  165,  221,  150,  149,    6,  165,
106103 /*  1220 */   129,  130,  131,  132,  133,  134,  193,  150,  174,  175,
106104 /*  1230 */   116,  165,   19,   20,  150,   22,  149,  151,  150,   26,
106105 /*  1240 */    27,  149,  165,  129,  130,  131,  132,  133,  134,  165,
106106 /*  1250 */    37,  174,  175,  165,  149,   19,   20,   13,   22,  150,
106107 /*  1260 */   150,  150,   26,   27,  146,  147,  151,  150,   25,   56,
106108 /*  1270 */   152,  159,  154,   37,  165,  165,  165,  193,  160,   66,
106109 /*  1280 */   116,  193,  165,  174,  175,  174,  175,  194,  199,  150,
106110 /*  1290 */   200,  126,   56,  124,  123,  150,  201,  122,  150,   86,
106111 /*  1300 */    87,  150,   66,  193,  165,  202,   93,   94,   95,  150,
106112 /*  1310 */   165,   98,  194,  165,  125,   22,  165,  150,  150,   26,
106113 /*  1320 */    27,  135,   86,   87,  165,  174,  175,  203,  226,   93,
106114 /*  1330 */    94,   95,  165,  165,   98,  150,  218,  150,  193,  157,
106115 /*  1340 */   118,  157,  129,  130,  131,  132,  133,  134,    5,  104,
106116 /*  1350 */   165,  211,  165,   10,   11,   12,   13,   14,  150,   66,
106117 /*  1360 */    17,  174,  175,  210,  246,  129,  130,  131,  132,  133,
106118 /*  1370 */   134,  150,  210,  165,   31,  121,   33,  150,  150,   86,
106119 /*  1380 */    87,  176,  174,  175,  150,   42,  165,   94,  211,  210,
106120 /*  1390 */   150,   98,  165,  165,  211,  174,  175,  150,   55,  165,
106121 /*  1400 */    57,  150,  174,  175,   61,  165,  150,   64,  174,  175,
106122 /*  1410 */   150,  150,  165,  150,  174,  175,  165,  104,  150,  184,
106123 /*  1420 */   150,  165,  129,  130,  131,  165,  165,  150,  165,  150,
106124 /*  1430 */   150,  176,  150,  165,   47,  165,  150,  150,  176,  103,
106125 /*  1440 */   150,   22,  165,  178,  165,  165,  179,  165,  105,  106,
106126 /*  1450 */   107,  165,  165,  229,  111,  165,   92,  176,  229,  116,
106127 /*  1460 */   184,  176,  179,  156,  176,  176,   18,  157,  156,  237,
106128 /*  1470 */    45,  157,  156,  135,  157,  157,  238,  156,   68,  157,
106129 /*  1480 */   189,  189,  139,  219,   22,  157,   18,  192,  192,  192,
106130 /*  1490 */   192,  189,  219,  199,  157,  242,   40,  157,  199,  242,
106131 /*  1500 */   153,  157,   38,  245,  196,  166,  232,  198,  177,  177,
106132 /*  1510 */   232,  227,  209,  178,  166,  182,  166,  148,  177,  177,
106133 /*  1520 */   209,  196,  177,  199,  209,  199,  166,  208,   92,  195,
106134 /*  1530 */   174,  174,  183,  252,  183,  183,  252,  191,  252,  235,
106135 /*  1540 */   186,  241,  241,  252,  186,  252,  252,  252,  252,  252,
106136 /*  1550 */   252,  252,  252,  252,  252,  252,  236,
106137};
106138#define YY_SHIFT_USE_DFLT (-74)
106139#define YY_SHIFT_COUNT (418)
106140#define YY_SHIFT_MIN   (-73)
106141#define YY_SHIFT_MAX   (1468)
106142static const short yy_shift_ofst[] = {
106143 /*     0 */   975, 1114, 1343, 1114, 1213, 1213,   90,   90,    0,  -19,
106144 /*    10 */  1213, 1213, 1213, 1213, 1213,  345,  445,  721, 1091, 1213,
106145 /*    20 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
106146 /*    30 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
106147 /*    40 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213,
106148 /*    50 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
106149 /*    60 */  1213,  199,  445,  445,  835,  835,  365, 1164,   55,  647,
106150 /*    70 */   573,  499,  425,  351,  277,  203,  129,  795,  795,  795,
106151 /*    80 */   795,  795,  795,  795,  795,  795,  795,  795,  795,  795,
106152 /*    90 */   795,  795,  795,  795,  795,  869,  795,  943, 1017, 1017,
106153 /*   100 */   -69,  -45,  -45,  -45,  -45,  -45,   -1,   58,  138,  100,
106154 /*   110 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
106155 /*   120 */   445,  445,  445,  445,  445,  445,  537,  438,  445,  445,
106156 /*   130 */   445,  445,  445,  365,  807, 1436,  -74,  -74,  -74, 1293,
106157 /*   140 */    73,  434,  434,  311,  314,  290,  283,  286,  540,  467,
106158 /*   150 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
106159 /*   160 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
106160 /*   170 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
106161 /*   180 */   445,  445,   65,  722,  722,  722,  688,  266, 1164, 1164,
106162 /*   190 */  1164,  -74,  -74,  -74,  136,  168,  168,  234,  360,  360,
106163 /*   200 */   360,  430,  372,  435,  352,  278,  126,  -36,  -36,  -36,
106164 /*   210 */   -36,  421,  651,  -36,  -36,  592,  292,  212,  623,  158,
106165 /*   220 */   204,  204,  505,  158,  505,  144,  365,  154,  365,  154,
106166 /*   230 */   645,  154,  204,  154,  154,  535,  548,  548,  365,  387,
106167 /*   240 */   508,  233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410,
106168 /*   250 */  1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410,
106169 /*   260 */  1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222,
106170 /*   270 */  1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313,
106171 /*   280 */  1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254,
106172 /*   290 */  1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164,
106173 /*   300 */  1243, 1244, 1244, 1212, 1212, 1212, 1212,  -74,  -74,  -74,
106174 /*   310 */   -74,  -74,  -74,  939,  104,  680,  571,  327,    1,  980,
106175 /*   320 */    26,  972,  971,  946,  901,  870,  830,  806,   54,   21,
106176 /*   330 */   -73,  510,  242, 1198, 1190, 1170, 1042, 1161, 1108, 1146,
106177 /*   340 */  1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116,
106178 /*   350 */  1121, 1005, 1099,  951, 1043, 1003,  969, 1045, 1035,  950,
106179 /*   360 */  1053, 1047, 1025,  942,  913,  992, 1019,  945,  984,  940,
106180 /*   370 */   876,  904,  953,  896,  748,  804,  880,  786,  868,  819,
106181 /*   380 */   805,  810,  773,  751,  766,  706,  716,  691,  681,  568,
106182 /*   390 */   655,  638,  676,  516,  541,  594,  599,  567,  541,  534,
106183 /*   400 */   507,  527,  498,  523,  466,  382,  409,  384,  357,    6,
106184 /*   410 */   240,  224,  143,   62,   18,   71,   39,    9,    5,
106185};
106186#define YY_REDUCE_USE_DFLT (-142)
106187#define YY_REDUCE_COUNT (312)
106188#define YY_REDUCE_MIN   (-141)
106189#define YY_REDUCE_MAX   (1369)
106190static const short yy_reduce_ofst[] = {
106191 /*     0 */  -141,  994, 1118,  223,  157,  -53,   93,   89,   83,  375,
106192 /*    10 */   386,  381,  379,  308,  295,  325,  -47,   27, 1240, 1234,
106193 /*    20 */  1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022,
106194 /*    30 */  1016, 1000,  911,  908,  906,  890,  888,  874,  834,  816,
106195 /*    40 */   800,  760,  758,  755,  742,  739,  726,  685,  672,  668,
106196 /*    50 */   665,  652,  611,  609,  607,  604,  591,  578,  526,  519,
106197 /*    60 */   453,  474,  454,  461,  443,  245,  442,  473,  484,  484,
106198 /*    70 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
106199 /*    80 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
106200 /*    90 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
106201 /*   100 */   484,  484,  484,  484,  484,  484,  484,  130,  484,  484,
106202 /*   110 */  1145,  909, 1110, 1088, 1084, 1033, 1002,  965,  820,  837,
106203 /*   120 */   746,  686,  612,  817,  610,  919,  221,  563,  814,  813,
106204 /*   130 */   744,  669,  470,  543,  484,  484,  484,  484,  484,  291,
106205 /*   140 */   569,  671,  658,  970, 1290, 1287, 1286, 1282,  518,  518,
106206 /*   150 */  1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251,
106207 /*   160 */  1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066,
106208 /*   170 */  1049, 1006,  998,  996,  995,  973,  970,  966,  964,  892,
106209 /*   180 */   762,  -52,  881,  932,  802,  731,  619,  812,  664,  660,
106210 /*   190 */   627,  392,  331,  124, 1358, 1357, 1356, 1354, 1352, 1351,
106211 /*   200 */  1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334,
106212 /*   210 */  1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326,
106213 /*   220 */  1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342,
106214 /*   230 */  1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309,
106215 /*   240 */  1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302,
106216 /*   250 */  1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291,
106217 /*   260 */  1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310,
106218 /*   270 */  1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281,
106219 /*   280 */  1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140,
106220 /*   290 */  1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093,
106221 /*   300 */  1112, 1115, 1086, 1105, 1092, 1087, 1068,  962,  955,  957,
106222 /*   310 */  1031, 1023, 1030,
106223};
106224static const YYACTIONTYPE yy_default[] = {
106225 /*     0 */   635,  870,  959,  959,  959,  870,  899,  899,  959,  759,
106226 /*    10 */   959,  959,  959,  959,  868,  959,  959,  933,  959,  959,
106227 /*    20 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106228 /*    30 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106229 /*    40 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106230 /*    50 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106231 /*    60 */   959,  959,  959,  959,  899,  899,  674,  763,  794,  959,
106232 /*    70 */   959,  959,  959,  959,  959,  959,  959,  932,  934,  809,
106233 /*    80 */   808,  802,  801,  912,  774,  799,  792,  785,  796,  871,
106234 /*    90 */   864,  865,  863,  867,  872,  959,  795,  831,  848,  830,
106235 /*   100 */   842,  847,  854,  846,  843,  833,  832,  666,  834,  835,
106236 /*   110 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106237 /*   120 */   959,  959,  959,  959,  959,  959,  661,  728,  959,  959,
106238 /*   130 */   959,  959,  959,  959,  836,  837,  851,  850,  849,  959,
106239 /*   140 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106240 /*   150 */   959,  939,  937,  959,  883,  959,  959,  959,  959,  959,
106241 /*   160 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106242 /*   170 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106243 /*   180 */   959,  641,  959,  759,  759,  759,  635,  959,  959,  959,
106244 /*   190 */   959,  951,  763,  753,  719,  959,  959,  959,  959,  959,
106245 /*   200 */   959,  959,  959,  959,  959,  959,  959,  804,  742,  922,
106246 /*   210 */   924,  959,  905,  740,  663,  761,  676,  751,  643,  798,
106247 /*   220 */   776,  776,  917,  798,  917,  700,  959,  788,  959,  788,
106248 /*   230 */   697,  788,  776,  788,  788,  866,  959,  959,  959,  760,
106249 /*   240 */   751,  959,  944,  767,  767,  936,  936,  767,  810,  732,
106250 /*   250 */   798,  739,  739,  739,  739,  767,  798,  810,  732,  732,
106251 /*   260 */   767,  658,  911,  909,  767,  767,  658,  767,  658,  767,
106252 /*   270 */   658,  876,  730,  730,  730,  715,  880,  880,  876,  730,
106253 /*   280 */   700,  730,  715,  730,  730,  780,  775,  780,  775,  780,
106254 /*   290 */   775,  767,  767,  959,  793,  781,  791,  789,  798,  959,
106255 /*   300 */   718,  651,  651,  640,  640,  640,  640,  956,  956,  951,
106256 /*   310 */   702,  702,  684,  959,  959,  959,  959,  959,  959,  959,
106257 /*   320 */   885,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106258 /*   330 */   959,  959,  959,  959,  636,  946,  959,  959,  943,  959,
106259 /*   340 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106260 /*   350 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  915,
106261 /*   360 */   959,  959,  959,  959,  959,  959,  908,  907,  959,  959,
106262 /*   370 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106263 /*   380 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
106264 /*   390 */   959,  959,  959,  959,  790,  959,  782,  959,  869,  959,
106265 /*   400 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  745,
106266 /*   410 */   819,  959,  818,  822,  817,  668,  959,  649,  959,  632,
106267 /*   420 */   637,  955,  958,  957,  954,  953,  952,  947,  945,  942,
106268 /*   430 */   941,  940,  938,  935,  931,  889,  887,  894,  893,  892,
106269 /*   440 */   891,  890,  888,  886,  884,  805,  803,  800,  797,  930,
106270 /*   450 */   882,  741,  738,  737,  657,  948,  914,  923,  921,  811,
106271 /*   460 */   920,  919,  918,  916,  913,  900,  807,  806,  733,  874,
106272 /*   470 */   873,  660,  904,  903,  902,  906,  910,  901,  769,  659,
106273 /*   480 */   656,  665,  722,  721,  729,  727,  726,  725,  724,  723,
106274 /*   490 */   720,  667,  675,  686,  714,  699,  698,  879,  881,  878,
106275 /*   500 */   877,  707,  706,  712,  711,  710,  709,  708,  705,  704,
106276 /*   510 */   703,  696,  695,  701,  694,  717,  716,  713,  693,  736,
106277 /*   520 */   735,  734,  731,  692,  691,  690,  822,  689,  688,  828,
106278 /*   530 */   827,  815,  858,  756,  755,  754,  766,  765,  778,  777,
106279 /*   540 */   813,  812,  779,  764,  758,  757,  773,  772,  771,  770,
106280 /*   550 */   762,  752,  784,  787,  786,  783,  860,  768,  857,  929,
106281 /*   560 */   928,  927,  926,  925,  862,  861,  829,  826,  679,  680,
106282 /*   570 */   898,  896,  897,  895,  682,  681,  678,  677,  859,  747,
106283 /*   580 */   746,  855,  852,  844,  840,  856,  853,  845,  841,  839,
106284 /*   590 */   838,  824,  823,  821,  820,  816,  825,  670,  748,  744,
106285 /*   600 */   743,  814,  750,  749,  687,  685,  683,  664,  662,  655,
106286 /*   610 */   653,  652,  654,  650,  648,  647,  646,  645,  644,  673,
106287 /*   620 */   672,  671,  669,  668,  642,  639,  638,  634,  633,  631,
106288};
106289
106290/* The next table maps tokens into fallback tokens.  If a construct
106291** like the following:
106292**
106293**      %fallback ID X Y Z.
106294**
106295** appears in the grammar, then ID becomes a fallback token for X, Y,
106296** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
106297** but it does not parse, the type of the token is changed to ID and
106298** the parse is retried before an error is thrown.
106299*/
106300#ifdef YYFALLBACK
106301static const YYCODETYPE yyFallback[] = {
106302    0,  /*          $ => nothing */
106303    0,  /*       SEMI => nothing */
106304   26,  /*    EXPLAIN => ID */
106305   26,  /*      QUERY => ID */
106306   26,  /*       PLAN => ID */
106307   26,  /*      BEGIN => ID */
106308    0,  /* TRANSACTION => nothing */
106309   26,  /*   DEFERRED => ID */
106310   26,  /*  IMMEDIATE => ID */
106311   26,  /*  EXCLUSIVE => ID */
106312    0,  /*     COMMIT => nothing */
106313   26,  /*        END => ID */
106314   26,  /*   ROLLBACK => ID */
106315   26,  /*  SAVEPOINT => ID */
106316   26,  /*    RELEASE => ID */
106317    0,  /*         TO => nothing */
106318    0,  /*      TABLE => nothing */
106319    0,  /*     CREATE => nothing */
106320   26,  /*         IF => ID */
106321    0,  /*        NOT => nothing */
106322    0,  /*     EXISTS => nothing */
106323   26,  /*       TEMP => ID */
106324    0,  /*         LP => nothing */
106325    0,  /*         RP => nothing */
106326    0,  /*         AS => nothing */
106327    0,  /*      COMMA => nothing */
106328    0,  /*         ID => nothing */
106329    0,  /*    INDEXED => nothing */
106330   26,  /*      ABORT => ID */
106331   26,  /*     ACTION => ID */
106332   26,  /*      AFTER => ID */
106333   26,  /*    ANALYZE => ID */
106334   26,  /*        ASC => ID */
106335   26,  /*     ATTACH => ID */
106336   26,  /*     BEFORE => ID */
106337   26,  /*         BY => ID */
106338   26,  /*    CASCADE => ID */
106339   26,  /*       CAST => ID */
106340   26,  /*   COLUMNKW => ID */
106341   26,  /*   CONFLICT => ID */
106342   26,  /*   DATABASE => ID */
106343   26,  /*       DESC => ID */
106344   26,  /*     DETACH => ID */
106345   26,  /*       EACH => ID */
106346   26,  /*       FAIL => ID */
106347   26,  /*        FOR => ID */
106348   26,  /*     IGNORE => ID */
106349   26,  /*  INITIALLY => ID */
106350   26,  /*    INSTEAD => ID */
106351   26,  /*    LIKE_KW => ID */
106352   26,  /*      MATCH => ID */
106353   26,  /*         NO => ID */
106354   26,  /*        KEY => ID */
106355   26,  /*         OF => ID */
106356   26,  /*     OFFSET => ID */
106357   26,  /*     PRAGMA => ID */
106358   26,  /*      RAISE => ID */
106359   26,  /*    REPLACE => ID */
106360   26,  /*   RESTRICT => ID */
106361   26,  /*        ROW => ID */
106362   26,  /*    TRIGGER => ID */
106363   26,  /*     VACUUM => ID */
106364   26,  /*       VIEW => ID */
106365   26,  /*    VIRTUAL => ID */
106366   26,  /*    REINDEX => ID */
106367   26,  /*     RENAME => ID */
106368   26,  /*   CTIME_KW => ID */
106369};
106370#endif /* YYFALLBACK */
106371
106372/* The following structure represents a single element of the
106373** parser's stack.  Information stored includes:
106374**
106375**   +  The state number for the parser at this level of the stack.
106376**
106377**   +  The value of the token stored at this level of the stack.
106378**      (In other words, the "major" token.)
106379**
106380**   +  The semantic value stored at this level of the stack.  This is
106381**      the information used by the action routines in the grammar.
106382**      It is sometimes called the "minor" token.
106383*/
106384struct yyStackEntry {
106385  YYACTIONTYPE stateno;  /* The state-number */
106386  YYCODETYPE major;      /* The major token value.  This is the code
106387                         ** number for the token at this stack level */
106388  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
106389                         ** is the value of the token  */
106390};
106391typedef struct yyStackEntry yyStackEntry;
106392
106393/* The state of the parser is completely contained in an instance of
106394** the following structure */
106395struct yyParser {
106396  int yyidx;                    /* Index of top element in stack */
106397#ifdef YYTRACKMAXSTACKDEPTH
106398  int yyidxMax;                 /* Maximum value of yyidx */
106399#endif
106400  int yyerrcnt;                 /* Shifts left before out of the error */
106401  sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
106402#if YYSTACKDEPTH<=0
106403  int yystksz;                  /* Current side of the stack */
106404  yyStackEntry *yystack;        /* The parser's stack */
106405#else
106406  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
106407#endif
106408};
106409typedef struct yyParser yyParser;
106410
106411#ifndef NDEBUG
106412/* #include <stdio.h> */
106413static FILE *yyTraceFILE = 0;
106414static char *yyTracePrompt = 0;
106415#endif /* NDEBUG */
106416
106417#ifndef NDEBUG
106418/*
106419** Turn parser tracing on by giving a stream to which to write the trace
106420** and a prompt to preface each trace message.  Tracing is turned off
106421** by making either argument NULL
106422**
106423** Inputs:
106424** <ul>
106425** <li> A FILE* to which trace output should be written.
106426**      If NULL, then tracing is turned off.
106427** <li> A prefix string written at the beginning of every
106428**      line of trace output.  If NULL, then tracing is
106429**      turned off.
106430** </ul>
106431**
106432** Outputs:
106433** None.
106434*/
106435SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
106436  yyTraceFILE = TraceFILE;
106437  yyTracePrompt = zTracePrompt;
106438  if( yyTraceFILE==0 ) yyTracePrompt = 0;
106439  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
106440}
106441#endif /* NDEBUG */
106442
106443#ifndef NDEBUG
106444/* For tracing shifts, the names of all terminals and nonterminals
106445** are required.  The following table supplies these names */
106446static const char *const yyTokenName[] = {
106447  "$",             "SEMI",          "EXPLAIN",       "QUERY",
106448  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",
106449  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",
106450  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",
106451  "TABLE",         "CREATE",        "IF",            "NOT",
106452  "EXISTS",        "TEMP",          "LP",            "RP",
106453  "AS",            "COMMA",         "ID",            "INDEXED",
106454  "ABORT",         "ACTION",        "AFTER",         "ANALYZE",
106455  "ASC",           "ATTACH",        "BEFORE",        "BY",
106456  "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",
106457  "DATABASE",      "DESC",          "DETACH",        "EACH",
106458  "FAIL",          "FOR",           "IGNORE",        "INITIALLY",
106459  "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",
106460  "KEY",           "OF",            "OFFSET",        "PRAGMA",
106461  "RAISE",         "REPLACE",       "RESTRICT",      "ROW",
106462  "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",
106463  "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",
106464  "OR",            "AND",           "IS",            "BETWEEN",
106465  "IN",            "ISNULL",        "NOTNULL",       "NE",
106466  "EQ",            "GT",            "LE",            "LT",
106467  "GE",            "ESCAPE",        "BITAND",        "BITOR",
106468  "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",
106469  "STAR",          "SLASH",         "REM",           "CONCAT",
106470  "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",
106471  "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",
106472  "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",
106473  "ON",            "INSERT",        "DELETE",        "UPDATE",
106474  "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",
106475  "UNION",         "ALL",           "EXCEPT",        "INTERSECT",
106476  "SELECT",        "DISTINCT",      "DOT",           "FROM",
106477  "JOIN",          "USING",         "ORDER",         "GROUP",
106478  "HAVING",        "LIMIT",         "WHERE",         "INTO",
106479  "VALUES",        "INTEGER",       "FLOAT",         "BLOB",
106480  "REGISTER",      "VARIABLE",      "CASE",          "WHEN",
106481  "THEN",          "ELSE",          "INDEX",         "ALTER",
106482  "ADD",           "error",         "input",         "cmdlist",
106483  "ecmd",          "explain",       "cmdx",          "cmd",
106484  "transtype",     "trans_opt",     "nm",            "savepoint_opt",
106485  "create_table",  "create_table_args",  "createkw",      "temp",
106486  "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
106487  "select",        "column",        "columnid",      "type",
106488  "carglist",      "id",            "ids",           "typetoken",
106489  "typename",      "signed",        "plus_num",      "minus_num",
106490  "carg",          "ccons",         "term",          "expr",
106491  "onconf",        "sortorder",     "autoinc",       "idxlist_opt",
106492  "refargs",       "defer_subclause",  "refarg",        "refact",
106493  "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",
106494  "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",
106495  "ifexists",      "fullname",      "oneselect",     "multiselect_op",
106496  "distinct",      "selcollist",    "from",          "where_opt",
106497  "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",
106498  "sclp",          "as",            "seltablist",    "stl_prefix",
106499  "joinop",        "indexed_opt",   "on_opt",        "using_opt",
106500  "joinop2",       "inscollist",    "sortlist",      "sortitem",
106501  "nexprlist",     "setlist",       "insert_cmd",    "inscollist_opt",
106502  "itemlist",      "exprlist",      "likeop",        "between_op",
106503  "in_op",         "case_operand",  "case_exprlist",  "case_else",
106504  "uniqueflag",    "collate",       "nmnum",         "plus_opt",
106505  "number",        "trigger_decl",  "trigger_cmd_list",  "trigger_time",
106506  "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd",
106507  "trnm",          "tridxby",       "database_kw_opt",  "key_opt",
106508  "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist",
106509  "vtabarg",       "vtabargtoken",  "lp",            "anylist",
106510};
106511#endif /* NDEBUG */
106512
106513#ifndef NDEBUG
106514/* For tracing reduce actions, the names of all rules are required.
106515*/
106516static const char *const yyRuleName[] = {
106517 /*   0 */ "input ::= cmdlist",
106518 /*   1 */ "cmdlist ::= cmdlist ecmd",
106519 /*   2 */ "cmdlist ::= ecmd",
106520 /*   3 */ "ecmd ::= SEMI",
106521 /*   4 */ "ecmd ::= explain cmdx SEMI",
106522 /*   5 */ "explain ::=",
106523 /*   6 */ "explain ::= EXPLAIN",
106524 /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
106525 /*   8 */ "cmdx ::= cmd",
106526 /*   9 */ "cmd ::= BEGIN transtype trans_opt",
106527 /*  10 */ "trans_opt ::=",
106528 /*  11 */ "trans_opt ::= TRANSACTION",
106529 /*  12 */ "trans_opt ::= TRANSACTION nm",
106530 /*  13 */ "transtype ::=",
106531 /*  14 */ "transtype ::= DEFERRED",
106532 /*  15 */ "transtype ::= IMMEDIATE",
106533 /*  16 */ "transtype ::= EXCLUSIVE",
106534 /*  17 */ "cmd ::= COMMIT trans_opt",
106535 /*  18 */ "cmd ::= END trans_opt",
106536 /*  19 */ "cmd ::= ROLLBACK trans_opt",
106537 /*  20 */ "savepoint_opt ::= SAVEPOINT",
106538 /*  21 */ "savepoint_opt ::=",
106539 /*  22 */ "cmd ::= SAVEPOINT nm",
106540 /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
106541 /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
106542 /*  25 */ "cmd ::= create_table create_table_args",
106543 /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
106544 /*  27 */ "createkw ::= CREATE",
106545 /*  28 */ "ifnotexists ::=",
106546 /*  29 */ "ifnotexists ::= IF NOT EXISTS",
106547 /*  30 */ "temp ::= TEMP",
106548 /*  31 */ "temp ::=",
106549 /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
106550 /*  33 */ "create_table_args ::= AS select",
106551 /*  34 */ "columnlist ::= columnlist COMMA column",
106552 /*  35 */ "columnlist ::= column",
106553 /*  36 */ "column ::= columnid type carglist",
106554 /*  37 */ "columnid ::= nm",
106555 /*  38 */ "id ::= ID",
106556 /*  39 */ "id ::= INDEXED",
106557 /*  40 */ "ids ::= ID|STRING",
106558 /*  41 */ "nm ::= id",
106559 /*  42 */ "nm ::= STRING",
106560 /*  43 */ "nm ::= JOIN_KW",
106561 /*  44 */ "type ::=",
106562 /*  45 */ "type ::= typetoken",
106563 /*  46 */ "typetoken ::= typename",
106564 /*  47 */ "typetoken ::= typename LP signed RP",
106565 /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
106566 /*  49 */ "typename ::= ids",
106567 /*  50 */ "typename ::= typename ids",
106568 /*  51 */ "signed ::= plus_num",
106569 /*  52 */ "signed ::= minus_num",
106570 /*  53 */ "carglist ::= carglist carg",
106571 /*  54 */ "carglist ::=",
106572 /*  55 */ "carg ::= CONSTRAINT nm ccons",
106573 /*  56 */ "carg ::= ccons",
106574 /*  57 */ "ccons ::= DEFAULT term",
106575 /*  58 */ "ccons ::= DEFAULT LP expr RP",
106576 /*  59 */ "ccons ::= DEFAULT PLUS term",
106577 /*  60 */ "ccons ::= DEFAULT MINUS term",
106578 /*  61 */ "ccons ::= DEFAULT id",
106579 /*  62 */ "ccons ::= NULL onconf",
106580 /*  63 */ "ccons ::= NOT NULL onconf",
106581 /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
106582 /*  65 */ "ccons ::= UNIQUE onconf",
106583 /*  66 */ "ccons ::= CHECK LP expr RP",
106584 /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
106585 /*  68 */ "ccons ::= defer_subclause",
106586 /*  69 */ "ccons ::= COLLATE ids",
106587 /*  70 */ "autoinc ::=",
106588 /*  71 */ "autoinc ::= AUTOINCR",
106589 /*  72 */ "refargs ::=",
106590 /*  73 */ "refargs ::= refargs refarg",
106591 /*  74 */ "refarg ::= MATCH nm",
106592 /*  75 */ "refarg ::= ON INSERT refact",
106593 /*  76 */ "refarg ::= ON DELETE refact",
106594 /*  77 */ "refarg ::= ON UPDATE refact",
106595 /*  78 */ "refact ::= SET NULL",
106596 /*  79 */ "refact ::= SET DEFAULT",
106597 /*  80 */ "refact ::= CASCADE",
106598 /*  81 */ "refact ::= RESTRICT",
106599 /*  82 */ "refact ::= NO ACTION",
106600 /*  83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
106601 /*  84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
106602 /*  85 */ "init_deferred_pred_opt ::=",
106603 /*  86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
106604 /*  87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
106605 /*  88 */ "conslist_opt ::=",
106606 /*  89 */ "conslist_opt ::= COMMA conslist",
106607 /*  90 */ "conslist ::= conslist COMMA tcons",
106608 /*  91 */ "conslist ::= conslist tcons",
106609 /*  92 */ "conslist ::= tcons",
106610 /*  93 */ "tcons ::= CONSTRAINT nm",
106611 /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
106612 /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
106613 /*  96 */ "tcons ::= CHECK LP expr RP onconf",
106614 /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
106615 /*  98 */ "defer_subclause_opt ::=",
106616 /*  99 */ "defer_subclause_opt ::= defer_subclause",
106617 /* 100 */ "onconf ::=",
106618 /* 101 */ "onconf ::= ON CONFLICT resolvetype",
106619 /* 102 */ "orconf ::=",
106620 /* 103 */ "orconf ::= OR resolvetype",
106621 /* 104 */ "resolvetype ::= raisetype",
106622 /* 105 */ "resolvetype ::= IGNORE",
106623 /* 106 */ "resolvetype ::= REPLACE",
106624 /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
106625 /* 108 */ "ifexists ::= IF EXISTS",
106626 /* 109 */ "ifexists ::=",
106627 /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
106628 /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
106629 /* 112 */ "cmd ::= select",
106630 /* 113 */ "select ::= oneselect",
106631 /* 114 */ "select ::= select multiselect_op oneselect",
106632 /* 115 */ "multiselect_op ::= UNION",
106633 /* 116 */ "multiselect_op ::= UNION ALL",
106634 /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
106635 /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
106636 /* 119 */ "distinct ::= DISTINCT",
106637 /* 120 */ "distinct ::= ALL",
106638 /* 121 */ "distinct ::=",
106639 /* 122 */ "sclp ::= selcollist COMMA",
106640 /* 123 */ "sclp ::=",
106641 /* 124 */ "selcollist ::= sclp expr as",
106642 /* 125 */ "selcollist ::= sclp STAR",
106643 /* 126 */ "selcollist ::= sclp nm DOT STAR",
106644 /* 127 */ "as ::= AS nm",
106645 /* 128 */ "as ::= ids",
106646 /* 129 */ "as ::=",
106647 /* 130 */ "from ::=",
106648 /* 131 */ "from ::= FROM seltablist",
106649 /* 132 */ "stl_prefix ::= seltablist joinop",
106650 /* 133 */ "stl_prefix ::=",
106651 /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
106652 /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
106653 /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
106654 /* 137 */ "dbnm ::=",
106655 /* 138 */ "dbnm ::= DOT nm",
106656 /* 139 */ "fullname ::= nm dbnm",
106657 /* 140 */ "joinop ::= COMMA|JOIN",
106658 /* 141 */ "joinop ::= JOIN_KW JOIN",
106659 /* 142 */ "joinop ::= JOIN_KW nm JOIN",
106660 /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
106661 /* 144 */ "on_opt ::= ON expr",
106662 /* 145 */ "on_opt ::=",
106663 /* 146 */ "indexed_opt ::=",
106664 /* 147 */ "indexed_opt ::= INDEXED BY nm",
106665 /* 148 */ "indexed_opt ::= NOT INDEXED",
106666 /* 149 */ "using_opt ::= USING LP inscollist RP",
106667 /* 150 */ "using_opt ::=",
106668 /* 151 */ "orderby_opt ::=",
106669 /* 152 */ "orderby_opt ::= ORDER BY sortlist",
106670 /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder",
106671 /* 154 */ "sortlist ::= sortitem sortorder",
106672 /* 155 */ "sortitem ::= expr",
106673 /* 156 */ "sortorder ::= ASC",
106674 /* 157 */ "sortorder ::= DESC",
106675 /* 158 */ "sortorder ::=",
106676 /* 159 */ "groupby_opt ::=",
106677 /* 160 */ "groupby_opt ::= GROUP BY nexprlist",
106678 /* 161 */ "having_opt ::=",
106679 /* 162 */ "having_opt ::= HAVING expr",
106680 /* 163 */ "limit_opt ::=",
106681 /* 164 */ "limit_opt ::= LIMIT expr",
106682 /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr",
106683 /* 166 */ "limit_opt ::= LIMIT expr COMMA expr",
106684 /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
106685 /* 168 */ "where_opt ::=",
106686 /* 169 */ "where_opt ::= WHERE expr",
106687 /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
106688 /* 171 */ "setlist ::= setlist COMMA nm EQ expr",
106689 /* 172 */ "setlist ::= nm EQ expr",
106690 /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
106691 /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
106692 /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
106693 /* 176 */ "insert_cmd ::= INSERT orconf",
106694 /* 177 */ "insert_cmd ::= REPLACE",
106695 /* 178 */ "itemlist ::= itemlist COMMA expr",
106696 /* 179 */ "itemlist ::= expr",
106697 /* 180 */ "inscollist_opt ::=",
106698 /* 181 */ "inscollist_opt ::= LP inscollist RP",
106699 /* 182 */ "inscollist ::= inscollist COMMA nm",
106700 /* 183 */ "inscollist ::= nm",
106701 /* 184 */ "expr ::= term",
106702 /* 185 */ "expr ::= LP expr RP",
106703 /* 186 */ "term ::= NULL",
106704 /* 187 */ "expr ::= id",
106705 /* 188 */ "expr ::= JOIN_KW",
106706 /* 189 */ "expr ::= nm DOT nm",
106707 /* 190 */ "expr ::= nm DOT nm DOT nm",
106708 /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
106709 /* 192 */ "term ::= STRING",
106710 /* 193 */ "expr ::= REGISTER",
106711 /* 194 */ "expr ::= VARIABLE",
106712 /* 195 */ "expr ::= expr COLLATE ids",
106713 /* 196 */ "expr ::= CAST LP expr AS typetoken RP",
106714 /* 197 */ "expr ::= ID LP distinct exprlist RP",
106715 /* 198 */ "expr ::= ID LP STAR RP",
106716 /* 199 */ "term ::= CTIME_KW",
106717 /* 200 */ "expr ::= expr AND expr",
106718 /* 201 */ "expr ::= expr OR expr",
106719 /* 202 */ "expr ::= expr LT|GT|GE|LE expr",
106720 /* 203 */ "expr ::= expr EQ|NE expr",
106721 /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
106722 /* 205 */ "expr ::= expr PLUS|MINUS expr",
106723 /* 206 */ "expr ::= expr STAR|SLASH|REM expr",
106724 /* 207 */ "expr ::= expr CONCAT expr",
106725 /* 208 */ "likeop ::= LIKE_KW",
106726 /* 209 */ "likeop ::= NOT LIKE_KW",
106727 /* 210 */ "likeop ::= MATCH",
106728 /* 211 */ "likeop ::= NOT MATCH",
106729 /* 212 */ "expr ::= expr likeop expr",
106730 /* 213 */ "expr ::= expr likeop expr ESCAPE expr",
106731 /* 214 */ "expr ::= expr ISNULL|NOTNULL",
106732 /* 215 */ "expr ::= expr NOT NULL",
106733 /* 216 */ "expr ::= expr IS expr",
106734 /* 217 */ "expr ::= expr IS NOT expr",
106735 /* 218 */ "expr ::= NOT expr",
106736 /* 219 */ "expr ::= BITNOT expr",
106737 /* 220 */ "expr ::= MINUS expr",
106738 /* 221 */ "expr ::= PLUS expr",
106739 /* 222 */ "between_op ::= BETWEEN",
106740 /* 223 */ "between_op ::= NOT BETWEEN",
106741 /* 224 */ "expr ::= expr between_op expr AND expr",
106742 /* 225 */ "in_op ::= IN",
106743 /* 226 */ "in_op ::= NOT IN",
106744 /* 227 */ "expr ::= expr in_op LP exprlist RP",
106745 /* 228 */ "expr ::= LP select RP",
106746 /* 229 */ "expr ::= expr in_op LP select RP",
106747 /* 230 */ "expr ::= expr in_op nm dbnm",
106748 /* 231 */ "expr ::= EXISTS LP select RP",
106749 /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
106750 /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
106751 /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
106752 /* 235 */ "case_else ::= ELSE expr",
106753 /* 236 */ "case_else ::=",
106754 /* 237 */ "case_operand ::= expr",
106755 /* 238 */ "case_operand ::=",
106756 /* 239 */ "exprlist ::= nexprlist",
106757 /* 240 */ "exprlist ::=",
106758 /* 241 */ "nexprlist ::= nexprlist COMMA expr",
106759 /* 242 */ "nexprlist ::= expr",
106760 /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
106761 /* 244 */ "uniqueflag ::= UNIQUE",
106762 /* 245 */ "uniqueflag ::=",
106763 /* 246 */ "idxlist_opt ::=",
106764 /* 247 */ "idxlist_opt ::= LP idxlist RP",
106765 /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
106766 /* 249 */ "idxlist ::= nm collate sortorder",
106767 /* 250 */ "collate ::=",
106768 /* 251 */ "collate ::= COLLATE ids",
106769 /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
106770 /* 253 */ "cmd ::= VACUUM",
106771 /* 254 */ "cmd ::= VACUUM nm",
106772 /* 255 */ "cmd ::= PRAGMA nm dbnm",
106773 /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
106774 /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
106775 /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
106776 /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
106777 /* 260 */ "nmnum ::= plus_num",
106778 /* 261 */ "nmnum ::= nm",
106779 /* 262 */ "nmnum ::= ON",
106780 /* 263 */ "nmnum ::= DELETE",
106781 /* 264 */ "nmnum ::= DEFAULT",
106782 /* 265 */ "plus_num ::= plus_opt number",
106783 /* 266 */ "minus_num ::= MINUS number",
106784 /* 267 */ "number ::= INTEGER|FLOAT",
106785 /* 268 */ "plus_opt ::= PLUS",
106786 /* 269 */ "plus_opt ::=",
106787 /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
106788 /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
106789 /* 272 */ "trigger_time ::= BEFORE",
106790 /* 273 */ "trigger_time ::= AFTER",
106791 /* 274 */ "trigger_time ::= INSTEAD OF",
106792 /* 275 */ "trigger_time ::=",
106793 /* 276 */ "trigger_event ::= DELETE|INSERT",
106794 /* 277 */ "trigger_event ::= UPDATE",
106795 /* 278 */ "trigger_event ::= UPDATE OF inscollist",
106796 /* 279 */ "foreach_clause ::=",
106797 /* 280 */ "foreach_clause ::= FOR EACH ROW",
106798 /* 281 */ "when_clause ::=",
106799 /* 282 */ "when_clause ::= WHEN expr",
106800 /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
106801 /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
106802 /* 285 */ "trnm ::= nm",
106803 /* 286 */ "trnm ::= nm DOT nm",
106804 /* 287 */ "tridxby ::=",
106805 /* 288 */ "tridxby ::= INDEXED BY nm",
106806 /* 289 */ "tridxby ::= NOT INDEXED",
106807 /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
106808 /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
106809 /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
106810 /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
106811 /* 294 */ "trigger_cmd ::= select",
106812 /* 295 */ "expr ::= RAISE LP IGNORE RP",
106813 /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
106814 /* 297 */ "raisetype ::= ROLLBACK",
106815 /* 298 */ "raisetype ::= ABORT",
106816 /* 299 */ "raisetype ::= FAIL",
106817 /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
106818 /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
106819 /* 302 */ "cmd ::= DETACH database_kw_opt expr",
106820 /* 303 */ "key_opt ::=",
106821 /* 304 */ "key_opt ::= KEY expr",
106822 /* 305 */ "database_kw_opt ::= DATABASE",
106823 /* 306 */ "database_kw_opt ::=",
106824 /* 307 */ "cmd ::= REINDEX",
106825 /* 308 */ "cmd ::= REINDEX nm dbnm",
106826 /* 309 */ "cmd ::= ANALYZE",
106827 /* 310 */ "cmd ::= ANALYZE nm dbnm",
106828 /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
106829 /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
106830 /* 313 */ "add_column_fullname ::= fullname",
106831 /* 314 */ "kwcolumn_opt ::=",
106832 /* 315 */ "kwcolumn_opt ::= COLUMNKW",
106833 /* 316 */ "cmd ::= create_vtab",
106834 /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
106835 /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
106836 /* 319 */ "vtabarglist ::= vtabarg",
106837 /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
106838 /* 321 */ "vtabarg ::=",
106839 /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
106840 /* 323 */ "vtabargtoken ::= ANY",
106841 /* 324 */ "vtabargtoken ::= lp anylist RP",
106842 /* 325 */ "lp ::= LP",
106843 /* 326 */ "anylist ::=",
106844 /* 327 */ "anylist ::= anylist LP anylist RP",
106845 /* 328 */ "anylist ::= anylist ANY",
106846};
106847#endif /* NDEBUG */
106848
106849
106850#if YYSTACKDEPTH<=0
106851/*
106852** Try to increase the size of the parser stack.
106853*/
106854static void yyGrowStack(yyParser *p){
106855  int newSize;
106856  yyStackEntry *pNew;
106857
106858  newSize = p->yystksz*2 + 100;
106859  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
106860  if( pNew ){
106861    p->yystack = pNew;
106862    p->yystksz = newSize;
106863#ifndef NDEBUG
106864    if( yyTraceFILE ){
106865      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
106866              yyTracePrompt, p->yystksz);
106867    }
106868#endif
106869  }
106870}
106871#endif
106872
106873/*
106874** This function allocates a new parser.
106875** The only argument is a pointer to a function which works like
106876** malloc.
106877**
106878** Inputs:
106879** A pointer to the function used to allocate memory.
106880**
106881** Outputs:
106882** A pointer to a parser.  This pointer is used in subsequent calls
106883** to sqlite3Parser and sqlite3ParserFree.
106884*/
106885SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
106886  yyParser *pParser;
106887  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
106888  if( pParser ){
106889    pParser->yyidx = -1;
106890#ifdef YYTRACKMAXSTACKDEPTH
106891    pParser->yyidxMax = 0;
106892#endif
106893#if YYSTACKDEPTH<=0
106894    pParser->yystack = NULL;
106895    pParser->yystksz = 0;
106896    yyGrowStack(pParser);
106897#endif
106898  }
106899  return pParser;
106900}
106901
106902/* The following function deletes the value associated with a
106903** symbol.  The symbol can be either a terminal or nonterminal.
106904** "yymajor" is the symbol code, and "yypminor" is a pointer to
106905** the value.
106906*/
106907static void yy_destructor(
106908  yyParser *yypParser,    /* The parser */
106909  YYCODETYPE yymajor,     /* Type code for object to destroy */
106910  YYMINORTYPE *yypminor   /* The object to be destroyed */
106911){
106912  sqlite3ParserARG_FETCH;
106913  switch( yymajor ){
106914    /* Here is inserted the actions which take place when a
106915    ** terminal or non-terminal is destroyed.  This can happen
106916    ** when the symbol is popped from the stack during a
106917    ** reduce or during error processing or when a parser is
106918    ** being destroyed before it is finished parsing.
106919    **
106920    ** Note: during a reduce, the only symbols destroyed are those
106921    ** which appear on the RHS of the rule, but which are not used
106922    ** inside the C code.
106923    */
106924    case 160: /* select */
106925    case 194: /* oneselect */
106926{
106927sqlite3SelectDelete(pParse->db, (yypminor->yy387));
106928}
106929      break;
106930    case 174: /* term */
106931    case 175: /* expr */
106932{
106933sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
106934}
106935      break;
106936    case 179: /* idxlist_opt */
106937    case 187: /* idxlist */
106938    case 197: /* selcollist */
106939    case 200: /* groupby_opt */
106940    case 202: /* orderby_opt */
106941    case 204: /* sclp */
106942    case 214: /* sortlist */
106943    case 216: /* nexprlist */
106944    case 217: /* setlist */
106945    case 220: /* itemlist */
106946    case 221: /* exprlist */
106947    case 226: /* case_exprlist */
106948{
106949sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
106950}
106951      break;
106952    case 193: /* fullname */
106953    case 198: /* from */
106954    case 206: /* seltablist */
106955    case 207: /* stl_prefix */
106956{
106957sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
106958}
106959      break;
106960    case 199: /* where_opt */
106961    case 201: /* having_opt */
106962    case 210: /* on_opt */
106963    case 215: /* sortitem */
106964    case 225: /* case_operand */
106965    case 227: /* case_else */
106966    case 238: /* when_clause */
106967    case 243: /* key_opt */
106968{
106969sqlite3ExprDelete(pParse->db, (yypminor->yy314));
106970}
106971      break;
106972    case 211: /* using_opt */
106973    case 213: /* inscollist */
106974    case 219: /* inscollist_opt */
106975{
106976sqlite3IdListDelete(pParse->db, (yypminor->yy384));
106977}
106978      break;
106979    case 234: /* trigger_cmd_list */
106980    case 239: /* trigger_cmd */
106981{
106982sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
106983}
106984      break;
106985    case 236: /* trigger_event */
106986{
106987sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
106988}
106989      break;
106990    default:  break;   /* If no destructor action specified: do nothing */
106991  }
106992}
106993
106994/*
106995** Pop the parser's stack once.
106996**
106997** If there is a destructor routine associated with the token which
106998** is popped from the stack, then call it.
106999**
107000** Return the major token number for the symbol popped.
107001*/
107002static int yy_pop_parser_stack(yyParser *pParser){
107003  YYCODETYPE yymajor;
107004  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
107005
107006  /* There is no mechanism by which the parser stack can be popped below
107007  ** empty in SQLite.  */
107008  if( NEVER(pParser->yyidx<0) ) return 0;
107009#ifndef NDEBUG
107010  if( yyTraceFILE && pParser->yyidx>=0 ){
107011    fprintf(yyTraceFILE,"%sPopping %s\n",
107012      yyTracePrompt,
107013      yyTokenName[yytos->major]);
107014  }
107015#endif
107016  yymajor = yytos->major;
107017  yy_destructor(pParser, yymajor, &yytos->minor);
107018  pParser->yyidx--;
107019  return yymajor;
107020}
107021
107022/*
107023** Deallocate and destroy a parser.  Destructors are all called for
107024** all stack elements before shutting the parser down.
107025**
107026** Inputs:
107027** <ul>
107028** <li>  A pointer to the parser.  This should be a pointer
107029**       obtained from sqlite3ParserAlloc.
107030** <li>  A pointer to a function used to reclaim memory obtained
107031**       from malloc.
107032** </ul>
107033*/
107034SQLITE_PRIVATE void sqlite3ParserFree(
107035  void *p,                    /* The parser to be deleted */
107036  void (*freeProc)(void*)     /* Function used to reclaim memory */
107037){
107038  yyParser *pParser = (yyParser*)p;
107039  /* In SQLite, we never try to destroy a parser that was not successfully
107040  ** created in the first place. */
107041  if( NEVER(pParser==0) ) return;
107042  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
107043#if YYSTACKDEPTH<=0
107044  free(pParser->yystack);
107045#endif
107046  (*freeProc)((void*)pParser);
107047}
107048
107049/*
107050** Return the peak depth of the stack for a parser.
107051*/
107052#ifdef YYTRACKMAXSTACKDEPTH
107053SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
107054  yyParser *pParser = (yyParser*)p;
107055  return pParser->yyidxMax;
107056}
107057#endif
107058
107059/*
107060** Find the appropriate action for a parser given the terminal
107061** look-ahead token iLookAhead.
107062**
107063** If the look-ahead token is YYNOCODE, then check to see if the action is
107064** independent of the look-ahead.  If it is, return the action, otherwise
107065** return YY_NO_ACTION.
107066*/
107067static int yy_find_shift_action(
107068  yyParser *pParser,        /* The parser */
107069  YYCODETYPE iLookAhead     /* The look-ahead token */
107070){
107071  int i;
107072  int stateno = pParser->yystack[pParser->yyidx].stateno;
107073
107074  if( stateno>YY_SHIFT_COUNT
107075   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
107076    return yy_default[stateno];
107077  }
107078  assert( iLookAhead!=YYNOCODE );
107079  i += iLookAhead;
107080  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
107081    if( iLookAhead>0 ){
107082#ifdef YYFALLBACK
107083      YYCODETYPE iFallback;            /* Fallback token */
107084      if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
107085             && (iFallback = yyFallback[iLookAhead])!=0 ){
107086#ifndef NDEBUG
107087        if( yyTraceFILE ){
107088          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
107089             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
107090        }
107091#endif
107092        return yy_find_shift_action(pParser, iFallback);
107093      }
107094#endif
107095#ifdef YYWILDCARD
107096      {
107097        int j = i - iLookAhead + YYWILDCARD;
107098        if(
107099#if YY_SHIFT_MIN+YYWILDCARD<0
107100          j>=0 &&
107101#endif
107102#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
107103          j<YY_ACTTAB_COUNT &&
107104#endif
107105          yy_lookahead[j]==YYWILDCARD
107106        ){
107107#ifndef NDEBUG
107108          if( yyTraceFILE ){
107109            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
107110               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
107111          }
107112#endif /* NDEBUG */
107113          return yy_action[j];
107114        }
107115      }
107116#endif /* YYWILDCARD */
107117    }
107118    return yy_default[stateno];
107119  }else{
107120    return yy_action[i];
107121  }
107122}
107123
107124/*
107125** Find the appropriate action for a parser given the non-terminal
107126** look-ahead token iLookAhead.
107127**
107128** If the look-ahead token is YYNOCODE, then check to see if the action is
107129** independent of the look-ahead.  If it is, return the action, otherwise
107130** return YY_NO_ACTION.
107131*/
107132static int yy_find_reduce_action(
107133  int stateno,              /* Current state number */
107134  YYCODETYPE iLookAhead     /* The look-ahead token */
107135){
107136  int i;
107137#ifdef YYERRORSYMBOL
107138  if( stateno>YY_REDUCE_COUNT ){
107139    return yy_default[stateno];
107140  }
107141#else
107142  assert( stateno<=YY_REDUCE_COUNT );
107143#endif
107144  i = yy_reduce_ofst[stateno];
107145  assert( i!=YY_REDUCE_USE_DFLT );
107146  assert( iLookAhead!=YYNOCODE );
107147  i += iLookAhead;
107148#ifdef YYERRORSYMBOL
107149  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
107150    return yy_default[stateno];
107151  }
107152#else
107153  assert( i>=0 && i<YY_ACTTAB_COUNT );
107154  assert( yy_lookahead[i]==iLookAhead );
107155#endif
107156  return yy_action[i];
107157}
107158
107159/*
107160** The following routine is called if the stack overflows.
107161*/
107162static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
107163   sqlite3ParserARG_FETCH;
107164   yypParser->yyidx--;
107165#ifndef NDEBUG
107166   if( yyTraceFILE ){
107167     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
107168   }
107169#endif
107170   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
107171   /* Here code is inserted which will execute if the parser
107172   ** stack every overflows */
107173
107174  UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
107175  sqlite3ErrorMsg(pParse, "parser stack overflow");
107176  pParse->parseError = 1;
107177   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
107178}
107179
107180/*
107181** Perform a shift action.
107182*/
107183static void yy_shift(
107184  yyParser *yypParser,          /* The parser to be shifted */
107185  int yyNewState,               /* The new state to shift in */
107186  int yyMajor,                  /* The major token to shift in */
107187  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
107188){
107189  yyStackEntry *yytos;
107190  yypParser->yyidx++;
107191#ifdef YYTRACKMAXSTACKDEPTH
107192  if( yypParser->yyidx>yypParser->yyidxMax ){
107193    yypParser->yyidxMax = yypParser->yyidx;
107194  }
107195#endif
107196#if YYSTACKDEPTH>0
107197  if( yypParser->yyidx>=YYSTACKDEPTH ){
107198    yyStackOverflow(yypParser, yypMinor);
107199    return;
107200  }
107201#else
107202  if( yypParser->yyidx>=yypParser->yystksz ){
107203    yyGrowStack(yypParser);
107204    if( yypParser->yyidx>=yypParser->yystksz ){
107205      yyStackOverflow(yypParser, yypMinor);
107206      return;
107207    }
107208  }
107209#endif
107210  yytos = &yypParser->yystack[yypParser->yyidx];
107211  yytos->stateno = (YYACTIONTYPE)yyNewState;
107212  yytos->major = (YYCODETYPE)yyMajor;
107213  yytos->minor = *yypMinor;
107214#ifndef NDEBUG
107215  if( yyTraceFILE && yypParser->yyidx>0 ){
107216    int i;
107217    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
107218    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
107219    for(i=1; i<=yypParser->yyidx; i++)
107220      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
107221    fprintf(yyTraceFILE,"\n");
107222  }
107223#endif
107224}
107225
107226/* The following table contains information about every rule that
107227** is used during the reduce.
107228*/
107229static const struct {
107230  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
107231  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
107232} yyRuleInfo[] = {
107233  { 142, 1 },
107234  { 143, 2 },
107235  { 143, 1 },
107236  { 144, 1 },
107237  { 144, 3 },
107238  { 145, 0 },
107239  { 145, 1 },
107240  { 145, 3 },
107241  { 146, 1 },
107242  { 147, 3 },
107243  { 149, 0 },
107244  { 149, 1 },
107245  { 149, 2 },
107246  { 148, 0 },
107247  { 148, 1 },
107248  { 148, 1 },
107249  { 148, 1 },
107250  { 147, 2 },
107251  { 147, 2 },
107252  { 147, 2 },
107253  { 151, 1 },
107254  { 151, 0 },
107255  { 147, 2 },
107256  { 147, 3 },
107257  { 147, 5 },
107258  { 147, 2 },
107259  { 152, 6 },
107260  { 154, 1 },
107261  { 156, 0 },
107262  { 156, 3 },
107263  { 155, 1 },
107264  { 155, 0 },
107265  { 153, 4 },
107266  { 153, 2 },
107267  { 158, 3 },
107268  { 158, 1 },
107269  { 161, 3 },
107270  { 162, 1 },
107271  { 165, 1 },
107272  { 165, 1 },
107273  { 166, 1 },
107274  { 150, 1 },
107275  { 150, 1 },
107276  { 150, 1 },
107277  { 163, 0 },
107278  { 163, 1 },
107279  { 167, 1 },
107280  { 167, 4 },
107281  { 167, 6 },
107282  { 168, 1 },
107283  { 168, 2 },
107284  { 169, 1 },
107285  { 169, 1 },
107286  { 164, 2 },
107287  { 164, 0 },
107288  { 172, 3 },
107289  { 172, 1 },
107290  { 173, 2 },
107291  { 173, 4 },
107292  { 173, 3 },
107293  { 173, 3 },
107294  { 173, 2 },
107295  { 173, 2 },
107296  { 173, 3 },
107297  { 173, 5 },
107298  { 173, 2 },
107299  { 173, 4 },
107300  { 173, 4 },
107301  { 173, 1 },
107302  { 173, 2 },
107303  { 178, 0 },
107304  { 178, 1 },
107305  { 180, 0 },
107306  { 180, 2 },
107307  { 182, 2 },
107308  { 182, 3 },
107309  { 182, 3 },
107310  { 182, 3 },
107311  { 183, 2 },
107312  { 183, 2 },
107313  { 183, 1 },
107314  { 183, 1 },
107315  { 183, 2 },
107316  { 181, 3 },
107317  { 181, 2 },
107318  { 184, 0 },
107319  { 184, 2 },
107320  { 184, 2 },
107321  { 159, 0 },
107322  { 159, 2 },
107323  { 185, 3 },
107324  { 185, 2 },
107325  { 185, 1 },
107326  { 186, 2 },
107327  { 186, 7 },
107328  { 186, 5 },
107329  { 186, 5 },
107330  { 186, 10 },
107331  { 188, 0 },
107332  { 188, 1 },
107333  { 176, 0 },
107334  { 176, 3 },
107335  { 189, 0 },
107336  { 189, 2 },
107337  { 190, 1 },
107338  { 190, 1 },
107339  { 190, 1 },
107340  { 147, 4 },
107341  { 192, 2 },
107342  { 192, 0 },
107343  { 147, 8 },
107344  { 147, 4 },
107345  { 147, 1 },
107346  { 160, 1 },
107347  { 160, 3 },
107348  { 195, 1 },
107349  { 195, 2 },
107350  { 195, 1 },
107351  { 194, 9 },
107352  { 196, 1 },
107353  { 196, 1 },
107354  { 196, 0 },
107355  { 204, 2 },
107356  { 204, 0 },
107357  { 197, 3 },
107358  { 197, 2 },
107359  { 197, 4 },
107360  { 205, 2 },
107361  { 205, 1 },
107362  { 205, 0 },
107363  { 198, 0 },
107364  { 198, 2 },
107365  { 207, 2 },
107366  { 207, 0 },
107367  { 206, 7 },
107368  { 206, 7 },
107369  { 206, 7 },
107370  { 157, 0 },
107371  { 157, 2 },
107372  { 193, 2 },
107373  { 208, 1 },
107374  { 208, 2 },
107375  { 208, 3 },
107376  { 208, 4 },
107377  { 210, 2 },
107378  { 210, 0 },
107379  { 209, 0 },
107380  { 209, 3 },
107381  { 209, 2 },
107382  { 211, 4 },
107383  { 211, 0 },
107384  { 202, 0 },
107385  { 202, 3 },
107386  { 214, 4 },
107387  { 214, 2 },
107388  { 215, 1 },
107389  { 177, 1 },
107390  { 177, 1 },
107391  { 177, 0 },
107392  { 200, 0 },
107393  { 200, 3 },
107394  { 201, 0 },
107395  { 201, 2 },
107396  { 203, 0 },
107397  { 203, 2 },
107398  { 203, 4 },
107399  { 203, 4 },
107400  { 147, 5 },
107401  { 199, 0 },
107402  { 199, 2 },
107403  { 147, 7 },
107404  { 217, 5 },
107405  { 217, 3 },
107406  { 147, 8 },
107407  { 147, 5 },
107408  { 147, 6 },
107409  { 218, 2 },
107410  { 218, 1 },
107411  { 220, 3 },
107412  { 220, 1 },
107413  { 219, 0 },
107414  { 219, 3 },
107415  { 213, 3 },
107416  { 213, 1 },
107417  { 175, 1 },
107418  { 175, 3 },
107419  { 174, 1 },
107420  { 175, 1 },
107421  { 175, 1 },
107422  { 175, 3 },
107423  { 175, 5 },
107424  { 174, 1 },
107425  { 174, 1 },
107426  { 175, 1 },
107427  { 175, 1 },
107428  { 175, 3 },
107429  { 175, 6 },
107430  { 175, 5 },
107431  { 175, 4 },
107432  { 174, 1 },
107433  { 175, 3 },
107434  { 175, 3 },
107435  { 175, 3 },
107436  { 175, 3 },
107437  { 175, 3 },
107438  { 175, 3 },
107439  { 175, 3 },
107440  { 175, 3 },
107441  { 222, 1 },
107442  { 222, 2 },
107443  { 222, 1 },
107444  { 222, 2 },
107445  { 175, 3 },
107446  { 175, 5 },
107447  { 175, 2 },
107448  { 175, 3 },
107449  { 175, 3 },
107450  { 175, 4 },
107451  { 175, 2 },
107452  { 175, 2 },
107453  { 175, 2 },
107454  { 175, 2 },
107455  { 223, 1 },
107456  { 223, 2 },
107457  { 175, 5 },
107458  { 224, 1 },
107459  { 224, 2 },
107460  { 175, 5 },
107461  { 175, 3 },
107462  { 175, 5 },
107463  { 175, 4 },
107464  { 175, 4 },
107465  { 175, 5 },
107466  { 226, 5 },
107467  { 226, 4 },
107468  { 227, 2 },
107469  { 227, 0 },
107470  { 225, 1 },
107471  { 225, 0 },
107472  { 221, 1 },
107473  { 221, 0 },
107474  { 216, 3 },
107475  { 216, 1 },
107476  { 147, 11 },
107477  { 228, 1 },
107478  { 228, 0 },
107479  { 179, 0 },
107480  { 179, 3 },
107481  { 187, 5 },
107482  { 187, 3 },
107483  { 229, 0 },
107484  { 229, 2 },
107485  { 147, 4 },
107486  { 147, 1 },
107487  { 147, 2 },
107488  { 147, 3 },
107489  { 147, 5 },
107490  { 147, 6 },
107491  { 147, 5 },
107492  { 147, 6 },
107493  { 230, 1 },
107494  { 230, 1 },
107495  { 230, 1 },
107496  { 230, 1 },
107497  { 230, 1 },
107498  { 170, 2 },
107499  { 171, 2 },
107500  { 232, 1 },
107501  { 231, 1 },
107502  { 231, 0 },
107503  { 147, 5 },
107504  { 233, 11 },
107505  { 235, 1 },
107506  { 235, 1 },
107507  { 235, 2 },
107508  { 235, 0 },
107509  { 236, 1 },
107510  { 236, 1 },
107511  { 236, 3 },
107512  { 237, 0 },
107513  { 237, 3 },
107514  { 238, 0 },
107515  { 238, 2 },
107516  { 234, 3 },
107517  { 234, 2 },
107518  { 240, 1 },
107519  { 240, 3 },
107520  { 241, 0 },
107521  { 241, 3 },
107522  { 241, 2 },
107523  { 239, 7 },
107524  { 239, 8 },
107525  { 239, 5 },
107526  { 239, 5 },
107527  { 239, 1 },
107528  { 175, 4 },
107529  { 175, 6 },
107530  { 191, 1 },
107531  { 191, 1 },
107532  { 191, 1 },
107533  { 147, 4 },
107534  { 147, 6 },
107535  { 147, 3 },
107536  { 243, 0 },
107537  { 243, 2 },
107538  { 242, 1 },
107539  { 242, 0 },
107540  { 147, 1 },
107541  { 147, 3 },
107542  { 147, 1 },
107543  { 147, 3 },
107544  { 147, 6 },
107545  { 147, 6 },
107546  { 244, 1 },
107547  { 245, 0 },
107548  { 245, 1 },
107549  { 147, 1 },
107550  { 147, 4 },
107551  { 246, 7 },
107552  { 247, 1 },
107553  { 247, 3 },
107554  { 248, 0 },
107555  { 248, 2 },
107556  { 249, 1 },
107557  { 249, 3 },
107558  { 250, 1 },
107559  { 251, 0 },
107560  { 251, 4 },
107561  { 251, 2 },
107562};
107563
107564static void yy_accept(yyParser*);  /* Forward Declaration */
107565
107566/*
107567** Perform a reduce action and the shift that must immediately
107568** follow the reduce.
107569*/
107570static void yy_reduce(
107571  yyParser *yypParser,         /* The parser */
107572  int yyruleno                 /* Number of the rule by which to reduce */
107573){
107574  int yygoto;                     /* The next state */
107575  int yyact;                      /* The next action */
107576  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
107577  yyStackEntry *yymsp;            /* The top of the parser's stack */
107578  int yysize;                     /* Amount to pop the stack */
107579  sqlite3ParserARG_FETCH;
107580  yymsp = &yypParser->yystack[yypParser->yyidx];
107581#ifndef NDEBUG
107582  if( yyTraceFILE && yyruleno>=0
107583        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
107584    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
107585      yyRuleName[yyruleno]);
107586  }
107587#endif /* NDEBUG */
107588
107589  /* Silence complaints from purify about yygotominor being uninitialized
107590  ** in some cases when it is copied into the stack after the following
107591  ** switch.  yygotominor is uninitialized when a rule reduces that does
107592  ** not set the value of its left-hand side nonterminal.  Leaving the
107593  ** value of the nonterminal uninitialized is utterly harmless as long
107594  ** as the value is never used.  So really the only thing this code
107595  ** accomplishes is to quieten purify.
107596  **
107597  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
107598  ** without this code, their parser segfaults.  I'm not sure what there
107599  ** parser is doing to make this happen.  This is the second bug report
107600  ** from wireshark this week.  Clearly they are stressing Lemon in ways
107601  ** that it has not been previously stressed...  (SQLite ticket #2172)
107602  */
107603  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
107604  yygotominor = yyzerominor;
107605
107606
107607  switch( yyruleno ){
107608  /* Beginning here are the reduction cases.  A typical example
107609  ** follows:
107610  **   case 0:
107611  **  #line <lineno> <grammarfile>
107612  **     { ... }           // User supplied code
107613  **  #line <lineno> <thisfile>
107614  **     break;
107615  */
107616      case 5: /* explain ::= */
107617{ sqlite3BeginParse(pParse, 0); }
107618        break;
107619      case 6: /* explain ::= EXPLAIN */
107620{ sqlite3BeginParse(pParse, 1); }
107621        break;
107622      case 7: /* explain ::= EXPLAIN QUERY PLAN */
107623{ sqlite3BeginParse(pParse, 2); }
107624        break;
107625      case 8: /* cmdx ::= cmd */
107626{ sqlite3FinishCoding(pParse); }
107627        break;
107628      case 9: /* cmd ::= BEGIN transtype trans_opt */
107629{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
107630        break;
107631      case 13: /* transtype ::= */
107632{yygotominor.yy4 = TK_DEFERRED;}
107633        break;
107634      case 14: /* transtype ::= DEFERRED */
107635      case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
107636      case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
107637      case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
107638      case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
107639{yygotominor.yy4 = yymsp[0].major;}
107640        break;
107641      case 17: /* cmd ::= COMMIT trans_opt */
107642      case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
107643{sqlite3CommitTransaction(pParse);}
107644        break;
107645      case 19: /* cmd ::= ROLLBACK trans_opt */
107646{sqlite3RollbackTransaction(pParse);}
107647        break;
107648      case 22: /* cmd ::= SAVEPOINT nm */
107649{
107650  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
107651}
107652        break;
107653      case 23: /* cmd ::= RELEASE savepoint_opt nm */
107654{
107655  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
107656}
107657        break;
107658      case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
107659{
107660  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
107661}
107662        break;
107663      case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
107664{
107665   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
107666}
107667        break;
107668      case 27: /* createkw ::= CREATE */
107669{
107670  pParse->db->lookaside.bEnabled = 0;
107671  yygotominor.yy0 = yymsp[0].minor.yy0;
107672}
107673        break;
107674      case 28: /* ifnotexists ::= */
107675      case 31: /* temp ::= */ yytestcase(yyruleno==31);
107676      case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
107677      case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
107678      case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
107679      case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
107680      case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
107681      case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
107682      case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
107683      case 121: /* distinct ::= */ yytestcase(yyruleno==121);
107684      case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
107685      case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
107686{yygotominor.yy4 = 0;}
107687        break;
107688      case 29: /* ifnotexists ::= IF NOT EXISTS */
107689      case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
107690      case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
107691      case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
107692      case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
107693      case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
107694      case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
107695      case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
107696{yygotominor.yy4 = 1;}
107697        break;
107698      case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
107699{
107700  sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
107701}
107702        break;
107703      case 33: /* create_table_args ::= AS select */
107704{
107705  sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387);
107706  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
107707}
107708        break;
107709      case 36: /* column ::= columnid type carglist */
107710{
107711  yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
107712  yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
107713}
107714        break;
107715      case 37: /* columnid ::= nm */
107716{
107717  sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
107718  yygotominor.yy0 = yymsp[0].minor.yy0;
107719}
107720        break;
107721      case 38: /* id ::= ID */
107722      case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
107723      case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
107724      case 41: /* nm ::= id */ yytestcase(yyruleno==41);
107725      case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
107726      case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
107727      case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
107728      case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
107729      case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
107730      case 128: /* as ::= ids */ yytestcase(yyruleno==128);
107731      case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
107732      case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
107733      case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
107734      case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
107735      case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
107736      case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
107737      case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
107738      case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
107739      case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
107740      case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
107741      case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
107742      case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
107743{yygotominor.yy0 = yymsp[0].minor.yy0;}
107744        break;
107745      case 45: /* type ::= typetoken */
107746{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
107747        break;
107748      case 47: /* typetoken ::= typename LP signed RP */
107749{
107750  yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
107751  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
107752}
107753        break;
107754      case 48: /* typetoken ::= typename LP signed COMMA signed RP */
107755{
107756  yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
107757  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
107758}
107759        break;
107760      case 50: /* typename ::= typename ids */
107761{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);}
107762        break;
107763      case 57: /* ccons ::= DEFAULT term */
107764      case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
107765{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
107766        break;
107767      case 58: /* ccons ::= DEFAULT LP expr RP */
107768{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
107769        break;
107770      case 60: /* ccons ::= DEFAULT MINUS term */
107771{
107772  ExprSpan v;
107773  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
107774  v.zStart = yymsp[-1].minor.yy0.z;
107775  v.zEnd = yymsp[0].minor.yy118.zEnd;
107776  sqlite3AddDefaultValue(pParse,&v);
107777}
107778        break;
107779      case 61: /* ccons ::= DEFAULT id */
107780{
107781  ExprSpan v;
107782  spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
107783  sqlite3AddDefaultValue(pParse,&v);
107784}
107785        break;
107786      case 63: /* ccons ::= NOT NULL onconf */
107787{sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
107788        break;
107789      case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
107790{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
107791        break;
107792      case 65: /* ccons ::= UNIQUE onconf */
107793{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
107794        break;
107795      case 66: /* ccons ::= CHECK LP expr RP */
107796{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
107797        break;
107798      case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
107799{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
107800        break;
107801      case 68: /* ccons ::= defer_subclause */
107802{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
107803        break;
107804      case 69: /* ccons ::= COLLATE ids */
107805{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
107806        break;
107807      case 72: /* refargs ::= */
107808{ yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
107809        break;
107810      case 73: /* refargs ::= refargs refarg */
107811{ yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; }
107812        break;
107813      case 74: /* refarg ::= MATCH nm */
107814      case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
107815{ yygotominor.yy215.value = 0;     yygotominor.yy215.mask = 0x000000; }
107816        break;
107817      case 76: /* refarg ::= ON DELETE refact */
107818{ yygotominor.yy215.value = yymsp[0].minor.yy4;     yygotominor.yy215.mask = 0x0000ff; }
107819        break;
107820      case 77: /* refarg ::= ON UPDATE refact */
107821{ yygotominor.yy215.value = yymsp[0].minor.yy4<<8;  yygotominor.yy215.mask = 0x00ff00; }
107822        break;
107823      case 78: /* refact ::= SET NULL */
107824{ yygotominor.yy4 = OE_SetNull;  /* EV: R-33326-45252 */}
107825        break;
107826      case 79: /* refact ::= SET DEFAULT */
107827{ yygotominor.yy4 = OE_SetDflt;  /* EV: R-33326-45252 */}
107828        break;
107829      case 80: /* refact ::= CASCADE */
107830{ yygotominor.yy4 = OE_Cascade;  /* EV: R-33326-45252 */}
107831        break;
107832      case 81: /* refact ::= RESTRICT */
107833{ yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
107834        break;
107835      case 82: /* refact ::= NO ACTION */
107836{ yygotominor.yy4 = OE_None;     /* EV: R-33326-45252 */}
107837        break;
107838      case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
107839      case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
107840      case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
107841      case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
107842{yygotominor.yy4 = yymsp[0].minor.yy4;}
107843        break;
107844      case 88: /* conslist_opt ::= */
107845{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
107846        break;
107847      case 89: /* conslist_opt ::= COMMA conslist */
107848{yygotominor.yy0 = yymsp[-1].minor.yy0;}
107849        break;
107850      case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
107851{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
107852        break;
107853      case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
107854{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);}
107855        break;
107856      case 96: /* tcons ::= CHECK LP expr RP onconf */
107857{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
107858        break;
107859      case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
107860{
107861    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
107862    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
107863}
107864        break;
107865      case 100: /* onconf ::= */
107866{yygotominor.yy4 = OE_Default;}
107867        break;
107868      case 102: /* orconf ::= */
107869{yygotominor.yy210 = OE_Default;}
107870        break;
107871      case 103: /* orconf ::= OR resolvetype */
107872{yygotominor.yy210 = (u8)yymsp[0].minor.yy4;}
107873        break;
107874      case 105: /* resolvetype ::= IGNORE */
107875{yygotominor.yy4 = OE_Ignore;}
107876        break;
107877      case 106: /* resolvetype ::= REPLACE */
107878{yygotominor.yy4 = OE_Replace;}
107879        break;
107880      case 107: /* cmd ::= DROP TABLE ifexists fullname */
107881{
107882  sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
107883}
107884        break;
107885      case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
107886{
107887  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);
107888}
107889        break;
107890      case 111: /* cmd ::= DROP VIEW ifexists fullname */
107891{
107892  sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
107893}
107894        break;
107895      case 112: /* cmd ::= select */
107896{
107897  SelectDest dest = {SRT_Output, 0, 0, 0, 0};
107898  sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
107899  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
107900}
107901        break;
107902      case 113: /* select ::= oneselect */
107903{yygotominor.yy387 = yymsp[0].minor.yy387;}
107904        break;
107905      case 114: /* select ::= select multiselect_op oneselect */
107906{
107907  if( yymsp[0].minor.yy387 ){
107908    yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4;
107909    yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387;
107910  }else{
107911    sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387);
107912  }
107913  yygotominor.yy387 = yymsp[0].minor.yy387;
107914}
107915        break;
107916      case 116: /* multiselect_op ::= UNION ALL */
107917{yygotominor.yy4 = TK_ALL;}
107918        break;
107919      case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
107920{
107921  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);
107922}
107923        break;
107924      case 122: /* sclp ::= selcollist COMMA */
107925      case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
107926{yygotominor.yy322 = yymsp[-1].minor.yy322;}
107927        break;
107928      case 123: /* sclp ::= */
107929      case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
107930      case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159);
107931      case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
107932      case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
107933{yygotominor.yy322 = 0;}
107934        break;
107935      case 124: /* selcollist ::= sclp expr as */
107936{
107937   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr);
107938   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1);
107939   sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
107940}
107941        break;
107942      case 125: /* selcollist ::= sclp STAR */
107943{
107944  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
107945  yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
107946}
107947        break;
107948      case 126: /* selcollist ::= sclp nm DOT STAR */
107949{
107950  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
107951  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
107952  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
107953  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
107954}
107955        break;
107956      case 129: /* as ::= */
107957{yygotominor.yy0.n = 0;}
107958        break;
107959      case 130: /* from ::= */
107960{yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));}
107961        break;
107962      case 131: /* from ::= FROM seltablist */
107963{
107964  yygotominor.yy259 = yymsp[0].minor.yy259;
107965  sqlite3SrcListShiftJoinType(yygotominor.yy259);
107966}
107967        break;
107968      case 132: /* stl_prefix ::= seltablist joinop */
107969{
107970   yygotominor.yy259 = yymsp[-1].minor.yy259;
107971   if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4;
107972}
107973        break;
107974      case 133: /* stl_prefix ::= */
107975{yygotominor.yy259 = 0;}
107976        break;
107977      case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
107978{
107979  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);
107980  sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
107981}
107982        break;
107983      case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
107984{
107985    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);
107986  }
107987        break;
107988      case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
107989{
107990    if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){
107991      yygotominor.yy259 = yymsp[-4].minor.yy259;
107992    }else{
107993      Select *pSubquery;
107994      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
107995      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0);
107996      yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
107997    }
107998  }
107999        break;
108000      case 137: /* dbnm ::= */
108001      case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
108002{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
108003        break;
108004      case 139: /* fullname ::= nm dbnm */
108005{yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
108006        break;
108007      case 140: /* joinop ::= COMMA|JOIN */
108008{ yygotominor.yy4 = JT_INNER; }
108009        break;
108010      case 141: /* joinop ::= JOIN_KW JOIN */
108011{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
108012        break;
108013      case 142: /* joinop ::= JOIN_KW nm JOIN */
108014{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
108015        break;
108016      case 143: /* joinop ::= JOIN_KW nm nm JOIN */
108017{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
108018        break;
108019      case 144: /* on_opt ::= ON expr */
108020      case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155);
108021      case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162);
108022      case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169);
108023      case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
108024      case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
108025{yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
108026        break;
108027      case 145: /* on_opt ::= */
108028      case 161: /* having_opt ::= */ yytestcase(yyruleno==161);
108029      case 168: /* where_opt ::= */ yytestcase(yyruleno==168);
108030      case 236: /* case_else ::= */ yytestcase(yyruleno==236);
108031      case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
108032{yygotominor.yy314 = 0;}
108033        break;
108034      case 148: /* indexed_opt ::= NOT INDEXED */
108035{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
108036        break;
108037      case 149: /* using_opt ::= USING LP inscollist RP */
108038      case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181);
108039{yygotominor.yy384 = yymsp[-1].minor.yy384;}
108040        break;
108041      case 150: /* using_opt ::= */
108042      case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180);
108043{yygotominor.yy384 = 0;}
108044        break;
108045      case 152: /* orderby_opt ::= ORDER BY sortlist */
108046      case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160);
108047      case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
108048{yygotominor.yy322 = yymsp[0].minor.yy322;}
108049        break;
108050      case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */
108051{
108052  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
108053  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
108054}
108055        break;
108056      case 154: /* sortlist ::= sortitem sortorder */
108057{
108058  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314);
108059  if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4;
108060}
108061        break;
108062      case 156: /* sortorder ::= ASC */
108063      case 158: /* sortorder ::= */ yytestcase(yyruleno==158);
108064{yygotominor.yy4 = SQLITE_SO_ASC;}
108065        break;
108066      case 157: /* sortorder ::= DESC */
108067{yygotominor.yy4 = SQLITE_SO_DESC;}
108068        break;
108069      case 163: /* limit_opt ::= */
108070{yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
108071        break;
108072      case 164: /* limit_opt ::= LIMIT expr */
108073{yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;}
108074        break;
108075      case 165: /* limit_opt ::= LIMIT expr OFFSET expr */
108076{yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;}
108077        break;
108078      case 166: /* limit_opt ::= LIMIT expr COMMA expr */
108079{yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;}
108080        break;
108081      case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
108082{
108083  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
108084  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
108085}
108086        break;
108087      case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
108088{
108089  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
108090  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list");
108091  sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210);
108092}
108093        break;
108094      case 171: /* setlist ::= setlist COMMA nm EQ expr */
108095{
108096  yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr);
108097  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
108098}
108099        break;
108100      case 172: /* setlist ::= nm EQ expr */
108101{
108102  yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr);
108103  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
108104}
108105        break;
108106      case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
108107{sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);}
108108        break;
108109      case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
108110{sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);}
108111        break;
108112      case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
108113{sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);}
108114        break;
108115      case 176: /* insert_cmd ::= INSERT orconf */
108116{yygotominor.yy210 = yymsp[0].minor.yy210;}
108117        break;
108118      case 177: /* insert_cmd ::= REPLACE */
108119{yygotominor.yy210 = OE_Replace;}
108120        break;
108121      case 178: /* itemlist ::= itemlist COMMA expr */
108122      case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
108123{yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);}
108124        break;
108125      case 179: /* itemlist ::= expr */
108126      case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
108127{yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);}
108128        break;
108129      case 182: /* inscollist ::= inscollist COMMA nm */
108130{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
108131        break;
108132      case 183: /* inscollist ::= nm */
108133{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
108134        break;
108135      case 184: /* expr ::= term */
108136{yygotominor.yy118 = yymsp[0].minor.yy118;}
108137        break;
108138      case 185: /* expr ::= LP expr RP */
108139{yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
108140        break;
108141      case 186: /* term ::= NULL */
108142      case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
108143      case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
108144{spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
108145        break;
108146      case 187: /* expr ::= id */
108147      case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
108148{spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
108149        break;
108150      case 189: /* expr ::= nm DOT nm */
108151{
108152  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
108153  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
108154  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
108155  spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
108156}
108157        break;
108158      case 190: /* expr ::= nm DOT nm DOT nm */
108159{
108160  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
108161  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
108162  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
108163  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
108164  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
108165  spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
108166}
108167        break;
108168      case 193: /* expr ::= REGISTER */
108169{
108170  /* When doing a nested parse, one can include terms in an expression
108171  ** that look like this:   #1 #2 ...  These terms refer to registers
108172  ** in the virtual machine.  #N is the N-th register. */
108173  if( pParse->nested==0 ){
108174    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
108175    yygotominor.yy118.pExpr = 0;
108176  }else{
108177    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
108178    if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable);
108179  }
108180  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
108181}
108182        break;
108183      case 194: /* expr ::= VARIABLE */
108184{
108185  spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
108186  sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
108187  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
108188}
108189        break;
108190      case 195: /* expr ::= expr COLLATE ids */
108191{
108192  yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0);
108193  yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
108194  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108195}
108196        break;
108197      case 196: /* expr ::= CAST LP expr AS typetoken RP */
108198{
108199  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0);
108200  spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
108201}
108202        break;
108203      case 197: /* expr ::= ID LP distinct exprlist RP */
108204{
108205  if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
108206    sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
108207  }
108208  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
108209  spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
108210  if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){
108211    yygotominor.yy118.pExpr->flags |= EP_Distinct;
108212  }
108213}
108214        break;
108215      case 198: /* expr ::= ID LP STAR RP */
108216{
108217  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
108218  spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
108219}
108220        break;
108221      case 199: /* term ::= CTIME_KW */
108222{
108223  /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
108224  ** treated as functions that return constants */
108225  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
108226  if( yygotominor.yy118.pExpr ){
108227    yygotominor.yy118.pExpr->op = TK_CONST_FUNC;
108228  }
108229  spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
108230}
108231        break;
108232      case 200: /* expr ::= expr AND expr */
108233      case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201);
108234      case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202);
108235      case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203);
108236      case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204);
108237      case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205);
108238      case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206);
108239      case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207);
108240{spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);}
108241        break;
108242      case 208: /* likeop ::= LIKE_KW */
108243      case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210);
108244{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;}
108245        break;
108246      case 209: /* likeop ::= NOT LIKE_KW */
108247      case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211);
108248{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;}
108249        break;
108250      case 212: /* expr ::= expr likeop expr */
108251{
108252  ExprList *pList;
108253  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
108254  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
108255  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator);
108256  if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108257  yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
108258  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
108259  if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
108260}
108261        break;
108262      case 213: /* expr ::= expr likeop expr ESCAPE expr */
108263{
108264  ExprList *pList;
108265  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
108266  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
108267  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
108268  yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator);
108269  if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108270  yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
108271  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
108272  if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
108273}
108274        break;
108275      case 214: /* expr ::= expr ISNULL|NOTNULL */
108276{spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);}
108277        break;
108278      case 215: /* expr ::= expr NOT NULL */
108279{spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);}
108280        break;
108281      case 216: /* expr ::= expr IS expr */
108282{
108283  spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);
108284  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL);
108285}
108286        break;
108287      case 217: /* expr ::= expr IS NOT expr */
108288{
108289  spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118);
108290  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL);
108291}
108292        break;
108293      case 218: /* expr ::= NOT expr */
108294      case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
108295{spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
108296        break;
108297      case 220: /* expr ::= MINUS expr */
108298{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
108299        break;
108300      case 221: /* expr ::= PLUS expr */
108301{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
108302        break;
108303      case 224: /* expr ::= expr between_op expr AND expr */
108304{
108305  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
108306  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
108307  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0);
108308  if( yygotominor.yy118.pExpr ){
108309    yygotominor.yy118.pExpr->x.pList = pList;
108310  }else{
108311    sqlite3ExprListDelete(pParse->db, pList);
108312  }
108313  if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108314  yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
108315  yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
108316}
108317        break;
108318      case 227: /* expr ::= expr in_op LP exprlist RP */
108319{
108320    if( yymsp[-1].minor.yy322==0 ){
108321      /* Expressions of the form
108322      **
108323      **      expr1 IN ()
108324      **      expr1 NOT IN ()
108325      **
108326      ** simplify to constants 0 (false) and 1 (true), respectively,
108327      ** regardless of the value of expr1.
108328      */
108329      yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]);
108330      sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
108331    }else{
108332      yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
108333      if( yygotominor.yy118.pExpr ){
108334        yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
108335        sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
108336      }else{
108337        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
108338      }
108339      if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108340    }
108341    yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
108342    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108343  }
108344        break;
108345      case 228: /* expr ::= LP select RP */
108346{
108347    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
108348    if( yygotominor.yy118.pExpr ){
108349      yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
108350      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
108351      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
108352    }else{
108353      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
108354    }
108355    yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
108356    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108357  }
108358        break;
108359      case 229: /* expr ::= expr in_op LP select RP */
108360{
108361    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
108362    if( yygotominor.yy118.pExpr ){
108363      yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
108364      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
108365      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
108366    }else{
108367      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
108368    }
108369    if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108370    yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
108371    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108372  }
108373        break;
108374      case 230: /* expr ::= expr in_op nm dbnm */
108375{
108376    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
108377    yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0);
108378    if( yygotominor.yy118.pExpr ){
108379      yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
108380      ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
108381      sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
108382    }else{
108383      sqlite3SrcListDelete(pParse->db, pSrc);
108384    }
108385    if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
108386    yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
108387    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];
108388  }
108389        break;
108390      case 231: /* expr ::= EXISTS LP select RP */
108391{
108392    Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
108393    if( p ){
108394      p->x.pSelect = yymsp[-1].minor.yy387;
108395      ExprSetProperty(p, EP_xIsSelect);
108396      sqlite3ExprSetHeight(pParse, p);
108397    }else{
108398      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
108399    }
108400    yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
108401    yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108402  }
108403        break;
108404      case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
108405{
108406  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0);
108407  if( yygotominor.yy118.pExpr ){
108408    yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322;
108409    sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
108410  }else{
108411    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
108412  }
108413  yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
108414  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108415}
108416        break;
108417      case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
108418{
108419  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr);
108420  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
108421}
108422        break;
108423      case 234: /* case_exprlist ::= WHEN expr THEN expr */
108424{
108425  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
108426  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
108427}
108428        break;
108429      case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
108430{
108431  sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0,
108432                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4,
108433                      &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4);
108434}
108435        break;
108436      case 244: /* uniqueflag ::= UNIQUE */
108437      case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
108438{yygotominor.yy4 = OE_Abort;}
108439        break;
108440      case 245: /* uniqueflag ::= */
108441{yygotominor.yy4 = OE_None;}
108442        break;
108443      case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
108444{
108445  Expr *p = 0;
108446  if( yymsp[-1].minor.yy0.n>0 ){
108447    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
108448    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
108449  }
108450  yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p);
108451  sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1);
108452  sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
108453  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
108454}
108455        break;
108456      case 249: /* idxlist ::= nm collate sortorder */
108457{
108458  Expr *p = 0;
108459  if( yymsp[-1].minor.yy0.n>0 ){
108460    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
108461    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
108462  }
108463  yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p);
108464  sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
108465  sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
108466  if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
108467}
108468        break;
108469      case 250: /* collate ::= */
108470{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
108471        break;
108472      case 252: /* cmd ::= DROP INDEX ifexists fullname */
108473{sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
108474        break;
108475      case 253: /* cmd ::= VACUUM */
108476      case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
108477{sqlite3Vacuum(pParse);}
108478        break;
108479      case 255: /* cmd ::= PRAGMA nm dbnm */
108480{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
108481        break;
108482      case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
108483{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
108484        break;
108485      case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
108486{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
108487        break;
108488      case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
108489{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
108490        break;
108491      case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
108492{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
108493        break;
108494      case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
108495{
108496  Token all;
108497  all.z = yymsp[-3].minor.yy0.z;
108498  all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
108499  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
108500}
108501        break;
108502      case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
108503{
108504  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);
108505  yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
108506}
108507        break;
108508      case 272: /* trigger_time ::= BEFORE */
108509      case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
108510{ yygotominor.yy4 = TK_BEFORE; }
108511        break;
108512      case 273: /* trigger_time ::= AFTER */
108513{ yygotominor.yy4 = TK_AFTER;  }
108514        break;
108515      case 274: /* trigger_time ::= INSTEAD OF */
108516{ yygotominor.yy4 = TK_INSTEAD;}
108517        break;
108518      case 276: /* trigger_event ::= DELETE|INSERT */
108519      case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
108520{yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
108521        break;
108522      case 278: /* trigger_event ::= UPDATE OF inscollist */
108523{yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
108524        break;
108525      case 281: /* when_clause ::= */
108526      case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
108527{ yygotominor.yy314 = 0; }
108528        break;
108529      case 282: /* when_clause ::= WHEN expr */
108530      case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
108531{ yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
108532        break;
108533      case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
108534{
108535  assert( yymsp[-2].minor.yy203!=0 );
108536  yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
108537  yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
108538  yygotominor.yy203 = yymsp[-2].minor.yy203;
108539}
108540        break;
108541      case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
108542{
108543  assert( yymsp[-1].minor.yy203!=0 );
108544  yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
108545  yygotominor.yy203 = yymsp[-1].minor.yy203;
108546}
108547        break;
108548      case 286: /* trnm ::= nm DOT nm */
108549{
108550  yygotominor.yy0 = yymsp[0].minor.yy0;
108551  sqlite3ErrorMsg(pParse,
108552        "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
108553        "statements within triggers");
108554}
108555        break;
108556      case 288: /* tridxby ::= INDEXED BY nm */
108557{
108558  sqlite3ErrorMsg(pParse,
108559        "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
108560        "within triggers");
108561}
108562        break;
108563      case 289: /* tridxby ::= NOT INDEXED */
108564{
108565  sqlite3ErrorMsg(pParse,
108566        "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
108567        "within triggers");
108568}
108569        break;
108570      case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
108571{ yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); }
108572        break;
108573      case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
108574{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);}
108575        break;
108576      case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
108577{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);}
108578        break;
108579      case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
108580{yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
108581        break;
108582      case 294: /* trigger_cmd ::= select */
108583{yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
108584        break;
108585      case 295: /* expr ::= RAISE LP IGNORE RP */
108586{
108587  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
108588  if( yygotominor.yy118.pExpr ){
108589    yygotominor.yy118.pExpr->affinity = OE_Ignore;
108590  }
108591  yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
108592  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108593}
108594        break;
108595      case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
108596{
108597  yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
108598  if( yygotominor.yy118.pExpr ) {
108599    yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
108600  }
108601  yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
108602  yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108603}
108604        break;
108605      case 297: /* raisetype ::= ROLLBACK */
108606{yygotominor.yy4 = OE_Rollback;}
108607        break;
108608      case 299: /* raisetype ::= FAIL */
108609{yygotominor.yy4 = OE_Fail;}
108610        break;
108611      case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
108612{
108613  sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
108614}
108615        break;
108616      case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
108617{
108618  sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314);
108619}
108620        break;
108621      case 302: /* cmd ::= DETACH database_kw_opt expr */
108622{
108623  sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
108624}
108625        break;
108626      case 307: /* cmd ::= REINDEX */
108627{sqlite3Reindex(pParse, 0, 0);}
108628        break;
108629      case 308: /* cmd ::= REINDEX nm dbnm */
108630{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
108631        break;
108632      case 309: /* cmd ::= ANALYZE */
108633{sqlite3Analyze(pParse, 0, 0);}
108634        break;
108635      case 310: /* cmd ::= ANALYZE nm dbnm */
108636{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
108637        break;
108638      case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
108639{
108640  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
108641}
108642        break;
108643      case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
108644{
108645  sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
108646}
108647        break;
108648      case 313: /* add_column_fullname ::= fullname */
108649{
108650  pParse->db->lookaside.bEnabled = 0;
108651  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
108652}
108653        break;
108654      case 316: /* cmd ::= create_vtab */
108655{sqlite3VtabFinishParse(pParse,0);}
108656        break;
108657      case 317: /* cmd ::= create_vtab LP vtabarglist RP */
108658{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
108659        break;
108660      case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
108661{
108662    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
108663}
108664        break;
108665      case 321: /* vtabarg ::= */
108666{sqlite3VtabArgInit(pParse);}
108667        break;
108668      case 323: /* vtabargtoken ::= ANY */
108669      case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
108670      case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
108671{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
108672        break;
108673      default:
108674      /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
108675      /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
108676      /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
108677      /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
108678      /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
108679      /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
108680      /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
108681      /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
108682      /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
108683      /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
108684      /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
108685      /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
108686      /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
108687      /* (44) type ::= */ yytestcase(yyruleno==44);
108688      /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
108689      /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
108690      /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
108691      /* (54) carglist ::= */ yytestcase(yyruleno==54);
108692      /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
108693      /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
108694      /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
108695      /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
108696      /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
108697      /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
108698      /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
108699      /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
108700      /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
108701      /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
108702      /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
108703      /* (287) tridxby ::= */ yytestcase(yyruleno==287);
108704      /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
108705      /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
108706      /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
108707      /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
108708      /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
108709      /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
108710      /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
108711      /* (326) anylist ::= */ yytestcase(yyruleno==326);
108712      /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
108713      /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
108714        break;
108715  };
108716  yygoto = yyRuleInfo[yyruleno].lhs;
108717  yysize = yyRuleInfo[yyruleno].nrhs;
108718  yypParser->yyidx -= yysize;
108719  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
108720  if( yyact < YYNSTATE ){
108721#ifdef NDEBUG
108722    /* If we are not debugging and the reduce action popped at least
108723    ** one element off the stack, then we can push the new element back
108724    ** onto the stack here, and skip the stack overflow test in yy_shift().
108725    ** That gives a significant speed improvement. */
108726    if( yysize ){
108727      yypParser->yyidx++;
108728      yymsp -= yysize-1;
108729      yymsp->stateno = (YYACTIONTYPE)yyact;
108730      yymsp->major = (YYCODETYPE)yygoto;
108731      yymsp->minor = yygotominor;
108732    }else
108733#endif
108734    {
108735      yy_shift(yypParser,yyact,yygoto,&yygotominor);
108736    }
108737  }else{
108738    assert( yyact == YYNSTATE + YYNRULE + 1 );
108739    yy_accept(yypParser);
108740  }
108741}
108742
108743/*
108744** The following code executes when the parse fails
108745*/
108746#ifndef YYNOERRORRECOVERY
108747static void yy_parse_failed(
108748  yyParser *yypParser           /* The parser */
108749){
108750  sqlite3ParserARG_FETCH;
108751#ifndef NDEBUG
108752  if( yyTraceFILE ){
108753    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
108754  }
108755#endif
108756  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
108757  /* Here code is inserted which will be executed whenever the
108758  ** parser fails */
108759  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108760}
108761#endif /* YYNOERRORRECOVERY */
108762
108763/*
108764** The following code executes when a syntax error first occurs.
108765*/
108766static void yy_syntax_error(
108767  yyParser *yypParser,           /* The parser */
108768  int yymajor,                   /* The major type of the error token */
108769  YYMINORTYPE yyminor            /* The minor type of the error token */
108770){
108771  sqlite3ParserARG_FETCH;
108772#define TOKEN (yyminor.yy0)
108773
108774  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
108775  assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
108776  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
108777  pParse->parseError = 1;
108778  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108779}
108780
108781/*
108782** The following is executed when the parser accepts
108783*/
108784static void yy_accept(
108785  yyParser *yypParser           /* The parser */
108786){
108787  sqlite3ParserARG_FETCH;
108788#ifndef NDEBUG
108789  if( yyTraceFILE ){
108790    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
108791  }
108792#endif
108793  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
108794  /* Here code is inserted which will be executed whenever the
108795  ** parser accepts */
108796  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108797}
108798
108799/* The main parser program.
108800** The first argument is a pointer to a structure obtained from
108801** "sqlite3ParserAlloc" which describes the current state of the parser.
108802** The second argument is the major token number.  The third is
108803** the minor token.  The fourth optional argument is whatever the
108804** user wants (and specified in the grammar) and is available for
108805** use by the action routines.
108806**
108807** Inputs:
108808** <ul>
108809** <li> A pointer to the parser (an opaque structure.)
108810** <li> The major token number.
108811** <li> The minor token number.
108812** <li> An option argument of a grammar-specified type.
108813** </ul>
108814**
108815** Outputs:
108816** None.
108817*/
108818SQLITE_PRIVATE void sqlite3Parser(
108819  void *yyp,                   /* The parser */
108820  int yymajor,                 /* The major token code number */
108821  sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
108822  sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
108823){
108824  YYMINORTYPE yyminorunion;
108825  int yyact;            /* The parser action. */
108826  int yyendofinput;     /* True if we are at the end of input */
108827#ifdef YYERRORSYMBOL
108828  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
108829#endif
108830  yyParser *yypParser;  /* The parser */
108831
108832  /* (re)initialize the parser, if necessary */
108833  yypParser = (yyParser*)yyp;
108834  if( yypParser->yyidx<0 ){
108835#if YYSTACKDEPTH<=0
108836    if( yypParser->yystksz <=0 ){
108837      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
108838      yyminorunion = yyzerominor;
108839      yyStackOverflow(yypParser, &yyminorunion);
108840      return;
108841    }
108842#endif
108843    yypParser->yyidx = 0;
108844    yypParser->yyerrcnt = -1;
108845    yypParser->yystack[0].stateno = 0;
108846    yypParser->yystack[0].major = 0;
108847  }
108848  yyminorunion.yy0 = yyminor;
108849  yyendofinput = (yymajor==0);
108850  sqlite3ParserARG_STORE;
108851
108852#ifndef NDEBUG
108853  if( yyTraceFILE ){
108854    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
108855  }
108856#endif
108857
108858  do{
108859    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
108860    if( yyact<YYNSTATE ){
108861      assert( !yyendofinput );  /* Impossible to shift the $ token */
108862      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
108863      yypParser->yyerrcnt--;
108864      yymajor = YYNOCODE;
108865    }else if( yyact < YYNSTATE + YYNRULE ){
108866      yy_reduce(yypParser,yyact-YYNSTATE);
108867    }else{
108868      assert( yyact == YY_ERROR_ACTION );
108869#ifdef YYERRORSYMBOL
108870      int yymx;
108871#endif
108872#ifndef NDEBUG
108873      if( yyTraceFILE ){
108874        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
108875      }
108876#endif
108877#ifdef YYERRORSYMBOL
108878      /* A syntax error has occurred.
108879      ** The response to an error depends upon whether or not the
108880      ** grammar defines an error token "ERROR".
108881      **
108882      ** This is what we do if the grammar does define ERROR:
108883      **
108884      **  * Call the %syntax_error function.
108885      **
108886      **  * Begin popping the stack until we enter a state where
108887      **    it is legal to shift the error symbol, then shift
108888      **    the error symbol.
108889      **
108890      **  * Set the error count to three.
108891      **
108892      **  * Begin accepting and shifting new tokens.  No new error
108893      **    processing will occur until three tokens have been
108894      **    shifted successfully.
108895      **
108896      */
108897      if( yypParser->yyerrcnt<0 ){
108898        yy_syntax_error(yypParser,yymajor,yyminorunion);
108899      }
108900      yymx = yypParser->yystack[yypParser->yyidx].major;
108901      if( yymx==YYERRORSYMBOL || yyerrorhit ){
108902#ifndef NDEBUG
108903        if( yyTraceFILE ){
108904          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
108905             yyTracePrompt,yyTokenName[yymajor]);
108906        }
108907#endif
108908        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
108909        yymajor = YYNOCODE;
108910      }else{
108911         while(
108912          yypParser->yyidx >= 0 &&
108913          yymx != YYERRORSYMBOL &&
108914          (yyact = yy_find_reduce_action(
108915                        yypParser->yystack[yypParser->yyidx].stateno,
108916                        YYERRORSYMBOL)) >= YYNSTATE
108917        ){
108918          yy_pop_parser_stack(yypParser);
108919        }
108920        if( yypParser->yyidx < 0 || yymajor==0 ){
108921          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108922          yy_parse_failed(yypParser);
108923          yymajor = YYNOCODE;
108924        }else if( yymx!=YYERRORSYMBOL ){
108925          YYMINORTYPE u2;
108926          u2.YYERRSYMDT = 0;
108927          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
108928        }
108929      }
108930      yypParser->yyerrcnt = 3;
108931      yyerrorhit = 1;
108932#elif defined(YYNOERRORRECOVERY)
108933      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
108934      ** do any kind of error recovery.  Instead, simply invoke the syntax
108935      ** error routine and continue going as if nothing had happened.
108936      **
108937      ** Applications can set this macro (for example inside %include) if
108938      ** they intend to abandon the parse upon the first syntax error seen.
108939      */
108940      yy_syntax_error(yypParser,yymajor,yyminorunion);
108941      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108942      yymajor = YYNOCODE;
108943
108944#else  /* YYERRORSYMBOL is not defined */
108945      /* This is what we do if the grammar does not define ERROR:
108946      **
108947      **  * Report an error message, and throw away the input token.
108948      **
108949      **  * If the input token is $, then fail the parse.
108950      **
108951      ** As before, subsequent error messages are suppressed until
108952      ** three input tokens have been successfully shifted.
108953      */
108954      if( yypParser->yyerrcnt<=0 ){
108955        yy_syntax_error(yypParser,yymajor,yyminorunion);
108956      }
108957      yypParser->yyerrcnt = 3;
108958      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108959      if( yyendofinput ){
108960        yy_parse_failed(yypParser);
108961      }
108962      yymajor = YYNOCODE;
108963#endif
108964    }
108965  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
108966  return;
108967}
108968
108969/************** End of parse.c ***********************************************/
108970/************** Begin file tokenize.c ****************************************/
108971/*
108972** 2001 September 15
108973**
108974** The author disclaims copyright to this source code.  In place of
108975** a legal notice, here is a blessing:
108976**
108977**    May you do good and not evil.
108978**    May you find forgiveness for yourself and forgive others.
108979**    May you share freely, never taking more than you give.
108980**
108981*************************************************************************
108982** An tokenizer for SQL
108983**
108984** This file contains C code that splits an SQL input string up into
108985** individual tokens and sends those tokens one-by-one over to the
108986** parser for analysis.
108987*/
108988/* #include <stdlib.h> */
108989
108990/*
108991** The charMap() macro maps alphabetic characters into their
108992** lower-case ASCII equivalent.  On ASCII machines, this is just
108993** an upper-to-lower case map.  On EBCDIC machines we also need
108994** to adjust the encoding.  Only alphabetic characters and underscores
108995** need to be translated.
108996*/
108997#ifdef SQLITE_ASCII
108998# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
108999#endif
109000#ifdef SQLITE_EBCDIC
109001# define charMap(X) ebcdicToAscii[(unsigned char)X]
109002const unsigned char ebcdicToAscii[] = {
109003/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
109004   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
109005   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
109006   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
109007   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
109008   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
109009   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
109010   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
109011   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
109012   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
109013   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
109014   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
109015   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
109016   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
109017   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
109018   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
109019   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
109020};
109021#endif
109022
109023/*
109024** The sqlite3KeywordCode function looks up an identifier to determine if
109025** it is a keyword.  If it is a keyword, the token code of that keyword is
109026** returned.  If the input is not a keyword, TK_ID is returned.
109027**
109028** The implementation of this routine was generated by a program,
109029** mkkeywordhash.h, located in the tool subdirectory of the distribution.
109030** The output of the mkkeywordhash.c program is written into a file
109031** named keywordhash.h and then included into this source file by
109032** the #include below.
109033*/
109034/************** Include keywordhash.h in the middle of tokenize.c ************/
109035/************** Begin file keywordhash.h *************************************/
109036/***** This file contains automatically generated code ******
109037**
109038** The code in this file has been automatically generated by
109039**
109040**   sqlite/tool/mkkeywordhash.c
109041**
109042** The code in this file implements a function that determines whether
109043** or not a given identifier is really an SQL keyword.  The same thing
109044** might be implemented more directly using a hand-written hash table.
109045** But by using this automatically generated code, the size of the code
109046** is substantially reduced.  This is important for embedded applications
109047** on platforms with limited memory.
109048*/
109049/* Hash score: 175 */
109050static int keywordCode(const char *z, int n){
109051  /* zText[] encodes 811 bytes of keywords in 541 bytes */
109052  /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
109053  /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
109054  /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
109055  /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
109056  /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
109057  /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
109058  /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
109059  /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
109060  /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
109061  /*   INITIALLY                                                          */
109062  static const char zText[540] = {
109063    'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
109064    'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
109065    'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
109066    'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
109067    'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
109068    'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
109069    'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
109070    'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
109071    'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
109072    'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
109073    'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
109074    'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
109075    'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
109076    'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
109077    'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
109078    'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
109079    'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
109080    'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
109081    'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
109082    'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
109083    'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
109084    'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
109085    'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
109086    'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
109087    'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
109088    'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
109089    'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
109090    'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
109091    'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
109092    'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
109093  };
109094  static const unsigned char aHash[127] = {
109095      72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
109096      42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
109097     118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
109098       0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
109099       0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
109100      92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
109101      96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
109102      39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
109103      58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
109104      29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
109105  };
109106  static const unsigned char aNext[121] = {
109107       0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
109108       0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
109109       0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
109110       0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
109111       0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
109112      62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
109113      61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
109114       0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
109115     103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
109116      35,  64,   0,   0,
109117  };
109118  static const unsigned char aLen[121] = {
109119       7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
109120       7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
109121      11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
109122       4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
109123       5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
109124       7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
109125       7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
109126       6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
109127       4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
109128       6,   4,   9,   3,
109129  };
109130  static const unsigned short int aOffset[121] = {
109131       0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
109132      36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
109133      86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
109134     159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
109135     203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
109136     248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
109137     326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
109138     387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
109139     462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
109140     521, 527, 531, 536,
109141  };
109142  static const unsigned char aCode[121] = {
109143    TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
109144    TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
109145    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
109146    TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
109147    TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
109148    TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
109149    TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,
109150    TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
109151    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,
109152    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,
109153    TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,
109154    TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,
109155    TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,
109156    TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,
109157    TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,
109158    TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,
109159    TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,
109160    TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,
109161    TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,
109162    TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,
109163    TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,
109164    TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,
109165    TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,
109166    TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,
109167    TK_ALL,
109168  };
109169  int h, i;
109170  if( n<2 ) return TK_ID;
109171  h = ((charMap(z[0])*4) ^
109172      (charMap(z[n-1])*3) ^
109173      n) % 127;
109174  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
109175    if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
109176      testcase( i==0 ); /* REINDEX */
109177      testcase( i==1 ); /* INDEXED */
109178      testcase( i==2 ); /* INDEX */
109179      testcase( i==3 ); /* DESC */
109180      testcase( i==4 ); /* ESCAPE */
109181      testcase( i==5 ); /* EACH */
109182      testcase( i==6 ); /* CHECK */
109183      testcase( i==7 ); /* KEY */
109184      testcase( i==8 ); /* BEFORE */
109185      testcase( i==9 ); /* FOREIGN */
109186      testcase( i==10 ); /* FOR */
109187      testcase( i==11 ); /* IGNORE */
109188      testcase( i==12 ); /* REGEXP */
109189      testcase( i==13 ); /* EXPLAIN */
109190      testcase( i==14 ); /* INSTEAD */
109191      testcase( i==15 ); /* ADD */
109192      testcase( i==16 ); /* DATABASE */
109193      testcase( i==17 ); /* AS */
109194      testcase( i==18 ); /* SELECT */
109195      testcase( i==19 ); /* TABLE */
109196      testcase( i==20 ); /* LEFT */
109197      testcase( i==21 ); /* THEN */
109198      testcase( i==22 ); /* END */
109199      testcase( i==23 ); /* DEFERRABLE */
109200      testcase( i==24 ); /* ELSE */
109201      testcase( i==25 ); /* EXCEPT */
109202      testcase( i==26 ); /* TRANSACTION */
109203      testcase( i==27 ); /* ACTION */
109204      testcase( i==28 ); /* ON */
109205      testcase( i==29 ); /* NATURAL */
109206      testcase( i==30 ); /* ALTER */
109207      testcase( i==31 ); /* RAISE */
109208      testcase( i==32 ); /* EXCLUSIVE */
109209      testcase( i==33 ); /* EXISTS */
109210      testcase( i==34 ); /* SAVEPOINT */
109211      testcase( i==35 ); /* INTERSECT */
109212      testcase( i==36 ); /* TRIGGER */
109213      testcase( i==37 ); /* REFERENCES */
109214      testcase( i==38 ); /* CONSTRAINT */
109215      testcase( i==39 ); /* INTO */
109216      testcase( i==40 ); /* OFFSET */
109217      testcase( i==41 ); /* OF */
109218      testcase( i==42 ); /* SET */
109219      testcase( i==43 ); /* TEMPORARY */
109220      testcase( i==44 ); /* TEMP */
109221      testcase( i==45 ); /* OR */
109222      testcase( i==46 ); /* UNIQUE */
109223      testcase( i==47 ); /* QUERY */
109224      testcase( i==48 ); /* ATTACH */
109225      testcase( i==49 ); /* HAVING */
109226      testcase( i==50 ); /* GROUP */
109227      testcase( i==51 ); /* UPDATE */
109228      testcase( i==52 ); /* BEGIN */
109229      testcase( i==53 ); /* INNER */
109230      testcase( i==54 ); /* RELEASE */
109231      testcase( i==55 ); /* BETWEEN */
109232      testcase( i==56 ); /* NOTNULL */
109233      testcase( i==57 ); /* NOT */
109234      testcase( i==58 ); /* NO */
109235      testcase( i==59 ); /* NULL */
109236      testcase( i==60 ); /* LIKE */
109237      testcase( i==61 ); /* CASCADE */
109238      testcase( i==62 ); /* ASC */
109239      testcase( i==63 ); /* DELETE */
109240      testcase( i==64 ); /* CASE */
109241      testcase( i==65 ); /* COLLATE */
109242      testcase( i==66 ); /* CREATE */
109243      testcase( i==67 ); /* CURRENT_DATE */
109244      testcase( i==68 ); /* DETACH */
109245      testcase( i==69 ); /* IMMEDIATE */
109246      testcase( i==70 ); /* JOIN */
109247      testcase( i==71 ); /* INSERT */
109248      testcase( i==72 ); /* MATCH */
109249      testcase( i==73 ); /* PLAN */
109250      testcase( i==74 ); /* ANALYZE */
109251      testcase( i==75 ); /* PRAGMA */
109252      testcase( i==76 ); /* ABORT */
109253      testcase( i==77 ); /* VALUES */
109254      testcase( i==78 ); /* VIRTUAL */
109255      testcase( i==79 ); /* LIMIT */
109256      testcase( i==80 ); /* WHEN */
109257      testcase( i==81 ); /* WHERE */
109258      testcase( i==82 ); /* RENAME */
109259      testcase( i==83 ); /* AFTER */
109260      testcase( i==84 ); /* REPLACE */
109261      testcase( i==85 ); /* AND */
109262      testcase( i==86 ); /* DEFAULT */
109263      testcase( i==87 ); /* AUTOINCREMENT */
109264      testcase( i==88 ); /* TO */
109265      testcase( i==89 ); /* IN */
109266      testcase( i==90 ); /* CAST */
109267      testcase( i==91 ); /* COLUMN */
109268      testcase( i==92 ); /* COMMIT */
109269      testcase( i==93 ); /* CONFLICT */
109270      testcase( i==94 ); /* CROSS */
109271      testcase( i==95 ); /* CURRENT_TIMESTAMP */
109272      testcase( i==96 ); /* CURRENT_TIME */
109273      testcase( i==97 ); /* PRIMARY */
109274      testcase( i==98 ); /* DEFERRED */
109275      testcase( i==99 ); /* DISTINCT */
109276      testcase( i==100 ); /* IS */
109277      testcase( i==101 ); /* DROP */
109278      testcase( i==102 ); /* FAIL */
109279      testcase( i==103 ); /* FROM */
109280      testcase( i==104 ); /* FULL */
109281      testcase( i==105 ); /* GLOB */
109282      testcase( i==106 ); /* BY */
109283      testcase( i==107 ); /* IF */
109284      testcase( i==108 ); /* ISNULL */
109285      testcase( i==109 ); /* ORDER */
109286      testcase( i==110 ); /* RESTRICT */
109287      testcase( i==111 ); /* OUTER */
109288      testcase( i==112 ); /* RIGHT */
109289      testcase( i==113 ); /* ROLLBACK */
109290      testcase( i==114 ); /* ROW */
109291      testcase( i==115 ); /* UNION */
109292      testcase( i==116 ); /* USING */
109293      testcase( i==117 ); /* VACUUM */
109294      testcase( i==118 ); /* VIEW */
109295      testcase( i==119 ); /* INITIALLY */
109296      testcase( i==120 ); /* ALL */
109297      return aCode[i];
109298    }
109299  }
109300  return TK_ID;
109301}
109302SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
109303  return keywordCode((char*)z, n);
109304}
109305#define SQLITE_N_KEYWORD 121
109306
109307/************** End of keywordhash.h *****************************************/
109308/************** Continuing where we left off in tokenize.c *******************/
109309
109310
109311/*
109312** If X is a character that can be used in an identifier then
109313** IdChar(X) will be true.  Otherwise it is false.
109314**
109315** For ASCII, any character with the high-order bit set is
109316** allowed in an identifier.  For 7-bit characters,
109317** sqlite3IsIdChar[X] must be 1.
109318**
109319** For EBCDIC, the rules are more complex but have the same
109320** end result.
109321**
109322** Ticket #1066.  the SQL standard does not allow '$' in the
109323** middle of identfiers.  But many SQL implementations do.
109324** SQLite will allow '$' in identifiers for compatibility.
109325** But the feature is undocumented.
109326*/
109327#ifdef SQLITE_ASCII
109328#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
109329#endif
109330#ifdef SQLITE_EBCDIC
109331SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
109332/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
109333    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
109334    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
109335    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
109336    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
109337    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
109338    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
109339    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
109340    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
109341    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
109342    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
109343    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
109344    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
109345};
109346#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
109347#endif
109348
109349
109350/*
109351** Return the length of the token that begins at z[0].
109352** Store the token type in *tokenType before returning.
109353*/
109354SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
109355  int i, c;
109356  switch( *z ){
109357    case ' ': case '\t': case '\n': case '\f': case '\r': {
109358      testcase( z[0]==' ' );
109359      testcase( z[0]=='\t' );
109360      testcase( z[0]=='\n' );
109361      testcase( z[0]=='\f' );
109362      testcase( z[0]=='\r' );
109363      for(i=1; sqlite3Isspace(z[i]); i++){}
109364      *tokenType = TK_SPACE;
109365      return i;
109366    }
109367    case '-': {
109368      if( z[1]=='-' ){
109369        /* IMP: R-15891-05542 -- syntax diagram for comments */
109370        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
109371        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
109372        return i;
109373      }
109374      *tokenType = TK_MINUS;
109375      return 1;
109376    }
109377    case '(': {
109378      *tokenType = TK_LP;
109379      return 1;
109380    }
109381    case ')': {
109382      *tokenType = TK_RP;
109383      return 1;
109384    }
109385    case ';': {
109386      *tokenType = TK_SEMI;
109387      return 1;
109388    }
109389    case '+': {
109390      *tokenType = TK_PLUS;
109391      return 1;
109392    }
109393    case '*': {
109394      *tokenType = TK_STAR;
109395      return 1;
109396    }
109397    case '/': {
109398      if( z[1]!='*' || z[2]==0 ){
109399        *tokenType = TK_SLASH;
109400        return 1;
109401      }
109402      /* IMP: R-15891-05542 -- syntax diagram for comments */
109403      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
109404      if( c ) i++;
109405      *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
109406      return i;
109407    }
109408    case '%': {
109409      *tokenType = TK_REM;
109410      return 1;
109411    }
109412    case '=': {
109413      *tokenType = TK_EQ;
109414      return 1 + (z[1]=='=');
109415    }
109416    case '<': {
109417      if( (c=z[1])=='=' ){
109418        *tokenType = TK_LE;
109419        return 2;
109420      }else if( c=='>' ){
109421        *tokenType = TK_NE;
109422        return 2;
109423      }else if( c=='<' ){
109424        *tokenType = TK_LSHIFT;
109425        return 2;
109426      }else{
109427        *tokenType = TK_LT;
109428        return 1;
109429      }
109430    }
109431    case '>': {
109432      if( (c=z[1])=='=' ){
109433        *tokenType = TK_GE;
109434        return 2;
109435      }else if( c=='>' ){
109436        *tokenType = TK_RSHIFT;
109437        return 2;
109438      }else{
109439        *tokenType = TK_GT;
109440        return 1;
109441      }
109442    }
109443    case '!': {
109444      if( z[1]!='=' ){
109445        *tokenType = TK_ILLEGAL;
109446        return 2;
109447      }else{
109448        *tokenType = TK_NE;
109449        return 2;
109450      }
109451    }
109452    case '|': {
109453      if( z[1]!='|' ){
109454        *tokenType = TK_BITOR;
109455        return 1;
109456      }else{
109457        *tokenType = TK_CONCAT;
109458        return 2;
109459      }
109460    }
109461    case ',': {
109462      *tokenType = TK_COMMA;
109463      return 1;
109464    }
109465    case '&': {
109466      *tokenType = TK_BITAND;
109467      return 1;
109468    }
109469    case '~': {
109470      *tokenType = TK_BITNOT;
109471      return 1;
109472    }
109473    case '`':
109474    case '\'':
109475    case '"': {
109476      int delim = z[0];
109477      testcase( delim=='`' );
109478      testcase( delim=='\'' );
109479      testcase( delim=='"' );
109480      for(i=1; (c=z[i])!=0; i++){
109481        if( c==delim ){
109482          if( z[i+1]==delim ){
109483            i++;
109484          }else{
109485            break;
109486          }
109487        }
109488      }
109489      if( c=='\'' ){
109490        *tokenType = TK_STRING;
109491        return i+1;
109492      }else if( c!=0 ){
109493        *tokenType = TK_ID;
109494        return i+1;
109495      }else{
109496        *tokenType = TK_ILLEGAL;
109497        return i;
109498      }
109499    }
109500    case '.': {
109501#ifndef SQLITE_OMIT_FLOATING_POINT
109502      if( !sqlite3Isdigit(z[1]) )
109503#endif
109504      {
109505        *tokenType = TK_DOT;
109506        return 1;
109507      }
109508      /* If the next character is a digit, this is a floating point
109509      ** number that begins with ".".  Fall thru into the next case */
109510    }
109511    case '0': case '1': case '2': case '3': case '4':
109512    case '5': case '6': case '7': case '8': case '9': {
109513      testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
109514      testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
109515      testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
109516      testcase( z[0]=='9' );
109517      *tokenType = TK_INTEGER;
109518      for(i=0; sqlite3Isdigit(z[i]); i++){}
109519#ifndef SQLITE_OMIT_FLOATING_POINT
109520      if( z[i]=='.' ){
109521        i++;
109522        while( sqlite3Isdigit(z[i]) ){ i++; }
109523        *tokenType = TK_FLOAT;
109524      }
109525      if( (z[i]=='e' || z[i]=='E') &&
109526           ( sqlite3Isdigit(z[i+1])
109527            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
109528           )
109529      ){
109530        i += 2;
109531        while( sqlite3Isdigit(z[i]) ){ i++; }
109532        *tokenType = TK_FLOAT;
109533      }
109534#endif
109535      while( IdChar(z[i]) ){
109536        *tokenType = TK_ILLEGAL;
109537        i++;
109538      }
109539      return i;
109540    }
109541    case '[': {
109542      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
109543      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
109544      return i;
109545    }
109546    case '?': {
109547      *tokenType = TK_VARIABLE;
109548      for(i=1; sqlite3Isdigit(z[i]); i++){}
109549      return i;
109550    }
109551    case '#': {
109552      for(i=1; sqlite3Isdigit(z[i]); i++){}
109553      if( i>1 ){
109554        /* Parameters of the form #NNN (where NNN is a number) are used
109555        ** internally by sqlite3NestedParse.  */
109556        *tokenType = TK_REGISTER;
109557        return i;
109558      }
109559      /* Fall through into the next case if the '#' is not followed by
109560      ** a digit. Try to match #AAAA where AAAA is a parameter name. */
109561    }
109562#ifndef SQLITE_OMIT_TCL_VARIABLE
109563    case '$':
109564#endif
109565    case '@':  /* For compatibility with MS SQL Server */
109566    case ':': {
109567      int n = 0;
109568      testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
109569      *tokenType = TK_VARIABLE;
109570      for(i=1; (c=z[i])!=0; i++){
109571        if( IdChar(c) ){
109572          n++;
109573#ifndef SQLITE_OMIT_TCL_VARIABLE
109574        }else if( c=='(' && n>0 ){
109575          do{
109576            i++;
109577          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
109578          if( c==')' ){
109579            i++;
109580          }else{
109581            *tokenType = TK_ILLEGAL;
109582          }
109583          break;
109584        }else if( c==':' && z[i+1]==':' ){
109585          i++;
109586#endif
109587        }else{
109588          break;
109589        }
109590      }
109591      if( n==0 ) *tokenType = TK_ILLEGAL;
109592      return i;
109593    }
109594#ifndef SQLITE_OMIT_BLOB_LITERAL
109595    case 'x': case 'X': {
109596      testcase( z[0]=='x' ); testcase( z[0]=='X' );
109597      if( z[1]=='\'' ){
109598        *tokenType = TK_BLOB;
109599        for(i=2; sqlite3Isxdigit(z[i]); i++){}
109600        if( z[i]!='\'' || i%2 ){
109601          *tokenType = TK_ILLEGAL;
109602          while( z[i] && z[i]!='\'' ){ i++; }
109603        }
109604        if( z[i] ) i++;
109605        return i;
109606      }
109607      /* Otherwise fall through to the next case */
109608    }
109609#endif
109610    default: {
109611      if( !IdChar(*z) ){
109612        break;
109613      }
109614      for(i=1; IdChar(z[i]); i++){}
109615      *tokenType = keywordCode((char*)z, i);
109616      return i;
109617    }
109618  }
109619  *tokenType = TK_ILLEGAL;
109620  return 1;
109621}
109622
109623/*
109624** Run the parser on the given SQL string.  The parser structure is
109625** passed in.  An SQLITE_ status code is returned.  If an error occurs
109626** then an and attempt is made to write an error message into
109627** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
109628** error message.
109629*/
109630SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
109631  int nErr = 0;                   /* Number of errors encountered */
109632  int i;                          /* Loop counter */
109633  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
109634  int tokenType;                  /* type of the next token */
109635  int lastTokenParsed = -1;       /* type of the previous token */
109636  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
109637  sqlite3 *db = pParse->db;       /* The database connection */
109638  int mxSqlLen;                   /* Max length of an SQL string */
109639
109640
109641  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
109642  if( db->activeVdbeCnt==0 ){
109643    db->u1.isInterrupted = 0;
109644  }
109645  pParse->rc = SQLITE_OK;
109646  pParse->zTail = zSql;
109647  i = 0;
109648  assert( pzErrMsg!=0 );
109649  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
109650  if( pEngine==0 ){
109651    db->mallocFailed = 1;
109652    return SQLITE_NOMEM;
109653  }
109654  assert( pParse->pNewTable==0 );
109655  assert( pParse->pNewTrigger==0 );
109656  assert( pParse->nVar==0 );
109657  assert( pParse->nzVar==0 );
109658  assert( pParse->azVar==0 );
109659  enableLookaside = db->lookaside.bEnabled;
109660  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
109661  while( !db->mallocFailed && zSql[i]!=0 ){
109662    assert( i>=0 );
109663    pParse->sLastToken.z = &zSql[i];
109664    pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
109665    i += pParse->sLastToken.n;
109666    if( i>mxSqlLen ){
109667      pParse->rc = SQLITE_TOOBIG;
109668      break;
109669    }
109670    switch( tokenType ){
109671      case TK_SPACE: {
109672        if( db->u1.isInterrupted ){
109673          sqlite3ErrorMsg(pParse, "interrupt");
109674          pParse->rc = SQLITE_INTERRUPT;
109675          goto abort_parse;
109676        }
109677        break;
109678      }
109679      case TK_ILLEGAL: {
109680        sqlite3DbFree(db, *pzErrMsg);
109681        *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
109682                        &pParse->sLastToken);
109683        nErr++;
109684        goto abort_parse;
109685      }
109686      case TK_SEMI: {
109687        pParse->zTail = &zSql[i];
109688        /* Fall thru into the default case */
109689      }
109690      default: {
109691        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
109692        lastTokenParsed = tokenType;
109693        if( pParse->rc!=SQLITE_OK ){
109694          goto abort_parse;
109695        }
109696        break;
109697      }
109698    }
109699  }
109700abort_parse:
109701  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
109702    if( lastTokenParsed!=TK_SEMI ){
109703      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
109704      pParse->zTail = &zSql[i];
109705    }
109706    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
109707  }
109708#ifdef YYTRACKMAXSTACKDEPTH
109709  sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
109710      sqlite3ParserStackPeak(pEngine)
109711  );
109712#endif /* YYDEBUG */
109713  sqlite3ParserFree(pEngine, sqlite3_free);
109714  db->lookaside.bEnabled = enableLookaside;
109715  if( db->mallocFailed ){
109716    pParse->rc = SQLITE_NOMEM;
109717  }
109718  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
109719    sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
109720  }
109721  assert( pzErrMsg!=0 );
109722  if( pParse->zErrMsg ){
109723    *pzErrMsg = pParse->zErrMsg;
109724    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
109725    pParse->zErrMsg = 0;
109726    nErr++;
109727  }
109728  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
109729    sqlite3VdbeDelete(pParse->pVdbe);
109730    pParse->pVdbe = 0;
109731  }
109732#ifndef SQLITE_OMIT_SHARED_CACHE
109733  if( pParse->nested==0 ){
109734    sqlite3DbFree(db, pParse->aTableLock);
109735    pParse->aTableLock = 0;
109736    pParse->nTableLock = 0;
109737  }
109738#endif
109739#ifndef SQLITE_OMIT_VIRTUALTABLE
109740  sqlite3_free(pParse->apVtabLock);
109741#endif
109742
109743  if( !IN_DECLARE_VTAB ){
109744    /* If the pParse->declareVtab flag is set, do not delete any table
109745    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
109746    ** will take responsibility for freeing the Table structure.
109747    */
109748    sqlite3DeleteTable(db, pParse->pNewTable);
109749  }
109750
109751  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
109752  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
109753  sqlite3DbFree(db, pParse->azVar);
109754  sqlite3DbFree(db, pParse->aAlias);
109755  while( pParse->pAinc ){
109756    AutoincInfo *p = pParse->pAinc;
109757    pParse->pAinc = p->pNext;
109758    sqlite3DbFree(db, p);
109759  }
109760  while( pParse->pZombieTab ){
109761    Table *p = pParse->pZombieTab;
109762    pParse->pZombieTab = p->pNextZombie;
109763    sqlite3DeleteTable(db, p);
109764  }
109765  if( nErr>0 && pParse->rc==SQLITE_OK ){
109766    pParse->rc = SQLITE_ERROR;
109767  }
109768  return nErr;
109769}
109770
109771/************** End of tokenize.c ********************************************/
109772/************** Begin file complete.c ****************************************/
109773/*
109774** 2001 September 15
109775**
109776** The author disclaims copyright to this source code.  In place of
109777** a legal notice, here is a blessing:
109778**
109779**    May you do good and not evil.
109780**    May you find forgiveness for yourself and forgive others.
109781**    May you share freely, never taking more than you give.
109782**
109783*************************************************************************
109784** An tokenizer for SQL
109785**
109786** This file contains C code that implements the sqlite3_complete() API.
109787** This code used to be part of the tokenizer.c source file.  But by
109788** separating it out, the code will be automatically omitted from
109789** static links that do not use it.
109790*/
109791#ifndef SQLITE_OMIT_COMPLETE
109792
109793/*
109794** This is defined in tokenize.c.  We just have to import the definition.
109795*/
109796#ifndef SQLITE_AMALGAMATION
109797#ifdef SQLITE_ASCII
109798#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
109799#endif
109800#ifdef SQLITE_EBCDIC
109801SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
109802#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
109803#endif
109804#endif /* SQLITE_AMALGAMATION */
109805
109806
109807/*
109808** Token types used by the sqlite3_complete() routine.  See the header
109809** comments on that procedure for additional information.
109810*/
109811#define tkSEMI    0
109812#define tkWS      1
109813#define tkOTHER   2
109814#ifndef SQLITE_OMIT_TRIGGER
109815#define tkEXPLAIN 3
109816#define tkCREATE  4
109817#define tkTEMP    5
109818#define tkTRIGGER 6
109819#define tkEND     7
109820#endif
109821
109822/*
109823** Return TRUE if the given SQL string ends in a semicolon.
109824**
109825** Special handling is require for CREATE TRIGGER statements.
109826** Whenever the CREATE TRIGGER keywords are seen, the statement
109827** must end with ";END;".
109828**
109829** This implementation uses a state machine with 8 states:
109830**
109831**   (0) INVALID   We have not yet seen a non-whitespace character.
109832**
109833**   (1) START     At the beginning or end of an SQL statement.  This routine
109834**                 returns 1 if it ends in the START state and 0 if it ends
109835**                 in any other state.
109836**
109837**   (2) NORMAL    We are in the middle of statement which ends with a single
109838**                 semicolon.
109839**
109840**   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
109841**                 a statement.
109842**
109843**   (4) CREATE    The keyword CREATE has been seen at the beginning of a
109844**                 statement, possibly preceeded by EXPLAIN and/or followed by
109845**                 TEMP or TEMPORARY
109846**
109847**   (5) TRIGGER   We are in the middle of a trigger definition that must be
109848**                 ended by a semicolon, the keyword END, and another semicolon.
109849**
109850**   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
109851**                 the end of a trigger definition.
109852**
109853**   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
109854**                 of a trigger difinition.
109855**
109856** Transitions between states above are determined by tokens extracted
109857** from the input.  The following tokens are significant:
109858**
109859**   (0) tkSEMI      A semicolon.
109860**   (1) tkWS        Whitespace.
109861**   (2) tkOTHER     Any other SQL token.
109862**   (3) tkEXPLAIN   The "explain" keyword.
109863**   (4) tkCREATE    The "create" keyword.
109864**   (5) tkTEMP      The "temp" or "temporary" keyword.
109865**   (6) tkTRIGGER   The "trigger" keyword.
109866**   (7) tkEND       The "end" keyword.
109867**
109868** Whitespace never causes a state transition and is always ignored.
109869** This means that a SQL string of all whitespace is invalid.
109870**
109871** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
109872** to recognize the end of a trigger can be omitted.  All we have to do
109873** is look for a semicolon that is not part of an string or comment.
109874*/
109875SQLITE_API int sqlite3_complete(const char *zSql){
109876  u8 state = 0;   /* Current state, using numbers defined in header comment */
109877  u8 token;       /* Value of the next token */
109878
109879#ifndef SQLITE_OMIT_TRIGGER
109880  /* A complex statement machine used to detect the end of a CREATE TRIGGER
109881  ** statement.  This is the normal case.
109882  */
109883  static const u8 trans[8][8] = {
109884                     /* Token:                                                */
109885     /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
109886     /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
109887     /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
109888     /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
109889     /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
109890     /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
109891     /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
109892     /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
109893     /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
109894  };
109895#else
109896  /* If triggers are not supported by this compile then the statement machine
109897  ** used to detect the end of a statement is much simplier
109898  */
109899  static const u8 trans[3][3] = {
109900                     /* Token:           */
109901     /* State:       **  SEMI  WS  OTHER */
109902     /* 0 INVALID: */ {    1,  0,     2, },
109903     /* 1   START: */ {    1,  1,     2, },
109904     /* 2  NORMAL: */ {    1,  2,     2, },
109905  };
109906#endif /* SQLITE_OMIT_TRIGGER */
109907
109908  while( *zSql ){
109909    switch( *zSql ){
109910      case ';': {  /* A semicolon */
109911        token = tkSEMI;
109912        break;
109913      }
109914      case ' ':
109915      case '\r':
109916      case '\t':
109917      case '\n':
109918      case '\f': {  /* White space is ignored */
109919        token = tkWS;
109920        break;
109921      }
109922      case '/': {   /* C-style comments */
109923        if( zSql[1]!='*' ){
109924          token = tkOTHER;
109925          break;
109926        }
109927        zSql += 2;
109928        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
109929        if( zSql[0]==0 ) return 0;
109930        zSql++;
109931        token = tkWS;
109932        break;
109933      }
109934      case '-': {   /* SQL-style comments from "--" to end of line */
109935        if( zSql[1]!='-' ){
109936          token = tkOTHER;
109937          break;
109938        }
109939        while( *zSql && *zSql!='\n' ){ zSql++; }
109940        if( *zSql==0 ) return state==1;
109941        token = tkWS;
109942        break;
109943      }
109944      case '[': {   /* Microsoft-style identifiers in [...] */
109945        zSql++;
109946        while( *zSql && *zSql!=']' ){ zSql++; }
109947        if( *zSql==0 ) return 0;
109948        token = tkOTHER;
109949        break;
109950      }
109951      case '`':     /* Grave-accent quoted symbols used by MySQL */
109952      case '"':     /* single- and double-quoted strings */
109953      case '\'': {
109954        int c = *zSql;
109955        zSql++;
109956        while( *zSql && *zSql!=c ){ zSql++; }
109957        if( *zSql==0 ) return 0;
109958        token = tkOTHER;
109959        break;
109960      }
109961      default: {
109962#ifdef SQLITE_EBCDIC
109963        unsigned char c;
109964#endif
109965        if( IdChar((u8)*zSql) ){
109966          /* Keywords and unquoted identifiers */
109967          int nId;
109968          for(nId=1; IdChar(zSql[nId]); nId++){}
109969#ifdef SQLITE_OMIT_TRIGGER
109970          token = tkOTHER;
109971#else
109972          switch( *zSql ){
109973            case 'c': case 'C': {
109974              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
109975                token = tkCREATE;
109976              }else{
109977                token = tkOTHER;
109978              }
109979              break;
109980            }
109981            case 't': case 'T': {
109982              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
109983                token = tkTRIGGER;
109984              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
109985                token = tkTEMP;
109986              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
109987                token = tkTEMP;
109988              }else{
109989                token = tkOTHER;
109990              }
109991              break;
109992            }
109993            case 'e':  case 'E': {
109994              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
109995                token = tkEND;
109996              }else
109997#ifndef SQLITE_OMIT_EXPLAIN
109998              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
109999                token = tkEXPLAIN;
110000              }else
110001#endif
110002              {
110003                token = tkOTHER;
110004              }
110005              break;
110006            }
110007            default: {
110008              token = tkOTHER;
110009              break;
110010            }
110011          }
110012#endif /* SQLITE_OMIT_TRIGGER */
110013          zSql += nId-1;
110014        }else{
110015          /* Operators and special symbols */
110016          token = tkOTHER;
110017        }
110018        break;
110019      }
110020    }
110021    state = trans[state][token];
110022    zSql++;
110023  }
110024  return state==1;
110025}
110026
110027#ifndef SQLITE_OMIT_UTF16
110028/*
110029** This routine is the same as the sqlite3_complete() routine described
110030** above, except that the parameter is required to be UTF-16 encoded, not
110031** UTF-8.
110032*/
110033SQLITE_API int sqlite3_complete16(const void *zSql){
110034  sqlite3_value *pVal;
110035  char const *zSql8;
110036  int rc = SQLITE_NOMEM;
110037
110038#ifndef SQLITE_OMIT_AUTOINIT
110039  rc = sqlite3_initialize();
110040  if( rc ) return rc;
110041#endif
110042  pVal = sqlite3ValueNew(0);
110043  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
110044  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
110045  if( zSql8 ){
110046    rc = sqlite3_complete(zSql8);
110047  }else{
110048    rc = SQLITE_NOMEM;
110049  }
110050  sqlite3ValueFree(pVal);
110051  return sqlite3ApiExit(0, rc);
110052}
110053#endif /* SQLITE_OMIT_UTF16 */
110054#endif /* SQLITE_OMIT_COMPLETE */
110055
110056/************** End of complete.c ********************************************/
110057/************** Begin file main.c ********************************************/
110058/*
110059** 2001 September 15
110060**
110061** The author disclaims copyright to this source code.  In place of
110062** a legal notice, here is a blessing:
110063**
110064**    May you do good and not evil.
110065**    May you find forgiveness for yourself and forgive others.
110066**    May you share freely, never taking more than you give.
110067**
110068*************************************************************************
110069** Main file for the SQLite library.  The routines in this file
110070** implement the programmer interface to the library.  Routines in
110071** other files are for internal use by SQLite and should not be
110072** accessed by users of the library.
110073*/
110074
110075#ifdef SQLITE_ENABLE_FTS3
110076/************** Include fts3.h in the middle of main.c ***********************/
110077/************** Begin file fts3.h ********************************************/
110078/*
110079** 2006 Oct 10
110080**
110081** The author disclaims copyright to this source code.  In place of
110082** a legal notice, here is a blessing:
110083**
110084**    May you do good and not evil.
110085**    May you find forgiveness for yourself and forgive others.
110086**    May you share freely, never taking more than you give.
110087**
110088******************************************************************************
110089**
110090** This header file is used by programs that want to link against the
110091** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
110092*/
110093
110094#if 0
110095extern "C" {
110096#endif  /* __cplusplus */
110097
110098SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
110099
110100#if 0
110101}  /* extern "C" */
110102#endif  /* __cplusplus */
110103
110104/************** End of fts3.h ************************************************/
110105/************** Continuing where we left off in main.c ***********************/
110106#endif
110107#ifdef SQLITE_ENABLE_RTREE
110108/************** Include rtree.h in the middle of main.c **********************/
110109/************** Begin file rtree.h *******************************************/
110110/*
110111** 2008 May 26
110112**
110113** The author disclaims copyright to this source code.  In place of
110114** a legal notice, here is a blessing:
110115**
110116**    May you do good and not evil.
110117**    May you find forgiveness for yourself and forgive others.
110118**    May you share freely, never taking more than you give.
110119**
110120******************************************************************************
110121**
110122** This header file is used by programs that want to link against the
110123** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
110124*/
110125
110126#if 0
110127extern "C" {
110128#endif  /* __cplusplus */
110129
110130SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
110131
110132#if 0
110133}  /* extern "C" */
110134#endif  /* __cplusplus */
110135
110136/************** End of rtree.h ***********************************************/
110137/************** Continuing where we left off in main.c ***********************/
110138#endif
110139#ifdef SQLITE_ENABLE_ICU
110140/************** Include sqliteicu.h in the middle of main.c ******************/
110141/************** Begin file sqliteicu.h ***************************************/
110142/*
110143** 2008 May 26
110144**
110145** The author disclaims copyright to this source code.  In place of
110146** a legal notice, here is a blessing:
110147**
110148**    May you do good and not evil.
110149**    May you find forgiveness for yourself and forgive others.
110150**    May you share freely, never taking more than you give.
110151**
110152******************************************************************************
110153**
110154** This header file is used by programs that want to link against the
110155** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
110156*/
110157
110158#if 0
110159extern "C" {
110160#endif  /* __cplusplus */
110161
110162SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
110163
110164#if 0
110165}  /* extern "C" */
110166#endif  /* __cplusplus */
110167
110168
110169/************** End of sqliteicu.h *******************************************/
110170/************** Continuing where we left off in main.c ***********************/
110171#endif
110172
110173#ifndef SQLITE_AMALGAMATION
110174/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
110175** contains the text of SQLITE_VERSION macro.
110176*/
110177SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
110178#endif
110179
110180/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
110181** a pointer to the to the sqlite3_version[] string constant.
110182*/
110183SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
110184
110185/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
110186** pointer to a string constant whose value is the same as the
110187** SQLITE_SOURCE_ID C preprocessor macro.
110188*/
110189SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
110190
110191/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
110192** returns an integer equal to SQLITE_VERSION_NUMBER.
110193*/
110194SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
110195
110196/* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
110197** zero if and only if SQLite was compiled mutexing code omitted due to
110198** the SQLITE_THREADSAFE compile-time option being set to 0.
110199*/
110200SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
110201
110202#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
110203/*
110204** If the following function pointer is not NULL and if
110205** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
110206** I/O active are written using this function.  These messages
110207** are intended for debugging activity only.
110208*/
110209SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
110210#endif
110211
110212/*
110213** If the following global variable points to a string which is the
110214** name of a directory, then that directory will be used to store
110215** temporary files.
110216**
110217** See also the "PRAGMA temp_store_directory" SQL command.
110218*/
110219SQLITE_API char *sqlite3_temp_directory = 0;
110220
110221/*
110222** Initialize SQLite.
110223**
110224** This routine must be called to initialize the memory allocation,
110225** VFS, and mutex subsystems prior to doing any serious work with
110226** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
110227** this routine will be called automatically by key routines such as
110228** sqlite3_open().
110229**
110230** This routine is a no-op except on its very first call for the process,
110231** or for the first call after a call to sqlite3_shutdown.
110232**
110233** The first thread to call this routine runs the initialization to
110234** completion.  If subsequent threads call this routine before the first
110235** thread has finished the initialization process, then the subsequent
110236** threads must block until the first thread finishes with the initialization.
110237**
110238** The first thread might call this routine recursively.  Recursive
110239** calls to this routine should not block, of course.  Otherwise the
110240** initialization process would never complete.
110241**
110242** Let X be the first thread to enter this routine.  Let Y be some other
110243** thread.  Then while the initial invocation of this routine by X is
110244** incomplete, it is required that:
110245**
110246**    *  Calls to this routine from Y must block until the outer-most
110247**       call by X completes.
110248**
110249**    *  Recursive calls to this routine from thread X return immediately
110250**       without blocking.
110251*/
110252SQLITE_API int sqlite3_initialize(void){
110253  sqlite3_mutex *pMaster;                      /* The main static mutex */
110254  int rc;                                      /* Result code */
110255
110256#ifdef SQLITE_OMIT_WSD
110257  rc = sqlite3_wsd_init(4096, 24);
110258  if( rc!=SQLITE_OK ){
110259    return rc;
110260  }
110261#endif
110262
110263  /* If SQLite is already completely initialized, then this call
110264  ** to sqlite3_initialize() should be a no-op.  But the initialization
110265  ** must be complete.  So isInit must not be set until the very end
110266  ** of this routine.
110267  */
110268  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
110269
110270  /* Make sure the mutex subsystem is initialized.  If unable to
110271  ** initialize the mutex subsystem, return early with the error.
110272  ** If the system is so sick that we are unable to allocate a mutex,
110273  ** there is not much SQLite is going to be able to do.
110274  **
110275  ** The mutex subsystem must take care of serializing its own
110276  ** initialization.
110277  */
110278  rc = sqlite3MutexInit();
110279  if( rc ) return rc;
110280
110281  /* Initialize the malloc() system and the recursive pInitMutex mutex.
110282  ** This operation is protected by the STATIC_MASTER mutex.  Note that
110283  ** MutexAlloc() is called for a static mutex prior to initializing the
110284  ** malloc subsystem - this implies that the allocation of a static
110285  ** mutex must not require support from the malloc subsystem.
110286  */
110287  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
110288  sqlite3_mutex_enter(pMaster);
110289  sqlite3GlobalConfig.isMutexInit = 1;
110290  if( !sqlite3GlobalConfig.isMallocInit ){
110291    rc = sqlite3MallocInit();
110292  }
110293  if( rc==SQLITE_OK ){
110294    sqlite3GlobalConfig.isMallocInit = 1;
110295    if( !sqlite3GlobalConfig.pInitMutex ){
110296      sqlite3GlobalConfig.pInitMutex =
110297           sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
110298      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
110299        rc = SQLITE_NOMEM;
110300      }
110301    }
110302  }
110303  if( rc==SQLITE_OK ){
110304    sqlite3GlobalConfig.nRefInitMutex++;
110305  }
110306  sqlite3_mutex_leave(pMaster);
110307
110308  /* If rc is not SQLITE_OK at this point, then either the malloc
110309  ** subsystem could not be initialized or the system failed to allocate
110310  ** the pInitMutex mutex. Return an error in either case.  */
110311  if( rc!=SQLITE_OK ){
110312    return rc;
110313  }
110314
110315  /* Do the rest of the initialization under the recursive mutex so
110316  ** that we will be able to handle recursive calls into
110317  ** sqlite3_initialize().  The recursive calls normally come through
110318  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
110319  ** recursive calls might also be possible.
110320  **
110321  ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
110322  ** to the xInit method, so the xInit method need not be threadsafe.
110323  **
110324  ** The following mutex is what serializes access to the appdef pcache xInit
110325  ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
110326  ** call to sqlite3PcacheInitialize().
110327  */
110328  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
110329  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
110330    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
110331    sqlite3GlobalConfig.inProgress = 1;
110332    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
110333    sqlite3RegisterGlobalFunctions();
110334    if( sqlite3GlobalConfig.isPCacheInit==0 ){
110335      rc = sqlite3PcacheInitialize();
110336    }
110337    if( rc==SQLITE_OK ){
110338      sqlite3GlobalConfig.isPCacheInit = 1;
110339      rc = sqlite3OsInit();
110340    }
110341    if( rc==SQLITE_OK ){
110342      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
110343          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
110344      sqlite3GlobalConfig.isInit = 1;
110345    }
110346    sqlite3GlobalConfig.inProgress = 0;
110347  }
110348  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
110349
110350  /* Go back under the static mutex and clean up the recursive
110351  ** mutex to prevent a resource leak.
110352  */
110353  sqlite3_mutex_enter(pMaster);
110354  sqlite3GlobalConfig.nRefInitMutex--;
110355  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
110356    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
110357    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
110358    sqlite3GlobalConfig.pInitMutex = 0;
110359  }
110360  sqlite3_mutex_leave(pMaster);
110361
110362  /* The following is just a sanity check to make sure SQLite has
110363  ** been compiled correctly.  It is important to run this code, but
110364  ** we don't want to run it too often and soak up CPU cycles for no
110365  ** reason.  So we run it once during initialization.
110366  */
110367#ifndef NDEBUG
110368#ifndef SQLITE_OMIT_FLOATING_POINT
110369  /* This section of code's only "output" is via assert() statements. */
110370  if ( rc==SQLITE_OK ){
110371    u64 x = (((u64)1)<<63)-1;
110372    double y;
110373    assert(sizeof(x)==8);
110374    assert(sizeof(x)==sizeof(y));
110375    memcpy(&y, &x, 8);
110376    assert( sqlite3IsNaN(y) );
110377  }
110378#endif
110379#endif
110380
110381  /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110382  ** compile-time option.
110383  */
110384#ifdef SQLITE_EXTRA_INIT
110385  if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110386    int SQLITE_EXTRA_INIT(void);
110387    rc = SQLITE_EXTRA_INIT();
110388  }
110389#endif
110390
110391  return rc;
110392}
110393
110394/*
110395** Undo the effects of sqlite3_initialize().  Must not be called while
110396** there are outstanding database connections or memory allocations or
110397** while any part of SQLite is otherwise in use in any thread.  This
110398** routine is not threadsafe.  But it is safe to invoke this routine
110399** on when SQLite is already shut down.  If SQLite is already shut down
110400** when this routine is invoked, then this routine is a harmless no-op.
110401*/
110402SQLITE_API int sqlite3_shutdown(void){
110403  if( sqlite3GlobalConfig.isInit ){
110404    sqlite3_os_end();
110405    sqlite3_reset_auto_extension();
110406    sqlite3GlobalConfig.isInit = 0;
110407  }
110408  if( sqlite3GlobalConfig.isPCacheInit ){
110409    sqlite3PcacheShutdown();
110410    sqlite3GlobalConfig.isPCacheInit = 0;
110411  }
110412  if( sqlite3GlobalConfig.isMallocInit ){
110413    sqlite3MallocEnd();
110414    sqlite3GlobalConfig.isMallocInit = 0;
110415  }
110416  if( sqlite3GlobalConfig.isMutexInit ){
110417    sqlite3MutexEnd();
110418    sqlite3GlobalConfig.isMutexInit = 0;
110419  }
110420
110421  return SQLITE_OK;
110422}
110423
110424/*
110425** This API allows applications to modify the global configuration of
110426** the SQLite library at run-time.
110427**
110428** This routine should only be called when there are no outstanding
110429** database connections or memory allocations.  This routine is not
110430** threadsafe.  Failure to heed these warnings can lead to unpredictable
110431** behavior.
110432*/
110433SQLITE_API int sqlite3_config(int op, ...){
110434  va_list ap;
110435  int rc = SQLITE_OK;
110436
110437  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
110438  ** the SQLite library is in use. */
110439  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
110440
110441  va_start(ap, op);
110442  switch( op ){
110443
110444    /* Mutex configuration options are only available in a threadsafe
110445    ** compile.
110446    */
110447#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
110448    case SQLITE_CONFIG_SINGLETHREAD: {
110449      /* Disable all mutexing */
110450      sqlite3GlobalConfig.bCoreMutex = 0;
110451      sqlite3GlobalConfig.bFullMutex = 0;
110452      break;
110453    }
110454    case SQLITE_CONFIG_MULTITHREAD: {
110455      /* Disable mutexing of database connections */
110456      /* Enable mutexing of core data structures */
110457      sqlite3GlobalConfig.bCoreMutex = 1;
110458      sqlite3GlobalConfig.bFullMutex = 0;
110459      break;
110460    }
110461    case SQLITE_CONFIG_SERIALIZED: {
110462      /* Enable all mutexing */
110463      sqlite3GlobalConfig.bCoreMutex = 1;
110464      sqlite3GlobalConfig.bFullMutex = 1;
110465      break;
110466    }
110467    case SQLITE_CONFIG_MUTEX: {
110468      /* Specify an alternative mutex implementation */
110469      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
110470      break;
110471    }
110472    case SQLITE_CONFIG_GETMUTEX: {
110473      /* Retrieve the current mutex implementation */
110474      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
110475      break;
110476    }
110477#endif
110478
110479
110480    case SQLITE_CONFIG_MALLOC: {
110481      /* Specify an alternative malloc implementation */
110482      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
110483      break;
110484    }
110485    case SQLITE_CONFIG_GETMALLOC: {
110486      /* Retrieve the current malloc() implementation */
110487      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
110488      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
110489      break;
110490    }
110491    case SQLITE_CONFIG_MEMSTATUS: {
110492      /* Enable or disable the malloc status collection */
110493      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
110494      break;
110495    }
110496    case SQLITE_CONFIG_SCRATCH: {
110497      /* Designate a buffer for scratch memory space */
110498      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
110499      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
110500      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
110501      break;
110502    }
110503    case SQLITE_CONFIG_PAGECACHE: {
110504      /* Designate a buffer for page cache memory space */
110505      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
110506      sqlite3GlobalConfig.szPage = va_arg(ap, int);
110507      sqlite3GlobalConfig.nPage = va_arg(ap, int);
110508      break;
110509    }
110510
110511    case SQLITE_CONFIG_PCACHE: {
110512      /* Specify an alternative page cache implementation */
110513      sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
110514      break;
110515    }
110516
110517    case SQLITE_CONFIG_GETPCACHE: {
110518      if( sqlite3GlobalConfig.pcache.xInit==0 ){
110519        sqlite3PCacheSetDefault();
110520      }
110521      *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
110522      break;
110523    }
110524
110525#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
110526    case SQLITE_CONFIG_HEAP: {
110527      /* Designate a buffer for heap memory space */
110528      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
110529      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
110530      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
110531
110532      if( sqlite3GlobalConfig.mnReq<1 ){
110533        sqlite3GlobalConfig.mnReq = 1;
110534      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
110535        /* cap min request size at 2^12 */
110536        sqlite3GlobalConfig.mnReq = (1<<12);
110537      }
110538
110539      if( sqlite3GlobalConfig.pHeap==0 ){
110540        /* If the heap pointer is NULL, then restore the malloc implementation
110541        ** back to NULL pointers too.  This will cause the malloc to go
110542        ** back to its default implementation when sqlite3_initialize() is
110543        ** run.
110544        */
110545        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
110546      }else{
110547        /* The heap pointer is not NULL, then install one of the
110548        ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
110549        ** ENABLE_MEMSYS5 is defined, return an error.
110550        */
110551#ifdef SQLITE_ENABLE_MEMSYS3
110552        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
110553#endif
110554#ifdef SQLITE_ENABLE_MEMSYS5
110555        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
110556#endif
110557      }
110558      break;
110559    }
110560#endif
110561
110562    case SQLITE_CONFIG_LOOKASIDE: {
110563      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
110564      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
110565      break;
110566    }
110567
110568    /* Record a pointer to the logger funcction and its first argument.
110569    ** The default is NULL.  Logging is disabled if the function pointer is
110570    ** NULL.
110571    */
110572    case SQLITE_CONFIG_LOG: {
110573      /* MSVC is picky about pulling func ptrs from va lists.
110574      ** http://support.microsoft.com/kb/47961
110575      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
110576      */
110577      typedef void(*LOGFUNC_t)(void*,int,const char*);
110578      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
110579      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
110580      break;
110581    }
110582
110583    case SQLITE_CONFIG_URI: {
110584      sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
110585      break;
110586    }
110587
110588    default: {
110589      rc = SQLITE_ERROR;
110590      break;
110591    }
110592  }
110593  va_end(ap);
110594  return rc;
110595}
110596
110597/*
110598** Set up the lookaside buffers for a database connection.
110599** Return SQLITE_OK on success.
110600** If lookaside is already active, return SQLITE_BUSY.
110601**
110602** The sz parameter is the number of bytes in each lookaside slot.
110603** The cnt parameter is the number of slots.  If pStart is NULL the
110604** space for the lookaside memory is obtained from sqlite3_malloc().
110605** If pStart is not NULL then it is sz*cnt bytes of memory to use for
110606** the lookaside memory.
110607*/
110608static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
110609  void *pStart;
110610  if( db->lookaside.nOut ){
110611    return SQLITE_BUSY;
110612  }
110613  /* Free any existing lookaside buffer for this handle before
110614  ** allocating a new one so we don't have to have space for
110615  ** both at the same time.
110616  */
110617  if( db->lookaside.bMalloced ){
110618    sqlite3_free(db->lookaside.pStart);
110619  }
110620  /* The size of a lookaside slot needs to be larger than a pointer
110621  ** to be useful.
110622  */
110623  if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
110624  if( cnt<0 ) cnt = 0;
110625  if( sz==0 || cnt==0 ){
110626    sz = 0;
110627    pStart = 0;
110628  }else if( pBuf==0 ){
110629    sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
110630    sqlite3BeginBenignMalloc();
110631    pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
110632    sqlite3EndBenignMalloc();
110633  }else{
110634    sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
110635    pStart = pBuf;
110636  }
110637  db->lookaside.pStart = pStart;
110638  db->lookaside.pFree = 0;
110639  db->lookaside.sz = (u16)sz;
110640  if( pStart ){
110641    int i;
110642    LookasideSlot *p;
110643    assert( sz > (int)sizeof(LookasideSlot*) );
110644    p = (LookasideSlot*)pStart;
110645    for(i=cnt-1; i>=0; i--){
110646      p->pNext = db->lookaside.pFree;
110647      db->lookaside.pFree = p;
110648      p = (LookasideSlot*)&((u8*)p)[sz];
110649    }
110650    db->lookaside.pEnd = p;
110651    db->lookaside.bEnabled = 1;
110652    db->lookaside.bMalloced = pBuf==0 ?1:0;
110653  }else{
110654    db->lookaside.pEnd = 0;
110655    db->lookaside.bEnabled = 0;
110656    db->lookaside.bMalloced = 0;
110657  }
110658  return SQLITE_OK;
110659}
110660
110661/*
110662** Return the mutex associated with a database connection.
110663*/
110664SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
110665  return db->mutex;
110666}
110667
110668/*
110669** Configuration settings for an individual database connection
110670*/
110671SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
110672  va_list ap;
110673  int rc;
110674  va_start(ap, op);
110675  switch( op ){
110676    case SQLITE_DBCONFIG_LOOKASIDE: {
110677      void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
110678      int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
110679      int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
110680      rc = setupLookaside(db, pBuf, sz, cnt);
110681      break;
110682    }
110683    default: {
110684      static const struct {
110685        int op;      /* The opcode */
110686        u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
110687      } aFlagOp[] = {
110688        { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
110689        { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
110690      };
110691      unsigned int i;
110692      rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
110693      for(i=0; i<ArraySize(aFlagOp); i++){
110694        if( aFlagOp[i].op==op ){
110695          int onoff = va_arg(ap, int);
110696          int *pRes = va_arg(ap, int*);
110697          int oldFlags = db->flags;
110698          if( onoff>0 ){
110699            db->flags |= aFlagOp[i].mask;
110700          }else if( onoff==0 ){
110701            db->flags &= ~aFlagOp[i].mask;
110702          }
110703          if( oldFlags!=db->flags ){
110704            sqlite3ExpirePreparedStatements(db);
110705          }
110706          if( pRes ){
110707            *pRes = (db->flags & aFlagOp[i].mask)!=0;
110708          }
110709          rc = SQLITE_OK;
110710          break;
110711        }
110712      }
110713      break;
110714    }
110715  }
110716  va_end(ap);
110717  return rc;
110718}
110719
110720
110721/*
110722** Return true if the buffer z[0..n-1] contains all spaces.
110723*/
110724static int allSpaces(const char *z, int n){
110725  while( n>0 && z[n-1]==' ' ){ n--; }
110726  return n==0;
110727}
110728
110729/*
110730** This is the default collating function named "BINARY" which is always
110731** available.
110732**
110733** If the padFlag argument is not NULL then space padding at the end
110734** of strings is ignored.  This implements the RTRIM collation.
110735*/
110736static int binCollFunc(
110737  void *padFlag,
110738  int nKey1, const void *pKey1,
110739  int nKey2, const void *pKey2
110740){
110741  int rc, n;
110742  n = nKey1<nKey2 ? nKey1 : nKey2;
110743  rc = memcmp(pKey1, pKey2, n);
110744  if( rc==0 ){
110745    if( padFlag
110746     && allSpaces(((char*)pKey1)+n, nKey1-n)
110747     && allSpaces(((char*)pKey2)+n, nKey2-n)
110748    ){
110749      /* Leave rc unchanged at 0 */
110750    }else{
110751      rc = nKey1 - nKey2;
110752    }
110753  }
110754  return rc;
110755}
110756
110757/*
110758** Another built-in collating sequence: NOCASE.
110759**
110760** This collating sequence is intended to be used for "case independant
110761** comparison". SQLite's knowledge of upper and lower case equivalents
110762** extends only to the 26 characters used in the English language.
110763**
110764** At the moment there is only a UTF-8 implementation.
110765*/
110766static int nocaseCollatingFunc(
110767  void *NotUsed,
110768  int nKey1, const void *pKey1,
110769  int nKey2, const void *pKey2
110770){
110771  int r = sqlite3StrNICmp(
110772      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
110773  UNUSED_PARAMETER(NotUsed);
110774  if( 0==r ){
110775    r = nKey1-nKey2;
110776  }
110777  return r;
110778}
110779
110780/*
110781** Return the ROWID of the most recent insert
110782*/
110783SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
110784  return db->lastRowid;
110785}
110786
110787/*
110788** Return the number of changes in the most recent call to sqlite3_exec().
110789*/
110790SQLITE_API int sqlite3_changes(sqlite3 *db){
110791  return db->nChange;
110792}
110793
110794/*
110795** Return the number of changes since the database handle was opened.
110796*/
110797SQLITE_API int sqlite3_total_changes(sqlite3 *db){
110798  return db->nTotalChange;
110799}
110800
110801/*
110802** Close all open savepoints. This function only manipulates fields of the
110803** database handle object, it does not close any savepoints that may be open
110804** at the b-tree/pager level.
110805*/
110806SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
110807  while( db->pSavepoint ){
110808    Savepoint *pTmp = db->pSavepoint;
110809    db->pSavepoint = pTmp->pNext;
110810    sqlite3DbFree(db, pTmp);
110811  }
110812  db->nSavepoint = 0;
110813  db->nStatement = 0;
110814  db->isTransactionSavepoint = 0;
110815}
110816
110817/*
110818** Invoke the destructor function associated with FuncDef p, if any. Except,
110819** if this is not the last copy of the function, do not invoke it. Multiple
110820** copies of a single function are created when create_function() is called
110821** with SQLITE_ANY as the encoding.
110822*/
110823static void functionDestroy(sqlite3 *db, FuncDef *p){
110824  FuncDestructor *pDestructor = p->pDestructor;
110825  if( pDestructor ){
110826    pDestructor->nRef--;
110827    if( pDestructor->nRef==0 ){
110828      pDestructor->xDestroy(pDestructor->pUserData);
110829      sqlite3DbFree(db, pDestructor);
110830    }
110831  }
110832}
110833
110834/*
110835** Close an existing SQLite database
110836*/
110837SQLITE_API int sqlite3_close(sqlite3 *db){
110838  HashElem *i;                    /* Hash table iterator */
110839  int j;
110840
110841  if( !db ){
110842    return SQLITE_OK;
110843  }
110844  if( !sqlite3SafetyCheckSickOrOk(db) ){
110845    return SQLITE_MISUSE_BKPT;
110846  }
110847  sqlite3_mutex_enter(db->mutex);
110848
110849  /* Force xDestroy calls on all virtual tables */
110850  sqlite3ResetInternalSchema(db, -1);
110851
110852  /* If a transaction is open, the ResetInternalSchema() call above
110853  ** will not have called the xDisconnect() method on any virtual
110854  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
110855  ** call will do so. We need to do this before the check for active
110856  ** SQL statements below, as the v-table implementation may be storing
110857  ** some prepared statements internally.
110858  */
110859  sqlite3VtabRollback(db);
110860
110861  /* If there are any outstanding VMs, return SQLITE_BUSY. */
110862  if( db->pVdbe ){
110863    sqlite3Error(db, SQLITE_BUSY,
110864        "unable to close due to unfinalised statements");
110865    sqlite3_mutex_leave(db->mutex);
110866    return SQLITE_BUSY;
110867  }
110868  assert( sqlite3SafetyCheckSickOrOk(db) );
110869
110870  for(j=0; j<db->nDb; j++){
110871    Btree *pBt = db->aDb[j].pBt;
110872    if( pBt && sqlite3BtreeIsInBackup(pBt) ){
110873      sqlite3Error(db, SQLITE_BUSY,
110874          "unable to close due to unfinished backup operation");
110875      sqlite3_mutex_leave(db->mutex);
110876      return SQLITE_BUSY;
110877    }
110878  }
110879
110880  /* Free any outstanding Savepoint structures. */
110881  sqlite3CloseSavepoints(db);
110882
110883  for(j=0; j<db->nDb; j++){
110884    struct Db *pDb = &db->aDb[j];
110885    if( pDb->pBt ){
110886      sqlite3BtreeClose(pDb->pBt);
110887      pDb->pBt = 0;
110888      if( j!=1 ){
110889        pDb->pSchema = 0;
110890      }
110891    }
110892  }
110893  sqlite3ResetInternalSchema(db, -1);
110894
110895  /* Tell the code in notify.c that the connection no longer holds any
110896  ** locks and does not require any further unlock-notify callbacks.
110897  */
110898  sqlite3ConnectionClosed(db);
110899
110900  assert( db->nDb<=2 );
110901  assert( db->aDb==db->aDbStatic );
110902  for(j=0; j<ArraySize(db->aFunc.a); j++){
110903    FuncDef *pNext, *pHash, *p;
110904    for(p=db->aFunc.a[j]; p; p=pHash){
110905      pHash = p->pHash;
110906      while( p ){
110907        functionDestroy(db, p);
110908        pNext = p->pNext;
110909        sqlite3DbFree(db, p);
110910        p = pNext;
110911      }
110912    }
110913  }
110914  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
110915    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
110916    /* Invoke any destructors registered for collation sequence user data. */
110917    for(j=0; j<3; j++){
110918      if( pColl[j].xDel ){
110919        pColl[j].xDel(pColl[j].pUser);
110920      }
110921    }
110922    sqlite3DbFree(db, pColl);
110923  }
110924  sqlite3HashClear(&db->aCollSeq);
110925#ifndef SQLITE_OMIT_VIRTUALTABLE
110926  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
110927    Module *pMod = (Module *)sqliteHashData(i);
110928    if( pMod->xDestroy ){
110929      pMod->xDestroy(pMod->pAux);
110930    }
110931    sqlite3DbFree(db, pMod);
110932  }
110933  sqlite3HashClear(&db->aModule);
110934#endif
110935
110936  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
110937  if( db->pErr ){
110938    sqlite3ValueFree(db->pErr);
110939  }
110940  sqlite3CloseExtensions(db);
110941
110942  db->magic = SQLITE_MAGIC_ERROR;
110943
110944  /* The temp-database schema is allocated differently from the other schema
110945  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
110946  ** So it needs to be freed here. Todo: Why not roll the temp schema into
110947  ** the same sqliteMalloc() as the one that allocates the database
110948  ** structure?
110949  */
110950  sqlite3DbFree(db, db->aDb[1].pSchema);
110951  sqlite3_mutex_leave(db->mutex);
110952  db->magic = SQLITE_MAGIC_CLOSED;
110953  sqlite3_mutex_free(db->mutex);
110954  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
110955  if( db->lookaside.bMalloced ){
110956    sqlite3_free(db->lookaside.pStart);
110957  }
110958  sqlite3_free(db);
110959  return SQLITE_OK;
110960}
110961
110962/*
110963** Rollback all database files.
110964*/
110965SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
110966  int i;
110967  int inTrans = 0;
110968  assert( sqlite3_mutex_held(db->mutex) );
110969  sqlite3BeginBenignMalloc();
110970  for(i=0; i<db->nDb; i++){
110971    if( db->aDb[i].pBt ){
110972      if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
110973        inTrans = 1;
110974      }
110975      sqlite3BtreeRollback(db->aDb[i].pBt);
110976      db->aDb[i].inTrans = 0;
110977    }
110978  }
110979  sqlite3VtabRollback(db);
110980  sqlite3EndBenignMalloc();
110981
110982  if( db->flags&SQLITE_InternChanges ){
110983    sqlite3ExpirePreparedStatements(db);
110984    sqlite3ResetInternalSchema(db, -1);
110985  }
110986
110987  /* Any deferred constraint violations have now been resolved. */
110988  db->nDeferredCons = 0;
110989
110990  /* If one has been configured, invoke the rollback-hook callback */
110991  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
110992    db->xRollbackCallback(db->pRollbackArg);
110993  }
110994}
110995
110996/*
110997** Return a static string that describes the kind of error specified in the
110998** argument.
110999*/
111000SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
111001  static const char* const aMsg[] = {
111002    /* SQLITE_OK          */ "not an error",
111003    /* SQLITE_ERROR       */ "SQL logic error or missing database",
111004    /* SQLITE_INTERNAL    */ 0,
111005    /* SQLITE_PERM        */ "access permission denied",
111006    /* SQLITE_ABORT       */ "callback requested query abort",
111007    /* SQLITE_BUSY        */ "database is locked",
111008    /* SQLITE_LOCKED      */ "database table is locked",
111009    /* SQLITE_NOMEM       */ "out of memory",
111010    /* SQLITE_READONLY    */ "attempt to write a readonly database",
111011    /* SQLITE_INTERRUPT   */ "interrupted",
111012    /* SQLITE_IOERR       */ "disk I/O error",
111013    /* SQLITE_CORRUPT     */ "database disk image is malformed",
111014    /* SQLITE_NOTFOUND    */ "unknown operation",
111015    /* SQLITE_FULL        */ "database or disk is full",
111016    /* SQLITE_CANTOPEN    */ "unable to open database file",
111017    /* SQLITE_PROTOCOL    */ "locking protocol",
111018    /* SQLITE_EMPTY       */ "table contains no data",
111019    /* SQLITE_SCHEMA      */ "database schema has changed",
111020    /* SQLITE_TOOBIG      */ "string or blob too big",
111021    /* SQLITE_CONSTRAINT  */ "constraint failed",
111022    /* SQLITE_MISMATCH    */ "datatype mismatch",
111023    /* SQLITE_MISUSE      */ "library routine called out of sequence",
111024    /* SQLITE_NOLFS       */ "large file support is disabled",
111025    /* SQLITE_AUTH        */ "authorization denied",
111026    /* SQLITE_FORMAT      */ "auxiliary database format error",
111027    /* SQLITE_RANGE       */ "bind or column index out of range",
111028    /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
111029  };
111030  rc &= 0xff;
111031  if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
111032    return aMsg[rc];
111033  }else{
111034    return "unknown error";
111035  }
111036}
111037
111038/*
111039** This routine implements a busy callback that sleeps and tries
111040** again until a timeout value is reached.  The timeout value is
111041** an integer number of milliseconds passed in as the first
111042** argument.
111043*/
111044static int sqliteDefaultBusyCallback(
111045 void *ptr,               /* Database connection */
111046 int count                /* Number of times table has been busy */
111047){
111048#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
111049  static const u8 delays[] =
111050     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
111051  static const u8 totals[] =
111052     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
111053# define NDELAY ArraySize(delays)
111054  sqlite3 *db = (sqlite3 *)ptr;
111055  int timeout = db->busyTimeout;
111056  int delay, prior;
111057
111058  assert( count>=0 );
111059  if( count < NDELAY ){
111060    delay = delays[count];
111061    prior = totals[count];
111062  }else{
111063    delay = delays[NDELAY-1];
111064    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
111065  }
111066  if( prior + delay > timeout ){
111067    delay = timeout - prior;
111068    if( delay<=0 ) return 0;
111069  }
111070  sqlite3OsSleep(db->pVfs, delay*1000);
111071  return 1;
111072#else
111073  sqlite3 *db = (sqlite3 *)ptr;
111074  int timeout = ((sqlite3 *)ptr)->busyTimeout;
111075  if( (count+1)*1000 > timeout ){
111076    return 0;
111077  }
111078  sqlite3OsSleep(db->pVfs, 1000000);
111079  return 1;
111080#endif
111081}
111082
111083/*
111084** Invoke the given busy handler.
111085**
111086** This routine is called when an operation failed with a lock.
111087** If this routine returns non-zero, the lock is retried.  If it
111088** returns 0, the operation aborts with an SQLITE_BUSY error.
111089*/
111090SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
111091  int rc;
111092  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
111093  rc = p->xFunc(p->pArg, p->nBusy);
111094  if( rc==0 ){
111095    p->nBusy = -1;
111096  }else{
111097    p->nBusy++;
111098  }
111099  return rc;
111100}
111101
111102/*
111103** This routine sets the busy callback for an Sqlite database to the
111104** given callback function with the given argument.
111105*/
111106SQLITE_API int sqlite3_busy_handler(
111107  sqlite3 *db,
111108  int (*xBusy)(void*,int),
111109  void *pArg
111110){
111111  sqlite3_mutex_enter(db->mutex);
111112  db->busyHandler.xFunc = xBusy;
111113  db->busyHandler.pArg = pArg;
111114  db->busyHandler.nBusy = 0;
111115  sqlite3_mutex_leave(db->mutex);
111116  return SQLITE_OK;
111117}
111118
111119#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
111120/*
111121** This routine sets the progress callback for an Sqlite database to the
111122** given callback function with the given argument. The progress callback will
111123** be invoked every nOps opcodes.
111124*/
111125SQLITE_API void sqlite3_progress_handler(
111126  sqlite3 *db,
111127  int nOps,
111128  int (*xProgress)(void*),
111129  void *pArg
111130){
111131  sqlite3_mutex_enter(db->mutex);
111132  if( nOps>0 ){
111133    db->xProgress = xProgress;
111134    db->nProgressOps = nOps;
111135    db->pProgressArg = pArg;
111136  }else{
111137    db->xProgress = 0;
111138    db->nProgressOps = 0;
111139    db->pProgressArg = 0;
111140  }
111141  sqlite3_mutex_leave(db->mutex);
111142}
111143#endif
111144
111145
111146/*
111147** This routine installs a default busy handler that waits for the
111148** specified number of milliseconds before returning 0.
111149*/
111150SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
111151  if( ms>0 ){
111152    db->busyTimeout = ms;
111153    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
111154  }else{
111155    sqlite3_busy_handler(db, 0, 0);
111156  }
111157  return SQLITE_OK;
111158}
111159
111160/*
111161** Cause any pending operation to stop at its earliest opportunity.
111162*/
111163SQLITE_API void sqlite3_interrupt(sqlite3 *db){
111164  db->u1.isInterrupted = 1;
111165}
111166
111167
111168/*
111169** This function is exactly the same as sqlite3_create_function(), except
111170** that it is designed to be called by internal code. The difference is
111171** that if a malloc() fails in sqlite3_create_function(), an error code
111172** is returned and the mallocFailed flag cleared.
111173*/
111174SQLITE_PRIVATE int sqlite3CreateFunc(
111175  sqlite3 *db,
111176  const char *zFunctionName,
111177  int nArg,
111178  int enc,
111179  void *pUserData,
111180  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
111181  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
111182  void (*xFinal)(sqlite3_context*),
111183  FuncDestructor *pDestructor
111184){
111185  FuncDef *p;
111186  int nName;
111187
111188  assert( sqlite3_mutex_held(db->mutex) );
111189  if( zFunctionName==0 ||
111190      (xFunc && (xFinal || xStep)) ||
111191      (!xFunc && (xFinal && !xStep)) ||
111192      (!xFunc && (!xFinal && xStep)) ||
111193      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
111194      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
111195    return SQLITE_MISUSE_BKPT;
111196  }
111197
111198#ifndef SQLITE_OMIT_UTF16
111199  /* If SQLITE_UTF16 is specified as the encoding type, transform this
111200  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
111201  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
111202  **
111203  ** If SQLITE_ANY is specified, add three versions of the function
111204  ** to the hash table.
111205  */
111206  if( enc==SQLITE_UTF16 ){
111207    enc = SQLITE_UTF16NATIVE;
111208  }else if( enc==SQLITE_ANY ){
111209    int rc;
111210    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
111211         pUserData, xFunc, xStep, xFinal, pDestructor);
111212    if( rc==SQLITE_OK ){
111213      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
111214          pUserData, xFunc, xStep, xFinal, pDestructor);
111215    }
111216    if( rc!=SQLITE_OK ){
111217      return rc;
111218    }
111219    enc = SQLITE_UTF16BE;
111220  }
111221#else
111222  enc = SQLITE_UTF8;
111223#endif
111224
111225  /* Check if an existing function is being overridden or deleted. If so,
111226  ** and there are active VMs, then return SQLITE_BUSY. If a function
111227  ** is being overridden/deleted but there are no active VMs, allow the
111228  ** operation to continue but invalidate all precompiled statements.
111229  */
111230  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
111231  if( p && p->iPrefEnc==enc && p->nArg==nArg ){
111232    if( db->activeVdbeCnt ){
111233      sqlite3Error(db, SQLITE_BUSY,
111234        "unable to delete/modify user-function due to active statements");
111235      assert( !db->mallocFailed );
111236      return SQLITE_BUSY;
111237    }else{
111238      sqlite3ExpirePreparedStatements(db);
111239    }
111240  }
111241
111242  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
111243  assert(p || db->mallocFailed);
111244  if( !p ){
111245    return SQLITE_NOMEM;
111246  }
111247
111248  /* If an older version of the function with a configured destructor is
111249  ** being replaced invoke the destructor function here. */
111250  functionDestroy(db, p);
111251
111252  if( pDestructor ){
111253    pDestructor->nRef++;
111254  }
111255  p->pDestructor = pDestructor;
111256  p->flags = 0;
111257  p->xFunc = xFunc;
111258  p->xStep = xStep;
111259  p->xFinalize = xFinal;
111260  p->pUserData = pUserData;
111261  p->nArg = (u16)nArg;
111262  return SQLITE_OK;
111263}
111264
111265/*
111266** Create new user functions.
111267*/
111268SQLITE_API int sqlite3_create_function(
111269  sqlite3 *db,
111270  const char *zFunc,
111271  int nArg,
111272  int enc,
111273  void *p,
111274  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
111275  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
111276  void (*xFinal)(sqlite3_context*)
111277){
111278  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
111279                                    xFinal, 0);
111280}
111281
111282SQLITE_API int sqlite3_create_function_v2(
111283  sqlite3 *db,
111284  const char *zFunc,
111285  int nArg,
111286  int enc,
111287  void *p,
111288  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
111289  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
111290  void (*xFinal)(sqlite3_context*),
111291  void (*xDestroy)(void *)
111292){
111293  int rc = SQLITE_ERROR;
111294  FuncDestructor *pArg = 0;
111295  sqlite3_mutex_enter(db->mutex);
111296  if( xDestroy ){
111297    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
111298    if( !pArg ){
111299      xDestroy(p);
111300      goto out;
111301    }
111302    pArg->xDestroy = xDestroy;
111303    pArg->pUserData = p;
111304  }
111305  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
111306  if( pArg && pArg->nRef==0 ){
111307    assert( rc!=SQLITE_OK );
111308    xDestroy(p);
111309    sqlite3DbFree(db, pArg);
111310  }
111311
111312 out:
111313  rc = sqlite3ApiExit(db, rc);
111314  sqlite3_mutex_leave(db->mutex);
111315  return rc;
111316}
111317
111318#ifndef SQLITE_OMIT_UTF16
111319SQLITE_API int sqlite3_create_function16(
111320  sqlite3 *db,
111321  const void *zFunctionName,
111322  int nArg,
111323  int eTextRep,
111324  void *p,
111325  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
111326  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
111327  void (*xFinal)(sqlite3_context*)
111328){
111329  int rc;
111330  char *zFunc8;
111331  sqlite3_mutex_enter(db->mutex);
111332  assert( !db->mallocFailed );
111333  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
111334  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
111335  sqlite3DbFree(db, zFunc8);
111336  rc = sqlite3ApiExit(db, rc);
111337  sqlite3_mutex_leave(db->mutex);
111338  return rc;
111339}
111340#endif
111341
111342
111343/*
111344** Declare that a function has been overloaded by a virtual table.
111345**
111346** If the function already exists as a regular global function, then
111347** this routine is a no-op.  If the function does not exist, then create
111348** a new one that always throws a run-time error.
111349**
111350** When virtual tables intend to provide an overloaded function, they
111351** should call this routine to make sure the global function exists.
111352** A global function must exist in order for name resolution to work
111353** properly.
111354*/
111355SQLITE_API int sqlite3_overload_function(
111356  sqlite3 *db,
111357  const char *zName,
111358  int nArg
111359){
111360  int nName = sqlite3Strlen30(zName);
111361  int rc;
111362  sqlite3_mutex_enter(db->mutex);
111363  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
111364    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111365                      0, sqlite3InvalidFunction, 0, 0, 0);
111366  }
111367  rc = sqlite3ApiExit(db, SQLITE_OK);
111368  sqlite3_mutex_leave(db->mutex);
111369  return rc;
111370}
111371
111372#ifndef SQLITE_OMIT_TRACE
111373/*
111374** Register a trace function.  The pArg from the previously registered trace
111375** is returned.
111376**
111377** A NULL trace function means that no tracing is executes.  A non-NULL
111378** trace is a pointer to a function that is invoked at the start of each
111379** SQL statement.
111380*/
111381SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
111382  void *pOld;
111383  sqlite3_mutex_enter(db->mutex);
111384  pOld = db->pTraceArg;
111385  db->xTrace = xTrace;
111386  db->pTraceArg = pArg;
111387  sqlite3_mutex_leave(db->mutex);
111388  return pOld;
111389}
111390/*
111391** Register a profile function.  The pArg from the previously registered
111392** profile function is returned.
111393**
111394** A NULL profile function means that no profiling is executes.  A non-NULL
111395** profile is a pointer to a function that is invoked at the conclusion of
111396** each SQL statement that is run.
111397*/
111398SQLITE_API void *sqlite3_profile(
111399  sqlite3 *db,
111400  void (*xProfile)(void*,const char*,sqlite_uint64),
111401  void *pArg
111402){
111403  void *pOld;
111404  sqlite3_mutex_enter(db->mutex);
111405  pOld = db->pProfileArg;
111406  db->xProfile = xProfile;
111407  db->pProfileArg = pArg;
111408  sqlite3_mutex_leave(db->mutex);
111409  return pOld;
111410}
111411#endif /* SQLITE_OMIT_TRACE */
111412
111413/*** EXPERIMENTAL ***
111414**
111415** Register a function to be invoked when a transaction comments.
111416** If the invoked function returns non-zero, then the commit becomes a
111417** rollback.
111418*/
111419SQLITE_API void *sqlite3_commit_hook(
111420  sqlite3 *db,              /* Attach the hook to this database */
111421  int (*xCallback)(void*),  /* Function to invoke on each commit */
111422  void *pArg                /* Argument to the function */
111423){
111424  void *pOld;
111425  sqlite3_mutex_enter(db->mutex);
111426  pOld = db->pCommitArg;
111427  db->xCommitCallback = xCallback;
111428  db->pCommitArg = pArg;
111429  sqlite3_mutex_leave(db->mutex);
111430  return pOld;
111431}
111432
111433/*
111434** Register a callback to be invoked each time a row is updated,
111435** inserted or deleted using this database connection.
111436*/
111437SQLITE_API void *sqlite3_update_hook(
111438  sqlite3 *db,              /* Attach the hook to this database */
111439  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
111440  void *pArg                /* Argument to the function */
111441){
111442  void *pRet;
111443  sqlite3_mutex_enter(db->mutex);
111444  pRet = db->pUpdateArg;
111445  db->xUpdateCallback = xCallback;
111446  db->pUpdateArg = pArg;
111447  sqlite3_mutex_leave(db->mutex);
111448  return pRet;
111449}
111450
111451/*
111452** Register a callback to be invoked each time a transaction is rolled
111453** back by this database connection.
111454*/
111455SQLITE_API void *sqlite3_rollback_hook(
111456  sqlite3 *db,              /* Attach the hook to this database */
111457  void (*xCallback)(void*), /* Callback function */
111458  void *pArg                /* Argument to the function */
111459){
111460  void *pRet;
111461  sqlite3_mutex_enter(db->mutex);
111462  pRet = db->pRollbackArg;
111463  db->xRollbackCallback = xCallback;
111464  db->pRollbackArg = pArg;
111465  sqlite3_mutex_leave(db->mutex);
111466  return pRet;
111467}
111468
111469#ifndef SQLITE_OMIT_WAL
111470/*
111471** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
111472** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
111473** is greater than sqlite3.pWalArg cast to an integer (the value configured by
111474** wal_autocheckpoint()).
111475*/
111476SQLITE_PRIVATE int sqlite3WalDefaultHook(
111477  void *pClientData,     /* Argument */
111478  sqlite3 *db,           /* Connection */
111479  const char *zDb,       /* Database */
111480  int nFrame             /* Size of WAL */
111481){
111482  if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
111483    sqlite3BeginBenignMalloc();
111484    sqlite3_wal_checkpoint(db, zDb);
111485    sqlite3EndBenignMalloc();
111486  }
111487  return SQLITE_OK;
111488}
111489#endif /* SQLITE_OMIT_WAL */
111490
111491/*
111492** Configure an sqlite3_wal_hook() callback to automatically checkpoint
111493** a database after committing a transaction if there are nFrame or
111494** more frames in the log file. Passing zero or a negative value as the
111495** nFrame parameter disables automatic checkpoints entirely.
111496**
111497** The callback registered by this function replaces any existing callback
111498** registered using sqlite3_wal_hook(). Likewise, registering a callback
111499** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
111500** configured by this function.
111501*/
111502SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
111503#ifdef SQLITE_OMIT_WAL
111504  UNUSED_PARAMETER(db);
111505  UNUSED_PARAMETER(nFrame);
111506#else
111507  if( nFrame>0 ){
111508    sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
111509  }else{
111510    sqlite3_wal_hook(db, 0, 0);
111511  }
111512#endif
111513  return SQLITE_OK;
111514}
111515
111516/*
111517** Register a callback to be invoked each time a transaction is written
111518** into the write-ahead-log by this database connection.
111519*/
111520SQLITE_API void *sqlite3_wal_hook(
111521  sqlite3 *db,                    /* Attach the hook to this db handle */
111522  int(*xCallback)(void *, sqlite3*, const char*, int),
111523  void *pArg                      /* First argument passed to xCallback() */
111524){
111525#ifndef SQLITE_OMIT_WAL
111526  void *pRet;
111527  sqlite3_mutex_enter(db->mutex);
111528  pRet = db->pWalArg;
111529  db->xWalCallback = xCallback;
111530  db->pWalArg = pArg;
111531  sqlite3_mutex_leave(db->mutex);
111532  return pRet;
111533#else
111534  return 0;
111535#endif
111536}
111537
111538/*
111539** Checkpoint database zDb.
111540*/
111541SQLITE_API int sqlite3_wal_checkpoint_v2(
111542  sqlite3 *db,                    /* Database handle */
111543  const char *zDb,                /* Name of attached database (or NULL) */
111544  int eMode,                      /* SQLITE_CHECKPOINT_* value */
111545  int *pnLog,                     /* OUT: Size of WAL log in frames */
111546  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
111547){
111548#ifdef SQLITE_OMIT_WAL
111549  return SQLITE_OK;
111550#else
111551  int rc;                         /* Return code */
111552  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
111553
111554  /* Initialize the output variables to -1 in case an error occurs. */
111555  if( pnLog ) *pnLog = -1;
111556  if( pnCkpt ) *pnCkpt = -1;
111557
111558  assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
111559  assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
111560  assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
111561  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
111562    return SQLITE_MISUSE;
111563  }
111564
111565  sqlite3_mutex_enter(db->mutex);
111566  if( zDb && zDb[0] ){
111567    iDb = sqlite3FindDbName(db, zDb);
111568  }
111569  if( iDb<0 ){
111570    rc = SQLITE_ERROR;
111571    sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
111572  }else{
111573    rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
111574    sqlite3Error(db, rc, 0);
111575  }
111576  rc = sqlite3ApiExit(db, rc);
111577  sqlite3_mutex_leave(db->mutex);
111578  return rc;
111579#endif
111580}
111581
111582
111583/*
111584** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
111585** to contains a zero-length string, all attached databases are
111586** checkpointed.
111587*/
111588SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
111589  return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
111590}
111591
111592#ifndef SQLITE_OMIT_WAL
111593/*
111594** Run a checkpoint on database iDb. This is a no-op if database iDb is
111595** not currently open in WAL mode.
111596**
111597** If a transaction is open on the database being checkpointed, this
111598** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
111599** an error occurs while running the checkpoint, an SQLite error code is
111600** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
111601**
111602** The mutex on database handle db should be held by the caller. The mutex
111603** associated with the specific b-tree being checkpointed is taken by
111604** this function while the checkpoint is running.
111605**
111606** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
111607** checkpointed. If an error is encountered it is returned immediately -
111608** no attempt is made to checkpoint any remaining databases.
111609**
111610** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
111611*/
111612SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
111613  int rc = SQLITE_OK;             /* Return code */
111614  int i;                          /* Used to iterate through attached dbs */
111615  int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
111616
111617  assert( sqlite3_mutex_held(db->mutex) );
111618  assert( !pnLog || *pnLog==-1 );
111619  assert( !pnCkpt || *pnCkpt==-1 );
111620
111621  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
111622    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
111623      rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
111624      pnLog = 0;
111625      pnCkpt = 0;
111626      if( rc==SQLITE_BUSY ){
111627        bBusy = 1;
111628        rc = SQLITE_OK;
111629      }
111630    }
111631  }
111632
111633  return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
111634}
111635#endif /* SQLITE_OMIT_WAL */
111636
111637/*
111638** This function returns true if main-memory should be used instead of
111639** a temporary file for transient pager files and statement journals.
111640** The value returned depends on the value of db->temp_store (runtime
111641** parameter) and the compile time value of SQLITE_TEMP_STORE. The
111642** following table describes the relationship between these two values
111643** and this functions return value.
111644**
111645**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
111646**   -----------------     --------------     ------------------------------
111647**   0                     any                file      (return 0)
111648**   1                     1                  file      (return 0)
111649**   1                     2                  memory    (return 1)
111650**   1                     0                  file      (return 0)
111651**   2                     1                  file      (return 0)
111652**   2                     2                  memory    (return 1)
111653**   2                     0                  memory    (return 1)
111654**   3                     any                memory    (return 1)
111655*/
111656SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
111657#if SQLITE_TEMP_STORE==1
111658  return ( db->temp_store==2 );
111659#endif
111660#if SQLITE_TEMP_STORE==2
111661  return ( db->temp_store!=1 );
111662#endif
111663#if SQLITE_TEMP_STORE==3
111664  return 1;
111665#endif
111666#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
111667  return 0;
111668#endif
111669}
111670
111671/*
111672** Return UTF-8 encoded English language explanation of the most recent
111673** error.
111674*/
111675SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
111676  const char *z;
111677  if( !db ){
111678    return sqlite3ErrStr(SQLITE_NOMEM);
111679  }
111680  if( !sqlite3SafetyCheckSickOrOk(db) ){
111681    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
111682  }
111683  sqlite3_mutex_enter(db->mutex);
111684  if( db->mallocFailed ){
111685    z = sqlite3ErrStr(SQLITE_NOMEM);
111686  }else{
111687    z = (char*)sqlite3_value_text(db->pErr);
111688    assert( !db->mallocFailed );
111689    if( z==0 ){
111690      z = sqlite3ErrStr(db->errCode);
111691    }
111692  }
111693  sqlite3_mutex_leave(db->mutex);
111694  return z;
111695}
111696
111697#ifndef SQLITE_OMIT_UTF16
111698/*
111699** Return UTF-16 encoded English language explanation of the most recent
111700** error.
111701*/
111702SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
111703  static const u16 outOfMem[] = {
111704    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
111705  };
111706  static const u16 misuse[] = {
111707    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
111708    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
111709    'c', 'a', 'l', 'l', 'e', 'd', ' ',
111710    'o', 'u', 't', ' ',
111711    'o', 'f', ' ',
111712    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
111713  };
111714
111715  const void *z;
111716  if( !db ){
111717    return (void *)outOfMem;
111718  }
111719  if( !sqlite3SafetyCheckSickOrOk(db) ){
111720    return (void *)misuse;
111721  }
111722  sqlite3_mutex_enter(db->mutex);
111723  if( db->mallocFailed ){
111724    z = (void *)outOfMem;
111725  }else{
111726    z = sqlite3_value_text16(db->pErr);
111727    if( z==0 ){
111728      sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
111729           SQLITE_UTF8, SQLITE_STATIC);
111730      z = sqlite3_value_text16(db->pErr);
111731    }
111732    /* A malloc() may have failed within the call to sqlite3_value_text16()
111733    ** above. If this is the case, then the db->mallocFailed flag needs to
111734    ** be cleared before returning. Do this directly, instead of via
111735    ** sqlite3ApiExit(), to avoid setting the database handle error message.
111736    */
111737    db->mallocFailed = 0;
111738  }
111739  sqlite3_mutex_leave(db->mutex);
111740  return z;
111741}
111742#endif /* SQLITE_OMIT_UTF16 */
111743
111744/*
111745** Return the most recent error code generated by an SQLite routine. If NULL is
111746** passed to this function, we assume a malloc() failed during sqlite3_open().
111747*/
111748SQLITE_API int sqlite3_errcode(sqlite3 *db){
111749  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
111750    return SQLITE_MISUSE_BKPT;
111751  }
111752  if( !db || db->mallocFailed ){
111753    return SQLITE_NOMEM;
111754  }
111755  return db->errCode & db->errMask;
111756}
111757SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
111758  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
111759    return SQLITE_MISUSE_BKPT;
111760  }
111761  if( !db || db->mallocFailed ){
111762    return SQLITE_NOMEM;
111763  }
111764  return db->errCode;
111765}
111766
111767/*
111768** Create a new collating function for database "db".  The name is zName
111769** and the encoding is enc.
111770*/
111771static int createCollation(
111772  sqlite3* db,
111773  const char *zName,
111774  u8 enc,
111775  u8 collType,
111776  void* pCtx,
111777  int(*xCompare)(void*,int,const void*,int,const void*),
111778  void(*xDel)(void*)
111779){
111780  CollSeq *pColl;
111781  int enc2;
111782  int nName = sqlite3Strlen30(zName);
111783
111784  assert( sqlite3_mutex_held(db->mutex) );
111785
111786  /* If SQLITE_UTF16 is specified as the encoding type, transform this
111787  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
111788  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
111789  */
111790  enc2 = enc;
111791  testcase( enc2==SQLITE_UTF16 );
111792  testcase( enc2==SQLITE_UTF16_ALIGNED );
111793  if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
111794    enc2 = SQLITE_UTF16NATIVE;
111795  }
111796  if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
111797    return SQLITE_MISUSE_BKPT;
111798  }
111799
111800  /* Check if this call is removing or replacing an existing collation
111801  ** sequence. If so, and there are active VMs, return busy. If there
111802  ** are no active VMs, invalidate any pre-compiled statements.
111803  */
111804  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
111805  if( pColl && pColl->xCmp ){
111806    if( db->activeVdbeCnt ){
111807      sqlite3Error(db, SQLITE_BUSY,
111808        "unable to delete/modify collation sequence due to active statements");
111809      return SQLITE_BUSY;
111810    }
111811    sqlite3ExpirePreparedStatements(db);
111812
111813    /* If collation sequence pColl was created directly by a call to
111814    ** sqlite3_create_collation, and not generated by synthCollSeq(),
111815    ** then any copies made by synthCollSeq() need to be invalidated.
111816    ** Also, collation destructor - CollSeq.xDel() - function may need
111817    ** to be called.
111818    */
111819    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
111820      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
111821      int j;
111822      for(j=0; j<3; j++){
111823        CollSeq *p = &aColl[j];
111824        if( p->enc==pColl->enc ){
111825          if( p->xDel ){
111826            p->xDel(p->pUser);
111827          }
111828          p->xCmp = 0;
111829        }
111830      }
111831    }
111832  }
111833
111834  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
111835  if( pColl==0 ) return SQLITE_NOMEM;
111836  pColl->xCmp = xCompare;
111837  pColl->pUser = pCtx;
111838  pColl->xDel = xDel;
111839  pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
111840  pColl->type = collType;
111841  sqlite3Error(db, SQLITE_OK, 0);
111842  return SQLITE_OK;
111843}
111844
111845
111846/*
111847** This array defines hard upper bounds on limit values.  The
111848** initializer must be kept in sync with the SQLITE_LIMIT_*
111849** #defines in sqlite3.h.
111850*/
111851static const int aHardLimit[] = {
111852  SQLITE_MAX_LENGTH,
111853  SQLITE_MAX_SQL_LENGTH,
111854  SQLITE_MAX_COLUMN,
111855  SQLITE_MAX_EXPR_DEPTH,
111856  SQLITE_MAX_COMPOUND_SELECT,
111857  SQLITE_MAX_VDBE_OP,
111858  SQLITE_MAX_FUNCTION_ARG,
111859  SQLITE_MAX_ATTACHED,
111860  SQLITE_MAX_LIKE_PATTERN_LENGTH,
111861  SQLITE_MAX_VARIABLE_NUMBER,
111862  SQLITE_MAX_TRIGGER_DEPTH,
111863};
111864
111865/*
111866** Make sure the hard limits are set to reasonable values
111867*/
111868#if SQLITE_MAX_LENGTH<100
111869# error SQLITE_MAX_LENGTH must be at least 100
111870#endif
111871#if SQLITE_MAX_SQL_LENGTH<100
111872# error SQLITE_MAX_SQL_LENGTH must be at least 100
111873#endif
111874#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
111875# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
111876#endif
111877#if SQLITE_MAX_COMPOUND_SELECT<2
111878# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
111879#endif
111880#if SQLITE_MAX_VDBE_OP<40
111881# error SQLITE_MAX_VDBE_OP must be at least 40
111882#endif
111883#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
111884# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
111885#endif
111886#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
111887# error SQLITE_MAX_ATTACHED must be between 0 and 62
111888#endif
111889#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
111890# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
111891#endif
111892#if SQLITE_MAX_COLUMN>32767
111893# error SQLITE_MAX_COLUMN must not exceed 32767
111894#endif
111895#if SQLITE_MAX_TRIGGER_DEPTH<1
111896# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
111897#endif
111898
111899
111900/*
111901** Change the value of a limit.  Report the old value.
111902** If an invalid limit index is supplied, report -1.
111903** Make no changes but still report the old value if the
111904** new limit is negative.
111905**
111906** A new lower limit does not shrink existing constructs.
111907** It merely prevents new constructs that exceed the limit
111908** from forming.
111909*/
111910SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
111911  int oldLimit;
111912
111913
111914  /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
111915  ** there is a hard upper bound set at compile-time by a C preprocessor
111916  ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
111917  ** "_MAX_".)
111918  */
111919  assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
111920  assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
111921  assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
111922  assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
111923  assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
111924  assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
111925  assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
111926  assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
111927  assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
111928                                               SQLITE_MAX_LIKE_PATTERN_LENGTH );
111929  assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
111930  assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
111931  assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
111932
111933
111934  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
111935    return -1;
111936  }
111937  oldLimit = db->aLimit[limitId];
111938  if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
111939    if( newLimit>aHardLimit[limitId] ){
111940      newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
111941    }
111942    db->aLimit[limitId] = newLimit;
111943  }
111944  return oldLimit;                     /* IMP: R-53341-35419 */
111945}
111946
111947/*
111948** This function is used to parse both URIs and non-URI filenames passed by the
111949** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
111950** URIs specified as part of ATTACH statements.
111951**
111952** The first argument to this function is the name of the VFS to use (or
111953** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
111954** query parameter. The second argument contains the URI (or non-URI filename)
111955** itself. When this function is called the *pFlags variable should contain
111956** the default flags to open the database handle with. The value stored in
111957** *pFlags may be updated before returning if the URI filename contains
111958** "cache=xxx" or "mode=xxx" query parameters.
111959**
111960** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
111961** the VFS that should be used to open the database file. *pzFile is set to
111962** point to a buffer containing the name of the file to open. It is the
111963** responsibility of the caller to eventually call sqlite3_free() to release
111964** this buffer.
111965**
111966** If an error occurs, then an SQLite error code is returned and *pzErrMsg
111967** may be set to point to a buffer containing an English language error
111968** message. It is the responsibility of the caller to eventually release
111969** this buffer by calling sqlite3_free().
111970*/
111971SQLITE_PRIVATE int sqlite3ParseUri(
111972  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
111973  const char *zUri,               /* Nul-terminated URI to parse */
111974  unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
111975  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
111976  char **pzFile,                  /* OUT: Filename component of URI */
111977  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
111978){
111979  int rc = SQLITE_OK;
111980  unsigned int flags = *pFlags;
111981  const char *zVfs = zDefaultVfs;
111982  char *zFile;
111983  char c;
111984  int nUri = sqlite3Strlen30(zUri);
111985
111986  assert( *pzErrMsg==0 );
111987
111988  if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
111989   && nUri>=5 && memcmp(zUri, "file:", 5)==0
111990  ){
111991    char *zOpt;
111992    int eState;                   /* Parser state when parsing URI */
111993    int iIn;                      /* Input character index */
111994    int iOut = 0;                 /* Output character index */
111995    int nByte = nUri+2;           /* Bytes of space to allocate */
111996
111997    /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
111998    ** method that there may be extra parameters following the file-name.  */
111999    flags |= SQLITE_OPEN_URI;
112000
112001    for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
112002    zFile = sqlite3_malloc(nByte);
112003    if( !zFile ) return SQLITE_NOMEM;
112004
112005    /* Discard the scheme and authority segments of the URI. */
112006    if( zUri[5]=='/' && zUri[6]=='/' ){
112007      iIn = 7;
112008      while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
112009
112010      if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
112011        *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
112012            iIn-7, &zUri[7]);
112013        rc = SQLITE_ERROR;
112014        goto parse_uri_out;
112015      }
112016    }else{
112017      iIn = 5;
112018    }
112019
112020    /* Copy the filename and any query parameters into the zFile buffer.
112021    ** Decode %HH escape codes along the way.
112022    **
112023    ** Within this loop, variable eState may be set to 0, 1 or 2, depending
112024    ** on the parsing context. As follows:
112025    **
112026    **   0: Parsing file-name.
112027    **   1: Parsing name section of a name=value query parameter.
112028    **   2: Parsing value section of a name=value query parameter.
112029    */
112030    eState = 0;
112031    while( (c = zUri[iIn])!=0 && c!='#' ){
112032      iIn++;
112033      if( c=='%'
112034       && sqlite3Isxdigit(zUri[iIn])
112035       && sqlite3Isxdigit(zUri[iIn+1])
112036      ){
112037        int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
112038        octet += sqlite3HexToInt(zUri[iIn++]);
112039
112040        assert( octet>=0 && octet<256 );
112041        if( octet==0 ){
112042          /* This branch is taken when "%00" appears within the URI. In this
112043          ** case we ignore all text in the remainder of the path, name or
112044          ** value currently being parsed. So ignore the current character
112045          ** and skip to the next "?", "=" or "&", as appropriate. */
112046          while( (c = zUri[iIn])!=0 && c!='#'
112047              && (eState!=0 || c!='?')
112048              && (eState!=1 || (c!='=' && c!='&'))
112049              && (eState!=2 || c!='&')
112050          ){
112051            iIn++;
112052          }
112053          continue;
112054        }
112055        c = octet;
112056      }else if( eState==1 && (c=='&' || c=='=') ){
112057        if( zFile[iOut-1]==0 ){
112058          /* An empty option name. Ignore this option altogether. */
112059          while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
112060          continue;
112061        }
112062        if( c=='&' ){
112063          zFile[iOut++] = '\0';
112064        }else{
112065          eState = 2;
112066        }
112067        c = 0;
112068      }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
112069        c = 0;
112070        eState = 1;
112071      }
112072      zFile[iOut++] = c;
112073    }
112074    if( eState==1 ) zFile[iOut++] = '\0';
112075    zFile[iOut++] = '\0';
112076    zFile[iOut++] = '\0';
112077
112078    /* Check if there were any options specified that should be interpreted
112079    ** here. Options that are interpreted here include "vfs" and those that
112080    ** correspond to flags that may be passed to the sqlite3_open_v2()
112081    ** method. */
112082    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
112083    while( zOpt[0] ){
112084      int nOpt = sqlite3Strlen30(zOpt);
112085      char *zVal = &zOpt[nOpt+1];
112086      int nVal = sqlite3Strlen30(zVal);
112087
112088      if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
112089        zVfs = zVal;
112090      }else{
112091        struct OpenMode {
112092          const char *z;
112093          int mode;
112094        } *aMode = 0;
112095        char *zModeType = 0;
112096        int mask = 0;
112097        int limit = 0;
112098
112099        if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
112100          static struct OpenMode aCacheMode[] = {
112101            { "shared",  SQLITE_OPEN_SHAREDCACHE },
112102            { "private", SQLITE_OPEN_PRIVATECACHE },
112103            { 0, 0 }
112104          };
112105
112106          mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
112107          aMode = aCacheMode;
112108          limit = mask;
112109          zModeType = "cache";
112110        }
112111        if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
112112          static struct OpenMode aOpenMode[] = {
112113            { "ro",  SQLITE_OPEN_READONLY },
112114            { "rw",  SQLITE_OPEN_READWRITE },
112115            { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
112116            { 0, 0 }
112117          };
112118
112119          mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
112120          aMode = aOpenMode;
112121          limit = mask & flags;
112122          zModeType = "access";
112123        }
112124
112125        if( aMode ){
112126          int i;
112127          int mode = 0;
112128          for(i=0; aMode[i].z; i++){
112129            const char *z = aMode[i].z;
112130            if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
112131              mode = aMode[i].mode;
112132              break;
112133            }
112134          }
112135          if( mode==0 ){
112136            *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
112137            rc = SQLITE_ERROR;
112138            goto parse_uri_out;
112139          }
112140          if( mode>limit ){
112141            *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
112142                                        zModeType, zVal);
112143            rc = SQLITE_PERM;
112144            goto parse_uri_out;
112145          }
112146          flags = (flags & ~mask) | mode;
112147        }
112148      }
112149
112150      zOpt = &zVal[nVal+1];
112151    }
112152
112153  }else{
112154    zFile = sqlite3_malloc(nUri+2);
112155    if( !zFile ) return SQLITE_NOMEM;
112156    memcpy(zFile, zUri, nUri);
112157    zFile[nUri] = '\0';
112158    zFile[nUri+1] = '\0';
112159  }
112160
112161  *ppVfs = sqlite3_vfs_find(zVfs);
112162  if( *ppVfs==0 ){
112163    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
112164    rc = SQLITE_ERROR;
112165  }
112166 parse_uri_out:
112167  if( rc!=SQLITE_OK ){
112168    sqlite3_free(zFile);
112169    zFile = 0;
112170  }
112171  *pFlags = flags;
112172  *pzFile = zFile;
112173  return rc;
112174}
112175
112176
112177/*
112178** This routine does the work of opening a database on behalf of
112179** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
112180** is UTF-8 encoded.
112181*/
112182static int openDatabase(
112183  const char *zFilename, /* Database filename UTF-8 encoded */
112184  sqlite3 **ppDb,        /* OUT: Returned database handle */
112185  unsigned int flags,    /* Operational flags */
112186  const char *zVfs       /* Name of the VFS to use */
112187){
112188  sqlite3 *db;                    /* Store allocated handle here */
112189  int rc;                         /* Return code */
112190  int isThreadsafe;               /* True for threadsafe connections */
112191  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
112192  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
112193
112194  *ppDb = 0;
112195#ifndef SQLITE_OMIT_AUTOINIT
112196  rc = sqlite3_initialize();
112197  if( rc ) return rc;
112198#endif
112199
112200  /* Only allow sensible combinations of bits in the flags argument.
112201  ** Throw an error if any non-sense combination is used.  If we
112202  ** do not block illegal combinations here, it could trigger
112203  ** assert() statements in deeper layers.  Sensible combinations
112204  ** are:
112205  **
112206  **  1:  SQLITE_OPEN_READONLY
112207  **  2:  SQLITE_OPEN_READWRITE
112208  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
112209  */
112210  assert( SQLITE_OPEN_READONLY  == 0x01 );
112211  assert( SQLITE_OPEN_READWRITE == 0x02 );
112212  assert( SQLITE_OPEN_CREATE    == 0x04 );
112213  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
112214  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
112215  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
112216  if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
112217
112218  if( sqlite3GlobalConfig.bCoreMutex==0 ){
112219    isThreadsafe = 0;
112220  }else if( flags & SQLITE_OPEN_NOMUTEX ){
112221    isThreadsafe = 0;
112222  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
112223    isThreadsafe = 1;
112224  }else{
112225    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
112226  }
112227  if( flags & SQLITE_OPEN_PRIVATECACHE ){
112228    flags &= ~SQLITE_OPEN_SHAREDCACHE;
112229  }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
112230    flags |= SQLITE_OPEN_SHAREDCACHE;
112231  }
112232
112233  /* Remove harmful bits from the flags parameter
112234  **
112235  ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
112236  ** dealt with in the previous code block.  Besides these, the only
112237  ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
112238  ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
112239  ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
112240  ** off all other flags.
112241  */
112242  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
112243               SQLITE_OPEN_EXCLUSIVE |
112244               SQLITE_OPEN_MAIN_DB |
112245               SQLITE_OPEN_TEMP_DB |
112246               SQLITE_OPEN_TRANSIENT_DB |
112247               SQLITE_OPEN_MAIN_JOURNAL |
112248               SQLITE_OPEN_TEMP_JOURNAL |
112249               SQLITE_OPEN_SUBJOURNAL |
112250               SQLITE_OPEN_MASTER_JOURNAL |
112251               SQLITE_OPEN_NOMUTEX |
112252               SQLITE_OPEN_FULLMUTEX |
112253               SQLITE_OPEN_WAL
112254             );
112255
112256  /* Allocate the sqlite data structure */
112257  db = sqlite3MallocZero( sizeof(sqlite3) );
112258  if( db==0 ) goto opendb_out;
112259  if( isThreadsafe ){
112260    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
112261    if( db->mutex==0 ){
112262      sqlite3_free(db);
112263      db = 0;
112264      goto opendb_out;
112265    }
112266  }
112267  sqlite3_mutex_enter(db->mutex);
112268  db->errMask = 0xff;
112269  db->nDb = 2;
112270  db->magic = SQLITE_MAGIC_BUSY;
112271  db->aDb = db->aDbStatic;
112272
112273  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
112274  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
112275  db->autoCommit = 1;
112276  db->nextAutovac = -1;
112277  db->nextPagesize = 0;
112278  db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
112279#if SQLITE_DEFAULT_FILE_FORMAT<4
112280                 | SQLITE_LegacyFileFmt
112281#endif
112282#ifdef SQLITE_ENABLE_LOAD_EXTENSION
112283                 | SQLITE_LoadExtension
112284#endif
112285#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
112286                 | SQLITE_RecTriggers
112287#endif
112288#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
112289                 | SQLITE_ForeignKeys
112290#endif
112291      ;
112292  sqlite3HashInit(&db->aCollSeq);
112293#ifndef SQLITE_OMIT_VIRTUALTABLE
112294  sqlite3HashInit(&db->aModule);
112295#endif
112296
112297  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
112298  ** and UTF-16, so add a version for each to avoid any unnecessary
112299  ** conversions. The only error that can occur here is a malloc() failure.
112300  */
112301  createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
112302                  binCollFunc, 0);
112303  createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
112304                  binCollFunc, 0);
112305  createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
112306                  binCollFunc, 0);
112307  createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
112308                  binCollFunc, 0);
112309  if( db->mallocFailed ){
112310    goto opendb_out;
112311  }
112312  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
112313  assert( db->pDfltColl!=0 );
112314
112315  /* Also add a UTF-8 case-insensitive collation sequence. */
112316  createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
112317                  nocaseCollatingFunc, 0);
112318
112319  /* Parse the filename/URI argument. */
112320  db->openFlags = flags;
112321  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
112322  if( rc!=SQLITE_OK ){
112323    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
112324    sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
112325    sqlite3_free(zErrMsg);
112326    goto opendb_out;
112327  }
112328
112329  /* Open the backend database driver */
112330  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
112331                        flags | SQLITE_OPEN_MAIN_DB);
112332  if( rc!=SQLITE_OK ){
112333    if( rc==SQLITE_IOERR_NOMEM ){
112334      rc = SQLITE_NOMEM;
112335    }
112336    sqlite3Error(db, rc, 0);
112337    goto opendb_out;
112338  }
112339  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
112340  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
112341
112342
112343  /* The default safety_level for the main database is 'full'; for the temp
112344  ** database it is 'NONE'. This matches the pager layer defaults.
112345  */
112346  db->aDb[0].zName = "main";
112347  db->aDb[0].safety_level = 3;
112348  db->aDb[1].zName = "temp";
112349  db->aDb[1].safety_level = 1;
112350
112351  db->magic = SQLITE_MAGIC_OPEN;
112352  if( db->mallocFailed ){
112353    goto opendb_out;
112354  }
112355
112356  /* Register all built-in functions, but do not attempt to read the
112357  ** database schema yet. This is delayed until the first time the database
112358  ** is accessed.
112359  */
112360  sqlite3Error(db, SQLITE_OK, 0);
112361  sqlite3RegisterBuiltinFunctions(db);
112362
112363  /* Load automatic extensions - extensions that have been registered
112364  ** using the sqlite3_automatic_extension() API.
112365  */
112366  sqlite3AutoLoadExtensions(db);
112367  rc = sqlite3_errcode(db);
112368  if( rc!=SQLITE_OK ){
112369    goto opendb_out;
112370  }
112371
112372#ifdef SQLITE_ENABLE_FTS1
112373  if( !db->mallocFailed ){
112374    extern int sqlite3Fts1Init(sqlite3*);
112375    rc = sqlite3Fts1Init(db);
112376  }
112377#endif
112378
112379#ifdef SQLITE_ENABLE_FTS2
112380  if( !db->mallocFailed && rc==SQLITE_OK ){
112381    extern int sqlite3Fts2Init(sqlite3*);
112382    rc = sqlite3Fts2Init(db);
112383  }
112384#endif
112385
112386#ifdef SQLITE_ENABLE_FTS3
112387  if( !db->mallocFailed && rc==SQLITE_OK ){
112388    rc = sqlite3Fts3Init(db);
112389  }
112390#endif
112391
112392#ifdef SQLITE_ENABLE_ICU
112393  if( !db->mallocFailed && rc==SQLITE_OK ){
112394    rc = sqlite3IcuInit(db);
112395  }
112396#endif
112397
112398#ifdef SQLITE_ENABLE_RTREE
112399  if( !db->mallocFailed && rc==SQLITE_OK){
112400    rc = sqlite3RtreeInit(db);
112401  }
112402#endif
112403
112404  sqlite3Error(db, rc, 0);
112405
112406  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
112407  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
112408  ** mode.  Doing nothing at all also makes NORMAL the default.
112409  */
112410#ifdef SQLITE_DEFAULT_LOCKING_MODE
112411  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
112412  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
112413                          SQLITE_DEFAULT_LOCKING_MODE);
112414#endif
112415
112416  /* Enable the lookaside-malloc subsystem */
112417  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
112418                        sqlite3GlobalConfig.nLookaside);
112419
112420  sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
112421
112422opendb_out:
112423  sqlite3_free(zOpen);
112424  if( db ){
112425    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
112426    sqlite3_mutex_leave(db->mutex);
112427  }
112428  rc = sqlite3_errcode(db);
112429  if( rc==SQLITE_NOMEM ){
112430    sqlite3_close(db);
112431    db = 0;
112432  }else if( rc!=SQLITE_OK ){
112433    db->magic = SQLITE_MAGIC_SICK;
112434  }
112435  *ppDb = db;
112436  return sqlite3ApiExit(0, rc);
112437}
112438
112439/*
112440** Open a new database handle.
112441*/
112442SQLITE_API int sqlite3_open(
112443  const char *zFilename,
112444  sqlite3 **ppDb
112445){
112446  return openDatabase(zFilename, ppDb,
112447                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
112448}
112449SQLITE_API int sqlite3_open_v2(
112450  const char *filename,   /* Database filename (UTF-8) */
112451  sqlite3 **ppDb,         /* OUT: SQLite db handle */
112452  int flags,              /* Flags */
112453  const char *zVfs        /* Name of VFS module to use */
112454){
112455  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
112456}
112457
112458#ifndef SQLITE_OMIT_UTF16
112459/*
112460** Open a new database handle.
112461*/
112462SQLITE_API int sqlite3_open16(
112463  const void *zFilename,
112464  sqlite3 **ppDb
112465){
112466  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
112467  sqlite3_value *pVal;
112468  int rc;
112469
112470  assert( zFilename );
112471  assert( ppDb );
112472  *ppDb = 0;
112473#ifndef SQLITE_OMIT_AUTOINIT
112474  rc = sqlite3_initialize();
112475  if( rc ) return rc;
112476#endif
112477  pVal = sqlite3ValueNew(0);
112478  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
112479  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
112480  if( zFilename8 ){
112481    rc = openDatabase(zFilename8, ppDb,
112482                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
112483    assert( *ppDb || rc==SQLITE_NOMEM );
112484    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
112485      ENC(*ppDb) = SQLITE_UTF16NATIVE;
112486    }
112487  }else{
112488    rc = SQLITE_NOMEM;
112489  }
112490  sqlite3ValueFree(pVal);
112491
112492  return sqlite3ApiExit(0, rc);
112493}
112494#endif /* SQLITE_OMIT_UTF16 */
112495
112496/*
112497** Register a new collation sequence with the database handle db.
112498*/
112499SQLITE_API int sqlite3_create_collation(
112500  sqlite3* db,
112501  const char *zName,
112502  int enc,
112503  void* pCtx,
112504  int(*xCompare)(void*,int,const void*,int,const void*)
112505){
112506  int rc;
112507  sqlite3_mutex_enter(db->mutex);
112508  assert( !db->mallocFailed );
112509  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
112510  rc = sqlite3ApiExit(db, rc);
112511  sqlite3_mutex_leave(db->mutex);
112512  return rc;
112513}
112514
112515/*
112516** Register a new collation sequence with the database handle db.
112517*/
112518SQLITE_API int sqlite3_create_collation_v2(
112519  sqlite3* db,
112520  const char *zName,
112521  int enc,
112522  void* pCtx,
112523  int(*xCompare)(void*,int,const void*,int,const void*),
112524  void(*xDel)(void*)
112525){
112526  int rc;
112527  sqlite3_mutex_enter(db->mutex);
112528  assert( !db->mallocFailed );
112529  rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
112530  rc = sqlite3ApiExit(db, rc);
112531  sqlite3_mutex_leave(db->mutex);
112532  return rc;
112533}
112534
112535#ifndef SQLITE_OMIT_UTF16
112536/*
112537** Register a new collation sequence with the database handle db.
112538*/
112539SQLITE_API int sqlite3_create_collation16(
112540  sqlite3* db,
112541  const void *zName,
112542  int enc,
112543  void* pCtx,
112544  int(*xCompare)(void*,int,const void*,int,const void*)
112545){
112546  int rc = SQLITE_OK;
112547  char *zName8;
112548  sqlite3_mutex_enter(db->mutex);
112549  assert( !db->mallocFailed );
112550  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
112551  if( zName8 ){
112552    rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
112553    sqlite3DbFree(db, zName8);
112554  }
112555  rc = sqlite3ApiExit(db, rc);
112556  sqlite3_mutex_leave(db->mutex);
112557  return rc;
112558}
112559#endif /* SQLITE_OMIT_UTF16 */
112560
112561/*
112562** Register a collation sequence factory callback with the database handle
112563** db. Replace any previously installed collation sequence factory.
112564*/
112565SQLITE_API int sqlite3_collation_needed(
112566  sqlite3 *db,
112567  void *pCollNeededArg,
112568  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
112569){
112570  sqlite3_mutex_enter(db->mutex);
112571  db->xCollNeeded = xCollNeeded;
112572  db->xCollNeeded16 = 0;
112573  db->pCollNeededArg = pCollNeededArg;
112574  sqlite3_mutex_leave(db->mutex);
112575  return SQLITE_OK;
112576}
112577
112578#ifndef SQLITE_OMIT_UTF16
112579/*
112580** Register a collation sequence factory callback with the database handle
112581** db. Replace any previously installed collation sequence factory.
112582*/
112583SQLITE_API int sqlite3_collation_needed16(
112584  sqlite3 *db,
112585  void *pCollNeededArg,
112586  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
112587){
112588  sqlite3_mutex_enter(db->mutex);
112589  db->xCollNeeded = 0;
112590  db->xCollNeeded16 = xCollNeeded16;
112591  db->pCollNeededArg = pCollNeededArg;
112592  sqlite3_mutex_leave(db->mutex);
112593  return SQLITE_OK;
112594}
112595#endif /* SQLITE_OMIT_UTF16 */
112596
112597#ifndef SQLITE_OMIT_DEPRECATED
112598/*
112599** This function is now an anachronism. It used to be used to recover from a
112600** malloc() failure, but SQLite now does this automatically.
112601*/
112602SQLITE_API int sqlite3_global_recover(void){
112603  return SQLITE_OK;
112604}
112605#endif
112606
112607/*
112608** Test to see whether or not the database connection is in autocommit
112609** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
112610** by default.  Autocommit is disabled by a BEGIN statement and reenabled
112611** by the next COMMIT or ROLLBACK.
112612**
112613******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
112614*/
112615SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
112616  return db->autoCommit;
112617}
112618
112619/*
112620** The following routines are subtitutes for constants SQLITE_CORRUPT,
112621** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
112622** constants.  They server two purposes:
112623**
112624**   1.  Serve as a convenient place to set a breakpoint in a debugger
112625**       to detect when version error conditions occurs.
112626**
112627**   2.  Invoke sqlite3_log() to provide the source code location where
112628**       a low-level error is first detected.
112629*/
112630SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
112631  testcase( sqlite3GlobalConfig.xLog!=0 );
112632  sqlite3_log(SQLITE_CORRUPT,
112633              "database corruption at line %d of [%.10s]",
112634              lineno, 20+sqlite3_sourceid());
112635  return SQLITE_CORRUPT;
112636}
112637SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
112638  testcase( sqlite3GlobalConfig.xLog!=0 );
112639  sqlite3_log(SQLITE_MISUSE,
112640              "misuse at line %d of [%.10s]",
112641              lineno, 20+sqlite3_sourceid());
112642  return SQLITE_MISUSE;
112643}
112644SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
112645  testcase( sqlite3GlobalConfig.xLog!=0 );
112646  sqlite3_log(SQLITE_CANTOPEN,
112647              "cannot open file at line %d of [%.10s]",
112648              lineno, 20+sqlite3_sourceid());
112649  return SQLITE_CANTOPEN;
112650}
112651
112652
112653#ifndef SQLITE_OMIT_DEPRECATED
112654/*
112655** This is a convenience routine that makes sure that all thread-specific
112656** data for this thread has been deallocated.
112657**
112658** SQLite no longer uses thread-specific data so this routine is now a
112659** no-op.  It is retained for historical compatibility.
112660*/
112661SQLITE_API void sqlite3_thread_cleanup(void){
112662}
112663#endif
112664
112665/*
112666** Return meta information about a specific column of a database table.
112667** See comment in sqlite3.h (sqlite.h.in) for details.
112668*/
112669#ifdef SQLITE_ENABLE_COLUMN_METADATA
112670SQLITE_API int sqlite3_table_column_metadata(
112671  sqlite3 *db,                /* Connection handle */
112672  const char *zDbName,        /* Database name or NULL */
112673  const char *zTableName,     /* Table name */
112674  const char *zColumnName,    /* Column name */
112675  char const **pzDataType,    /* OUTPUT: Declared data type */
112676  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
112677  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
112678  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
112679  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
112680){
112681  int rc;
112682  char *zErrMsg = 0;
112683  Table *pTab = 0;
112684  Column *pCol = 0;
112685  int iCol;
112686
112687  char const *zDataType = 0;
112688  char const *zCollSeq = 0;
112689  int notnull = 0;
112690  int primarykey = 0;
112691  int autoinc = 0;
112692
112693  /* Ensure the database schema has been loaded */
112694  sqlite3_mutex_enter(db->mutex);
112695  sqlite3BtreeEnterAll(db);
112696  rc = sqlite3Init(db, &zErrMsg);
112697  if( SQLITE_OK!=rc ){
112698    goto error_out;
112699  }
112700
112701  /* Locate the table in question */
112702  pTab = sqlite3FindTable(db, zTableName, zDbName);
112703  if( !pTab || pTab->pSelect ){
112704    pTab = 0;
112705    goto error_out;
112706  }
112707
112708  /* Find the column for which info is requested */
112709  if( sqlite3IsRowid(zColumnName) ){
112710    iCol = pTab->iPKey;
112711    if( iCol>=0 ){
112712      pCol = &pTab->aCol[iCol];
112713    }
112714  }else{
112715    for(iCol=0; iCol<pTab->nCol; iCol++){
112716      pCol = &pTab->aCol[iCol];
112717      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
112718        break;
112719      }
112720    }
112721    if( iCol==pTab->nCol ){
112722      pTab = 0;
112723      goto error_out;
112724    }
112725  }
112726
112727  /* The following block stores the meta information that will be returned
112728  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
112729  ** and autoinc. At this point there are two possibilities:
112730  **
112731  **     1. The specified column name was rowid", "oid" or "_rowid_"
112732  **        and there is no explicitly declared IPK column.
112733  **
112734  **     2. The table is not a view and the column name identified an
112735  **        explicitly declared column. Copy meta information from *pCol.
112736  */
112737  if( pCol ){
112738    zDataType = pCol->zType;
112739    zCollSeq = pCol->zColl;
112740    notnull = pCol->notNull!=0;
112741    primarykey  = pCol->isPrimKey!=0;
112742    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
112743  }else{
112744    zDataType = "INTEGER";
112745    primarykey = 1;
112746  }
112747  if( !zCollSeq ){
112748    zCollSeq = "BINARY";
112749  }
112750
112751error_out:
112752  sqlite3BtreeLeaveAll(db);
112753
112754  /* Whether the function call succeeded or failed, set the output parameters
112755  ** to whatever their local counterparts contain. If an error did occur,
112756  ** this has the effect of zeroing all output parameters.
112757  */
112758  if( pzDataType ) *pzDataType = zDataType;
112759  if( pzCollSeq ) *pzCollSeq = zCollSeq;
112760  if( pNotNull ) *pNotNull = notnull;
112761  if( pPrimaryKey ) *pPrimaryKey = primarykey;
112762  if( pAutoinc ) *pAutoinc = autoinc;
112763
112764  if( SQLITE_OK==rc && !pTab ){
112765    sqlite3DbFree(db, zErrMsg);
112766    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
112767        zColumnName);
112768    rc = SQLITE_ERROR;
112769  }
112770  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
112771  sqlite3DbFree(db, zErrMsg);
112772  rc = sqlite3ApiExit(db, rc);
112773  sqlite3_mutex_leave(db->mutex);
112774  return rc;
112775}
112776#endif
112777
112778/*
112779** Sleep for a little while.  Return the amount of time slept.
112780*/
112781SQLITE_API int sqlite3_sleep(int ms){
112782  sqlite3_vfs *pVfs;
112783  int rc;
112784  pVfs = sqlite3_vfs_find(0);
112785  if( pVfs==0 ) return 0;
112786
112787  /* This function works in milliseconds, but the underlying OsSleep()
112788  ** API uses microseconds. Hence the 1000's.
112789  */
112790  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
112791  return rc;
112792}
112793
112794/*
112795** Enable or disable the extended result codes.
112796*/
112797SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
112798  sqlite3_mutex_enter(db->mutex);
112799  db->errMask = onoff ? 0xffffffff : 0xff;
112800  sqlite3_mutex_leave(db->mutex);
112801  return SQLITE_OK;
112802}
112803
112804/*
112805** Invoke the xFileControl method on a particular database.
112806*/
112807SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
112808  int rc = SQLITE_ERROR;
112809  int iDb;
112810  sqlite3_mutex_enter(db->mutex);
112811  if( zDbName==0 ){
112812    iDb = 0;
112813  }else{
112814    for(iDb=0; iDb<db->nDb; iDb++){
112815      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
112816    }
112817  }
112818  if( iDb<db->nDb ){
112819    Btree *pBtree = db->aDb[iDb].pBt;
112820    if( pBtree ){
112821      Pager *pPager;
112822      sqlite3_file *fd;
112823      sqlite3BtreeEnter(pBtree);
112824      pPager = sqlite3BtreePager(pBtree);
112825      assert( pPager!=0 );
112826      fd = sqlite3PagerFile(pPager);
112827      assert( fd!=0 );
112828      if( op==SQLITE_FCNTL_FILE_POINTER ){
112829        *(sqlite3_file**)pArg = fd;
112830        rc = SQLITE_OK;
112831      }else if( fd->pMethods ){
112832        rc = sqlite3OsFileControl(fd, op, pArg);
112833      }else{
112834        rc = SQLITE_NOTFOUND;
112835      }
112836      sqlite3BtreeLeave(pBtree);
112837    }
112838  }
112839  sqlite3_mutex_leave(db->mutex);
112840  return rc;
112841}
112842
112843/*
112844** Interface to the testing logic.
112845*/
112846SQLITE_API int sqlite3_test_control(int op, ...){
112847  int rc = 0;
112848#ifndef SQLITE_OMIT_BUILTIN_TEST
112849  va_list ap;
112850  va_start(ap, op);
112851  switch( op ){
112852
112853    /*
112854    ** Save the current state of the PRNG.
112855    */
112856    case SQLITE_TESTCTRL_PRNG_SAVE: {
112857      sqlite3PrngSaveState();
112858      break;
112859    }
112860
112861    /*
112862    ** Restore the state of the PRNG to the last state saved using
112863    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
112864    ** this verb acts like PRNG_RESET.
112865    */
112866    case SQLITE_TESTCTRL_PRNG_RESTORE: {
112867      sqlite3PrngRestoreState();
112868      break;
112869    }
112870
112871    /*
112872    ** Reset the PRNG back to its uninitialized state.  The next call
112873    ** to sqlite3_randomness() will reseed the PRNG using a single call
112874    ** to the xRandomness method of the default VFS.
112875    */
112876    case SQLITE_TESTCTRL_PRNG_RESET: {
112877      sqlite3PrngResetState();
112878      break;
112879    }
112880
112881    /*
112882    **  sqlite3_test_control(BITVEC_TEST, size, program)
112883    **
112884    ** Run a test against a Bitvec object of size.  The program argument
112885    ** is an array of integers that defines the test.  Return -1 on a
112886    ** memory allocation error, 0 on success, or non-zero for an error.
112887    ** See the sqlite3BitvecBuiltinTest() for additional information.
112888    */
112889    case SQLITE_TESTCTRL_BITVEC_TEST: {
112890      int sz = va_arg(ap, int);
112891      int *aProg = va_arg(ap, int*);
112892      rc = sqlite3BitvecBuiltinTest(sz, aProg);
112893      break;
112894    }
112895
112896    /*
112897    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
112898    **
112899    ** Register hooks to call to indicate which malloc() failures
112900    ** are benign.
112901    */
112902    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
112903      typedef void (*void_function)(void);
112904      void_function xBenignBegin;
112905      void_function xBenignEnd;
112906      xBenignBegin = va_arg(ap, void_function);
112907      xBenignEnd = va_arg(ap, void_function);
112908      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
112909      break;
112910    }
112911
112912    /*
112913    **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
112914    **
112915    ** Set the PENDING byte to the value in the argument, if X>0.
112916    ** Make no changes if X==0.  Return the value of the pending byte
112917    ** as it existing before this routine was called.
112918    **
112919    ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
112920    ** an incompatible database file format.  Changing the PENDING byte
112921    ** while any database connection is open results in undefined and
112922    ** dileterious behavior.
112923    */
112924    case SQLITE_TESTCTRL_PENDING_BYTE: {
112925      rc = PENDING_BYTE;
112926#ifndef SQLITE_OMIT_WSD
112927      {
112928        unsigned int newVal = va_arg(ap, unsigned int);
112929        if( newVal ) sqlite3PendingByte = newVal;
112930      }
112931#endif
112932      break;
112933    }
112934
112935    /*
112936    **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
112937    **
112938    ** This action provides a run-time test to see whether or not
112939    ** assert() was enabled at compile-time.  If X is true and assert()
112940    ** is enabled, then the return value is true.  If X is true and
112941    ** assert() is disabled, then the return value is zero.  If X is
112942    ** false and assert() is enabled, then the assertion fires and the
112943    ** process aborts.  If X is false and assert() is disabled, then the
112944    ** return value is zero.
112945    */
112946    case SQLITE_TESTCTRL_ASSERT: {
112947      volatile int x = 0;
112948      assert( (x = va_arg(ap,int))!=0 );
112949      rc = x;
112950      break;
112951    }
112952
112953
112954    /*
112955    **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
112956    **
112957    ** This action provides a run-time test to see how the ALWAYS and
112958    ** NEVER macros were defined at compile-time.
112959    **
112960    ** The return value is ALWAYS(X).
112961    **
112962    ** The recommended test is X==2.  If the return value is 2, that means
112963    ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
112964    ** default setting.  If the return value is 1, then ALWAYS() is either
112965    ** hard-coded to true or else it asserts if its argument is false.
112966    ** The first behavior (hard-coded to true) is the case if
112967    ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
112968    ** behavior (assert if the argument to ALWAYS() is false) is the case if
112969    ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
112970    **
112971    ** The run-time test procedure might look something like this:
112972    **
112973    **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
112974    **      // ALWAYS() and NEVER() are no-op pass-through macros
112975    **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
112976    **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
112977    **    }else{
112978    **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
112979    **    }
112980    */
112981    case SQLITE_TESTCTRL_ALWAYS: {
112982      int x = va_arg(ap,int);
112983      rc = ALWAYS(x);
112984      break;
112985    }
112986
112987    /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
112988    **
112989    ** Set the nReserve size to N for the main database on the database
112990    ** connection db.
112991    */
112992    case SQLITE_TESTCTRL_RESERVE: {
112993      sqlite3 *db = va_arg(ap, sqlite3*);
112994      int x = va_arg(ap,int);
112995      sqlite3_mutex_enter(db->mutex);
112996      sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
112997      sqlite3_mutex_leave(db->mutex);
112998      break;
112999    }
113000
113001    /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
113002    **
113003    ** Enable or disable various optimizations for testing purposes.  The
113004    ** argument N is a bitmask of optimizations to be disabled.  For normal
113005    ** operation N should be 0.  The idea is that a test program (like the
113006    ** SQL Logic Test or SLT test module) can run the same SQL multiple times
113007    ** with various optimizations disabled to verify that the same answer
113008    ** is obtained in every case.
113009    */
113010    case SQLITE_TESTCTRL_OPTIMIZATIONS: {
113011      sqlite3 *db = va_arg(ap, sqlite3*);
113012      int x = va_arg(ap,int);
113013      db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
113014      break;
113015    }
113016
113017#ifdef SQLITE_N_KEYWORD
113018    /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
113019    **
113020    ** If zWord is a keyword recognized by the parser, then return the
113021    ** number of keywords.  Or if zWord is not a keyword, return 0.
113022    **
113023    ** This test feature is only available in the amalgamation since
113024    ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
113025    ** is built using separate source files.
113026    */
113027    case SQLITE_TESTCTRL_ISKEYWORD: {
113028      const char *zWord = va_arg(ap, const char*);
113029      int n = sqlite3Strlen30(zWord);
113030      rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
113031      break;
113032    }
113033#endif
113034
113035    /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
113036    **
113037    ** Return the size of a pcache header in bytes.
113038    */
113039    case SQLITE_TESTCTRL_PGHDRSZ: {
113040      rc = sizeof(PgHdr);
113041      break;
113042    }
113043
113044    /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
113045    **
113046    ** Pass pFree into sqlite3ScratchFree().
113047    ** If sz>0 then allocate a scratch buffer into pNew.
113048    */
113049    case SQLITE_TESTCTRL_SCRATCHMALLOC: {
113050      void *pFree, **ppNew;
113051      int sz;
113052      sz = va_arg(ap, int);
113053      ppNew = va_arg(ap, void**);
113054      pFree = va_arg(ap, void*);
113055      if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
113056      sqlite3ScratchFree(pFree);
113057      break;
113058    }
113059
113060    /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
113061    **
113062    ** If parameter onoff is non-zero, configure the wrappers so that all
113063    ** subsequent calls to localtime() and variants fail. If onoff is zero,
113064    ** undo this setting.
113065    */
113066    case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
113067      sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
113068      break;
113069    }
113070
113071  }
113072  va_end(ap);
113073#endif /* SQLITE_OMIT_BUILTIN_TEST */
113074  return rc;
113075}
113076
113077/*
113078** This is a utility routine, useful to VFS implementations, that checks
113079** to see if a database file was a URI that contained a specific query
113080** parameter, and if so obtains the value of the query parameter.
113081**
113082** The zFilename argument is the filename pointer passed into the xOpen()
113083** method of a VFS implementation.  The zParam argument is the name of the
113084** query parameter we seek.  This routine returns the value of the zParam
113085** parameter if it exists.  If the parameter does not exist, this routine
113086** returns a NULL pointer.
113087*/
113088SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
113089  zFilename += sqlite3Strlen30(zFilename) + 1;
113090  while( zFilename[0] ){
113091    int x = strcmp(zFilename, zParam);
113092    zFilename += sqlite3Strlen30(zFilename) + 1;
113093    if( x==0 ) return zFilename;
113094    zFilename += sqlite3Strlen30(zFilename) + 1;
113095  }
113096  return 0;
113097}
113098
113099/************** End of main.c ************************************************/
113100/************** Begin file notify.c ******************************************/
113101/*
113102** 2009 March 3
113103**
113104** The author disclaims copyright to this source code.  In place of
113105** a legal notice, here is a blessing:
113106**
113107**    May you do good and not evil.
113108**    May you find forgiveness for yourself and forgive others.
113109**    May you share freely, never taking more than you give.
113110**
113111*************************************************************************
113112**
113113** This file contains the implementation of the sqlite3_unlock_notify()
113114** API method and its associated functionality.
113115*/
113116
113117/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
113118#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
113119
113120/*
113121** Public interfaces:
113122**
113123**   sqlite3ConnectionBlocked()
113124**   sqlite3ConnectionUnlocked()
113125**   sqlite3ConnectionClosed()
113126**   sqlite3_unlock_notify()
113127*/
113128
113129#define assertMutexHeld() \
113130  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
113131
113132/*
113133** Head of a linked list of all sqlite3 objects created by this process
113134** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
113135** is not NULL. This variable may only accessed while the STATIC_MASTER
113136** mutex is held.
113137*/
113138static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
113139
113140#ifndef NDEBUG
113141/*
113142** This function is a complex assert() that verifies the following
113143** properties of the blocked connections list:
113144**
113145**   1) Each entry in the list has a non-NULL value for either
113146**      pUnlockConnection or pBlockingConnection, or both.
113147**
113148**   2) All entries in the list that share a common value for
113149**      xUnlockNotify are grouped together.
113150**
113151**   3) If the argument db is not NULL, then none of the entries in the
113152**      blocked connections list have pUnlockConnection or pBlockingConnection
113153**      set to db. This is used when closing connection db.
113154*/
113155static void checkListProperties(sqlite3 *db){
113156  sqlite3 *p;
113157  for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
113158    int seen = 0;
113159    sqlite3 *p2;
113160
113161    /* Verify property (1) */
113162    assert( p->pUnlockConnection || p->pBlockingConnection );
113163
113164    /* Verify property (2) */
113165    for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
113166      if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
113167      assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
113168      assert( db==0 || p->pUnlockConnection!=db );
113169      assert( db==0 || p->pBlockingConnection!=db );
113170    }
113171  }
113172}
113173#else
113174# define checkListProperties(x)
113175#endif
113176
113177/*
113178** Remove connection db from the blocked connections list. If connection
113179** db is not currently a part of the list, this function is a no-op.
113180*/
113181static void removeFromBlockedList(sqlite3 *db){
113182  sqlite3 **pp;
113183  assertMutexHeld();
113184  for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
113185    if( *pp==db ){
113186      *pp = (*pp)->pNextBlocked;
113187      break;
113188    }
113189  }
113190}
113191
113192/*
113193** Add connection db to the blocked connections list. It is assumed
113194** that it is not already a part of the list.
113195*/
113196static void addToBlockedList(sqlite3 *db){
113197  sqlite3 **pp;
113198  assertMutexHeld();
113199  for(
113200    pp=&sqlite3BlockedList;
113201    *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
113202    pp=&(*pp)->pNextBlocked
113203  );
113204  db->pNextBlocked = *pp;
113205  *pp = db;
113206}
113207
113208/*
113209** Obtain the STATIC_MASTER mutex.
113210*/
113211static void enterMutex(void){
113212  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
113213  checkListProperties(0);
113214}
113215
113216/*
113217** Release the STATIC_MASTER mutex.
113218*/
113219static void leaveMutex(void){
113220  assertMutexHeld();
113221  checkListProperties(0);
113222  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
113223}
113224
113225/*
113226** Register an unlock-notify callback.
113227**
113228** This is called after connection "db" has attempted some operation
113229** but has received an SQLITE_LOCKED error because another connection
113230** (call it pOther) in the same process was busy using the same shared
113231** cache.  pOther is found by looking at db->pBlockingConnection.
113232**
113233** If there is no blocking connection, the callback is invoked immediately,
113234** before this routine returns.
113235**
113236** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
113237** a deadlock.
113238**
113239** Otherwise, make arrangements to invoke xNotify when pOther drops
113240** its locks.
113241**
113242** Each call to this routine overrides any prior callbacks registered
113243** on the same "db".  If xNotify==0 then any prior callbacks are immediately
113244** cancelled.
113245*/
113246SQLITE_API int sqlite3_unlock_notify(
113247  sqlite3 *db,
113248  void (*xNotify)(void **, int),
113249  void *pArg
113250){
113251  int rc = SQLITE_OK;
113252
113253  sqlite3_mutex_enter(db->mutex);
113254  enterMutex();
113255
113256  if( xNotify==0 ){
113257    removeFromBlockedList(db);
113258    db->pBlockingConnection = 0;
113259    db->pUnlockConnection = 0;
113260    db->xUnlockNotify = 0;
113261    db->pUnlockArg = 0;
113262  }else if( 0==db->pBlockingConnection ){
113263    /* The blocking transaction has been concluded. Or there never was a
113264    ** blocking transaction. In either case, invoke the notify callback
113265    ** immediately.
113266    */
113267    xNotify(&pArg, 1);
113268  }else{
113269    sqlite3 *p;
113270
113271    for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
113272    if( p ){
113273      rc = SQLITE_LOCKED;              /* Deadlock detected. */
113274    }else{
113275      db->pUnlockConnection = db->pBlockingConnection;
113276      db->xUnlockNotify = xNotify;
113277      db->pUnlockArg = pArg;
113278      removeFromBlockedList(db);
113279      addToBlockedList(db);
113280    }
113281  }
113282
113283  leaveMutex();
113284  assert( !db->mallocFailed );
113285  sqlite3Error(db, rc, (rc?"database is deadlocked":0));
113286  sqlite3_mutex_leave(db->mutex);
113287  return rc;
113288}
113289
113290/*
113291** This function is called while stepping or preparing a statement
113292** associated with connection db. The operation will return SQLITE_LOCKED
113293** to the user because it requires a lock that will not be available
113294** until connection pBlocker concludes its current transaction.
113295*/
113296SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
113297  enterMutex();
113298  if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
113299    addToBlockedList(db);
113300  }
113301  db->pBlockingConnection = pBlocker;
113302  leaveMutex();
113303}
113304
113305/*
113306** This function is called when
113307** the transaction opened by database db has just finished. Locks held
113308** by database connection db have been released.
113309**
113310** This function loops through each entry in the blocked connections
113311** list and does the following:
113312**
113313**   1) If the sqlite3.pBlockingConnection member of a list entry is
113314**      set to db, then set pBlockingConnection=0.
113315**
113316**   2) If the sqlite3.pUnlockConnection member of a list entry is
113317**      set to db, then invoke the configured unlock-notify callback and
113318**      set pUnlockConnection=0.
113319**
113320**   3) If the two steps above mean that pBlockingConnection==0 and
113321**      pUnlockConnection==0, remove the entry from the blocked connections
113322**      list.
113323*/
113324SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
113325  void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
113326  int nArg = 0;                            /* Number of entries in aArg[] */
113327  sqlite3 **pp;                            /* Iterator variable */
113328  void **aArg;               /* Arguments to the unlock callback */
113329  void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
113330  void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
113331
113332  aArg = aStatic;
113333  enterMutex();         /* Enter STATIC_MASTER mutex */
113334
113335  /* This loop runs once for each entry in the blocked-connections list. */
113336  for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
113337    sqlite3 *p = *pp;
113338
113339    /* Step 1. */
113340    if( p->pBlockingConnection==db ){
113341      p->pBlockingConnection = 0;
113342    }
113343
113344    /* Step 2. */
113345    if( p->pUnlockConnection==db ){
113346      assert( p->xUnlockNotify );
113347      if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
113348        xUnlockNotify(aArg, nArg);
113349        nArg = 0;
113350      }
113351
113352      sqlite3BeginBenignMalloc();
113353      assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
113354      assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
113355      if( (!aDyn && nArg==(int)ArraySize(aStatic))
113356       || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
113357      ){
113358        /* The aArg[] array needs to grow. */
113359        void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
113360        if( pNew ){
113361          memcpy(pNew, aArg, nArg*sizeof(void *));
113362          sqlite3_free(aDyn);
113363          aDyn = aArg = pNew;
113364        }else{
113365          /* This occurs when the array of context pointers that need to
113366          ** be passed to the unlock-notify callback is larger than the
113367          ** aStatic[] array allocated on the stack and the attempt to
113368          ** allocate a larger array from the heap has failed.
113369          **
113370          ** This is a difficult situation to handle. Returning an error
113371          ** code to the caller is insufficient, as even if an error code
113372          ** is returned the transaction on connection db will still be
113373          ** closed and the unlock-notify callbacks on blocked connections
113374          ** will go unissued. This might cause the application to wait
113375          ** indefinitely for an unlock-notify callback that will never
113376          ** arrive.
113377          **
113378          ** Instead, invoke the unlock-notify callback with the context
113379          ** array already accumulated. We can then clear the array and
113380          ** begin accumulating any further context pointers without
113381          ** requiring any dynamic allocation. This is sub-optimal because
113382          ** it means that instead of one callback with a large array of
113383          ** context pointers the application will receive two or more
113384          ** callbacks with smaller arrays of context pointers, which will
113385          ** reduce the applications ability to prioritize multiple
113386          ** connections. But it is the best that can be done under the
113387          ** circumstances.
113388          */
113389          xUnlockNotify(aArg, nArg);
113390          nArg = 0;
113391        }
113392      }
113393      sqlite3EndBenignMalloc();
113394
113395      aArg[nArg++] = p->pUnlockArg;
113396      xUnlockNotify = p->xUnlockNotify;
113397      p->pUnlockConnection = 0;
113398      p->xUnlockNotify = 0;
113399      p->pUnlockArg = 0;
113400    }
113401
113402    /* Step 3. */
113403    if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
113404      /* Remove connection p from the blocked connections list. */
113405      *pp = p->pNextBlocked;
113406      p->pNextBlocked = 0;
113407    }else{
113408      pp = &p->pNextBlocked;
113409    }
113410  }
113411
113412  if( nArg!=0 ){
113413    xUnlockNotify(aArg, nArg);
113414  }
113415  sqlite3_free(aDyn);
113416  leaveMutex();         /* Leave STATIC_MASTER mutex */
113417}
113418
113419/*
113420** This is called when the database connection passed as an argument is
113421** being closed. The connection is removed from the blocked list.
113422*/
113423SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
113424  sqlite3ConnectionUnlocked(db);
113425  enterMutex();
113426  removeFromBlockedList(db);
113427  checkListProperties(db);
113428  leaveMutex();
113429}
113430#endif
113431
113432/************** End of notify.c **********************************************/
113433/************** Begin file fts3.c ********************************************/
113434/*
113435** 2006 Oct 10
113436**
113437** The author disclaims copyright to this source code.  In place of
113438** a legal notice, here is a blessing:
113439**
113440**    May you do good and not evil.
113441**    May you find forgiveness for yourself and forgive others.
113442**    May you share freely, never taking more than you give.
113443**
113444******************************************************************************
113445**
113446** This is an SQLite module implementing full-text search.
113447*/
113448
113449/*
113450** The code in this file is only compiled if:
113451**
113452**     * The FTS3 module is being built as an extension
113453**       (in which case SQLITE_CORE is not defined), or
113454**
113455**     * The FTS3 module is being built into the core of
113456**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
113457*/
113458
113459/* The full-text index is stored in a series of b+tree (-like)
113460** structures called segments which map terms to doclists.  The
113461** structures are like b+trees in layout, but are constructed from the
113462** bottom up in optimal fashion and are not updatable.  Since trees
113463** are built from the bottom up, things will be described from the
113464** bottom up.
113465**
113466**
113467**** Varints ****
113468** The basic unit of encoding is a variable-length integer called a
113469** varint.  We encode variable-length integers in little-endian order
113470** using seven bits * per byte as follows:
113471**
113472** KEY:
113473**         A = 0xxxxxxx    7 bits of data and one flag bit
113474**         B = 1xxxxxxx    7 bits of data and one flag bit
113475**
113476**  7 bits - A
113477** 14 bits - BA
113478** 21 bits - BBA
113479** and so on.
113480**
113481** This is similar in concept to how sqlite encodes "varints" but
113482** the encoding is not the same.  SQLite varints are big-endian
113483** are are limited to 9 bytes in length whereas FTS3 varints are
113484** little-endian and can be up to 10 bytes in length (in theory).
113485**
113486** Example encodings:
113487**
113488**     1:    0x01
113489**   127:    0x7f
113490**   128:    0x81 0x00
113491**
113492**
113493**** Document lists ****
113494** A doclist (document list) holds a docid-sorted list of hits for a
113495** given term.  Doclists hold docids and associated token positions.
113496** A docid is the unique integer identifier for a single document.
113497** A position is the index of a word within the document.  The first
113498** word of the document has a position of 0.
113499**
113500** FTS3 used to optionally store character offsets using a compile-time
113501** option.  But that functionality is no longer supported.
113502**
113503** A doclist is stored like this:
113504**
113505** array {
113506**   varint docid;
113507**   array {                (position list for column 0)
113508**     varint position;     (2 more than the delta from previous position)
113509**   }
113510**   array {
113511**     varint POS_COLUMN;   (marks start of position list for new column)
113512**     varint column;       (index of new column)
113513**     array {
113514**       varint position;   (2 more than the delta from previous position)
113515**     }
113516**   }
113517**   varint POS_END;        (marks end of positions for this document.
113518** }
113519**
113520** Here, array { X } means zero or more occurrences of X, adjacent in
113521** memory.  A "position" is an index of a token in the token stream
113522** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
113523** in the same logical place as the position element, and act as sentinals
113524** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
113525** The positions numbers are not stored literally but rather as two more
113526** than the difference from the prior position, or the just the position plus
113527** 2 for the first position.  Example:
113528**
113529**   label:       A B C D E  F  G H   I  J K
113530**   value:     123 5 9 1 1 14 35 0 234 72 0
113531**
113532** The 123 value is the first docid.  For column zero in this document
113533** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
113534** at D signals the start of a new column; the 1 at E indicates that the
113535** new column is column number 1.  There are two positions at 12 and 45
113536** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
113537** 234 at I is the next docid.  It has one position 72 (72-2) and then
113538** terminates with the 0 at K.
113539**
113540** A "position-list" is the list of positions for multiple columns for
113541** a single docid.  A "column-list" is the set of positions for a single
113542** column.  Hence, a position-list consists of one or more column-lists,
113543** a document record consists of a docid followed by a position-list and
113544** a doclist consists of one or more document records.
113545**
113546** A bare doclist omits the position information, becoming an
113547** array of varint-encoded docids.
113548**
113549**** Segment leaf nodes ****
113550** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
113551** nodes are written using LeafWriter, and read using LeafReader (to
113552** iterate through a single leaf node's data) and LeavesReader (to
113553** iterate through a segment's entire leaf layer).  Leaf nodes have
113554** the format:
113555**
113556** varint iHeight;             (height from leaf level, always 0)
113557** varint nTerm;               (length of first term)
113558** char pTerm[nTerm];          (content of first term)
113559** varint nDoclist;            (length of term's associated doclist)
113560** char pDoclist[nDoclist];    (content of doclist)
113561** array {
113562**                             (further terms are delta-encoded)
113563**   varint nPrefix;           (length of prefix shared with previous term)
113564**   varint nSuffix;           (length of unshared suffix)
113565**   char pTermSuffix[nSuffix];(unshared suffix of next term)
113566**   varint nDoclist;          (length of term's associated doclist)
113567**   char pDoclist[nDoclist];  (content of doclist)
113568** }
113569**
113570** Here, array { X } means zero or more occurrences of X, adjacent in
113571** memory.
113572**
113573** Leaf nodes are broken into blocks which are stored contiguously in
113574** the %_segments table in sorted order.  This means that when the end
113575** of a node is reached, the next term is in the node with the next
113576** greater node id.
113577**
113578** New data is spilled to a new leaf node when the current node
113579** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
113580** larger than STANDALONE_MIN (default 1024) is placed in a standalone
113581** node (a leaf node with a single term and doclist).  The goal of
113582** these settings is to pack together groups of small doclists while
113583** making it efficient to directly access large doclists.  The
113584** assumption is that large doclists represent terms which are more
113585** likely to be query targets.
113586**
113587** TODO(shess) It may be useful for blocking decisions to be more
113588** dynamic.  For instance, it may make more sense to have a 2.5k leaf
113589** node rather than splitting into 2k and .5k nodes.  My intuition is
113590** that this might extend through 2x or 4x the pagesize.
113591**
113592**
113593**** Segment interior nodes ****
113594** Segment interior nodes store blockids for subtree nodes and terms
113595** to describe what data is stored by the each subtree.  Interior
113596** nodes are written using InteriorWriter, and read using
113597** InteriorReader.  InteriorWriters are created as needed when
113598** SegmentWriter creates new leaf nodes, or when an interior node
113599** itself grows too big and must be split.  The format of interior
113600** nodes:
113601**
113602** varint iHeight;           (height from leaf level, always >0)
113603** varint iBlockid;          (block id of node's leftmost subtree)
113604** optional {
113605**   varint nTerm;           (length of first term)
113606**   char pTerm[nTerm];      (content of first term)
113607**   array {
113608**                                (further terms are delta-encoded)
113609**     varint nPrefix;            (length of shared prefix with previous term)
113610**     varint nSuffix;            (length of unshared suffix)
113611**     char pTermSuffix[nSuffix]; (unshared suffix of next term)
113612**   }
113613** }
113614**
113615** Here, optional { X } means an optional element, while array { X }
113616** means zero or more occurrences of X, adjacent in memory.
113617**
113618** An interior node encodes n terms separating n+1 subtrees.  The
113619** subtree blocks are contiguous, so only the first subtree's blockid
113620** is encoded.  The subtree at iBlockid will contain all terms less
113621** than the first term encoded (or all terms if no term is encoded).
113622** Otherwise, for terms greater than or equal to pTerm[i] but less
113623** than pTerm[i+1], the subtree for that term will be rooted at
113624** iBlockid+i.  Interior nodes only store enough term data to
113625** distinguish adjacent children (if the rightmost term of the left
113626** child is "something", and the leftmost term of the right child is
113627** "wicked", only "w" is stored).
113628**
113629** New data is spilled to a new interior node at the same height when
113630** the current node exceeds INTERIOR_MAX bytes (default 2048).
113631** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
113632** interior nodes and making the tree too skinny.  The interior nodes
113633** at a given height are naturally tracked by interior nodes at
113634** height+1, and so on.
113635**
113636**
113637**** Segment directory ****
113638** The segment directory in table %_segdir stores meta-information for
113639** merging and deleting segments, and also the root node of the
113640** segment's tree.
113641**
113642** The root node is the top node of the segment's tree after encoding
113643** the entire segment, restricted to ROOT_MAX bytes (default 1024).
113644** This could be either a leaf node or an interior node.  If the top
113645** node requires more than ROOT_MAX bytes, it is flushed to %_segments
113646** and a new root interior node is generated (which should always fit
113647** within ROOT_MAX because it only needs space for 2 varints, the
113648** height and the blockid of the previous root).
113649**
113650** The meta-information in the segment directory is:
113651**   level               - segment level (see below)
113652**   idx                 - index within level
113653**                       - (level,idx uniquely identify a segment)
113654**   start_block         - first leaf node
113655**   leaves_end_block    - last leaf node
113656**   end_block           - last block (including interior nodes)
113657**   root                - contents of root node
113658**
113659** If the root node is a leaf node, then start_block,
113660** leaves_end_block, and end_block are all 0.
113661**
113662**
113663**** Segment merging ****
113664** To amortize update costs, segments are grouped into levels and
113665** merged in batches.  Each increase in level represents exponentially
113666** more documents.
113667**
113668** New documents (actually, document updates) are tokenized and
113669** written individually (using LeafWriter) to a level 0 segment, with
113670** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
113671** level 0 segments are merged into a single level 1 segment.  Level 1
113672** is populated like level 0, and eventually MERGE_COUNT level 1
113673** segments are merged to a single level 2 segment (representing
113674** MERGE_COUNT^2 updates), and so on.
113675**
113676** A segment merge traverses all segments at a given level in
113677** parallel, performing a straightforward sorted merge.  Since segment
113678** leaf nodes are written in to the %_segments table in order, this
113679** merge traverses the underlying sqlite disk structures efficiently.
113680** After the merge, all segment blocks from the merged level are
113681** deleted.
113682**
113683** MERGE_COUNT controls how often we merge segments.  16 seems to be
113684** somewhat of a sweet spot for insertion performance.  32 and 64 show
113685** very similar performance numbers to 16 on insertion, though they're
113686** a tiny bit slower (perhaps due to more overhead in merge-time
113687** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
113688** 16, 2 about 66% slower than 16.
113689**
113690** At query time, high MERGE_COUNT increases the number of segments
113691** which need to be scanned and merged.  For instance, with 100k docs
113692** inserted:
113693**
113694**    MERGE_COUNT   segments
113695**       16           25
113696**        8           12
113697**        4           10
113698**        2            6
113699**
113700** This appears to have only a moderate impact on queries for very
113701** frequent terms (which are somewhat dominated by segment merge
113702** costs), and infrequent and non-existent terms still seem to be fast
113703** even with many segments.
113704**
113705** TODO(shess) That said, it would be nice to have a better query-side
113706** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
113707** optimizations to things like doclist merging will swing the sweet
113708** spot around.
113709**
113710**
113711**
113712**** Handling of deletions and updates ****
113713** Since we're using a segmented structure, with no docid-oriented
113714** index into the term index, we clearly cannot simply update the term
113715** index when a document is deleted or updated.  For deletions, we
113716** write an empty doclist (varint(docid) varint(POS_END)), for updates
113717** we simply write the new doclist.  Segment merges overwrite older
113718** data for a particular docid with newer data, so deletes or updates
113719** will eventually overtake the earlier data and knock it out.  The
113720** query logic likewise merges doclists so that newer data knocks out
113721** older data.
113722**
113723** TODO(shess) Provide a VACUUM type operation to clear out all
113724** deletions and duplications.  This would basically be a forced merge
113725** into a single segment.
113726*/
113727
113728/************** Include fts3Int.h in the middle of fts3.c ********************/
113729/************** Begin file fts3Int.h *****************************************/
113730/*
113731** 2009 Nov 12
113732**
113733** The author disclaims copyright to this source code.  In place of
113734** a legal notice, here is a blessing:
113735**
113736**    May you do good and not evil.
113737**    May you find forgiveness for yourself and forgive others.
113738**    May you share freely, never taking more than you give.
113739**
113740******************************************************************************
113741**
113742*/
113743#ifndef _FTSINT_H
113744#define _FTSINT_H
113745
113746#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
113747# define NDEBUG 1
113748#endif
113749
113750/*
113751** FTS4 is really an extension for FTS3.  It is enabled using the
113752** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
113753** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
113754*/
113755#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
113756# define SQLITE_ENABLE_FTS3
113757#endif
113758
113759#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
113760
113761/* If not building as part of the core, include sqlite3ext.h. */
113762#ifndef SQLITE_CORE
113763SQLITE_API extern const sqlite3_api_routines *sqlite3_api;
113764#endif
113765
113766/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
113767/************** Begin file fts3_tokenizer.h **********************************/
113768/*
113769** 2006 July 10
113770**
113771** The author disclaims copyright to this source code.
113772**
113773*************************************************************************
113774** Defines the interface to tokenizers used by fulltext-search.  There
113775** are three basic components:
113776**
113777** sqlite3_tokenizer_module is a singleton defining the tokenizer
113778** interface functions.  This is essentially the class structure for
113779** tokenizers.
113780**
113781** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
113782** including customization information defined at creation time.
113783**
113784** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
113785** tokens from a particular input.
113786*/
113787#ifndef _FTS3_TOKENIZER_H_
113788#define _FTS3_TOKENIZER_H_
113789
113790/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
113791** If tokenizers are to be allowed to call sqlite3_*() functions, then
113792** we will need a way to register the API consistently.
113793*/
113794
113795/*
113796** Structures used by the tokenizer interface. When a new tokenizer
113797** implementation is registered, the caller provides a pointer to
113798** an sqlite3_tokenizer_module containing pointers to the callback
113799** functions that make up an implementation.
113800**
113801** When an fts3 table is created, it passes any arguments passed to
113802** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
113803** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
113804** implementation. The xCreate() function in turn returns an
113805** sqlite3_tokenizer structure representing the specific tokenizer to
113806** be used for the fts3 table (customized by the tokenizer clause arguments).
113807**
113808** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
113809** method is called. It returns an sqlite3_tokenizer_cursor object
113810** that may be used to tokenize a specific input buffer based on
113811** the tokenization rules supplied by a specific sqlite3_tokenizer
113812** object.
113813*/
113814typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
113815typedef struct sqlite3_tokenizer sqlite3_tokenizer;
113816typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
113817
113818struct sqlite3_tokenizer_module {
113819
113820  /*
113821  ** Structure version. Should always be set to 0.
113822  */
113823  int iVersion;
113824
113825  /*
113826  ** Create a new tokenizer. The values in the argv[] array are the
113827  ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
113828  ** TABLE statement that created the fts3 table. For example, if
113829  ** the following SQL is executed:
113830  **
113831  **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
113832  **
113833  ** then argc is set to 2, and the argv[] array contains pointers
113834  ** to the strings "arg1" and "arg2".
113835  **
113836  ** This method should return either SQLITE_OK (0), or an SQLite error
113837  ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
113838  ** to point at the newly created tokenizer structure. The generic
113839  ** sqlite3_tokenizer.pModule variable should not be initialised by
113840  ** this callback. The caller will do so.
113841  */
113842  int (*xCreate)(
113843    int argc,                           /* Size of argv array */
113844    const char *const*argv,             /* Tokenizer argument strings */
113845    sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
113846  );
113847
113848  /*
113849  ** Destroy an existing tokenizer. The fts3 module calls this method
113850  ** exactly once for each successful call to xCreate().
113851  */
113852  int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
113853
113854  /*
113855  ** Create a tokenizer cursor to tokenize an input buffer. The caller
113856  ** is responsible for ensuring that the input buffer remains valid
113857  ** until the cursor is closed (using the xClose() method).
113858  */
113859  int (*xOpen)(
113860    sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
113861    const char *pInput, int nBytes,      /* Input buffer */
113862    sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
113863  );
113864
113865  /*
113866  ** Destroy an existing tokenizer cursor. The fts3 module calls this
113867  ** method exactly once for each successful call to xOpen().
113868  */
113869  int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
113870
113871  /*
113872  ** Retrieve the next token from the tokenizer cursor pCursor. This
113873  ** method should either return SQLITE_OK and set the values of the
113874  ** "OUT" variables identified below, or SQLITE_DONE to indicate that
113875  ** the end of the buffer has been reached, or an SQLite error code.
113876  **
113877  ** *ppToken should be set to point at a buffer containing the
113878  ** normalized version of the token (i.e. after any case-folding and/or
113879  ** stemming has been performed). *pnBytes should be set to the length
113880  ** of this buffer in bytes. The input text that generated the token is
113881  ** identified by the byte offsets returned in *piStartOffset and
113882  ** *piEndOffset. *piStartOffset should be set to the index of the first
113883  ** byte of the token in the input buffer. *piEndOffset should be set
113884  ** to the index of the first byte just past the end of the token in
113885  ** the input buffer.
113886  **
113887  ** The buffer *ppToken is set to point at is managed by the tokenizer
113888  ** implementation. It is only required to be valid until the next call
113889  ** to xNext() or xClose().
113890  */
113891  /* TODO(shess) current implementation requires pInput to be
113892  ** nul-terminated.  This should either be fixed, or pInput/nBytes
113893  ** should be converted to zInput.
113894  */
113895  int (*xNext)(
113896    sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
113897    const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
113898    int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
113899    int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
113900    int *piPosition      /* OUT: Number of tokens returned before this one */
113901  );
113902};
113903
113904struct sqlite3_tokenizer {
113905  const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
113906  /* Tokenizer implementations will typically add additional fields */
113907};
113908
113909struct sqlite3_tokenizer_cursor {
113910  sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
113911  /* Tokenizer implementations will typically add additional fields */
113912};
113913
113914int fts3_global_term_cnt(int iTerm, int iCol);
113915int fts3_term_cnt(int iTerm, int iCol);
113916
113917
113918#endif /* _FTS3_TOKENIZER_H_ */
113919
113920/************** End of fts3_tokenizer.h **************************************/
113921/************** Continuing where we left off in fts3Int.h ********************/
113922/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
113923/************** Begin file fts3_hash.h ***************************************/
113924/*
113925** 2001 September 22
113926**
113927** The author disclaims copyright to this source code.  In place of
113928** a legal notice, here is a blessing:
113929**
113930**    May you do good and not evil.
113931**    May you find forgiveness for yourself and forgive others.
113932**    May you share freely, never taking more than you give.
113933**
113934*************************************************************************
113935** This is the header file for the generic hash-table implemenation
113936** used in SQLite.  We've modified it slightly to serve as a standalone
113937** hash table implementation for the full-text indexing module.
113938**
113939*/
113940#ifndef _FTS3_HASH_H_
113941#define _FTS3_HASH_H_
113942
113943/* Forward declarations of structures. */
113944typedef struct Fts3Hash Fts3Hash;
113945typedef struct Fts3HashElem Fts3HashElem;
113946
113947/* A complete hash table is an instance of the following structure.
113948** The internals of this structure are intended to be opaque -- client
113949** code should not attempt to access or modify the fields of this structure
113950** directly.  Change this structure only by using the routines below.
113951** However, many of the "procedures" and "functions" for modifying and
113952** accessing this structure are really macros, so we can't really make
113953** this structure opaque.
113954*/
113955struct Fts3Hash {
113956  char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
113957  char copyKey;           /* True if copy of key made on insert */
113958  int count;              /* Number of entries in this table */
113959  Fts3HashElem *first;    /* The first element of the array */
113960  int htsize;             /* Number of buckets in the hash table */
113961  struct _fts3ht {        /* the hash table */
113962    int count;               /* Number of entries with this hash */
113963    Fts3HashElem *chain;     /* Pointer to first entry with this hash */
113964  } *ht;
113965};
113966
113967/* Each element in the hash table is an instance of the following
113968** structure.  All elements are stored on a single doubly-linked list.
113969**
113970** Again, this structure is intended to be opaque, but it can't really
113971** be opaque because it is used by macros.
113972*/
113973struct Fts3HashElem {
113974  Fts3HashElem *next, *prev; /* Next and previous elements in the table */
113975  void *data;                /* Data associated with this element */
113976  void *pKey; int nKey;      /* Key associated with this element */
113977};
113978
113979/*
113980** There are 2 different modes of operation for a hash table:
113981**
113982**   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
113983**                           (including the null-terminator, if any).  Case
113984**                           is respected in comparisons.
113985**
113986**   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
113987**                           memcmp() is used to compare keys.
113988**
113989** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
113990*/
113991#define FTS3_HASH_STRING    1
113992#define FTS3_HASH_BINARY    2
113993
113994/*
113995** Access routines.  To delete, insert a NULL pointer.
113996*/
113997SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
113998SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
113999SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
114000SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
114001SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
114002
114003/*
114004** Shorthand for the functions above
114005*/
114006#define fts3HashInit     sqlite3Fts3HashInit
114007#define fts3HashInsert   sqlite3Fts3HashInsert
114008#define fts3HashFind     sqlite3Fts3HashFind
114009#define fts3HashClear    sqlite3Fts3HashClear
114010#define fts3HashFindElem sqlite3Fts3HashFindElem
114011
114012/*
114013** Macros for looping over all elements of a hash table.  The idiom is
114014** like this:
114015**
114016**   Fts3Hash h;
114017**   Fts3HashElem *p;
114018**   ...
114019**   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
114020**     SomeStructure *pData = fts3HashData(p);
114021**     // do something with pData
114022**   }
114023*/
114024#define fts3HashFirst(H)  ((H)->first)
114025#define fts3HashNext(E)   ((E)->next)
114026#define fts3HashData(E)   ((E)->data)
114027#define fts3HashKey(E)    ((E)->pKey)
114028#define fts3HashKeysize(E) ((E)->nKey)
114029
114030/*
114031** Number of entries in a hash table
114032*/
114033#define fts3HashCount(H)  ((H)->count)
114034
114035#endif /* _FTS3_HASH_H_ */
114036
114037/************** End of fts3_hash.h *******************************************/
114038/************** Continuing where we left off in fts3Int.h ********************/
114039
114040/*
114041** This constant controls how often segments are merged. Once there are
114042** FTS3_MERGE_COUNT segments of level N, they are merged into a single
114043** segment of level N+1.
114044*/
114045#define FTS3_MERGE_COUNT 16
114046
114047/*
114048** This is the maximum amount of data (in bytes) to store in the
114049** Fts3Table.pendingTerms hash table. Normally, the hash table is
114050** populated as documents are inserted/updated/deleted in a transaction
114051** and used to create a new segment when the transaction is committed.
114052** However if this limit is reached midway through a transaction, a new
114053** segment is created and the hash table cleared immediately.
114054*/
114055#define FTS3_MAX_PENDING_DATA (1*1024*1024)
114056
114057/*
114058** Macro to return the number of elements in an array. SQLite has a
114059** similar macro called ArraySize(). Use a different name to avoid
114060** a collision when building an amalgamation with built-in FTS3.
114061*/
114062#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
114063
114064
114065#ifndef MIN
114066# define MIN(x,y) ((x)<(y)?(x):(y))
114067#endif
114068
114069/*
114070** Maximum length of a varint encoded integer. The varint format is different
114071** from that used by SQLite, so the maximum length is 10, not 9.
114072*/
114073#define FTS3_VARINT_MAX 10
114074
114075/*
114076** FTS4 virtual tables may maintain multiple indexes - one index of all terms
114077** in the document set and zero or more prefix indexes. All indexes are stored
114078** as one or more b+-trees in the %_segments and %_segdir tables.
114079**
114080** It is possible to determine which index a b+-tree belongs to based on the
114081** value stored in the "%_segdir.level" column. Given this value L, the index
114082** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
114083** level values between 0 and 1023 (inclusive) belong to index 0, all levels
114084** between 1024 and 2047 to index 1, and so on.
114085**
114086** It is considered impossible for an index to use more than 1024 levels. In
114087** theory though this may happen, but only after at least
114088** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
114089*/
114090#define FTS3_SEGDIR_MAXLEVEL      1024
114091#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
114092
114093/*
114094** The testcase() macro is only used by the amalgamation.  If undefined,
114095** make it a no-op.
114096*/
114097#ifndef testcase
114098# define testcase(X)
114099#endif
114100
114101/*
114102** Terminator values for position-lists and column-lists.
114103*/
114104#define POS_COLUMN  (1)     /* Column-list terminator */
114105#define POS_END     (0)     /* Position-list terminator */
114106
114107/*
114108** This section provides definitions to allow the
114109** FTS3 extension to be compiled outside of the
114110** amalgamation.
114111*/
114112#ifndef SQLITE_AMALGAMATION
114113/*
114114** Macros indicating that conditional expressions are always true or
114115** false.
114116*/
114117#ifdef SQLITE_COVERAGE_TEST
114118# define ALWAYS(x) (1)
114119# define NEVER(X)  (0)
114120#else
114121# define ALWAYS(x) (x)
114122# define NEVER(X)  (x)
114123#endif
114124
114125/*
114126** Internal types used by SQLite.
114127*/
114128typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
114129typedef short int i16;            /* 2-byte (or larger) signed integer */
114130typedef unsigned int u32;         /* 4-byte unsigned integer */
114131typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
114132
114133/*
114134** Macro used to suppress compiler warnings for unused parameters.
114135*/
114136#define UNUSED_PARAMETER(x) (void)(x)
114137
114138/*
114139** Activate assert() only if SQLITE_TEST is enabled.
114140*/
114141#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
114142# define NDEBUG 1
114143#endif
114144
114145/*
114146** The TESTONLY macro is used to enclose variable declarations or
114147** other bits of code that are needed to support the arguments
114148** within testcase() and assert() macros.
114149*/
114150#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
114151# define TESTONLY(X)  X
114152#else
114153# define TESTONLY(X)
114154#endif
114155
114156#endif /* SQLITE_AMALGAMATION */
114157
114158typedef struct Fts3Table Fts3Table;
114159typedef struct Fts3Cursor Fts3Cursor;
114160typedef struct Fts3Expr Fts3Expr;
114161typedef struct Fts3Phrase Fts3Phrase;
114162typedef struct Fts3PhraseToken Fts3PhraseToken;
114163
114164typedef struct Fts3Doclist Fts3Doclist;
114165typedef struct Fts3SegFilter Fts3SegFilter;
114166typedef struct Fts3DeferredToken Fts3DeferredToken;
114167typedef struct Fts3SegReader Fts3SegReader;
114168typedef struct Fts3MultiSegReader Fts3MultiSegReader;
114169
114170/*
114171** A connection to a fulltext index is an instance of the following
114172** structure. The xCreate and xConnect methods create an instance
114173** of this structure and xDestroy and xDisconnect free that instance.
114174** All other methods receive a pointer to the structure as one of their
114175** arguments.
114176*/
114177struct Fts3Table {
114178  sqlite3_vtab base;              /* Base class used by SQLite core */
114179  sqlite3 *db;                    /* The database connection */
114180  const char *zDb;                /* logical database name */
114181  const char *zName;              /* virtual table name */
114182  int nColumn;                    /* number of named columns in virtual table */
114183  char **azColumn;                /* column names.  malloced */
114184  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
114185
114186  /* Precompiled statements used by the implementation. Each of these
114187  ** statements is run and reset within a single virtual table API call.
114188  */
114189  sqlite3_stmt *aStmt[27];
114190
114191  char *zReadExprlist;
114192  char *zWriteExprlist;
114193
114194  int nNodeSize;                  /* Soft limit for node size */
114195  u8 bHasStat;                    /* True if %_stat table exists */
114196  u8 bHasDocsize;                 /* True if %_docsize table exists */
114197  u8 bDescIdx;                    /* True if doclists are in reverse order */
114198  int nPgsz;                      /* Page size for host database */
114199  char *zSegmentsTbl;             /* Name of %_segments table */
114200  sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
114201
114202  /* TODO: Fix the first paragraph of this comment.
114203  **
114204  ** The following hash table is used to buffer pending index updates during
114205  ** transactions. Variable nPendingData estimates the memory size of the
114206  ** pending data, including hash table overhead, but not malloc overhead.
114207  ** When nPendingData exceeds nMaxPendingData, the buffer is flushed
114208  ** automatically. Variable iPrevDocid is the docid of the most recently
114209  ** inserted record.
114210  **
114211  ** A single FTS4 table may have multiple full-text indexes. For each index
114212  ** there is an entry in the aIndex[] array. Index 0 is an index of all the
114213  ** terms that appear in the document set. Each subsequent index in aIndex[]
114214  ** is an index of prefixes of a specific length.
114215  */
114216  int nIndex;                     /* Size of aIndex[] */
114217  struct Fts3Index {
114218    int nPrefix;                  /* Prefix length (0 for main terms index) */
114219    Fts3Hash hPending;            /* Pending terms table for this index */
114220  } *aIndex;
114221  int nMaxPendingData;            /* Max pending data before flush to disk */
114222  int nPendingData;               /* Current bytes of pending data */
114223  sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
114224
114225#if defined(SQLITE_DEBUG)
114226  /* State variables used for validating that the transaction control
114227  ** methods of the virtual table are called at appropriate times.  These
114228  ** values do not contribution to the FTS computation; they are used for
114229  ** verifying the SQLite core.
114230  */
114231  int inTransaction;     /* True after xBegin but before xCommit/xRollback */
114232  int mxSavepoint;       /* Largest valid xSavepoint integer */
114233#endif
114234};
114235
114236/*
114237** When the core wants to read from the virtual table, it creates a
114238** virtual table cursor (an instance of the following structure) using
114239** the xOpen method. Cursors are destroyed using the xClose method.
114240*/
114241struct Fts3Cursor {
114242  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
114243  i16 eSearch;                    /* Search strategy (see below) */
114244  u8 isEof;                       /* True if at End Of Results */
114245  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
114246  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
114247  Fts3Expr *pExpr;                /* Parsed MATCH query string */
114248  int nPhrase;                    /* Number of matchable phrases in query */
114249  Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
114250  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
114251  char *pNextId;                  /* Pointer into the body of aDoclist */
114252  char *aDoclist;                 /* List of docids for full-text queries */
114253  int nDoclist;                   /* Size of buffer at aDoclist */
114254  u8 bDesc;                       /* True to sort in descending order */
114255  int eEvalmode;                  /* An FTS3_EVAL_XX constant */
114256  int nRowAvg;                    /* Average size of database rows, in pages */
114257  sqlite3_int64 nDoc;             /* Documents in table */
114258
114259  int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
114260  u32 *aMatchinfo;                /* Information about most recent match */
114261  int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
114262  char *zMatchinfo;               /* Matchinfo specification */
114263};
114264
114265#define FTS3_EVAL_FILTER    0
114266#define FTS3_EVAL_NEXT      1
114267#define FTS3_EVAL_MATCHINFO 2
114268
114269/*
114270** The Fts3Cursor.eSearch member is always set to one of the following.
114271** Actualy, Fts3Cursor.eSearch can be greater than or equal to
114272** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
114273** of the column to be searched.  For example, in
114274**
114275**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
114276**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
114277**
114278** Because the LHS of the MATCH operator is 2nd column "b",
114279** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
114280** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
114281** indicating that all columns should be searched,
114282** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
114283*/
114284#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
114285#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
114286#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
114287
114288
114289struct Fts3Doclist {
114290  char *aAll;                    /* Array containing doclist (or NULL) */
114291  int nAll;                      /* Size of a[] in bytes */
114292  char *pNextDocid;              /* Pointer to next docid */
114293
114294  sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
114295  int bFreeList;                 /* True if pList should be sqlite3_free()d */
114296  char *pList;                   /* Pointer to position list following iDocid */
114297  int nList;                     /* Length of position list */
114298};
114299
114300/*
114301** A "phrase" is a sequence of one or more tokens that must match in
114302** sequence.  A single token is the base case and the most common case.
114303** For a sequence of tokens contained in double-quotes (i.e. "one two three")
114304** nToken will be the number of tokens in the string.
114305*/
114306struct Fts3PhraseToken {
114307  char *z;                        /* Text of the token */
114308  int n;                          /* Number of bytes in buffer z */
114309  int isPrefix;                   /* True if token ends with a "*" character */
114310
114311  /* Variables above this point are populated when the expression is
114312  ** parsed (by code in fts3_expr.c). Below this point the variables are
114313  ** used when evaluating the expression. */
114314  Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
114315  Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
114316};
114317
114318struct Fts3Phrase {
114319  /* Cache of doclist for this phrase. */
114320  Fts3Doclist doclist;
114321  int bIncr;                 /* True if doclist is loaded incrementally */
114322  int iDoclistToken;
114323
114324  /* Variables below this point are populated by fts3_expr.c when parsing
114325  ** a MATCH expression. Everything above is part of the evaluation phase.
114326  */
114327  int nToken;                /* Number of tokens in the phrase */
114328  int iColumn;               /* Index of column this phrase must match */
114329  Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
114330};
114331
114332/*
114333** A tree of these objects forms the RHS of a MATCH operator.
114334**
114335** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
114336** points to a malloced buffer, size nDoclist bytes, containing the results
114337** of this phrase query in FTS3 doclist format. As usual, the initial
114338** "Length" field found in doclists stored on disk is omitted from this
114339** buffer.
114340**
114341** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
114342** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
114343** where nCol is the number of columns in the queried FTS table. The array
114344** is populated as follows:
114345**
114346**   aMI[iCol*3 + 0] = Undefined
114347**   aMI[iCol*3 + 1] = Number of occurrences
114348**   aMI[iCol*3 + 2] = Number of rows containing at least one instance
114349**
114350** The aMI array is allocated using sqlite3_malloc(). It should be freed
114351** when the expression node is.
114352*/
114353struct Fts3Expr {
114354  int eType;                 /* One of the FTSQUERY_XXX values defined below */
114355  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
114356  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
114357  Fts3Expr *pLeft;           /* Left operand */
114358  Fts3Expr *pRight;          /* Right operand */
114359  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
114360
114361  /* The following are used by the fts3_eval.c module. */
114362  sqlite3_int64 iDocid;      /* Current docid */
114363  u8 bEof;                   /* True this expression is at EOF already */
114364  u8 bStart;                 /* True if iDocid is valid */
114365  u8 bDeferred;              /* True if this expression is entirely deferred */
114366
114367  u32 *aMI;
114368};
114369
114370/*
114371** Candidate values for Fts3Query.eType. Note that the order of the first
114372** four values is in order of precedence when parsing expressions. For
114373** example, the following:
114374**
114375**   "a OR b AND c NOT d NEAR e"
114376**
114377** is equivalent to:
114378**
114379**   "a OR (b AND (c NOT (d NEAR e)))"
114380*/
114381#define FTSQUERY_NEAR   1
114382#define FTSQUERY_NOT    2
114383#define FTSQUERY_AND    3
114384#define FTSQUERY_OR     4
114385#define FTSQUERY_PHRASE 5
114386
114387
114388/* fts3_write.c */
114389SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
114390SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
114391SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
114392SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
114393SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
114394  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
114395SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
114396  Fts3Table*,int,const char*,int,int,Fts3SegReader**);
114397SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
114398SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, sqlite3_stmt **);
114399SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
114400SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
114401
114402SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
114403SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
114404
114405SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
114406SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
114407SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
114408SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
114409SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
114410
114411/* Special values interpreted by sqlite3SegReaderCursor() */
114412#define FTS3_SEGCURSOR_PENDING        -1
114413#define FTS3_SEGCURSOR_ALL            -2
114414
114415SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
114416SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
114417SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
114418
114419SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
114420    Fts3Table *, int, int, const char *, int, int, int, Fts3MultiSegReader *);
114421
114422/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
114423#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
114424#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
114425#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
114426#define FTS3_SEGMENT_PREFIX        0x00000008
114427#define FTS3_SEGMENT_SCAN          0x00000010
114428
114429/* Type passed as 4th argument to SegmentReaderIterate() */
114430struct Fts3SegFilter {
114431  const char *zTerm;
114432  int nTerm;
114433  int iCol;
114434  int flags;
114435};
114436
114437struct Fts3MultiSegReader {
114438  /* Used internally by sqlite3Fts3SegReaderXXX() calls */
114439  Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
114440  int nSegment;                   /* Size of apSegment array */
114441  int nAdvance;                   /* How many seg-readers to advance */
114442  Fts3SegFilter *pFilter;         /* Pointer to filter object */
114443  char *aBuffer;                  /* Buffer to merge doclists in */
114444  int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
114445
114446  int iColFilter;                 /* If >=0, filter for this column */
114447  int bRestart;
114448
114449  /* Used by fts3.c only. */
114450  int nCost;                      /* Cost of running iterator */
114451  int bLookup;                    /* True if a lookup of a single entry. */
114452
114453  /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
114454  char *zTerm;                    /* Pointer to term buffer */
114455  int nTerm;                      /* Size of zTerm in bytes */
114456  char *aDoclist;                 /* Pointer to doclist buffer */
114457  int nDoclist;                   /* Size of aDoclist[] in bytes */
114458};
114459
114460/* fts3.c */
114461SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
114462SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
114463SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
114464SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
114465SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
114466SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
114467
114468SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
114469
114470/* fts3_tokenizer.c */
114471SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
114472SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
114473SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
114474    sqlite3_tokenizer **, char **
114475);
114476SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
114477
114478/* fts3_snippet.c */
114479SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
114480SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
114481  const char *, const char *, int, int
114482);
114483SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
114484
114485/* fts3_expr.c */
114486SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *,
114487  char **, int, int, const char *, int, Fts3Expr **
114488);
114489SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
114490#ifdef SQLITE_TEST
114491SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
114492SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
114493#endif
114494
114495/* fts3_aux.c */
114496SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
114497
114498SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
114499
114500SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
114501    Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
114502SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
114503    Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
114504SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol);
114505SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
114506SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
114507
114508SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
114509
114510#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
114511#endif /* _FTSINT_H */
114512
114513/************** End of fts3Int.h *********************************************/
114514/************** Continuing where we left off in fts3.c ***********************/
114515#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
114516
114517#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
114518# define SQLITE_CORE 1
114519#endif
114520
114521/* #include <assert.h> */
114522/* #include <stdlib.h> */
114523/* #include <stddef.h> */
114524/* #include <stdio.h> */
114525/* #include <string.h> */
114526/* #include <stdarg.h> */
114527
114528#ifndef SQLITE_CORE
114529  SQLITE_EXTENSION_INIT1
114530#endif
114531
114532static int fts3EvalNext(Fts3Cursor *pCsr);
114533static int fts3EvalStart(Fts3Cursor *pCsr);
114534static int fts3TermSegReaderCursor(
114535    Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
114536
114537/*
114538** Write a 64-bit variable-length integer to memory starting at p[0].
114539** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
114540** The number of bytes written is returned.
114541*/
114542SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
114543  unsigned char *q = (unsigned char *) p;
114544  sqlite_uint64 vu = v;
114545  do{
114546    *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
114547    vu >>= 7;
114548  }while( vu!=0 );
114549  q[-1] &= 0x7f;  /* turn off high bit in final byte */
114550  assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
114551  return (int) (q - (unsigned char *)p);
114552}
114553
114554/*
114555** Read a 64-bit variable-length integer from memory starting at p[0].
114556** Return the number of bytes read, or 0 on error.
114557** The value is stored in *v.
114558*/
114559SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
114560  const unsigned char *q = (const unsigned char *) p;
114561  sqlite_uint64 x = 0, y = 1;
114562  while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
114563    x += y * (*q++ & 0x7f);
114564    y <<= 7;
114565  }
114566  x += y * (*q++);
114567  *v = (sqlite_int64) x;
114568  return (int) (q - (unsigned char *)p);
114569}
114570
114571/*
114572** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
114573** 32-bit integer before it is returned.
114574*/
114575SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
114576 sqlite_int64 i;
114577 int ret = sqlite3Fts3GetVarint(p, &i);
114578 *pi = (int) i;
114579 return ret;
114580}
114581
114582/*
114583** Return the number of bytes required to encode v as a varint
114584*/
114585SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
114586  int i = 0;
114587  do{
114588    i++;
114589    v >>= 7;
114590  }while( v!=0 );
114591  return i;
114592}
114593
114594/*
114595** Convert an SQL-style quoted string into a normal string by removing
114596** the quote characters.  The conversion is done in-place.  If the
114597** input does not begin with a quote character, then this routine
114598** is a no-op.
114599**
114600** Examples:
114601**
114602**     "abc"   becomes   abc
114603**     'xyz'   becomes   xyz
114604**     [pqr]   becomes   pqr
114605**     `mno`   becomes   mno
114606**
114607*/
114608SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
114609  char quote;                     /* Quote character (if any ) */
114610
114611  quote = z[0];
114612  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
114613    int iIn = 1;                  /* Index of next byte to read from input */
114614    int iOut = 0;                 /* Index of next byte to write to output */
114615
114616    /* If the first byte was a '[', then the close-quote character is a ']' */
114617    if( quote=='[' ) quote = ']';
114618
114619    while( ALWAYS(z[iIn]) ){
114620      if( z[iIn]==quote ){
114621        if( z[iIn+1]!=quote ) break;
114622        z[iOut++] = quote;
114623        iIn += 2;
114624      }else{
114625        z[iOut++] = z[iIn++];
114626      }
114627    }
114628    z[iOut] = '\0';
114629  }
114630}
114631
114632/*
114633** Read a single varint from the doclist at *pp and advance *pp to point
114634** to the first byte past the end of the varint.  Add the value of the varint
114635** to *pVal.
114636*/
114637static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
114638  sqlite3_int64 iVal;
114639  *pp += sqlite3Fts3GetVarint(*pp, &iVal);
114640  *pVal += iVal;
114641}
114642
114643/*
114644** When this function is called, *pp points to the first byte following a
114645** varint that is part of a doclist (or position-list, or any other list
114646** of varints). This function moves *pp to point to the start of that varint,
114647** and sets *pVal by the varint value.
114648**
114649** Argument pStart points to the first byte of the doclist that the
114650** varint is part of.
114651*/
114652static void fts3GetReverseVarint(
114653  char **pp,
114654  char *pStart,
114655  sqlite3_int64 *pVal
114656){
114657  sqlite3_int64 iVal;
114658  char *p = *pp;
114659
114660  /* Pointer p now points at the first byte past the varint we are
114661  ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
114662  ** clear on character p[-1]. */
114663  for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
114664  p++;
114665  *pp = p;
114666
114667  sqlite3Fts3GetVarint(p, &iVal);
114668  *pVal = iVal;
114669}
114670
114671/*
114672** The xDisconnect() virtual table method.
114673*/
114674static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
114675  Fts3Table *p = (Fts3Table *)pVtab;
114676  int i;
114677
114678  assert( p->nPendingData==0 );
114679  assert( p->pSegments==0 );
114680
114681  /* Free any prepared statements held */
114682  for(i=0; i<SizeofArray(p->aStmt); i++){
114683    sqlite3_finalize(p->aStmt[i]);
114684  }
114685  sqlite3_free(p->zSegmentsTbl);
114686  sqlite3_free(p->zReadExprlist);
114687  sqlite3_free(p->zWriteExprlist);
114688
114689  /* Invoke the tokenizer destructor to free the tokenizer. */
114690  p->pTokenizer->pModule->xDestroy(p->pTokenizer);
114691
114692  sqlite3_free(p);
114693  return SQLITE_OK;
114694}
114695
114696/*
114697** Construct one or more SQL statements from the format string given
114698** and then evaluate those statements. The success code is written
114699** into *pRc.
114700**
114701** If *pRc is initially non-zero then this routine is a no-op.
114702*/
114703static void fts3DbExec(
114704  int *pRc,              /* Success code */
114705  sqlite3 *db,           /* Database in which to run SQL */
114706  const char *zFormat,   /* Format string for SQL */
114707  ...                    /* Arguments to the format string */
114708){
114709  va_list ap;
114710  char *zSql;
114711  if( *pRc ) return;
114712  va_start(ap, zFormat);
114713  zSql = sqlite3_vmprintf(zFormat, ap);
114714  va_end(ap);
114715  if( zSql==0 ){
114716    *pRc = SQLITE_NOMEM;
114717  }else{
114718    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
114719    sqlite3_free(zSql);
114720  }
114721}
114722
114723/*
114724** The xDestroy() virtual table method.
114725*/
114726static int fts3DestroyMethod(sqlite3_vtab *pVtab){
114727  int rc = SQLITE_OK;              /* Return code */
114728  Fts3Table *p = (Fts3Table *)pVtab;
114729  sqlite3 *db = p->db;
114730
114731  /* Drop the shadow tables */
114732  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName);
114733  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName);
114734  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName);
114735  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName);
114736  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName);
114737
114738  /* If everything has worked, invoke fts3DisconnectMethod() to free the
114739  ** memory associated with the Fts3Table structure and return SQLITE_OK.
114740  ** Otherwise, return an SQLite error code.
114741  */
114742  return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
114743}
114744
114745
114746/*
114747** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
114748** passed as the first argument. This is done as part of the xConnect()
114749** and xCreate() methods.
114750**
114751** If *pRc is non-zero when this function is called, it is a no-op.
114752** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
114753** before returning.
114754*/
114755static void fts3DeclareVtab(int *pRc, Fts3Table *p){
114756  if( *pRc==SQLITE_OK ){
114757    int i;                        /* Iterator variable */
114758    int rc;                       /* Return code */
114759    char *zSql;                   /* SQL statement passed to declare_vtab() */
114760    char *zCols;                  /* List of user defined columns */
114761
114762    sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
114763
114764    /* Create a list of user columns for the virtual table */
114765    zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
114766    for(i=1; zCols && i<p->nColumn; i++){
114767      zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
114768    }
114769
114770    /* Create the whole "CREATE TABLE" statement to pass to SQLite */
114771    zSql = sqlite3_mprintf(
114772        "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
114773    );
114774    if( !zCols || !zSql ){
114775      rc = SQLITE_NOMEM;
114776    }else{
114777      rc = sqlite3_declare_vtab(p->db, zSql);
114778    }
114779
114780    sqlite3_free(zSql);
114781    sqlite3_free(zCols);
114782    *pRc = rc;
114783  }
114784}
114785
114786/*
114787** Create the backing store tables (%_content, %_segments and %_segdir)
114788** required by the FTS3 table passed as the only argument. This is done
114789** as part of the vtab xCreate() method.
114790**
114791** If the p->bHasDocsize boolean is true (indicating that this is an
114792** FTS4 table, not an FTS3 table) then also create the %_docsize and
114793** %_stat tables required by FTS4.
114794*/
114795static int fts3CreateTables(Fts3Table *p){
114796  int rc = SQLITE_OK;             /* Return code */
114797  int i;                          /* Iterator variable */
114798  char *zContentCols;             /* Columns of %_content table */
114799  sqlite3 *db = p->db;            /* The database connection */
114800
114801  /* Create a list of user columns for the content table */
114802  zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
114803  for(i=0; zContentCols && i<p->nColumn; i++){
114804    char *z = p->azColumn[i];
114805    zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
114806  }
114807  if( zContentCols==0 ) rc = SQLITE_NOMEM;
114808
114809  /* Create the content table */
114810  fts3DbExec(&rc, db,
114811     "CREATE TABLE %Q.'%q_content'(%s)",
114812     p->zDb, p->zName, zContentCols
114813  );
114814  sqlite3_free(zContentCols);
114815  /* Create other tables */
114816  fts3DbExec(&rc, db,
114817      "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
114818      p->zDb, p->zName
114819  );
114820  fts3DbExec(&rc, db,
114821      "CREATE TABLE %Q.'%q_segdir'("
114822        "level INTEGER,"
114823        "idx INTEGER,"
114824        "start_block INTEGER,"
114825        "leaves_end_block INTEGER,"
114826        "end_block INTEGER,"
114827        "root BLOB,"
114828        "PRIMARY KEY(level, idx)"
114829      ");",
114830      p->zDb, p->zName
114831  );
114832  if( p->bHasDocsize ){
114833    fts3DbExec(&rc, db,
114834        "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
114835        p->zDb, p->zName
114836    );
114837  }
114838  if( p->bHasStat ){
114839    fts3DbExec(&rc, db,
114840        "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
114841        p->zDb, p->zName
114842    );
114843  }
114844  return rc;
114845}
114846
114847/*
114848** Store the current database page-size in bytes in p->nPgsz.
114849**
114850** If *pRc is non-zero when this function is called, it is a no-op.
114851** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
114852** before returning.
114853*/
114854static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
114855  if( *pRc==SQLITE_OK ){
114856    int rc;                       /* Return code */
114857    char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
114858    sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
114859
114860    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
114861    if( !zSql ){
114862      rc = SQLITE_NOMEM;
114863    }else{
114864      rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
114865      if( rc==SQLITE_OK ){
114866        sqlite3_step(pStmt);
114867        p->nPgsz = sqlite3_column_int(pStmt, 0);
114868        rc = sqlite3_finalize(pStmt);
114869      }else if( rc==SQLITE_AUTH ){
114870        p->nPgsz = 1024;
114871        rc = SQLITE_OK;
114872      }
114873    }
114874    assert( p->nPgsz>0 || rc!=SQLITE_OK );
114875    sqlite3_free(zSql);
114876    *pRc = rc;
114877  }
114878}
114879
114880/*
114881** "Special" FTS4 arguments are column specifications of the following form:
114882**
114883**   <key> = <value>
114884**
114885** There may not be whitespace surrounding the "=" character. The <value>
114886** term may be quoted, but the <key> may not.
114887*/
114888static int fts3IsSpecialColumn(
114889  const char *z,
114890  int *pnKey,
114891  char **pzValue
114892){
114893  char *zValue;
114894  const char *zCsr = z;
114895
114896  while( *zCsr!='=' ){
114897    if( *zCsr=='\0' ) return 0;
114898    zCsr++;
114899  }
114900
114901  *pnKey = (int)(zCsr-z);
114902  zValue = sqlite3_mprintf("%s", &zCsr[1]);
114903  if( zValue ){
114904    sqlite3Fts3Dequote(zValue);
114905  }
114906  *pzValue = zValue;
114907  return 1;
114908}
114909
114910/*
114911** Append the output of a printf() style formatting to an existing string.
114912*/
114913static void fts3Appendf(
114914  int *pRc,                       /* IN/OUT: Error code */
114915  char **pz,                      /* IN/OUT: Pointer to string buffer */
114916  const char *zFormat,            /* Printf format string to append */
114917  ...                             /* Arguments for printf format string */
114918){
114919  if( *pRc==SQLITE_OK ){
114920    va_list ap;
114921    char *z;
114922    va_start(ap, zFormat);
114923    z = sqlite3_vmprintf(zFormat, ap);
114924    if( z && *pz ){
114925      char *z2 = sqlite3_mprintf("%s%s", *pz, z);
114926      sqlite3_free(z);
114927      z = z2;
114928    }
114929    if( z==0 ) *pRc = SQLITE_NOMEM;
114930    sqlite3_free(*pz);
114931    *pz = z;
114932  }
114933}
114934
114935/*
114936** Return a copy of input string zInput enclosed in double-quotes (") and
114937** with all double quote characters escaped. For example:
114938**
114939**     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
114940**
114941** The pointer returned points to memory obtained from sqlite3_malloc(). It
114942** is the callers responsibility to call sqlite3_free() to release this
114943** memory.
114944*/
114945static char *fts3QuoteId(char const *zInput){
114946  int nRet;
114947  char *zRet;
114948  nRet = 2 + strlen(zInput)*2 + 1;
114949  zRet = sqlite3_malloc(nRet);
114950  if( zRet ){
114951    int i;
114952    char *z = zRet;
114953    *(z++) = '"';
114954    for(i=0; zInput[i]; i++){
114955      if( zInput[i]=='"' ) *(z++) = '"';
114956      *(z++) = zInput[i];
114957    }
114958    *(z++) = '"';
114959    *(z++) = '\0';
114960  }
114961  return zRet;
114962}
114963
114964/*
114965** Return a list of comma separated SQL expressions that could be used
114966** in a SELECT statement such as the following:
114967**
114968**     SELECT <list of expressions> FROM %_content AS x ...
114969**
114970** to return the docid, followed by each column of text data in order
114971** from left to write. If parameter zFunc is not NULL, then instead of
114972** being returned directly each column of text data is passed to an SQL
114973** function named zFunc first. For example, if zFunc is "unzip" and the
114974** table has the three user-defined columns "a", "b", and "c", the following
114975** string is returned:
114976**
114977**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
114978**
114979** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
114980** is the responsibility of the caller to eventually free it.
114981**
114982** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
114983** a NULL pointer is returned). Otherwise, if an OOM error is encountered
114984** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
114985** no error occurs, *pRc is left unmodified.
114986*/
114987static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
114988  char *zRet = 0;
114989  char *zFree = 0;
114990  char *zFunction;
114991  int i;
114992
114993  if( !zFunc ){
114994    zFunction = "";
114995  }else{
114996    zFree = zFunction = fts3QuoteId(zFunc);
114997  }
114998  fts3Appendf(pRc, &zRet, "docid");
114999  for(i=0; i<p->nColumn; i++){
115000    fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
115001  }
115002  sqlite3_free(zFree);
115003  return zRet;
115004}
115005
115006/*
115007** Return a list of N comma separated question marks, where N is the number
115008** of columns in the %_content table (one for the docid plus one for each
115009** user-defined text column).
115010**
115011** If argument zFunc is not NULL, then all but the first question mark
115012** is preceded by zFunc and an open bracket, and followed by a closed
115013** bracket. For example, if zFunc is "zip" and the FTS3 table has three
115014** user-defined text columns, the following string is returned:
115015**
115016**     "?, zip(?), zip(?), zip(?)"
115017**
115018** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
115019** is the responsibility of the caller to eventually free it.
115020**
115021** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
115022** a NULL pointer is returned). Otherwise, if an OOM error is encountered
115023** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
115024** no error occurs, *pRc is left unmodified.
115025*/
115026static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
115027  char *zRet = 0;
115028  char *zFree = 0;
115029  char *zFunction;
115030  int i;
115031
115032  if( !zFunc ){
115033    zFunction = "";
115034  }else{
115035    zFree = zFunction = fts3QuoteId(zFunc);
115036  }
115037  fts3Appendf(pRc, &zRet, "?");
115038  for(i=0; i<p->nColumn; i++){
115039    fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
115040  }
115041  sqlite3_free(zFree);
115042  return zRet;
115043}
115044
115045/*
115046** This function interprets the string at (*pp) as a non-negative integer
115047** value. It reads the integer and sets *pnOut to the value read, then
115048** sets *pp to point to the byte immediately following the last byte of
115049** the integer value.
115050**
115051** Only decimal digits ('0'..'9') may be part of an integer value.
115052**
115053** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
115054** the output value undefined. Otherwise SQLITE_OK is returned.
115055**
115056** This function is used when parsing the "prefix=" FTS4 parameter.
115057*/
115058static int fts3GobbleInt(const char **pp, int *pnOut){
115059  const char *p = *pp;            /* Iterator pointer */
115060  int nInt = 0;                   /* Output value */
115061
115062  for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
115063    nInt = nInt * 10 + (p[0] - '0');
115064  }
115065  if( p==*pp ) return SQLITE_ERROR;
115066  *pnOut = nInt;
115067  *pp = p;
115068  return SQLITE_OK;
115069}
115070
115071/*
115072** This function is called to allocate an array of Fts3Index structures
115073** representing the indexes maintained by the current FTS table. FTS tables
115074** always maintain the main "terms" index, but may also maintain one or
115075** more "prefix" indexes, depending on the value of the "prefix=" parameter
115076** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
115077**
115078** Argument zParam is passed the value of the "prefix=" option if one was
115079** specified, or NULL otherwise.
115080**
115081** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
115082** the allocated array. *pnIndex is set to the number of elements in the
115083** array. If an error does occur, an SQLite error code is returned.
115084**
115085** Regardless of whether or not an error is returned, it is the responsibility
115086** of the caller to call sqlite3_free() on the output array to free it.
115087*/
115088static int fts3PrefixParameter(
115089  const char *zParam,             /* ABC in prefix=ABC parameter to parse */
115090  int *pnIndex,                   /* OUT: size of *apIndex[] array */
115091  struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
115092){
115093  struct Fts3Index *aIndex;       /* Allocated array */
115094  int nIndex = 1;                 /* Number of entries in array */
115095
115096  if( zParam && zParam[0] ){
115097    const char *p;
115098    nIndex++;
115099    for(p=zParam; *p; p++){
115100      if( *p==',' ) nIndex++;
115101    }
115102  }
115103
115104  aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
115105  *apIndex = aIndex;
115106  *pnIndex = nIndex;
115107  if( !aIndex ){
115108    return SQLITE_NOMEM;
115109  }
115110
115111  memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
115112  if( zParam ){
115113    const char *p = zParam;
115114    int i;
115115    for(i=1; i<nIndex; i++){
115116      int nPrefix;
115117      if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
115118      aIndex[i].nPrefix = nPrefix;
115119      p++;
115120    }
115121  }
115122
115123  return SQLITE_OK;
115124}
115125
115126/*
115127** This function is the implementation of both the xConnect and xCreate
115128** methods of the FTS3 virtual table.
115129**
115130** The argv[] array contains the following:
115131**
115132**   argv[0]   -> module name  ("fts3" or "fts4")
115133**   argv[1]   -> database name
115134**   argv[2]   -> table name
115135**   argv[...] -> "column name" and other module argument fields.
115136*/
115137static int fts3InitVtab(
115138  int isCreate,                   /* True for xCreate, false for xConnect */
115139  sqlite3 *db,                    /* The SQLite database connection */
115140  void *pAux,                     /* Hash table containing tokenizers */
115141  int argc,                       /* Number of elements in argv array */
115142  const char * const *argv,       /* xCreate/xConnect argument array */
115143  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
115144  char **pzErr                    /* Write any error message here */
115145){
115146  Fts3Hash *pHash = (Fts3Hash *)pAux;
115147  Fts3Table *p = 0;               /* Pointer to allocated vtab */
115148  int rc = SQLITE_OK;             /* Return code */
115149  int i;                          /* Iterator variable */
115150  int nByte;                      /* Size of allocation used for *p */
115151  int iCol;                       /* Column index */
115152  int nString = 0;                /* Bytes required to hold all column names */
115153  int nCol = 0;                   /* Number of columns in the FTS table */
115154  char *zCsr;                     /* Space for holding column names */
115155  int nDb;                        /* Bytes required to hold database name */
115156  int nName;                      /* Bytes required to hold table name */
115157  int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
115158  const char **aCol;              /* Array of column names */
115159  sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
115160
115161  int nIndex;                     /* Size of aIndex[] array */
115162  struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
115163
115164  /* The results of parsing supported FTS4 key=value options: */
115165  int bNoDocsize = 0;             /* True to omit %_docsize table */
115166  int bDescIdx = 0;               /* True to store descending indexes */
115167  char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
115168  char *zCompress = 0;            /* compress=? parameter (or NULL) */
115169  char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
115170
115171  assert( strlen(argv[0])==4 );
115172  assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
115173       || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
115174  );
115175
115176  nDb = (int)strlen(argv[1]) + 1;
115177  nName = (int)strlen(argv[2]) + 1;
115178
115179  aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
115180  if( !aCol ) return SQLITE_NOMEM;
115181  memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
115182
115183  /* Loop through all of the arguments passed by the user to the FTS3/4
115184  ** module (i.e. all the column names and special arguments). This loop
115185  ** does the following:
115186  **
115187  **   + Figures out the number of columns the FTSX table will have, and
115188  **     the number of bytes of space that must be allocated to store copies
115189  **     of the column names.
115190  **
115191  **   + If there is a tokenizer specification included in the arguments,
115192  **     initializes the tokenizer pTokenizer.
115193  */
115194  for(i=3; rc==SQLITE_OK && i<argc; i++){
115195    char const *z = argv[i];
115196    int nKey;
115197    char *zVal;
115198
115199    /* Check if this is a tokenizer specification */
115200    if( !pTokenizer
115201     && strlen(z)>8
115202     && 0==sqlite3_strnicmp(z, "tokenize", 8)
115203     && 0==sqlite3Fts3IsIdChar(z[8])
115204    ){
115205      rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
115206    }
115207
115208    /* Check if it is an FTS4 special argument. */
115209    else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
115210      struct Fts4Option {
115211        const char *zOpt;
115212        int nOpt;
115213        char **pzVar;
115214      } aFts4Opt[] = {
115215        { "matchinfo",   9, 0 },            /* 0 -> MATCHINFO */
115216        { "prefix",      6, 0 },            /* 1 -> PREFIX */
115217        { "compress",    8, 0 },            /* 2 -> COMPRESS */
115218        { "uncompress", 10, 0 },            /* 3 -> UNCOMPRESS */
115219        { "order",       5, 0 }             /* 4 -> ORDER */
115220      };
115221
115222      int iOpt;
115223      if( !zVal ){
115224        rc = SQLITE_NOMEM;
115225      }else{
115226        for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
115227          struct Fts4Option *pOp = &aFts4Opt[iOpt];
115228          if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
115229            break;
115230          }
115231        }
115232        if( iOpt==SizeofArray(aFts4Opt) ){
115233          *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
115234          rc = SQLITE_ERROR;
115235        }else{
115236          switch( iOpt ){
115237            case 0:               /* MATCHINFO */
115238              if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
115239                *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
115240                rc = SQLITE_ERROR;
115241              }
115242              bNoDocsize = 1;
115243              break;
115244
115245            case 1:               /* PREFIX */
115246              sqlite3_free(zPrefix);
115247              zPrefix = zVal;
115248              zVal = 0;
115249              break;
115250
115251            case 2:               /* COMPRESS */
115252              sqlite3_free(zCompress);
115253              zCompress = zVal;
115254              zVal = 0;
115255              break;
115256
115257            case 3:               /* UNCOMPRESS */
115258              sqlite3_free(zUncompress);
115259              zUncompress = zVal;
115260              zVal = 0;
115261              break;
115262
115263            case 4:               /* ORDER */
115264              if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
115265               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 3))
115266              ){
115267                *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
115268                rc = SQLITE_ERROR;
115269              }
115270              bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
115271              break;
115272          }
115273        }
115274        sqlite3_free(zVal);
115275      }
115276    }
115277
115278    /* Otherwise, the argument is a column name. */
115279    else {
115280      nString += (int)(strlen(z) + 1);
115281      aCol[nCol++] = z;
115282    }
115283  }
115284  if( rc!=SQLITE_OK ) goto fts3_init_out;
115285
115286  if( nCol==0 ){
115287    assert( nString==0 );
115288    aCol[0] = "content";
115289    nString = 8;
115290    nCol = 1;
115291  }
115292
115293  if( pTokenizer==0 ){
115294    rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
115295    if( rc!=SQLITE_OK ) goto fts3_init_out;
115296  }
115297  assert( pTokenizer );
115298
115299  rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
115300  if( rc==SQLITE_ERROR ){
115301    assert( zPrefix );
115302    *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
115303  }
115304  if( rc!=SQLITE_OK ) goto fts3_init_out;
115305
115306  /* Allocate and populate the Fts3Table structure. */
115307  nByte = sizeof(Fts3Table) +                  /* Fts3Table */
115308          nCol * sizeof(char *) +              /* azColumn */
115309          nIndex * sizeof(struct Fts3Index) +  /* aIndex */
115310          nName +                              /* zName */
115311          nDb +                                /* zDb */
115312          nString;                             /* Space for azColumn strings */
115313  p = (Fts3Table*)sqlite3_malloc(nByte);
115314  if( p==0 ){
115315    rc = SQLITE_NOMEM;
115316    goto fts3_init_out;
115317  }
115318  memset(p, 0, nByte);
115319  p->db = db;
115320  p->nColumn = nCol;
115321  p->nPendingData = 0;
115322  p->azColumn = (char **)&p[1];
115323  p->pTokenizer = pTokenizer;
115324  p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
115325  p->bHasDocsize = (isFts4 && bNoDocsize==0);
115326  p->bHasStat = isFts4;
115327  p->bDescIdx = bDescIdx;
115328  TESTONLY( p->inTransaction = -1 );
115329  TESTONLY( p->mxSavepoint = -1 );
115330
115331  p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
115332  memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
115333  p->nIndex = nIndex;
115334  for(i=0; i<nIndex; i++){
115335    fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
115336  }
115337
115338  /* Fill in the zName and zDb fields of the vtab structure. */
115339  zCsr = (char *)&p->aIndex[nIndex];
115340  p->zName = zCsr;
115341  memcpy(zCsr, argv[2], nName);
115342  zCsr += nName;
115343  p->zDb = zCsr;
115344  memcpy(zCsr, argv[1], nDb);
115345  zCsr += nDb;
115346
115347  /* Fill in the azColumn array */
115348  for(iCol=0; iCol<nCol; iCol++){
115349    char *z;
115350    int n = 0;
115351    z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
115352    memcpy(zCsr, z, n);
115353    zCsr[n] = '\0';
115354    sqlite3Fts3Dequote(zCsr);
115355    p->azColumn[iCol] = zCsr;
115356    zCsr += n+1;
115357    assert( zCsr <= &((char *)p)[nByte] );
115358  }
115359
115360  if( (zCompress==0)!=(zUncompress==0) ){
115361    char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
115362    rc = SQLITE_ERROR;
115363    *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
115364  }
115365  p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
115366  p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
115367  if( rc!=SQLITE_OK ) goto fts3_init_out;
115368
115369  /* If this is an xCreate call, create the underlying tables in the
115370  ** database. TODO: For xConnect(), it could verify that said tables exist.
115371  */
115372  if( isCreate ){
115373    rc = fts3CreateTables(p);
115374  }
115375
115376  /* Figure out the page-size for the database. This is required in order to
115377  ** estimate the cost of loading large doclists from the database.  */
115378  fts3DatabasePageSize(&rc, p);
115379  p->nNodeSize = p->nPgsz-35;
115380
115381  /* Declare the table schema to SQLite. */
115382  fts3DeclareVtab(&rc, p);
115383
115384fts3_init_out:
115385  sqlite3_free(zPrefix);
115386  sqlite3_free(aIndex);
115387  sqlite3_free(zCompress);
115388  sqlite3_free(zUncompress);
115389  sqlite3_free((void *)aCol);
115390  if( rc!=SQLITE_OK ){
115391    if( p ){
115392      fts3DisconnectMethod((sqlite3_vtab *)p);
115393    }else if( pTokenizer ){
115394      pTokenizer->pModule->xDestroy(pTokenizer);
115395    }
115396  }else{
115397    assert( p->pSegments==0 );
115398    *ppVTab = &p->base;
115399  }
115400  return rc;
115401}
115402
115403/*
115404** The xConnect() and xCreate() methods for the virtual table. All the
115405** work is done in function fts3InitVtab().
115406*/
115407static int fts3ConnectMethod(
115408  sqlite3 *db,                    /* Database connection */
115409  void *pAux,                     /* Pointer to tokenizer hash table */
115410  int argc,                       /* Number of elements in argv array */
115411  const char * const *argv,       /* xCreate/xConnect argument array */
115412  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
115413  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
115414){
115415  return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
115416}
115417static int fts3CreateMethod(
115418  sqlite3 *db,                    /* Database connection */
115419  void *pAux,                     /* Pointer to tokenizer hash table */
115420  int argc,                       /* Number of elements in argv array */
115421  const char * const *argv,       /* xCreate/xConnect argument array */
115422  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
115423  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
115424){
115425  return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
115426}
115427
115428/*
115429** Implementation of the xBestIndex method for FTS3 tables. There
115430** are three possible strategies, in order of preference:
115431**
115432**   1. Direct lookup by rowid or docid.
115433**   2. Full-text search using a MATCH operator on a non-docid column.
115434**   3. Linear scan of %_content table.
115435*/
115436static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
115437  Fts3Table *p = (Fts3Table *)pVTab;
115438  int i;                          /* Iterator variable */
115439  int iCons = -1;                 /* Index of constraint to use */
115440
115441  /* By default use a full table scan. This is an expensive option,
115442  ** so search through the constraints to see if a more efficient
115443  ** strategy is possible.
115444  */
115445  pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
115446  pInfo->estimatedCost = 500000;
115447  for(i=0; i<pInfo->nConstraint; i++){
115448    struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
115449    if( pCons->usable==0 ) continue;
115450
115451    /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
115452    if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
115453     && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
115454    ){
115455      pInfo->idxNum = FTS3_DOCID_SEARCH;
115456      pInfo->estimatedCost = 1.0;
115457      iCons = i;
115458    }
115459
115460    /* A MATCH constraint. Use a full-text search.
115461    **
115462    ** If there is more than one MATCH constraint available, use the first
115463    ** one encountered. If there is both a MATCH constraint and a direct
115464    ** rowid/docid lookup, prefer the MATCH strategy. This is done even
115465    ** though the rowid/docid lookup is faster than a MATCH query, selecting
115466    ** it would lead to an "unable to use function MATCH in the requested
115467    ** context" error.
115468    */
115469    if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
115470     && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
115471    ){
115472      pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
115473      pInfo->estimatedCost = 2.0;
115474      iCons = i;
115475      break;
115476    }
115477  }
115478
115479  if( iCons>=0 ){
115480    pInfo->aConstraintUsage[iCons].argvIndex = 1;
115481    pInfo->aConstraintUsage[iCons].omit = 1;
115482  }
115483
115484  /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
115485  ** docid) order. Both ascending and descending are possible.
115486  */
115487  if( pInfo->nOrderBy==1 ){
115488    struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
115489    if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
115490      if( pOrder->desc ){
115491        pInfo->idxStr = "DESC";
115492      }else{
115493        pInfo->idxStr = "ASC";
115494      }
115495      pInfo->orderByConsumed = 1;
115496    }
115497  }
115498
115499  assert( p->pSegments==0 );
115500  return SQLITE_OK;
115501}
115502
115503/*
115504** Implementation of xOpen method.
115505*/
115506static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
115507  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
115508
115509  UNUSED_PARAMETER(pVTab);
115510
115511  /* Allocate a buffer large enough for an Fts3Cursor structure. If the
115512  ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
115513  ** if the allocation fails, return SQLITE_NOMEM.
115514  */
115515  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
115516  if( !pCsr ){
115517    return SQLITE_NOMEM;
115518  }
115519  memset(pCsr, 0, sizeof(Fts3Cursor));
115520  return SQLITE_OK;
115521}
115522
115523/*
115524** Close the cursor.  For additional information see the documentation
115525** on the xClose method of the virtual table interface.
115526*/
115527static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
115528  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
115529  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
115530  sqlite3_finalize(pCsr->pStmt);
115531  sqlite3Fts3ExprFree(pCsr->pExpr);
115532  sqlite3Fts3FreeDeferredTokens(pCsr);
115533  sqlite3_free(pCsr->aDoclist);
115534  sqlite3_free(pCsr->aMatchinfo);
115535  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
115536  sqlite3_free(pCsr);
115537  return SQLITE_OK;
115538}
115539
115540/*
115541** Position the pCsr->pStmt statement so that it is on the row
115542** of the %_content table that contains the last match.  Return
115543** SQLITE_OK on success.
115544*/
115545static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
115546  if( pCsr->isRequireSeek ){
115547    sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
115548    pCsr->isRequireSeek = 0;
115549    if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
115550      return SQLITE_OK;
115551    }else{
115552      int rc = sqlite3_reset(pCsr->pStmt);
115553      if( rc==SQLITE_OK ){
115554        /* If no row was found and no error has occured, then the %_content
115555        ** table is missing a row that is present in the full-text index.
115556        ** The data structures are corrupt.
115557        */
115558        rc = SQLITE_CORRUPT_VTAB;
115559      }
115560      pCsr->isEof = 1;
115561      if( pContext ){
115562        sqlite3_result_error_code(pContext, rc);
115563      }
115564      return rc;
115565    }
115566  }else{
115567    return SQLITE_OK;
115568  }
115569}
115570
115571/*
115572** This function is used to process a single interior node when searching
115573** a b-tree for a term or term prefix. The node data is passed to this
115574** function via the zNode/nNode parameters. The term to search for is
115575** passed in zTerm/nTerm.
115576**
115577** If piFirst is not NULL, then this function sets *piFirst to the blockid
115578** of the child node that heads the sub-tree that may contain the term.
115579**
115580** If piLast is not NULL, then *piLast is set to the right-most child node
115581** that heads a sub-tree that may contain a term for which zTerm/nTerm is
115582** a prefix.
115583**
115584** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
115585*/
115586static int fts3ScanInteriorNode(
115587  const char *zTerm,              /* Term to select leaves for */
115588  int nTerm,                      /* Size of term zTerm in bytes */
115589  const char *zNode,              /* Buffer containing segment interior node */
115590  int nNode,                      /* Size of buffer at zNode */
115591  sqlite3_int64 *piFirst,         /* OUT: Selected child node */
115592  sqlite3_int64 *piLast           /* OUT: Selected child node */
115593){
115594  int rc = SQLITE_OK;             /* Return code */
115595  const char *zCsr = zNode;       /* Cursor to iterate through node */
115596  const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
115597  char *zBuffer = 0;              /* Buffer to load terms into */
115598  int nAlloc = 0;                 /* Size of allocated buffer */
115599  int isFirstTerm = 1;            /* True when processing first term on page */
115600  sqlite3_int64 iChild;           /* Block id of child node to descend to */
115601
115602  /* Skip over the 'height' varint that occurs at the start of every
115603  ** interior node. Then load the blockid of the left-child of the b-tree
115604  ** node into variable iChild.
115605  **
115606  ** Even if the data structure on disk is corrupted, this (reading two
115607  ** varints from the buffer) does not risk an overread. If zNode is a
115608  ** root node, then the buffer comes from a SELECT statement. SQLite does
115609  ** not make this guarantee explicitly, but in practice there are always
115610  ** either more than 20 bytes of allocated space following the nNode bytes of
115611  ** contents, or two zero bytes. Or, if the node is read from the %_segments
115612  ** table, then there are always 20 bytes of zeroed padding following the
115613  ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
115614  */
115615  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115616  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115617  if( zCsr>zEnd ){
115618    return SQLITE_CORRUPT_VTAB;
115619  }
115620
115621  while( zCsr<zEnd && (piFirst || piLast) ){
115622    int cmp;                      /* memcmp() result */
115623    int nSuffix;                  /* Size of term suffix */
115624    int nPrefix = 0;              /* Size of term prefix */
115625    int nBuffer;                  /* Total term size */
115626
115627    /* Load the next term on the node into zBuffer. Use realloc() to expand
115628    ** the size of zBuffer if required.  */
115629    if( !isFirstTerm ){
115630      zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
115631    }
115632    isFirstTerm = 0;
115633    zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
115634
115635    if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
115636      rc = SQLITE_CORRUPT_VTAB;
115637      goto finish_scan;
115638    }
115639    if( nPrefix+nSuffix>nAlloc ){
115640      char *zNew;
115641      nAlloc = (nPrefix+nSuffix) * 2;
115642      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
115643      if( !zNew ){
115644        rc = SQLITE_NOMEM;
115645        goto finish_scan;
115646      }
115647      zBuffer = zNew;
115648    }
115649    memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
115650    nBuffer = nPrefix + nSuffix;
115651    zCsr += nSuffix;
115652
115653    /* Compare the term we are searching for with the term just loaded from
115654    ** the interior node. If the specified term is greater than or equal
115655    ** to the term from the interior node, then all terms on the sub-tree
115656    ** headed by node iChild are smaller than zTerm. No need to search
115657    ** iChild.
115658    **
115659    ** If the interior node term is larger than the specified term, then
115660    ** the tree headed by iChild may contain the specified term.
115661    */
115662    cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
115663    if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
115664      *piFirst = iChild;
115665      piFirst = 0;
115666    }
115667
115668    if( piLast && cmp<0 ){
115669      *piLast = iChild;
115670      piLast = 0;
115671    }
115672
115673    iChild++;
115674  };
115675
115676  if( piFirst ) *piFirst = iChild;
115677  if( piLast ) *piLast = iChild;
115678
115679 finish_scan:
115680  sqlite3_free(zBuffer);
115681  return rc;
115682}
115683
115684
115685/*
115686** The buffer pointed to by argument zNode (size nNode bytes) contains an
115687** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
115688** contains a term. This function searches the sub-tree headed by the zNode
115689** node for the range of leaf nodes that may contain the specified term
115690** or terms for which the specified term is a prefix.
115691**
115692** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
115693** left-most leaf node in the tree that may contain the specified term.
115694** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
115695** right-most leaf node that may contain a term for which the specified
115696** term is a prefix.
115697**
115698** It is possible that the range of returned leaf nodes does not contain
115699** the specified term or any terms for which it is a prefix. However, if the
115700** segment does contain any such terms, they are stored within the identified
115701** range. Because this function only inspects interior segment nodes (and
115702** never loads leaf nodes into memory), it is not possible to be sure.
115703**
115704** If an error occurs, an error code other than SQLITE_OK is returned.
115705*/
115706static int fts3SelectLeaf(
115707  Fts3Table *p,                   /* Virtual table handle */
115708  const char *zTerm,              /* Term to select leaves for */
115709  int nTerm,                      /* Size of term zTerm in bytes */
115710  const char *zNode,              /* Buffer containing segment interior node */
115711  int nNode,                      /* Size of buffer at zNode */
115712  sqlite3_int64 *piLeaf,          /* Selected leaf node */
115713  sqlite3_int64 *piLeaf2          /* Selected leaf node */
115714){
115715  int rc;                         /* Return code */
115716  int iHeight;                    /* Height of this node in tree */
115717
115718  assert( piLeaf || piLeaf2 );
115719
115720  sqlite3Fts3GetVarint32(zNode, &iHeight);
115721  rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
115722  assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
115723
115724  if( rc==SQLITE_OK && iHeight>1 ){
115725    char *zBlob = 0;              /* Blob read from %_segments table */
115726    int nBlob;                    /* Size of zBlob in bytes */
115727
115728    if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
115729      rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
115730      if( rc==SQLITE_OK ){
115731        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
115732      }
115733      sqlite3_free(zBlob);
115734      piLeaf = 0;
115735      zBlob = 0;
115736    }
115737
115738    if( rc==SQLITE_OK ){
115739      rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
115740    }
115741    if( rc==SQLITE_OK ){
115742      rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
115743    }
115744    sqlite3_free(zBlob);
115745  }
115746
115747  return rc;
115748}
115749
115750/*
115751** This function is used to create delta-encoded serialized lists of FTS3
115752** varints. Each call to this function appends a single varint to a list.
115753*/
115754static void fts3PutDeltaVarint(
115755  char **pp,                      /* IN/OUT: Output pointer */
115756  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
115757  sqlite3_int64 iVal              /* Write this value to the list */
115758){
115759  assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
115760  *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
115761  *piPrev = iVal;
115762}
115763
115764/*
115765** When this function is called, *ppPoslist is assumed to point to the
115766** start of a position-list. After it returns, *ppPoslist points to the
115767** first byte after the position-list.
115768**
115769** A position list is list of positions (delta encoded) and columns for
115770** a single document record of a doclist.  So, in other words, this
115771** routine advances *ppPoslist so that it points to the next docid in
115772** the doclist, or to the first byte past the end of the doclist.
115773**
115774** If pp is not NULL, then the contents of the position list are copied
115775** to *pp. *pp is set to point to the first byte past the last byte copied
115776** before this function returns.
115777*/
115778static void fts3PoslistCopy(char **pp, char **ppPoslist){
115779  char *pEnd = *ppPoslist;
115780  char c = 0;
115781
115782  /* The end of a position list is marked by a zero encoded as an FTS3
115783  ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
115784  ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
115785  ** of some other, multi-byte, value.
115786  **
115787  ** The following while-loop moves pEnd to point to the first byte that is not
115788  ** immediately preceded by a byte with the 0x80 bit set. Then increments
115789  ** pEnd once more so that it points to the byte immediately following the
115790  ** last byte in the position-list.
115791  */
115792  while( *pEnd | c ){
115793    c = *pEnd++ & 0x80;
115794    testcase( c!=0 && (*pEnd)==0 );
115795  }
115796  pEnd++;  /* Advance past the POS_END terminator byte */
115797
115798  if( pp ){
115799    int n = (int)(pEnd - *ppPoslist);
115800    char *p = *pp;
115801    memcpy(p, *ppPoslist, n);
115802    p += n;
115803    *pp = p;
115804  }
115805  *ppPoslist = pEnd;
115806}
115807
115808/*
115809** When this function is called, *ppPoslist is assumed to point to the
115810** start of a column-list. After it returns, *ppPoslist points to the
115811** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
115812**
115813** A column-list is list of delta-encoded positions for a single column
115814** within a single document within a doclist.
115815**
115816** The column-list is terminated either by a POS_COLUMN varint (1) or
115817** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
115818** the POS_COLUMN or POS_END that terminates the column-list.
115819**
115820** If pp is not NULL, then the contents of the column-list are copied
115821** to *pp. *pp is set to point to the first byte past the last byte copied
115822** before this function returns.  The POS_COLUMN or POS_END terminator
115823** is not copied into *pp.
115824*/
115825static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
115826  char *pEnd = *ppPoslist;
115827  char c = 0;
115828
115829  /* A column-list is terminated by either a 0x01 or 0x00 byte that is
115830  ** not part of a multi-byte varint.
115831  */
115832  while( 0xFE & (*pEnd | c) ){
115833    c = *pEnd++ & 0x80;
115834    testcase( c!=0 && ((*pEnd)&0xfe)==0 );
115835  }
115836  if( pp ){
115837    int n = (int)(pEnd - *ppPoslist);
115838    char *p = *pp;
115839    memcpy(p, *ppPoslist, n);
115840    p += n;
115841    *pp = p;
115842  }
115843  *ppPoslist = pEnd;
115844}
115845
115846/*
115847** Value used to signify the end of an position-list. This is safe because
115848** it is not possible to have a document with 2^31 terms.
115849*/
115850#define POSITION_LIST_END 0x7fffffff
115851
115852/*
115853** This function is used to help parse position-lists. When this function is
115854** called, *pp may point to the start of the next varint in the position-list
115855** being parsed, or it may point to 1 byte past the end of the position-list
115856** (in which case **pp will be a terminator bytes POS_END (0) or
115857** (1)).
115858**
115859** If *pp points past the end of the current position-list, set *pi to
115860** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
115861** increment the current value of *pi by the value read, and set *pp to
115862** point to the next value before returning.
115863**
115864** Before calling this routine *pi must be initialized to the value of
115865** the previous position, or zero if we are reading the first position
115866** in the position-list.  Because positions are delta-encoded, the value
115867** of the previous position is needed in order to compute the value of
115868** the next position.
115869*/
115870static void fts3ReadNextPos(
115871  char **pp,                    /* IN/OUT: Pointer into position-list buffer */
115872  sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
115873){
115874  if( (**pp)&0xFE ){
115875    fts3GetDeltaVarint(pp, pi);
115876    *pi -= 2;
115877  }else{
115878    *pi = POSITION_LIST_END;
115879  }
115880}
115881
115882/*
115883** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
115884** the value of iCol encoded as a varint to *pp.   This will start a new
115885** column list.
115886**
115887** Set *pp to point to the byte just after the last byte written before
115888** returning (do not modify it if iCol==0). Return the total number of bytes
115889** written (0 if iCol==0).
115890*/
115891static int fts3PutColNumber(char **pp, int iCol){
115892  int n = 0;                      /* Number of bytes written */
115893  if( iCol ){
115894    char *p = *pp;                /* Output pointer */
115895    n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
115896    *p = 0x01;
115897    *pp = &p[n];
115898  }
115899  return n;
115900}
115901
115902/*
115903** Compute the union of two position lists.  The output written
115904** into *pp contains all positions of both *pp1 and *pp2 in sorted
115905** order and with any duplicates removed.  All pointers are
115906** updated appropriately.   The caller is responsible for insuring
115907** that there is enough space in *pp to hold the complete output.
115908*/
115909static void fts3PoslistMerge(
115910  char **pp,                      /* Output buffer */
115911  char **pp1,                     /* Left input list */
115912  char **pp2                      /* Right input list */
115913){
115914  char *p = *pp;
115915  char *p1 = *pp1;
115916  char *p2 = *pp2;
115917
115918  while( *p1 || *p2 ){
115919    int iCol1;         /* The current column index in pp1 */
115920    int iCol2;         /* The current column index in pp2 */
115921
115922    if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
115923    else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
115924    else iCol1 = 0;
115925
115926    if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
115927    else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
115928    else iCol2 = 0;
115929
115930    if( iCol1==iCol2 ){
115931      sqlite3_int64 i1 = 0;       /* Last position from pp1 */
115932      sqlite3_int64 i2 = 0;       /* Last position from pp2 */
115933      sqlite3_int64 iPrev = 0;
115934      int n = fts3PutColNumber(&p, iCol1);
115935      p1 += n;
115936      p2 += n;
115937
115938      /* At this point, both p1 and p2 point to the start of column-lists
115939      ** for the same column (the column with index iCol1 and iCol2).
115940      ** A column-list is a list of non-negative delta-encoded varints, each
115941      ** incremented by 2 before being stored. Each list is terminated by a
115942      ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
115943      ** and writes the results to buffer p. p is left pointing to the byte
115944      ** after the list written. No terminator (POS_END or POS_COLUMN) is
115945      ** written to the output.
115946      */
115947      fts3GetDeltaVarint(&p1, &i1);
115948      fts3GetDeltaVarint(&p2, &i2);
115949      do {
115950        fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
115951        iPrev -= 2;
115952        if( i1==i2 ){
115953          fts3ReadNextPos(&p1, &i1);
115954          fts3ReadNextPos(&p2, &i2);
115955        }else if( i1<i2 ){
115956          fts3ReadNextPos(&p1, &i1);
115957        }else{
115958          fts3ReadNextPos(&p2, &i2);
115959        }
115960      }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
115961    }else if( iCol1<iCol2 ){
115962      p1 += fts3PutColNumber(&p, iCol1);
115963      fts3ColumnlistCopy(&p, &p1);
115964    }else{
115965      p2 += fts3PutColNumber(&p, iCol2);
115966      fts3ColumnlistCopy(&p, &p2);
115967    }
115968  }
115969
115970  *p++ = POS_END;
115971  *pp = p;
115972  *pp1 = p1 + 1;
115973  *pp2 = p2 + 1;
115974}
115975
115976/*
115977** This function is used to merge two position lists into one. When it is
115978** called, *pp1 and *pp2 must both point to position lists. A position-list is
115979** the part of a doclist that follows each document id. For example, if a row
115980** contains:
115981**
115982**     'a b c'|'x y z'|'a b b a'
115983**
115984** Then the position list for this row for token 'b' would consist of:
115985**
115986**     0x02 0x01 0x02 0x03 0x03 0x00
115987**
115988** When this function returns, both *pp1 and *pp2 are left pointing to the
115989** byte following the 0x00 terminator of their respective position lists.
115990**
115991** If isSaveLeft is 0, an entry is added to the output position list for
115992** each position in *pp2 for which there exists one or more positions in
115993** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
115994** when the *pp1 token appears before the *pp2 token, but not more than nToken
115995** slots before it.
115996**
115997** e.g. nToken==1 searches for adjacent positions.
115998*/
115999static int fts3PoslistPhraseMerge(
116000  char **pp,                      /* IN/OUT: Preallocated output buffer */
116001  int nToken,                     /* Maximum difference in token positions */
116002  int isSaveLeft,                 /* Save the left position */
116003  int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
116004  char **pp1,                     /* IN/OUT: Left input list */
116005  char **pp2                      /* IN/OUT: Right input list */
116006){
116007  char *p = (pp ? *pp : 0);
116008  char *p1 = *pp1;
116009  char *p2 = *pp2;
116010  int iCol1 = 0;
116011  int iCol2 = 0;
116012
116013  /* Never set both isSaveLeft and isExact for the same invocation. */
116014  assert( isSaveLeft==0 || isExact==0 );
116015
116016  assert( *p1!=0 && *p2!=0 );
116017  if( *p1==POS_COLUMN ){
116018    p1++;
116019    p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
116020  }
116021  if( *p2==POS_COLUMN ){
116022    p2++;
116023    p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
116024  }
116025
116026  while( 1 ){
116027    if( iCol1==iCol2 ){
116028      char *pSave = p;
116029      sqlite3_int64 iPrev = 0;
116030      sqlite3_int64 iPos1 = 0;
116031      sqlite3_int64 iPos2 = 0;
116032
116033      if( pp && iCol1 ){
116034        *p++ = POS_COLUMN;
116035        p += sqlite3Fts3PutVarint(p, iCol1);
116036      }
116037
116038      assert( *p1!=POS_END && *p1!=POS_COLUMN );
116039      assert( *p2!=POS_END && *p2!=POS_COLUMN );
116040      fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
116041      fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
116042
116043      while( 1 ){
116044        if( iPos2==iPos1+nToken
116045         || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
116046        ){
116047          sqlite3_int64 iSave;
116048          if( !pp ){
116049            fts3PoslistCopy(0, &p2);
116050            fts3PoslistCopy(0, &p1);
116051            *pp1 = p1;
116052            *pp2 = p2;
116053            return 1;
116054          }
116055          iSave = isSaveLeft ? iPos1 : iPos2;
116056          fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
116057          pSave = 0;
116058        }
116059        if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
116060          if( (*p2&0xFE)==0 ) break;
116061          fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
116062        }else{
116063          if( (*p1&0xFE)==0 ) break;
116064          fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
116065        }
116066      }
116067
116068      if( pSave ){
116069        assert( pp && p );
116070        p = pSave;
116071      }
116072
116073      fts3ColumnlistCopy(0, &p1);
116074      fts3ColumnlistCopy(0, &p2);
116075      assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
116076      if( 0==*p1 || 0==*p2 ) break;
116077
116078      p1++;
116079      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
116080      p2++;
116081      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
116082    }
116083
116084    /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
116085    ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
116086    ** end of the position list, or the 0x01 that precedes the next
116087    ** column-number in the position list.
116088    */
116089    else if( iCol1<iCol2 ){
116090      fts3ColumnlistCopy(0, &p1);
116091      if( 0==*p1 ) break;
116092      p1++;
116093      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
116094    }else{
116095      fts3ColumnlistCopy(0, &p2);
116096      if( 0==*p2 ) break;
116097      p2++;
116098      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
116099    }
116100  }
116101
116102  fts3PoslistCopy(0, &p2);
116103  fts3PoslistCopy(0, &p1);
116104  *pp1 = p1;
116105  *pp2 = p2;
116106  if( !pp || *pp==p ){
116107    return 0;
116108  }
116109  *p++ = 0x00;
116110  *pp = p;
116111  return 1;
116112}
116113
116114/*
116115** Merge two position-lists as required by the NEAR operator. The argument
116116** position lists correspond to the left and right phrases of an expression
116117** like:
116118**
116119**     "phrase 1" NEAR "phrase number 2"
116120**
116121** Position list *pp1 corresponds to the left-hand side of the NEAR
116122** expression and *pp2 to the right. As usual, the indexes in the position
116123** lists are the offsets of the last token in each phrase (tokens "1" and "2"
116124** in the example above).
116125**
116126** The output position list - written to *pp - is a copy of *pp2 with those
116127** entries that are not sufficiently NEAR entries in *pp1 removed.
116128*/
116129static int fts3PoslistNearMerge(
116130  char **pp,                      /* Output buffer */
116131  char *aTmp,                     /* Temporary buffer space */
116132  int nRight,                     /* Maximum difference in token positions */
116133  int nLeft,                      /* Maximum difference in token positions */
116134  char **pp1,                     /* IN/OUT: Left input list */
116135  char **pp2                      /* IN/OUT: Right input list */
116136){
116137  char *p1 = *pp1;
116138  char *p2 = *pp2;
116139
116140  char *pTmp1 = aTmp;
116141  char *pTmp2;
116142  char *aTmp2;
116143  int res = 1;
116144
116145  fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
116146  aTmp2 = pTmp2 = pTmp1;
116147  *pp1 = p1;
116148  *pp2 = p2;
116149  fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
116150  if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
116151    fts3PoslistMerge(pp, &aTmp, &aTmp2);
116152  }else if( pTmp1!=aTmp ){
116153    fts3PoslistCopy(pp, &aTmp);
116154  }else if( pTmp2!=aTmp2 ){
116155    fts3PoslistCopy(pp, &aTmp2);
116156  }else{
116157    res = 0;
116158  }
116159
116160  return res;
116161}
116162
116163/*
116164** An instance of this function is used to merge together the (potentially
116165** large number of) doclists for each term that matches a prefix query.
116166** See function fts3TermSelectMerge() for details.
116167*/
116168typedef struct TermSelect TermSelect;
116169struct TermSelect {
116170  char *aaOutput[16];             /* Malloc'd output buffers */
116171  int anOutput[16];               /* Size each output buffer in bytes */
116172};
116173
116174/*
116175** This function is used to read a single varint from a buffer. Parameter
116176** pEnd points 1 byte past the end of the buffer. When this function is
116177** called, if *pp points to pEnd or greater, then the end of the buffer
116178** has been reached. In this case *pp is set to 0 and the function returns.
116179**
116180** If *pp does not point to or past pEnd, then a single varint is read
116181** from *pp. *pp is then set to point 1 byte past the end of the read varint.
116182**
116183** If bDescIdx is false, the value read is added to *pVal before returning.
116184** If it is true, the value read is subtracted from *pVal before this
116185** function returns.
116186*/
116187static void fts3GetDeltaVarint3(
116188  char **pp,                      /* IN/OUT: Point to read varint from */
116189  char *pEnd,                     /* End of buffer */
116190  int bDescIdx,                   /* True if docids are descending */
116191  sqlite3_int64 *pVal             /* IN/OUT: Integer value */
116192){
116193  if( *pp>=pEnd ){
116194    *pp = 0;
116195  }else{
116196    sqlite3_int64 iVal;
116197    *pp += sqlite3Fts3GetVarint(*pp, &iVal);
116198    if( bDescIdx ){
116199      *pVal -= iVal;
116200    }else{
116201      *pVal += iVal;
116202    }
116203  }
116204}
116205
116206/*
116207** This function is used to write a single varint to a buffer. The varint
116208** is written to *pp. Before returning, *pp is set to point 1 byte past the
116209** end of the value written.
116210**
116211** If *pbFirst is zero when this function is called, the value written to
116212** the buffer is that of parameter iVal.
116213**
116214** If *pbFirst is non-zero when this function is called, then the value
116215** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
116216** (if bDescIdx is non-zero).
116217**
116218** Before returning, this function always sets *pbFirst to 1 and *piPrev
116219** to the value of parameter iVal.
116220*/
116221static void fts3PutDeltaVarint3(
116222  char **pp,                      /* IN/OUT: Output pointer */
116223  int bDescIdx,                   /* True for descending docids */
116224  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
116225  int *pbFirst,                   /* IN/OUT: True after first int written */
116226  sqlite3_int64 iVal              /* Write this value to the list */
116227){
116228  sqlite3_int64 iWrite;
116229  if( bDescIdx==0 || *pbFirst==0 ){
116230    iWrite = iVal - *piPrev;
116231  }else{
116232    iWrite = *piPrev - iVal;
116233  }
116234  assert( *pbFirst || *piPrev==0 );
116235  assert( *pbFirst==0 || iWrite>0 );
116236  *pp += sqlite3Fts3PutVarint(*pp, iWrite);
116237  *piPrev = iVal;
116238  *pbFirst = 1;
116239}
116240
116241
116242/*
116243** This macro is used by various functions that merge doclists. The two
116244** arguments are 64-bit docid values. If the value of the stack variable
116245** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
116246** Otherwise, (i2-i1).
116247**
116248** Using this makes it easier to write code that can merge doclists that are
116249** sorted in either ascending or descending order.
116250*/
116251#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
116252
116253/*
116254** This function does an "OR" merge of two doclists (output contains all
116255** positions contained in either argument doclist). If the docids in the
116256** input doclists are sorted in ascending order, parameter bDescDoclist
116257** should be false. If they are sorted in ascending order, it should be
116258** passed a non-zero value.
116259**
116260** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
116261** containing the output doclist and SQLITE_OK is returned. In this case
116262** *pnOut is set to the number of bytes in the output doclist.
116263**
116264** If an error occurs, an SQLite error code is returned. The output values
116265** are undefined in this case.
116266*/
116267static int fts3DoclistOrMerge(
116268  int bDescDoclist,               /* True if arguments are desc */
116269  char *a1, int n1,               /* First doclist */
116270  char *a2, int n2,               /* Second doclist */
116271  char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
116272){
116273  sqlite3_int64 i1 = 0;
116274  sqlite3_int64 i2 = 0;
116275  sqlite3_int64 iPrev = 0;
116276  char *pEnd1 = &a1[n1];
116277  char *pEnd2 = &a2[n2];
116278  char *p1 = a1;
116279  char *p2 = a2;
116280  char *p;
116281  char *aOut;
116282  int bFirstOut = 0;
116283
116284  *paOut = 0;
116285  *pnOut = 0;
116286
116287  /* Allocate space for the output. Both the input and output doclists
116288  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
116289  ** then the first docid in each list is simply encoded as a varint. For
116290  ** each subsequent docid, the varint stored is the difference between the
116291  ** current and previous docid (a positive number - since the list is in
116292  ** ascending order).
116293  **
116294  ** The first docid written to the output is therefore encoded using the
116295  ** same number of bytes as it is in whichever of the input lists it is
116296  ** read from. And each subsequent docid read from the same input list
116297  ** consumes either the same or less bytes as it did in the input (since
116298  ** the difference between it and the previous value in the output must
116299  ** be a positive value less than or equal to the delta value read from
116300  ** the input list). The same argument applies to all but the first docid
116301  ** read from the 'other' list. And to the contents of all position lists
116302  ** that will be copied and merged from the input to the output.
116303  **
116304  ** However, if the first docid copied to the output is a negative number,
116305  ** then the encoding of the first docid from the 'other' input list may
116306  ** be larger in the output than it was in the input (since the delta value
116307  ** may be a larger positive integer than the actual docid).
116308  **
116309  ** The space required to store the output is therefore the sum of the
116310  ** sizes of the two inputs, plus enough space for exactly one of the input
116311  ** docids to grow.
116312  **
116313  ** A symetric argument may be made if the doclists are in descending
116314  ** order.
116315  */
116316  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
116317  if( !aOut ) return SQLITE_NOMEM;
116318
116319  p = aOut;
116320  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
116321  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
116322  while( p1 || p2 ){
116323    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
116324
116325    if( p2 && p1 && iDiff==0 ){
116326      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
116327      fts3PoslistMerge(&p, &p1, &p2);
116328      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
116329      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116330    }else if( !p2 || (p1 && iDiff<0) ){
116331      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
116332      fts3PoslistCopy(&p, &p1);
116333      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
116334    }else{
116335      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
116336      fts3PoslistCopy(&p, &p2);
116337      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116338    }
116339  }
116340
116341  *paOut = aOut;
116342  *pnOut = (p-aOut);
116343  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
116344  return SQLITE_OK;
116345}
116346
116347/*
116348** This function does a "phrase" merge of two doclists. In a phrase merge,
116349** the output contains a copy of each position from the right-hand input
116350** doclist for which there is a position in the left-hand input doclist
116351** exactly nDist tokens before it.
116352**
116353** If the docids in the input doclists are sorted in ascending order,
116354** parameter bDescDoclist should be false. If they are sorted in ascending
116355** order, it should be passed a non-zero value.
116356**
116357** The right-hand input doclist is overwritten by this function.
116358*/
116359static void fts3DoclistPhraseMerge(
116360  int bDescDoclist,               /* True if arguments are desc */
116361  int nDist,                      /* Distance from left to right (1=adjacent) */
116362  char *aLeft, int nLeft,         /* Left doclist */
116363  char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
116364){
116365  sqlite3_int64 i1 = 0;
116366  sqlite3_int64 i2 = 0;
116367  sqlite3_int64 iPrev = 0;
116368  char *pEnd1 = &aLeft[nLeft];
116369  char *pEnd2 = &aRight[*pnRight];
116370  char *p1 = aLeft;
116371  char *p2 = aRight;
116372  char *p;
116373  int bFirstOut = 0;
116374  char *aOut = aRight;
116375
116376  assert( nDist>0 );
116377
116378  p = aOut;
116379  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
116380  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
116381
116382  while( p1 && p2 ){
116383    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
116384    if( iDiff==0 ){
116385      char *pSave = p;
116386      sqlite3_int64 iPrevSave = iPrev;
116387      int bFirstOutSave = bFirstOut;
116388
116389      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
116390      if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
116391        p = pSave;
116392        iPrev = iPrevSave;
116393        bFirstOut = bFirstOutSave;
116394      }
116395      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
116396      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116397    }else if( iDiff<0 ){
116398      fts3PoslistCopy(0, &p1);
116399      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
116400    }else{
116401      fts3PoslistCopy(0, &p2);
116402      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116403    }
116404  }
116405
116406  *pnRight = p - aOut;
116407}
116408
116409
116410/*
116411** Merge all doclists in the TermSelect.aaOutput[] array into a single
116412** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
116413** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
116414**
116415** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
116416** the responsibility of the caller to free any doclists left in the
116417** TermSelect.aaOutput[] array.
116418*/
116419static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
116420  char *aOut = 0;
116421  int nOut = 0;
116422  int i;
116423
116424  /* Loop through the doclists in the aaOutput[] array. Merge them all
116425  ** into a single doclist.
116426  */
116427  for(i=0; i<SizeofArray(pTS->aaOutput); i++){
116428    if( pTS->aaOutput[i] ){
116429      if( !aOut ){
116430        aOut = pTS->aaOutput[i];
116431        nOut = pTS->anOutput[i];
116432        pTS->aaOutput[i] = 0;
116433      }else{
116434        int nNew;
116435        char *aNew;
116436
116437        int rc = fts3DoclistOrMerge(p->bDescIdx,
116438            pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
116439        );
116440        if( rc!=SQLITE_OK ){
116441          sqlite3_free(aOut);
116442          return rc;
116443        }
116444
116445        sqlite3_free(pTS->aaOutput[i]);
116446        sqlite3_free(aOut);
116447        pTS->aaOutput[i] = 0;
116448        aOut = aNew;
116449        nOut = nNew;
116450      }
116451    }
116452  }
116453
116454  pTS->aaOutput[0] = aOut;
116455  pTS->anOutput[0] = nOut;
116456  return SQLITE_OK;
116457}
116458
116459/*
116460** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
116461** as the first argument. The merge is an "OR" merge (see function
116462** fts3DoclistOrMerge() for details).
116463**
116464** This function is called with the doclist for each term that matches
116465** a queried prefix. It merges all these doclists into one, the doclist
116466** for the specified prefix. Since there can be a very large number of
116467** doclists to merge, the merging is done pair-wise using the TermSelect
116468** object.
116469**
116470** This function returns SQLITE_OK if the merge is successful, or an
116471** SQLite error code (SQLITE_NOMEM) if an error occurs.
116472*/
116473static int fts3TermSelectMerge(
116474  Fts3Table *p,                   /* FTS table handle */
116475  TermSelect *pTS,                /* TermSelect object to merge into */
116476  char *aDoclist,                 /* Pointer to doclist */
116477  int nDoclist                    /* Size of aDoclist in bytes */
116478){
116479  if( pTS->aaOutput[0]==0 ){
116480    /* If this is the first term selected, copy the doclist to the output
116481    ** buffer using memcpy(). */
116482    pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
116483    pTS->anOutput[0] = nDoclist;
116484    if( pTS->aaOutput[0] ){
116485      memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
116486    }else{
116487      return SQLITE_NOMEM;
116488    }
116489  }else{
116490    char *aMerge = aDoclist;
116491    int nMerge = nDoclist;
116492    int iOut;
116493
116494    for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
116495      if( pTS->aaOutput[iOut]==0 ){
116496        assert( iOut>0 );
116497        pTS->aaOutput[iOut] = aMerge;
116498        pTS->anOutput[iOut] = nMerge;
116499        break;
116500      }else{
116501        char *aNew;
116502        int nNew;
116503
116504        int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
116505            pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
116506        );
116507        if( rc!=SQLITE_OK ){
116508          if( aMerge!=aDoclist ) sqlite3_free(aMerge);
116509          return rc;
116510        }
116511
116512        if( aMerge!=aDoclist ) sqlite3_free(aMerge);
116513        sqlite3_free(pTS->aaOutput[iOut]);
116514        pTS->aaOutput[iOut] = 0;
116515
116516        aMerge = aNew;
116517        nMerge = nNew;
116518        if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
116519          pTS->aaOutput[iOut] = aMerge;
116520          pTS->anOutput[iOut] = nMerge;
116521        }
116522      }
116523    }
116524  }
116525  return SQLITE_OK;
116526}
116527
116528/*
116529** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
116530*/
116531static int fts3SegReaderCursorAppend(
116532  Fts3MultiSegReader *pCsr,
116533  Fts3SegReader *pNew
116534){
116535  if( (pCsr->nSegment%16)==0 ){
116536    Fts3SegReader **apNew;
116537    int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
116538    apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
116539    if( !apNew ){
116540      sqlite3Fts3SegReaderFree(pNew);
116541      return SQLITE_NOMEM;
116542    }
116543    pCsr->apSegment = apNew;
116544  }
116545  pCsr->apSegment[pCsr->nSegment++] = pNew;
116546  return SQLITE_OK;
116547}
116548
116549/*
116550** Add seg-reader objects to the Fts3MultiSegReader object passed as the
116551** 8th argument.
116552**
116553** This function returns SQLITE_OK if successful, or an SQLite error code
116554** otherwise.
116555*/
116556static int fts3SegReaderCursor(
116557  Fts3Table *p,                   /* FTS3 table handle */
116558  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
116559  int iLevel,                     /* Level of segments to scan */
116560  const char *zTerm,              /* Term to query for */
116561  int nTerm,                      /* Size of zTerm in bytes */
116562  int isPrefix,                   /* True for a prefix search */
116563  int isScan,                     /* True to scan from zTerm to EOF */
116564  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
116565){
116566  int rc = SQLITE_OK;             /* Error code */
116567  sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
116568  int rc2;                        /* Result of sqlite3_reset() */
116569
116570  /* If iLevel is less than 0 and this is not a scan, include a seg-reader
116571  ** for the pending-terms. If this is a scan, then this call must be being
116572  ** made by an fts4aux module, not an FTS table. In this case calling
116573  ** Fts3SegReaderPending might segfault, as the data structures used by
116574  ** fts4aux are not completely populated. So it's easiest to filter these
116575  ** calls out here.  */
116576  if( iLevel<0 && p->aIndex ){
116577    Fts3SegReader *pSeg = 0;
116578    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
116579    if( rc==SQLITE_OK && pSeg ){
116580      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
116581    }
116582  }
116583
116584  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
116585    if( rc==SQLITE_OK ){
116586      rc = sqlite3Fts3AllSegdirs(p, iIndex, iLevel, &pStmt);
116587    }
116588
116589    while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
116590      Fts3SegReader *pSeg = 0;
116591
116592      /* Read the values returned by the SELECT into local variables. */
116593      sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
116594      sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
116595      sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
116596      int nRoot = sqlite3_column_bytes(pStmt, 4);
116597      char const *zRoot = sqlite3_column_blob(pStmt, 4);
116598
116599      /* If zTerm is not NULL, and this segment is not stored entirely on its
116600      ** root node, the range of leaves scanned can be reduced. Do this. */
116601      if( iStartBlock && zTerm ){
116602        sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
116603        rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
116604        if( rc!=SQLITE_OK ) goto finished;
116605        if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
116606      }
116607
116608      rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
116609          iStartBlock, iLeavesEndBlock, iEndBlock, zRoot, nRoot, &pSeg
116610      );
116611      if( rc!=SQLITE_OK ) goto finished;
116612      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
116613    }
116614  }
116615
116616 finished:
116617  rc2 = sqlite3_reset(pStmt);
116618  if( rc==SQLITE_DONE ) rc = rc2;
116619
116620  return rc;
116621}
116622
116623/*
116624** Set up a cursor object for iterating through a full-text index or a
116625** single level therein.
116626*/
116627SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
116628  Fts3Table *p,                   /* FTS3 table handle */
116629  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
116630  int iLevel,                     /* Level of segments to scan */
116631  const char *zTerm,              /* Term to query for */
116632  int nTerm,                      /* Size of zTerm in bytes */
116633  int isPrefix,                   /* True for a prefix search */
116634  int isScan,                     /* True to scan from zTerm to EOF */
116635  Fts3MultiSegReader *pCsr       /* Cursor object to populate */
116636){
116637  assert( iIndex>=0 && iIndex<p->nIndex );
116638  assert( iLevel==FTS3_SEGCURSOR_ALL
116639      ||  iLevel==FTS3_SEGCURSOR_PENDING
116640      ||  iLevel>=0
116641  );
116642  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
116643  assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
116644  assert( isPrefix==0 || isScan==0 );
116645
116646  /* "isScan" is only set to true by the ft4aux module, an ordinary
116647  ** full-text tables. */
116648  assert( isScan==0 || p->aIndex==0 );
116649
116650  memset(pCsr, 0, sizeof(Fts3MultiSegReader));
116651
116652  return fts3SegReaderCursor(
116653      p, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
116654  );
116655}
116656
116657/*
116658** In addition to its current configuration, have the Fts3MultiSegReader
116659** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
116660**
116661** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
116662*/
116663static int fts3SegReaderCursorAddZero(
116664  Fts3Table *p,                   /* FTS virtual table handle */
116665  const char *zTerm,              /* Term to scan doclist of */
116666  int nTerm,                      /* Number of bytes in zTerm */
116667  Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
116668){
116669  return fts3SegReaderCursor(p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr);
116670}
116671
116672/*
116673** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
116674** if isPrefix is true, to scan the doclist for all terms for which
116675** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
116676** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
116677** an SQLite error code.
116678**
116679** It is the responsibility of the caller to free this object by eventually
116680** passing it to fts3SegReaderCursorFree()
116681**
116682** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
116683** Output parameter *ppSegcsr is set to 0 if an error occurs.
116684*/
116685static int fts3TermSegReaderCursor(
116686  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
116687  const char *zTerm,              /* Term to query for */
116688  int nTerm,                      /* Size of zTerm in bytes */
116689  int isPrefix,                   /* True for a prefix search */
116690  Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
116691){
116692  Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
116693  int rc = SQLITE_NOMEM;          /* Return code */
116694
116695  pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
116696  if( pSegcsr ){
116697    int i;
116698    int bFound = 0;               /* True once an index has been found */
116699    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
116700
116701    if( isPrefix ){
116702      for(i=1; bFound==0 && i<p->nIndex; i++){
116703        if( p->aIndex[i].nPrefix==nTerm ){
116704          bFound = 1;
116705          rc = sqlite3Fts3SegReaderCursor(
116706              p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr);
116707          pSegcsr->bLookup = 1;
116708        }
116709      }
116710
116711      for(i=1; bFound==0 && i<p->nIndex; i++){
116712        if( p->aIndex[i].nPrefix==nTerm+1 ){
116713          bFound = 1;
116714          rc = sqlite3Fts3SegReaderCursor(
116715              p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
116716          );
116717          if( rc==SQLITE_OK ){
116718            rc = fts3SegReaderCursorAddZero(p, zTerm, nTerm, pSegcsr);
116719          }
116720        }
116721      }
116722    }
116723
116724    if( bFound==0 ){
116725      rc = sqlite3Fts3SegReaderCursor(
116726          p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
116727      );
116728      pSegcsr->bLookup = !isPrefix;
116729    }
116730  }
116731
116732  *ppSegcsr = pSegcsr;
116733  return rc;
116734}
116735
116736/*
116737** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
116738*/
116739static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
116740  sqlite3Fts3SegReaderFinish(pSegcsr);
116741  sqlite3_free(pSegcsr);
116742}
116743
116744/*
116745** This function retreives the doclist for the specified term (or term
116746** prefix) from the database.
116747*/
116748static int fts3TermSelect(
116749  Fts3Table *p,                   /* Virtual table handle */
116750  Fts3PhraseToken *pTok,          /* Token to query for */
116751  int iColumn,                    /* Column to query (or -ve for all columns) */
116752  int *pnOut,                     /* OUT: Size of buffer at *ppOut */
116753  char **ppOut                    /* OUT: Malloced result buffer */
116754){
116755  int rc;                         /* Return code */
116756  Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
116757  TermSelect tsc;                 /* Object for pair-wise doclist merging */
116758  Fts3SegFilter filter;           /* Segment term filter configuration */
116759
116760  pSegcsr = pTok->pSegcsr;
116761  memset(&tsc, 0, sizeof(TermSelect));
116762
116763  filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
116764        | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
116765        | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
116766  filter.iCol = iColumn;
116767  filter.zTerm = pTok->z;
116768  filter.nTerm = pTok->n;
116769
116770  rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
116771  while( SQLITE_OK==rc
116772      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
116773  ){
116774    rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
116775  }
116776
116777  if( rc==SQLITE_OK ){
116778    rc = fts3TermSelectFinishMerge(p, &tsc);
116779  }
116780  if( rc==SQLITE_OK ){
116781    *ppOut = tsc.aaOutput[0];
116782    *pnOut = tsc.anOutput[0];
116783  }else{
116784    int i;
116785    for(i=0; i<SizeofArray(tsc.aaOutput); i++){
116786      sqlite3_free(tsc.aaOutput[i]);
116787    }
116788  }
116789
116790  fts3SegReaderCursorFree(pSegcsr);
116791  pTok->pSegcsr = 0;
116792  return rc;
116793}
116794
116795/*
116796** This function counts the total number of docids in the doclist stored
116797** in buffer aList[], size nList bytes.
116798**
116799** If the isPoslist argument is true, then it is assumed that the doclist
116800** contains a position-list following each docid. Otherwise, it is assumed
116801** that the doclist is simply a list of docids stored as delta encoded
116802** varints.
116803*/
116804static int fts3DoclistCountDocids(char *aList, int nList){
116805  int nDoc = 0;                   /* Return value */
116806  if( aList ){
116807    char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
116808    char *p = aList;              /* Cursor */
116809    while( p<aEnd ){
116810      nDoc++;
116811      while( (*p++)&0x80 );     /* Skip docid varint */
116812      fts3PoslistCopy(0, &p);   /* Skip over position list */
116813    }
116814  }
116815
116816  return nDoc;
116817}
116818
116819/*
116820** Advance the cursor to the next row in the %_content table that
116821** matches the search criteria.  For a MATCH search, this will be
116822** the next row that matches. For a full-table scan, this will be
116823** simply the next row in the %_content table.  For a docid lookup,
116824** this routine simply sets the EOF flag.
116825**
116826** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
116827** even if we reach end-of-file.  The fts3EofMethod() will be called
116828** subsequently to determine whether or not an EOF was hit.
116829*/
116830static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
116831  int rc;
116832  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
116833  if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
116834    if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
116835      pCsr->isEof = 1;
116836      rc = sqlite3_reset(pCsr->pStmt);
116837    }else{
116838      pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
116839      rc = SQLITE_OK;
116840    }
116841  }else{
116842    rc = fts3EvalNext((Fts3Cursor *)pCursor);
116843  }
116844  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
116845  return rc;
116846}
116847
116848/*
116849** This is the xFilter interface for the virtual table.  See
116850** the virtual table xFilter method documentation for additional
116851** information.
116852**
116853** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
116854** the %_content table.
116855**
116856** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
116857** in the %_content table.
116858**
116859** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
116860** column on the left-hand side of the MATCH operator is column
116861** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
116862** side of the MATCH operator.
116863*/
116864static int fts3FilterMethod(
116865  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
116866  int idxNum,                     /* Strategy index */
116867  const char *idxStr,             /* Unused */
116868  int nVal,                       /* Number of elements in apVal */
116869  sqlite3_value **apVal           /* Arguments for the indexing scheme */
116870){
116871  int rc;
116872  char *zSql;                     /* SQL statement used to access %_content */
116873  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
116874  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
116875
116876  UNUSED_PARAMETER(idxStr);
116877  UNUSED_PARAMETER(nVal);
116878
116879  assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
116880  assert( nVal==0 || nVal==1 );
116881  assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
116882  assert( p->pSegments==0 );
116883
116884  /* In case the cursor has been used before, clear it now. */
116885  sqlite3_finalize(pCsr->pStmt);
116886  sqlite3_free(pCsr->aDoclist);
116887  sqlite3Fts3ExprFree(pCsr->pExpr);
116888  memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
116889
116890  if( idxStr ){
116891    pCsr->bDesc = (idxStr[0]=='D');
116892  }else{
116893    pCsr->bDesc = p->bDescIdx;
116894  }
116895  pCsr->eSearch = (i16)idxNum;
116896
116897  if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
116898    int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
116899    const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
116900
116901    if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
116902      return SQLITE_NOMEM;
116903    }
116904
116905    rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn,
116906        iCol, zQuery, -1, &pCsr->pExpr
116907    );
116908    if( rc!=SQLITE_OK ){
116909      if( rc==SQLITE_ERROR ){
116910        static const char *zErr = "malformed MATCH expression: [%s]";
116911        p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
116912      }
116913      return rc;
116914    }
116915
116916    rc = sqlite3Fts3ReadLock(p);
116917    if( rc!=SQLITE_OK ) return rc;
116918
116919    rc = fts3EvalStart(pCsr);
116920
116921    sqlite3Fts3SegmentsClose(p);
116922    if( rc!=SQLITE_OK ) return rc;
116923    pCsr->pNextId = pCsr->aDoclist;
116924    pCsr->iPrevId = 0;
116925  }
116926
116927  /* Compile a SELECT statement for this cursor. For a full-table-scan, the
116928  ** statement loops through all rows of the %_content table. For a
116929  ** full-text query or docid lookup, the statement retrieves a single
116930  ** row by docid.
116931  */
116932  if( idxNum==FTS3_FULLSCAN_SEARCH ){
116933    const char *zSort = (pCsr->bDesc ? "DESC" : "ASC");
116934    const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x ORDER BY docid %s";
116935    zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName, zSort);
116936  }else{
116937    const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?";
116938    zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName);
116939  }
116940  if( !zSql ) return SQLITE_NOMEM;
116941  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
116942  sqlite3_free(zSql);
116943  if( rc!=SQLITE_OK ) return rc;
116944
116945  if( idxNum==FTS3_DOCID_SEARCH ){
116946    rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
116947    if( rc!=SQLITE_OK ) return rc;
116948  }
116949
116950  return fts3NextMethod(pCursor);
116951}
116952
116953/*
116954** This is the xEof method of the virtual table. SQLite calls this
116955** routine to find out if it has reached the end of a result set.
116956*/
116957static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
116958  return ((Fts3Cursor *)pCursor)->isEof;
116959}
116960
116961/*
116962** This is the xRowid method. The SQLite core calls this routine to
116963** retrieve the rowid for the current row of the result set. fts3
116964** exposes %_content.docid as the rowid for the virtual table. The
116965** rowid should be written to *pRowid.
116966*/
116967static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
116968  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
116969  *pRowid = pCsr->iPrevId;
116970  return SQLITE_OK;
116971}
116972
116973/*
116974** This is the xColumn method, called by SQLite to request a value from
116975** the row that the supplied cursor currently points to.
116976*/
116977static int fts3ColumnMethod(
116978  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
116979  sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
116980  int iCol                        /* Index of column to read value from */
116981){
116982  int rc = SQLITE_OK;             /* Return Code */
116983  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
116984  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
116985
116986  /* The column value supplied by SQLite must be in range. */
116987  assert( iCol>=0 && iCol<=p->nColumn+1 );
116988
116989  if( iCol==p->nColumn+1 ){
116990    /* This call is a request for the "docid" column. Since "docid" is an
116991    ** alias for "rowid", use the xRowid() method to obtain the value.
116992    */
116993    sqlite3_result_int64(pContext, pCsr->iPrevId);
116994  }else if( iCol==p->nColumn ){
116995    /* The extra column whose name is the same as the table.
116996    ** Return a blob which is a pointer to the cursor.
116997    */
116998    sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
116999  }else{
117000    rc = fts3CursorSeek(0, pCsr);
117001    if( rc==SQLITE_OK ){
117002      sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
117003    }
117004  }
117005
117006  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
117007  return rc;
117008}
117009
117010/*
117011** This function is the implementation of the xUpdate callback used by
117012** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
117013** inserted, updated or deleted.
117014*/
117015static int fts3UpdateMethod(
117016  sqlite3_vtab *pVtab,            /* Virtual table handle */
117017  int nArg,                       /* Size of argument array */
117018  sqlite3_value **apVal,          /* Array of arguments */
117019  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
117020){
117021  return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
117022}
117023
117024/*
117025** Implementation of xSync() method. Flush the contents of the pending-terms
117026** hash-table to the database.
117027*/
117028static int fts3SyncMethod(sqlite3_vtab *pVtab){
117029  int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
117030  sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
117031  return rc;
117032}
117033
117034/*
117035** Implementation of xBegin() method. This is a no-op.
117036*/
117037static int fts3BeginMethod(sqlite3_vtab *pVtab){
117038  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
117039  UNUSED_PARAMETER(pVtab);
117040  assert( p->pSegments==0 );
117041  assert( p->nPendingData==0 );
117042  assert( p->inTransaction!=1 );
117043  TESTONLY( p->inTransaction = 1 );
117044  TESTONLY( p->mxSavepoint = -1; );
117045  return SQLITE_OK;
117046}
117047
117048/*
117049** Implementation of xCommit() method. This is a no-op. The contents of
117050** the pending-terms hash-table have already been flushed into the database
117051** by fts3SyncMethod().
117052*/
117053static int fts3CommitMethod(sqlite3_vtab *pVtab){
117054  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
117055  UNUSED_PARAMETER(pVtab);
117056  assert( p->nPendingData==0 );
117057  assert( p->inTransaction!=0 );
117058  assert( p->pSegments==0 );
117059  TESTONLY( p->inTransaction = 0 );
117060  TESTONLY( p->mxSavepoint = -1; );
117061  return SQLITE_OK;
117062}
117063
117064/*
117065** Implementation of xRollback(). Discard the contents of the pending-terms
117066** hash-table. Any changes made to the database are reverted by SQLite.
117067*/
117068static int fts3RollbackMethod(sqlite3_vtab *pVtab){
117069  Fts3Table *p = (Fts3Table*)pVtab;
117070  sqlite3Fts3PendingTermsClear(p);
117071  assert( p->inTransaction!=0 );
117072  TESTONLY( p->inTransaction = 0 );
117073  TESTONLY( p->mxSavepoint = -1; );
117074  return SQLITE_OK;
117075}
117076
117077/*
117078** When called, *ppPoslist must point to the byte immediately following the
117079** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
117080** moves *ppPoslist so that it instead points to the first byte of the
117081** same position list.
117082*/
117083static void fts3ReversePoslist(char *pStart, char **ppPoslist){
117084  char *p = &(*ppPoslist)[-2];
117085  char c;
117086
117087  while( p>pStart && (c=*p--)==0 );
117088  while( p>pStart && (*p & 0x80) | c ){
117089    c = *p--;
117090  }
117091  if( p>pStart ){ p = &p[2]; }
117092  while( *p++&0x80 );
117093  *ppPoslist = p;
117094}
117095
117096/*
117097** Helper function used by the implementation of the overloaded snippet(),
117098** offsets() and optimize() SQL functions.
117099**
117100** If the value passed as the third argument is a blob of size
117101** sizeof(Fts3Cursor*), then the blob contents are copied to the
117102** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
117103** message is written to context pContext and SQLITE_ERROR returned. The
117104** string passed via zFunc is used as part of the error message.
117105*/
117106static int fts3FunctionArg(
117107  sqlite3_context *pContext,      /* SQL function call context */
117108  const char *zFunc,              /* Function name */
117109  sqlite3_value *pVal,            /* argv[0] passed to function */
117110  Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
117111){
117112  Fts3Cursor *pRet;
117113  if( sqlite3_value_type(pVal)!=SQLITE_BLOB
117114   || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
117115  ){
117116    char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
117117    sqlite3_result_error(pContext, zErr, -1);
117118    sqlite3_free(zErr);
117119    return SQLITE_ERROR;
117120  }
117121  memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
117122  *ppCsr = pRet;
117123  return SQLITE_OK;
117124}
117125
117126/*
117127** Implementation of the snippet() function for FTS3
117128*/
117129static void fts3SnippetFunc(
117130  sqlite3_context *pContext,      /* SQLite function call context */
117131  int nVal,                       /* Size of apVal[] array */
117132  sqlite3_value **apVal           /* Array of arguments */
117133){
117134  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
117135  const char *zStart = "<b>";
117136  const char *zEnd = "</b>";
117137  const char *zEllipsis = "<b>...</b>";
117138  int iCol = -1;
117139  int nToken = 15;                /* Default number of tokens in snippet */
117140
117141  /* There must be at least one argument passed to this function (otherwise
117142  ** the non-overloaded version would have been called instead of this one).
117143  */
117144  assert( nVal>=1 );
117145
117146  if( nVal>6 ){
117147    sqlite3_result_error(pContext,
117148        "wrong number of arguments to function snippet()", -1);
117149    return;
117150  }
117151  if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
117152
117153  switch( nVal ){
117154    case 6: nToken = sqlite3_value_int(apVal[5]);
117155    case 5: iCol = sqlite3_value_int(apVal[4]);
117156    case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
117157    case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
117158    case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
117159  }
117160  if( !zEllipsis || !zEnd || !zStart ){
117161    sqlite3_result_error_nomem(pContext);
117162  }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
117163    sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
117164  }
117165}
117166
117167/*
117168** Implementation of the offsets() function for FTS3
117169*/
117170static void fts3OffsetsFunc(
117171  sqlite3_context *pContext,      /* SQLite function call context */
117172  int nVal,                       /* Size of argument array */
117173  sqlite3_value **apVal           /* Array of arguments */
117174){
117175  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
117176
117177  UNUSED_PARAMETER(nVal);
117178
117179  assert( nVal==1 );
117180  if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
117181  assert( pCsr );
117182  if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
117183    sqlite3Fts3Offsets(pContext, pCsr);
117184  }
117185}
117186
117187/*
117188** Implementation of the special optimize() function for FTS3. This
117189** function merges all segments in the database to a single segment.
117190** Example usage is:
117191**
117192**   SELECT optimize(t) FROM t LIMIT 1;
117193**
117194** where 't' is the name of an FTS3 table.
117195*/
117196static void fts3OptimizeFunc(
117197  sqlite3_context *pContext,      /* SQLite function call context */
117198  int nVal,                       /* Size of argument array */
117199  sqlite3_value **apVal           /* Array of arguments */
117200){
117201  int rc;                         /* Return code */
117202  Fts3Table *p;                   /* Virtual table handle */
117203  Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
117204
117205  UNUSED_PARAMETER(nVal);
117206
117207  assert( nVal==1 );
117208  if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
117209  p = (Fts3Table *)pCursor->base.pVtab;
117210  assert( p );
117211
117212  rc = sqlite3Fts3Optimize(p);
117213
117214  switch( rc ){
117215    case SQLITE_OK:
117216      sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
117217      break;
117218    case SQLITE_DONE:
117219      sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
117220      break;
117221    default:
117222      sqlite3_result_error_code(pContext, rc);
117223      break;
117224  }
117225}
117226
117227/*
117228** Implementation of the matchinfo() function for FTS3
117229*/
117230static void fts3MatchinfoFunc(
117231  sqlite3_context *pContext,      /* SQLite function call context */
117232  int nVal,                       /* Size of argument array */
117233  sqlite3_value **apVal           /* Array of arguments */
117234){
117235  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
117236  assert( nVal==1 || nVal==2 );
117237  if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
117238    const char *zArg = 0;
117239    if( nVal>1 ){
117240      zArg = (const char *)sqlite3_value_text(apVal[1]);
117241    }
117242    sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
117243  }
117244}
117245
117246/*
117247** This routine implements the xFindFunction method for the FTS3
117248** virtual table.
117249*/
117250static int fts3FindFunctionMethod(
117251  sqlite3_vtab *pVtab,            /* Virtual table handle */
117252  int nArg,                       /* Number of SQL function arguments */
117253  const char *zName,              /* Name of SQL function */
117254  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
117255  void **ppArg                    /* Unused */
117256){
117257  struct Overloaded {
117258    const char *zName;
117259    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
117260  } aOverload[] = {
117261    { "snippet", fts3SnippetFunc },
117262    { "offsets", fts3OffsetsFunc },
117263    { "optimize", fts3OptimizeFunc },
117264    { "matchinfo", fts3MatchinfoFunc },
117265  };
117266  int i;                          /* Iterator variable */
117267
117268  UNUSED_PARAMETER(pVtab);
117269  UNUSED_PARAMETER(nArg);
117270  UNUSED_PARAMETER(ppArg);
117271
117272  for(i=0; i<SizeofArray(aOverload); i++){
117273    if( strcmp(zName, aOverload[i].zName)==0 ){
117274      *pxFunc = aOverload[i].xFunc;
117275      return 1;
117276    }
117277  }
117278
117279  /* No function of the specified name was found. Return 0. */
117280  return 0;
117281}
117282
117283/*
117284** Implementation of FTS3 xRename method. Rename an fts3 table.
117285*/
117286static int fts3RenameMethod(
117287  sqlite3_vtab *pVtab,            /* Virtual table handle */
117288  const char *zName               /* New name of table */
117289){
117290  Fts3Table *p = (Fts3Table *)pVtab;
117291  sqlite3 *db = p->db;            /* Database connection */
117292  int rc;                         /* Return Code */
117293
117294  rc = sqlite3Fts3PendingTermsFlush(p);
117295  if( rc!=SQLITE_OK ){
117296    return rc;
117297  }
117298
117299  fts3DbExec(&rc, db,
117300    "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
117301    p->zDb, p->zName, zName
117302  );
117303  if( p->bHasDocsize ){
117304    fts3DbExec(&rc, db,
117305      "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
117306      p->zDb, p->zName, zName
117307    );
117308  }
117309  if( p->bHasStat ){
117310    fts3DbExec(&rc, db,
117311      "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
117312      p->zDb, p->zName, zName
117313    );
117314  }
117315  fts3DbExec(&rc, db,
117316    "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
117317    p->zDb, p->zName, zName
117318  );
117319  fts3DbExec(&rc, db,
117320    "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
117321    p->zDb, p->zName, zName
117322  );
117323  return rc;
117324}
117325
117326/*
117327** The xSavepoint() method.
117328**
117329** Flush the contents of the pending-terms table to disk.
117330*/
117331static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
117332  UNUSED_PARAMETER(iSavepoint);
117333  assert( ((Fts3Table *)pVtab)->inTransaction );
117334  assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
117335  TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
117336  return fts3SyncMethod(pVtab);
117337}
117338
117339/*
117340** The xRelease() method.
117341**
117342** This is a no-op.
117343*/
117344static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
117345  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
117346  UNUSED_PARAMETER(iSavepoint);
117347  UNUSED_PARAMETER(pVtab);
117348  assert( p->inTransaction );
117349  assert( p->mxSavepoint >= iSavepoint );
117350  TESTONLY( p->mxSavepoint = iSavepoint-1 );
117351  return SQLITE_OK;
117352}
117353
117354/*
117355** The xRollbackTo() method.
117356**
117357** Discard the contents of the pending terms table.
117358*/
117359static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
117360  Fts3Table *p = (Fts3Table*)pVtab;
117361  UNUSED_PARAMETER(iSavepoint);
117362  assert( p->inTransaction );
117363  assert( p->mxSavepoint >= iSavepoint );
117364  TESTONLY( p->mxSavepoint = iSavepoint );
117365  sqlite3Fts3PendingTermsClear(p);
117366  return SQLITE_OK;
117367}
117368
117369static const sqlite3_module fts3Module = {
117370  /* iVersion      */ 2,
117371  /* xCreate       */ fts3CreateMethod,
117372  /* xConnect      */ fts3ConnectMethod,
117373  /* xBestIndex    */ fts3BestIndexMethod,
117374  /* xDisconnect   */ fts3DisconnectMethod,
117375  /* xDestroy      */ fts3DestroyMethod,
117376  /* xOpen         */ fts3OpenMethod,
117377  /* xClose        */ fts3CloseMethod,
117378  /* xFilter       */ fts3FilterMethod,
117379  /* xNext         */ fts3NextMethod,
117380  /* xEof          */ fts3EofMethod,
117381  /* xColumn       */ fts3ColumnMethod,
117382  /* xRowid        */ fts3RowidMethod,
117383  /* xUpdate       */ fts3UpdateMethod,
117384  /* xBegin        */ fts3BeginMethod,
117385  /* xSync         */ fts3SyncMethod,
117386  /* xCommit       */ fts3CommitMethod,
117387  /* xRollback     */ fts3RollbackMethod,
117388  /* xFindFunction */ fts3FindFunctionMethod,
117389  /* xRename */       fts3RenameMethod,
117390  /* xSavepoint    */ fts3SavepointMethod,
117391  /* xRelease      */ fts3ReleaseMethod,
117392  /* xRollbackTo   */ fts3RollbackToMethod,
117393};
117394
117395/*
117396** This function is registered as the module destructor (called when an
117397** FTS3 enabled database connection is closed). It frees the memory
117398** allocated for the tokenizer hash table.
117399*/
117400static void hashDestroy(void *p){
117401  Fts3Hash *pHash = (Fts3Hash *)p;
117402  sqlite3Fts3HashClear(pHash);
117403  sqlite3_free(pHash);
117404}
117405
117406/*
117407** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
117408** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
117409** respectively. The following three forward declarations are for functions
117410** declared in these files used to retrieve the respective implementations.
117411**
117412** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
117413** to by the argument to point to the "simple" tokenizer implementation.
117414** And so on.
117415*/
117416SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
117417SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
117418#ifdef SQLITE_ENABLE_ICU
117419SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
117420#endif
117421
117422/*
117423** Initialise the fts3 extension. If this extension is built as part
117424** of the sqlite library, then this function is called directly by
117425** SQLite. If fts3 is built as a dynamically loadable extension, this
117426** function is called by the sqlite3_extension_init() entry point.
117427*/
117428SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
117429  int rc = SQLITE_OK;
117430  Fts3Hash *pHash = 0;
117431  const sqlite3_tokenizer_module *pSimple = 0;
117432  const sqlite3_tokenizer_module *pPorter = 0;
117433
117434#ifdef SQLITE_ENABLE_ICU
117435  const sqlite3_tokenizer_module *pIcu = 0;
117436  sqlite3Fts3IcuTokenizerModule(&pIcu);
117437#endif
117438
117439#ifdef SQLITE_TEST
117440  rc = sqlite3Fts3InitTerm(db);
117441  if( rc!=SQLITE_OK ) return rc;
117442#endif
117443
117444  rc = sqlite3Fts3InitAux(db);
117445  if( rc!=SQLITE_OK ) return rc;
117446
117447  sqlite3Fts3SimpleTokenizerModule(&pSimple);
117448  sqlite3Fts3PorterTokenizerModule(&pPorter);
117449
117450  /* Allocate and initialise the hash-table used to store tokenizers. */
117451  pHash = sqlite3_malloc(sizeof(Fts3Hash));
117452  if( !pHash ){
117453    rc = SQLITE_NOMEM;
117454  }else{
117455    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
117456  }
117457
117458  /* Load the built-in tokenizers into the hash table */
117459  if( rc==SQLITE_OK ){
117460    if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
117461     || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
117462#ifdef SQLITE_ENABLE_ICU
117463     || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
117464#endif
117465    ){
117466      rc = SQLITE_NOMEM;
117467    }
117468  }
117469
117470#ifdef SQLITE_TEST
117471  if( rc==SQLITE_OK ){
117472    rc = sqlite3Fts3ExprInitTestInterface(db);
117473  }
117474#endif
117475
117476  /* Create the virtual table wrapper around the hash-table and overload
117477  ** the two scalar functions. If this is successful, register the
117478  ** module with sqlite.
117479  */
117480  if( SQLITE_OK==rc
117481   && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
117482   && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
117483   && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
117484   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
117485   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
117486   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
117487  ){
117488    rc = sqlite3_create_module_v2(
117489        db, "fts3", &fts3Module, (void *)pHash, hashDestroy
117490    );
117491    if( rc==SQLITE_OK ){
117492      rc = sqlite3_create_module_v2(
117493          db, "fts4", &fts3Module, (void *)pHash, 0
117494      );
117495    }
117496    return rc;
117497  }
117498
117499  /* An error has occurred. Delete the hash table and return the error code. */
117500  assert( rc!=SQLITE_OK );
117501  if( pHash ){
117502    sqlite3Fts3HashClear(pHash);
117503    sqlite3_free(pHash);
117504  }
117505  return rc;
117506}
117507
117508/*
117509** Allocate an Fts3MultiSegReader for each token in the expression headed
117510** by pExpr.
117511**
117512** An Fts3SegReader object is a cursor that can seek or scan a range of
117513** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
117514** Fts3SegReader objects internally to provide an interface to seek or scan
117515** within the union of all segments of a b-tree. Hence the name.
117516**
117517** If the allocated Fts3MultiSegReader just seeks to a single entry in a
117518** segment b-tree (if the term is not a prefix or it is a prefix for which
117519** there exists prefix b-tree of the right length) then it may be traversed
117520** and merged incrementally. Otherwise, it has to be merged into an in-memory
117521** doclist and then traversed.
117522*/
117523static void fts3EvalAllocateReaders(
117524  Fts3Cursor *pCsr,               /* FTS cursor handle */
117525  Fts3Expr *pExpr,                /* Allocate readers for this expression */
117526  int *pnToken,                   /* OUT: Total number of tokens in phrase. */
117527  int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
117528  int *pRc                        /* IN/OUT: Error code */
117529){
117530  if( pExpr && SQLITE_OK==*pRc ){
117531    if( pExpr->eType==FTSQUERY_PHRASE ){
117532      int i;
117533      int nToken = pExpr->pPhrase->nToken;
117534      *pnToken += nToken;
117535      for(i=0; i<nToken; i++){
117536        Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
117537        int rc = fts3TermSegReaderCursor(pCsr,
117538            pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
117539        );
117540        if( rc!=SQLITE_OK ){
117541          *pRc = rc;
117542          return;
117543        }
117544      }
117545      assert( pExpr->pPhrase->iDoclistToken==0 );
117546      pExpr->pPhrase->iDoclistToken = -1;
117547    }else{
117548      *pnOr += (pExpr->eType==FTSQUERY_OR);
117549      fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
117550      fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
117551    }
117552  }
117553}
117554
117555/*
117556** Arguments pList/nList contain the doclist for token iToken of phrase p.
117557** It is merged into the main doclist stored in p->doclist.aAll/nAll.
117558**
117559** This function assumes that pList points to a buffer allocated using
117560** sqlite3_malloc(). This function takes responsibility for eventually
117561** freeing the buffer.
117562*/
117563static void fts3EvalPhraseMergeToken(
117564  Fts3Table *pTab,                /* FTS Table pointer */
117565  Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
117566  int iToken,                     /* Token pList/nList corresponds to */
117567  char *pList,                    /* Pointer to doclist */
117568  int nList                       /* Number of bytes in pList */
117569){
117570  assert( iToken!=p->iDoclistToken );
117571
117572  if( pList==0 ){
117573    sqlite3_free(p->doclist.aAll);
117574    p->doclist.aAll = 0;
117575    p->doclist.nAll = 0;
117576  }
117577
117578  else if( p->iDoclistToken<0 ){
117579    p->doclist.aAll = pList;
117580    p->doclist.nAll = nList;
117581  }
117582
117583  else if( p->doclist.aAll==0 ){
117584    sqlite3_free(pList);
117585  }
117586
117587  else {
117588    char *pLeft;
117589    char *pRight;
117590    int nLeft;
117591    int nRight;
117592    int nDiff;
117593
117594    if( p->iDoclistToken<iToken ){
117595      pLeft = p->doclist.aAll;
117596      nLeft = p->doclist.nAll;
117597      pRight = pList;
117598      nRight = nList;
117599      nDiff = iToken - p->iDoclistToken;
117600    }else{
117601      pRight = p->doclist.aAll;
117602      nRight = p->doclist.nAll;
117603      pLeft = pList;
117604      nLeft = nList;
117605      nDiff = p->iDoclistToken - iToken;
117606    }
117607
117608    fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
117609    sqlite3_free(pLeft);
117610    p->doclist.aAll = pRight;
117611    p->doclist.nAll = nRight;
117612  }
117613
117614  if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
117615}
117616
117617/*
117618** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
117619** does not take deferred tokens into account.
117620**
117621** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
117622*/
117623static int fts3EvalPhraseLoad(
117624  Fts3Cursor *pCsr,               /* FTS Cursor handle */
117625  Fts3Phrase *p                   /* Phrase object */
117626){
117627  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
117628  int iToken;
117629  int rc = SQLITE_OK;
117630
117631  for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
117632    Fts3PhraseToken *pToken = &p->aToken[iToken];
117633    assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
117634
117635    if( pToken->pSegcsr ){
117636      int nThis = 0;
117637      char *pThis = 0;
117638      rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
117639      if( rc==SQLITE_OK ){
117640        fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
117641      }
117642    }
117643    assert( pToken->pSegcsr==0 );
117644  }
117645
117646  return rc;
117647}
117648
117649/*
117650** This function is called on each phrase after the position lists for
117651** any deferred tokens have been loaded into memory. It updates the phrases
117652** current position list to include only those positions that are really
117653** instances of the phrase (after considering deferred tokens). If this
117654** means that the phrase does not appear in the current row, doclist.pList
117655** and doclist.nList are both zeroed.
117656**
117657** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
117658*/
117659static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
117660  int iToken;                     /* Used to iterate through phrase tokens */
117661  int rc = SQLITE_OK;             /* Return code */
117662  char *aPoslist = 0;             /* Position list for deferred tokens */
117663  int nPoslist = 0;               /* Number of bytes in aPoslist */
117664  int iPrev = -1;                 /* Token number of previous deferred token */
117665
117666  assert( pPhrase->doclist.bFreeList==0 );
117667
117668  for(iToken=0; rc==SQLITE_OK && iToken<pPhrase->nToken; iToken++){
117669    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
117670    Fts3DeferredToken *pDeferred = pToken->pDeferred;
117671
117672    if( pDeferred ){
117673      char *pList;
117674      int nList;
117675      rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
117676      if( rc!=SQLITE_OK ) return rc;
117677
117678      if( pList==0 ){
117679        sqlite3_free(aPoslist);
117680        pPhrase->doclist.pList = 0;
117681        pPhrase->doclist.nList = 0;
117682        return SQLITE_OK;
117683
117684      }else if( aPoslist==0 ){
117685        aPoslist = pList;
117686        nPoslist = nList;
117687
117688      }else{
117689        char *aOut = pList;
117690        char *p1 = aPoslist;
117691        char *p2 = aOut;
117692
117693        assert( iPrev>=0 );
117694        fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
117695        sqlite3_free(aPoslist);
117696        aPoslist = pList;
117697        nPoslist = aOut - aPoslist;
117698        if( nPoslist==0 ){
117699          sqlite3_free(aPoslist);
117700          pPhrase->doclist.pList = 0;
117701          pPhrase->doclist.nList = 0;
117702          return SQLITE_OK;
117703        }
117704      }
117705      iPrev = iToken;
117706    }
117707  }
117708
117709  if( iPrev>=0 ){
117710    int nMaxUndeferred = pPhrase->iDoclistToken;
117711    if( nMaxUndeferred<0 ){
117712      pPhrase->doclist.pList = aPoslist;
117713      pPhrase->doclist.nList = nPoslist;
117714      pPhrase->doclist.iDocid = pCsr->iPrevId;
117715      pPhrase->doclist.bFreeList = 1;
117716    }else{
117717      int nDistance;
117718      char *p1;
117719      char *p2;
117720      char *aOut;
117721
117722      if( nMaxUndeferred>iPrev ){
117723        p1 = aPoslist;
117724        p2 = pPhrase->doclist.pList;
117725        nDistance = nMaxUndeferred - iPrev;
117726      }else{
117727        p1 = pPhrase->doclist.pList;
117728        p2 = aPoslist;
117729        nDistance = iPrev - nMaxUndeferred;
117730      }
117731
117732      aOut = (char *)sqlite3_malloc(nPoslist+8);
117733      if( !aOut ){
117734        sqlite3_free(aPoslist);
117735        return SQLITE_NOMEM;
117736      }
117737
117738      pPhrase->doclist.pList = aOut;
117739      if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
117740        pPhrase->doclist.bFreeList = 1;
117741        pPhrase->doclist.nList = (aOut - pPhrase->doclist.pList);
117742      }else{
117743        sqlite3_free(aOut);
117744        pPhrase->doclist.pList = 0;
117745        pPhrase->doclist.nList = 0;
117746      }
117747      sqlite3_free(aPoslist);
117748    }
117749  }
117750
117751  return SQLITE_OK;
117752}
117753
117754/*
117755** This function is called for each Fts3Phrase in a full-text query
117756** expression to initialize the mechanism for returning rows. Once this
117757** function has been called successfully on an Fts3Phrase, it may be
117758** used with fts3EvalPhraseNext() to iterate through the matching docids.
117759**
117760** If parameter bOptOk is true, then the phrase may (or may not) use the
117761** incremental loading strategy. Otherwise, the entire doclist is loaded into
117762** memory within this call.
117763**
117764** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
117765*/
117766static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
117767  int rc;                         /* Error code */
117768  Fts3PhraseToken *pFirst = &p->aToken[0];
117769  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
117770
117771  if( pCsr->bDesc==pTab->bDescIdx
117772   && bOptOk==1
117773   && p->nToken==1
117774   && pFirst->pSegcsr
117775   && pFirst->pSegcsr->bLookup
117776  ){
117777    /* Use the incremental approach. */
117778    int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
117779    rc = sqlite3Fts3MsrIncrStart(
117780        pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
117781    p->bIncr = 1;
117782
117783  }else{
117784    /* Load the full doclist for the phrase into memory. */
117785    rc = fts3EvalPhraseLoad(pCsr, p);
117786    p->bIncr = 0;
117787  }
117788
117789  assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
117790  return rc;
117791}
117792
117793/*
117794** This function is used to iterate backwards (from the end to start)
117795** through doclists. It is used by this module to iterate through phrase
117796** doclists in reverse and by the fts3_write.c module to iterate through
117797** pending-terms lists when writing to databases with "order=desc".
117798**
117799** The doclist may be sorted in ascending (parameter bDescIdx==0) or
117800** descending (parameter bDescIdx==1) order of docid. Regardless, this
117801** function iterates from the end of the doclist to the beginning.
117802*/
117803SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
117804  int bDescIdx,                   /* True if the doclist is desc */
117805  char *aDoclist,                 /* Pointer to entire doclist */
117806  int nDoclist,                   /* Length of aDoclist in bytes */
117807  char **ppIter,                  /* IN/OUT: Iterator pointer */
117808  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
117809  int *pnList,                    /* IN/OUT: List length pointer */
117810  u8 *pbEof                       /* OUT: End-of-file flag */
117811){
117812  char *p = *ppIter;
117813
117814  assert( nDoclist>0 );
117815  assert( *pbEof==0 );
117816  assert( p || *piDocid==0 );
117817  assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
117818
117819  if( p==0 ){
117820    sqlite3_int64 iDocid = 0;
117821    char *pNext = 0;
117822    char *pDocid = aDoclist;
117823    char *pEnd = &aDoclist[nDoclist];
117824    int iMul = 1;
117825
117826    while( pDocid<pEnd ){
117827      sqlite3_int64 iDelta;
117828      pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
117829      iDocid += (iMul * iDelta);
117830      pNext = pDocid;
117831      fts3PoslistCopy(0, &pDocid);
117832      while( pDocid<pEnd && *pDocid==0 ) pDocid++;
117833      iMul = (bDescIdx ? -1 : 1);
117834    }
117835
117836    *pnList = pEnd - pNext;
117837    *ppIter = pNext;
117838    *piDocid = iDocid;
117839  }else{
117840    int iMul = (bDescIdx ? -1 : 1);
117841    sqlite3_int64 iDelta;
117842    fts3GetReverseVarint(&p, aDoclist, &iDelta);
117843    *piDocid -= (iMul * iDelta);
117844
117845    if( p==aDoclist ){
117846      *pbEof = 1;
117847    }else{
117848      char *pSave = p;
117849      fts3ReversePoslist(aDoclist, &p);
117850      *pnList = (pSave - p);
117851    }
117852    *ppIter = p;
117853  }
117854}
117855
117856/*
117857** Attempt to move the phrase iterator to point to the next matching docid.
117858** If an error occurs, return an SQLite error code. Otherwise, return
117859** SQLITE_OK.
117860**
117861** If there is no "next" entry and no error occurs, then *pbEof is set to
117862** 1 before returning. Otherwise, if no error occurs and the iterator is
117863** successfully advanced, *pbEof is set to 0.
117864*/
117865static int fts3EvalPhraseNext(
117866  Fts3Cursor *pCsr,               /* FTS Cursor handle */
117867  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
117868  u8 *pbEof                       /* OUT: Set to 1 if EOF */
117869){
117870  int rc = SQLITE_OK;
117871  Fts3Doclist *pDL = &p->doclist;
117872  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
117873
117874  if( p->bIncr ){
117875    assert( p->nToken==1 );
117876    assert( pDL->pNextDocid==0 );
117877    rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
117878        &pDL->iDocid, &pDL->pList, &pDL->nList
117879    );
117880    if( rc==SQLITE_OK && !pDL->pList ){
117881      *pbEof = 1;
117882    }
117883  }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
117884    sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
117885        &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
117886    );
117887    pDL->pList = pDL->pNextDocid;
117888  }else{
117889    char *pIter;                            /* Used to iterate through aAll */
117890    char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
117891    if( pDL->pNextDocid ){
117892      pIter = pDL->pNextDocid;
117893    }else{
117894      pIter = pDL->aAll;
117895    }
117896
117897    if( pIter>=pEnd ){
117898      /* We have already reached the end of this doclist. EOF. */
117899      *pbEof = 1;
117900    }else{
117901      sqlite3_int64 iDelta;
117902      pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
117903      if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
117904        pDL->iDocid += iDelta;
117905      }else{
117906        pDL->iDocid -= iDelta;
117907      }
117908      pDL->pList = pIter;
117909      fts3PoslistCopy(0, &pIter);
117910      pDL->nList = (pIter - pDL->pList);
117911
117912      /* pIter now points just past the 0x00 that terminates the position-
117913      ** list for document pDL->iDocid. However, if this position-list was
117914      ** edited in place by fts3EvalNearTrim(), then pIter may not actually
117915      ** point to the start of the next docid value. The following line deals
117916      ** with this case by advancing pIter past the zero-padding added by
117917      ** fts3EvalNearTrim().  */
117918      while( pIter<pEnd && *pIter==0 ) pIter++;
117919
117920      pDL->pNextDocid = pIter;
117921      assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
117922      *pbEof = 0;
117923    }
117924  }
117925
117926  return rc;
117927}
117928
117929/*
117930**
117931** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
117932** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
117933** expression. Also the Fts3Expr.bDeferred variable is set to true for any
117934** expressions for which all descendent tokens are deferred.
117935**
117936** If parameter bOptOk is zero, then it is guaranteed that the
117937** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
117938** each phrase in the expression (subject to deferred token processing).
117939** Or, if bOptOk is non-zero, then one or more tokens within the expression
117940** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
117941**
117942** If an error occurs within this function, *pRc is set to an SQLite error
117943** code before returning.
117944*/
117945static void fts3EvalStartReaders(
117946  Fts3Cursor *pCsr,               /* FTS Cursor handle */
117947  Fts3Expr *pExpr,                /* Expression to initialize phrases in */
117948  int bOptOk,                     /* True to enable incremental loading */
117949  int *pRc                        /* IN/OUT: Error code */
117950){
117951  if( pExpr && SQLITE_OK==*pRc ){
117952    if( pExpr->eType==FTSQUERY_PHRASE ){
117953      int i;
117954      int nToken = pExpr->pPhrase->nToken;
117955      for(i=0; i<nToken; i++){
117956        if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
117957      }
117958      pExpr->bDeferred = (i==nToken);
117959      *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
117960    }else{
117961      fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
117962      fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
117963      pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
117964    }
117965  }
117966}
117967
117968/*
117969** An array of the following structures is assembled as part of the process
117970** of selecting tokens to defer before the query starts executing (as part
117971** of the xFilter() method). There is one element in the array for each
117972** token in the FTS expression.
117973**
117974** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
117975** to phrases that are connected only by AND and NEAR operators (not OR or
117976** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
117977** separately. The root of a tokens AND/NEAR cluster is stored in
117978** Fts3TokenAndCost.pRoot.
117979*/
117980typedef struct Fts3TokenAndCost Fts3TokenAndCost;
117981struct Fts3TokenAndCost {
117982  Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
117983  int iToken;                     /* Position of token in phrase */
117984  Fts3PhraseToken *pToken;        /* The token itself */
117985  Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
117986  int nOvfl;                      /* Number of overflow pages to load doclist */
117987  int iCol;                       /* The column the token must match */
117988};
117989
117990/*
117991** This function is used to populate an allocated Fts3TokenAndCost array.
117992**
117993** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
117994** Otherwise, if an error occurs during execution, *pRc is set to an
117995** SQLite error code.
117996*/
117997static void fts3EvalTokenCosts(
117998  Fts3Cursor *pCsr,               /* FTS Cursor handle */
117999  Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
118000  Fts3Expr *pExpr,                /* Expression to consider */
118001  Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
118002  Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
118003  int *pRc                        /* IN/OUT: Error code */
118004){
118005  if( *pRc==SQLITE_OK && pExpr ){
118006    if( pExpr->eType==FTSQUERY_PHRASE ){
118007      Fts3Phrase *pPhrase = pExpr->pPhrase;
118008      int i;
118009      for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
118010        Fts3TokenAndCost *pTC = (*ppTC)++;
118011        pTC->pPhrase = pPhrase;
118012        pTC->iToken = i;
118013        pTC->pRoot = pRoot;
118014        pTC->pToken = &pPhrase->aToken[i];
118015        pTC->iCol = pPhrase->iColumn;
118016        *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
118017      }
118018    }else if( pExpr->eType!=FTSQUERY_NOT ){
118019      if( pExpr->eType==FTSQUERY_OR ){
118020        pRoot = pExpr->pLeft;
118021        **ppOr = pRoot;
118022        (*ppOr)++;
118023      }
118024      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
118025      if( pExpr->eType==FTSQUERY_OR ){
118026        pRoot = pExpr->pRight;
118027        **ppOr = pRoot;
118028        (*ppOr)++;
118029      }
118030      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
118031    }
118032  }
118033}
118034
118035/*
118036** Determine the average document (row) size in pages. If successful,
118037** write this value to *pnPage and return SQLITE_OK. Otherwise, return
118038** an SQLite error code.
118039**
118040** The average document size in pages is calculated by first calculating
118041** determining the average size in bytes, B. If B is less than the amount
118042** of data that will fit on a single leaf page of an intkey table in
118043** this database, then the average docsize is 1. Otherwise, it is 1 plus
118044** the number of overflow pages consumed by a record B bytes in size.
118045*/
118046static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
118047  if( pCsr->nRowAvg==0 ){
118048    /* The average document size, which is required to calculate the cost
118049    ** of each doclist, has not yet been determined. Read the required
118050    ** data from the %_stat table to calculate it.
118051    **
118052    ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
118053    ** varints, where nCol is the number of columns in the FTS3 table.
118054    ** The first varint is the number of documents currently stored in
118055    ** the table. The following nCol varints contain the total amount of
118056    ** data stored in all rows of each column of the table, from left
118057    ** to right.
118058    */
118059    int rc;
118060    Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
118061    sqlite3_stmt *pStmt;
118062    sqlite3_int64 nDoc = 0;
118063    sqlite3_int64 nByte = 0;
118064    const char *pEnd;
118065    const char *a;
118066
118067    rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
118068    if( rc!=SQLITE_OK ) return rc;
118069    a = sqlite3_column_blob(pStmt, 0);
118070    assert( a );
118071
118072    pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
118073    a += sqlite3Fts3GetVarint(a, &nDoc);
118074    while( a<pEnd ){
118075      a += sqlite3Fts3GetVarint(a, &nByte);
118076    }
118077    if( nDoc==0 || nByte==0 ){
118078      sqlite3_reset(pStmt);
118079      return SQLITE_CORRUPT_VTAB;
118080    }
118081
118082    pCsr->nDoc = nDoc;
118083    pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
118084    assert( pCsr->nRowAvg>0 );
118085    rc = sqlite3_reset(pStmt);
118086    if( rc!=SQLITE_OK ) return rc;
118087  }
118088
118089  *pnPage = pCsr->nRowAvg;
118090  return SQLITE_OK;
118091}
118092
118093/*
118094** This function is called to select the tokens (if any) that will be
118095** deferred. The array aTC[] has already been populated when this is
118096** called.
118097**
118098** This function is called once for each AND/NEAR cluster in the
118099** expression. Each invocation determines which tokens to defer within
118100** the cluster with root node pRoot. See comments above the definition
118101** of struct Fts3TokenAndCost for more details.
118102**
118103** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
118104** called on each token to defer. Otherwise, an SQLite error code is
118105** returned.
118106*/
118107static int fts3EvalSelectDeferred(
118108  Fts3Cursor *pCsr,               /* FTS Cursor handle */
118109  Fts3Expr *pRoot,                /* Consider tokens with this root node */
118110  Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
118111  int nTC                         /* Number of entries in aTC[] */
118112){
118113  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
118114  int nDocSize = 0;               /* Number of pages per doc loaded */
118115  int rc = SQLITE_OK;             /* Return code */
118116  int ii;                         /* Iterator variable for various purposes */
118117  int nOvfl = 0;                  /* Total overflow pages used by doclists */
118118  int nToken = 0;                 /* Total number of tokens in cluster */
118119
118120  int nMinEst = 0;                /* The minimum count for any phrase so far. */
118121  int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
118122
118123  /* Count the tokens in this AND/NEAR cluster. If none of the doclists
118124  ** associated with the tokens spill onto overflow pages, or if there is
118125  ** only 1 token, exit early. No tokens to defer in this case. */
118126  for(ii=0; ii<nTC; ii++){
118127    if( aTC[ii].pRoot==pRoot ){
118128      nOvfl += aTC[ii].nOvfl;
118129      nToken++;
118130    }
118131  }
118132  if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
118133
118134  /* Obtain the average docsize (in pages). */
118135  rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
118136  assert( rc!=SQLITE_OK || nDocSize>0 );
118137
118138
118139  /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
118140  ** of the number of overflow pages that will be loaded by the pager layer
118141  ** to retrieve the entire doclist for the token from the full-text index.
118142  ** Load the doclists for tokens that are either:
118143  **
118144  **   a. The cheapest token in the entire query (i.e. the one visited by the
118145  **      first iteration of this loop), or
118146  **
118147  **   b. Part of a multi-token phrase.
118148  **
118149  ** After each token doclist is loaded, merge it with the others from the
118150  ** same phrase and count the number of documents that the merged doclist
118151  ** contains. Set variable "nMinEst" to the smallest number of documents in
118152  ** any phrase doclist for which 1 or more token doclists have been loaded.
118153  ** Let nOther be the number of other phrases for which it is certain that
118154  ** one or more tokens will not be deferred.
118155  **
118156  ** Then, for each token, defer it if loading the doclist would result in
118157  ** loading N or more overflow pages into memory, where N is computed as:
118158  **
118159  **    (nMinEst + 4^nOther - 1) / (4^nOther)
118160  */
118161  for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
118162    int iTC;                      /* Used to iterate through aTC[] array. */
118163    Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
118164
118165    /* Set pTC to point to the cheapest remaining token. */
118166    for(iTC=0; iTC<nTC; iTC++){
118167      if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
118168       && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
118169      ){
118170        pTC = &aTC[iTC];
118171      }
118172    }
118173    assert( pTC );
118174
118175    if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
118176      /* The number of overflow pages to load for this (and therefore all
118177      ** subsequent) tokens is greater than the estimated number of pages
118178      ** that will be loaded if all subsequent tokens are deferred.
118179      */
118180      Fts3PhraseToken *pToken = pTC->pToken;
118181      rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
118182      fts3SegReaderCursorFree(pToken->pSegcsr);
118183      pToken->pSegcsr = 0;
118184    }else{
118185      nLoad4 = nLoad4*4;
118186      if( ii==0 || pTC->pPhrase->nToken>1 ){
118187        /* Either this is the cheapest token in the entire query, or it is
118188        ** part of a multi-token phrase. Either way, the entire doclist will
118189        ** (eventually) be loaded into memory. It may as well be now. */
118190        Fts3PhraseToken *pToken = pTC->pToken;
118191        int nList = 0;
118192        char *pList = 0;
118193        rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
118194        assert( rc==SQLITE_OK || pList==0 );
118195        if( rc==SQLITE_OK ){
118196          int nCount;
118197          fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
118198          nCount = fts3DoclistCountDocids(
118199              pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
118200          );
118201          if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
118202        }
118203      }
118204    }
118205    pTC->pToken = 0;
118206  }
118207
118208  return rc;
118209}
118210
118211/*
118212** This function is called from within the xFilter method. It initializes
118213** the full-text query currently stored in pCsr->pExpr. To iterate through
118214** the results of a query, the caller does:
118215**
118216**    fts3EvalStart(pCsr);
118217**    while( 1 ){
118218**      fts3EvalNext(pCsr);
118219**      if( pCsr->bEof ) break;
118220**      ... return row pCsr->iPrevId to the caller ...
118221**    }
118222*/
118223static int fts3EvalStart(Fts3Cursor *pCsr){
118224  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
118225  int rc = SQLITE_OK;
118226  int nToken = 0;
118227  int nOr = 0;
118228
118229  /* Allocate a MultiSegReader for each token in the expression. */
118230  fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
118231
118232  /* Determine which, if any, tokens in the expression should be deferred. */
118233  if( rc==SQLITE_OK && nToken>1 && pTab->bHasStat ){
118234    Fts3TokenAndCost *aTC;
118235    Fts3Expr **apOr;
118236    aTC = (Fts3TokenAndCost *)sqlite3_malloc(
118237        sizeof(Fts3TokenAndCost) * nToken
118238      + sizeof(Fts3Expr *) * nOr * 2
118239    );
118240    apOr = (Fts3Expr **)&aTC[nToken];
118241
118242    if( !aTC ){
118243      rc = SQLITE_NOMEM;
118244    }else{
118245      int ii;
118246      Fts3TokenAndCost *pTC = aTC;
118247      Fts3Expr **ppOr = apOr;
118248
118249      fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
118250      nToken = pTC-aTC;
118251      nOr = ppOr-apOr;
118252
118253      if( rc==SQLITE_OK ){
118254        rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
118255        for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
118256          rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
118257        }
118258      }
118259
118260      sqlite3_free(aTC);
118261    }
118262  }
118263
118264  fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
118265  return rc;
118266}
118267
118268/*
118269** Invalidate the current position list for phrase pPhrase.
118270*/
118271static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
118272  if( pPhrase->doclist.bFreeList ){
118273    sqlite3_free(pPhrase->doclist.pList);
118274  }
118275  pPhrase->doclist.pList = 0;
118276  pPhrase->doclist.nList = 0;
118277  pPhrase->doclist.bFreeList = 0;
118278}
118279
118280/*
118281** This function is called to edit the position list associated with
118282** the phrase object passed as the fifth argument according to a NEAR
118283** condition. For example:
118284**
118285**     abc NEAR/5 "def ghi"
118286**
118287** Parameter nNear is passed the NEAR distance of the expression (5 in
118288** the example above). When this function is called, *paPoslist points to
118289** the position list, and *pnToken is the number of phrase tokens in, the
118290** phrase on the other side of the NEAR operator to pPhrase. For example,
118291** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
118292** the position list associated with phrase "abc".
118293**
118294** All positions in the pPhrase position list that are not sufficiently
118295** close to a position in the *paPoslist position list are removed. If this
118296** leaves 0 positions, zero is returned. Otherwise, non-zero.
118297**
118298** Before returning, *paPoslist is set to point to the position lsit
118299** associated with pPhrase. And *pnToken is set to the number of tokens in
118300** pPhrase.
118301*/
118302static int fts3EvalNearTrim(
118303  int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
118304  char *aTmp,                     /* Temporary space to use */
118305  char **paPoslist,               /* IN/OUT: Position list */
118306  int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
118307  Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
118308){
118309  int nParam1 = nNear + pPhrase->nToken;
118310  int nParam2 = nNear + *pnToken;
118311  int nNew;
118312  char *p2;
118313  char *pOut;
118314  int res;
118315
118316  assert( pPhrase->doclist.pList );
118317
118318  p2 = pOut = pPhrase->doclist.pList;
118319  res = fts3PoslistNearMerge(
118320    &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
118321  );
118322  if( res ){
118323    nNew = (pOut - pPhrase->doclist.pList) - 1;
118324    assert( pPhrase->doclist.pList[nNew]=='\0' );
118325    assert( nNew<=pPhrase->doclist.nList && nNew>0 );
118326    memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
118327    pPhrase->doclist.nList = nNew;
118328    *paPoslist = pPhrase->doclist.pList;
118329    *pnToken = pPhrase->nToken;
118330  }
118331
118332  return res;
118333}
118334
118335/*
118336** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
118337** Otherwise, it advances the expression passed as the second argument to
118338** point to the next matching row in the database. Expressions iterate through
118339** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
118340** or descending if it is non-zero.
118341**
118342** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
118343** successful, the following variables in pExpr are set:
118344**
118345**   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
118346**   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
118347**
118348** If the expression is of type FTSQUERY_PHRASE, and the expression is not
118349** at EOF, then the following variables are populated with the position list
118350** for the phrase for the visited row:
118351**
118352**   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
118353**   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
118354**
118355** It says above that this function advances the expression to the next
118356** matching row. This is usually true, but there are the following exceptions:
118357**
118358**   1. Deferred tokens are not taken into account. If a phrase consists
118359**      entirely of deferred tokens, it is assumed to match every row in
118360**      the db. In this case the position-list is not populated at all.
118361**
118362**      Or, if a phrase contains one or more deferred tokens and one or
118363**      more non-deferred tokens, then the expression is advanced to the
118364**      next possible match, considering only non-deferred tokens. In other
118365**      words, if the phrase is "A B C", and "B" is deferred, the expression
118366**      is advanced to the next row that contains an instance of "A * C",
118367**      where "*" may match any single token. The position list in this case
118368**      is populated as for "A * C" before returning.
118369**
118370**   2. NEAR is treated as AND. If the expression is "x NEAR y", it is
118371**      advanced to point to the next row that matches "x AND y".
118372**
118373** See fts3EvalTestDeferredAndNear() for details on testing if a row is
118374** really a match, taking into account deferred tokens and NEAR operators.
118375*/
118376static void fts3EvalNextRow(
118377  Fts3Cursor *pCsr,               /* FTS Cursor handle */
118378  Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
118379  int *pRc                        /* IN/OUT: Error code */
118380){
118381  if( *pRc==SQLITE_OK ){
118382    int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
118383    assert( pExpr->bEof==0 );
118384    pExpr->bStart = 1;
118385
118386    switch( pExpr->eType ){
118387      case FTSQUERY_NEAR:
118388      case FTSQUERY_AND: {
118389        Fts3Expr *pLeft = pExpr->pLeft;
118390        Fts3Expr *pRight = pExpr->pRight;
118391        assert( !pLeft->bDeferred || !pRight->bDeferred );
118392
118393        if( pLeft->bDeferred ){
118394          /* LHS is entirely deferred. So we assume it matches every row.
118395          ** Advance the RHS iterator to find the next row visited. */
118396          fts3EvalNextRow(pCsr, pRight, pRc);
118397          pExpr->iDocid = pRight->iDocid;
118398          pExpr->bEof = pRight->bEof;
118399        }else if( pRight->bDeferred ){
118400          /* RHS is entirely deferred. So we assume it matches every row.
118401          ** Advance the LHS iterator to find the next row visited. */
118402          fts3EvalNextRow(pCsr, pLeft, pRc);
118403          pExpr->iDocid = pLeft->iDocid;
118404          pExpr->bEof = pLeft->bEof;
118405        }else{
118406          /* Neither the RHS or LHS are deferred. */
118407          fts3EvalNextRow(pCsr, pLeft, pRc);
118408          fts3EvalNextRow(pCsr, pRight, pRc);
118409          while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
118410            sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
118411            if( iDiff==0 ) break;
118412            if( iDiff<0 ){
118413              fts3EvalNextRow(pCsr, pLeft, pRc);
118414            }else{
118415              fts3EvalNextRow(pCsr, pRight, pRc);
118416            }
118417          }
118418          pExpr->iDocid = pLeft->iDocid;
118419          pExpr->bEof = (pLeft->bEof || pRight->bEof);
118420        }
118421        break;
118422      }
118423
118424      case FTSQUERY_OR: {
118425        Fts3Expr *pLeft = pExpr->pLeft;
118426        Fts3Expr *pRight = pExpr->pRight;
118427        sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
118428
118429        assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
118430        assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
118431
118432        if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
118433          fts3EvalNextRow(pCsr, pLeft, pRc);
118434        }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
118435          fts3EvalNextRow(pCsr, pRight, pRc);
118436        }else{
118437          fts3EvalNextRow(pCsr, pLeft, pRc);
118438          fts3EvalNextRow(pCsr, pRight, pRc);
118439        }
118440
118441        pExpr->bEof = (pLeft->bEof && pRight->bEof);
118442        iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
118443        if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
118444          pExpr->iDocid = pLeft->iDocid;
118445        }else{
118446          pExpr->iDocid = pRight->iDocid;
118447        }
118448
118449        break;
118450      }
118451
118452      case FTSQUERY_NOT: {
118453        Fts3Expr *pLeft = pExpr->pLeft;
118454        Fts3Expr *pRight = pExpr->pRight;
118455
118456        if( pRight->bStart==0 ){
118457          fts3EvalNextRow(pCsr, pRight, pRc);
118458          assert( *pRc!=SQLITE_OK || pRight->bStart );
118459        }
118460
118461        fts3EvalNextRow(pCsr, pLeft, pRc);
118462        if( pLeft->bEof==0 ){
118463          while( !*pRc
118464              && !pRight->bEof
118465              && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
118466          ){
118467            fts3EvalNextRow(pCsr, pRight, pRc);
118468          }
118469        }
118470        pExpr->iDocid = pLeft->iDocid;
118471        pExpr->bEof = pLeft->bEof;
118472        break;
118473      }
118474
118475      default: {
118476        Fts3Phrase *pPhrase = pExpr->pPhrase;
118477        fts3EvalInvalidatePoslist(pPhrase);
118478        *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
118479        pExpr->iDocid = pPhrase->doclist.iDocid;
118480        break;
118481      }
118482    }
118483  }
118484}
118485
118486/*
118487** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
118488** cluster, then this function returns 1 immediately.
118489**
118490** Otherwise, it checks if the current row really does match the NEAR
118491** expression, using the data currently stored in the position lists
118492** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
118493**
118494** If the current row is a match, the position list associated with each
118495** phrase in the NEAR expression is edited in place to contain only those
118496** phrase instances sufficiently close to their peers to satisfy all NEAR
118497** constraints. In this case it returns 1. If the NEAR expression does not
118498** match the current row, 0 is returned. The position lists may or may not
118499** be edited if 0 is returned.
118500*/
118501static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
118502  int res = 1;
118503
118504  /* The following block runs if pExpr is the root of a NEAR query.
118505  ** For example, the query:
118506  **
118507  **         "w" NEAR "x" NEAR "y" NEAR "z"
118508  **
118509  ** which is represented in tree form as:
118510  **
118511  **                               |
118512  **                          +--NEAR--+      <-- root of NEAR query
118513  **                          |        |
118514  **                     +--NEAR--+   "z"
118515  **                     |        |
118516  **                +--NEAR--+   "y"
118517  **                |        |
118518  **               "w"      "x"
118519  **
118520  ** The right-hand child of a NEAR node is always a phrase. The
118521  ** left-hand child may be either a phrase or a NEAR node. There are
118522  ** no exceptions to this - it's the way the parser in fts3_expr.c works.
118523  */
118524  if( *pRc==SQLITE_OK
118525   && pExpr->eType==FTSQUERY_NEAR
118526   && pExpr->bEof==0
118527   && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
118528  ){
118529    Fts3Expr *p;
118530    int nTmp = 0;                 /* Bytes of temp space */
118531    char *aTmp;                   /* Temp space for PoslistNearMerge() */
118532
118533    /* Allocate temporary working space. */
118534    for(p=pExpr; p->pLeft; p=p->pLeft){
118535      nTmp += p->pRight->pPhrase->doclist.nList;
118536    }
118537    nTmp += p->pPhrase->doclist.nList;
118538    aTmp = sqlite3_malloc(nTmp*2);
118539    if( !aTmp ){
118540      *pRc = SQLITE_NOMEM;
118541      res = 0;
118542    }else{
118543      char *aPoslist = p->pPhrase->doclist.pList;
118544      int nToken = p->pPhrase->nToken;
118545
118546      for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
118547        Fts3Phrase *pPhrase = p->pRight->pPhrase;
118548        int nNear = p->nNear;
118549        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118550      }
118551
118552      aPoslist = pExpr->pRight->pPhrase->doclist.pList;
118553      nToken = pExpr->pRight->pPhrase->nToken;
118554      for(p=pExpr->pLeft; p && res; p=p->pLeft){
118555        int nNear = p->pParent->nNear;
118556        Fts3Phrase *pPhrase = (
118557            p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
118558        );
118559        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118560      }
118561    }
118562
118563    sqlite3_free(aTmp);
118564  }
118565
118566  return res;
118567}
118568
118569/*
118570** This function is a helper function for fts3EvalTestDeferredAndNear().
118571** Assuming no error occurs or has occurred, It returns non-zero if the
118572** expression passed as the second argument matches the row that pCsr
118573** currently points to, or zero if it does not.
118574**
118575** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
118576** If an error occurs during execution of this function, *pRc is set to
118577** the appropriate SQLite error code. In this case the returned value is
118578** undefined.
118579*/
118580static int fts3EvalTestExpr(
118581  Fts3Cursor *pCsr,               /* FTS cursor handle */
118582  Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
118583  int *pRc                        /* IN/OUT: Error code */
118584){
118585  int bHit = 1;                   /* Return value */
118586  if( *pRc==SQLITE_OK ){
118587    switch( pExpr->eType ){
118588      case FTSQUERY_NEAR:
118589      case FTSQUERY_AND:
118590        bHit = (
118591            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
118592         && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
118593         && fts3EvalNearTest(pExpr, pRc)
118594        );
118595
118596        /* If the NEAR expression does not match any rows, zero the doclist for
118597        ** all phrases involved in the NEAR. This is because the snippet(),
118598        ** offsets() and matchinfo() functions are not supposed to recognize
118599        ** any instances of phrases that are part of unmatched NEAR queries.
118600        ** For example if this expression:
118601        **
118602        **    ... MATCH 'a OR (b NEAR c)'
118603        **
118604        ** is matched against a row containing:
118605        **
118606        **        'a b d e'
118607        **
118608        ** then any snippet() should ony highlight the "a" term, not the "b"
118609        ** (as "b" is part of a non-matching NEAR clause).
118610        */
118611        if( bHit==0
118612         && pExpr->eType==FTSQUERY_NEAR
118613         && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
118614        ){
118615          Fts3Expr *p;
118616          for(p=pExpr; p->pPhrase==0; p=p->pLeft){
118617            if( p->pRight->iDocid==pCsr->iPrevId ){
118618              fts3EvalInvalidatePoslist(p->pRight->pPhrase);
118619            }
118620          }
118621          if( p->iDocid==pCsr->iPrevId ){
118622            fts3EvalInvalidatePoslist(p->pPhrase);
118623          }
118624        }
118625
118626        break;
118627
118628      case FTSQUERY_OR: {
118629        int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
118630        int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
118631        bHit = bHit1 || bHit2;
118632        break;
118633      }
118634
118635      case FTSQUERY_NOT:
118636        bHit = (
118637            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
118638         && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
118639        );
118640        break;
118641
118642      default: {
118643        if( pCsr->pDeferred
118644         && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
118645        ){
118646          Fts3Phrase *pPhrase = pExpr->pPhrase;
118647          assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
118648          if( pExpr->bDeferred ){
118649            fts3EvalInvalidatePoslist(pPhrase);
118650          }
118651          *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
118652          bHit = (pPhrase->doclist.pList!=0);
118653          pExpr->iDocid = pCsr->iPrevId;
118654        }else{
118655          bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
118656        }
118657        break;
118658      }
118659    }
118660  }
118661  return bHit;
118662}
118663
118664/*
118665** This function is called as the second part of each xNext operation when
118666** iterating through the results of a full-text query. At this point the
118667** cursor points to a row that matches the query expression, with the
118668** following caveats:
118669**
118670**   * Up until this point, "NEAR" operators in the expression have been
118671**     treated as "AND".
118672**
118673**   * Deferred tokens have not yet been considered.
118674**
118675** If *pRc is not SQLITE_OK when this function is called, it immediately
118676** returns 0. Otherwise, it tests whether or not after considering NEAR
118677** operators and deferred tokens the current row is still a match for the
118678** expression. It returns 1 if both of the following are true:
118679**
118680**   1. *pRc is SQLITE_OK when this function returns, and
118681**
118682**   2. After scanning the current FTS table row for the deferred tokens,
118683**      it is determined that the row does *not* match the query.
118684**
118685** Or, if no error occurs and it seems the current row does match the FTS
118686** query, return 0.
118687*/
118688static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
118689  int rc = *pRc;
118690  int bMiss = 0;
118691  if( rc==SQLITE_OK ){
118692
118693    /* If there are one or more deferred tokens, load the current row into
118694    ** memory and scan it to determine the position list for each deferred
118695    ** token. Then, see if this row is really a match, considering deferred
118696    ** tokens and NEAR operators (neither of which were taken into account
118697    ** earlier, by fts3EvalNextRow()).
118698    */
118699    if( pCsr->pDeferred ){
118700      rc = fts3CursorSeek(0, pCsr);
118701      if( rc==SQLITE_OK ){
118702        rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
118703      }
118704    }
118705    bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
118706
118707    /* Free the position-lists accumulated for each deferred token above. */
118708    sqlite3Fts3FreeDeferredDoclists(pCsr);
118709    *pRc = rc;
118710  }
118711  return (rc==SQLITE_OK && bMiss);
118712}
118713
118714/*
118715** Advance to the next document that matches the FTS expression in
118716** Fts3Cursor.pExpr.
118717*/
118718static int fts3EvalNext(Fts3Cursor *pCsr){
118719  int rc = SQLITE_OK;             /* Return Code */
118720  Fts3Expr *pExpr = pCsr->pExpr;
118721  assert( pCsr->isEof==0 );
118722  if( pExpr==0 ){
118723    pCsr->isEof = 1;
118724  }else{
118725    do {
118726      if( pCsr->isRequireSeek==0 ){
118727        sqlite3_reset(pCsr->pStmt);
118728      }
118729      assert( sqlite3_data_count(pCsr->pStmt)==0 );
118730      fts3EvalNextRow(pCsr, pExpr, &rc);
118731      pCsr->isEof = pExpr->bEof;
118732      pCsr->isRequireSeek = 1;
118733      pCsr->isMatchinfoNeeded = 1;
118734      pCsr->iPrevId = pExpr->iDocid;
118735    }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
118736  }
118737  return rc;
118738}
118739
118740/*
118741** Restart interation for expression pExpr so that the next call to
118742** fts3EvalNext() visits the first row. Do not allow incremental
118743** loading or merging of phrase doclists for this iteration.
118744**
118745** If *pRc is other than SQLITE_OK when this function is called, it is
118746** a no-op. If an error occurs within this function, *pRc is set to an
118747** SQLite error code before returning.
118748*/
118749static void fts3EvalRestart(
118750  Fts3Cursor *pCsr,
118751  Fts3Expr *pExpr,
118752  int *pRc
118753){
118754  if( pExpr && *pRc==SQLITE_OK ){
118755    Fts3Phrase *pPhrase = pExpr->pPhrase;
118756
118757    if( pPhrase ){
118758      fts3EvalInvalidatePoslist(pPhrase);
118759      if( pPhrase->bIncr ){
118760        assert( pPhrase->nToken==1 );
118761        assert( pPhrase->aToken[0].pSegcsr );
118762        sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
118763        *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
118764      }
118765
118766      pPhrase->doclist.pNextDocid = 0;
118767      pPhrase->doclist.iDocid = 0;
118768    }
118769
118770    pExpr->iDocid = 0;
118771    pExpr->bEof = 0;
118772    pExpr->bStart = 0;
118773
118774    fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
118775    fts3EvalRestart(pCsr, pExpr->pRight, pRc);
118776  }
118777}
118778
118779/*
118780** After allocating the Fts3Expr.aMI[] array for each phrase in the
118781** expression rooted at pExpr, the cursor iterates through all rows matched
118782** by pExpr, calling this function for each row. This function increments
118783** the values in Fts3Expr.aMI[] according to the position-list currently
118784** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
118785** expression nodes.
118786*/
118787static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
118788  if( pExpr ){
118789    Fts3Phrase *pPhrase = pExpr->pPhrase;
118790    if( pPhrase && pPhrase->doclist.pList ){
118791      int iCol = 0;
118792      char *p = pPhrase->doclist.pList;
118793
118794      assert( *p );
118795      while( 1 ){
118796        u8 c = 0;
118797        int iCnt = 0;
118798        while( 0xFE & (*p | c) ){
118799          if( (c&0x80)==0 ) iCnt++;
118800          c = *p++ & 0x80;
118801        }
118802
118803        /* aMI[iCol*3 + 1] = Number of occurrences
118804        ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
118805        */
118806        pExpr->aMI[iCol*3 + 1] += iCnt;
118807        pExpr->aMI[iCol*3 + 2] += (iCnt>0);
118808        if( *p==0x00 ) break;
118809        p++;
118810        p += sqlite3Fts3GetVarint32(p, &iCol);
118811      }
118812    }
118813
118814    fts3EvalUpdateCounts(pExpr->pLeft);
118815    fts3EvalUpdateCounts(pExpr->pRight);
118816  }
118817}
118818
118819/*
118820** Expression pExpr must be of type FTSQUERY_PHRASE.
118821**
118822** If it is not already allocated and populated, this function allocates and
118823** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
118824** of a NEAR expression, then it also allocates and populates the same array
118825** for all other phrases that are part of the NEAR expression.
118826**
118827** SQLITE_OK is returned if the aMI[] array is successfully allocated and
118828** populated. Otherwise, if an error occurs, an SQLite error code is returned.
118829*/
118830static int fts3EvalGatherStats(
118831  Fts3Cursor *pCsr,               /* Cursor object */
118832  Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
118833){
118834  int rc = SQLITE_OK;             /* Return code */
118835
118836  assert( pExpr->eType==FTSQUERY_PHRASE );
118837  if( pExpr->aMI==0 ){
118838    Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
118839    Fts3Expr *pRoot;                /* Root of NEAR expression */
118840    Fts3Expr *p;                    /* Iterator used for several purposes */
118841
118842    sqlite3_int64 iPrevId = pCsr->iPrevId;
118843    sqlite3_int64 iDocid;
118844    u8 bEof;
118845
118846    /* Find the root of the NEAR expression */
118847    pRoot = pExpr;
118848    while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
118849      pRoot = pRoot->pParent;
118850    }
118851    iDocid = pRoot->iDocid;
118852    bEof = pRoot->bEof;
118853    assert( pRoot->bStart );
118854
118855    /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
118856    for(p=pRoot; p; p=p->pLeft){
118857      Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
118858      assert( pE->aMI==0 );
118859      pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
118860      if( !pE->aMI ) return SQLITE_NOMEM;
118861      memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
118862    }
118863
118864    fts3EvalRestart(pCsr, pRoot, &rc);
118865
118866    while( pCsr->isEof==0 && rc==SQLITE_OK ){
118867
118868      do {
118869        /* Ensure the %_content statement is reset. */
118870        if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
118871        assert( sqlite3_data_count(pCsr->pStmt)==0 );
118872
118873        /* Advance to the next document */
118874        fts3EvalNextRow(pCsr, pRoot, &rc);
118875        pCsr->isEof = pRoot->bEof;
118876        pCsr->isRequireSeek = 1;
118877        pCsr->isMatchinfoNeeded = 1;
118878        pCsr->iPrevId = pRoot->iDocid;
118879      }while( pCsr->isEof==0
118880           && pRoot->eType==FTSQUERY_NEAR
118881           && fts3EvalTestDeferredAndNear(pCsr, &rc)
118882      );
118883
118884      if( rc==SQLITE_OK && pCsr->isEof==0 ){
118885        fts3EvalUpdateCounts(pRoot);
118886      }
118887    }
118888
118889    pCsr->isEof = 0;
118890    pCsr->iPrevId = iPrevId;
118891
118892    if( bEof ){
118893      pRoot->bEof = bEof;
118894    }else{
118895      /* Caution: pRoot may iterate through docids in ascending or descending
118896      ** order. For this reason, even though it seems more defensive, the
118897      ** do loop can not be written:
118898      **
118899      **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
118900      */
118901      fts3EvalRestart(pCsr, pRoot, &rc);
118902      do {
118903        fts3EvalNextRow(pCsr, pRoot, &rc);
118904        assert( pRoot->bEof==0 );
118905      }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
118906      fts3EvalTestDeferredAndNear(pCsr, &rc);
118907    }
118908  }
118909  return rc;
118910}
118911
118912/*
118913** This function is used by the matchinfo() module to query a phrase
118914** expression node for the following information:
118915**
118916**   1. The total number of occurrences of the phrase in each column of
118917**      the FTS table (considering all rows), and
118918**
118919**   2. For each column, the number of rows in the table for which the
118920**      column contains at least one instance of the phrase.
118921**
118922** If no error occurs, SQLITE_OK is returned and the values for each column
118923** written into the array aiOut as follows:
118924**
118925**   aiOut[iCol*3 + 1] = Number of occurrences
118926**   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
118927**
118928** Caveats:
118929**
118930**   * If a phrase consists entirely of deferred tokens, then all output
118931**     values are set to the number of documents in the table. In other
118932**     words we assume that very common tokens occur exactly once in each
118933**     column of each row of the table.
118934**
118935**   * If a phrase contains some deferred tokens (and some non-deferred
118936**     tokens), count the potential occurrence identified by considering
118937**     the non-deferred tokens instead of actual phrase occurrences.
118938**
118939**   * If the phrase is part of a NEAR expression, then only phrase instances
118940**     that meet the NEAR constraint are included in the counts.
118941*/
118942SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
118943  Fts3Cursor *pCsr,               /* FTS cursor handle */
118944  Fts3Expr *pExpr,                /* Phrase expression */
118945  u32 *aiOut                      /* Array to write results into (see above) */
118946){
118947  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
118948  int rc = SQLITE_OK;
118949  int iCol;
118950
118951  if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
118952    assert( pCsr->nDoc>0 );
118953    for(iCol=0; iCol<pTab->nColumn; iCol++){
118954      aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
118955      aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
118956    }
118957  }else{
118958    rc = fts3EvalGatherStats(pCsr, pExpr);
118959    if( rc==SQLITE_OK ){
118960      assert( pExpr->aMI );
118961      for(iCol=0; iCol<pTab->nColumn; iCol++){
118962        aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
118963        aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
118964      }
118965    }
118966  }
118967
118968  return rc;
118969}
118970
118971/*
118972** The expression pExpr passed as the second argument to this function
118973** must be of type FTSQUERY_PHRASE.
118974**
118975** The returned value is either NULL or a pointer to a buffer containing
118976** a position-list indicating the occurrences of the phrase in column iCol
118977** of the current row.
118978**
118979** More specifically, the returned buffer contains 1 varint for each
118980** occurence of the phrase in the column, stored using the normal (delta+2)
118981** compression and is terminated by either an 0x01 or 0x00 byte. For example,
118982** if the requested column contains "a b X c d X X" and the position-list
118983** for 'X' is requested, the buffer returned may contain:
118984**
118985**     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
118986**
118987** This function works regardless of whether or not the phrase is deferred,
118988** incremental, or neither.
118989*/
118990SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
118991  Fts3Cursor *pCsr,               /* FTS3 cursor object */
118992  Fts3Expr *pExpr,                /* Phrase to return doclist for */
118993  int iCol                        /* Column to return position list for */
118994){
118995  Fts3Phrase *pPhrase = pExpr->pPhrase;
118996  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
118997  char *pIter = pPhrase->doclist.pList;
118998  int iThis;
118999
119000  assert( iCol>=0 && iCol<pTab->nColumn );
119001  if( !pIter
119002   || pExpr->bEof
119003   || pExpr->iDocid!=pCsr->iPrevId
119004   || (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol)
119005  ){
119006    return 0;
119007  }
119008
119009  assert( pPhrase->doclist.nList>0 );
119010  if( *pIter==0x01 ){
119011    pIter++;
119012    pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
119013  }else{
119014    iThis = 0;
119015  }
119016  while( iThis<iCol ){
119017    fts3ColumnlistCopy(0, &pIter);
119018    if( *pIter==0x00 ) return 0;
119019    pIter++;
119020    pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
119021  }
119022
119023  return ((iCol==iThis)?pIter:0);
119024}
119025
119026/*
119027** Free all components of the Fts3Phrase structure that were allocated by
119028** the eval module. Specifically, this means to free:
119029**
119030**   * the contents of pPhrase->doclist, and
119031**   * any Fts3MultiSegReader objects held by phrase tokens.
119032*/
119033SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
119034  if( pPhrase ){
119035    int i;
119036    sqlite3_free(pPhrase->doclist.aAll);
119037    fts3EvalInvalidatePoslist(pPhrase);
119038    memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
119039    for(i=0; i<pPhrase->nToken; i++){
119040      fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
119041      pPhrase->aToken[i].pSegcsr = 0;
119042    }
119043  }
119044}
119045
119046#if !SQLITE_CORE
119047/*
119048** Initialize API pointer table, if required.
119049*/
119050SQLITE_API int sqlite3_extension_init(
119051  sqlite3 *db,
119052  char **pzErrMsg,
119053  const sqlite3_api_routines *pApi
119054){
119055  SQLITE_EXTENSION_INIT2(pApi)
119056  return sqlite3Fts3Init(db);
119057}
119058#endif
119059
119060#endif
119061
119062/************** End of fts3.c ************************************************/
119063/************** Begin file fts3_aux.c ****************************************/
119064/*
119065** 2011 Jan 27
119066**
119067** The author disclaims copyright to this source code.  In place of
119068** a legal notice, here is a blessing:
119069**
119070**    May you do good and not evil.
119071**    May you find forgiveness for yourself and forgive others.
119072**    May you share freely, never taking more than you give.
119073**
119074******************************************************************************
119075**
119076*/
119077#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119078
119079/* #include <string.h> */
119080/* #include <assert.h> */
119081
119082typedef struct Fts3auxTable Fts3auxTable;
119083typedef struct Fts3auxCursor Fts3auxCursor;
119084
119085struct Fts3auxTable {
119086  sqlite3_vtab base;              /* Base class used by SQLite core */
119087  Fts3Table *pFts3Tab;
119088};
119089
119090struct Fts3auxCursor {
119091  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
119092  Fts3MultiSegReader csr;        /* Must be right after "base" */
119093  Fts3SegFilter filter;
119094  char *zStop;
119095  int nStop;                      /* Byte-length of string zStop */
119096  int isEof;                      /* True if cursor is at EOF */
119097  sqlite3_int64 iRowid;           /* Current rowid */
119098
119099  int iCol;                       /* Current value of 'col' column */
119100  int nStat;                      /* Size of aStat[] array */
119101  struct Fts3auxColstats {
119102    sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
119103    sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
119104  } *aStat;
119105};
119106
119107/*
119108** Schema of the terms table.
119109*/
119110#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
119111
119112/*
119113** This function does all the work for both the xConnect and xCreate methods.
119114** These tables have no persistent representation of their own, so xConnect
119115** and xCreate are identical operations.
119116*/
119117static int fts3auxConnectMethod(
119118  sqlite3 *db,                    /* Database connection */
119119  void *pUnused,                  /* Unused */
119120  int argc,                       /* Number of elements in argv array */
119121  const char * const *argv,       /* xCreate/xConnect argument array */
119122  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
119123  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
119124){
119125  char const *zDb;                /* Name of database (e.g. "main") */
119126  char const *zFts3;              /* Name of fts3 table */
119127  int nDb;                        /* Result of strlen(zDb) */
119128  int nFts3;                      /* Result of strlen(zFts3) */
119129  int nByte;                      /* Bytes of space to allocate here */
119130  int rc;                         /* value returned by declare_vtab() */
119131  Fts3auxTable *p;                /* Virtual table object to return */
119132
119133  UNUSED_PARAMETER(pUnused);
119134
119135  /* The user should specify a single argument - the name of an fts3 table. */
119136  if( argc!=4 ){
119137    *pzErr = sqlite3_mprintf(
119138        "wrong number of arguments to fts4aux constructor"
119139    );
119140    return SQLITE_ERROR;
119141  }
119142
119143  zDb = argv[1];
119144  nDb = strlen(zDb);
119145  zFts3 = argv[3];
119146  nFts3 = strlen(zFts3);
119147
119148  rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
119149  if( rc!=SQLITE_OK ) return rc;
119150
119151  nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
119152  p = (Fts3auxTable *)sqlite3_malloc(nByte);
119153  if( !p ) return SQLITE_NOMEM;
119154  memset(p, 0, nByte);
119155
119156  p->pFts3Tab = (Fts3Table *)&p[1];
119157  p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
119158  p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
119159  p->pFts3Tab->db = db;
119160  p->pFts3Tab->nIndex = 1;
119161
119162  memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
119163  memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
119164  sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
119165
119166  *ppVtab = (sqlite3_vtab *)p;
119167  return SQLITE_OK;
119168}
119169
119170/*
119171** This function does the work for both the xDisconnect and xDestroy methods.
119172** These tables have no persistent representation of their own, so xDisconnect
119173** and xDestroy are identical operations.
119174*/
119175static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
119176  Fts3auxTable *p = (Fts3auxTable *)pVtab;
119177  Fts3Table *pFts3 = p->pFts3Tab;
119178  int i;
119179
119180  /* Free any prepared statements held */
119181  for(i=0; i<SizeofArray(pFts3->aStmt); i++){
119182    sqlite3_finalize(pFts3->aStmt[i]);
119183  }
119184  sqlite3_free(pFts3->zSegmentsTbl);
119185  sqlite3_free(p);
119186  return SQLITE_OK;
119187}
119188
119189#define FTS4AUX_EQ_CONSTRAINT 1
119190#define FTS4AUX_GE_CONSTRAINT 2
119191#define FTS4AUX_LE_CONSTRAINT 4
119192
119193/*
119194** xBestIndex - Analyze a WHERE and ORDER BY clause.
119195*/
119196static int fts3auxBestIndexMethod(
119197  sqlite3_vtab *pVTab,
119198  sqlite3_index_info *pInfo
119199){
119200  int i;
119201  int iEq = -1;
119202  int iGe = -1;
119203  int iLe = -1;
119204
119205  UNUSED_PARAMETER(pVTab);
119206
119207  /* This vtab delivers always results in "ORDER BY term ASC" order. */
119208  if( pInfo->nOrderBy==1
119209   && pInfo->aOrderBy[0].iColumn==0
119210   && pInfo->aOrderBy[0].desc==0
119211  ){
119212    pInfo->orderByConsumed = 1;
119213  }
119214
119215  /* Search for equality and range constraints on the "term" column. */
119216  for(i=0; i<pInfo->nConstraint; i++){
119217    if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
119218      int op = pInfo->aConstraint[i].op;
119219      if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
119220      if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
119221      if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
119222      if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
119223      if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
119224    }
119225  }
119226
119227  if( iEq>=0 ){
119228    pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
119229    pInfo->aConstraintUsage[iEq].argvIndex = 1;
119230    pInfo->estimatedCost = 5;
119231  }else{
119232    pInfo->idxNum = 0;
119233    pInfo->estimatedCost = 20000;
119234    if( iGe>=0 ){
119235      pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
119236      pInfo->aConstraintUsage[iGe].argvIndex = 1;
119237      pInfo->estimatedCost /= 2;
119238    }
119239    if( iLe>=0 ){
119240      pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
119241      pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
119242      pInfo->estimatedCost /= 2;
119243    }
119244  }
119245
119246  return SQLITE_OK;
119247}
119248
119249/*
119250** xOpen - Open a cursor.
119251*/
119252static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
119253  Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
119254
119255  UNUSED_PARAMETER(pVTab);
119256
119257  pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
119258  if( !pCsr ) return SQLITE_NOMEM;
119259  memset(pCsr, 0, sizeof(Fts3auxCursor));
119260
119261  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
119262  return SQLITE_OK;
119263}
119264
119265/*
119266** xClose - Close a cursor.
119267*/
119268static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
119269  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
119270  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
119271
119272  sqlite3Fts3SegmentsClose(pFts3);
119273  sqlite3Fts3SegReaderFinish(&pCsr->csr);
119274  sqlite3_free((void *)pCsr->filter.zTerm);
119275  sqlite3_free(pCsr->zStop);
119276  sqlite3_free(pCsr->aStat);
119277  sqlite3_free(pCsr);
119278  return SQLITE_OK;
119279}
119280
119281static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
119282  if( nSize>pCsr->nStat ){
119283    struct Fts3auxColstats *aNew;
119284    aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
119285        sizeof(struct Fts3auxColstats) * nSize
119286    );
119287    if( aNew==0 ) return SQLITE_NOMEM;
119288    memset(&aNew[pCsr->nStat], 0,
119289        sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
119290    );
119291    pCsr->aStat = aNew;
119292    pCsr->nStat = nSize;
119293  }
119294  return SQLITE_OK;
119295}
119296
119297/*
119298** xNext - Advance the cursor to the next row, if any.
119299*/
119300static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
119301  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
119302  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
119303  int rc;
119304
119305  /* Increment our pretend rowid value. */
119306  pCsr->iRowid++;
119307
119308  for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
119309    if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
119310  }
119311
119312  rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
119313  if( rc==SQLITE_ROW ){
119314    int i = 0;
119315    int nDoclist = pCsr->csr.nDoclist;
119316    char *aDoclist = pCsr->csr.aDoclist;
119317    int iCol;
119318
119319    int eState = 0;
119320
119321    if( pCsr->zStop ){
119322      int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
119323      int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
119324      if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
119325        pCsr->isEof = 1;
119326        return SQLITE_OK;
119327      }
119328    }
119329
119330    if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
119331    memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
119332    iCol = 0;
119333
119334    while( i<nDoclist ){
119335      sqlite3_int64 v = 0;
119336
119337      i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
119338      switch( eState ){
119339        /* State 0. In this state the integer just read was a docid. */
119340        case 0:
119341          pCsr->aStat[0].nDoc++;
119342          eState = 1;
119343          iCol = 0;
119344          break;
119345
119346        /* State 1. In this state we are expecting either a 1, indicating
119347        ** that the following integer will be a column number, or the
119348        ** start of a position list for column 0.
119349        **
119350        ** The only difference between state 1 and state 2 is that if the
119351        ** integer encountered in state 1 is not 0 or 1, then we need to
119352        ** increment the column 0 "nDoc" count for this term.
119353        */
119354        case 1:
119355          assert( iCol==0 );
119356          if( v>1 ){
119357            pCsr->aStat[1].nDoc++;
119358          }
119359          eState = 2;
119360          /* fall through */
119361
119362        case 2:
119363          if( v==0 ){       /* 0x00. Next integer will be a docid. */
119364            eState = 0;
119365          }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
119366            eState = 3;
119367          }else{            /* 2 or greater. A position. */
119368            pCsr->aStat[iCol+1].nOcc++;
119369            pCsr->aStat[0].nOcc++;
119370          }
119371          break;
119372
119373        /* State 3. The integer just read is a column number. */
119374        default: assert( eState==3 );
119375          iCol = (int)v;
119376          if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
119377          pCsr->aStat[iCol+1].nDoc++;
119378          eState = 2;
119379          break;
119380      }
119381    }
119382
119383    pCsr->iCol = 0;
119384    rc = SQLITE_OK;
119385  }else{
119386    pCsr->isEof = 1;
119387  }
119388  return rc;
119389}
119390
119391/*
119392** xFilter - Initialize a cursor to point at the start of its data.
119393*/
119394static int fts3auxFilterMethod(
119395  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
119396  int idxNum,                     /* Strategy index */
119397  const char *idxStr,             /* Unused */
119398  int nVal,                       /* Number of elements in apVal */
119399  sqlite3_value **apVal           /* Arguments for the indexing scheme */
119400){
119401  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
119402  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
119403  int rc;
119404  int isScan;
119405
119406  UNUSED_PARAMETER(nVal);
119407  UNUSED_PARAMETER(idxStr);
119408
119409  assert( idxStr==0 );
119410  assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
119411       || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
119412       || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
119413  );
119414  isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
119415
119416  /* In case this cursor is being reused, close and zero it. */
119417  testcase(pCsr->filter.zTerm);
119418  sqlite3Fts3SegReaderFinish(&pCsr->csr);
119419  sqlite3_free((void *)pCsr->filter.zTerm);
119420  sqlite3_free(pCsr->aStat);
119421  memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
119422
119423  pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
119424  if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
119425
119426  if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
119427    const unsigned char *zStr = sqlite3_value_text(apVal[0]);
119428    if( zStr ){
119429      pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
119430      pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
119431      if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
119432    }
119433  }
119434  if( idxNum&FTS4AUX_LE_CONSTRAINT ){
119435    int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
119436    pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
119437    pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
119438    if( pCsr->zStop==0 ) return SQLITE_NOMEM;
119439  }
119440
119441  rc = sqlite3Fts3SegReaderCursor(pFts3, 0, FTS3_SEGCURSOR_ALL,
119442      pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
119443  );
119444  if( rc==SQLITE_OK ){
119445    rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
119446  }
119447
119448  if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
119449  return rc;
119450}
119451
119452/*
119453** xEof - Return true if the cursor is at EOF, or false otherwise.
119454*/
119455static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
119456  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
119457  return pCsr->isEof;
119458}
119459
119460/*
119461** xColumn - Return a column value.
119462*/
119463static int fts3auxColumnMethod(
119464  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
119465  sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
119466  int iCol                        /* Index of column to read value from */
119467){
119468  Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
119469
119470  assert( p->isEof==0 );
119471  if( iCol==0 ){        /* Column "term" */
119472    sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
119473  }else if( iCol==1 ){  /* Column "col" */
119474    if( p->iCol ){
119475      sqlite3_result_int(pContext, p->iCol-1);
119476    }else{
119477      sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
119478    }
119479  }else if( iCol==2 ){  /* Column "documents" */
119480    sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
119481  }else{                /* Column "occurrences" */
119482    sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
119483  }
119484
119485  return SQLITE_OK;
119486}
119487
119488/*
119489** xRowid - Return the current rowid for the cursor.
119490*/
119491static int fts3auxRowidMethod(
119492  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
119493  sqlite_int64 *pRowid            /* OUT: Rowid value */
119494){
119495  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
119496  *pRowid = pCsr->iRowid;
119497  return SQLITE_OK;
119498}
119499
119500/*
119501** Register the fts3aux module with database connection db. Return SQLITE_OK
119502** if successful or an error code if sqlite3_create_module() fails.
119503*/
119504SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
119505  static const sqlite3_module fts3aux_module = {
119506     0,                           /* iVersion      */
119507     fts3auxConnectMethod,        /* xCreate       */
119508     fts3auxConnectMethod,        /* xConnect      */
119509     fts3auxBestIndexMethod,      /* xBestIndex    */
119510     fts3auxDisconnectMethod,     /* xDisconnect   */
119511     fts3auxDisconnectMethod,     /* xDestroy      */
119512     fts3auxOpenMethod,           /* xOpen         */
119513     fts3auxCloseMethod,          /* xClose        */
119514     fts3auxFilterMethod,         /* xFilter       */
119515     fts3auxNextMethod,           /* xNext         */
119516     fts3auxEofMethod,            /* xEof          */
119517     fts3auxColumnMethod,         /* xColumn       */
119518     fts3auxRowidMethod,          /* xRowid        */
119519     0,                           /* xUpdate       */
119520     0,                           /* xBegin        */
119521     0,                           /* xSync         */
119522     0,                           /* xCommit       */
119523     0,                           /* xRollback     */
119524     0,                           /* xFindFunction */
119525     0,                           /* xRename       */
119526     0,                           /* xSavepoint    */
119527     0,                           /* xRelease      */
119528     0                            /* xRollbackTo   */
119529  };
119530  int rc;                         /* Return code */
119531
119532  rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
119533  return rc;
119534}
119535
119536#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
119537
119538/************** End of fts3_aux.c ********************************************/
119539/************** Begin file fts3_expr.c ***************************************/
119540/*
119541** 2008 Nov 28
119542**
119543** The author disclaims copyright to this source code.  In place of
119544** a legal notice, here is a blessing:
119545**
119546**    May you do good and not evil.
119547**    May you find forgiveness for yourself and forgive others.
119548**    May you share freely, never taking more than you give.
119549**
119550******************************************************************************
119551**
119552** This module contains code that implements a parser for fts3 query strings
119553** (the right-hand argument to the MATCH operator). Because the supported
119554** syntax is relatively simple, the whole tokenizer/parser system is
119555** hand-coded.
119556*/
119557#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119558
119559/*
119560** By default, this module parses the legacy syntax that has been
119561** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
119562** is defined, then it uses the new syntax. The differences between
119563** the new and the old syntaxes are:
119564**
119565**  a) The new syntax supports parenthesis. The old does not.
119566**
119567**  b) The new syntax supports the AND and NOT operators. The old does not.
119568**
119569**  c) The old syntax supports the "-" token qualifier. This is not
119570**     supported by the new syntax (it is replaced by the NOT operator).
119571**
119572**  d) When using the old syntax, the OR operator has a greater precedence
119573**     than an implicit AND. When using the new, both implicity and explicit
119574**     AND operators have a higher precedence than OR.
119575**
119576** If compiled with SQLITE_TEST defined, then this module exports the
119577** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
119578** to zero causes the module to use the old syntax. If it is set to
119579** non-zero the new syntax is activated. This is so both syntaxes can
119580** be tested using a single build of testfixture.
119581**
119582** The following describes the syntax supported by the fts3 MATCH
119583** operator in a similar format to that used by the lemon parser
119584** generator. This module does not use actually lemon, it uses a
119585** custom parser.
119586**
119587**   query ::= andexpr (OR andexpr)*.
119588**
119589**   andexpr ::= notexpr (AND? notexpr)*.
119590**
119591**   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
119592**   notexpr ::= LP query RP.
119593**
119594**   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
119595**
119596**   distance_opt ::= .
119597**   distance_opt ::= / INTEGER.
119598**
119599**   phrase ::= TOKEN.
119600**   phrase ::= COLUMN:TOKEN.
119601**   phrase ::= "TOKEN TOKEN TOKEN...".
119602*/
119603
119604#ifdef SQLITE_TEST
119605SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
119606#else
119607# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
119608#  define sqlite3_fts3_enable_parentheses 1
119609# else
119610#  define sqlite3_fts3_enable_parentheses 0
119611# endif
119612#endif
119613
119614/*
119615** Default span for NEAR operators.
119616*/
119617#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
119618
119619/* #include <string.h> */
119620/* #include <assert.h> */
119621
119622/*
119623** isNot:
119624**   This variable is used by function getNextNode(). When getNextNode() is
119625**   called, it sets ParseContext.isNot to true if the 'next node' is a
119626**   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
119627**   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
119628**   zero.
119629*/
119630typedef struct ParseContext ParseContext;
119631struct ParseContext {
119632  sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
119633  const char **azCol;                 /* Array of column names for fts3 table */
119634  int nCol;                           /* Number of entries in azCol[] */
119635  int iDefaultCol;                    /* Default column to query */
119636  int isNot;                          /* True if getNextNode() sees a unary - */
119637  sqlite3_context *pCtx;              /* Write error message here */
119638  int nNest;                          /* Number of nested brackets */
119639};
119640
119641/*
119642** This function is equivalent to the standard isspace() function.
119643**
119644** The standard isspace() can be awkward to use safely, because although it
119645** is defined to accept an argument of type int, its behaviour when passed
119646** an integer that falls outside of the range of the unsigned char type
119647** is undefined (and sometimes, "undefined" means segfault). This wrapper
119648** is defined to accept an argument of type char, and always returns 0 for
119649** any values that fall outside of the range of the unsigned char type (i.e.
119650** negative values).
119651*/
119652static int fts3isspace(char c){
119653  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
119654}
119655
119656/*
119657** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
119658** zero the memory before returning a pointer to it. If unsuccessful,
119659** return NULL.
119660*/
119661static void *fts3MallocZero(int nByte){
119662  void *pRet = sqlite3_malloc(nByte);
119663  if( pRet ) memset(pRet, 0, nByte);
119664  return pRet;
119665}
119666
119667
119668/*
119669** Extract the next token from buffer z (length n) using the tokenizer
119670** and other information (column names etc.) in pParse. Create an Fts3Expr
119671** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
119672** single token and set *ppExpr to point to it. If the end of the buffer is
119673** reached before a token is found, set *ppExpr to zero. It is the
119674** responsibility of the caller to eventually deallocate the allocated
119675** Fts3Expr structure (if any) by passing it to sqlite3_free().
119676**
119677** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
119678** fails.
119679*/
119680static int getNextToken(
119681  ParseContext *pParse,                   /* fts3 query parse context */
119682  int iCol,                               /* Value for Fts3Phrase.iColumn */
119683  const char *z, int n,                   /* Input string */
119684  Fts3Expr **ppExpr,                      /* OUT: expression */
119685  int *pnConsumed                         /* OUT: Number of bytes consumed */
119686){
119687  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
119688  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
119689  int rc;
119690  sqlite3_tokenizer_cursor *pCursor;
119691  Fts3Expr *pRet = 0;
119692  int nConsumed = 0;
119693
119694  rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
119695  if( rc==SQLITE_OK ){
119696    const char *zToken;
119697    int nToken, iStart, iEnd, iPosition;
119698    int nByte;                               /* total space to allocate */
119699
119700    pCursor->pTokenizer = pTokenizer;
119701    rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
119702
119703    if( rc==SQLITE_OK ){
119704      nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
119705      pRet = (Fts3Expr *)fts3MallocZero(nByte);
119706      if( !pRet ){
119707        rc = SQLITE_NOMEM;
119708      }else{
119709        pRet->eType = FTSQUERY_PHRASE;
119710        pRet->pPhrase = (Fts3Phrase *)&pRet[1];
119711        pRet->pPhrase->nToken = 1;
119712        pRet->pPhrase->iColumn = iCol;
119713        pRet->pPhrase->aToken[0].n = nToken;
119714        pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
119715        memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
119716
119717        if( iEnd<n && z[iEnd]=='*' ){
119718          pRet->pPhrase->aToken[0].isPrefix = 1;
119719          iEnd++;
119720        }
119721        if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){
119722          pParse->isNot = 1;
119723        }
119724      }
119725      nConsumed = iEnd;
119726    }
119727
119728    pModule->xClose(pCursor);
119729  }
119730
119731  *pnConsumed = nConsumed;
119732  *ppExpr = pRet;
119733  return rc;
119734}
119735
119736
119737/*
119738** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
119739** then free the old allocation.
119740*/
119741static void *fts3ReallocOrFree(void *pOrig, int nNew){
119742  void *pRet = sqlite3_realloc(pOrig, nNew);
119743  if( !pRet ){
119744    sqlite3_free(pOrig);
119745  }
119746  return pRet;
119747}
119748
119749/*
119750** Buffer zInput, length nInput, contains the contents of a quoted string
119751** that appeared as part of an fts3 query expression. Neither quote character
119752** is included in the buffer. This function attempts to tokenize the entire
119753** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
119754** containing the results.
119755**
119756** If successful, SQLITE_OK is returned and *ppExpr set to point at the
119757** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
119758** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
119759** to 0.
119760*/
119761static int getNextString(
119762  ParseContext *pParse,                   /* fts3 query parse context */
119763  const char *zInput, int nInput,         /* Input string */
119764  Fts3Expr **ppExpr                       /* OUT: expression */
119765){
119766  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
119767  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
119768  int rc;
119769  Fts3Expr *p = 0;
119770  sqlite3_tokenizer_cursor *pCursor = 0;
119771  char *zTemp = 0;
119772  int nTemp = 0;
119773
119774  const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
119775  int nToken = 0;
119776
119777  /* The final Fts3Expr data structure, including the Fts3Phrase,
119778  ** Fts3PhraseToken structures token buffers are all stored as a single
119779  ** allocation so that the expression can be freed with a single call to
119780  ** sqlite3_free(). Setting this up requires a two pass approach.
119781  **
119782  ** The first pass, in the block below, uses a tokenizer cursor to iterate
119783  ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
119784  ** to assemble data in two dynamic buffers:
119785  **
119786  **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
119787  **             structure, followed by the array of Fts3PhraseToken
119788  **             structures. This pass only populates the Fts3PhraseToken array.
119789  **
119790  **   Buffer zTemp: Contains copies of all tokens.
119791  **
119792  ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
119793  ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
119794  ** structures.
119795  */
119796  rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
119797  if( rc==SQLITE_OK ){
119798    int ii;
119799    pCursor->pTokenizer = pTokenizer;
119800    for(ii=0; rc==SQLITE_OK; ii++){
119801      const char *zByte;
119802      int nByte, iBegin, iEnd, iPos;
119803      rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
119804      if( rc==SQLITE_OK ){
119805        Fts3PhraseToken *pToken;
119806
119807        p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
119808        if( !p ) goto no_mem;
119809
119810        zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
119811        if( !zTemp ) goto no_mem;
119812
119813        assert( nToken==ii );
119814        pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
119815        memset(pToken, 0, sizeof(Fts3PhraseToken));
119816
119817        memcpy(&zTemp[nTemp], zByte, nByte);
119818        nTemp += nByte;
119819
119820        pToken->n = nByte;
119821        pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
119822        nToken = ii+1;
119823      }
119824    }
119825
119826    pModule->xClose(pCursor);
119827    pCursor = 0;
119828  }
119829
119830  if( rc==SQLITE_DONE ){
119831    int jj;
119832    char *zBuf = 0;
119833
119834    p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
119835    if( !p ) goto no_mem;
119836    memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
119837    p->eType = FTSQUERY_PHRASE;
119838    p->pPhrase = (Fts3Phrase *)&p[1];
119839    p->pPhrase->iColumn = pParse->iDefaultCol;
119840    p->pPhrase->nToken = nToken;
119841
119842    zBuf = (char *)&p->pPhrase->aToken[nToken];
119843    memcpy(zBuf, zTemp, nTemp);
119844    sqlite3_free(zTemp);
119845
119846    for(jj=0; jj<p->pPhrase->nToken; jj++){
119847      p->pPhrase->aToken[jj].z = zBuf;
119848      zBuf += p->pPhrase->aToken[jj].n;
119849    }
119850    rc = SQLITE_OK;
119851  }
119852
119853  *ppExpr = p;
119854  return rc;
119855no_mem:
119856
119857  if( pCursor ){
119858    pModule->xClose(pCursor);
119859  }
119860  sqlite3_free(zTemp);
119861  sqlite3_free(p);
119862  *ppExpr = 0;
119863  return SQLITE_NOMEM;
119864}
119865
119866/*
119867** Function getNextNode(), which is called by fts3ExprParse(), may itself
119868** call fts3ExprParse(). So this forward declaration is required.
119869*/
119870static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
119871
119872/*
119873** The output variable *ppExpr is populated with an allocated Fts3Expr
119874** structure, or set to 0 if the end of the input buffer is reached.
119875**
119876** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
119877** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
119878** If SQLITE_ERROR is returned, pContext is populated with an error message.
119879*/
119880static int getNextNode(
119881  ParseContext *pParse,                   /* fts3 query parse context */
119882  const char *z, int n,                   /* Input string */
119883  Fts3Expr **ppExpr,                      /* OUT: expression */
119884  int *pnConsumed                         /* OUT: Number of bytes consumed */
119885){
119886  static const struct Fts3Keyword {
119887    char *z;                              /* Keyword text */
119888    unsigned char n;                      /* Length of the keyword */
119889    unsigned char parenOnly;              /* Only valid in paren mode */
119890    unsigned char eType;                  /* Keyword code */
119891  } aKeyword[] = {
119892    { "OR" ,  2, 0, FTSQUERY_OR   },
119893    { "AND",  3, 1, FTSQUERY_AND  },
119894    { "NOT",  3, 1, FTSQUERY_NOT  },
119895    { "NEAR", 4, 0, FTSQUERY_NEAR }
119896  };
119897  int ii;
119898  int iCol;
119899  int iColLen;
119900  int rc;
119901  Fts3Expr *pRet = 0;
119902
119903  const char *zInput = z;
119904  int nInput = n;
119905
119906  pParse->isNot = 0;
119907
119908  /* Skip over any whitespace before checking for a keyword, an open or
119909  ** close bracket, or a quoted string.
119910  */
119911  while( nInput>0 && fts3isspace(*zInput) ){
119912    nInput--;
119913    zInput++;
119914  }
119915  if( nInput==0 ){
119916    return SQLITE_DONE;
119917  }
119918
119919  /* See if we are dealing with a keyword. */
119920  for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
119921    const struct Fts3Keyword *pKey = &aKeyword[ii];
119922
119923    if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
119924      continue;
119925    }
119926
119927    if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
119928      int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
119929      int nKey = pKey->n;
119930      char cNext;
119931
119932      /* If this is a "NEAR" keyword, check for an explicit nearness. */
119933      if( pKey->eType==FTSQUERY_NEAR ){
119934        assert( nKey==4 );
119935        if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
119936          nNear = 0;
119937          for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
119938            nNear = nNear * 10 + (zInput[nKey] - '0');
119939          }
119940        }
119941      }
119942
119943      /* At this point this is probably a keyword. But for that to be true,
119944      ** the next byte must contain either whitespace, an open or close
119945      ** parenthesis, a quote character, or EOF.
119946      */
119947      cNext = zInput[nKey];
119948      if( fts3isspace(cNext)
119949       || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
119950      ){
119951        pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
119952        if( !pRet ){
119953          return SQLITE_NOMEM;
119954        }
119955        pRet->eType = pKey->eType;
119956        pRet->nNear = nNear;
119957        *ppExpr = pRet;
119958        *pnConsumed = (int)((zInput - z) + nKey);
119959        return SQLITE_OK;
119960      }
119961
119962      /* Turns out that wasn't a keyword after all. This happens if the
119963      ** user has supplied a token such as "ORacle". Continue.
119964      */
119965    }
119966  }
119967
119968  /* Check for an open bracket. */
119969  if( sqlite3_fts3_enable_parentheses ){
119970    if( *zInput=='(' ){
119971      int nConsumed;
119972      pParse->nNest++;
119973      rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
119974      if( rc==SQLITE_OK && !*ppExpr ){
119975        rc = SQLITE_DONE;
119976      }
119977      *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
119978      return rc;
119979    }
119980
119981    /* Check for a close bracket. */
119982    if( *zInput==')' ){
119983      pParse->nNest--;
119984      *pnConsumed = (int)((zInput - z) + 1);
119985      return SQLITE_DONE;
119986    }
119987  }
119988
119989  /* See if we are dealing with a quoted phrase. If this is the case, then
119990  ** search for the closing quote and pass the whole string to getNextString()
119991  ** for processing. This is easy to do, as fts3 has no syntax for escaping
119992  ** a quote character embedded in a string.
119993  */
119994  if( *zInput=='"' ){
119995    for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
119996    *pnConsumed = (int)((zInput - z) + ii + 1);
119997    if( ii==nInput ){
119998      return SQLITE_ERROR;
119999    }
120000    return getNextString(pParse, &zInput[1], ii-1, ppExpr);
120001  }
120002
120003
120004  /* If control flows to this point, this must be a regular token, or
120005  ** the end of the input. Read a regular token using the sqlite3_tokenizer
120006  ** interface. Before doing so, figure out if there is an explicit
120007  ** column specifier for the token.
120008  **
120009  ** TODO: Strangely, it is not possible to associate a column specifier
120010  ** with a quoted phrase, only with a single token. Not sure if this was
120011  ** an implementation artifact or an intentional decision when fts3 was
120012  ** first implemented. Whichever it was, this module duplicates the
120013  ** limitation.
120014  */
120015  iCol = pParse->iDefaultCol;
120016  iColLen = 0;
120017  for(ii=0; ii<pParse->nCol; ii++){
120018    const char *zStr = pParse->azCol[ii];
120019    int nStr = (int)strlen(zStr);
120020    if( nInput>nStr && zInput[nStr]==':'
120021     && sqlite3_strnicmp(zStr, zInput, nStr)==0
120022    ){
120023      iCol = ii;
120024      iColLen = (int)((zInput - z) + nStr + 1);
120025      break;
120026    }
120027  }
120028  rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
120029  *pnConsumed += iColLen;
120030  return rc;
120031}
120032
120033/*
120034** The argument is an Fts3Expr structure for a binary operator (any type
120035** except an FTSQUERY_PHRASE). Return an integer value representing the
120036** precedence of the operator. Lower values have a higher precedence (i.e.
120037** group more tightly). For example, in the C language, the == operator
120038** groups more tightly than ||, and would therefore have a higher precedence.
120039**
120040** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
120041** is defined), the order of the operators in precedence from highest to
120042** lowest is:
120043**
120044**   NEAR
120045**   NOT
120046**   AND (including implicit ANDs)
120047**   OR
120048**
120049** Note that when using the old query syntax, the OR operator has a higher
120050** precedence than the AND operator.
120051*/
120052static int opPrecedence(Fts3Expr *p){
120053  assert( p->eType!=FTSQUERY_PHRASE );
120054  if( sqlite3_fts3_enable_parentheses ){
120055    return p->eType;
120056  }else if( p->eType==FTSQUERY_NEAR ){
120057    return 1;
120058  }else if( p->eType==FTSQUERY_OR ){
120059    return 2;
120060  }
120061  assert( p->eType==FTSQUERY_AND );
120062  return 3;
120063}
120064
120065/*
120066** Argument ppHead contains a pointer to the current head of a query
120067** expression tree being parsed. pPrev is the expression node most recently
120068** inserted into the tree. This function adds pNew, which is always a binary
120069** operator node, into the expression tree based on the relative precedence
120070** of pNew and the existing nodes of the tree. This may result in the head
120071** of the tree changing, in which case *ppHead is set to the new root node.
120072*/
120073static void insertBinaryOperator(
120074  Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
120075  Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
120076  Fts3Expr *pNew           /* New binary node to insert into expression tree */
120077){
120078  Fts3Expr *pSplit = pPrev;
120079  while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
120080    pSplit = pSplit->pParent;
120081  }
120082
120083  if( pSplit->pParent ){
120084    assert( pSplit->pParent->pRight==pSplit );
120085    pSplit->pParent->pRight = pNew;
120086    pNew->pParent = pSplit->pParent;
120087  }else{
120088    *ppHead = pNew;
120089  }
120090  pNew->pLeft = pSplit;
120091  pSplit->pParent = pNew;
120092}
120093
120094/*
120095** Parse the fts3 query expression found in buffer z, length n. This function
120096** returns either when the end of the buffer is reached or an unmatched
120097** closing bracket - ')' - is encountered.
120098**
120099** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
120100** parsed form of the expression and *pnConsumed is set to the number of
120101** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
120102** (out of memory error) or SQLITE_ERROR (parse error) is returned.
120103*/
120104static int fts3ExprParse(
120105  ParseContext *pParse,                   /* fts3 query parse context */
120106  const char *z, int n,                   /* Text of MATCH query */
120107  Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
120108  int *pnConsumed                         /* OUT: Number of bytes consumed */
120109){
120110  Fts3Expr *pRet = 0;
120111  Fts3Expr *pPrev = 0;
120112  Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
120113  int nIn = n;
120114  const char *zIn = z;
120115  int rc = SQLITE_OK;
120116  int isRequirePhrase = 1;
120117
120118  while( rc==SQLITE_OK ){
120119    Fts3Expr *p = 0;
120120    int nByte = 0;
120121    rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
120122    if( rc==SQLITE_OK ){
120123      int isPhrase;
120124
120125      if( !sqlite3_fts3_enable_parentheses
120126       && p->eType==FTSQUERY_PHRASE && pParse->isNot
120127      ){
120128        /* Create an implicit NOT operator. */
120129        Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
120130        if( !pNot ){
120131          sqlite3Fts3ExprFree(p);
120132          rc = SQLITE_NOMEM;
120133          goto exprparse_out;
120134        }
120135        pNot->eType = FTSQUERY_NOT;
120136        pNot->pRight = p;
120137        if( pNotBranch ){
120138          pNot->pLeft = pNotBranch;
120139        }
120140        pNotBranch = pNot;
120141        p = pPrev;
120142      }else{
120143        int eType = p->eType;
120144        isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
120145
120146        /* The isRequirePhrase variable is set to true if a phrase or
120147        ** an expression contained in parenthesis is required. If a
120148        ** binary operator (AND, OR, NOT or NEAR) is encounted when
120149        ** isRequirePhrase is set, this is a syntax error.
120150        */
120151        if( !isPhrase && isRequirePhrase ){
120152          sqlite3Fts3ExprFree(p);
120153          rc = SQLITE_ERROR;
120154          goto exprparse_out;
120155        }
120156
120157        if( isPhrase && !isRequirePhrase ){
120158          /* Insert an implicit AND operator. */
120159          Fts3Expr *pAnd;
120160          assert( pRet && pPrev );
120161          pAnd = fts3MallocZero(sizeof(Fts3Expr));
120162          if( !pAnd ){
120163            sqlite3Fts3ExprFree(p);
120164            rc = SQLITE_NOMEM;
120165            goto exprparse_out;
120166          }
120167          pAnd->eType = FTSQUERY_AND;
120168          insertBinaryOperator(&pRet, pPrev, pAnd);
120169          pPrev = pAnd;
120170        }
120171
120172        /* This test catches attempts to make either operand of a NEAR
120173        ** operator something other than a phrase. For example, either of
120174        ** the following:
120175        **
120176        **    (bracketed expression) NEAR phrase
120177        **    phrase NEAR (bracketed expression)
120178        **
120179        ** Return an error in either case.
120180        */
120181        if( pPrev && (
120182            (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
120183         || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
120184        )){
120185          sqlite3Fts3ExprFree(p);
120186          rc = SQLITE_ERROR;
120187          goto exprparse_out;
120188        }
120189
120190        if( isPhrase ){
120191          if( pRet ){
120192            assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
120193            pPrev->pRight = p;
120194            p->pParent = pPrev;
120195          }else{
120196            pRet = p;
120197          }
120198        }else{
120199          insertBinaryOperator(&pRet, pPrev, p);
120200        }
120201        isRequirePhrase = !isPhrase;
120202      }
120203      assert( nByte>0 );
120204    }
120205    assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
120206    nIn -= nByte;
120207    zIn += nByte;
120208    pPrev = p;
120209  }
120210
120211  if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
120212    rc = SQLITE_ERROR;
120213  }
120214
120215  if( rc==SQLITE_DONE ){
120216    rc = SQLITE_OK;
120217    if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
120218      if( !pRet ){
120219        rc = SQLITE_ERROR;
120220      }else{
120221        Fts3Expr *pIter = pNotBranch;
120222        while( pIter->pLeft ){
120223          pIter = pIter->pLeft;
120224        }
120225        pIter->pLeft = pRet;
120226        pRet = pNotBranch;
120227      }
120228    }
120229  }
120230  *pnConsumed = n - nIn;
120231
120232exprparse_out:
120233  if( rc!=SQLITE_OK ){
120234    sqlite3Fts3ExprFree(pRet);
120235    sqlite3Fts3ExprFree(pNotBranch);
120236    pRet = 0;
120237  }
120238  *ppExpr = pRet;
120239  return rc;
120240}
120241
120242/*
120243** Parameters z and n contain a pointer to and length of a buffer containing
120244** an fts3 query expression, respectively. This function attempts to parse the
120245** query expression and create a tree of Fts3Expr structures representing the
120246** parsed expression. If successful, *ppExpr is set to point to the head
120247** of the parsed expression tree and SQLITE_OK is returned. If an error
120248** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
120249** error) is returned and *ppExpr is set to 0.
120250**
120251** If parameter n is a negative number, then z is assumed to point to a
120252** nul-terminated string and the length is determined using strlen().
120253**
120254** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
120255** use to normalize query tokens while parsing the expression. The azCol[]
120256** array, which is assumed to contain nCol entries, should contain the names
120257** of each column in the target fts3 table, in order from left to right.
120258** Column names must be nul-terminated strings.
120259**
120260** The iDefaultCol parameter should be passed the index of the table column
120261** that appears on the left-hand-side of the MATCH operator (the default
120262** column to match against for tokens for which a column name is not explicitly
120263** specified as part of the query string), or -1 if tokens may by default
120264** match any table column.
120265*/
120266SQLITE_PRIVATE int sqlite3Fts3ExprParse(
120267  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
120268  char **azCol,                       /* Array of column names for fts3 table */
120269  int nCol,                           /* Number of entries in azCol[] */
120270  int iDefaultCol,                    /* Default column to query */
120271  const char *z, int n,               /* Text of MATCH query */
120272  Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
120273){
120274  int nParsed;
120275  int rc;
120276  ParseContext sParse;
120277  sParse.pTokenizer = pTokenizer;
120278  sParse.azCol = (const char **)azCol;
120279  sParse.nCol = nCol;
120280  sParse.iDefaultCol = iDefaultCol;
120281  sParse.nNest = 0;
120282  if( z==0 ){
120283    *ppExpr = 0;
120284    return SQLITE_OK;
120285  }
120286  if( n<0 ){
120287    n = (int)strlen(z);
120288  }
120289  rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
120290
120291  /* Check for mismatched parenthesis */
120292  if( rc==SQLITE_OK && sParse.nNest ){
120293    rc = SQLITE_ERROR;
120294    sqlite3Fts3ExprFree(*ppExpr);
120295    *ppExpr = 0;
120296  }
120297
120298  return rc;
120299}
120300
120301/*
120302** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
120303*/
120304SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
120305  if( p ){
120306    assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
120307    sqlite3Fts3ExprFree(p->pLeft);
120308    sqlite3Fts3ExprFree(p->pRight);
120309    sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
120310    sqlite3_free(p->aMI);
120311    sqlite3_free(p);
120312  }
120313}
120314
120315/****************************************************************************
120316*****************************************************************************
120317** Everything after this point is just test code.
120318*/
120319
120320#ifdef SQLITE_TEST
120321
120322/* #include <stdio.h> */
120323
120324/*
120325** Function to query the hash-table of tokenizers (see README.tokenizers).
120326*/
120327static int queryTestTokenizer(
120328  sqlite3 *db,
120329  const char *zName,
120330  const sqlite3_tokenizer_module **pp
120331){
120332  int rc;
120333  sqlite3_stmt *pStmt;
120334  const char zSql[] = "SELECT fts3_tokenizer(?)";
120335
120336  *pp = 0;
120337  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
120338  if( rc!=SQLITE_OK ){
120339    return rc;
120340  }
120341
120342  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
120343  if( SQLITE_ROW==sqlite3_step(pStmt) ){
120344    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
120345      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
120346    }
120347  }
120348
120349  return sqlite3_finalize(pStmt);
120350}
120351
120352/*
120353** Return a pointer to a buffer containing a text representation of the
120354** expression passed as the first argument. The buffer is obtained from
120355** sqlite3_malloc(). It is the responsibility of the caller to use
120356** sqlite3_free() to release the memory. If an OOM condition is encountered,
120357** NULL is returned.
120358**
120359** If the second argument is not NULL, then its contents are prepended to
120360** the returned expression text and then freed using sqlite3_free().
120361*/
120362static char *exprToString(Fts3Expr *pExpr, char *zBuf){
120363  switch( pExpr->eType ){
120364    case FTSQUERY_PHRASE: {
120365      Fts3Phrase *pPhrase = pExpr->pPhrase;
120366      int i;
120367      zBuf = sqlite3_mprintf(
120368          "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
120369      for(i=0; zBuf && i<pPhrase->nToken; i++){
120370        zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
120371            pPhrase->aToken[i].n, pPhrase->aToken[i].z,
120372            (pPhrase->aToken[i].isPrefix?"+":"")
120373        );
120374      }
120375      return zBuf;
120376    }
120377
120378    case FTSQUERY_NEAR:
120379      zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
120380      break;
120381    case FTSQUERY_NOT:
120382      zBuf = sqlite3_mprintf("%zNOT ", zBuf);
120383      break;
120384    case FTSQUERY_AND:
120385      zBuf = sqlite3_mprintf("%zAND ", zBuf);
120386      break;
120387    case FTSQUERY_OR:
120388      zBuf = sqlite3_mprintf("%zOR ", zBuf);
120389      break;
120390  }
120391
120392  if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
120393  if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
120394  if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
120395
120396  if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
120397  if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
120398
120399  return zBuf;
120400}
120401
120402/*
120403** This is the implementation of a scalar SQL function used to test the
120404** expression parser. It should be called as follows:
120405**
120406**   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
120407**
120408** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
120409** to parse the query expression (see README.tokenizers). The second argument
120410** is the query expression to parse. Each subsequent argument is the name
120411** of a column of the fts3 table that the query expression may refer to.
120412** For example:
120413**
120414**   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
120415*/
120416static void fts3ExprTest(
120417  sqlite3_context *context,
120418  int argc,
120419  sqlite3_value **argv
120420){
120421  sqlite3_tokenizer_module const *pModule = 0;
120422  sqlite3_tokenizer *pTokenizer = 0;
120423  int rc;
120424  char **azCol = 0;
120425  const char *zExpr;
120426  int nExpr;
120427  int nCol;
120428  int ii;
120429  Fts3Expr *pExpr;
120430  char *zBuf = 0;
120431  sqlite3 *db = sqlite3_context_db_handle(context);
120432
120433  if( argc<3 ){
120434    sqlite3_result_error(context,
120435        "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
120436    );
120437    return;
120438  }
120439
120440  rc = queryTestTokenizer(db,
120441                          (const char *)sqlite3_value_text(argv[0]), &pModule);
120442  if( rc==SQLITE_NOMEM ){
120443    sqlite3_result_error_nomem(context);
120444    goto exprtest_out;
120445  }else if( !pModule ){
120446    sqlite3_result_error(context, "No such tokenizer module", -1);
120447    goto exprtest_out;
120448  }
120449
120450  rc = pModule->xCreate(0, 0, &pTokenizer);
120451  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
120452  if( rc==SQLITE_NOMEM ){
120453    sqlite3_result_error_nomem(context);
120454    goto exprtest_out;
120455  }
120456  pTokenizer->pModule = pModule;
120457
120458  zExpr = (const char *)sqlite3_value_text(argv[1]);
120459  nExpr = sqlite3_value_bytes(argv[1]);
120460  nCol = argc-2;
120461  azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
120462  if( !azCol ){
120463    sqlite3_result_error_nomem(context);
120464    goto exprtest_out;
120465  }
120466  for(ii=0; ii<nCol; ii++){
120467    azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
120468  }
120469
120470  rc = sqlite3Fts3ExprParse(
120471      pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr
120472  );
120473  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
120474    sqlite3_result_error(context, "Error parsing expression", -1);
120475  }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
120476    sqlite3_result_error_nomem(context);
120477  }else{
120478    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
120479    sqlite3_free(zBuf);
120480  }
120481
120482  sqlite3Fts3ExprFree(pExpr);
120483
120484exprtest_out:
120485  if( pModule && pTokenizer ){
120486    rc = pModule->xDestroy(pTokenizer);
120487  }
120488  sqlite3_free(azCol);
120489}
120490
120491/*
120492** Register the query expression parser test function fts3_exprtest()
120493** with database connection db.
120494*/
120495SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
120496  return sqlite3_create_function(
120497      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
120498  );
120499}
120500
120501#endif
120502#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
120503
120504/************** End of fts3_expr.c *******************************************/
120505/************** Begin file fts3_hash.c ***************************************/
120506/*
120507** 2001 September 22
120508**
120509** The author disclaims copyright to this source code.  In place of
120510** a legal notice, here is a blessing:
120511**
120512**    May you do good and not evil.
120513**    May you find forgiveness for yourself and forgive others.
120514**    May you share freely, never taking more than you give.
120515**
120516*************************************************************************
120517** This is the implementation of generic hash-tables used in SQLite.
120518** We've modified it slightly to serve as a standalone hash table
120519** implementation for the full-text indexing module.
120520*/
120521
120522/*
120523** The code in this file is only compiled if:
120524**
120525**     * The FTS3 module is being built as an extension
120526**       (in which case SQLITE_CORE is not defined), or
120527**
120528**     * The FTS3 module is being built into the core of
120529**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120530*/
120531#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120532
120533/* #include <assert.h> */
120534/* #include <stdlib.h> */
120535/* #include <string.h> */
120536
120537
120538/*
120539** Malloc and Free functions
120540*/
120541static void *fts3HashMalloc(int n){
120542  void *p = sqlite3_malloc(n);
120543  if( p ){
120544    memset(p, 0, n);
120545  }
120546  return p;
120547}
120548static void fts3HashFree(void *p){
120549  sqlite3_free(p);
120550}
120551
120552/* Turn bulk memory into a hash table object by initializing the
120553** fields of the Hash structure.
120554**
120555** "pNew" is a pointer to the hash table that is to be initialized.
120556** keyClass is one of the constants
120557** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
120558** determines what kind of key the hash table will use.  "copyKey" is
120559** true if the hash table should make its own private copy of keys and
120560** false if it should just use the supplied pointer.
120561*/
120562SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
120563  assert( pNew!=0 );
120564  assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
120565  pNew->keyClass = keyClass;
120566  pNew->copyKey = copyKey;
120567  pNew->first = 0;
120568  pNew->count = 0;
120569  pNew->htsize = 0;
120570  pNew->ht = 0;
120571}
120572
120573/* Remove all entries from a hash table.  Reclaim all memory.
120574** Call this routine to delete a hash table or to reset a hash table
120575** to the empty state.
120576*/
120577SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
120578  Fts3HashElem *elem;         /* For looping over all elements of the table */
120579
120580  assert( pH!=0 );
120581  elem = pH->first;
120582  pH->first = 0;
120583  fts3HashFree(pH->ht);
120584  pH->ht = 0;
120585  pH->htsize = 0;
120586  while( elem ){
120587    Fts3HashElem *next_elem = elem->next;
120588    if( pH->copyKey && elem->pKey ){
120589      fts3HashFree(elem->pKey);
120590    }
120591    fts3HashFree(elem);
120592    elem = next_elem;
120593  }
120594  pH->count = 0;
120595}
120596
120597/*
120598** Hash and comparison functions when the mode is FTS3_HASH_STRING
120599*/
120600static int fts3StrHash(const void *pKey, int nKey){
120601  const char *z = (const char *)pKey;
120602  int h = 0;
120603  if( nKey<=0 ) nKey = (int) strlen(z);
120604  while( nKey > 0  ){
120605    h = (h<<3) ^ h ^ *z++;
120606    nKey--;
120607  }
120608  return h & 0x7fffffff;
120609}
120610static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
120611  if( n1!=n2 ) return 1;
120612  return strncmp((const char*)pKey1,(const char*)pKey2,n1);
120613}
120614
120615/*
120616** Hash and comparison functions when the mode is FTS3_HASH_BINARY
120617*/
120618static int fts3BinHash(const void *pKey, int nKey){
120619  int h = 0;
120620  const char *z = (const char *)pKey;
120621  while( nKey-- > 0 ){
120622    h = (h<<3) ^ h ^ *(z++);
120623  }
120624  return h & 0x7fffffff;
120625}
120626static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
120627  if( n1!=n2 ) return 1;
120628  return memcmp(pKey1,pKey2,n1);
120629}
120630
120631/*
120632** Return a pointer to the appropriate hash function given the key class.
120633**
120634** The C syntax in this function definition may be unfamilar to some
120635** programmers, so we provide the following additional explanation:
120636**
120637** The name of the function is "ftsHashFunction".  The function takes a
120638** single parameter "keyClass".  The return value of ftsHashFunction()
120639** is a pointer to another function.  Specifically, the return value
120640** of ftsHashFunction() is a pointer to a function that takes two parameters
120641** with types "const void*" and "int" and returns an "int".
120642*/
120643static int (*ftsHashFunction(int keyClass))(const void*,int){
120644  if( keyClass==FTS3_HASH_STRING ){
120645    return &fts3StrHash;
120646  }else{
120647    assert( keyClass==FTS3_HASH_BINARY );
120648    return &fts3BinHash;
120649  }
120650}
120651
120652/*
120653** Return a pointer to the appropriate hash function given the key class.
120654**
120655** For help in interpreted the obscure C code in the function definition,
120656** see the header comment on the previous function.
120657*/
120658static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
120659  if( keyClass==FTS3_HASH_STRING ){
120660    return &fts3StrCompare;
120661  }else{
120662    assert( keyClass==FTS3_HASH_BINARY );
120663    return &fts3BinCompare;
120664  }
120665}
120666
120667/* Link an element into the hash table
120668*/
120669static void fts3HashInsertElement(
120670  Fts3Hash *pH,            /* The complete hash table */
120671  struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
120672  Fts3HashElem *pNew       /* The element to be inserted */
120673){
120674  Fts3HashElem *pHead;     /* First element already in pEntry */
120675  pHead = pEntry->chain;
120676  if( pHead ){
120677    pNew->next = pHead;
120678    pNew->prev = pHead->prev;
120679    if( pHead->prev ){ pHead->prev->next = pNew; }
120680    else             { pH->first = pNew; }
120681    pHead->prev = pNew;
120682  }else{
120683    pNew->next = pH->first;
120684    if( pH->first ){ pH->first->prev = pNew; }
120685    pNew->prev = 0;
120686    pH->first = pNew;
120687  }
120688  pEntry->count++;
120689  pEntry->chain = pNew;
120690}
120691
120692
120693/* Resize the hash table so that it cantains "new_size" buckets.
120694** "new_size" must be a power of 2.  The hash table might fail
120695** to resize if sqliteMalloc() fails.
120696**
120697** Return non-zero if a memory allocation error occurs.
120698*/
120699static int fts3Rehash(Fts3Hash *pH, int new_size){
120700  struct _fts3ht *new_ht;          /* The new hash table */
120701  Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
120702  int (*xHash)(const void*,int);   /* The hash function */
120703
120704  assert( (new_size & (new_size-1))==0 );
120705  new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
120706  if( new_ht==0 ) return 1;
120707  fts3HashFree(pH->ht);
120708  pH->ht = new_ht;
120709  pH->htsize = new_size;
120710  xHash = ftsHashFunction(pH->keyClass);
120711  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
120712    int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
120713    next_elem = elem->next;
120714    fts3HashInsertElement(pH, &new_ht[h], elem);
120715  }
120716  return 0;
120717}
120718
120719/* This function (for internal use only) locates an element in an
120720** hash table that matches the given key.  The hash for this key has
120721** already been computed and is passed as the 4th parameter.
120722*/
120723static Fts3HashElem *fts3FindElementByHash(
120724  const Fts3Hash *pH, /* The pH to be searched */
120725  const void *pKey,   /* The key we are searching for */
120726  int nKey,
120727  int h               /* The hash for this key. */
120728){
120729  Fts3HashElem *elem;            /* Used to loop thru the element list */
120730  int count;                     /* Number of elements left to test */
120731  int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
120732
120733  if( pH->ht ){
120734    struct _fts3ht *pEntry = &pH->ht[h];
120735    elem = pEntry->chain;
120736    count = pEntry->count;
120737    xCompare = ftsCompareFunction(pH->keyClass);
120738    while( count-- && elem ){
120739      if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
120740        return elem;
120741      }
120742      elem = elem->next;
120743    }
120744  }
120745  return 0;
120746}
120747
120748/* Remove a single entry from the hash table given a pointer to that
120749** element and a hash on the element's key.
120750*/
120751static void fts3RemoveElementByHash(
120752  Fts3Hash *pH,         /* The pH containing "elem" */
120753  Fts3HashElem* elem,   /* The element to be removed from the pH */
120754  int h                 /* Hash value for the element */
120755){
120756  struct _fts3ht *pEntry;
120757  if( elem->prev ){
120758    elem->prev->next = elem->next;
120759  }else{
120760    pH->first = elem->next;
120761  }
120762  if( elem->next ){
120763    elem->next->prev = elem->prev;
120764  }
120765  pEntry = &pH->ht[h];
120766  if( pEntry->chain==elem ){
120767    pEntry->chain = elem->next;
120768  }
120769  pEntry->count--;
120770  if( pEntry->count<=0 ){
120771    pEntry->chain = 0;
120772  }
120773  if( pH->copyKey && elem->pKey ){
120774    fts3HashFree(elem->pKey);
120775  }
120776  fts3HashFree( elem );
120777  pH->count--;
120778  if( pH->count<=0 ){
120779    assert( pH->first==0 );
120780    assert( pH->count==0 );
120781    fts3HashClear(pH);
120782  }
120783}
120784
120785SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
120786  const Fts3Hash *pH,
120787  const void *pKey,
120788  int nKey
120789){
120790  int h;                          /* A hash on key */
120791  int (*xHash)(const void*,int);  /* The hash function */
120792
120793  if( pH==0 || pH->ht==0 ) return 0;
120794  xHash = ftsHashFunction(pH->keyClass);
120795  assert( xHash!=0 );
120796  h = (*xHash)(pKey,nKey);
120797  assert( (pH->htsize & (pH->htsize-1))==0 );
120798  return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
120799}
120800
120801/*
120802** Attempt to locate an element of the hash table pH with a key
120803** that matches pKey,nKey.  Return the data for this element if it is
120804** found, or NULL if there is no match.
120805*/
120806SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
120807  Fts3HashElem *pElem;            /* The element that matches key (if any) */
120808
120809  pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
120810  return pElem ? pElem->data : 0;
120811}
120812
120813/* Insert an element into the hash table pH.  The key is pKey,nKey
120814** and the data is "data".
120815**
120816** If no element exists with a matching key, then a new
120817** element is created.  A copy of the key is made if the copyKey
120818** flag is set.  NULL is returned.
120819**
120820** If another element already exists with the same key, then the
120821** new data replaces the old data and the old data is returned.
120822** The key is not copied in this instance.  If a malloc fails, then
120823** the new data is returned and the hash table is unchanged.
120824**
120825** If the "data" parameter to this function is NULL, then the
120826** element corresponding to "key" is removed from the hash table.
120827*/
120828SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
120829  Fts3Hash *pH,        /* The hash table to insert into */
120830  const void *pKey,    /* The key */
120831  int nKey,            /* Number of bytes in the key */
120832  void *data           /* The data */
120833){
120834  int hraw;                 /* Raw hash value of the key */
120835  int h;                    /* the hash of the key modulo hash table size */
120836  Fts3HashElem *elem;       /* Used to loop thru the element list */
120837  Fts3HashElem *new_elem;   /* New element added to the pH */
120838  int (*xHash)(const void*,int);  /* The hash function */
120839
120840  assert( pH!=0 );
120841  xHash = ftsHashFunction(pH->keyClass);
120842  assert( xHash!=0 );
120843  hraw = (*xHash)(pKey, nKey);
120844  assert( (pH->htsize & (pH->htsize-1))==0 );
120845  h = hraw & (pH->htsize-1);
120846  elem = fts3FindElementByHash(pH,pKey,nKey,h);
120847  if( elem ){
120848    void *old_data = elem->data;
120849    if( data==0 ){
120850      fts3RemoveElementByHash(pH,elem,h);
120851    }else{
120852      elem->data = data;
120853    }
120854    return old_data;
120855  }
120856  if( data==0 ) return 0;
120857  if( (pH->htsize==0 && fts3Rehash(pH,8))
120858   || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
120859  ){
120860    pH->count = 0;
120861    return data;
120862  }
120863  assert( pH->htsize>0 );
120864  new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
120865  if( new_elem==0 ) return data;
120866  if( pH->copyKey && pKey!=0 ){
120867    new_elem->pKey = fts3HashMalloc( nKey );
120868    if( new_elem->pKey==0 ){
120869      fts3HashFree(new_elem);
120870      return data;
120871    }
120872    memcpy((void*)new_elem->pKey, pKey, nKey);
120873  }else{
120874    new_elem->pKey = (void*)pKey;
120875  }
120876  new_elem->nKey = nKey;
120877  pH->count++;
120878  assert( pH->htsize>0 );
120879  assert( (pH->htsize & (pH->htsize-1))==0 );
120880  h = hraw & (pH->htsize-1);
120881  fts3HashInsertElement(pH, &pH->ht[h], new_elem);
120882  new_elem->data = data;
120883  return 0;
120884}
120885
120886#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
120887
120888/************** End of fts3_hash.c *******************************************/
120889/************** Begin file fts3_porter.c *************************************/
120890/*
120891** 2006 September 30
120892**
120893** The author disclaims copyright to this source code.  In place of
120894** a legal notice, here is a blessing:
120895**
120896**    May you do good and not evil.
120897**    May you find forgiveness for yourself and forgive others.
120898**    May you share freely, never taking more than you give.
120899**
120900*************************************************************************
120901** Implementation of the full-text-search tokenizer that implements
120902** a Porter stemmer.
120903*/
120904
120905/*
120906** The code in this file is only compiled if:
120907**
120908**     * The FTS3 module is being built as an extension
120909**       (in which case SQLITE_CORE is not defined), or
120910**
120911**     * The FTS3 module is being built into the core of
120912**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120913*/
120914#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120915
120916/* #include <assert.h> */
120917/* #include <stdlib.h> */
120918/* #include <stdio.h> */
120919/* #include <string.h> */
120920
120921
120922/*
120923** Class derived from sqlite3_tokenizer
120924*/
120925typedef struct porter_tokenizer {
120926  sqlite3_tokenizer base;      /* Base class */
120927} porter_tokenizer;
120928
120929/*
120930** Class derived from sqlit3_tokenizer_cursor
120931*/
120932typedef struct porter_tokenizer_cursor {
120933  sqlite3_tokenizer_cursor base;
120934  const char *zInput;          /* input we are tokenizing */
120935  int nInput;                  /* size of the input */
120936  int iOffset;                 /* current position in zInput */
120937  int iToken;                  /* index of next token to be returned */
120938  char *zToken;                /* storage for current token */
120939  int nAllocated;              /* space allocated to zToken buffer */
120940} porter_tokenizer_cursor;
120941
120942
120943/*
120944** Create a new tokenizer instance.
120945*/
120946static int porterCreate(
120947  int argc, const char * const *argv,
120948  sqlite3_tokenizer **ppTokenizer
120949){
120950  porter_tokenizer *t;
120951
120952  UNUSED_PARAMETER(argc);
120953  UNUSED_PARAMETER(argv);
120954
120955  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
120956  if( t==NULL ) return SQLITE_NOMEM;
120957  memset(t, 0, sizeof(*t));
120958  *ppTokenizer = &t->base;
120959  return SQLITE_OK;
120960}
120961
120962/*
120963** Destroy a tokenizer
120964*/
120965static int porterDestroy(sqlite3_tokenizer *pTokenizer){
120966  sqlite3_free(pTokenizer);
120967  return SQLITE_OK;
120968}
120969
120970/*
120971** Prepare to begin tokenizing a particular string.  The input
120972** string to be tokenized is zInput[0..nInput-1].  A cursor
120973** used to incrementally tokenize this string is returned in
120974** *ppCursor.
120975*/
120976static int porterOpen(
120977  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
120978  const char *zInput, int nInput,        /* String to be tokenized */
120979  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
120980){
120981  porter_tokenizer_cursor *c;
120982
120983  UNUSED_PARAMETER(pTokenizer);
120984
120985  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
120986  if( c==NULL ) return SQLITE_NOMEM;
120987
120988  c->zInput = zInput;
120989  if( zInput==0 ){
120990    c->nInput = 0;
120991  }else if( nInput<0 ){
120992    c->nInput = (int)strlen(zInput);
120993  }else{
120994    c->nInput = nInput;
120995  }
120996  c->iOffset = 0;                 /* start tokenizing at the beginning */
120997  c->iToken = 0;
120998  c->zToken = NULL;               /* no space allocated, yet. */
120999  c->nAllocated = 0;
121000
121001  *ppCursor = &c->base;
121002  return SQLITE_OK;
121003}
121004
121005/*
121006** Close a tokenization cursor previously opened by a call to
121007** porterOpen() above.
121008*/
121009static int porterClose(sqlite3_tokenizer_cursor *pCursor){
121010  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
121011  sqlite3_free(c->zToken);
121012  sqlite3_free(c);
121013  return SQLITE_OK;
121014}
121015/*
121016** Vowel or consonant
121017*/
121018static const char cType[] = {
121019   0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
121020   1, 1, 1, 2, 1
121021};
121022
121023/*
121024** isConsonant() and isVowel() determine if their first character in
121025** the string they point to is a consonant or a vowel, according
121026** to Porter ruls.
121027**
121028** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
121029** 'Y' is a consonant unless it follows another consonant,
121030** in which case it is a vowel.
121031**
121032** In these routine, the letters are in reverse order.  So the 'y' rule
121033** is that 'y' is a consonant unless it is followed by another
121034** consonent.
121035*/
121036static int isVowel(const char*);
121037static int isConsonant(const char *z){
121038  int j;
121039  char x = *z;
121040  if( x==0 ) return 0;
121041  assert( x>='a' && x<='z' );
121042  j = cType[x-'a'];
121043  if( j<2 ) return j;
121044  return z[1]==0 || isVowel(z + 1);
121045}
121046static int isVowel(const char *z){
121047  int j;
121048  char x = *z;
121049  if( x==0 ) return 0;
121050  assert( x>='a' && x<='z' );
121051  j = cType[x-'a'];
121052  if( j<2 ) return 1-j;
121053  return isConsonant(z + 1);
121054}
121055
121056/*
121057** Let any sequence of one or more vowels be represented by V and let
121058** C be sequence of one or more consonants.  Then every word can be
121059** represented as:
121060**
121061**           [C] (VC){m} [V]
121062**
121063** In prose:  A word is an optional consonant followed by zero or
121064** vowel-consonant pairs followed by an optional vowel.  "m" is the
121065** number of vowel consonant pairs.  This routine computes the value
121066** of m for the first i bytes of a word.
121067**
121068** Return true if the m-value for z is 1 or more.  In other words,
121069** return true if z contains at least one vowel that is followed
121070** by a consonant.
121071**
121072** In this routine z[] is in reverse order.  So we are really looking
121073** for an instance of of a consonant followed by a vowel.
121074*/
121075static int m_gt_0(const char *z){
121076  while( isVowel(z) ){ z++; }
121077  if( *z==0 ) return 0;
121078  while( isConsonant(z) ){ z++; }
121079  return *z!=0;
121080}
121081
121082/* Like mgt0 above except we are looking for a value of m which is
121083** exactly 1
121084*/
121085static int m_eq_1(const char *z){
121086  while( isVowel(z) ){ z++; }
121087  if( *z==0 ) return 0;
121088  while( isConsonant(z) ){ z++; }
121089  if( *z==0 ) return 0;
121090  while( isVowel(z) ){ z++; }
121091  if( *z==0 ) return 1;
121092  while( isConsonant(z) ){ z++; }
121093  return *z==0;
121094}
121095
121096/* Like mgt0 above except we are looking for a value of m>1 instead
121097** or m>0
121098*/
121099static int m_gt_1(const char *z){
121100  while( isVowel(z) ){ z++; }
121101  if( *z==0 ) return 0;
121102  while( isConsonant(z) ){ z++; }
121103  if( *z==0 ) return 0;
121104  while( isVowel(z) ){ z++; }
121105  if( *z==0 ) return 0;
121106  while( isConsonant(z) ){ z++; }
121107  return *z!=0;
121108}
121109
121110/*
121111** Return TRUE if there is a vowel anywhere within z[0..n-1]
121112*/
121113static int hasVowel(const char *z){
121114  while( isConsonant(z) ){ z++; }
121115  return *z!=0;
121116}
121117
121118/*
121119** Return TRUE if the word ends in a double consonant.
121120**
121121** The text is reversed here. So we are really looking at
121122** the first two characters of z[].
121123*/
121124static int doubleConsonant(const char *z){
121125  return isConsonant(z) && z[0]==z[1];
121126}
121127
121128/*
121129** Return TRUE if the word ends with three letters which
121130** are consonant-vowel-consonent and where the final consonant
121131** is not 'w', 'x', or 'y'.
121132**
121133** The word is reversed here.  So we are really checking the
121134** first three letters and the first one cannot be in [wxy].
121135*/
121136static int star_oh(const char *z){
121137  return
121138    isConsonant(z) &&
121139    z[0]!='w' && z[0]!='x' && z[0]!='y' &&
121140    isVowel(z+1) &&
121141    isConsonant(z+2);
121142}
121143
121144/*
121145** If the word ends with zFrom and xCond() is true for the stem
121146** of the word that preceeds the zFrom ending, then change the
121147** ending to zTo.
121148**
121149** The input word *pz and zFrom are both in reverse order.  zTo
121150** is in normal order.
121151**
121152** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
121153** match.  Not that TRUE is returned even if xCond() fails and
121154** no substitution occurs.
121155*/
121156static int stem(
121157  char **pz,             /* The word being stemmed (Reversed) */
121158  const char *zFrom,     /* If the ending matches this... (Reversed) */
121159  const char *zTo,       /* ... change the ending to this (not reversed) */
121160  int (*xCond)(const char*)   /* Condition that must be true */
121161){
121162  char *z = *pz;
121163  while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
121164  if( *zFrom!=0 ) return 0;
121165  if( xCond && !xCond(z) ) return 1;
121166  while( *zTo ){
121167    *(--z) = *(zTo++);
121168  }
121169  *pz = z;
121170  return 1;
121171}
121172
121173/*
121174** This is the fallback stemmer used when the porter stemmer is
121175** inappropriate.  The input word is copied into the output with
121176** US-ASCII case folding.  If the input word is too long (more
121177** than 20 bytes if it contains no digits or more than 6 bytes if
121178** it contains digits) then word is truncated to 20 or 6 bytes
121179** by taking 10 or 3 bytes from the beginning and end.
121180*/
121181static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
121182  int i, mx, j;
121183  int hasDigit = 0;
121184  for(i=0; i<nIn; i++){
121185    char c = zIn[i];
121186    if( c>='A' && c<='Z' ){
121187      zOut[i] = c - 'A' + 'a';
121188    }else{
121189      if( c>='0' && c<='9' ) hasDigit = 1;
121190      zOut[i] = c;
121191    }
121192  }
121193  mx = hasDigit ? 3 : 10;
121194  if( nIn>mx*2 ){
121195    for(j=mx, i=nIn-mx; i<nIn; i++, j++){
121196      zOut[j] = zOut[i];
121197    }
121198    i = j;
121199  }
121200  zOut[i] = 0;
121201  *pnOut = i;
121202}
121203
121204
121205/*
121206** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
121207** zOut is at least big enough to hold nIn bytes.  Write the actual
121208** size of the output word (exclusive of the '\0' terminator) into *pnOut.
121209**
121210** Any upper-case characters in the US-ASCII character set ([A-Z])
121211** are converted to lower case.  Upper-case UTF characters are
121212** unchanged.
121213**
121214** Words that are longer than about 20 bytes are stemmed by retaining
121215** a few bytes from the beginning and the end of the word.  If the
121216** word contains digits, 3 bytes are taken from the beginning and
121217** 3 bytes from the end.  For long words without digits, 10 bytes
121218** are taken from each end.  US-ASCII case folding still applies.
121219**
121220** If the input word contains not digits but does characters not
121221** in [a-zA-Z] then no stemming is attempted and this routine just
121222** copies the input into the input into the output with US-ASCII
121223** case folding.
121224**
121225** Stemming never increases the length of the word.  So there is
121226** no chance of overflowing the zOut buffer.
121227*/
121228static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
121229  int i, j;
121230  char zReverse[28];
121231  char *z, *z2;
121232  if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
121233    /* The word is too big or too small for the porter stemmer.
121234    ** Fallback to the copy stemmer */
121235    copy_stemmer(zIn, nIn, zOut, pnOut);
121236    return;
121237  }
121238  for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
121239    char c = zIn[i];
121240    if( c>='A' && c<='Z' ){
121241      zReverse[j] = c + 'a' - 'A';
121242    }else if( c>='a' && c<='z' ){
121243      zReverse[j] = c;
121244    }else{
121245      /* The use of a character not in [a-zA-Z] means that we fallback
121246      ** to the copy stemmer */
121247      copy_stemmer(zIn, nIn, zOut, pnOut);
121248      return;
121249    }
121250  }
121251  memset(&zReverse[sizeof(zReverse)-5], 0, 5);
121252  z = &zReverse[j+1];
121253
121254
121255  /* Step 1a */
121256  if( z[0]=='s' ){
121257    if(
121258     !stem(&z, "sess", "ss", 0) &&
121259     !stem(&z, "sei", "i", 0)  &&
121260     !stem(&z, "ss", "ss", 0)
121261    ){
121262      z++;
121263    }
121264  }
121265
121266  /* Step 1b */
121267  z2 = z;
121268  if( stem(&z, "dee", "ee", m_gt_0) ){
121269    /* Do nothing.  The work was all in the test */
121270  }else if(
121271     (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
121272      && z!=z2
121273  ){
121274     if( stem(&z, "ta", "ate", 0) ||
121275         stem(&z, "lb", "ble", 0) ||
121276         stem(&z, "zi", "ize", 0) ){
121277       /* Do nothing.  The work was all in the test */
121278     }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
121279       z++;
121280     }else if( m_eq_1(z) && star_oh(z) ){
121281       *(--z) = 'e';
121282     }
121283  }
121284
121285  /* Step 1c */
121286  if( z[0]=='y' && hasVowel(z+1) ){
121287    z[0] = 'i';
121288  }
121289
121290  /* Step 2 */
121291  switch( z[1] ){
121292   case 'a':
121293     stem(&z, "lanoita", "ate", m_gt_0) ||
121294     stem(&z, "lanoit", "tion", m_gt_0);
121295     break;
121296   case 'c':
121297     stem(&z, "icne", "ence", m_gt_0) ||
121298     stem(&z, "icna", "ance", m_gt_0);
121299     break;
121300   case 'e':
121301     stem(&z, "rezi", "ize", m_gt_0);
121302     break;
121303   case 'g':
121304     stem(&z, "igol", "log", m_gt_0);
121305     break;
121306   case 'l':
121307     stem(&z, "ilb", "ble", m_gt_0) ||
121308     stem(&z, "illa", "al", m_gt_0) ||
121309     stem(&z, "iltne", "ent", m_gt_0) ||
121310     stem(&z, "ile", "e", m_gt_0) ||
121311     stem(&z, "ilsuo", "ous", m_gt_0);
121312     break;
121313   case 'o':
121314     stem(&z, "noitazi", "ize", m_gt_0) ||
121315     stem(&z, "noita", "ate", m_gt_0) ||
121316     stem(&z, "rota", "ate", m_gt_0);
121317     break;
121318   case 's':
121319     stem(&z, "msila", "al", m_gt_0) ||
121320     stem(&z, "ssenevi", "ive", m_gt_0) ||
121321     stem(&z, "ssenluf", "ful", m_gt_0) ||
121322     stem(&z, "ssensuo", "ous", m_gt_0);
121323     break;
121324   case 't':
121325     stem(&z, "itila", "al", m_gt_0) ||
121326     stem(&z, "itivi", "ive", m_gt_0) ||
121327     stem(&z, "itilib", "ble", m_gt_0);
121328     break;
121329  }
121330
121331  /* Step 3 */
121332  switch( z[0] ){
121333   case 'e':
121334     stem(&z, "etaci", "ic", m_gt_0) ||
121335     stem(&z, "evita", "", m_gt_0)   ||
121336     stem(&z, "ezila", "al", m_gt_0);
121337     break;
121338   case 'i':
121339     stem(&z, "itici", "ic", m_gt_0);
121340     break;
121341   case 'l':
121342     stem(&z, "laci", "ic", m_gt_0) ||
121343     stem(&z, "luf", "", m_gt_0);
121344     break;
121345   case 's':
121346     stem(&z, "ssen", "", m_gt_0);
121347     break;
121348  }
121349
121350  /* Step 4 */
121351  switch( z[1] ){
121352   case 'a':
121353     if( z[0]=='l' && m_gt_1(z+2) ){
121354       z += 2;
121355     }
121356     break;
121357   case 'c':
121358     if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
121359       z += 4;
121360     }
121361     break;
121362   case 'e':
121363     if( z[0]=='r' && m_gt_1(z+2) ){
121364       z += 2;
121365     }
121366     break;
121367   case 'i':
121368     if( z[0]=='c' && m_gt_1(z+2) ){
121369       z += 2;
121370     }
121371     break;
121372   case 'l':
121373     if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
121374       z += 4;
121375     }
121376     break;
121377   case 'n':
121378     if( z[0]=='t' ){
121379       if( z[2]=='a' ){
121380         if( m_gt_1(z+3) ){
121381           z += 3;
121382         }
121383       }else if( z[2]=='e' ){
121384         stem(&z, "tneme", "", m_gt_1) ||
121385         stem(&z, "tnem", "", m_gt_1) ||
121386         stem(&z, "tne", "", m_gt_1);
121387       }
121388     }
121389     break;
121390   case 'o':
121391     if( z[0]=='u' ){
121392       if( m_gt_1(z+2) ){
121393         z += 2;
121394       }
121395     }else if( z[3]=='s' || z[3]=='t' ){
121396       stem(&z, "noi", "", m_gt_1);
121397     }
121398     break;
121399   case 's':
121400     if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
121401       z += 3;
121402     }
121403     break;
121404   case 't':
121405     stem(&z, "eta", "", m_gt_1) ||
121406     stem(&z, "iti", "", m_gt_1);
121407     break;
121408   case 'u':
121409     if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
121410       z += 3;
121411     }
121412     break;
121413   case 'v':
121414   case 'z':
121415     if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
121416       z += 3;
121417     }
121418     break;
121419  }
121420
121421  /* Step 5a */
121422  if( z[0]=='e' ){
121423    if( m_gt_1(z+1) ){
121424      z++;
121425    }else if( m_eq_1(z+1) && !star_oh(z+1) ){
121426      z++;
121427    }
121428  }
121429
121430  /* Step 5b */
121431  if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
121432    z++;
121433  }
121434
121435  /* z[] is now the stemmed word in reverse order.  Flip it back
121436  ** around into forward order and return.
121437  */
121438  *pnOut = i = (int)strlen(z);
121439  zOut[i] = 0;
121440  while( *z ){
121441    zOut[--i] = *(z++);
121442  }
121443}
121444
121445/*
121446** Characters that can be part of a token.  We assume any character
121447** whose value is greater than 0x80 (any UTF character) can be
121448** part of a token.  In other words, delimiters all must have
121449** values of 0x7f or lower.
121450*/
121451static const char porterIdChar[] = {
121452/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
121453    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
121454    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
121455    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
121456    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
121457    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
121458};
121459#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
121460
121461/*
121462** Extract the next token from a tokenization cursor.  The cursor must
121463** have been opened by a prior call to porterOpen().
121464*/
121465static int porterNext(
121466  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
121467  const char **pzToken,               /* OUT: *pzToken is the token text */
121468  int *pnBytes,                       /* OUT: Number of bytes in token */
121469  int *piStartOffset,                 /* OUT: Starting offset of token */
121470  int *piEndOffset,                   /* OUT: Ending offset of token */
121471  int *piPosition                     /* OUT: Position integer of token */
121472){
121473  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
121474  const char *z = c->zInput;
121475
121476  while( c->iOffset<c->nInput ){
121477    int iStartOffset, ch;
121478
121479    /* Scan past delimiter characters */
121480    while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
121481      c->iOffset++;
121482    }
121483
121484    /* Count non-delimiter characters. */
121485    iStartOffset = c->iOffset;
121486    while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
121487      c->iOffset++;
121488    }
121489
121490    if( c->iOffset>iStartOffset ){
121491      int n = c->iOffset-iStartOffset;
121492      if( n>c->nAllocated ){
121493        char *pNew;
121494        c->nAllocated = n+20;
121495        pNew = sqlite3_realloc(c->zToken, c->nAllocated);
121496        if( !pNew ) return SQLITE_NOMEM;
121497        c->zToken = pNew;
121498      }
121499      porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
121500      *pzToken = c->zToken;
121501      *piStartOffset = iStartOffset;
121502      *piEndOffset = c->iOffset;
121503      *piPosition = c->iToken++;
121504      return SQLITE_OK;
121505    }
121506  }
121507  return SQLITE_DONE;
121508}
121509
121510/*
121511** The set of routines that implement the porter-stemmer tokenizer
121512*/
121513static const sqlite3_tokenizer_module porterTokenizerModule = {
121514  0,
121515  porterCreate,
121516  porterDestroy,
121517  porterOpen,
121518  porterClose,
121519  porterNext,
121520};
121521
121522/*
121523** Allocate a new porter tokenizer.  Return a pointer to the new
121524** tokenizer in *ppModule
121525*/
121526SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
121527  sqlite3_tokenizer_module const**ppModule
121528){
121529  *ppModule = &porterTokenizerModule;
121530}
121531
121532#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
121533
121534/************** End of fts3_porter.c *****************************************/
121535/************** Begin file fts3_tokenizer.c **********************************/
121536/*
121537** 2007 June 22
121538**
121539** The author disclaims copyright to this source code.  In place of
121540** a legal notice, here is a blessing:
121541**
121542**    May you do good and not evil.
121543**    May you find forgiveness for yourself and forgive others.
121544**    May you share freely, never taking more than you give.
121545**
121546******************************************************************************
121547**
121548** This is part of an SQLite module implementing full-text search.
121549** This particular file implements the generic tokenizer interface.
121550*/
121551
121552/*
121553** The code in this file is only compiled if:
121554**
121555**     * The FTS3 module is being built as an extension
121556**       (in which case SQLITE_CORE is not defined), or
121557**
121558**     * The FTS3 module is being built into the core of
121559**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
121560*/
121561#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
121562
121563/* #include <assert.h> */
121564/* #include <string.h> */
121565
121566/*
121567** Implementation of the SQL scalar function for accessing the underlying
121568** hash table. This function may be called as follows:
121569**
121570**   SELECT <function-name>(<key-name>);
121571**   SELECT <function-name>(<key-name>, <pointer>);
121572**
121573** where <function-name> is the name passed as the second argument
121574** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
121575**
121576** If the <pointer> argument is specified, it must be a blob value
121577** containing a pointer to be stored as the hash data corresponding
121578** to the string <key-name>. If <pointer> is not specified, then
121579** the string <key-name> must already exist in the has table. Otherwise,
121580** an error is returned.
121581**
121582** Whether or not the <pointer> argument is specified, the value returned
121583** is a blob containing the pointer stored as the hash data corresponding
121584** to string <key-name> (after the hash-table is updated, if applicable).
121585*/
121586static void scalarFunc(
121587  sqlite3_context *context,
121588  int argc,
121589  sqlite3_value **argv
121590){
121591  Fts3Hash *pHash;
121592  void *pPtr = 0;
121593  const unsigned char *zName;
121594  int nName;
121595
121596  assert( argc==1 || argc==2 );
121597
121598  pHash = (Fts3Hash *)sqlite3_user_data(context);
121599
121600  zName = sqlite3_value_text(argv[0]);
121601  nName = sqlite3_value_bytes(argv[0])+1;
121602
121603  if( argc==2 ){
121604    void *pOld;
121605    int n = sqlite3_value_bytes(argv[1]);
121606    if( n!=sizeof(pPtr) ){
121607      sqlite3_result_error(context, "argument type mismatch", -1);
121608      return;
121609    }
121610    pPtr = *(void **)sqlite3_value_blob(argv[1]);
121611    pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
121612    if( pOld==pPtr ){
121613      sqlite3_result_error(context, "out of memory", -1);
121614      return;
121615    }
121616  }else{
121617    pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
121618    if( !pPtr ){
121619      char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
121620      sqlite3_result_error(context, zErr, -1);
121621      sqlite3_free(zErr);
121622      return;
121623    }
121624  }
121625
121626  sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
121627}
121628
121629SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
121630  static const char isFtsIdChar[] = {
121631      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
121632      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
121633      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
121634      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
121635      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
121636      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
121637      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
121638      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
121639  };
121640  return (c&0x80 || isFtsIdChar[(int)(c)]);
121641}
121642
121643SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
121644  const char *z1;
121645  const char *z2 = 0;
121646
121647  /* Find the start of the next token. */
121648  z1 = zStr;
121649  while( z2==0 ){
121650    char c = *z1;
121651    switch( c ){
121652      case '\0': return 0;        /* No more tokens here */
121653      case '\'':
121654      case '"':
121655      case '`': {
121656        z2 = z1;
121657        while( *++z2 && (*z2!=c || *++z2==c) );
121658        break;
121659      }
121660      case '[':
121661        z2 = &z1[1];
121662        while( *z2 && z2[0]!=']' ) z2++;
121663        if( *z2 ) z2++;
121664        break;
121665
121666      default:
121667        if( sqlite3Fts3IsIdChar(*z1) ){
121668          z2 = &z1[1];
121669          while( sqlite3Fts3IsIdChar(*z2) ) z2++;
121670        }else{
121671          z1++;
121672        }
121673    }
121674  }
121675
121676  *pn = (int)(z2-z1);
121677  return z1;
121678}
121679
121680SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
121681  Fts3Hash *pHash,                /* Tokenizer hash table */
121682  const char *zArg,               /* Tokenizer name */
121683  sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
121684  char **pzErr                    /* OUT: Set to malloced error message */
121685){
121686  int rc;
121687  char *z = (char *)zArg;
121688  int n = 0;
121689  char *zCopy;
121690  char *zEnd;                     /* Pointer to nul-term of zCopy */
121691  sqlite3_tokenizer_module *m;
121692
121693  zCopy = sqlite3_mprintf("%s", zArg);
121694  if( !zCopy ) return SQLITE_NOMEM;
121695  zEnd = &zCopy[strlen(zCopy)];
121696
121697  z = (char *)sqlite3Fts3NextToken(zCopy, &n);
121698  z[n] = '\0';
121699  sqlite3Fts3Dequote(z);
121700
121701  m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
121702  if( !m ){
121703    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
121704    rc = SQLITE_ERROR;
121705  }else{
121706    char const **aArg = 0;
121707    int iArg = 0;
121708    z = &z[n+1];
121709    while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
121710      int nNew = sizeof(char *)*(iArg+1);
121711      char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
121712      if( !aNew ){
121713        sqlite3_free(zCopy);
121714        sqlite3_free((void *)aArg);
121715        return SQLITE_NOMEM;
121716      }
121717      aArg = aNew;
121718      aArg[iArg++] = z;
121719      z[n] = '\0';
121720      sqlite3Fts3Dequote(z);
121721      z = &z[n+1];
121722    }
121723    rc = m->xCreate(iArg, aArg, ppTok);
121724    assert( rc!=SQLITE_OK || *ppTok );
121725    if( rc!=SQLITE_OK ){
121726      *pzErr = sqlite3_mprintf("unknown tokenizer");
121727    }else{
121728      (*ppTok)->pModule = m;
121729    }
121730    sqlite3_free((void *)aArg);
121731  }
121732
121733  sqlite3_free(zCopy);
121734  return rc;
121735}
121736
121737
121738#ifdef SQLITE_TEST
121739
121740/* #include <tcl.h> */
121741/* #include <string.h> */
121742
121743/*
121744** Implementation of a special SQL scalar function for testing tokenizers
121745** designed to be used in concert with the Tcl testing framework. This
121746** function must be called with two arguments:
121747**
121748**   SELECT <function-name>(<key-name>, <input-string>);
121749**   SELECT <function-name>(<key-name>, <pointer>);
121750**
121751** where <function-name> is the name passed as the second argument
121752** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
121753** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
121754**
121755** The return value is a string that may be interpreted as a Tcl
121756** list. For each token in the <input-string>, three elements are
121757** added to the returned list. The first is the token position, the
121758** second is the token text (folded, stemmed, etc.) and the third is the
121759** substring of <input-string> associated with the token. For example,
121760** using the built-in "simple" tokenizer:
121761**
121762**   SELECT fts_tokenizer_test('simple', 'I don't see how');
121763**
121764** will return the string:
121765**
121766**   "{0 i I 1 dont don't 2 see see 3 how how}"
121767**
121768*/
121769static void testFunc(
121770  sqlite3_context *context,
121771  int argc,
121772  sqlite3_value **argv
121773){
121774  Fts3Hash *pHash;
121775  sqlite3_tokenizer_module *p;
121776  sqlite3_tokenizer *pTokenizer = 0;
121777  sqlite3_tokenizer_cursor *pCsr = 0;
121778
121779  const char *zErr = 0;
121780
121781  const char *zName;
121782  int nName;
121783  const char *zInput;
121784  int nInput;
121785
121786  const char *zArg = 0;
121787
121788  const char *zToken;
121789  int nToken;
121790  int iStart;
121791  int iEnd;
121792  int iPos;
121793
121794  Tcl_Obj *pRet;
121795
121796  assert( argc==2 || argc==3 );
121797
121798  nName = sqlite3_value_bytes(argv[0]);
121799  zName = (const char *)sqlite3_value_text(argv[0]);
121800  nInput = sqlite3_value_bytes(argv[argc-1]);
121801  zInput = (const char *)sqlite3_value_text(argv[argc-1]);
121802
121803  if( argc==3 ){
121804    zArg = (const char *)sqlite3_value_text(argv[1]);
121805  }
121806
121807  pHash = (Fts3Hash *)sqlite3_user_data(context);
121808  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
121809
121810  if( !p ){
121811    char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
121812    sqlite3_result_error(context, zErr, -1);
121813    sqlite3_free(zErr);
121814    return;
121815  }
121816
121817  pRet = Tcl_NewObj();
121818  Tcl_IncrRefCount(pRet);
121819
121820  if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
121821    zErr = "error in xCreate()";
121822    goto finish;
121823  }
121824  pTokenizer->pModule = p;
121825  if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
121826    zErr = "error in xOpen()";
121827    goto finish;
121828  }
121829  pCsr->pTokenizer = pTokenizer;
121830
121831  while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
121832    Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
121833    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
121834    zToken = &zInput[iStart];
121835    nToken = iEnd-iStart;
121836    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
121837  }
121838
121839  if( SQLITE_OK!=p->xClose(pCsr) ){
121840    zErr = "error in xClose()";
121841    goto finish;
121842  }
121843  if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
121844    zErr = "error in xDestroy()";
121845    goto finish;
121846  }
121847
121848finish:
121849  if( zErr ){
121850    sqlite3_result_error(context, zErr, -1);
121851  }else{
121852    sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
121853  }
121854  Tcl_DecrRefCount(pRet);
121855}
121856
121857static
121858int registerTokenizer(
121859  sqlite3 *db,
121860  char *zName,
121861  const sqlite3_tokenizer_module *p
121862){
121863  int rc;
121864  sqlite3_stmt *pStmt;
121865  const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
121866
121867  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
121868  if( rc!=SQLITE_OK ){
121869    return rc;
121870  }
121871
121872  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
121873  sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
121874  sqlite3_step(pStmt);
121875
121876  return sqlite3_finalize(pStmt);
121877}
121878
121879static
121880int queryTokenizer(
121881  sqlite3 *db,
121882  char *zName,
121883  const sqlite3_tokenizer_module **pp
121884){
121885  int rc;
121886  sqlite3_stmt *pStmt;
121887  const char zSql[] = "SELECT fts3_tokenizer(?)";
121888
121889  *pp = 0;
121890  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
121891  if( rc!=SQLITE_OK ){
121892    return rc;
121893  }
121894
121895  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
121896  if( SQLITE_ROW==sqlite3_step(pStmt) ){
121897    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
121898      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
121899    }
121900  }
121901
121902  return sqlite3_finalize(pStmt);
121903}
121904
121905SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
121906
121907/*
121908** Implementation of the scalar function fts3_tokenizer_internal_test().
121909** This function is used for testing only, it is not included in the
121910** build unless SQLITE_TEST is defined.
121911**
121912** The purpose of this is to test that the fts3_tokenizer() function
121913** can be used as designed by the C-code in the queryTokenizer and
121914** registerTokenizer() functions above. These two functions are repeated
121915** in the README.tokenizer file as an example, so it is important to
121916** test them.
121917**
121918** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
121919** function with no arguments. An assert() will fail if a problem is
121920** detected. i.e.:
121921**
121922**     SELECT fts3_tokenizer_internal_test();
121923**
121924*/
121925static void intTestFunc(
121926  sqlite3_context *context,
121927  int argc,
121928  sqlite3_value **argv
121929){
121930  int rc;
121931  const sqlite3_tokenizer_module *p1;
121932  const sqlite3_tokenizer_module *p2;
121933  sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
121934
121935  UNUSED_PARAMETER(argc);
121936  UNUSED_PARAMETER(argv);
121937
121938  /* Test the query function */
121939  sqlite3Fts3SimpleTokenizerModule(&p1);
121940  rc = queryTokenizer(db, "simple", &p2);
121941  assert( rc==SQLITE_OK );
121942  assert( p1==p2 );
121943  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
121944  assert( rc==SQLITE_ERROR );
121945  assert( p2==0 );
121946  assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
121947
121948  /* Test the storage function */
121949  rc = registerTokenizer(db, "nosuchtokenizer", p1);
121950  assert( rc==SQLITE_OK );
121951  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
121952  assert( rc==SQLITE_OK );
121953  assert( p2==p1 );
121954
121955  sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
121956}
121957
121958#endif
121959
121960/*
121961** Set up SQL objects in database db used to access the contents of
121962** the hash table pointed to by argument pHash. The hash table must
121963** been initialised to use string keys, and to take a private copy
121964** of the key when a value is inserted. i.e. by a call similar to:
121965**
121966**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
121967**
121968** This function adds a scalar function (see header comment above
121969** scalarFunc() in this file for details) and, if ENABLE_TABLE is
121970** defined at compilation time, a temporary virtual table (see header
121971** comment above struct HashTableVtab) to the database schema. Both
121972** provide read/write access to the contents of *pHash.
121973**
121974** The third argument to this function, zName, is used as the name
121975** of both the scalar and, if created, the virtual table.
121976*/
121977SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
121978  sqlite3 *db,
121979  Fts3Hash *pHash,
121980  const char *zName
121981){
121982  int rc = SQLITE_OK;
121983  void *p = (void *)pHash;
121984  const int any = SQLITE_ANY;
121985
121986#ifdef SQLITE_TEST
121987  char *zTest = 0;
121988  char *zTest2 = 0;
121989  void *pdb = (void *)db;
121990  zTest = sqlite3_mprintf("%s_test", zName);
121991  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
121992  if( !zTest || !zTest2 ){
121993    rc = SQLITE_NOMEM;
121994  }
121995#endif
121996
121997  if( SQLITE_OK==rc ){
121998    rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
121999  }
122000  if( SQLITE_OK==rc ){
122001    rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
122002  }
122003#ifdef SQLITE_TEST
122004  if( SQLITE_OK==rc ){
122005    rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
122006  }
122007  if( SQLITE_OK==rc ){
122008    rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
122009  }
122010  if( SQLITE_OK==rc ){
122011    rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
122012  }
122013#endif
122014
122015#ifdef SQLITE_TEST
122016  sqlite3_free(zTest);
122017  sqlite3_free(zTest2);
122018#endif
122019
122020  return rc;
122021}
122022
122023#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
122024
122025/************** End of fts3_tokenizer.c **************************************/
122026/************** Begin file fts3_tokenizer1.c *********************************/
122027/*
122028** 2006 Oct 10
122029**
122030** The author disclaims copyright to this source code.  In place of
122031** a legal notice, here is a blessing:
122032**
122033**    May you do good and not evil.
122034**    May you find forgiveness for yourself and forgive others.
122035**    May you share freely, never taking more than you give.
122036**
122037******************************************************************************
122038**
122039** Implementation of the "simple" full-text-search tokenizer.
122040*/
122041
122042/*
122043** The code in this file is only compiled if:
122044**
122045**     * The FTS3 module is being built as an extension
122046**       (in which case SQLITE_CORE is not defined), or
122047**
122048**     * The FTS3 module is being built into the core of
122049**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
122050*/
122051#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
122052
122053/* #include <assert.h> */
122054/* #include <stdlib.h> */
122055/* #include <stdio.h> */
122056/* #include <string.h> */
122057
122058
122059typedef struct simple_tokenizer {
122060  sqlite3_tokenizer base;
122061  char delim[128];             /* flag ASCII delimiters */
122062} simple_tokenizer;
122063
122064typedef struct simple_tokenizer_cursor {
122065  sqlite3_tokenizer_cursor base;
122066  const char *pInput;          /* input we are tokenizing */
122067  int nBytes;                  /* size of the input */
122068  int iOffset;                 /* current position in pInput */
122069  int iToken;                  /* index of next token to be returned */
122070  char *pToken;                /* storage for current token */
122071  int nTokenAllocated;         /* space allocated to zToken buffer */
122072} simple_tokenizer_cursor;
122073
122074
122075static int simpleDelim(simple_tokenizer *t, unsigned char c){
122076  return c<0x80 && t->delim[c];
122077}
122078static int fts3_isalnum(int x){
122079  return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
122080}
122081
122082/*
122083** Create a new tokenizer instance.
122084*/
122085static int simpleCreate(
122086  int argc, const char * const *argv,
122087  sqlite3_tokenizer **ppTokenizer
122088){
122089  simple_tokenizer *t;
122090
122091  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
122092  if( t==NULL ) return SQLITE_NOMEM;
122093  memset(t, 0, sizeof(*t));
122094
122095  /* TODO(shess) Delimiters need to remain the same from run to run,
122096  ** else we need to reindex.  One solution would be a meta-table to
122097  ** track such information in the database, then we'd only want this
122098  ** information on the initial create.
122099  */
122100  if( argc>1 ){
122101    int i, n = (int)strlen(argv[1]);
122102    for(i=0; i<n; i++){
122103      unsigned char ch = argv[1][i];
122104      /* We explicitly don't support UTF-8 delimiters for now. */
122105      if( ch>=0x80 ){
122106        sqlite3_free(t);
122107        return SQLITE_ERROR;
122108      }
122109      t->delim[ch] = 1;
122110    }
122111  } else {
122112    /* Mark non-alphanumeric ASCII characters as delimiters */
122113    int i;
122114    for(i=1; i<0x80; i++){
122115      t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
122116    }
122117  }
122118
122119  *ppTokenizer = &t->base;
122120  return SQLITE_OK;
122121}
122122
122123/*
122124** Destroy a tokenizer
122125*/
122126static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
122127  sqlite3_free(pTokenizer);
122128  return SQLITE_OK;
122129}
122130
122131/*
122132** Prepare to begin tokenizing a particular string.  The input
122133** string to be tokenized is pInput[0..nBytes-1].  A cursor
122134** used to incrementally tokenize this string is returned in
122135** *ppCursor.
122136*/
122137static int simpleOpen(
122138  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
122139  const char *pInput, int nBytes,        /* String to be tokenized */
122140  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
122141){
122142  simple_tokenizer_cursor *c;
122143
122144  UNUSED_PARAMETER(pTokenizer);
122145
122146  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
122147  if( c==NULL ) return SQLITE_NOMEM;
122148
122149  c->pInput = pInput;
122150  if( pInput==0 ){
122151    c->nBytes = 0;
122152  }else if( nBytes<0 ){
122153    c->nBytes = (int)strlen(pInput);
122154  }else{
122155    c->nBytes = nBytes;
122156  }
122157  c->iOffset = 0;                 /* start tokenizing at the beginning */
122158  c->iToken = 0;
122159  c->pToken = NULL;               /* no space allocated, yet. */
122160  c->nTokenAllocated = 0;
122161
122162  *ppCursor = &c->base;
122163  return SQLITE_OK;
122164}
122165
122166/*
122167** Close a tokenization cursor previously opened by a call to
122168** simpleOpen() above.
122169*/
122170static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
122171  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
122172  sqlite3_free(c->pToken);
122173  sqlite3_free(c);
122174  return SQLITE_OK;
122175}
122176
122177/*
122178** Extract the next token from a tokenization cursor.  The cursor must
122179** have been opened by a prior call to simpleOpen().
122180*/
122181static int simpleNext(
122182  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
122183  const char **ppToken,               /* OUT: *ppToken is the token text */
122184  int *pnBytes,                       /* OUT: Number of bytes in token */
122185  int *piStartOffset,                 /* OUT: Starting offset of token */
122186  int *piEndOffset,                   /* OUT: Ending offset of token */
122187  int *piPosition                     /* OUT: Position integer of token */
122188){
122189  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
122190  simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
122191  unsigned char *p = (unsigned char *)c->pInput;
122192
122193  while( c->iOffset<c->nBytes ){
122194    int iStartOffset;
122195
122196    /* Scan past delimiter characters */
122197    while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
122198      c->iOffset++;
122199    }
122200
122201    /* Count non-delimiter characters. */
122202    iStartOffset = c->iOffset;
122203    while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
122204      c->iOffset++;
122205    }
122206
122207    if( c->iOffset>iStartOffset ){
122208      int i, n = c->iOffset-iStartOffset;
122209      if( n>c->nTokenAllocated ){
122210        char *pNew;
122211        c->nTokenAllocated = n+20;
122212        pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
122213        if( !pNew ) return SQLITE_NOMEM;
122214        c->pToken = pNew;
122215      }
122216      for(i=0; i<n; i++){
122217        /* TODO(shess) This needs expansion to handle UTF-8
122218        ** case-insensitivity.
122219        */
122220        unsigned char ch = p[iStartOffset+i];
122221        c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
122222      }
122223      *ppToken = c->pToken;
122224      *pnBytes = n;
122225      *piStartOffset = iStartOffset;
122226      *piEndOffset = c->iOffset;
122227      *piPosition = c->iToken++;
122228
122229      return SQLITE_OK;
122230    }
122231  }
122232  return SQLITE_DONE;
122233}
122234
122235/*
122236** The set of routines that implement the simple tokenizer
122237*/
122238static const sqlite3_tokenizer_module simpleTokenizerModule = {
122239  0,
122240  simpleCreate,
122241  simpleDestroy,
122242  simpleOpen,
122243  simpleClose,
122244  simpleNext,
122245};
122246
122247/*
122248** Allocate a new simple tokenizer.  Return a pointer to the new
122249** tokenizer in *ppModule
122250*/
122251SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
122252  sqlite3_tokenizer_module const**ppModule
122253){
122254  *ppModule = &simpleTokenizerModule;
122255}
122256
122257#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
122258
122259/************** End of fts3_tokenizer1.c *************************************/
122260/************** Begin file fts3_write.c **************************************/
122261/*
122262** 2009 Oct 23
122263**
122264** The author disclaims copyright to this source code.  In place of
122265** a legal notice, here is a blessing:
122266**
122267**    May you do good and not evil.
122268**    May you find forgiveness for yourself and forgive others.
122269**    May you share freely, never taking more than you give.
122270**
122271******************************************************************************
122272**
122273** This file is part of the SQLite FTS3 extension module. Specifically,
122274** this file contains code to insert, update and delete rows from FTS3
122275** tables. It also contains code to merge FTS3 b-tree segments. Some
122276** of the sub-routines used to merge segments are also used by the query
122277** code in fts3.c.
122278*/
122279
122280#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
122281
122282/* #include <string.h> */
122283/* #include <assert.h> */
122284/* #include <stdlib.h> */
122285
122286/*
122287** When full-text index nodes are loaded from disk, the buffer that they
122288** are loaded into has the following number of bytes of padding at the end
122289** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
122290** of 920 bytes is allocated for it.
122291**
122292** This means that if we have a pointer into a buffer containing node data,
122293** it is always safe to read up to two varints from it without risking an
122294** overread, even if the node data is corrupted.
122295*/
122296#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
122297
122298/*
122299** Under certain circumstances, b-tree nodes (doclists) can be loaded into
122300** memory incrementally instead of all at once. This can be a big performance
122301** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
122302** method before retrieving all query results (as may happen, for example,
122303** if a query has a LIMIT clause).
122304**
122305** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
122306** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
122307** The code is written so that the hard lower-limit for each of these values
122308** is 1. Clearly such small values would be inefficient, but can be useful
122309** for testing purposes.
122310**
122311** If this module is built with SQLITE_TEST defined, these constants may
122312** be overridden at runtime for testing purposes. File fts3_test.c contains
122313** a Tcl interface to read and write the values.
122314*/
122315#ifdef SQLITE_TEST
122316int test_fts3_node_chunksize = (4*1024);
122317int test_fts3_node_chunk_threshold = (4*1024)*4;
122318# define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
122319# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
122320#else
122321# define FTS3_NODE_CHUNKSIZE (4*1024)
122322# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
122323#endif
122324
122325typedef struct PendingList PendingList;
122326typedef struct SegmentNode SegmentNode;
122327typedef struct SegmentWriter SegmentWriter;
122328
122329/*
122330** An instance of the following data structure is used to build doclists
122331** incrementally. See function fts3PendingListAppend() for details.
122332*/
122333struct PendingList {
122334  int nData;
122335  char *aData;
122336  int nSpace;
122337  sqlite3_int64 iLastDocid;
122338  sqlite3_int64 iLastCol;
122339  sqlite3_int64 iLastPos;
122340};
122341
122342
122343/*
122344** Each cursor has a (possibly empty) linked list of the following objects.
122345*/
122346struct Fts3DeferredToken {
122347  Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
122348  int iCol;                       /* Column token must occur in */
122349  Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
122350  PendingList *pList;             /* Doclist is assembled here */
122351};
122352
122353/*
122354** An instance of this structure is used to iterate through the terms on
122355** a contiguous set of segment b-tree leaf nodes. Although the details of
122356** this structure are only manipulated by code in this file, opaque handles
122357** of type Fts3SegReader* are also used by code in fts3.c to iterate through
122358** terms when querying the full-text index. See functions:
122359**
122360**   sqlite3Fts3SegReaderNew()
122361**   sqlite3Fts3SegReaderFree()
122362**   sqlite3Fts3SegReaderIterate()
122363**
122364** Methods used to manipulate Fts3SegReader structures:
122365**
122366**   fts3SegReaderNext()
122367**   fts3SegReaderFirstDocid()
122368**   fts3SegReaderNextDocid()
122369*/
122370struct Fts3SegReader {
122371  int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
122372
122373  sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
122374  sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
122375  sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
122376  sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
122377
122378  char *aNode;                    /* Pointer to node data (or NULL) */
122379  int nNode;                      /* Size of buffer at aNode (or 0) */
122380  int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
122381  sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
122382
122383  Fts3HashElem **ppNextElem;
122384
122385  /* Variables set by fts3SegReaderNext(). These may be read directly
122386  ** by the caller. They are valid from the time SegmentReaderNew() returns
122387  ** until SegmentReaderNext() returns something other than SQLITE_OK
122388  ** (i.e. SQLITE_DONE).
122389  */
122390  int nTerm;                      /* Number of bytes in current term */
122391  char *zTerm;                    /* Pointer to current term */
122392  int nTermAlloc;                 /* Allocated size of zTerm buffer */
122393  char *aDoclist;                 /* Pointer to doclist of current entry */
122394  int nDoclist;                   /* Size of doclist in current entry */
122395
122396  /* The following variables are used by fts3SegReaderNextDocid() to iterate
122397  ** through the current doclist (aDoclist/nDoclist).
122398  */
122399  char *pOffsetList;
122400  int nOffsetList;                /* For descending pending seg-readers only */
122401  sqlite3_int64 iDocid;
122402};
122403
122404#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
122405#define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
122406
122407/*
122408** An instance of this structure is used to create a segment b-tree in the
122409** database. The internal details of this type are only accessed by the
122410** following functions:
122411**
122412**   fts3SegWriterAdd()
122413**   fts3SegWriterFlush()
122414**   fts3SegWriterFree()
122415*/
122416struct SegmentWriter {
122417  SegmentNode *pTree;             /* Pointer to interior tree structure */
122418  sqlite3_int64 iFirst;           /* First slot in %_segments written */
122419  sqlite3_int64 iFree;            /* Next free slot in %_segments */
122420  char *zTerm;                    /* Pointer to previous term buffer */
122421  int nTerm;                      /* Number of bytes in zTerm */
122422  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
122423  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
122424  int nSize;                      /* Size of allocation at aData */
122425  int nData;                      /* Bytes of data in aData */
122426  char *aData;                    /* Pointer to block from malloc() */
122427};
122428
122429/*
122430** Type SegmentNode is used by the following three functions to create
122431** the interior part of the segment b+-tree structures (everything except
122432** the leaf nodes). These functions and type are only ever used by code
122433** within the fts3SegWriterXXX() family of functions described above.
122434**
122435**   fts3NodeAddTerm()
122436**   fts3NodeWrite()
122437**   fts3NodeFree()
122438**
122439** When a b+tree is written to the database (either as a result of a merge
122440** or the pending-terms table being flushed), leaves are written into the
122441** database file as soon as they are completely populated. The interior of
122442** the tree is assembled in memory and written out only once all leaves have
122443** been populated and stored. This is Ok, as the b+-tree fanout is usually
122444** very large, meaning that the interior of the tree consumes relatively
122445** little memory.
122446*/
122447struct SegmentNode {
122448  SegmentNode *pParent;           /* Parent node (or NULL for root node) */
122449  SegmentNode *pRight;            /* Pointer to right-sibling */
122450  SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
122451  int nEntry;                     /* Number of terms written to node so far */
122452  char *zTerm;                    /* Pointer to previous term buffer */
122453  int nTerm;                      /* Number of bytes in zTerm */
122454  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
122455  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
122456  int nData;                      /* Bytes of valid data so far */
122457  char *aData;                    /* Node data */
122458};
122459
122460/*
122461** Valid values for the second argument to fts3SqlStmt().
122462*/
122463#define SQL_DELETE_CONTENT             0
122464#define SQL_IS_EMPTY                   1
122465#define SQL_DELETE_ALL_CONTENT         2
122466#define SQL_DELETE_ALL_SEGMENTS        3
122467#define SQL_DELETE_ALL_SEGDIR          4
122468#define SQL_DELETE_ALL_DOCSIZE         5
122469#define SQL_DELETE_ALL_STAT            6
122470#define SQL_SELECT_CONTENT_BY_ROWID    7
122471#define SQL_NEXT_SEGMENT_INDEX         8
122472#define SQL_INSERT_SEGMENTS            9
122473#define SQL_NEXT_SEGMENTS_ID          10
122474#define SQL_INSERT_SEGDIR             11
122475#define SQL_SELECT_LEVEL              12
122476#define SQL_SELECT_LEVEL_RANGE        13
122477#define SQL_SELECT_LEVEL_COUNT        14
122478#define SQL_SELECT_SEGDIR_MAX_LEVEL   15
122479#define SQL_DELETE_SEGDIR_LEVEL       16
122480#define SQL_DELETE_SEGMENTS_RANGE     17
122481#define SQL_CONTENT_INSERT            18
122482#define SQL_DELETE_DOCSIZE            19
122483#define SQL_REPLACE_DOCSIZE           20
122484#define SQL_SELECT_DOCSIZE            21
122485#define SQL_SELECT_DOCTOTAL           22
122486#define SQL_REPLACE_DOCTOTAL          23
122487
122488#define SQL_SELECT_ALL_PREFIX_LEVEL   24
122489#define SQL_DELETE_ALL_TERMS_SEGDIR   25
122490
122491#define SQL_DELETE_SEGDIR_RANGE       26
122492
122493/*
122494** This function is used to obtain an SQLite prepared statement handle
122495** for the statement identified by the second argument. If successful,
122496** *pp is set to the requested statement handle and SQLITE_OK returned.
122497** Otherwise, an SQLite error code is returned and *pp is set to 0.
122498**
122499** If argument apVal is not NULL, then it must point to an array with
122500** at least as many entries as the requested statement has bound
122501** parameters. The values are bound to the statements parameters before
122502** returning.
122503*/
122504static int fts3SqlStmt(
122505  Fts3Table *p,                   /* Virtual table handle */
122506  int eStmt,                      /* One of the SQL_XXX constants above */
122507  sqlite3_stmt **pp,              /* OUT: Statement handle */
122508  sqlite3_value **apVal           /* Values to bind to statement */
122509){
122510  const char *azSql[] = {
122511/* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
122512/* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
122513/* 2  */  "DELETE FROM %Q.'%q_content'",
122514/* 3  */  "DELETE FROM %Q.'%q_segments'",
122515/* 4  */  "DELETE FROM %Q.'%q_segdir'",
122516/* 5  */  "DELETE FROM %Q.'%q_docsize'",
122517/* 6  */  "DELETE FROM %Q.'%q_stat'",
122518/* 7  */  "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
122519/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
122520/* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
122521/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
122522/* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
122523
122524          /* Return segments in order from oldest to newest.*/
122525/* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
122526            "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
122527/* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
122528            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
122529            "ORDER BY level DESC, idx ASC",
122530
122531/* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
122532/* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
122533
122534/* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
122535/* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
122536/* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
122537/* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
122538/* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
122539/* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
122540/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=0",
122541/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
122542/* 24 */  "",
122543/* 25 */  "",
122544
122545/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
122546
122547  };
122548  int rc = SQLITE_OK;
122549  sqlite3_stmt *pStmt;
122550
122551  assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
122552  assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
122553
122554  pStmt = p->aStmt[eStmt];
122555  if( !pStmt ){
122556    char *zSql;
122557    if( eStmt==SQL_CONTENT_INSERT ){
122558      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
122559    }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
122560      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
122561    }else{
122562      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
122563    }
122564    if( !zSql ){
122565      rc = SQLITE_NOMEM;
122566    }else{
122567      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
122568      sqlite3_free(zSql);
122569      assert( rc==SQLITE_OK || pStmt==0 );
122570      p->aStmt[eStmt] = pStmt;
122571    }
122572  }
122573  if( apVal ){
122574    int i;
122575    int nParam = sqlite3_bind_parameter_count(pStmt);
122576    for(i=0; rc==SQLITE_OK && i<nParam; i++){
122577      rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
122578    }
122579  }
122580  *pp = pStmt;
122581  return rc;
122582}
122583
122584static int fts3SelectDocsize(
122585  Fts3Table *pTab,                /* FTS3 table handle */
122586  int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
122587  sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
122588  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
122589){
122590  sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
122591  int rc;                         /* Return code */
122592
122593  assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
122594
122595  rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
122596  if( rc==SQLITE_OK ){
122597    if( eStmt==SQL_SELECT_DOCSIZE ){
122598      sqlite3_bind_int64(pStmt, 1, iDocid);
122599    }
122600    rc = sqlite3_step(pStmt);
122601    if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
122602      rc = sqlite3_reset(pStmt);
122603      if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB;
122604      pStmt = 0;
122605    }else{
122606      rc = SQLITE_OK;
122607    }
122608  }
122609
122610  *ppStmt = pStmt;
122611  return rc;
122612}
122613
122614SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
122615  Fts3Table *pTab,                /* Fts3 table handle */
122616  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
122617){
122618  return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
122619}
122620
122621SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
122622  Fts3Table *pTab,                /* Fts3 table handle */
122623  sqlite3_int64 iDocid,           /* Docid to read size data for */
122624  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
122625){
122626  return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
122627}
122628
122629/*
122630** Similar to fts3SqlStmt(). Except, after binding the parameters in
122631** array apVal[] to the SQL statement identified by eStmt, the statement
122632** is executed.
122633**
122634** Returns SQLITE_OK if the statement is successfully executed, or an
122635** SQLite error code otherwise.
122636*/
122637static void fts3SqlExec(
122638  int *pRC,                /* Result code */
122639  Fts3Table *p,            /* The FTS3 table */
122640  int eStmt,               /* Index of statement to evaluate */
122641  sqlite3_value **apVal    /* Parameters to bind */
122642){
122643  sqlite3_stmt *pStmt;
122644  int rc;
122645  if( *pRC ) return;
122646  rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
122647  if( rc==SQLITE_OK ){
122648    sqlite3_step(pStmt);
122649    rc = sqlite3_reset(pStmt);
122650  }
122651  *pRC = rc;
122652}
122653
122654
122655/*
122656** This function ensures that the caller has obtained a shared-cache
122657** table-lock on the %_content table. This is required before reading
122658** data from the fts3 table. If this lock is not acquired first, then
122659** the caller may end up holding read-locks on the %_segments and %_segdir
122660** tables, but no read-lock on the %_content table. If this happens
122661** a second connection will be able to write to the fts3 table, but
122662** attempting to commit those writes might return SQLITE_LOCKED or
122663** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain
122664** write-locks on the %_segments and %_segdir ** tables).
122665**
122666** We try to avoid this because if FTS3 returns any error when committing
122667** a transaction, the whole transaction will be rolled back. And this is
122668** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
122669** still happen if the user reads data directly from the %_segments or
122670** %_segdir tables instead of going through FTS3 though.
122671*/
122672SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
122673  int rc;                         /* Return code */
122674  sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
122675
122676  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
122677  if( rc==SQLITE_OK ){
122678    sqlite3_bind_null(pStmt, 1);
122679    sqlite3_step(pStmt);
122680    rc = sqlite3_reset(pStmt);
122681  }
122682  return rc;
122683}
122684
122685/*
122686** Set *ppStmt to a statement handle that may be used to iterate through
122687** all rows in the %_segdir table, from oldest to newest. If successful,
122688** return SQLITE_OK. If an error occurs while preparing the statement,
122689** return an SQLite error code.
122690**
122691** There is only ever one instance of this SQL statement compiled for
122692** each FTS3 table.
122693**
122694** The statement returns the following columns from the %_segdir table:
122695**
122696**   0: idx
122697**   1: start_block
122698**   2: leaves_end_block
122699**   3: end_block
122700**   4: root
122701*/
122702SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
122703  Fts3Table *p,                   /* FTS3 table */
122704  int iIndex,                     /* Index for p->aIndex[] */
122705  int iLevel,                     /* Level to select */
122706  sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
122707){
122708  int rc;
122709  sqlite3_stmt *pStmt = 0;
122710
122711  assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
122712  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
122713  assert( iIndex>=0 && iIndex<p->nIndex );
122714
122715  if( iLevel<0 ){
122716    /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
122717    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
122718    if( rc==SQLITE_OK ){
122719      sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
122720      sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL-1);
122721    }
122722  }else{
122723    /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
122724    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
122725    if( rc==SQLITE_OK ){
122726      sqlite3_bind_int(pStmt, 1, iLevel+iIndex*FTS3_SEGDIR_MAXLEVEL);
122727    }
122728  }
122729  *ppStmt = pStmt;
122730  return rc;
122731}
122732
122733
122734/*
122735** Append a single varint to a PendingList buffer. SQLITE_OK is returned
122736** if successful, or an SQLite error code otherwise.
122737**
122738** This function also serves to allocate the PendingList structure itself.
122739** For example, to create a new PendingList structure containing two
122740** varints:
122741**
122742**   PendingList *p = 0;
122743**   fts3PendingListAppendVarint(&p, 1);
122744**   fts3PendingListAppendVarint(&p, 2);
122745*/
122746static int fts3PendingListAppendVarint(
122747  PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
122748  sqlite3_int64 i                 /* Value to append to data */
122749){
122750  PendingList *p = *pp;
122751
122752  /* Allocate or grow the PendingList as required. */
122753  if( !p ){
122754    p = sqlite3_malloc(sizeof(*p) + 100);
122755    if( !p ){
122756      return SQLITE_NOMEM;
122757    }
122758    p->nSpace = 100;
122759    p->aData = (char *)&p[1];
122760    p->nData = 0;
122761  }
122762  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
122763    int nNew = p->nSpace * 2;
122764    p = sqlite3_realloc(p, sizeof(*p) + nNew);
122765    if( !p ){
122766      sqlite3_free(*pp);
122767      *pp = 0;
122768      return SQLITE_NOMEM;
122769    }
122770    p->nSpace = nNew;
122771    p->aData = (char *)&p[1];
122772  }
122773
122774  /* Append the new serialized varint to the end of the list. */
122775  p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
122776  p->aData[p->nData] = '\0';
122777  *pp = p;
122778  return SQLITE_OK;
122779}
122780
122781/*
122782** Add a docid/column/position entry to a PendingList structure. Non-zero
122783** is returned if the structure is sqlite3_realloced as part of adding
122784** the entry. Otherwise, zero.
122785**
122786** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
122787** Zero is always returned in this case. Otherwise, if no OOM error occurs,
122788** it is set to SQLITE_OK.
122789*/
122790static int fts3PendingListAppend(
122791  PendingList **pp,               /* IN/OUT: PendingList structure */
122792  sqlite3_int64 iDocid,           /* Docid for entry to add */
122793  sqlite3_int64 iCol,             /* Column for entry to add */
122794  sqlite3_int64 iPos,             /* Position of term for entry to add */
122795  int *pRc                        /* OUT: Return code */
122796){
122797  PendingList *p = *pp;
122798  int rc = SQLITE_OK;
122799
122800  assert( !p || p->iLastDocid<=iDocid );
122801
122802  if( !p || p->iLastDocid!=iDocid ){
122803    sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
122804    if( p ){
122805      assert( p->nData<p->nSpace );
122806      assert( p->aData[p->nData]==0 );
122807      p->nData++;
122808    }
122809    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
122810      goto pendinglistappend_out;
122811    }
122812    p->iLastCol = -1;
122813    p->iLastPos = 0;
122814    p->iLastDocid = iDocid;
122815  }
122816  if( iCol>0 && p->iLastCol!=iCol ){
122817    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
122818     || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
122819    ){
122820      goto pendinglistappend_out;
122821    }
122822    p->iLastCol = iCol;
122823    p->iLastPos = 0;
122824  }
122825  if( iCol>=0 ){
122826    assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
122827    rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
122828    if( rc==SQLITE_OK ){
122829      p->iLastPos = iPos;
122830    }
122831  }
122832
122833 pendinglistappend_out:
122834  *pRc = rc;
122835  if( p!=*pp ){
122836    *pp = p;
122837    return 1;
122838  }
122839  return 0;
122840}
122841
122842/*
122843** Free a PendingList object allocated by fts3PendingListAppend().
122844*/
122845static void fts3PendingListDelete(PendingList *pList){
122846  sqlite3_free(pList);
122847}
122848
122849/*
122850** Add an entry to one of the pending-terms hash tables.
122851*/
122852static int fts3PendingTermsAddOne(
122853  Fts3Table *p,
122854  int iCol,
122855  int iPos,
122856  Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
122857  const char *zToken,
122858  int nToken
122859){
122860  PendingList *pList;
122861  int rc = SQLITE_OK;
122862
122863  pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
122864  if( pList ){
122865    p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
122866  }
122867  if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
122868    if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
122869      /* Malloc failed while inserting the new entry. This can only
122870      ** happen if there was no previous entry for this token.
122871      */
122872      assert( 0==fts3HashFind(pHash, zToken, nToken) );
122873      sqlite3_free(pList);
122874      rc = SQLITE_NOMEM;
122875    }
122876  }
122877  if( rc==SQLITE_OK ){
122878    p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
122879  }
122880  return rc;
122881}
122882
122883/*
122884** Tokenize the nul-terminated string zText and add all tokens to the
122885** pending-terms hash-table. The docid used is that currently stored in
122886** p->iPrevDocid, and the column is specified by argument iCol.
122887**
122888** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
122889*/
122890static int fts3PendingTermsAdd(
122891  Fts3Table *p,                   /* Table into which text will be inserted */
122892  const char *zText,              /* Text of document to be inserted */
122893  int iCol,                       /* Column into which text is being inserted */
122894  u32 *pnWord                     /* OUT: Number of tokens inserted */
122895){
122896  int rc;
122897  int iStart;
122898  int iEnd;
122899  int iPos;
122900  int nWord = 0;
122901
122902  char const *zToken;
122903  int nToken;
122904
122905  sqlite3_tokenizer *pTokenizer = p->pTokenizer;
122906  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
122907  sqlite3_tokenizer_cursor *pCsr;
122908  int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
122909      const char**,int*,int*,int*,int*);
122910
122911  assert( pTokenizer && pModule );
122912
122913  /* If the user has inserted a NULL value, this function may be called with
122914  ** zText==0. In this case, add zero token entries to the hash table and
122915  ** return early. */
122916  if( zText==0 ){
122917    *pnWord = 0;
122918    return SQLITE_OK;
122919  }
122920
122921  rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr);
122922  if( rc!=SQLITE_OK ){
122923    return rc;
122924  }
122925  pCsr->pTokenizer = pTokenizer;
122926
122927  xNext = pModule->xNext;
122928  while( SQLITE_OK==rc
122929      && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
122930  ){
122931    int i;
122932    if( iPos>=nWord ) nWord = iPos+1;
122933
122934    /* Positions cannot be negative; we use -1 as a terminator internally.
122935    ** Tokens must have a non-zero length.
122936    */
122937    if( iPos<0 || !zToken || nToken<=0 ){
122938      rc = SQLITE_ERROR;
122939      break;
122940    }
122941
122942    /* Add the term to the terms index */
122943    rc = fts3PendingTermsAddOne(
122944        p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
122945    );
122946
122947    /* Add the term to each of the prefix indexes that it is not too
122948    ** short for. */
122949    for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
122950      struct Fts3Index *pIndex = &p->aIndex[i];
122951      if( nToken<pIndex->nPrefix ) continue;
122952      rc = fts3PendingTermsAddOne(
122953          p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
122954      );
122955    }
122956  }
122957
122958  pModule->xClose(pCsr);
122959  *pnWord = nWord;
122960  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
122961}
122962
122963/*
122964** Calling this function indicates that subsequent calls to
122965** fts3PendingTermsAdd() are to add term/position-list pairs for the
122966** contents of the document with docid iDocid.
122967*/
122968static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){
122969  /* TODO(shess) Explore whether partially flushing the buffer on
122970  ** forced-flush would provide better performance.  I suspect that if
122971  ** we ordered the doclists by size and flushed the largest until the
122972  ** buffer was half empty, that would let the less frequent terms
122973  ** generate longer doclists.
122974  */
122975  if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){
122976    int rc = sqlite3Fts3PendingTermsFlush(p);
122977    if( rc!=SQLITE_OK ) return rc;
122978  }
122979  p->iPrevDocid = iDocid;
122980  return SQLITE_OK;
122981}
122982
122983/*
122984** Discard the contents of the pending-terms hash tables.
122985*/
122986SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
122987  int i;
122988  for(i=0; i<p->nIndex; i++){
122989    Fts3HashElem *pElem;
122990    Fts3Hash *pHash = &p->aIndex[i].hPending;
122991    for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
122992      PendingList *pList = (PendingList *)fts3HashData(pElem);
122993      fts3PendingListDelete(pList);
122994    }
122995    fts3HashClear(pHash);
122996  }
122997  p->nPendingData = 0;
122998}
122999
123000/*
123001** This function is called by the xUpdate() method as part of an INSERT
123002** operation. It adds entries for each term in the new record to the
123003** pendingTerms hash table.
123004**
123005** Argument apVal is the same as the similarly named argument passed to
123006** fts3InsertData(). Parameter iDocid is the docid of the new row.
123007*/
123008static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){
123009  int i;                          /* Iterator variable */
123010  for(i=2; i<p->nColumn+2; i++){
123011    const char *zText = (const char *)sqlite3_value_text(apVal[i]);
123012    int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]);
123013    if( rc!=SQLITE_OK ){
123014      return rc;
123015    }
123016    aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
123017  }
123018  return SQLITE_OK;
123019}
123020
123021/*
123022** This function is called by the xUpdate() method for an INSERT operation.
123023** The apVal parameter is passed a copy of the apVal argument passed by
123024** SQLite to the xUpdate() method. i.e:
123025**
123026**   apVal[0]                Not used for INSERT.
123027**   apVal[1]                rowid
123028**   apVal[2]                Left-most user-defined column
123029**   ...
123030**   apVal[p->nColumn+1]     Right-most user-defined column
123031**   apVal[p->nColumn+2]     Hidden column with same name as table
123032**   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
123033*/
123034static int fts3InsertData(
123035  Fts3Table *p,                   /* Full-text table */
123036  sqlite3_value **apVal,          /* Array of values to insert */
123037  sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
123038){
123039  int rc;                         /* Return code */
123040  sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
123041
123042  /* Locate the statement handle used to insert data into the %_content
123043  ** table. The SQL for this statement is:
123044  **
123045  **   INSERT INTO %_content VALUES(?, ?, ?, ...)
123046  **
123047  ** The statement features N '?' variables, where N is the number of user
123048  ** defined columns in the FTS3 table, plus one for the docid field.
123049  */
123050  rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
123051  if( rc!=SQLITE_OK ){
123052    return rc;
123053  }
123054
123055  /* There is a quirk here. The users INSERT statement may have specified
123056  ** a value for the "rowid" field, for the "docid" field, or for both.
123057  ** Which is a problem, since "rowid" and "docid" are aliases for the
123058  ** same value. For example:
123059  **
123060  **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
123061  **
123062  ** In FTS3, this is an error. It is an error to specify non-NULL values
123063  ** for both docid and some other rowid alias.
123064  */
123065  if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
123066    if( SQLITE_NULL==sqlite3_value_type(apVal[0])
123067     && SQLITE_NULL!=sqlite3_value_type(apVal[1])
123068    ){
123069      /* A rowid/docid conflict. */
123070      return SQLITE_ERROR;
123071    }
123072    rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
123073    if( rc!=SQLITE_OK ) return rc;
123074  }
123075
123076  /* Execute the statement to insert the record. Set *piDocid to the
123077  ** new docid value.
123078  */
123079  sqlite3_step(pContentInsert);
123080  rc = sqlite3_reset(pContentInsert);
123081
123082  *piDocid = sqlite3_last_insert_rowid(p->db);
123083  return rc;
123084}
123085
123086
123087
123088/*
123089** Remove all data from the FTS3 table. Clear the hash table containing
123090** pending terms.
123091*/
123092static int fts3DeleteAll(Fts3Table *p){
123093  int rc = SQLITE_OK;             /* Return code */
123094
123095  /* Discard the contents of the pending-terms hash table. */
123096  sqlite3Fts3PendingTermsClear(p);
123097
123098  /* Delete everything from the %_content, %_segments and %_segdir tables. */
123099  fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
123100  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
123101  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
123102  if( p->bHasDocsize ){
123103    fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
123104  }
123105  if( p->bHasStat ){
123106    fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
123107  }
123108  return rc;
123109}
123110
123111/*
123112** The first element in the apVal[] array is assumed to contain the docid
123113** (an integer) of a row about to be deleted. Remove all terms from the
123114** full-text index.
123115*/
123116static void fts3DeleteTerms(
123117  int *pRC,               /* Result code */
123118  Fts3Table *p,           /* The FTS table to delete from */
123119  sqlite3_value *pRowid,  /* The docid to be deleted */
123120  u32 *aSz                /* Sizes of deleted document written here */
123121){
123122  int rc;
123123  sqlite3_stmt *pSelect;
123124
123125  if( *pRC ) return;
123126  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
123127  if( rc==SQLITE_OK ){
123128    if( SQLITE_ROW==sqlite3_step(pSelect) ){
123129      int i;
123130      for(i=1; i<=p->nColumn; i++){
123131        const char *zText = (const char *)sqlite3_column_text(pSelect, i);
123132        rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]);
123133        if( rc!=SQLITE_OK ){
123134          sqlite3_reset(pSelect);
123135          *pRC = rc;
123136          return;
123137        }
123138        aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
123139      }
123140    }
123141    rc = sqlite3_reset(pSelect);
123142  }else{
123143    sqlite3_reset(pSelect);
123144  }
123145  *pRC = rc;
123146}
123147
123148/*
123149** Forward declaration to account for the circular dependency between
123150** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
123151*/
123152static int fts3SegmentMerge(Fts3Table *, int, int);
123153
123154/*
123155** This function allocates a new level iLevel index in the segdir table.
123156** Usually, indexes are allocated within a level sequentially starting
123157** with 0, so the allocated index is one greater than the value returned
123158** by:
123159**
123160**   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
123161**
123162** However, if there are already FTS3_MERGE_COUNT indexes at the requested
123163** level, they are merged into a single level (iLevel+1) segment and the
123164** allocated index is 0.
123165**
123166** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
123167** returned. Otherwise, an SQLite error code is returned.
123168*/
123169static int fts3AllocateSegdirIdx(
123170  Fts3Table *p,
123171  int iIndex,                     /* Index for p->aIndex */
123172  int iLevel,
123173  int *piIdx
123174){
123175  int rc;                         /* Return Code */
123176  sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
123177  int iNext = 0;                  /* Result of query pNextIdx */
123178
123179  /* Set variable iNext to the next available segdir index at level iLevel. */
123180  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
123181  if( rc==SQLITE_OK ){
123182    sqlite3_bind_int(pNextIdx, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
123183    if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
123184      iNext = sqlite3_column_int(pNextIdx, 0);
123185    }
123186    rc = sqlite3_reset(pNextIdx);
123187  }
123188
123189  if( rc==SQLITE_OK ){
123190    /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
123191    ** full, merge all segments in level iLevel into a single iLevel+1
123192    ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
123193    ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
123194    */
123195    if( iNext>=FTS3_MERGE_COUNT ){
123196      rc = fts3SegmentMerge(p, iIndex, iLevel);
123197      *piIdx = 0;
123198    }else{
123199      *piIdx = iNext;
123200    }
123201  }
123202
123203  return rc;
123204}
123205
123206/*
123207** The %_segments table is declared as follows:
123208**
123209**   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
123210**
123211** This function reads data from a single row of the %_segments table. The
123212** specific row is identified by the iBlockid parameter. If paBlob is not
123213** NULL, then a buffer is allocated using sqlite3_malloc() and populated
123214** with the contents of the blob stored in the "block" column of the
123215** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
123216** to the size of the blob in bytes before returning.
123217**
123218** If an error occurs, or the table does not contain the specified row,
123219** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
123220** paBlob is non-NULL, then it is the responsibility of the caller to
123221** eventually free the returned buffer.
123222**
123223** This function may leave an open sqlite3_blob* handle in the
123224** Fts3Table.pSegments variable. This handle is reused by subsequent calls
123225** to this function. The handle may be closed by calling the
123226** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
123227** performance improvement, but the blob handle should always be closed
123228** before control is returned to the user (to prevent a lock being held
123229** on the database file for longer than necessary). Thus, any virtual table
123230** method (xFilter etc.) that may directly or indirectly call this function
123231** must call sqlite3Fts3SegmentsClose() before returning.
123232*/
123233SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
123234  Fts3Table *p,                   /* FTS3 table handle */
123235  sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
123236  char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
123237  int *pnBlob,                    /* OUT: Size of blob data */
123238  int *pnLoad                     /* OUT: Bytes actually loaded */
123239){
123240  int rc;                         /* Return code */
123241
123242  /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
123243  assert( pnBlob);
123244
123245  if( p->pSegments ){
123246    rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
123247  }else{
123248    if( 0==p->zSegmentsTbl ){
123249      p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
123250      if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
123251    }
123252    rc = sqlite3_blob_open(
123253       p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
123254    );
123255  }
123256
123257  if( rc==SQLITE_OK ){
123258    int nByte = sqlite3_blob_bytes(p->pSegments);
123259    *pnBlob = nByte;
123260    if( paBlob ){
123261      char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
123262      if( !aByte ){
123263        rc = SQLITE_NOMEM;
123264      }else{
123265        if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
123266          nByte = FTS3_NODE_CHUNKSIZE;
123267          *pnLoad = nByte;
123268        }
123269        rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
123270        memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
123271        if( rc!=SQLITE_OK ){
123272          sqlite3_free(aByte);
123273          aByte = 0;
123274        }
123275      }
123276      *paBlob = aByte;
123277    }
123278  }
123279
123280  return rc;
123281}
123282
123283/*
123284** Close the blob handle at p->pSegments, if it is open. See comments above
123285** the sqlite3Fts3ReadBlock() function for details.
123286*/
123287SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
123288  sqlite3_blob_close(p->pSegments);
123289  p->pSegments = 0;
123290}
123291
123292static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
123293  int nRead;                      /* Number of bytes to read */
123294  int rc;                         /* Return code */
123295
123296  nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
123297  rc = sqlite3_blob_read(
123298      pReader->pBlob,
123299      &pReader->aNode[pReader->nPopulate],
123300      nRead,
123301      pReader->nPopulate
123302  );
123303
123304  if( rc==SQLITE_OK ){
123305    pReader->nPopulate += nRead;
123306    memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
123307    if( pReader->nPopulate==pReader->nNode ){
123308      sqlite3_blob_close(pReader->pBlob);
123309      pReader->pBlob = 0;
123310      pReader->nPopulate = 0;
123311    }
123312  }
123313  return rc;
123314}
123315
123316static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
123317  int rc = SQLITE_OK;
123318  assert( !pReader->pBlob
123319       || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
123320  );
123321  while( pReader->pBlob && rc==SQLITE_OK
123322     &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
123323  ){
123324    rc = fts3SegReaderIncrRead(pReader);
123325  }
123326  return rc;
123327}
123328
123329/*
123330** Move the iterator passed as the first argument to the next term in the
123331** segment. If successful, SQLITE_OK is returned. If there is no next term,
123332** SQLITE_DONE. Otherwise, an SQLite error code.
123333*/
123334static int fts3SegReaderNext(
123335  Fts3Table *p,
123336  Fts3SegReader *pReader,
123337  int bIncr
123338){
123339  int rc;                         /* Return code of various sub-routines */
123340  char *pNext;                    /* Cursor variable */
123341  int nPrefix;                    /* Number of bytes in term prefix */
123342  int nSuffix;                    /* Number of bytes in term suffix */
123343
123344  if( !pReader->aDoclist ){
123345    pNext = pReader->aNode;
123346  }else{
123347    pNext = &pReader->aDoclist[pReader->nDoclist];
123348  }
123349
123350  if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
123351
123352    if( fts3SegReaderIsPending(pReader) ){
123353      Fts3HashElem *pElem = *(pReader->ppNextElem);
123354      if( pElem==0 ){
123355        pReader->aNode = 0;
123356      }else{
123357        PendingList *pList = (PendingList *)fts3HashData(pElem);
123358        pReader->zTerm = (char *)fts3HashKey(pElem);
123359        pReader->nTerm = fts3HashKeysize(pElem);
123360        pReader->nNode = pReader->nDoclist = pList->nData + 1;
123361        pReader->aNode = pReader->aDoclist = pList->aData;
123362        pReader->ppNextElem++;
123363        assert( pReader->aNode );
123364      }
123365      return SQLITE_OK;
123366    }
123367
123368    if( !fts3SegReaderIsRootOnly(pReader) ){
123369      sqlite3_free(pReader->aNode);
123370      sqlite3_blob_close(pReader->pBlob);
123371      pReader->pBlob = 0;
123372    }
123373    pReader->aNode = 0;
123374
123375    /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
123376    ** blocks have already been traversed.  */
123377    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
123378    if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
123379      return SQLITE_OK;
123380    }
123381
123382    rc = sqlite3Fts3ReadBlock(
123383        p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
123384        (bIncr ? &pReader->nPopulate : 0)
123385    );
123386    if( rc!=SQLITE_OK ) return rc;
123387    assert( pReader->pBlob==0 );
123388    if( bIncr && pReader->nPopulate<pReader->nNode ){
123389      pReader->pBlob = p->pSegments;
123390      p->pSegments = 0;
123391    }
123392    pNext = pReader->aNode;
123393  }
123394
123395  assert( !fts3SegReaderIsPending(pReader) );
123396
123397  rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
123398  if( rc!=SQLITE_OK ) return rc;
123399
123400  /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
123401  ** safe (no risk of overread) even if the node data is corrupted. */
123402  pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
123403  pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
123404  if( nPrefix<0 || nSuffix<=0
123405   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
123406  ){
123407    return SQLITE_CORRUPT_VTAB;
123408  }
123409
123410  if( nPrefix+nSuffix>pReader->nTermAlloc ){
123411    int nNew = (nPrefix+nSuffix)*2;
123412    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
123413    if( !zNew ){
123414      return SQLITE_NOMEM;
123415    }
123416    pReader->zTerm = zNew;
123417    pReader->nTermAlloc = nNew;
123418  }
123419
123420  rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
123421  if( rc!=SQLITE_OK ) return rc;
123422
123423  memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
123424  pReader->nTerm = nPrefix+nSuffix;
123425  pNext += nSuffix;
123426  pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
123427  pReader->aDoclist = pNext;
123428  pReader->pOffsetList = 0;
123429
123430  /* Check that the doclist does not appear to extend past the end of the
123431  ** b-tree node. And that the final byte of the doclist is 0x00. If either
123432  ** of these statements is untrue, then the data structure is corrupt.
123433  */
123434  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
123435   || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
123436  ){
123437    return SQLITE_CORRUPT_VTAB;
123438  }
123439  return SQLITE_OK;
123440}
123441
123442/*
123443** Set the SegReader to point to the first docid in the doclist associated
123444** with the current term.
123445*/
123446static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
123447  int rc = SQLITE_OK;
123448  assert( pReader->aDoclist );
123449  assert( !pReader->pOffsetList );
123450  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
123451    u8 bEof = 0;
123452    pReader->iDocid = 0;
123453    pReader->nOffsetList = 0;
123454    sqlite3Fts3DoclistPrev(0,
123455        pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
123456        &pReader->iDocid, &pReader->nOffsetList, &bEof
123457    );
123458  }else{
123459    rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
123460    if( rc==SQLITE_OK ){
123461      int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
123462      pReader->pOffsetList = &pReader->aDoclist[n];
123463    }
123464  }
123465  return rc;
123466}
123467
123468/*
123469** Advance the SegReader to point to the next docid in the doclist
123470** associated with the current term.
123471**
123472** If arguments ppOffsetList and pnOffsetList are not NULL, then
123473** *ppOffsetList is set to point to the first column-offset list
123474** in the doclist entry (i.e. immediately past the docid varint).
123475** *pnOffsetList is set to the length of the set of column-offset
123476** lists, not including the nul-terminator byte. For example:
123477*/
123478static int fts3SegReaderNextDocid(
123479  Fts3Table *pTab,
123480  Fts3SegReader *pReader,         /* Reader to advance to next docid */
123481  char **ppOffsetList,            /* OUT: Pointer to current position-list */
123482  int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
123483){
123484  int rc = SQLITE_OK;
123485  char *p = pReader->pOffsetList;
123486  char c = 0;
123487
123488  assert( p );
123489
123490  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
123491    /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
123492    ** Pending-terms doclists are always built up in ascending order, so
123493    ** we have to iterate through them backwards here. */
123494    u8 bEof = 0;
123495    if( ppOffsetList ){
123496      *ppOffsetList = pReader->pOffsetList;
123497      *pnOffsetList = pReader->nOffsetList - 1;
123498    }
123499    sqlite3Fts3DoclistPrev(0,
123500        pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
123501        &pReader->nOffsetList, &bEof
123502    );
123503    if( bEof ){
123504      pReader->pOffsetList = 0;
123505    }else{
123506      pReader->pOffsetList = p;
123507    }
123508  }else{
123509    char *pEnd = &pReader->aDoclist[pReader->nDoclist];
123510
123511    /* Pointer p currently points at the first byte of an offset list. The
123512    ** following block advances it to point one byte past the end of
123513    ** the same offset list. */
123514    while( 1 ){
123515
123516      /* The following line of code (and the "p++" below the while() loop) is
123517      ** normally all that is required to move pointer p to the desired
123518      ** position. The exception is if this node is being loaded from disk
123519      ** incrementally and pointer "p" now points to the first byte passed
123520      ** the populated part of pReader->aNode[].
123521      */
123522      while( *p | c ) c = *p++ & 0x80;
123523      assert( *p==0 );
123524
123525      if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
123526      rc = fts3SegReaderIncrRead(pReader);
123527      if( rc!=SQLITE_OK ) return rc;
123528    }
123529    p++;
123530
123531    /* If required, populate the output variables with a pointer to and the
123532    ** size of the previous offset-list.
123533    */
123534    if( ppOffsetList ){
123535      *ppOffsetList = pReader->pOffsetList;
123536      *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
123537    }
123538
123539    while( p<pEnd && *p==0 ) p++;
123540
123541    /* If there are no more entries in the doclist, set pOffsetList to
123542    ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
123543    ** Fts3SegReader.pOffsetList to point to the next offset list before
123544    ** returning.
123545    */
123546    if( p>=pEnd ){
123547      pReader->pOffsetList = 0;
123548    }else{
123549      rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
123550      if( rc==SQLITE_OK ){
123551        sqlite3_int64 iDelta;
123552        pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
123553        if( pTab->bDescIdx ){
123554          pReader->iDocid -= iDelta;
123555        }else{
123556          pReader->iDocid += iDelta;
123557        }
123558      }
123559    }
123560  }
123561
123562  return SQLITE_OK;
123563}
123564
123565
123566SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
123567  Fts3Cursor *pCsr,
123568  Fts3MultiSegReader *pMsr,
123569  int *pnOvfl
123570){
123571  Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
123572  int nOvfl = 0;
123573  int ii;
123574  int rc = SQLITE_OK;
123575  int pgsz = p->nPgsz;
123576
123577  assert( p->bHasStat );
123578  assert( pgsz>0 );
123579
123580  for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
123581    Fts3SegReader *pReader = pMsr->apSegment[ii];
123582    if( !fts3SegReaderIsPending(pReader)
123583     && !fts3SegReaderIsRootOnly(pReader)
123584    ){
123585      sqlite3_int64 jj;
123586      for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
123587        int nBlob;
123588        rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
123589        if( rc!=SQLITE_OK ) break;
123590        if( (nBlob+35)>pgsz ){
123591          nOvfl += (nBlob + 34)/pgsz;
123592        }
123593      }
123594    }
123595  }
123596  *pnOvfl = nOvfl;
123597  return rc;
123598}
123599
123600/*
123601** Free all allocations associated with the iterator passed as the
123602** second argument.
123603*/
123604SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
123605  if( pReader && !fts3SegReaderIsPending(pReader) ){
123606    sqlite3_free(pReader->zTerm);
123607    if( !fts3SegReaderIsRootOnly(pReader) ){
123608      sqlite3_free(pReader->aNode);
123609      sqlite3_blob_close(pReader->pBlob);
123610    }
123611  }
123612  sqlite3_free(pReader);
123613}
123614
123615/*
123616** Allocate a new SegReader object.
123617*/
123618SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
123619  int iAge,                       /* Segment "age". */
123620  sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
123621  sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
123622  sqlite3_int64 iEndBlock,        /* Final block of segment */
123623  const char *zRoot,              /* Buffer containing root node */
123624  int nRoot,                      /* Size of buffer containing root node */
123625  Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
123626){
123627  int rc = SQLITE_OK;             /* Return code */
123628  Fts3SegReader *pReader;         /* Newly allocated SegReader object */
123629  int nExtra = 0;                 /* Bytes to allocate segment root node */
123630
123631  assert( iStartLeaf<=iEndLeaf );
123632  if( iStartLeaf==0 ){
123633    nExtra = nRoot + FTS3_NODE_PADDING;
123634  }
123635
123636  pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
123637  if( !pReader ){
123638    return SQLITE_NOMEM;
123639  }
123640  memset(pReader, 0, sizeof(Fts3SegReader));
123641  pReader->iIdx = iAge;
123642  pReader->iStartBlock = iStartLeaf;
123643  pReader->iLeafEndBlock = iEndLeaf;
123644  pReader->iEndBlock = iEndBlock;
123645
123646  if( nExtra ){
123647    /* The entire segment is stored in the root node. */
123648    pReader->aNode = (char *)&pReader[1];
123649    pReader->nNode = nRoot;
123650    memcpy(pReader->aNode, zRoot, nRoot);
123651    memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
123652  }else{
123653    pReader->iCurrentBlock = iStartLeaf-1;
123654  }
123655
123656  if( rc==SQLITE_OK ){
123657    *ppReader = pReader;
123658  }else{
123659    sqlite3Fts3SegReaderFree(pReader);
123660  }
123661  return rc;
123662}
123663
123664/*
123665** This is a comparison function used as a qsort() callback when sorting
123666** an array of pending terms by term. This occurs as part of flushing
123667** the contents of the pending-terms hash table to the database.
123668*/
123669static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
123670  char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
123671  char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
123672  int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
123673  int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
123674
123675  int n = (n1<n2 ? n1 : n2);
123676  int c = memcmp(z1, z2, n);
123677  if( c==0 ){
123678    c = n1 - n2;
123679  }
123680  return c;
123681}
123682
123683/*
123684** This function is used to allocate an Fts3SegReader that iterates through
123685** a subset of the terms stored in the Fts3Table.pendingTerms array.
123686**
123687** If the isPrefixIter parameter is zero, then the returned SegReader iterates
123688** through each term in the pending-terms table. Or, if isPrefixIter is
123689** non-zero, it iterates through each term and its prefixes. For example, if
123690** the pending terms hash table contains the terms "sqlite", "mysql" and
123691** "firebird", then the iterator visits the following 'terms' (in the order
123692** shown):
123693**
123694**   f fi fir fire fireb firebi firebir firebird
123695**   m my mys mysq mysql
123696**   s sq sql sqli sqlit sqlite
123697**
123698** Whereas if isPrefixIter is zero, the terms visited are:
123699**
123700**   firebird mysql sqlite
123701*/
123702SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
123703  Fts3Table *p,                   /* Virtual table handle */
123704  int iIndex,                     /* Index for p->aIndex */
123705  const char *zTerm,              /* Term to search for */
123706  int nTerm,                      /* Size of buffer zTerm */
123707  int bPrefix,                    /* True for a prefix iterator */
123708  Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
123709){
123710  Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
123711  Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
123712  int nElem = 0;                  /* Size of array at aElem */
123713  int rc = SQLITE_OK;             /* Return Code */
123714  Fts3Hash *pHash;
123715
123716  pHash = &p->aIndex[iIndex].hPending;
123717  if( bPrefix ){
123718    int nAlloc = 0;               /* Size of allocated array at aElem */
123719    Fts3HashElem *pE = 0;         /* Iterator variable */
123720
123721    for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
123722      char *zKey = (char *)fts3HashKey(pE);
123723      int nKey = fts3HashKeysize(pE);
123724      if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
123725        if( nElem==nAlloc ){
123726          Fts3HashElem **aElem2;
123727          nAlloc += 16;
123728          aElem2 = (Fts3HashElem **)sqlite3_realloc(
123729              aElem, nAlloc*sizeof(Fts3HashElem *)
123730          );
123731          if( !aElem2 ){
123732            rc = SQLITE_NOMEM;
123733            nElem = 0;
123734            break;
123735          }
123736          aElem = aElem2;
123737        }
123738
123739        aElem[nElem++] = pE;
123740      }
123741    }
123742
123743    /* If more than one term matches the prefix, sort the Fts3HashElem
123744    ** objects in term order using qsort(). This uses the same comparison
123745    ** callback as is used when flushing terms to disk.
123746    */
123747    if( nElem>1 ){
123748      qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
123749    }
123750
123751  }else{
123752    /* The query is a simple term lookup that matches at most one term in
123753    ** the index. All that is required is a straight hash-lookup. */
123754    Fts3HashElem *pE = fts3HashFindElem(pHash, zTerm, nTerm);
123755    if( pE ){
123756      aElem = &pE;
123757      nElem = 1;
123758    }
123759  }
123760
123761  if( nElem>0 ){
123762    int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
123763    pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
123764    if( !pReader ){
123765      rc = SQLITE_NOMEM;
123766    }else{
123767      memset(pReader, 0, nByte);
123768      pReader->iIdx = 0x7FFFFFFF;
123769      pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
123770      memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
123771    }
123772  }
123773
123774  if( bPrefix ){
123775    sqlite3_free(aElem);
123776  }
123777  *ppReader = pReader;
123778  return rc;
123779}
123780
123781/*
123782** Compare the entries pointed to by two Fts3SegReader structures.
123783** Comparison is as follows:
123784**
123785**   1) EOF is greater than not EOF.
123786**
123787**   2) The current terms (if any) are compared using memcmp(). If one
123788**      term is a prefix of another, the longer term is considered the
123789**      larger.
123790**
123791**   3) By segment age. An older segment is considered larger.
123792*/
123793static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
123794  int rc;
123795  if( pLhs->aNode && pRhs->aNode ){
123796    int rc2 = pLhs->nTerm - pRhs->nTerm;
123797    if( rc2<0 ){
123798      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
123799    }else{
123800      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
123801    }
123802    if( rc==0 ){
123803      rc = rc2;
123804    }
123805  }else{
123806    rc = (pLhs->aNode==0) - (pRhs->aNode==0);
123807  }
123808  if( rc==0 ){
123809    rc = pRhs->iIdx - pLhs->iIdx;
123810  }
123811  assert( rc!=0 );
123812  return rc;
123813}
123814
123815/*
123816** A different comparison function for SegReader structures. In this
123817** version, it is assumed that each SegReader points to an entry in
123818** a doclist for identical terms. Comparison is made as follows:
123819**
123820**   1) EOF (end of doclist in this case) is greater than not EOF.
123821**
123822**   2) By current docid.
123823**
123824**   3) By segment age. An older segment is considered larger.
123825*/
123826static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
123827  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
123828  if( rc==0 ){
123829    if( pLhs->iDocid==pRhs->iDocid ){
123830      rc = pRhs->iIdx - pLhs->iIdx;
123831    }else{
123832      rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
123833    }
123834  }
123835  assert( pLhs->aNode && pRhs->aNode );
123836  return rc;
123837}
123838static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
123839  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
123840  if( rc==0 ){
123841    if( pLhs->iDocid==pRhs->iDocid ){
123842      rc = pRhs->iIdx - pLhs->iIdx;
123843    }else{
123844      rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
123845    }
123846  }
123847  assert( pLhs->aNode && pRhs->aNode );
123848  return rc;
123849}
123850
123851/*
123852** Compare the term that the Fts3SegReader object passed as the first argument
123853** points to with the term specified by arguments zTerm and nTerm.
123854**
123855** If the pSeg iterator is already at EOF, return 0. Otherwise, return
123856** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
123857** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
123858*/
123859static int fts3SegReaderTermCmp(
123860  Fts3SegReader *pSeg,            /* Segment reader object */
123861  const char *zTerm,              /* Term to compare to */
123862  int nTerm                       /* Size of term zTerm in bytes */
123863){
123864  int res = 0;
123865  if( pSeg->aNode ){
123866    if( pSeg->nTerm>nTerm ){
123867      res = memcmp(pSeg->zTerm, zTerm, nTerm);
123868    }else{
123869      res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
123870    }
123871    if( res==0 ){
123872      res = pSeg->nTerm-nTerm;
123873    }
123874  }
123875  return res;
123876}
123877
123878/*
123879** Argument apSegment is an array of nSegment elements. It is known that
123880** the final (nSegment-nSuspect) members are already in sorted order
123881** (according to the comparison function provided). This function shuffles
123882** the array around until all entries are in sorted order.
123883*/
123884static void fts3SegReaderSort(
123885  Fts3SegReader **apSegment,                     /* Array to sort entries of */
123886  int nSegment,                                  /* Size of apSegment array */
123887  int nSuspect,                                  /* Unsorted entry count */
123888  int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
123889){
123890  int i;                          /* Iterator variable */
123891
123892  assert( nSuspect<=nSegment );
123893
123894  if( nSuspect==nSegment ) nSuspect--;
123895  for(i=nSuspect-1; i>=0; i--){
123896    int j;
123897    for(j=i; j<(nSegment-1); j++){
123898      Fts3SegReader *pTmp;
123899      if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
123900      pTmp = apSegment[j+1];
123901      apSegment[j+1] = apSegment[j];
123902      apSegment[j] = pTmp;
123903    }
123904  }
123905
123906#ifndef NDEBUG
123907  /* Check that the list really is sorted now. */
123908  for(i=0; i<(nSuspect-1); i++){
123909    assert( xCmp(apSegment[i], apSegment[i+1])<0 );
123910  }
123911#endif
123912}
123913
123914/*
123915** Insert a record into the %_segments table.
123916*/
123917static int fts3WriteSegment(
123918  Fts3Table *p,                   /* Virtual table handle */
123919  sqlite3_int64 iBlock,           /* Block id for new block */
123920  char *z,                        /* Pointer to buffer containing block data */
123921  int n                           /* Size of buffer z in bytes */
123922){
123923  sqlite3_stmt *pStmt;
123924  int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
123925  if( rc==SQLITE_OK ){
123926    sqlite3_bind_int64(pStmt, 1, iBlock);
123927    sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
123928    sqlite3_step(pStmt);
123929    rc = sqlite3_reset(pStmt);
123930  }
123931  return rc;
123932}
123933
123934/*
123935** Insert a record into the %_segdir table.
123936*/
123937static int fts3WriteSegdir(
123938  Fts3Table *p,                   /* Virtual table handle */
123939  int iLevel,                     /* Value for "level" field */
123940  int iIdx,                       /* Value for "idx" field */
123941  sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
123942  sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
123943  sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
123944  char *zRoot,                    /* Blob value for "root" field */
123945  int nRoot                       /* Number of bytes in buffer zRoot */
123946){
123947  sqlite3_stmt *pStmt;
123948  int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
123949  if( rc==SQLITE_OK ){
123950    sqlite3_bind_int(pStmt, 1, iLevel);
123951    sqlite3_bind_int(pStmt, 2, iIdx);
123952    sqlite3_bind_int64(pStmt, 3, iStartBlock);
123953    sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
123954    sqlite3_bind_int64(pStmt, 5, iEndBlock);
123955    sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
123956    sqlite3_step(pStmt);
123957    rc = sqlite3_reset(pStmt);
123958  }
123959  return rc;
123960}
123961
123962/*
123963** Return the size of the common prefix (if any) shared by zPrev and
123964** zNext, in bytes. For example,
123965**
123966**   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
123967**   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
123968**   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
123969*/
123970static int fts3PrefixCompress(
123971  const char *zPrev,              /* Buffer containing previous term */
123972  int nPrev,                      /* Size of buffer zPrev in bytes */
123973  const char *zNext,              /* Buffer containing next term */
123974  int nNext                       /* Size of buffer zNext in bytes */
123975){
123976  int n;
123977  UNUSED_PARAMETER(nNext);
123978  for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
123979  return n;
123980}
123981
123982/*
123983** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
123984** (according to memcmp) than the previous term.
123985*/
123986static int fts3NodeAddTerm(
123987  Fts3Table *p,                   /* Virtual table handle */
123988  SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
123989  int isCopyTerm,                 /* True if zTerm/nTerm is transient */
123990  const char *zTerm,              /* Pointer to buffer containing term */
123991  int nTerm                       /* Size of term in bytes */
123992){
123993  SegmentNode *pTree = *ppTree;
123994  int rc;
123995  SegmentNode *pNew;
123996
123997  /* First try to append the term to the current node. Return early if
123998  ** this is possible.
123999  */
124000  if( pTree ){
124001    int nData = pTree->nData;     /* Current size of node in bytes */
124002    int nReq = nData;             /* Required space after adding zTerm */
124003    int nPrefix;                  /* Number of bytes of prefix compression */
124004    int nSuffix;                  /* Suffix length */
124005
124006    nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
124007    nSuffix = nTerm-nPrefix;
124008
124009    nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
124010    if( nReq<=p->nNodeSize || !pTree->zTerm ){
124011
124012      if( nReq>p->nNodeSize ){
124013        /* An unusual case: this is the first term to be added to the node
124014        ** and the static node buffer (p->nNodeSize bytes) is not large
124015        ** enough. Use a separately malloced buffer instead This wastes
124016        ** p->nNodeSize bytes, but since this scenario only comes about when
124017        ** the database contain two terms that share a prefix of almost 2KB,
124018        ** this is not expected to be a serious problem.
124019        */
124020        assert( pTree->aData==(char *)&pTree[1] );
124021        pTree->aData = (char *)sqlite3_malloc(nReq);
124022        if( !pTree->aData ){
124023          return SQLITE_NOMEM;
124024        }
124025      }
124026
124027      if( pTree->zTerm ){
124028        /* There is no prefix-length field for first term in a node */
124029        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
124030      }
124031
124032      nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
124033      memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
124034      pTree->nData = nData + nSuffix;
124035      pTree->nEntry++;
124036
124037      if( isCopyTerm ){
124038        if( pTree->nMalloc<nTerm ){
124039          char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
124040          if( !zNew ){
124041            return SQLITE_NOMEM;
124042          }
124043          pTree->nMalloc = nTerm*2;
124044          pTree->zMalloc = zNew;
124045        }
124046        pTree->zTerm = pTree->zMalloc;
124047        memcpy(pTree->zTerm, zTerm, nTerm);
124048        pTree->nTerm = nTerm;
124049      }else{
124050        pTree->zTerm = (char *)zTerm;
124051        pTree->nTerm = nTerm;
124052      }
124053      return SQLITE_OK;
124054    }
124055  }
124056
124057  /* If control flows to here, it was not possible to append zTerm to the
124058  ** current node. Create a new node (a right-sibling of the current node).
124059  ** If this is the first node in the tree, the term is added to it.
124060  **
124061  ** Otherwise, the term is not added to the new node, it is left empty for
124062  ** now. Instead, the term is inserted into the parent of pTree. If pTree
124063  ** has no parent, one is created here.
124064  */
124065  pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
124066  if( !pNew ){
124067    return SQLITE_NOMEM;
124068  }
124069  memset(pNew, 0, sizeof(SegmentNode));
124070  pNew->nData = 1 + FTS3_VARINT_MAX;
124071  pNew->aData = (char *)&pNew[1];
124072
124073  if( pTree ){
124074    SegmentNode *pParent = pTree->pParent;
124075    rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
124076    if( pTree->pParent==0 ){
124077      pTree->pParent = pParent;
124078    }
124079    pTree->pRight = pNew;
124080    pNew->pLeftmost = pTree->pLeftmost;
124081    pNew->pParent = pParent;
124082    pNew->zMalloc = pTree->zMalloc;
124083    pNew->nMalloc = pTree->nMalloc;
124084    pTree->zMalloc = 0;
124085  }else{
124086    pNew->pLeftmost = pNew;
124087    rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
124088  }
124089
124090  *ppTree = pNew;
124091  return rc;
124092}
124093
124094/*
124095** Helper function for fts3NodeWrite().
124096*/
124097static int fts3TreeFinishNode(
124098  SegmentNode *pTree,
124099  int iHeight,
124100  sqlite3_int64 iLeftChild
124101){
124102  int nStart;
124103  assert( iHeight>=1 && iHeight<128 );
124104  nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
124105  pTree->aData[nStart] = (char)iHeight;
124106  sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
124107  return nStart;
124108}
124109
124110/*
124111** Write the buffer for the segment node pTree and all of its peers to the
124112** database. Then call this function recursively to write the parent of
124113** pTree and its peers to the database.
124114**
124115** Except, if pTree is a root node, do not write it to the database. Instead,
124116** set output variables *paRoot and *pnRoot to contain the root node.
124117**
124118** If successful, SQLITE_OK is returned and output variable *piLast is
124119** set to the largest blockid written to the database (or zero if no
124120** blocks were written to the db). Otherwise, an SQLite error code is
124121** returned.
124122*/
124123static int fts3NodeWrite(
124124  Fts3Table *p,                   /* Virtual table handle */
124125  SegmentNode *pTree,             /* SegmentNode handle */
124126  int iHeight,                    /* Height of this node in tree */
124127  sqlite3_int64 iLeaf,            /* Block id of first leaf node */
124128  sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
124129  sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
124130  char **paRoot,                  /* OUT: Data for root node */
124131  int *pnRoot                     /* OUT: Size of root node in bytes */
124132){
124133  int rc = SQLITE_OK;
124134
124135  if( !pTree->pParent ){
124136    /* Root node of the tree. */
124137    int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
124138    *piLast = iFree-1;
124139    *pnRoot = pTree->nData - nStart;
124140    *paRoot = &pTree->aData[nStart];
124141  }else{
124142    SegmentNode *pIter;
124143    sqlite3_int64 iNextFree = iFree;
124144    sqlite3_int64 iNextLeaf = iLeaf;
124145    for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
124146      int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
124147      int nWrite = pIter->nData - nStart;
124148
124149      rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
124150      iNextFree++;
124151      iNextLeaf += (pIter->nEntry+1);
124152    }
124153    if( rc==SQLITE_OK ){
124154      assert( iNextLeaf==iFree );
124155      rc = fts3NodeWrite(
124156          p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
124157      );
124158    }
124159  }
124160
124161  return rc;
124162}
124163
124164/*
124165** Free all memory allocations associated with the tree pTree.
124166*/
124167static void fts3NodeFree(SegmentNode *pTree){
124168  if( pTree ){
124169    SegmentNode *p = pTree->pLeftmost;
124170    fts3NodeFree(p->pParent);
124171    while( p ){
124172      SegmentNode *pRight = p->pRight;
124173      if( p->aData!=(char *)&p[1] ){
124174        sqlite3_free(p->aData);
124175      }
124176      assert( pRight==0 || p->zMalloc==0 );
124177      sqlite3_free(p->zMalloc);
124178      sqlite3_free(p);
124179      p = pRight;
124180    }
124181  }
124182}
124183
124184/*
124185** Add a term to the segment being constructed by the SegmentWriter object
124186** *ppWriter. When adding the first term to a segment, *ppWriter should
124187** be passed NULL. This function will allocate a new SegmentWriter object
124188** and return it via the input/output variable *ppWriter in this case.
124189**
124190** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
124191*/
124192static int fts3SegWriterAdd(
124193  Fts3Table *p,                   /* Virtual table handle */
124194  SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
124195  int isCopyTerm,                 /* True if buffer zTerm must be copied */
124196  const char *zTerm,              /* Pointer to buffer containing term */
124197  int nTerm,                      /* Size of term in bytes */
124198  const char *aDoclist,           /* Pointer to buffer containing doclist */
124199  int nDoclist                    /* Size of doclist in bytes */
124200){
124201  int nPrefix;                    /* Size of term prefix in bytes */
124202  int nSuffix;                    /* Size of term suffix in bytes */
124203  int nReq;                       /* Number of bytes required on leaf page */
124204  int nData;
124205  SegmentWriter *pWriter = *ppWriter;
124206
124207  if( !pWriter ){
124208    int rc;
124209    sqlite3_stmt *pStmt;
124210
124211    /* Allocate the SegmentWriter structure */
124212    pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
124213    if( !pWriter ) return SQLITE_NOMEM;
124214    memset(pWriter, 0, sizeof(SegmentWriter));
124215    *ppWriter = pWriter;
124216
124217    /* Allocate a buffer in which to accumulate data */
124218    pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
124219    if( !pWriter->aData ) return SQLITE_NOMEM;
124220    pWriter->nSize = p->nNodeSize;
124221
124222    /* Find the next free blockid in the %_segments table */
124223    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
124224    if( rc!=SQLITE_OK ) return rc;
124225    if( SQLITE_ROW==sqlite3_step(pStmt) ){
124226      pWriter->iFree = sqlite3_column_int64(pStmt, 0);
124227      pWriter->iFirst = pWriter->iFree;
124228    }
124229    rc = sqlite3_reset(pStmt);
124230    if( rc!=SQLITE_OK ) return rc;
124231  }
124232  nData = pWriter->nData;
124233
124234  nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
124235  nSuffix = nTerm-nPrefix;
124236
124237  /* Figure out how many bytes are required by this new entry */
124238  nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
124239    sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
124240    nSuffix +                               /* Term suffix */
124241    sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
124242    nDoclist;                               /* Doclist data */
124243
124244  if( nData>0 && nData+nReq>p->nNodeSize ){
124245    int rc;
124246
124247    /* The current leaf node is full. Write it out to the database. */
124248    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
124249    if( rc!=SQLITE_OK ) return rc;
124250
124251    /* Add the current term to the interior node tree. The term added to
124252    ** the interior tree must:
124253    **
124254    **   a) be greater than the largest term on the leaf node just written
124255    **      to the database (still available in pWriter->zTerm), and
124256    **
124257    **   b) be less than or equal to the term about to be added to the new
124258    **      leaf node (zTerm/nTerm).
124259    **
124260    ** In other words, it must be the prefix of zTerm 1 byte longer than
124261    ** the common prefix (if any) of zTerm and pWriter->zTerm.
124262    */
124263    assert( nPrefix<nTerm );
124264    rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
124265    if( rc!=SQLITE_OK ) return rc;
124266
124267    nData = 0;
124268    pWriter->nTerm = 0;
124269
124270    nPrefix = 0;
124271    nSuffix = nTerm;
124272    nReq = 1 +                              /* varint containing prefix size */
124273      sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
124274      nTerm +                               /* Term suffix */
124275      sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
124276      nDoclist;                             /* Doclist data */
124277  }
124278
124279  /* If the buffer currently allocated is too small for this entry, realloc
124280  ** the buffer to make it large enough.
124281  */
124282  if( nReq>pWriter->nSize ){
124283    char *aNew = sqlite3_realloc(pWriter->aData, nReq);
124284    if( !aNew ) return SQLITE_NOMEM;
124285    pWriter->aData = aNew;
124286    pWriter->nSize = nReq;
124287  }
124288  assert( nData+nReq<=pWriter->nSize );
124289
124290  /* Append the prefix-compressed term and doclist to the buffer. */
124291  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
124292  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
124293  memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
124294  nData += nSuffix;
124295  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
124296  memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
124297  pWriter->nData = nData + nDoclist;
124298
124299  /* Save the current term so that it can be used to prefix-compress the next.
124300  ** If the isCopyTerm parameter is true, then the buffer pointed to by
124301  ** zTerm is transient, so take a copy of the term data. Otherwise, just
124302  ** store a copy of the pointer.
124303  */
124304  if( isCopyTerm ){
124305    if( nTerm>pWriter->nMalloc ){
124306      char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
124307      if( !zNew ){
124308        return SQLITE_NOMEM;
124309      }
124310      pWriter->nMalloc = nTerm*2;
124311      pWriter->zMalloc = zNew;
124312      pWriter->zTerm = zNew;
124313    }
124314    assert( pWriter->zTerm==pWriter->zMalloc );
124315    memcpy(pWriter->zTerm, zTerm, nTerm);
124316  }else{
124317    pWriter->zTerm = (char *)zTerm;
124318  }
124319  pWriter->nTerm = nTerm;
124320
124321  return SQLITE_OK;
124322}
124323
124324/*
124325** Flush all data associated with the SegmentWriter object pWriter to the
124326** database. This function must be called after all terms have been added
124327** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
124328** returned. Otherwise, an SQLite error code.
124329*/
124330static int fts3SegWriterFlush(
124331  Fts3Table *p,                   /* Virtual table handle */
124332  SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
124333  int iLevel,                     /* Value for 'level' column of %_segdir */
124334  int iIdx                        /* Value for 'idx' column of %_segdir */
124335){
124336  int rc;                         /* Return code */
124337  if( pWriter->pTree ){
124338    sqlite3_int64 iLast = 0;      /* Largest block id written to database */
124339    sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
124340    char *zRoot = NULL;           /* Pointer to buffer containing root node */
124341    int nRoot = 0;                /* Size of buffer zRoot */
124342
124343    iLastLeaf = pWriter->iFree;
124344    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
124345    if( rc==SQLITE_OK ){
124346      rc = fts3NodeWrite(p, pWriter->pTree, 1,
124347          pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
124348    }
124349    if( rc==SQLITE_OK ){
124350      rc = fts3WriteSegdir(
124351          p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
124352    }
124353  }else{
124354    /* The entire tree fits on the root node. Write it to the segdir table. */
124355    rc = fts3WriteSegdir(
124356        p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
124357  }
124358  return rc;
124359}
124360
124361/*
124362** Release all memory held by the SegmentWriter object passed as the
124363** first argument.
124364*/
124365static void fts3SegWriterFree(SegmentWriter *pWriter){
124366  if( pWriter ){
124367    sqlite3_free(pWriter->aData);
124368    sqlite3_free(pWriter->zMalloc);
124369    fts3NodeFree(pWriter->pTree);
124370    sqlite3_free(pWriter);
124371  }
124372}
124373
124374/*
124375** The first value in the apVal[] array is assumed to contain an integer.
124376** This function tests if there exist any documents with docid values that
124377** are different from that integer. i.e. if deleting the document with docid
124378** pRowid would mean the FTS3 table were empty.
124379**
124380** If successful, *pisEmpty is set to true if the table is empty except for
124381** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
124382** error occurs, an SQLite error code is returned.
124383*/
124384static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
124385  sqlite3_stmt *pStmt;
124386  int rc;
124387  rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
124388  if( rc==SQLITE_OK ){
124389    if( SQLITE_ROW==sqlite3_step(pStmt) ){
124390      *pisEmpty = sqlite3_column_int(pStmt, 0);
124391    }
124392    rc = sqlite3_reset(pStmt);
124393  }
124394  return rc;
124395}
124396
124397/*
124398** Set *pnMax to the largest segment level in the database for the index
124399** iIndex.
124400**
124401** Segment levels are stored in the 'level' column of the %_segdir table.
124402**
124403** Return SQLITE_OK if successful, or an SQLite error code if not.
124404*/
124405static int fts3SegmentMaxLevel(Fts3Table *p, int iIndex, int *pnMax){
124406  sqlite3_stmt *pStmt;
124407  int rc;
124408  assert( iIndex>=0 && iIndex<p->nIndex );
124409
124410  /* Set pStmt to the compiled version of:
124411  **
124412  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
124413  **
124414  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
124415  */
124416  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
124417  if( rc!=SQLITE_OK ) return rc;
124418  sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
124419  sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL - 1);
124420  if( SQLITE_ROW==sqlite3_step(pStmt) ){
124421    *pnMax = sqlite3_column_int(pStmt, 0);
124422  }
124423  return sqlite3_reset(pStmt);
124424}
124425
124426/*
124427** This function is used after merging multiple segments into a single large
124428** segment to delete the old, now redundant, segment b-trees. Specifically,
124429** it:
124430**
124431**   1) Deletes all %_segments entries for the segments associated with
124432**      each of the SegReader objects in the array passed as the third
124433**      argument, and
124434**
124435**   2) deletes all %_segdir entries with level iLevel, or all %_segdir
124436**      entries regardless of level if (iLevel<0).
124437**
124438** SQLITE_OK is returned if successful, otherwise an SQLite error code.
124439*/
124440static int fts3DeleteSegdir(
124441  Fts3Table *p,                   /* Virtual table handle */
124442  int iIndex,                     /* Index for p->aIndex */
124443  int iLevel,                     /* Level of %_segdir entries to delete */
124444  Fts3SegReader **apSegment,      /* Array of SegReader objects */
124445  int nReader                     /* Size of array apSegment */
124446){
124447  int rc;                         /* Return Code */
124448  int i;                          /* Iterator variable */
124449  sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
124450
124451  rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
124452  for(i=0; rc==SQLITE_OK && i<nReader; i++){
124453    Fts3SegReader *pSegment = apSegment[i];
124454    if( pSegment->iStartBlock ){
124455      sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
124456      sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
124457      sqlite3_step(pDelete);
124458      rc = sqlite3_reset(pDelete);
124459    }
124460  }
124461  if( rc!=SQLITE_OK ){
124462    return rc;
124463  }
124464
124465  assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
124466  if( iLevel==FTS3_SEGCURSOR_ALL ){
124467    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
124468    if( rc==SQLITE_OK ){
124469      sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
124470      sqlite3_bind_int(pDelete, 2, (iIndex+1) * FTS3_SEGDIR_MAXLEVEL - 1);
124471    }
124472  }else{
124473    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
124474    if( rc==SQLITE_OK ){
124475      sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
124476    }
124477  }
124478
124479  if( rc==SQLITE_OK ){
124480    sqlite3_step(pDelete);
124481    rc = sqlite3_reset(pDelete);
124482  }
124483
124484  return rc;
124485}
124486
124487/*
124488** When this function is called, buffer *ppList (size *pnList bytes) contains
124489** a position list that may (or may not) feature multiple columns. This
124490** function adjusts the pointer *ppList and the length *pnList so that they
124491** identify the subset of the position list that corresponds to column iCol.
124492**
124493** If there are no entries in the input position list for column iCol, then
124494** *pnList is set to zero before returning.
124495*/
124496static void fts3ColumnFilter(
124497  int iCol,                       /* Column to filter on */
124498  char **ppList,                  /* IN/OUT: Pointer to position list */
124499  int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
124500){
124501  char *pList = *ppList;
124502  int nList = *pnList;
124503  char *pEnd = &pList[nList];
124504  int iCurrent = 0;
124505  char *p = pList;
124506
124507  assert( iCol>=0 );
124508  while( 1 ){
124509    char c = 0;
124510    while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
124511
124512    if( iCol==iCurrent ){
124513      nList = (int)(p - pList);
124514      break;
124515    }
124516
124517    nList -= (int)(p - pList);
124518    pList = p;
124519    if( nList==0 ){
124520      break;
124521    }
124522    p = &pList[1];
124523    p += sqlite3Fts3GetVarint32(p, &iCurrent);
124524  }
124525
124526  *ppList = pList;
124527  *pnList = nList;
124528}
124529
124530/*
124531** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
124532** existing data). Grow the buffer if required.
124533**
124534** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
124535** trying to resize the buffer, return SQLITE_NOMEM.
124536*/
124537static int fts3MsrBufferData(
124538  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
124539  char *pList,
124540  int nList
124541){
124542  if( nList>pMsr->nBuffer ){
124543    char *pNew;
124544    pMsr->nBuffer = nList*2;
124545    pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
124546    if( !pNew ) return SQLITE_NOMEM;
124547    pMsr->aBuffer = pNew;
124548  }
124549
124550  memcpy(pMsr->aBuffer, pList, nList);
124551  return SQLITE_OK;
124552}
124553
124554SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
124555  Fts3Table *p,                   /* Virtual table handle */
124556  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
124557  sqlite3_int64 *piDocid,         /* OUT: Docid value */
124558  char **paPoslist,               /* OUT: Pointer to position list */
124559  int *pnPoslist                  /* OUT: Size of position list in bytes */
124560){
124561  int nMerge = pMsr->nAdvance;
124562  Fts3SegReader **apSegment = pMsr->apSegment;
124563  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
124564    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
124565  );
124566
124567  if( nMerge==0 ){
124568    *paPoslist = 0;
124569    return SQLITE_OK;
124570  }
124571
124572  while( 1 ){
124573    Fts3SegReader *pSeg;
124574    pSeg = pMsr->apSegment[0];
124575
124576    if( pSeg->pOffsetList==0 ){
124577      *paPoslist = 0;
124578      break;
124579    }else{
124580      int rc;
124581      char *pList;
124582      int nList;
124583      int j;
124584      sqlite3_int64 iDocid = apSegment[0]->iDocid;
124585
124586      rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
124587      j = 1;
124588      while( rc==SQLITE_OK
124589        && j<nMerge
124590        && apSegment[j]->pOffsetList
124591        && apSegment[j]->iDocid==iDocid
124592      ){
124593        rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
124594        j++;
124595      }
124596      if( rc!=SQLITE_OK ) return rc;
124597      fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
124598
124599      if( pMsr->iColFilter>=0 ){
124600        fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
124601      }
124602
124603      if( nList>0 ){
124604        if( fts3SegReaderIsPending(apSegment[0]) ){
124605          rc = fts3MsrBufferData(pMsr, pList, nList+1);
124606          if( rc!=SQLITE_OK ) return rc;
124607          *paPoslist = pMsr->aBuffer;
124608          assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
124609        }else{
124610          *paPoslist = pList;
124611        }
124612        *piDocid = iDocid;
124613        *pnPoslist = nList;
124614        break;
124615      }
124616    }
124617  }
124618
124619  return SQLITE_OK;
124620}
124621
124622static int fts3SegReaderStart(
124623  Fts3Table *p,                   /* Virtual table handle */
124624  Fts3MultiSegReader *pCsr,       /* Cursor object */
124625  const char *zTerm,              /* Term searched for (or NULL) */
124626  int nTerm                       /* Length of zTerm in bytes */
124627){
124628  int i;
124629  int nSeg = pCsr->nSegment;
124630
124631  /* If the Fts3SegFilter defines a specific term (or term prefix) to search
124632  ** for, then advance each segment iterator until it points to a term of
124633  ** equal or greater value than the specified term. This prevents many
124634  ** unnecessary merge/sort operations for the case where single segment
124635  ** b-tree leaf nodes contain more than one term.
124636  */
124637  for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
124638    Fts3SegReader *pSeg = pCsr->apSegment[i];
124639    do {
124640      int rc = fts3SegReaderNext(p, pSeg, 0);
124641      if( rc!=SQLITE_OK ) return rc;
124642    }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
124643  }
124644  fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
124645
124646  return SQLITE_OK;
124647}
124648
124649SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
124650  Fts3Table *p,                   /* Virtual table handle */
124651  Fts3MultiSegReader *pCsr,       /* Cursor object */
124652  Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
124653){
124654  pCsr->pFilter = pFilter;
124655  return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
124656}
124657
124658SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
124659  Fts3Table *p,                   /* Virtual table handle */
124660  Fts3MultiSegReader *pCsr,       /* Cursor object */
124661  int iCol,                       /* Column to match on. */
124662  const char *zTerm,              /* Term to iterate through a doclist for */
124663  int nTerm                       /* Number of bytes in zTerm */
124664){
124665  int i;
124666  int rc;
124667  int nSegment = pCsr->nSegment;
124668  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
124669    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
124670  );
124671
124672  assert( pCsr->pFilter==0 );
124673  assert( zTerm && nTerm>0 );
124674
124675  /* Advance each segment iterator until it points to the term zTerm/nTerm. */
124676  rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
124677  if( rc!=SQLITE_OK ) return rc;
124678
124679  /* Determine how many of the segments actually point to zTerm/nTerm. */
124680  for(i=0; i<nSegment; i++){
124681    Fts3SegReader *pSeg = pCsr->apSegment[i];
124682    if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
124683      break;
124684    }
124685  }
124686  pCsr->nAdvance = i;
124687
124688  /* Advance each of the segments to point to the first docid. */
124689  for(i=0; i<pCsr->nAdvance; i++){
124690    rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
124691    if( rc!=SQLITE_OK ) return rc;
124692  }
124693  fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
124694
124695  assert( iCol<0 || iCol<p->nColumn );
124696  pCsr->iColFilter = iCol;
124697
124698  return SQLITE_OK;
124699}
124700
124701/*
124702** This function is called on a MultiSegReader that has been started using
124703** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
124704** have been made. Calling this function puts the MultiSegReader in such
124705** a state that if the next two calls are:
124706**
124707**   sqlite3Fts3SegReaderStart()
124708**   sqlite3Fts3SegReaderStep()
124709**
124710** then the entire doclist for the term is available in
124711** MultiSegReader.aDoclist/nDoclist.
124712*/
124713SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
124714  int i;                          /* Used to iterate through segment-readers */
124715
124716  assert( pCsr->zTerm==0 );
124717  assert( pCsr->nTerm==0 );
124718  assert( pCsr->aDoclist==0 );
124719  assert( pCsr->nDoclist==0 );
124720
124721  pCsr->nAdvance = 0;
124722  pCsr->bRestart = 1;
124723  for(i=0; i<pCsr->nSegment; i++){
124724    pCsr->apSegment[i]->pOffsetList = 0;
124725    pCsr->apSegment[i]->nOffsetList = 0;
124726    pCsr->apSegment[i]->iDocid = 0;
124727  }
124728
124729  return SQLITE_OK;
124730}
124731
124732
124733SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
124734  Fts3Table *p,                   /* Virtual table handle */
124735  Fts3MultiSegReader *pCsr        /* Cursor object */
124736){
124737  int rc = SQLITE_OK;
124738
124739  int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
124740  int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
124741  int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
124742  int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
124743  int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
124744
124745  Fts3SegReader **apSegment = pCsr->apSegment;
124746  int nSegment = pCsr->nSegment;
124747  Fts3SegFilter *pFilter = pCsr->pFilter;
124748  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
124749    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
124750  );
124751
124752  if( pCsr->nSegment==0 ) return SQLITE_OK;
124753
124754  do {
124755    int nMerge;
124756    int i;
124757
124758    /* Advance the first pCsr->nAdvance entries in the apSegment[] array
124759    ** forward. Then sort the list in order of current term again.
124760    */
124761    for(i=0; i<pCsr->nAdvance; i++){
124762      rc = fts3SegReaderNext(p, apSegment[i], 0);
124763      if( rc!=SQLITE_OK ) return rc;
124764    }
124765    fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
124766    pCsr->nAdvance = 0;
124767
124768    /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
124769    assert( rc==SQLITE_OK );
124770    if( apSegment[0]->aNode==0 ) break;
124771
124772    pCsr->nTerm = apSegment[0]->nTerm;
124773    pCsr->zTerm = apSegment[0]->zTerm;
124774
124775    /* If this is a prefix-search, and if the term that apSegment[0] points
124776    ** to does not share a suffix with pFilter->zTerm/nTerm, then all
124777    ** required callbacks have been made. In this case exit early.
124778    **
124779    ** Similarly, if this is a search for an exact match, and the first term
124780    ** of segment apSegment[0] is not a match, exit early.
124781    */
124782    if( pFilter->zTerm && !isScan ){
124783      if( pCsr->nTerm<pFilter->nTerm
124784       || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
124785       || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
124786      ){
124787        break;
124788      }
124789    }
124790
124791    nMerge = 1;
124792    while( nMerge<nSegment
124793        && apSegment[nMerge]->aNode
124794        && apSegment[nMerge]->nTerm==pCsr->nTerm
124795        && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
124796    ){
124797      nMerge++;
124798    }
124799
124800    assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
124801    if( nMerge==1
124802     && !isIgnoreEmpty
124803     && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
124804    ){
124805      pCsr->nDoclist = apSegment[0]->nDoclist;
124806      if( fts3SegReaderIsPending(apSegment[0]) ){
124807        rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
124808        pCsr->aDoclist = pCsr->aBuffer;
124809      }else{
124810        pCsr->aDoclist = apSegment[0]->aDoclist;
124811      }
124812      if( rc==SQLITE_OK ) rc = SQLITE_ROW;
124813    }else{
124814      int nDoclist = 0;           /* Size of doclist */
124815      sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
124816
124817      /* The current term of the first nMerge entries in the array
124818      ** of Fts3SegReader objects is the same. The doclists must be merged
124819      ** and a single term returned with the merged doclist.
124820      */
124821      for(i=0; i<nMerge; i++){
124822        fts3SegReaderFirstDocid(p, apSegment[i]);
124823      }
124824      fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
124825      while( apSegment[0]->pOffsetList ){
124826        int j;                    /* Number of segments that share a docid */
124827        char *pList;
124828        int nList;
124829        int nByte;
124830        sqlite3_int64 iDocid = apSegment[0]->iDocid;
124831        fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
124832        j = 1;
124833        while( j<nMerge
124834            && apSegment[j]->pOffsetList
124835            && apSegment[j]->iDocid==iDocid
124836        ){
124837          fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
124838          j++;
124839        }
124840
124841        if( isColFilter ){
124842          fts3ColumnFilter(pFilter->iCol, &pList, &nList);
124843        }
124844
124845        if( !isIgnoreEmpty || nList>0 ){
124846
124847          /* Calculate the 'docid' delta value to write into the merged
124848          ** doclist. */
124849          sqlite3_int64 iDelta;
124850          if( p->bDescIdx && nDoclist>0 ){
124851            iDelta = iPrev - iDocid;
124852          }else{
124853            iDelta = iDocid - iPrev;
124854          }
124855          assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
124856          assert( nDoclist>0 || iDelta==iDocid );
124857
124858          nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
124859          if( nDoclist+nByte>pCsr->nBuffer ){
124860            char *aNew;
124861            pCsr->nBuffer = (nDoclist+nByte)*2;
124862            aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
124863            if( !aNew ){
124864              return SQLITE_NOMEM;
124865            }
124866            pCsr->aBuffer = aNew;
124867          }
124868          nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
124869          iPrev = iDocid;
124870          if( isRequirePos ){
124871            memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
124872            nDoclist += nList;
124873            pCsr->aBuffer[nDoclist++] = '\0';
124874          }
124875        }
124876
124877        fts3SegReaderSort(apSegment, nMerge, j, xCmp);
124878      }
124879      if( nDoclist>0 ){
124880        pCsr->aDoclist = pCsr->aBuffer;
124881        pCsr->nDoclist = nDoclist;
124882        rc = SQLITE_ROW;
124883      }
124884    }
124885    pCsr->nAdvance = nMerge;
124886  }while( rc==SQLITE_OK );
124887
124888  return rc;
124889}
124890
124891
124892SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
124893  Fts3MultiSegReader *pCsr       /* Cursor object */
124894){
124895  if( pCsr ){
124896    int i;
124897    for(i=0; i<pCsr->nSegment; i++){
124898      sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
124899    }
124900    sqlite3_free(pCsr->apSegment);
124901    sqlite3_free(pCsr->aBuffer);
124902
124903    pCsr->nSegment = 0;
124904    pCsr->apSegment = 0;
124905    pCsr->aBuffer = 0;
124906  }
124907}
124908
124909/*
124910** Merge all level iLevel segments in the database into a single
124911** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
124912** single segment with a level equal to the numerically largest level
124913** currently present in the database.
124914**
124915** If this function is called with iLevel<0, but there is only one
124916** segment in the database, SQLITE_DONE is returned immediately.
124917** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
124918** an SQLite error code is returned.
124919*/
124920static int fts3SegmentMerge(Fts3Table *p, int iIndex, int iLevel){
124921  int rc;                         /* Return code */
124922  int iIdx = 0;                   /* Index of new segment */
124923  int iNewLevel = 0;              /* Level/index to create new segment at */
124924  SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
124925  Fts3SegFilter filter;           /* Segment term filter condition */
124926  Fts3MultiSegReader csr;        /* Cursor to iterate through level(s) */
124927  int bIgnoreEmpty = 0;           /* True to ignore empty segments */
124928
124929  assert( iLevel==FTS3_SEGCURSOR_ALL
124930       || iLevel==FTS3_SEGCURSOR_PENDING
124931       || iLevel>=0
124932  );
124933  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
124934  assert( iIndex>=0 && iIndex<p->nIndex );
124935
124936  rc = sqlite3Fts3SegReaderCursor(p, iIndex, iLevel, 0, 0, 1, 0, &csr);
124937  if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
124938
124939  if( iLevel==FTS3_SEGCURSOR_ALL ){
124940    /* This call is to merge all segments in the database to a single
124941    ** segment. The level of the new segment is equal to the the numerically
124942    ** greatest segment level currently present in the database for this
124943    ** index. The idx of the new segment is always 0.  */
124944    if( csr.nSegment==1 ){
124945      rc = SQLITE_DONE;
124946      goto finished;
124947    }
124948    rc = fts3SegmentMaxLevel(p, iIndex, &iNewLevel);
124949    bIgnoreEmpty = 1;
124950
124951  }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
124952    iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL;
124953    rc = fts3AllocateSegdirIdx(p, iIndex, 0, &iIdx);
124954  }else{
124955    /* This call is to merge all segments at level iLevel. find the next
124956    ** available segment index at level iLevel+1. The call to
124957    ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
124958    ** a single iLevel+2 segment if necessary.  */
124959    rc = fts3AllocateSegdirIdx(p, iIndex, iLevel+1, &iIdx);
124960    iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL + iLevel+1;
124961  }
124962  if( rc!=SQLITE_OK ) goto finished;
124963  assert( csr.nSegment>0 );
124964  assert( iNewLevel>=(iIndex*FTS3_SEGDIR_MAXLEVEL) );
124965  assert( iNewLevel<((iIndex+1)*FTS3_SEGDIR_MAXLEVEL) );
124966
124967  memset(&filter, 0, sizeof(Fts3SegFilter));
124968  filter.flags = FTS3_SEGMENT_REQUIRE_POS;
124969  filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
124970
124971  rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
124972  while( SQLITE_OK==rc ){
124973    rc = sqlite3Fts3SegReaderStep(p, &csr);
124974    if( rc!=SQLITE_ROW ) break;
124975    rc = fts3SegWriterAdd(p, &pWriter, 1,
124976        csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
124977  }
124978  if( rc!=SQLITE_OK ) goto finished;
124979  assert( pWriter );
124980
124981  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
124982    rc = fts3DeleteSegdir(p, iIndex, iLevel, csr.apSegment, csr.nSegment);
124983    if( rc!=SQLITE_OK ) goto finished;
124984  }
124985  rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
124986
124987 finished:
124988  fts3SegWriterFree(pWriter);
124989  sqlite3Fts3SegReaderFinish(&csr);
124990  return rc;
124991}
124992
124993
124994/*
124995** Flush the contents of pendingTerms to level 0 segments.
124996*/
124997SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
124998  int rc = SQLITE_OK;
124999  int i;
125000  for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
125001    rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_PENDING);
125002    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
125003  }
125004  sqlite3Fts3PendingTermsClear(p);
125005  return rc;
125006}
125007
125008/*
125009** Encode N integers as varints into a blob.
125010*/
125011static void fts3EncodeIntArray(
125012  int N,             /* The number of integers to encode */
125013  u32 *a,            /* The integer values */
125014  char *zBuf,        /* Write the BLOB here */
125015  int *pNBuf         /* Write number of bytes if zBuf[] used here */
125016){
125017  int i, j;
125018  for(i=j=0; i<N; i++){
125019    j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
125020  }
125021  *pNBuf = j;
125022}
125023
125024/*
125025** Decode a blob of varints into N integers
125026*/
125027static void fts3DecodeIntArray(
125028  int N,             /* The number of integers to decode */
125029  u32 *a,            /* Write the integer values */
125030  const char *zBuf,  /* The BLOB containing the varints */
125031  int nBuf           /* size of the BLOB */
125032){
125033  int i, j;
125034  UNUSED_PARAMETER(nBuf);
125035  for(i=j=0; i<N; i++){
125036    sqlite3_int64 x;
125037    j += sqlite3Fts3GetVarint(&zBuf[j], &x);
125038    assert(j<=nBuf);
125039    a[i] = (u32)(x & 0xffffffff);
125040  }
125041}
125042
125043/*
125044** Insert the sizes (in tokens) for each column of the document
125045** with docid equal to p->iPrevDocid.  The sizes are encoded as
125046** a blob of varints.
125047*/
125048static void fts3InsertDocsize(
125049  int *pRC,         /* Result code */
125050  Fts3Table *p,     /* Table into which to insert */
125051  u32 *aSz          /* Sizes of each column */
125052){
125053  char *pBlob;             /* The BLOB encoding of the document size */
125054  int nBlob;               /* Number of bytes in the BLOB */
125055  sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
125056  int rc;                  /* Result code from subfunctions */
125057
125058  if( *pRC ) return;
125059  pBlob = sqlite3_malloc( 10*p->nColumn );
125060  if( pBlob==0 ){
125061    *pRC = SQLITE_NOMEM;
125062    return;
125063  }
125064  fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
125065  rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
125066  if( rc ){
125067    sqlite3_free(pBlob);
125068    *pRC = rc;
125069    return;
125070  }
125071  sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
125072  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
125073  sqlite3_step(pStmt);
125074  *pRC = sqlite3_reset(pStmt);
125075}
125076
125077/*
125078** Record 0 of the %_stat table contains a blob consisting of N varints,
125079** where N is the number of user defined columns in the fts3 table plus
125080** two. If nCol is the number of user defined columns, then values of the
125081** varints are set as follows:
125082**
125083**   Varint 0:       Total number of rows in the table.
125084**
125085**   Varint 1..nCol: For each column, the total number of tokens stored in
125086**                   the column for all rows of the table.
125087**
125088**   Varint 1+nCol:  The total size, in bytes, of all text values in all
125089**                   columns of all rows of the table.
125090**
125091*/
125092static void fts3UpdateDocTotals(
125093  int *pRC,                       /* The result code */
125094  Fts3Table *p,                   /* Table being updated */
125095  u32 *aSzIns,                    /* Size increases */
125096  u32 *aSzDel,                    /* Size decreases */
125097  int nChng                       /* Change in the number of documents */
125098){
125099  char *pBlob;             /* Storage for BLOB written into %_stat */
125100  int nBlob;               /* Size of BLOB written into %_stat */
125101  u32 *a;                  /* Array of integers that becomes the BLOB */
125102  sqlite3_stmt *pStmt;     /* Statement for reading and writing */
125103  int i;                   /* Loop counter */
125104  int rc;                  /* Result code from subfunctions */
125105
125106  const int nStat = p->nColumn+2;
125107
125108  if( *pRC ) return;
125109  a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
125110  if( a==0 ){
125111    *pRC = SQLITE_NOMEM;
125112    return;
125113  }
125114  pBlob = (char*)&a[nStat];
125115  rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
125116  if( rc ){
125117    sqlite3_free(a);
125118    *pRC = rc;
125119    return;
125120  }
125121  if( sqlite3_step(pStmt)==SQLITE_ROW ){
125122    fts3DecodeIntArray(nStat, a,
125123         sqlite3_column_blob(pStmt, 0),
125124         sqlite3_column_bytes(pStmt, 0));
125125  }else{
125126    memset(a, 0, sizeof(u32)*(nStat) );
125127  }
125128  sqlite3_reset(pStmt);
125129  if( nChng<0 && a[0]<(u32)(-nChng) ){
125130    a[0] = 0;
125131  }else{
125132    a[0] += nChng;
125133  }
125134  for(i=0; i<p->nColumn+1; i++){
125135    u32 x = a[i+1];
125136    if( x+aSzIns[i] < aSzDel[i] ){
125137      x = 0;
125138    }else{
125139      x = x + aSzIns[i] - aSzDel[i];
125140    }
125141    a[i+1] = x;
125142  }
125143  fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
125144  rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
125145  if( rc ){
125146    sqlite3_free(a);
125147    *pRC = rc;
125148    return;
125149  }
125150  sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
125151  sqlite3_step(pStmt);
125152  *pRC = sqlite3_reset(pStmt);
125153  sqlite3_free(a);
125154}
125155
125156static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
125157  int i;
125158  int bSeenDone = 0;
125159  int rc = SQLITE_OK;
125160  for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
125161    rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_ALL);
125162    if( rc==SQLITE_DONE ){
125163      bSeenDone = 1;
125164      rc = SQLITE_OK;
125165    }
125166  }
125167  sqlite3Fts3SegmentsClose(p);
125168  sqlite3Fts3PendingTermsClear(p);
125169
125170  return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
125171}
125172
125173/*
125174** Handle a 'special' INSERT of the form:
125175**
125176**   "INSERT INTO tbl(tbl) VALUES(<expr>)"
125177**
125178** Argument pVal contains the result of <expr>. Currently the only
125179** meaningful value to insert is the text 'optimize'.
125180*/
125181static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
125182  int rc;                         /* Return Code */
125183  const char *zVal = (const char *)sqlite3_value_text(pVal);
125184  int nVal = sqlite3_value_bytes(pVal);
125185
125186  if( !zVal ){
125187    return SQLITE_NOMEM;
125188  }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
125189    rc = fts3DoOptimize(p, 0);
125190#ifdef SQLITE_TEST
125191  }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
125192    p->nNodeSize = atoi(&zVal[9]);
125193    rc = SQLITE_OK;
125194  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
125195    p->nMaxPendingData = atoi(&zVal[11]);
125196    rc = SQLITE_OK;
125197#endif
125198  }else{
125199    rc = SQLITE_ERROR;
125200  }
125201
125202  return rc;
125203}
125204
125205/*
125206** Delete all cached deferred doclists. Deferred doclists are cached
125207** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
125208*/
125209SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
125210  Fts3DeferredToken *pDef;
125211  for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
125212    fts3PendingListDelete(pDef->pList);
125213    pDef->pList = 0;
125214  }
125215}
125216
125217/*
125218** Free all entries in the pCsr->pDeffered list. Entries are added to
125219** this list using sqlite3Fts3DeferToken().
125220*/
125221SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
125222  Fts3DeferredToken *pDef;
125223  Fts3DeferredToken *pNext;
125224  for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
125225    pNext = pDef->pNext;
125226    fts3PendingListDelete(pDef->pList);
125227    sqlite3_free(pDef);
125228  }
125229  pCsr->pDeferred = 0;
125230}
125231
125232/*
125233** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
125234** based on the row that pCsr currently points to.
125235**
125236** A deferred-doclist is like any other doclist with position information
125237** included, except that it only contains entries for a single row of the
125238** table, not for all rows.
125239*/
125240SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
125241  int rc = SQLITE_OK;             /* Return code */
125242  if( pCsr->pDeferred ){
125243    int i;                        /* Used to iterate through table columns */
125244    sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
125245    Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
125246
125247    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
125248    sqlite3_tokenizer *pT = p->pTokenizer;
125249    sqlite3_tokenizer_module const *pModule = pT->pModule;
125250
125251    assert( pCsr->isRequireSeek==0 );
125252    iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
125253
125254    for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
125255      const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
125256      sqlite3_tokenizer_cursor *pTC = 0;
125257
125258      rc = pModule->xOpen(pT, zText, -1, &pTC);
125259      while( rc==SQLITE_OK ){
125260        char const *zToken;       /* Buffer containing token */
125261        int nToken;               /* Number of bytes in token */
125262        int iDum1, iDum2;         /* Dummy variables */
125263        int iPos;                 /* Position of token in zText */
125264
125265        pTC->pTokenizer = pT;
125266        rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
125267        for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
125268          Fts3PhraseToken *pPT = pDef->pToken;
125269          if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
125270           && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
125271           && (0==memcmp(zToken, pPT->z, pPT->n))
125272          ){
125273            fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
125274          }
125275        }
125276      }
125277      if( pTC ) pModule->xClose(pTC);
125278      if( rc==SQLITE_DONE ) rc = SQLITE_OK;
125279    }
125280
125281    for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
125282      if( pDef->pList ){
125283        rc = fts3PendingListAppendVarint(&pDef->pList, 0);
125284      }
125285    }
125286  }
125287
125288  return rc;
125289}
125290
125291SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
125292  Fts3DeferredToken *p,
125293  char **ppData,
125294  int *pnData
125295){
125296  char *pRet;
125297  int nSkip;
125298  sqlite3_int64 dummy;
125299
125300  *ppData = 0;
125301  *pnData = 0;
125302
125303  if( p->pList==0 ){
125304    return SQLITE_OK;
125305  }
125306
125307  pRet = (char *)sqlite3_malloc(p->pList->nData);
125308  if( !pRet ) return SQLITE_NOMEM;
125309
125310  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
125311  *pnData = p->pList->nData - nSkip;
125312  *ppData = pRet;
125313
125314  memcpy(pRet, &p->pList->aData[nSkip], *pnData);
125315  return SQLITE_OK;
125316}
125317
125318/*
125319** Add an entry for token pToken to the pCsr->pDeferred list.
125320*/
125321SQLITE_PRIVATE int sqlite3Fts3DeferToken(
125322  Fts3Cursor *pCsr,               /* Fts3 table cursor */
125323  Fts3PhraseToken *pToken,        /* Token to defer */
125324  int iCol                        /* Column that token must appear in (or -1) */
125325){
125326  Fts3DeferredToken *pDeferred;
125327  pDeferred = sqlite3_malloc(sizeof(*pDeferred));
125328  if( !pDeferred ){
125329    return SQLITE_NOMEM;
125330  }
125331  memset(pDeferred, 0, sizeof(*pDeferred));
125332  pDeferred->pToken = pToken;
125333  pDeferred->pNext = pCsr->pDeferred;
125334  pDeferred->iCol = iCol;
125335  pCsr->pDeferred = pDeferred;
125336
125337  assert( pToken->pDeferred==0 );
125338  pToken->pDeferred = pDeferred;
125339
125340  return SQLITE_OK;
125341}
125342
125343/*
125344** SQLite value pRowid contains the rowid of a row that may or may not be
125345** present in the FTS3 table. If it is, delete it and adjust the contents
125346** of subsiduary data structures accordingly.
125347*/
125348static int fts3DeleteByRowid(
125349  Fts3Table *p,
125350  sqlite3_value *pRowid,
125351  int *pnDoc,
125352  u32 *aSzDel
125353){
125354  int isEmpty = 0;
125355  int rc = fts3IsEmpty(p, pRowid, &isEmpty);
125356  if( rc==SQLITE_OK ){
125357    if( isEmpty ){
125358      /* Deleting this row means the whole table is empty. In this case
125359      ** delete the contents of all three tables and throw away any
125360      ** data in the pendingTerms hash table.  */
125361      rc = fts3DeleteAll(p);
125362      *pnDoc = *pnDoc - 1;
125363    }else{
125364      sqlite3_int64 iRemove = sqlite3_value_int64(pRowid);
125365      rc = fts3PendingTermsDocid(p, iRemove);
125366      fts3DeleteTerms(&rc, p, pRowid, aSzDel);
125367      fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
125368      if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
125369      if( p->bHasDocsize ){
125370        fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
125371      }
125372    }
125373  }
125374
125375  return rc;
125376}
125377
125378/*
125379** This function does the work for the xUpdate method of FTS3 virtual
125380** tables.
125381*/
125382SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
125383  sqlite3_vtab *pVtab,            /* FTS3 vtab object */
125384  int nArg,                       /* Size of argument array */
125385  sqlite3_value **apVal,          /* Array of arguments */
125386  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
125387){
125388  Fts3Table *p = (Fts3Table *)pVtab;
125389  int rc = SQLITE_OK;             /* Return Code */
125390  int isRemove = 0;               /* True for an UPDATE or DELETE */
125391  sqlite3_int64 iRemove = 0;      /* Rowid removed by UPDATE or DELETE */
125392  u32 *aSzIns = 0;                /* Sizes of inserted documents */
125393  u32 *aSzDel;                    /* Sizes of deleted documents */
125394  int nChng = 0;                  /* Net change in number of documents */
125395  int bInsertDone = 0;
125396
125397  assert( p->pSegments==0 );
125398
125399  /* Check for a "special" INSERT operation. One of the form:
125400  **
125401  **   INSERT INTO xyz(xyz) VALUES('command');
125402  */
125403  if( nArg>1
125404   && sqlite3_value_type(apVal[0])==SQLITE_NULL
125405   && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
125406  ){
125407    rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
125408    goto update_out;
125409  }
125410
125411  /* Allocate space to hold the change in document sizes */
125412  aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
125413  if( aSzIns==0 ){
125414    rc = SQLITE_NOMEM;
125415    goto update_out;
125416  }
125417  aSzDel = &aSzIns[p->nColumn+1];
125418  memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
125419
125420  /* If this is an INSERT operation, or an UPDATE that modifies the rowid
125421  ** value, then this operation requires constraint handling.
125422  **
125423  ** If the on-conflict mode is REPLACE, this means that the existing row
125424  ** should be deleted from the database before inserting the new row. Or,
125425  ** if the on-conflict mode is other than REPLACE, then this method must
125426  ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
125427  ** modify the database file.
125428  */
125429  if( nArg>1 ){
125430    /* Find the value object that holds the new rowid value. */
125431    sqlite3_value *pNewRowid = apVal[3+p->nColumn];
125432    if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
125433      pNewRowid = apVal[1];
125434    }
125435
125436    if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
125437        sqlite3_value_type(apVal[0])==SQLITE_NULL
125438     || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
125439    )){
125440      /* The new rowid is not NULL (in this case the rowid will be
125441      ** automatically assigned and there is no chance of a conflict), and
125442      ** the statement is either an INSERT or an UPDATE that modifies the
125443      ** rowid column. So if the conflict mode is REPLACE, then delete any
125444      ** existing row with rowid=pNewRowid.
125445      **
125446      ** Or, if the conflict mode is not REPLACE, insert the new record into
125447      ** the %_content table. If we hit the duplicate rowid constraint (or any
125448      ** other error) while doing so, return immediately.
125449      **
125450      ** This branch may also run if pNewRowid contains a value that cannot
125451      ** be losslessly converted to an integer. In this case, the eventual
125452      ** call to fts3InsertData() (either just below or further on in this
125453      ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
125454      ** invoked, it will delete zero rows (since no row will have
125455      ** docid=$pNewRowid if $pNewRowid is not an integer value).
125456      */
125457      if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
125458        rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
125459      }else{
125460        rc = fts3InsertData(p, apVal, pRowid);
125461        bInsertDone = 1;
125462      }
125463    }
125464  }
125465  if( rc!=SQLITE_OK ){
125466    goto update_out;
125467  }
125468
125469  /* If this is a DELETE or UPDATE operation, remove the old record. */
125470  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
125471    assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
125472    rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
125473    isRemove = 1;
125474    iRemove = sqlite3_value_int64(apVal[0]);
125475  }
125476
125477  /* If this is an INSERT or UPDATE operation, insert the new record. */
125478  if( nArg>1 && rc==SQLITE_OK ){
125479    if( bInsertDone==0 ){
125480      rc = fts3InsertData(p, apVal, pRowid);
125481      if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB;
125482    }
125483    if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
125484      rc = fts3PendingTermsDocid(p, *pRowid);
125485    }
125486    if( rc==SQLITE_OK ){
125487      rc = fts3InsertTerms(p, apVal, aSzIns);
125488    }
125489    if( p->bHasDocsize ){
125490      fts3InsertDocsize(&rc, p, aSzIns);
125491    }
125492    nChng++;
125493  }
125494
125495  if( p->bHasStat ){
125496    fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
125497  }
125498
125499 update_out:
125500  sqlite3_free(aSzIns);
125501  sqlite3Fts3SegmentsClose(p);
125502  return rc;
125503}
125504
125505/*
125506** Flush any data in the pending-terms hash table to disk. If successful,
125507** merge all segments in the database (including the new segment, if
125508** there was any data to flush) into a single segment.
125509*/
125510SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
125511  int rc;
125512  rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
125513  if( rc==SQLITE_OK ){
125514    rc = fts3DoOptimize(p, 1);
125515    if( rc==SQLITE_OK || rc==SQLITE_DONE ){
125516      int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
125517      if( rc2!=SQLITE_OK ) rc = rc2;
125518    }else{
125519      sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
125520      sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
125521    }
125522  }
125523  sqlite3Fts3SegmentsClose(p);
125524  return rc;
125525}
125526
125527#endif
125528
125529/************** End of fts3_write.c ******************************************/
125530/************** Begin file fts3_snippet.c ************************************/
125531/*
125532** 2009 Oct 23
125533**
125534** The author disclaims copyright to this source code.  In place of
125535** a legal notice, here is a blessing:
125536**
125537**    May you do good and not evil.
125538**    May you find forgiveness for yourself and forgive others.
125539**    May you share freely, never taking more than you give.
125540**
125541******************************************************************************
125542*/
125543
125544#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125545
125546/* #include <string.h> */
125547/* #include <assert.h> */
125548
125549/*
125550** Characters that may appear in the second argument to matchinfo().
125551*/
125552#define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
125553#define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
125554#define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
125555#define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
125556#define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
125557#define FTS3_MATCHINFO_LCS       's'        /* nCol values */
125558#define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
125559
125560/*
125561** The default value for the second argument to matchinfo().
125562*/
125563#define FTS3_MATCHINFO_DEFAULT   "pcx"
125564
125565
125566/*
125567** Used as an fts3ExprIterate() context when loading phrase doclists to
125568** Fts3Expr.aDoclist[]/nDoclist.
125569*/
125570typedef struct LoadDoclistCtx LoadDoclistCtx;
125571struct LoadDoclistCtx {
125572  Fts3Cursor *pCsr;               /* FTS3 Cursor */
125573  int nPhrase;                    /* Number of phrases seen so far */
125574  int nToken;                     /* Number of tokens seen so far */
125575};
125576
125577/*
125578** The following types are used as part of the implementation of the
125579** fts3BestSnippet() routine.
125580*/
125581typedef struct SnippetIter SnippetIter;
125582typedef struct SnippetPhrase SnippetPhrase;
125583typedef struct SnippetFragment SnippetFragment;
125584
125585struct SnippetIter {
125586  Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
125587  int iCol;                       /* Extract snippet from this column */
125588  int nSnippet;                   /* Requested snippet length (in tokens) */
125589  int nPhrase;                    /* Number of phrases in query */
125590  SnippetPhrase *aPhrase;         /* Array of size nPhrase */
125591  int iCurrent;                   /* First token of current snippet */
125592};
125593
125594struct SnippetPhrase {
125595  int nToken;                     /* Number of tokens in phrase */
125596  char *pList;                    /* Pointer to start of phrase position list */
125597  int iHead;                      /* Next value in position list */
125598  char *pHead;                    /* Position list data following iHead */
125599  int iTail;                      /* Next value in trailing position list */
125600  char *pTail;                    /* Position list data following iTail */
125601};
125602
125603struct SnippetFragment {
125604  int iCol;                       /* Column snippet is extracted from */
125605  int iPos;                       /* Index of first token in snippet */
125606  u64 covered;                    /* Mask of query phrases covered */
125607  u64 hlmask;                     /* Mask of snippet terms to highlight */
125608};
125609
125610/*
125611** This type is used as an fts3ExprIterate() context object while
125612** accumulating the data returned by the matchinfo() function.
125613*/
125614typedef struct MatchInfo MatchInfo;
125615struct MatchInfo {
125616  Fts3Cursor *pCursor;            /* FTS3 Cursor */
125617  int nCol;                       /* Number of columns in table */
125618  int nPhrase;                    /* Number of matchable phrases in query */
125619  sqlite3_int64 nDoc;             /* Number of docs in database */
125620  u32 *aMatchinfo;                /* Pre-allocated buffer */
125621};
125622
125623
125624
125625/*
125626** The snippet() and offsets() functions both return text values. An instance
125627** of the following structure is used to accumulate those values while the
125628** functions are running. See fts3StringAppend() for details.
125629*/
125630typedef struct StrBuffer StrBuffer;
125631struct StrBuffer {
125632  char *z;                        /* Pointer to buffer containing string */
125633  int n;                          /* Length of z in bytes (excl. nul-term) */
125634  int nAlloc;                     /* Allocated size of buffer z in bytes */
125635};
125636
125637
125638/*
125639** This function is used to help iterate through a position-list. A position
125640** list is a list of unique integers, sorted from smallest to largest. Each
125641** element of the list is represented by an FTS3 varint that takes the value
125642** of the difference between the current element and the previous one plus
125643** two. For example, to store the position-list:
125644**
125645**     4 9 113
125646**
125647** the three varints:
125648**
125649**     6 7 106
125650**
125651** are encoded.
125652**
125653** When this function is called, *pp points to the start of an element of
125654** the list. *piPos contains the value of the previous entry in the list.
125655** After it returns, *piPos contains the value of the next element of the
125656** list and *pp is advanced to the following varint.
125657*/
125658static void fts3GetDeltaPosition(char **pp, int *piPos){
125659  int iVal;
125660  *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
125661  *piPos += (iVal-2);
125662}
125663
125664/*
125665** Helper function for fts3ExprIterate() (see below).
125666*/
125667static int fts3ExprIterate2(
125668  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
125669  int *piPhrase,                  /* Pointer to phrase counter */
125670  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
125671  void *pCtx                      /* Second argument to pass to callback */
125672){
125673  int rc;                         /* Return code */
125674  int eType = pExpr->eType;       /* Type of expression node pExpr */
125675
125676  if( eType!=FTSQUERY_PHRASE ){
125677    assert( pExpr->pLeft && pExpr->pRight );
125678    rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
125679    if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
125680      rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
125681    }
125682  }else{
125683    rc = x(pExpr, *piPhrase, pCtx);
125684    (*piPhrase)++;
125685  }
125686  return rc;
125687}
125688
125689/*
125690** Iterate through all phrase nodes in an FTS3 query, except those that
125691** are part of a sub-tree that is the right-hand-side of a NOT operator.
125692** For each phrase node found, the supplied callback function is invoked.
125693**
125694** If the callback function returns anything other than SQLITE_OK,
125695** the iteration is abandoned and the error code returned immediately.
125696** Otherwise, SQLITE_OK is returned after a callback has been made for
125697** all eligible phrase nodes.
125698*/
125699static int fts3ExprIterate(
125700  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
125701  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
125702  void *pCtx                      /* Second argument to pass to callback */
125703){
125704  int iPhrase = 0;                /* Variable used as the phrase counter */
125705  return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
125706}
125707
125708/*
125709** This is an fts3ExprIterate() callback used while loading the doclists
125710** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
125711** fts3ExprLoadDoclists().
125712*/
125713static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
125714  int rc = SQLITE_OK;
125715  Fts3Phrase *pPhrase = pExpr->pPhrase;
125716  LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
125717
125718  UNUSED_PARAMETER(iPhrase);
125719
125720  p->nPhrase++;
125721  p->nToken += pPhrase->nToken;
125722
125723  return rc;
125724}
125725
125726/*
125727** Load the doclists for each phrase in the query associated with FTS3 cursor
125728** pCsr.
125729**
125730** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
125731** phrases in the expression (all phrases except those directly or
125732** indirectly descended from the right-hand-side of a NOT operator). If
125733** pnToken is not NULL, then it is set to the number of tokens in all
125734** matchable phrases of the expression.
125735*/
125736static int fts3ExprLoadDoclists(
125737  Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
125738  int *pnPhrase,                  /* OUT: Number of phrases in query */
125739  int *pnToken                    /* OUT: Number of tokens in query */
125740){
125741  int rc;                         /* Return Code */
125742  LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
125743  sCtx.pCsr = pCsr;
125744  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
125745  if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
125746  if( pnToken ) *pnToken = sCtx.nToken;
125747  return rc;
125748}
125749
125750static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
125751  (*(int *)ctx)++;
125752  UNUSED_PARAMETER(pExpr);
125753  UNUSED_PARAMETER(iPhrase);
125754  return SQLITE_OK;
125755}
125756static int fts3ExprPhraseCount(Fts3Expr *pExpr){
125757  int nPhrase = 0;
125758  (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
125759  return nPhrase;
125760}
125761
125762/*
125763** Advance the position list iterator specified by the first two
125764** arguments so that it points to the first element with a value greater
125765** than or equal to parameter iNext.
125766*/
125767static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
125768  char *pIter = *ppIter;
125769  if( pIter ){
125770    int iIter = *piIter;
125771
125772    while( iIter<iNext ){
125773      if( 0==(*pIter & 0xFE) ){
125774        iIter = -1;
125775        pIter = 0;
125776        break;
125777      }
125778      fts3GetDeltaPosition(&pIter, &iIter);
125779    }
125780
125781    *piIter = iIter;
125782    *ppIter = pIter;
125783  }
125784}
125785
125786/*
125787** Advance the snippet iterator to the next candidate snippet.
125788*/
125789static int fts3SnippetNextCandidate(SnippetIter *pIter){
125790  int i;                          /* Loop counter */
125791
125792  if( pIter->iCurrent<0 ){
125793    /* The SnippetIter object has just been initialized. The first snippet
125794    ** candidate always starts at offset 0 (even if this candidate has a
125795    ** score of 0.0).
125796    */
125797    pIter->iCurrent = 0;
125798
125799    /* Advance the 'head' iterator of each phrase to the first offset that
125800    ** is greater than or equal to (iNext+nSnippet).
125801    */
125802    for(i=0; i<pIter->nPhrase; i++){
125803      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
125804      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
125805    }
125806  }else{
125807    int iStart;
125808    int iEnd = 0x7FFFFFFF;
125809
125810    for(i=0; i<pIter->nPhrase; i++){
125811      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
125812      if( pPhrase->pHead && pPhrase->iHead<iEnd ){
125813        iEnd = pPhrase->iHead;
125814      }
125815    }
125816    if( iEnd==0x7FFFFFFF ){
125817      return 1;
125818    }
125819
125820    pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
125821    for(i=0; i<pIter->nPhrase; i++){
125822      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
125823      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
125824      fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
125825    }
125826  }
125827
125828  return 0;
125829}
125830
125831/*
125832** Retrieve information about the current candidate snippet of snippet
125833** iterator pIter.
125834*/
125835static void fts3SnippetDetails(
125836  SnippetIter *pIter,             /* Snippet iterator */
125837  u64 mCovered,                   /* Bitmask of phrases already covered */
125838  int *piToken,                   /* OUT: First token of proposed snippet */
125839  int *piScore,                   /* OUT: "Score" for this snippet */
125840  u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
125841  u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
125842){
125843  int iStart = pIter->iCurrent;   /* First token of snippet */
125844  int iScore = 0;                 /* Score of this snippet */
125845  int i;                          /* Loop counter */
125846  u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
125847  u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
125848
125849  for(i=0; i<pIter->nPhrase; i++){
125850    SnippetPhrase *pPhrase = &pIter->aPhrase[i];
125851    if( pPhrase->pTail ){
125852      char *pCsr = pPhrase->pTail;
125853      int iCsr = pPhrase->iTail;
125854
125855      while( iCsr<(iStart+pIter->nSnippet) ){
125856        int j;
125857        u64 mPhrase = (u64)1 << i;
125858        u64 mPos = (u64)1 << (iCsr - iStart);
125859        assert( iCsr>=iStart );
125860        if( (mCover|mCovered)&mPhrase ){
125861          iScore++;
125862        }else{
125863          iScore += 1000;
125864        }
125865        mCover |= mPhrase;
125866
125867        for(j=0; j<pPhrase->nToken; j++){
125868          mHighlight |= (mPos>>j);
125869        }
125870
125871        if( 0==(*pCsr & 0x0FE) ) break;
125872        fts3GetDeltaPosition(&pCsr, &iCsr);
125873      }
125874    }
125875  }
125876
125877  /* Set the output variables before returning. */
125878  *piToken = iStart;
125879  *piScore = iScore;
125880  *pmCover = mCover;
125881  *pmHighlight = mHighlight;
125882}
125883
125884/*
125885** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
125886** Each invocation populates an element of the SnippetIter.aPhrase[] array.
125887*/
125888static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
125889  SnippetIter *p = (SnippetIter *)ctx;
125890  SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
125891  char *pCsr;
125892
125893  pPhrase->nToken = pExpr->pPhrase->nToken;
125894
125895  pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
125896  if( pCsr ){
125897    int iFirst = 0;
125898    pPhrase->pList = pCsr;
125899    fts3GetDeltaPosition(&pCsr, &iFirst);
125900    pPhrase->pHead = pCsr;
125901    pPhrase->pTail = pCsr;
125902    pPhrase->iHead = iFirst;
125903    pPhrase->iTail = iFirst;
125904  }else{
125905    assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
125906  }
125907
125908  return SQLITE_OK;
125909}
125910
125911/*
125912** Select the fragment of text consisting of nFragment contiguous tokens
125913** from column iCol that represent the "best" snippet. The best snippet
125914** is the snippet with the highest score, where scores are calculated
125915** by adding:
125916**
125917**   (a) +1 point for each occurence of a matchable phrase in the snippet.
125918**
125919**   (b) +1000 points for the first occurence of each matchable phrase in
125920**       the snippet for which the corresponding mCovered bit is not set.
125921**
125922** The selected snippet parameters are stored in structure *pFragment before
125923** returning. The score of the selected snippet is stored in *piScore
125924** before returning.
125925*/
125926static int fts3BestSnippet(
125927  int nSnippet,                   /* Desired snippet length */
125928  Fts3Cursor *pCsr,               /* Cursor to create snippet for */
125929  int iCol,                       /* Index of column to create snippet from */
125930  u64 mCovered,                   /* Mask of phrases already covered */
125931  u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
125932  SnippetFragment *pFragment,     /* OUT: Best snippet found */
125933  int *piScore                    /* OUT: Score of snippet pFragment */
125934){
125935  int rc;                         /* Return Code */
125936  int nList;                      /* Number of phrases in expression */
125937  SnippetIter sIter;              /* Iterates through snippet candidates */
125938  int nByte;                      /* Number of bytes of space to allocate */
125939  int iBestScore = -1;            /* Best snippet score found so far */
125940  int i;                          /* Loop counter */
125941
125942  memset(&sIter, 0, sizeof(sIter));
125943
125944  /* Iterate through the phrases in the expression to count them. The same
125945  ** callback makes sure the doclists are loaded for each phrase.
125946  */
125947  rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
125948  if( rc!=SQLITE_OK ){
125949    return rc;
125950  }
125951
125952  /* Now that it is known how many phrases there are, allocate and zero
125953  ** the required space using malloc().
125954  */
125955  nByte = sizeof(SnippetPhrase) * nList;
125956  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
125957  if( !sIter.aPhrase ){
125958    return SQLITE_NOMEM;
125959  }
125960  memset(sIter.aPhrase, 0, nByte);
125961
125962  /* Initialize the contents of the SnippetIter object. Then iterate through
125963  ** the set of phrases in the expression to populate the aPhrase[] array.
125964  */
125965  sIter.pCsr = pCsr;
125966  sIter.iCol = iCol;
125967  sIter.nSnippet = nSnippet;
125968  sIter.nPhrase = nList;
125969  sIter.iCurrent = -1;
125970  (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
125971
125972  /* Set the *pmSeen output variable. */
125973  for(i=0; i<nList; i++){
125974    if( sIter.aPhrase[i].pHead ){
125975      *pmSeen |= (u64)1 << i;
125976    }
125977  }
125978
125979  /* Loop through all candidate snippets. Store the best snippet in
125980  ** *pFragment. Store its associated 'score' in iBestScore.
125981  */
125982  pFragment->iCol = iCol;
125983  while( !fts3SnippetNextCandidate(&sIter) ){
125984    int iPos;
125985    int iScore;
125986    u64 mCover;
125987    u64 mHighlight;
125988    fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
125989    assert( iScore>=0 );
125990    if( iScore>iBestScore ){
125991      pFragment->iPos = iPos;
125992      pFragment->hlmask = mHighlight;
125993      pFragment->covered = mCover;
125994      iBestScore = iScore;
125995    }
125996  }
125997
125998  sqlite3_free(sIter.aPhrase);
125999  *piScore = iBestScore;
126000  return SQLITE_OK;
126001}
126002
126003
126004/*
126005** Append a string to the string-buffer passed as the first argument.
126006**
126007** If nAppend is negative, then the length of the string zAppend is
126008** determined using strlen().
126009*/
126010static int fts3StringAppend(
126011  StrBuffer *pStr,                /* Buffer to append to */
126012  const char *zAppend,            /* Pointer to data to append to buffer */
126013  int nAppend                     /* Size of zAppend in bytes (or -1) */
126014){
126015  if( nAppend<0 ){
126016    nAppend = (int)strlen(zAppend);
126017  }
126018
126019  /* If there is insufficient space allocated at StrBuffer.z, use realloc()
126020  ** to grow the buffer until so that it is big enough to accomadate the
126021  ** appended data.
126022  */
126023  if( pStr->n+nAppend+1>=pStr->nAlloc ){
126024    int nAlloc = pStr->nAlloc+nAppend+100;
126025    char *zNew = sqlite3_realloc(pStr->z, nAlloc);
126026    if( !zNew ){
126027      return SQLITE_NOMEM;
126028    }
126029    pStr->z = zNew;
126030    pStr->nAlloc = nAlloc;
126031  }
126032
126033  /* Append the data to the string buffer. */
126034  memcpy(&pStr->z[pStr->n], zAppend, nAppend);
126035  pStr->n += nAppend;
126036  pStr->z[pStr->n] = '\0';
126037
126038  return SQLITE_OK;
126039}
126040
126041/*
126042** The fts3BestSnippet() function often selects snippets that end with a
126043** query term. That is, the final term of the snippet is always a term
126044** that requires highlighting. For example, if 'X' is a highlighted term
126045** and '.' is a non-highlighted term, BestSnippet() may select:
126046**
126047**     ........X.....X
126048**
126049** This function "shifts" the beginning of the snippet forward in the
126050** document so that there are approximately the same number of
126051** non-highlighted terms to the right of the final highlighted term as there
126052** are to the left of the first highlighted term. For example, to this:
126053**
126054**     ....X.....X....
126055**
126056** This is done as part of extracting the snippet text, not when selecting
126057** the snippet. Snippet selection is done based on doclists only, so there
126058** is no way for fts3BestSnippet() to know whether or not the document
126059** actually contains terms that follow the final highlighted term.
126060*/
126061static int fts3SnippetShift(
126062  Fts3Table *pTab,                /* FTS3 table snippet comes from */
126063  int nSnippet,                   /* Number of tokens desired for snippet */
126064  const char *zDoc,               /* Document text to extract snippet from */
126065  int nDoc,                       /* Size of buffer zDoc in bytes */
126066  int *piPos,                     /* IN/OUT: First token of snippet */
126067  u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
126068){
126069  u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
126070
126071  if( hlmask ){
126072    int nLeft;                    /* Tokens to the left of first highlight */
126073    int nRight;                   /* Tokens to the right of last highlight */
126074    int nDesired;                 /* Ideal number of tokens to shift forward */
126075
126076    for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
126077    for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
126078    nDesired = (nLeft-nRight)/2;
126079
126080    /* Ideally, the start of the snippet should be pushed forward in the
126081    ** document nDesired tokens. This block checks if there are actually
126082    ** nDesired tokens to the right of the snippet. If so, *piPos and
126083    ** *pHlMask are updated to shift the snippet nDesired tokens to the
126084    ** right. Otherwise, the snippet is shifted by the number of tokens
126085    ** available.
126086    */
126087    if( nDesired>0 ){
126088      int nShift;                 /* Number of tokens to shift snippet by */
126089      int iCurrent = 0;           /* Token counter */
126090      int rc;                     /* Return Code */
126091      sqlite3_tokenizer_module *pMod;
126092      sqlite3_tokenizer_cursor *pC;
126093      pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
126094
126095      /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
126096      ** or more tokens in zDoc/nDoc.
126097      */
126098      rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
126099      if( rc!=SQLITE_OK ){
126100        return rc;
126101      }
126102      pC->pTokenizer = pTab->pTokenizer;
126103      while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
126104        const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
126105        rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
126106      }
126107      pMod->xClose(pC);
126108      if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
126109
126110      nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
126111      assert( nShift<=nDesired );
126112      if( nShift>0 ){
126113        *piPos += nShift;
126114        *pHlmask = hlmask >> nShift;
126115      }
126116    }
126117  }
126118  return SQLITE_OK;
126119}
126120
126121/*
126122** Extract the snippet text for fragment pFragment from cursor pCsr and
126123** append it to string buffer pOut.
126124*/
126125static int fts3SnippetText(
126126  Fts3Cursor *pCsr,               /* FTS3 Cursor */
126127  SnippetFragment *pFragment,     /* Snippet to extract */
126128  int iFragment,                  /* Fragment number */
126129  int isLast,                     /* True for final fragment in snippet */
126130  int nSnippet,                   /* Number of tokens in extracted snippet */
126131  const char *zOpen,              /* String inserted before highlighted term */
126132  const char *zClose,             /* String inserted after highlighted term */
126133  const char *zEllipsis,          /* String inserted between snippets */
126134  StrBuffer *pOut                 /* Write output here */
126135){
126136  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126137  int rc;                         /* Return code */
126138  const char *zDoc;               /* Document text to extract snippet from */
126139  int nDoc;                       /* Size of zDoc in bytes */
126140  int iCurrent = 0;               /* Current token number of document */
126141  int iEnd = 0;                   /* Byte offset of end of current token */
126142  int isShiftDone = 0;            /* True after snippet is shifted */
126143  int iPos = pFragment->iPos;     /* First token of snippet */
126144  u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
126145  int iCol = pFragment->iCol+1;   /* Query column to extract text from */
126146  sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
126147  sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
126148  const char *ZDUMMY;             /* Dummy argument used with tokenizer */
126149  int DUMMY1;                     /* Dummy argument used with tokenizer */
126150
126151  zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
126152  if( zDoc==0 ){
126153    if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
126154      return SQLITE_NOMEM;
126155    }
126156    return SQLITE_OK;
126157  }
126158  nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
126159
126160  /* Open a token cursor on the document. */
126161  pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
126162  rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
126163  if( rc!=SQLITE_OK ){
126164    return rc;
126165  }
126166  pC->pTokenizer = pTab->pTokenizer;
126167
126168  while( rc==SQLITE_OK ){
126169    int iBegin;                   /* Offset in zDoc of start of token */
126170    int iFin;                     /* Offset in zDoc of end of token */
126171    int isHighlight;              /* True for highlighted terms */
126172
126173    rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
126174    if( rc!=SQLITE_OK ){
126175      if( rc==SQLITE_DONE ){
126176        /* Special case - the last token of the snippet is also the last token
126177        ** of the column. Append any punctuation that occurred between the end
126178        ** of the previous token and the end of the document to the output.
126179        ** Then break out of the loop. */
126180        rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
126181      }
126182      break;
126183    }
126184    if( iCurrent<iPos ){ continue; }
126185
126186    if( !isShiftDone ){
126187      int n = nDoc - iBegin;
126188      rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask);
126189      isShiftDone = 1;
126190
126191      /* Now that the shift has been done, check if the initial "..." are
126192      ** required. They are required if (a) this is not the first fragment,
126193      ** or (b) this fragment does not begin at position 0 of its column.
126194      */
126195      if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
126196        rc = fts3StringAppend(pOut, zEllipsis, -1);
126197      }
126198      if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
126199    }
126200
126201    if( iCurrent>=(iPos+nSnippet) ){
126202      if( isLast ){
126203        rc = fts3StringAppend(pOut, zEllipsis, -1);
126204      }
126205      break;
126206    }
126207
126208    /* Set isHighlight to true if this term should be highlighted. */
126209    isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
126210
126211    if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
126212    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
126213    if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
126214    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
126215
126216    iEnd = iFin;
126217  }
126218
126219  pMod->xClose(pC);
126220  return rc;
126221}
126222
126223
126224/*
126225** This function is used to count the entries in a column-list (a
126226** delta-encoded list of term offsets within a single column of a single
126227** row). When this function is called, *ppCollist should point to the
126228** beginning of the first varint in the column-list (the varint that
126229** contains the position of the first matching term in the column data).
126230** Before returning, *ppCollist is set to point to the first byte after
126231** the last varint in the column-list (either the 0x00 signifying the end
126232** of the position-list, or the 0x01 that precedes the column number of
126233** the next column in the position-list).
126234**
126235** The number of elements in the column-list is returned.
126236*/
126237static int fts3ColumnlistCount(char **ppCollist){
126238  char *pEnd = *ppCollist;
126239  char c = 0;
126240  int nEntry = 0;
126241
126242  /* A column-list is terminated by either a 0x01 or 0x00. */
126243  while( 0xFE & (*pEnd | c) ){
126244    c = *pEnd++ & 0x80;
126245    if( !c ) nEntry++;
126246  }
126247
126248  *ppCollist = pEnd;
126249  return nEntry;
126250}
126251
126252/*
126253** fts3ExprIterate() callback used to collect the "global" matchinfo stats
126254** for a single query.
126255**
126256** fts3ExprIterate() callback to load the 'global' elements of a
126257** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
126258** of the matchinfo array that are constant for all rows returned by the
126259** current query.
126260**
126261** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
126262** function populates Matchinfo.aMatchinfo[] as follows:
126263**
126264**   for(iCol=0; iCol<nCol; iCol++){
126265**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
126266**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
126267**   }
126268**
126269** where X is the number of matches for phrase iPhrase is column iCol of all
126270** rows of the table. Y is the number of rows for which column iCol contains
126271** at least one instance of phrase iPhrase.
126272**
126273** If the phrase pExpr consists entirely of deferred tokens, then all X and
126274** Y values are set to nDoc, where nDoc is the number of documents in the
126275** file system. This is done because the full-text index doclist is required
126276** to calculate these values properly, and the full-text index doclist is
126277** not available for deferred tokens.
126278*/
126279static int fts3ExprGlobalHitsCb(
126280  Fts3Expr *pExpr,                /* Phrase expression node */
126281  int iPhrase,                    /* Phrase number (numbered from zero) */
126282  void *pCtx                      /* Pointer to MatchInfo structure */
126283){
126284  MatchInfo *p = (MatchInfo *)pCtx;
126285  return sqlite3Fts3EvalPhraseStats(
126286      p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
126287  );
126288}
126289
126290/*
126291** fts3ExprIterate() callback used to collect the "local" part of the
126292** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
126293** array that are different for each row returned by the query.
126294*/
126295static int fts3ExprLocalHitsCb(
126296  Fts3Expr *pExpr,                /* Phrase expression node */
126297  int iPhrase,                    /* Phrase number */
126298  void *pCtx                      /* Pointer to MatchInfo structure */
126299){
126300  MatchInfo *p = (MatchInfo *)pCtx;
126301  int iStart = iPhrase * p->nCol * 3;
126302  int i;
126303
126304  for(i=0; i<p->nCol; i++){
126305    char *pCsr;
126306    pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
126307    if( pCsr ){
126308      p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
126309    }else{
126310      p->aMatchinfo[iStart+i*3] = 0;
126311    }
126312  }
126313
126314  return SQLITE_OK;
126315}
126316
126317static int fts3MatchinfoCheck(
126318  Fts3Table *pTab,
126319  char cArg,
126320  char **pzErr
126321){
126322  if( (cArg==FTS3_MATCHINFO_NPHRASE)
126323   || (cArg==FTS3_MATCHINFO_NCOL)
126324   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
126325   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
126326   || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
126327   || (cArg==FTS3_MATCHINFO_LCS)
126328   || (cArg==FTS3_MATCHINFO_HITS)
126329  ){
126330    return SQLITE_OK;
126331  }
126332  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
126333  return SQLITE_ERROR;
126334}
126335
126336static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
126337  int nVal;                       /* Number of integers output by cArg */
126338
126339  switch( cArg ){
126340    case FTS3_MATCHINFO_NDOC:
126341    case FTS3_MATCHINFO_NPHRASE:
126342    case FTS3_MATCHINFO_NCOL:
126343      nVal = 1;
126344      break;
126345
126346    case FTS3_MATCHINFO_AVGLENGTH:
126347    case FTS3_MATCHINFO_LENGTH:
126348    case FTS3_MATCHINFO_LCS:
126349      nVal = pInfo->nCol;
126350      break;
126351
126352    default:
126353      assert( cArg==FTS3_MATCHINFO_HITS );
126354      nVal = pInfo->nCol * pInfo->nPhrase * 3;
126355      break;
126356  }
126357
126358  return nVal;
126359}
126360
126361static int fts3MatchinfoSelectDoctotal(
126362  Fts3Table *pTab,
126363  sqlite3_stmt **ppStmt,
126364  sqlite3_int64 *pnDoc,
126365  const char **paLen
126366){
126367  sqlite3_stmt *pStmt;
126368  const char *a;
126369  sqlite3_int64 nDoc;
126370
126371  if( !*ppStmt ){
126372    int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
126373    if( rc!=SQLITE_OK ) return rc;
126374  }
126375  pStmt = *ppStmt;
126376  assert( sqlite3_data_count(pStmt)==1 );
126377
126378  a = sqlite3_column_blob(pStmt, 0);
126379  a += sqlite3Fts3GetVarint(a, &nDoc);
126380  if( nDoc==0 ) return SQLITE_CORRUPT_VTAB;
126381  *pnDoc = (u32)nDoc;
126382
126383  if( paLen ) *paLen = a;
126384  return SQLITE_OK;
126385}
126386
126387/*
126388** An instance of the following structure is used to store state while
126389** iterating through a multi-column position-list corresponding to the
126390** hits for a single phrase on a single row in order to calculate the
126391** values for a matchinfo() FTS3_MATCHINFO_LCS request.
126392*/
126393typedef struct LcsIterator LcsIterator;
126394struct LcsIterator {
126395  Fts3Expr *pExpr;                /* Pointer to phrase expression */
126396  int iPosOffset;                 /* Tokens count up to end of this phrase */
126397  char *pRead;                    /* Cursor used to iterate through aDoclist */
126398  int iPos;                       /* Current position */
126399};
126400
126401/*
126402** If LcsIterator.iCol is set to the following value, the iterator has
126403** finished iterating through all offsets for all columns.
126404*/
126405#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
126406
126407static int fts3MatchinfoLcsCb(
126408  Fts3Expr *pExpr,                /* Phrase expression node */
126409  int iPhrase,                    /* Phrase number (numbered from zero) */
126410  void *pCtx                      /* Pointer to MatchInfo structure */
126411){
126412  LcsIterator *aIter = (LcsIterator *)pCtx;
126413  aIter[iPhrase].pExpr = pExpr;
126414  return SQLITE_OK;
126415}
126416
126417/*
126418** Advance the iterator passed as an argument to the next position. Return
126419** 1 if the iterator is at EOF or if it now points to the start of the
126420** position list for the next column.
126421*/
126422static int fts3LcsIteratorAdvance(LcsIterator *pIter){
126423  char *pRead = pIter->pRead;
126424  sqlite3_int64 iRead;
126425  int rc = 0;
126426
126427  pRead += sqlite3Fts3GetVarint(pRead, &iRead);
126428  if( iRead==0 || iRead==1 ){
126429    pRead = 0;
126430    rc = 1;
126431  }else{
126432    pIter->iPos += (int)(iRead-2);
126433  }
126434
126435  pIter->pRead = pRead;
126436  return rc;
126437}
126438
126439/*
126440** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
126441**
126442** If the call is successful, the longest-common-substring lengths for each
126443** column are written into the first nCol elements of the pInfo->aMatchinfo[]
126444** array before returning. SQLITE_OK is returned in this case.
126445**
126446** Otherwise, if an error occurs, an SQLite error code is returned and the
126447** data written to the first nCol elements of pInfo->aMatchinfo[] is
126448** undefined.
126449*/
126450static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
126451  LcsIterator *aIter;
126452  int i;
126453  int iCol;
126454  int nToken = 0;
126455
126456  /* Allocate and populate the array of LcsIterator objects. The array
126457  ** contains one element for each matchable phrase in the query.
126458  **/
126459  aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
126460  if( !aIter ) return SQLITE_NOMEM;
126461  memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
126462  (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
126463
126464  for(i=0; i<pInfo->nPhrase; i++){
126465    LcsIterator *pIter = &aIter[i];
126466    nToken -= pIter->pExpr->pPhrase->nToken;
126467    pIter->iPosOffset = nToken;
126468  }
126469
126470  for(iCol=0; iCol<pInfo->nCol; iCol++){
126471    int nLcs = 0;                 /* LCS value for this column */
126472    int nLive = 0;                /* Number of iterators in aIter not at EOF */
126473
126474    for(i=0; i<pInfo->nPhrase; i++){
126475      LcsIterator *pIt = &aIter[i];
126476      pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
126477      if( pIt->pRead ){
126478        pIt->iPos = pIt->iPosOffset;
126479        fts3LcsIteratorAdvance(&aIter[i]);
126480        nLive++;
126481      }
126482    }
126483
126484    while( nLive>0 ){
126485      LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
126486      int nThisLcs = 0;           /* LCS for the current iterator positions */
126487
126488      for(i=0; i<pInfo->nPhrase; i++){
126489        LcsIterator *pIter = &aIter[i];
126490        if( pIter->pRead==0 ){
126491          /* This iterator is already at EOF for this column. */
126492          nThisLcs = 0;
126493        }else{
126494          if( pAdv==0 || pIter->iPos<pAdv->iPos ){
126495            pAdv = pIter;
126496          }
126497          if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
126498            nThisLcs++;
126499          }else{
126500            nThisLcs = 1;
126501          }
126502          if( nThisLcs>nLcs ) nLcs = nThisLcs;
126503        }
126504      }
126505      if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
126506    }
126507
126508    pInfo->aMatchinfo[iCol] = nLcs;
126509  }
126510
126511  sqlite3_free(aIter);
126512  return SQLITE_OK;
126513}
126514
126515/*
126516** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
126517** be returned by the matchinfo() function. Argument zArg contains the
126518** format string passed as the second argument to matchinfo (or the
126519** default value "pcx" if no second argument was specified). The format
126520** string has already been validated and the pInfo->aMatchinfo[] array
126521** is guaranteed to be large enough for the output.
126522**
126523** If bGlobal is true, then populate all fields of the matchinfo() output.
126524** If it is false, then assume that those fields that do not change between
126525** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
126526** have already been populated.
126527**
126528** Return SQLITE_OK if successful, or an SQLite error code if an error
126529** occurs. If a value other than SQLITE_OK is returned, the state the
126530** pInfo->aMatchinfo[] buffer is left in is undefined.
126531*/
126532static int fts3MatchinfoValues(
126533  Fts3Cursor *pCsr,               /* FTS3 cursor object */
126534  int bGlobal,                    /* True to grab the global stats */
126535  MatchInfo *pInfo,               /* Matchinfo context object */
126536  const char *zArg                /* Matchinfo format string */
126537){
126538  int rc = SQLITE_OK;
126539  int i;
126540  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126541  sqlite3_stmt *pSelect = 0;
126542
126543  for(i=0; rc==SQLITE_OK && zArg[i]; i++){
126544
126545    switch( zArg[i] ){
126546      case FTS3_MATCHINFO_NPHRASE:
126547        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
126548        break;
126549
126550      case FTS3_MATCHINFO_NCOL:
126551        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
126552        break;
126553
126554      case FTS3_MATCHINFO_NDOC:
126555        if( bGlobal ){
126556          sqlite3_int64 nDoc = 0;
126557          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
126558          pInfo->aMatchinfo[0] = (u32)nDoc;
126559        }
126560        break;
126561
126562      case FTS3_MATCHINFO_AVGLENGTH:
126563        if( bGlobal ){
126564          sqlite3_int64 nDoc;     /* Number of rows in table */
126565          const char *a;          /* Aggregate column length array */
126566
126567          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
126568          if( rc==SQLITE_OK ){
126569            int iCol;
126570            for(iCol=0; iCol<pInfo->nCol; iCol++){
126571              u32 iVal;
126572              sqlite3_int64 nToken;
126573              a += sqlite3Fts3GetVarint(a, &nToken);
126574              iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
126575              pInfo->aMatchinfo[iCol] = iVal;
126576            }
126577          }
126578        }
126579        break;
126580
126581      case FTS3_MATCHINFO_LENGTH: {
126582        sqlite3_stmt *pSelectDocsize = 0;
126583        rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
126584        if( rc==SQLITE_OK ){
126585          int iCol;
126586          const char *a = sqlite3_column_blob(pSelectDocsize, 0);
126587          for(iCol=0; iCol<pInfo->nCol; iCol++){
126588            sqlite3_int64 nToken;
126589            a += sqlite3Fts3GetVarint(a, &nToken);
126590            pInfo->aMatchinfo[iCol] = (u32)nToken;
126591          }
126592        }
126593        sqlite3_reset(pSelectDocsize);
126594        break;
126595      }
126596
126597      case FTS3_MATCHINFO_LCS:
126598        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
126599        if( rc==SQLITE_OK ){
126600          rc = fts3MatchinfoLcs(pCsr, pInfo);
126601        }
126602        break;
126603
126604      default: {
126605        Fts3Expr *pExpr;
126606        assert( zArg[i]==FTS3_MATCHINFO_HITS );
126607        pExpr = pCsr->pExpr;
126608        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
126609        if( rc!=SQLITE_OK ) break;
126610        if( bGlobal ){
126611          if( pCsr->pDeferred ){
126612            rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
126613            if( rc!=SQLITE_OK ) break;
126614          }
126615          rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
126616          if( rc!=SQLITE_OK ) break;
126617        }
126618        (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
126619        break;
126620      }
126621    }
126622
126623    pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
126624  }
126625
126626  sqlite3_reset(pSelect);
126627  return rc;
126628}
126629
126630
126631/*
126632** Populate pCsr->aMatchinfo[] with data for the current row. The
126633** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
126634*/
126635static int fts3GetMatchinfo(
126636  Fts3Cursor *pCsr,               /* FTS3 Cursor object */
126637  const char *zArg                /* Second argument to matchinfo() function */
126638){
126639  MatchInfo sInfo;
126640  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126641  int rc = SQLITE_OK;
126642  int bGlobal = 0;                /* Collect 'global' stats as well as local */
126643
126644  memset(&sInfo, 0, sizeof(MatchInfo));
126645  sInfo.pCursor = pCsr;
126646  sInfo.nCol = pTab->nColumn;
126647
126648  /* If there is cached matchinfo() data, but the format string for the
126649  ** cache does not match the format string for this request, discard
126650  ** the cached data. */
126651  if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
126652    assert( pCsr->aMatchinfo );
126653    sqlite3_free(pCsr->aMatchinfo);
126654    pCsr->zMatchinfo = 0;
126655    pCsr->aMatchinfo = 0;
126656  }
126657
126658  /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
126659  ** matchinfo function has been called for this query. In this case
126660  ** allocate the array used to accumulate the matchinfo data and
126661  ** initialize those elements that are constant for every row.
126662  */
126663  if( pCsr->aMatchinfo==0 ){
126664    int nMatchinfo = 0;           /* Number of u32 elements in match-info */
126665    int nArg;                     /* Bytes in zArg */
126666    int i;                        /* Used to iterate through zArg */
126667
126668    /* Determine the number of phrases in the query */
126669    pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
126670    sInfo.nPhrase = pCsr->nPhrase;
126671
126672    /* Determine the number of integers in the buffer returned by this call. */
126673    for(i=0; zArg[i]; i++){
126674      nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
126675    }
126676
126677    /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
126678    nArg = (int)strlen(zArg);
126679    pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
126680    if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
126681
126682    pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
126683    pCsr->nMatchinfo = nMatchinfo;
126684    memcpy(pCsr->zMatchinfo, zArg, nArg+1);
126685    memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
126686    pCsr->isMatchinfoNeeded = 1;
126687    bGlobal = 1;
126688  }
126689
126690  sInfo.aMatchinfo = pCsr->aMatchinfo;
126691  sInfo.nPhrase = pCsr->nPhrase;
126692  if( pCsr->isMatchinfoNeeded ){
126693    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
126694    pCsr->isMatchinfoNeeded = 0;
126695  }
126696
126697  return rc;
126698}
126699
126700/*
126701** Implementation of snippet() function.
126702*/
126703SQLITE_PRIVATE void sqlite3Fts3Snippet(
126704  sqlite3_context *pCtx,          /* SQLite function call context */
126705  Fts3Cursor *pCsr,               /* Cursor object */
126706  const char *zStart,             /* Snippet start text - "<b>" */
126707  const char *zEnd,               /* Snippet end text - "</b>" */
126708  const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
126709  int iCol,                       /* Extract snippet from this column */
126710  int nToken                      /* Approximate number of tokens in snippet */
126711){
126712  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126713  int rc = SQLITE_OK;
126714  int i;
126715  StrBuffer res = {0, 0, 0};
126716
126717  /* The returned text includes up to four fragments of text extracted from
126718  ** the data in the current row. The first iteration of the for(...) loop
126719  ** below attempts to locate a single fragment of text nToken tokens in
126720  ** size that contains at least one instance of all phrases in the query
126721  ** expression that appear in the current row. If such a fragment of text
126722  ** cannot be found, the second iteration of the loop attempts to locate
126723  ** a pair of fragments, and so on.
126724  */
126725  int nSnippet = 0;               /* Number of fragments in this snippet */
126726  SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
126727  int nFToken = -1;               /* Number of tokens in each fragment */
126728
126729  if( !pCsr->pExpr ){
126730    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
126731    return;
126732  }
126733
126734  for(nSnippet=1; 1; nSnippet++){
126735
126736    int iSnip;                    /* Loop counter 0..nSnippet-1 */
126737    u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
126738    u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
126739
126740    if( nToken>=0 ){
126741      nFToken = (nToken+nSnippet-1) / nSnippet;
126742    }else{
126743      nFToken = -1 * nToken;
126744    }
126745
126746    for(iSnip=0; iSnip<nSnippet; iSnip++){
126747      int iBestScore = -1;        /* Best score of columns checked so far */
126748      int iRead;                  /* Used to iterate through columns */
126749      SnippetFragment *pFragment = &aSnippet[iSnip];
126750
126751      memset(pFragment, 0, sizeof(*pFragment));
126752
126753      /* Loop through all columns of the table being considered for snippets.
126754      ** If the iCol argument to this function was negative, this means all
126755      ** columns of the FTS3 table. Otherwise, only column iCol is considered.
126756      */
126757      for(iRead=0; iRead<pTab->nColumn; iRead++){
126758        SnippetFragment sF = {0, 0, 0, 0};
126759        int iS;
126760        if( iCol>=0 && iRead!=iCol ) continue;
126761
126762        /* Find the best snippet of nFToken tokens in column iRead. */
126763        rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
126764        if( rc!=SQLITE_OK ){
126765          goto snippet_out;
126766        }
126767        if( iS>iBestScore ){
126768          *pFragment = sF;
126769          iBestScore = iS;
126770        }
126771      }
126772
126773      mCovered |= pFragment->covered;
126774    }
126775
126776    /* If all query phrases seen by fts3BestSnippet() are present in at least
126777    ** one of the nSnippet snippet fragments, break out of the loop.
126778    */
126779    assert( (mCovered&mSeen)==mCovered );
126780    if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
126781  }
126782
126783  assert( nFToken>0 );
126784
126785  for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
126786    rc = fts3SnippetText(pCsr, &aSnippet[i],
126787        i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
126788    );
126789  }
126790
126791 snippet_out:
126792  sqlite3Fts3SegmentsClose(pTab);
126793  if( rc!=SQLITE_OK ){
126794    sqlite3_result_error_code(pCtx, rc);
126795    sqlite3_free(res.z);
126796  }else{
126797    sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
126798  }
126799}
126800
126801
126802typedef struct TermOffset TermOffset;
126803typedef struct TermOffsetCtx TermOffsetCtx;
126804
126805struct TermOffset {
126806  char *pList;                    /* Position-list */
126807  int iPos;                       /* Position just read from pList */
126808  int iOff;                       /* Offset of this term from read positions */
126809};
126810
126811struct TermOffsetCtx {
126812  Fts3Cursor *pCsr;
126813  int iCol;                       /* Column of table to populate aTerm for */
126814  int iTerm;
126815  sqlite3_int64 iDocid;
126816  TermOffset *aTerm;
126817};
126818
126819/*
126820** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
126821*/
126822static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
126823  TermOffsetCtx *p = (TermOffsetCtx *)ctx;
126824  int nTerm;                      /* Number of tokens in phrase */
126825  int iTerm;                      /* For looping through nTerm phrase terms */
126826  char *pList;                    /* Pointer to position list for phrase */
126827  int iPos = 0;                   /* First position in position-list */
126828
126829  UNUSED_PARAMETER(iPhrase);
126830  pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
126831  nTerm = pExpr->pPhrase->nToken;
126832  if( pList ){
126833    fts3GetDeltaPosition(&pList, &iPos);
126834    assert( iPos>=0 );
126835  }
126836
126837  for(iTerm=0; iTerm<nTerm; iTerm++){
126838    TermOffset *pT = &p->aTerm[p->iTerm++];
126839    pT->iOff = nTerm-iTerm-1;
126840    pT->pList = pList;
126841    pT->iPos = iPos;
126842  }
126843
126844  return SQLITE_OK;
126845}
126846
126847/*
126848** Implementation of offsets() function.
126849*/
126850SQLITE_PRIVATE void sqlite3Fts3Offsets(
126851  sqlite3_context *pCtx,          /* SQLite function call context */
126852  Fts3Cursor *pCsr                /* Cursor object */
126853){
126854  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126855  sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
126856  const char *ZDUMMY;             /* Dummy argument used with xNext() */
126857  int NDUMMY;                     /* Dummy argument used with xNext() */
126858  int rc;                         /* Return Code */
126859  int nToken;                     /* Number of tokens in query */
126860  int iCol;                       /* Column currently being processed */
126861  StrBuffer res = {0, 0, 0};      /* Result string */
126862  TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
126863
126864  if( !pCsr->pExpr ){
126865    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
126866    return;
126867  }
126868
126869  memset(&sCtx, 0, sizeof(sCtx));
126870  assert( pCsr->isRequireSeek==0 );
126871
126872  /* Count the number of terms in the query */
126873  rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
126874  if( rc!=SQLITE_OK ) goto offsets_out;
126875
126876  /* Allocate the array of TermOffset iterators. */
126877  sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
126878  if( 0==sCtx.aTerm ){
126879    rc = SQLITE_NOMEM;
126880    goto offsets_out;
126881  }
126882  sCtx.iDocid = pCsr->iPrevId;
126883  sCtx.pCsr = pCsr;
126884
126885  /* Loop through the table columns, appending offset information to
126886  ** string-buffer res for each column.
126887  */
126888  for(iCol=0; iCol<pTab->nColumn; iCol++){
126889    sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
126890    int iStart;
126891    int iEnd;
126892    int iCurrent;
126893    const char *zDoc;
126894    int nDoc;
126895
126896    /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
126897    ** no way that this operation can fail, so the return code from
126898    ** fts3ExprIterate() can be discarded.
126899    */
126900    sCtx.iCol = iCol;
126901    sCtx.iTerm = 0;
126902    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
126903
126904    /* Retreive the text stored in column iCol. If an SQL NULL is stored
126905    ** in column iCol, jump immediately to the next iteration of the loop.
126906    ** If an OOM occurs while retrieving the data (this can happen if SQLite
126907    ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
126908    ** to the caller.
126909    */
126910    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
126911    nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
126912    if( zDoc==0 ){
126913      if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
126914        continue;
126915      }
126916      rc = SQLITE_NOMEM;
126917      goto offsets_out;
126918    }
126919
126920    /* Initialize a tokenizer iterator to iterate through column iCol. */
126921    rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
126922    if( rc!=SQLITE_OK ) goto offsets_out;
126923    pC->pTokenizer = pTab->pTokenizer;
126924
126925    rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
126926    while( rc==SQLITE_OK ){
126927      int i;                      /* Used to loop through terms */
126928      int iMinPos = 0x7FFFFFFF;   /* Position of next token */
126929      TermOffset *pTerm = 0;      /* TermOffset associated with next token */
126930
126931      for(i=0; i<nToken; i++){
126932        TermOffset *pT = &sCtx.aTerm[i];
126933        if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
126934          iMinPos = pT->iPos-pT->iOff;
126935          pTerm = pT;
126936        }
126937      }
126938
126939      if( !pTerm ){
126940        /* All offsets for this column have been gathered. */
126941        break;
126942      }else{
126943        assert( iCurrent<=iMinPos );
126944        if( 0==(0xFE&*pTerm->pList) ){
126945          pTerm->pList = 0;
126946        }else{
126947          fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
126948        }
126949        while( rc==SQLITE_OK && iCurrent<iMinPos ){
126950          rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
126951        }
126952        if( rc==SQLITE_OK ){
126953          char aBuffer[64];
126954          sqlite3_snprintf(sizeof(aBuffer), aBuffer,
126955              "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
126956          );
126957          rc = fts3StringAppend(&res, aBuffer, -1);
126958        }else if( rc==SQLITE_DONE ){
126959          rc = SQLITE_CORRUPT_VTAB;
126960        }
126961      }
126962    }
126963    if( rc==SQLITE_DONE ){
126964      rc = SQLITE_OK;
126965    }
126966
126967    pMod->xClose(pC);
126968    if( rc!=SQLITE_OK ) goto offsets_out;
126969  }
126970
126971 offsets_out:
126972  sqlite3_free(sCtx.aTerm);
126973  assert( rc!=SQLITE_DONE );
126974  sqlite3Fts3SegmentsClose(pTab);
126975  if( rc!=SQLITE_OK ){
126976    sqlite3_result_error_code(pCtx,  rc);
126977    sqlite3_free(res.z);
126978  }else{
126979    sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
126980  }
126981  return;
126982}
126983
126984/*
126985** Implementation of matchinfo() function.
126986*/
126987SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
126988  sqlite3_context *pContext,      /* Function call context */
126989  Fts3Cursor *pCsr,               /* FTS3 table cursor */
126990  const char *zArg                /* Second arg to matchinfo() function */
126991){
126992  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
126993  int rc;
126994  int i;
126995  const char *zFormat;
126996
126997  if( zArg ){
126998    for(i=0; zArg[i]; i++){
126999      char *zErr = 0;
127000      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
127001        sqlite3_result_error(pContext, zErr, -1);
127002        sqlite3_free(zErr);
127003        return;
127004      }
127005    }
127006    zFormat = zArg;
127007  }else{
127008    zFormat = FTS3_MATCHINFO_DEFAULT;
127009  }
127010
127011  if( !pCsr->pExpr ){
127012    sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
127013    return;
127014  }
127015
127016  /* Retrieve matchinfo() data. */
127017  rc = fts3GetMatchinfo(pCsr, zFormat);
127018  sqlite3Fts3SegmentsClose(pTab);
127019
127020  if( rc!=SQLITE_OK ){
127021    sqlite3_result_error_code(pContext, rc);
127022  }else{
127023    int n = pCsr->nMatchinfo * sizeof(u32);
127024    sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
127025  }
127026}
127027
127028#endif
127029
127030/************** End of fts3_snippet.c ****************************************/
127031/************** Begin file rtree.c *******************************************/
127032/*
127033** 2001 September 15
127034**
127035** The author disclaims copyright to this source code.  In place of
127036** a legal notice, here is a blessing:
127037**
127038**    May you do good and not evil.
127039**    May you find forgiveness for yourself and forgive others.
127040**    May you share freely, never taking more than you give.
127041**
127042*************************************************************************
127043** This file contains code for implementations of the r-tree and r*-tree
127044** algorithms packaged as an SQLite virtual table module.
127045*/
127046
127047/*
127048** Database Format of R-Tree Tables
127049** --------------------------------
127050**
127051** The data structure for a single virtual r-tree table is stored in three
127052** native SQLite tables declared as follows. In each case, the '%' character
127053** in the table name is replaced with the user-supplied name of the r-tree
127054** table.
127055**
127056**   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
127057**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
127058**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
127059**
127060** The data for each node of the r-tree structure is stored in the %_node
127061** table. For each node that is not the root node of the r-tree, there is
127062** an entry in the %_parent table associating the node with its parent.
127063** And for each row of data in the table, there is an entry in the %_rowid
127064** table that maps from the entries rowid to the id of the node that it
127065** is stored on.
127066**
127067** The root node of an r-tree always exists, even if the r-tree table is
127068** empty. The nodeno of the root node is always 1. All other nodes in the
127069** table must be the same size as the root node. The content of each node
127070** is formatted as follows:
127071**
127072**   1. If the node is the root node (node 1), then the first 2 bytes
127073**      of the node contain the tree depth as a big-endian integer.
127074**      For non-root nodes, the first 2 bytes are left unused.
127075**
127076**   2. The next 2 bytes contain the number of entries currently
127077**      stored in the node.
127078**
127079**   3. The remainder of the node contains the node entries. Each entry
127080**      consists of a single 8-byte integer followed by an even number
127081**      of 4-byte coordinates. For leaf nodes the integer is the rowid
127082**      of a record. For internal nodes it is the node number of a
127083**      child page.
127084*/
127085
127086#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
127087
127088/*
127089** This file contains an implementation of a couple of different variants
127090** of the r-tree algorithm. See the README file for further details. The
127091** same data-structure is used for all, but the algorithms for insert and
127092** delete operations vary. The variants used are selected at compile time
127093** by defining the following symbols:
127094*/
127095
127096/* Either, both or none of the following may be set to activate
127097** r*tree variant algorithms.
127098*/
127099#define VARIANT_RSTARTREE_CHOOSESUBTREE 0
127100#define VARIANT_RSTARTREE_REINSERT      1
127101
127102/*
127103** Exactly one of the following must be set to 1.
127104*/
127105#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
127106#define VARIANT_GUTTMAN_LINEAR_SPLIT    0
127107#define VARIANT_RSTARTREE_SPLIT         1
127108
127109#define VARIANT_GUTTMAN_SPLIT \
127110        (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
127111
127112#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
127113  #define PickNext QuadraticPickNext
127114  #define PickSeeds QuadraticPickSeeds
127115  #define AssignCells splitNodeGuttman
127116#endif
127117#if VARIANT_GUTTMAN_LINEAR_SPLIT
127118  #define PickNext LinearPickNext
127119  #define PickSeeds LinearPickSeeds
127120  #define AssignCells splitNodeGuttman
127121#endif
127122#if VARIANT_RSTARTREE_SPLIT
127123  #define AssignCells splitNodeStartree
127124#endif
127125
127126#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
127127# define NDEBUG 1
127128#endif
127129
127130#ifndef SQLITE_CORE
127131  SQLITE_EXTENSION_INIT1
127132#else
127133#endif
127134
127135/* #include <string.h> */
127136/* #include <assert.h> */
127137
127138#ifndef SQLITE_AMALGAMATION
127139#include "sqlite3rtree.h"
127140typedef sqlite3_int64 i64;
127141typedef unsigned char u8;
127142typedef unsigned int u32;
127143#endif
127144
127145/*  The following macro is used to suppress compiler warnings.
127146*/
127147#ifndef UNUSED_PARAMETER
127148# define UNUSED_PARAMETER(x) (void)(x)
127149#endif
127150
127151typedef struct Rtree Rtree;
127152typedef struct RtreeCursor RtreeCursor;
127153typedef struct RtreeNode RtreeNode;
127154typedef struct RtreeCell RtreeCell;
127155typedef struct RtreeConstraint RtreeConstraint;
127156typedef struct RtreeMatchArg RtreeMatchArg;
127157typedef struct RtreeGeomCallback RtreeGeomCallback;
127158typedef union RtreeCoord RtreeCoord;
127159
127160/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
127161#define RTREE_MAX_DIMENSIONS 5
127162
127163/* Size of hash table Rtree.aHash. This hash table is not expected to
127164** ever contain very many entries, so a fixed number of buckets is
127165** used.
127166*/
127167#define HASHSIZE 128
127168
127169/*
127170** An rtree virtual-table object.
127171*/
127172struct Rtree {
127173  sqlite3_vtab base;
127174  sqlite3 *db;                /* Host database connection */
127175  int iNodeSize;              /* Size in bytes of each node in the node table */
127176  int nDim;                   /* Number of dimensions */
127177  int nBytesPerCell;          /* Bytes consumed per cell */
127178  int iDepth;                 /* Current depth of the r-tree structure */
127179  char *zDb;                  /* Name of database containing r-tree table */
127180  char *zName;                /* Name of r-tree table */
127181  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
127182  int nBusy;                  /* Current number of users of this structure */
127183
127184  /* List of nodes removed during a CondenseTree operation. List is
127185  ** linked together via the pointer normally used for hash chains -
127186  ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
127187  ** headed by the node (leaf nodes have RtreeNode.iNode==0).
127188  */
127189  RtreeNode *pDeleted;
127190  int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
127191
127192  /* Statements to read/write/delete a record from xxx_node */
127193  sqlite3_stmt *pReadNode;
127194  sqlite3_stmt *pWriteNode;
127195  sqlite3_stmt *pDeleteNode;
127196
127197  /* Statements to read/write/delete a record from xxx_rowid */
127198  sqlite3_stmt *pReadRowid;
127199  sqlite3_stmt *pWriteRowid;
127200  sqlite3_stmt *pDeleteRowid;
127201
127202  /* Statements to read/write/delete a record from xxx_parent */
127203  sqlite3_stmt *pReadParent;
127204  sqlite3_stmt *pWriteParent;
127205  sqlite3_stmt *pDeleteParent;
127206
127207  int eCoordType;
127208};
127209
127210/* Possible values for eCoordType: */
127211#define RTREE_COORD_REAL32 0
127212#define RTREE_COORD_INT32  1
127213
127214/*
127215** The minimum number of cells allowed for a node is a third of the
127216** maximum. In Gutman's notation:
127217**
127218**     m = M/3
127219**
127220** If an R*-tree "Reinsert" operation is required, the same number of
127221** cells are removed from the overfull node and reinserted into the tree.
127222*/
127223#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
127224#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
127225#define RTREE_MAXCELLS 51
127226
127227/*
127228** The smallest possible node-size is (512-64)==448 bytes. And the largest
127229** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
127230** Therefore all non-root nodes must contain at least 3 entries. Since
127231** 2^40 is greater than 2^64, an r-tree structure always has a depth of
127232** 40 or less.
127233*/
127234#define RTREE_MAX_DEPTH 40
127235
127236/*
127237** An rtree cursor object.
127238*/
127239struct RtreeCursor {
127240  sqlite3_vtab_cursor base;
127241  RtreeNode *pNode;                 /* Node cursor is currently pointing at */
127242  int iCell;                        /* Index of current cell in pNode */
127243  int iStrategy;                    /* Copy of idxNum search parameter */
127244  int nConstraint;                  /* Number of entries in aConstraint */
127245  RtreeConstraint *aConstraint;     /* Search constraints. */
127246};
127247
127248union RtreeCoord {
127249  float f;
127250  int i;
127251};
127252
127253/*
127254** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
127255** formatted as a double. This macro assumes that local variable pRtree points
127256** to the Rtree structure associated with the RtreeCoord.
127257*/
127258#define DCOORD(coord) (                           \
127259  (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
127260    ((double)coord.f) :                           \
127261    ((double)coord.i)                             \
127262)
127263
127264/*
127265** A search constraint.
127266*/
127267struct RtreeConstraint {
127268  int iCoord;                     /* Index of constrained coordinate */
127269  int op;                         /* Constraining operation */
127270  double rValue;                  /* Constraint value. */
127271  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
127272  sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
127273};
127274
127275/* Possible values for RtreeConstraint.op */
127276#define RTREE_EQ    0x41
127277#define RTREE_LE    0x42
127278#define RTREE_LT    0x43
127279#define RTREE_GE    0x44
127280#define RTREE_GT    0x45
127281#define RTREE_MATCH 0x46
127282
127283/*
127284** An rtree structure node.
127285*/
127286struct RtreeNode {
127287  RtreeNode *pParent;               /* Parent node */
127288  i64 iNode;
127289  int nRef;
127290  int isDirty;
127291  u8 *zData;
127292  RtreeNode *pNext;                 /* Next node in this hash chain */
127293};
127294#define NCELL(pNode) readInt16(&(pNode)->zData[2])
127295
127296/*
127297** Structure to store a deserialized rtree record.
127298*/
127299struct RtreeCell {
127300  i64 iRowid;
127301  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
127302};
127303
127304
127305/*
127306** Value for the first field of every RtreeMatchArg object. The MATCH
127307** operator tests that the first field of a blob operand matches this
127308** value to avoid operating on invalid blobs (which could cause a segfault).
127309*/
127310#define RTREE_GEOMETRY_MAGIC 0x891245AB
127311
127312/*
127313** An instance of this structure must be supplied as a blob argument to
127314** the right-hand-side of an SQL MATCH operator used to constrain an
127315** r-tree query.
127316*/
127317struct RtreeMatchArg {
127318  u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
127319  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
127320  void *pContext;
127321  int nParam;
127322  double aParam[1];
127323};
127324
127325/*
127326** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
127327** a single instance of the following structure is allocated. It is used
127328** as the context for the user-function created by by s_r_g_c(). The object
127329** is eventually deleted by the destructor mechanism provided by
127330** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
127331** the geometry callback function).
127332*/
127333struct RtreeGeomCallback {
127334  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
127335  void *pContext;
127336};
127337
127338#ifndef MAX
127339# define MAX(x,y) ((x) < (y) ? (y) : (x))
127340#endif
127341#ifndef MIN
127342# define MIN(x,y) ((x) > (y) ? (y) : (x))
127343#endif
127344
127345/*
127346** Functions to deserialize a 16 bit integer, 32 bit real number and
127347** 64 bit integer. The deserialized value is returned.
127348*/
127349static int readInt16(u8 *p){
127350  return (p[0]<<8) + p[1];
127351}
127352static void readCoord(u8 *p, RtreeCoord *pCoord){
127353  u32 i = (
127354    (((u32)p[0]) << 24) +
127355    (((u32)p[1]) << 16) +
127356    (((u32)p[2]) <<  8) +
127357    (((u32)p[3]) <<  0)
127358  );
127359  *(u32 *)pCoord = i;
127360}
127361static i64 readInt64(u8 *p){
127362  return (
127363    (((i64)p[0]) << 56) +
127364    (((i64)p[1]) << 48) +
127365    (((i64)p[2]) << 40) +
127366    (((i64)p[3]) << 32) +
127367    (((i64)p[4]) << 24) +
127368    (((i64)p[5]) << 16) +
127369    (((i64)p[6]) <<  8) +
127370    (((i64)p[7]) <<  0)
127371  );
127372}
127373
127374/*
127375** Functions to serialize a 16 bit integer, 32 bit real number and
127376** 64 bit integer. The value returned is the number of bytes written
127377** to the argument buffer (always 2, 4 and 8 respectively).
127378*/
127379static int writeInt16(u8 *p, int i){
127380  p[0] = (i>> 8)&0xFF;
127381  p[1] = (i>> 0)&0xFF;
127382  return 2;
127383}
127384static int writeCoord(u8 *p, RtreeCoord *pCoord){
127385  u32 i;
127386  assert( sizeof(RtreeCoord)==4 );
127387  assert( sizeof(u32)==4 );
127388  i = *(u32 *)pCoord;
127389  p[0] = (i>>24)&0xFF;
127390  p[1] = (i>>16)&0xFF;
127391  p[2] = (i>> 8)&0xFF;
127392  p[3] = (i>> 0)&0xFF;
127393  return 4;
127394}
127395static int writeInt64(u8 *p, i64 i){
127396  p[0] = (i>>56)&0xFF;
127397  p[1] = (i>>48)&0xFF;
127398  p[2] = (i>>40)&0xFF;
127399  p[3] = (i>>32)&0xFF;
127400  p[4] = (i>>24)&0xFF;
127401  p[5] = (i>>16)&0xFF;
127402  p[6] = (i>> 8)&0xFF;
127403  p[7] = (i>> 0)&0xFF;
127404  return 8;
127405}
127406
127407/*
127408** Increment the reference count of node p.
127409*/
127410static void nodeReference(RtreeNode *p){
127411  if( p ){
127412    p->nRef++;
127413  }
127414}
127415
127416/*
127417** Clear the content of node p (set all bytes to 0x00).
127418*/
127419static void nodeZero(Rtree *pRtree, RtreeNode *p){
127420  memset(&p->zData[2], 0, pRtree->iNodeSize-2);
127421  p->isDirty = 1;
127422}
127423
127424/*
127425** Given a node number iNode, return the corresponding key to use
127426** in the Rtree.aHash table.
127427*/
127428static int nodeHash(i64 iNode){
127429  return (
127430    (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
127431    (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
127432  ) % HASHSIZE;
127433}
127434
127435/*
127436** Search the node hash table for node iNode. If found, return a pointer
127437** to it. Otherwise, return 0.
127438*/
127439static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
127440  RtreeNode *p;
127441  for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
127442  return p;
127443}
127444
127445/*
127446** Add node pNode to the node hash table.
127447*/
127448static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
127449  int iHash;
127450  assert( pNode->pNext==0 );
127451  iHash = nodeHash(pNode->iNode);
127452  pNode->pNext = pRtree->aHash[iHash];
127453  pRtree->aHash[iHash] = pNode;
127454}
127455
127456/*
127457** Remove node pNode from the node hash table.
127458*/
127459static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
127460  RtreeNode **pp;
127461  if( pNode->iNode!=0 ){
127462    pp = &pRtree->aHash[nodeHash(pNode->iNode)];
127463    for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
127464    *pp = pNode->pNext;
127465    pNode->pNext = 0;
127466  }
127467}
127468
127469/*
127470** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
127471** indicating that node has not yet been assigned a node number. It is
127472** assigned a node number when nodeWrite() is called to write the
127473** node contents out to the database.
127474*/
127475static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
127476  RtreeNode *pNode;
127477  pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
127478  if( pNode ){
127479    memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
127480    pNode->zData = (u8 *)&pNode[1];
127481    pNode->nRef = 1;
127482    pNode->pParent = pParent;
127483    pNode->isDirty = 1;
127484    nodeReference(pParent);
127485  }
127486  return pNode;
127487}
127488
127489/*
127490** Obtain a reference to an r-tree node.
127491*/
127492static int
127493nodeAcquire(
127494  Rtree *pRtree,             /* R-tree structure */
127495  i64 iNode,                 /* Node number to load */
127496  RtreeNode *pParent,        /* Either the parent node or NULL */
127497  RtreeNode **ppNode         /* OUT: Acquired node */
127498){
127499  int rc;
127500  int rc2 = SQLITE_OK;
127501  RtreeNode *pNode;
127502
127503  /* Check if the requested node is already in the hash table. If so,
127504  ** increase its reference count and return it.
127505  */
127506  if( (pNode = nodeHashLookup(pRtree, iNode)) ){
127507    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
127508    if( pParent && !pNode->pParent ){
127509      nodeReference(pParent);
127510      pNode->pParent = pParent;
127511    }
127512    pNode->nRef++;
127513    *ppNode = pNode;
127514    return SQLITE_OK;
127515  }
127516
127517  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
127518  rc = sqlite3_step(pRtree->pReadNode);
127519  if( rc==SQLITE_ROW ){
127520    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
127521    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
127522      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
127523      if( !pNode ){
127524        rc2 = SQLITE_NOMEM;
127525      }else{
127526        pNode->pParent = pParent;
127527        pNode->zData = (u8 *)&pNode[1];
127528        pNode->nRef = 1;
127529        pNode->iNode = iNode;
127530        pNode->isDirty = 0;
127531        pNode->pNext = 0;
127532        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
127533        nodeReference(pParent);
127534      }
127535    }
127536  }
127537  rc = sqlite3_reset(pRtree->pReadNode);
127538  if( rc==SQLITE_OK ) rc = rc2;
127539
127540  /* If the root node was just loaded, set pRtree->iDepth to the height
127541  ** of the r-tree structure. A height of zero means all data is stored on
127542  ** the root node. A height of one means the children of the root node
127543  ** are the leaves, and so on. If the depth as specified on the root node
127544  ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
127545  */
127546  if( pNode && iNode==1 ){
127547    pRtree->iDepth = readInt16(pNode->zData);
127548    if( pRtree->iDepth>RTREE_MAX_DEPTH ){
127549      rc = SQLITE_CORRUPT_VTAB;
127550    }
127551  }
127552
127553  /* If no error has occurred so far, check if the "number of entries"
127554  ** field on the node is too large. If so, set the return code to
127555  ** SQLITE_CORRUPT_VTAB.
127556  */
127557  if( pNode && rc==SQLITE_OK ){
127558    if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
127559      rc = SQLITE_CORRUPT_VTAB;
127560    }
127561  }
127562
127563  if( rc==SQLITE_OK ){
127564    if( pNode!=0 ){
127565      nodeHashInsert(pRtree, pNode);
127566    }else{
127567      rc = SQLITE_CORRUPT_VTAB;
127568    }
127569    *ppNode = pNode;
127570  }else{
127571    sqlite3_free(pNode);
127572    *ppNode = 0;
127573  }
127574
127575  return rc;
127576}
127577
127578/*
127579** Overwrite cell iCell of node pNode with the contents of pCell.
127580*/
127581static void nodeOverwriteCell(
127582  Rtree *pRtree,
127583  RtreeNode *pNode,
127584  RtreeCell *pCell,
127585  int iCell
127586){
127587  int ii;
127588  u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
127589  p += writeInt64(p, pCell->iRowid);
127590  for(ii=0; ii<(pRtree->nDim*2); ii++){
127591    p += writeCoord(p, &pCell->aCoord[ii]);
127592  }
127593  pNode->isDirty = 1;
127594}
127595
127596/*
127597** Remove cell the cell with index iCell from node pNode.
127598*/
127599static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
127600  u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
127601  u8 *pSrc = &pDst[pRtree->nBytesPerCell];
127602  int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
127603  memmove(pDst, pSrc, nByte);
127604  writeInt16(&pNode->zData[2], NCELL(pNode)-1);
127605  pNode->isDirty = 1;
127606}
127607
127608/*
127609** Insert the contents of cell pCell into node pNode. If the insert
127610** is successful, return SQLITE_OK.
127611**
127612** If there is not enough free space in pNode, return SQLITE_FULL.
127613*/
127614static int
127615nodeInsertCell(
127616  Rtree *pRtree,
127617  RtreeNode *pNode,
127618  RtreeCell *pCell
127619){
127620  int nCell;                    /* Current number of cells in pNode */
127621  int nMaxCell;                 /* Maximum number of cells for pNode */
127622
127623  nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
127624  nCell = NCELL(pNode);
127625
127626  assert( nCell<=nMaxCell );
127627  if( nCell<nMaxCell ){
127628    nodeOverwriteCell(pRtree, pNode, pCell, nCell);
127629    writeInt16(&pNode->zData[2], nCell+1);
127630    pNode->isDirty = 1;
127631  }
127632
127633  return (nCell==nMaxCell);
127634}
127635
127636/*
127637** If the node is dirty, write it out to the database.
127638*/
127639static int
127640nodeWrite(Rtree *pRtree, RtreeNode *pNode){
127641  int rc = SQLITE_OK;
127642  if( pNode->isDirty ){
127643    sqlite3_stmt *p = pRtree->pWriteNode;
127644    if( pNode->iNode ){
127645      sqlite3_bind_int64(p, 1, pNode->iNode);
127646    }else{
127647      sqlite3_bind_null(p, 1);
127648    }
127649    sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
127650    sqlite3_step(p);
127651    pNode->isDirty = 0;
127652    rc = sqlite3_reset(p);
127653    if( pNode->iNode==0 && rc==SQLITE_OK ){
127654      pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
127655      nodeHashInsert(pRtree, pNode);
127656    }
127657  }
127658  return rc;
127659}
127660
127661/*
127662** Release a reference to a node. If the node is dirty and the reference
127663** count drops to zero, the node data is written to the database.
127664*/
127665static int
127666nodeRelease(Rtree *pRtree, RtreeNode *pNode){
127667  int rc = SQLITE_OK;
127668  if( pNode ){
127669    assert( pNode->nRef>0 );
127670    pNode->nRef--;
127671    if( pNode->nRef==0 ){
127672      if( pNode->iNode==1 ){
127673        pRtree->iDepth = -1;
127674      }
127675      if( pNode->pParent ){
127676        rc = nodeRelease(pRtree, pNode->pParent);
127677      }
127678      if( rc==SQLITE_OK ){
127679        rc = nodeWrite(pRtree, pNode);
127680      }
127681      nodeHashDelete(pRtree, pNode);
127682      sqlite3_free(pNode);
127683    }
127684  }
127685  return rc;
127686}
127687
127688/*
127689** Return the 64-bit integer value associated with cell iCell of
127690** node pNode. If pNode is a leaf node, this is a rowid. If it is
127691** an internal node, then the 64-bit integer is a child page number.
127692*/
127693static i64 nodeGetRowid(
127694  Rtree *pRtree,
127695  RtreeNode *pNode,
127696  int iCell
127697){
127698  assert( iCell<NCELL(pNode) );
127699  return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
127700}
127701
127702/*
127703** Return coordinate iCoord from cell iCell in node pNode.
127704*/
127705static void nodeGetCoord(
127706  Rtree *pRtree,
127707  RtreeNode *pNode,
127708  int iCell,
127709  int iCoord,
127710  RtreeCoord *pCoord           /* Space to write result to */
127711){
127712  readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
127713}
127714
127715/*
127716** Deserialize cell iCell of node pNode. Populate the structure pointed
127717** to by pCell with the results.
127718*/
127719static void nodeGetCell(
127720  Rtree *pRtree,
127721  RtreeNode *pNode,
127722  int iCell,
127723  RtreeCell *pCell
127724){
127725  int ii;
127726  pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
127727  for(ii=0; ii<pRtree->nDim*2; ii++){
127728    nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
127729  }
127730}
127731
127732
127733/* Forward declaration for the function that does the work of
127734** the virtual table module xCreate() and xConnect() methods.
127735*/
127736static int rtreeInit(
127737  sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
127738);
127739
127740/*
127741** Rtree virtual table module xCreate method.
127742*/
127743static int rtreeCreate(
127744  sqlite3 *db,
127745  void *pAux,
127746  int argc, const char *const*argv,
127747  sqlite3_vtab **ppVtab,
127748  char **pzErr
127749){
127750  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
127751}
127752
127753/*
127754** Rtree virtual table module xConnect method.
127755*/
127756static int rtreeConnect(
127757  sqlite3 *db,
127758  void *pAux,
127759  int argc, const char *const*argv,
127760  sqlite3_vtab **ppVtab,
127761  char **pzErr
127762){
127763  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
127764}
127765
127766/*
127767** Increment the r-tree reference count.
127768*/
127769static void rtreeReference(Rtree *pRtree){
127770  pRtree->nBusy++;
127771}
127772
127773/*
127774** Decrement the r-tree reference count. When the reference count reaches
127775** zero the structure is deleted.
127776*/
127777static void rtreeRelease(Rtree *pRtree){
127778  pRtree->nBusy--;
127779  if( pRtree->nBusy==0 ){
127780    sqlite3_finalize(pRtree->pReadNode);
127781    sqlite3_finalize(pRtree->pWriteNode);
127782    sqlite3_finalize(pRtree->pDeleteNode);
127783    sqlite3_finalize(pRtree->pReadRowid);
127784    sqlite3_finalize(pRtree->pWriteRowid);
127785    sqlite3_finalize(pRtree->pDeleteRowid);
127786    sqlite3_finalize(pRtree->pReadParent);
127787    sqlite3_finalize(pRtree->pWriteParent);
127788    sqlite3_finalize(pRtree->pDeleteParent);
127789    sqlite3_free(pRtree);
127790  }
127791}
127792
127793/*
127794** Rtree virtual table module xDisconnect method.
127795*/
127796static int rtreeDisconnect(sqlite3_vtab *pVtab){
127797  rtreeRelease((Rtree *)pVtab);
127798  return SQLITE_OK;
127799}
127800
127801/*
127802** Rtree virtual table module xDestroy method.
127803*/
127804static int rtreeDestroy(sqlite3_vtab *pVtab){
127805  Rtree *pRtree = (Rtree *)pVtab;
127806  int rc;
127807  char *zCreate = sqlite3_mprintf(
127808    "DROP TABLE '%q'.'%q_node';"
127809    "DROP TABLE '%q'.'%q_rowid';"
127810    "DROP TABLE '%q'.'%q_parent';",
127811    pRtree->zDb, pRtree->zName,
127812    pRtree->zDb, pRtree->zName,
127813    pRtree->zDb, pRtree->zName
127814  );
127815  if( !zCreate ){
127816    rc = SQLITE_NOMEM;
127817  }else{
127818    rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
127819    sqlite3_free(zCreate);
127820  }
127821  if( rc==SQLITE_OK ){
127822    rtreeRelease(pRtree);
127823  }
127824
127825  return rc;
127826}
127827
127828/*
127829** Rtree virtual table module xOpen method.
127830*/
127831static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
127832  int rc = SQLITE_NOMEM;
127833  RtreeCursor *pCsr;
127834
127835  pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
127836  if( pCsr ){
127837    memset(pCsr, 0, sizeof(RtreeCursor));
127838    pCsr->base.pVtab = pVTab;
127839    rc = SQLITE_OK;
127840  }
127841  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
127842
127843  return rc;
127844}
127845
127846
127847/*
127848** Free the RtreeCursor.aConstraint[] array and its contents.
127849*/
127850static void freeCursorConstraints(RtreeCursor *pCsr){
127851  if( pCsr->aConstraint ){
127852    int i;                        /* Used to iterate through constraint array */
127853    for(i=0; i<pCsr->nConstraint; i++){
127854      sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
127855      if( pGeom ){
127856        if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
127857        sqlite3_free(pGeom);
127858      }
127859    }
127860    sqlite3_free(pCsr->aConstraint);
127861    pCsr->aConstraint = 0;
127862  }
127863}
127864
127865/*
127866** Rtree virtual table module xClose method.
127867*/
127868static int rtreeClose(sqlite3_vtab_cursor *cur){
127869  Rtree *pRtree = (Rtree *)(cur->pVtab);
127870  int rc;
127871  RtreeCursor *pCsr = (RtreeCursor *)cur;
127872  freeCursorConstraints(pCsr);
127873  rc = nodeRelease(pRtree, pCsr->pNode);
127874  sqlite3_free(pCsr);
127875  return rc;
127876}
127877
127878/*
127879** Rtree virtual table module xEof method.
127880**
127881** Return non-zero if the cursor does not currently point to a valid
127882** record (i.e if the scan has finished), or zero otherwise.
127883*/
127884static int rtreeEof(sqlite3_vtab_cursor *cur){
127885  RtreeCursor *pCsr = (RtreeCursor *)cur;
127886  return (pCsr->pNode==0);
127887}
127888
127889/*
127890** The r-tree constraint passed as the second argument to this function is
127891** guaranteed to be a MATCH constraint.
127892*/
127893static int testRtreeGeom(
127894  Rtree *pRtree,                  /* R-Tree object */
127895  RtreeConstraint *pConstraint,   /* MATCH constraint to test */
127896  RtreeCell *pCell,               /* Cell to test */
127897  int *pbRes                      /* OUT: Test result */
127898){
127899  int i;
127900  double aCoord[RTREE_MAX_DIMENSIONS*2];
127901  int nCoord = pRtree->nDim*2;
127902
127903  assert( pConstraint->op==RTREE_MATCH );
127904  assert( pConstraint->pGeom );
127905
127906  for(i=0; i<nCoord; i++){
127907    aCoord[i] = DCOORD(pCell->aCoord[i]);
127908  }
127909  return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
127910}
127911
127912/*
127913** Cursor pCursor currently points to a cell in a non-leaf page.
127914** Set *pbEof to true if the sub-tree headed by the cell is filtered
127915** (excluded) by the constraints in the pCursor->aConstraint[]
127916** array, or false otherwise.
127917**
127918** Return SQLITE_OK if successful or an SQLite error code if an error
127919** occurs within a geometry callback.
127920*/
127921static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
127922  RtreeCell cell;
127923  int ii;
127924  int bRes = 0;
127925  int rc = SQLITE_OK;
127926
127927  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
127928  for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
127929    RtreeConstraint *p = &pCursor->aConstraint[ii];
127930    double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
127931    double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
127932
127933    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
127934        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
127935    );
127936
127937    switch( p->op ){
127938      case RTREE_LE: case RTREE_LT:
127939        bRes = p->rValue<cell_min;
127940        break;
127941
127942      case RTREE_GE: case RTREE_GT:
127943        bRes = p->rValue>cell_max;
127944        break;
127945
127946      case RTREE_EQ:
127947        bRes = (p->rValue>cell_max || p->rValue<cell_min);
127948        break;
127949
127950      default: {
127951        assert( p->op==RTREE_MATCH );
127952        rc = testRtreeGeom(pRtree, p, &cell, &bRes);
127953        bRes = !bRes;
127954        break;
127955      }
127956    }
127957  }
127958
127959  *pbEof = bRes;
127960  return rc;
127961}
127962
127963/*
127964** Test if the cell that cursor pCursor currently points to
127965** would be filtered (excluded) by the constraints in the
127966** pCursor->aConstraint[] array. If so, set *pbEof to true before
127967** returning. If the cell is not filtered (excluded) by the constraints,
127968** set pbEof to zero.
127969**
127970** Return SQLITE_OK if successful or an SQLite error code if an error
127971** occurs within a geometry callback.
127972**
127973** This function assumes that the cell is part of a leaf node.
127974*/
127975static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
127976  RtreeCell cell;
127977  int ii;
127978  *pbEof = 0;
127979
127980  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
127981  for(ii=0; ii<pCursor->nConstraint; ii++){
127982    RtreeConstraint *p = &pCursor->aConstraint[ii];
127983    double coord = DCOORD(cell.aCoord[p->iCoord]);
127984    int res;
127985    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
127986        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
127987    );
127988    switch( p->op ){
127989      case RTREE_LE: res = (coord<=p->rValue); break;
127990      case RTREE_LT: res = (coord<p->rValue);  break;
127991      case RTREE_GE: res = (coord>=p->rValue); break;
127992      case RTREE_GT: res = (coord>p->rValue);  break;
127993      case RTREE_EQ: res = (coord==p->rValue); break;
127994      default: {
127995        int rc;
127996        assert( p->op==RTREE_MATCH );
127997        rc = testRtreeGeom(pRtree, p, &cell, &res);
127998        if( rc!=SQLITE_OK ){
127999          return rc;
128000        }
128001        break;
128002      }
128003    }
128004
128005    if( !res ){
128006      *pbEof = 1;
128007      return SQLITE_OK;
128008    }
128009  }
128010
128011  return SQLITE_OK;
128012}
128013
128014/*
128015** Cursor pCursor currently points at a node that heads a sub-tree of
128016** height iHeight (if iHeight==0, then the node is a leaf). Descend
128017** to point to the left-most cell of the sub-tree that matches the
128018** configured constraints.
128019*/
128020static int descendToCell(
128021  Rtree *pRtree,
128022  RtreeCursor *pCursor,
128023  int iHeight,
128024  int *pEof                 /* OUT: Set to true if cannot descend */
128025){
128026  int isEof;
128027  int rc;
128028  int ii;
128029  RtreeNode *pChild;
128030  sqlite3_int64 iRowid;
128031
128032  RtreeNode *pSavedNode = pCursor->pNode;
128033  int iSavedCell = pCursor->iCell;
128034
128035  assert( iHeight>=0 );
128036
128037  if( iHeight==0 ){
128038    rc = testRtreeEntry(pRtree, pCursor, &isEof);
128039  }else{
128040    rc = testRtreeCell(pRtree, pCursor, &isEof);
128041  }
128042  if( rc!=SQLITE_OK || isEof || iHeight==0 ){
128043    goto descend_to_cell_out;
128044  }
128045
128046  iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
128047  rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
128048  if( rc!=SQLITE_OK ){
128049    goto descend_to_cell_out;
128050  }
128051
128052  nodeRelease(pRtree, pCursor->pNode);
128053  pCursor->pNode = pChild;
128054  isEof = 1;
128055  for(ii=0; isEof && ii<NCELL(pChild); ii++){
128056    pCursor->iCell = ii;
128057    rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
128058    if( rc!=SQLITE_OK ){
128059      goto descend_to_cell_out;
128060    }
128061  }
128062
128063  if( isEof ){
128064    assert( pCursor->pNode==pChild );
128065    nodeReference(pSavedNode);
128066    nodeRelease(pRtree, pChild);
128067    pCursor->pNode = pSavedNode;
128068    pCursor->iCell = iSavedCell;
128069  }
128070
128071descend_to_cell_out:
128072  *pEof = isEof;
128073  return rc;
128074}
128075
128076/*
128077** One of the cells in node pNode is guaranteed to have a 64-bit
128078** integer value equal to iRowid. Return the index of this cell.
128079*/
128080static int nodeRowidIndex(
128081  Rtree *pRtree,
128082  RtreeNode *pNode,
128083  i64 iRowid,
128084  int *piIndex
128085){
128086  int ii;
128087  int nCell = NCELL(pNode);
128088  for(ii=0; ii<nCell; ii++){
128089    if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
128090      *piIndex = ii;
128091      return SQLITE_OK;
128092    }
128093  }
128094  return SQLITE_CORRUPT_VTAB;
128095}
128096
128097/*
128098** Return the index of the cell containing a pointer to node pNode
128099** in its parent. If pNode is the root node, return -1.
128100*/
128101static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
128102  RtreeNode *pParent = pNode->pParent;
128103  if( pParent ){
128104    return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
128105  }
128106  *piIndex = -1;
128107  return SQLITE_OK;
128108}
128109
128110/*
128111** Rtree virtual table module xNext method.
128112*/
128113static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
128114  Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
128115  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
128116  int rc = SQLITE_OK;
128117
128118  /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
128119  ** already at EOF. It is against the rules to call the xNext() method of
128120  ** a cursor that has already reached EOF.
128121  */
128122  assert( pCsr->pNode );
128123
128124  if( pCsr->iStrategy==1 ){
128125    /* This "scan" is a direct lookup by rowid. There is no next entry. */
128126    nodeRelease(pRtree, pCsr->pNode);
128127    pCsr->pNode = 0;
128128  }else{
128129    /* Move to the next entry that matches the configured constraints. */
128130    int iHeight = 0;
128131    while( pCsr->pNode ){
128132      RtreeNode *pNode = pCsr->pNode;
128133      int nCell = NCELL(pNode);
128134      for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
128135        int isEof;
128136        rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
128137        if( rc!=SQLITE_OK || !isEof ){
128138          return rc;
128139        }
128140      }
128141      pCsr->pNode = pNode->pParent;
128142      rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
128143      if( rc!=SQLITE_OK ){
128144        return rc;
128145      }
128146      nodeReference(pCsr->pNode);
128147      nodeRelease(pRtree, pNode);
128148      iHeight++;
128149    }
128150  }
128151
128152  return rc;
128153}
128154
128155/*
128156** Rtree virtual table module xRowid method.
128157*/
128158static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
128159  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
128160  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
128161
128162  assert(pCsr->pNode);
128163  *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
128164
128165  return SQLITE_OK;
128166}
128167
128168/*
128169** Rtree virtual table module xColumn method.
128170*/
128171static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
128172  Rtree *pRtree = (Rtree *)cur->pVtab;
128173  RtreeCursor *pCsr = (RtreeCursor *)cur;
128174
128175  if( i==0 ){
128176    i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
128177    sqlite3_result_int64(ctx, iRowid);
128178  }else{
128179    RtreeCoord c;
128180    nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
128181    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
128182      sqlite3_result_double(ctx, c.f);
128183    }else{
128184      assert( pRtree->eCoordType==RTREE_COORD_INT32 );
128185      sqlite3_result_int(ctx, c.i);
128186    }
128187  }
128188
128189  return SQLITE_OK;
128190}
128191
128192/*
128193** Use nodeAcquire() to obtain the leaf node containing the record with
128194** rowid iRowid. If successful, set *ppLeaf to point to the node and
128195** return SQLITE_OK. If there is no such record in the table, set
128196** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
128197** to zero and return an SQLite error code.
128198*/
128199static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
128200  int rc;
128201  *ppLeaf = 0;
128202  sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
128203  if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
128204    i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
128205    rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
128206    sqlite3_reset(pRtree->pReadRowid);
128207  }else{
128208    rc = sqlite3_reset(pRtree->pReadRowid);
128209  }
128210  return rc;
128211}
128212
128213/*
128214** This function is called to configure the RtreeConstraint object passed
128215** as the second argument for a MATCH constraint. The value passed as the
128216** first argument to this function is the right-hand operand to the MATCH
128217** operator.
128218*/
128219static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
128220  RtreeMatchArg *p;
128221  sqlite3_rtree_geometry *pGeom;
128222  int nBlob;
128223
128224  /* Check that value is actually a blob. */
128225  if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR;
128226
128227  /* Check that the blob is roughly the right size. */
128228  nBlob = sqlite3_value_bytes(pValue);
128229  if( nBlob<(int)sizeof(RtreeMatchArg)
128230   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
128231  ){
128232    return SQLITE_ERROR;
128233  }
128234
128235  pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
128236      sizeof(sqlite3_rtree_geometry) + nBlob
128237  );
128238  if( !pGeom ) return SQLITE_NOMEM;
128239  memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
128240  p = (RtreeMatchArg *)&pGeom[1];
128241
128242  memcpy(p, sqlite3_value_blob(pValue), nBlob);
128243  if( p->magic!=RTREE_GEOMETRY_MAGIC
128244   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
128245  ){
128246    sqlite3_free(pGeom);
128247    return SQLITE_ERROR;
128248  }
128249
128250  pGeom->pContext = p->pContext;
128251  pGeom->nParam = p->nParam;
128252  pGeom->aParam = p->aParam;
128253
128254  pCons->xGeom = p->xGeom;
128255  pCons->pGeom = pGeom;
128256  return SQLITE_OK;
128257}
128258
128259/*
128260** Rtree virtual table module xFilter method.
128261*/
128262static int rtreeFilter(
128263  sqlite3_vtab_cursor *pVtabCursor,
128264  int idxNum, const char *idxStr,
128265  int argc, sqlite3_value **argv
128266){
128267  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
128268  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
128269
128270  RtreeNode *pRoot = 0;
128271  int ii;
128272  int rc = SQLITE_OK;
128273
128274  rtreeReference(pRtree);
128275
128276  freeCursorConstraints(pCsr);
128277  pCsr->iStrategy = idxNum;
128278
128279  if( idxNum==1 ){
128280    /* Special case - lookup by rowid. */
128281    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
128282    i64 iRowid = sqlite3_value_int64(argv[0]);
128283    rc = findLeafNode(pRtree, iRowid, &pLeaf);
128284    pCsr->pNode = pLeaf;
128285    if( pLeaf ){
128286      assert( rc==SQLITE_OK );
128287      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
128288    }
128289  }else{
128290    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
128291    ** with the configured constraints.
128292    */
128293    if( argc>0 ){
128294      pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
128295      pCsr->nConstraint = argc;
128296      if( !pCsr->aConstraint ){
128297        rc = SQLITE_NOMEM;
128298      }else{
128299        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
128300        assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
128301        for(ii=0; ii<argc; ii++){
128302          RtreeConstraint *p = &pCsr->aConstraint[ii];
128303          p->op = idxStr[ii*2];
128304          p->iCoord = idxStr[ii*2+1]-'a';
128305          if( p->op==RTREE_MATCH ){
128306            /* A MATCH operator. The right-hand-side must be a blob that
128307            ** can be cast into an RtreeMatchArg object. One created using
128308            ** an sqlite3_rtree_geometry_callback() SQL user function.
128309            */
128310            rc = deserializeGeometry(argv[ii], p);
128311            if( rc!=SQLITE_OK ){
128312              break;
128313            }
128314          }else{
128315            p->rValue = sqlite3_value_double(argv[ii]);
128316          }
128317        }
128318      }
128319    }
128320
128321    if( rc==SQLITE_OK ){
128322      pCsr->pNode = 0;
128323      rc = nodeAcquire(pRtree, 1, 0, &pRoot);
128324    }
128325    if( rc==SQLITE_OK ){
128326      int isEof = 1;
128327      int nCell = NCELL(pRoot);
128328      pCsr->pNode = pRoot;
128329      for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
128330        assert( pCsr->pNode==pRoot );
128331        rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
128332        if( !isEof ){
128333          break;
128334        }
128335      }
128336      if( rc==SQLITE_OK && isEof ){
128337        assert( pCsr->pNode==pRoot );
128338        nodeRelease(pRtree, pRoot);
128339        pCsr->pNode = 0;
128340      }
128341      assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
128342    }
128343  }
128344
128345  rtreeRelease(pRtree);
128346  return rc;
128347}
128348
128349/*
128350** Rtree virtual table module xBestIndex method. There are three
128351** table scan strategies to choose from (in order from most to
128352** least desirable):
128353**
128354**   idxNum     idxStr        Strategy
128355**   ------------------------------------------------
128356**     1        Unused        Direct lookup by rowid.
128357**     2        See below     R-tree query or full-table scan.
128358**   ------------------------------------------------
128359**
128360** If strategy 1 is used, then idxStr is not meaningful. If strategy
128361** 2 is used, idxStr is formatted to contain 2 bytes for each
128362** constraint used. The first two bytes of idxStr correspond to
128363** the constraint in sqlite3_index_info.aConstraintUsage[] with
128364** (argvIndex==1) etc.
128365**
128366** The first of each pair of bytes in idxStr identifies the constraint
128367** operator as follows:
128368**
128369**   Operator    Byte Value
128370**   ----------------------
128371**      =        0x41 ('A')
128372**     <=        0x42 ('B')
128373**      <        0x43 ('C')
128374**     >=        0x44 ('D')
128375**      >        0x45 ('E')
128376**   MATCH       0x46 ('F')
128377**   ----------------------
128378**
128379** The second of each pair of bytes identifies the coordinate column
128380** to which the constraint applies. The leftmost coordinate column
128381** is 'a', the second from the left 'b' etc.
128382*/
128383static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
128384  int rc = SQLITE_OK;
128385  int ii;
128386
128387  int iIdx = 0;
128388  char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
128389  memset(zIdxStr, 0, sizeof(zIdxStr));
128390  UNUSED_PARAMETER(tab);
128391
128392  assert( pIdxInfo->idxStr==0 );
128393  for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
128394    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
128395
128396    if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
128397      /* We have an equality constraint on the rowid. Use strategy 1. */
128398      int jj;
128399      for(jj=0; jj<ii; jj++){
128400        pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
128401        pIdxInfo->aConstraintUsage[jj].omit = 0;
128402      }
128403      pIdxInfo->idxNum = 1;
128404      pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
128405      pIdxInfo->aConstraintUsage[jj].omit = 1;
128406
128407      /* This strategy involves a two rowid lookups on an B-Tree structures
128408      ** and then a linear search of an R-Tree node. This should be
128409      ** considered almost as quick as a direct rowid lookup (for which
128410      ** sqlite uses an internal cost of 0.0).
128411      */
128412      pIdxInfo->estimatedCost = 10.0;
128413      return SQLITE_OK;
128414    }
128415
128416    if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
128417      u8 op;
128418      switch( p->op ){
128419        case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
128420        case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
128421        case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
128422        case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
128423        case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
128424        default:
128425          assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
128426          op = RTREE_MATCH;
128427          break;
128428      }
128429      zIdxStr[iIdx++] = op;
128430      zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
128431      pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
128432      pIdxInfo->aConstraintUsage[ii].omit = 1;
128433    }
128434  }
128435
128436  pIdxInfo->idxNum = 2;
128437  pIdxInfo->needToFreeIdxStr = 1;
128438  if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
128439    return SQLITE_NOMEM;
128440  }
128441  assert( iIdx>=0 );
128442  pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
128443  return rc;
128444}
128445
128446/*
128447** Return the N-dimensional volumn of the cell stored in *p.
128448*/
128449static float cellArea(Rtree *pRtree, RtreeCell *p){
128450  float area = 1.0;
128451  int ii;
128452  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
128453    area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
128454  }
128455  return area;
128456}
128457
128458/*
128459** Return the margin length of cell p. The margin length is the sum
128460** of the objects size in each dimension.
128461*/
128462static float cellMargin(Rtree *pRtree, RtreeCell *p){
128463  float margin = 0.0;
128464  int ii;
128465  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
128466    margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
128467  }
128468  return margin;
128469}
128470
128471/*
128472** Store the union of cells p1 and p2 in p1.
128473*/
128474static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
128475  int ii;
128476  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
128477    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
128478      p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
128479      p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
128480    }
128481  }else{
128482    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
128483      p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
128484      p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
128485    }
128486  }
128487}
128488
128489/*
128490** Return true if the area covered by p2 is a subset of the area covered
128491** by p1. False otherwise.
128492*/
128493static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
128494  int ii;
128495  int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
128496  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
128497    RtreeCoord *a1 = &p1->aCoord[ii];
128498    RtreeCoord *a2 = &p2->aCoord[ii];
128499    if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
128500     || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
128501    ){
128502      return 0;
128503    }
128504  }
128505  return 1;
128506}
128507
128508/*
128509** Return the amount cell p would grow by if it were unioned with pCell.
128510*/
128511static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
128512  float area;
128513  RtreeCell cell;
128514  memcpy(&cell, p, sizeof(RtreeCell));
128515  area = cellArea(pRtree, &cell);
128516  cellUnion(pRtree, &cell, pCell);
128517  return (cellArea(pRtree, &cell)-area);
128518}
128519
128520#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
128521static float cellOverlap(
128522  Rtree *pRtree,
128523  RtreeCell *p,
128524  RtreeCell *aCell,
128525  int nCell,
128526  int iExclude
128527){
128528  int ii;
128529  float overlap = 0.0;
128530  for(ii=0; ii<nCell; ii++){
128531#if VARIANT_RSTARTREE_CHOOSESUBTREE
128532    if( ii!=iExclude )
128533#else
128534    assert( iExclude==-1 );
128535    UNUSED_PARAMETER(iExclude);
128536#endif
128537    {
128538      int jj;
128539      float o = 1.0;
128540      for(jj=0; jj<(pRtree->nDim*2); jj+=2){
128541        double x1;
128542        double x2;
128543
128544        x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
128545        x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
128546
128547        if( x2<x1 ){
128548          o = 0.0;
128549          break;
128550        }else{
128551          o = o * (float)(x2-x1);
128552        }
128553      }
128554      overlap += o;
128555    }
128556  }
128557  return overlap;
128558}
128559#endif
128560
128561#if VARIANT_RSTARTREE_CHOOSESUBTREE
128562static float cellOverlapEnlargement(
128563  Rtree *pRtree,
128564  RtreeCell *p,
128565  RtreeCell *pInsert,
128566  RtreeCell *aCell,
128567  int nCell,
128568  int iExclude
128569){
128570  double before;
128571  double after;
128572  before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
128573  cellUnion(pRtree, p, pInsert);
128574  after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
128575  return (float)(after-before);
128576}
128577#endif
128578
128579
128580/*
128581** This function implements the ChooseLeaf algorithm from Gutman[84].
128582** ChooseSubTree in r*tree terminology.
128583*/
128584static int ChooseLeaf(
128585  Rtree *pRtree,               /* Rtree table */
128586  RtreeCell *pCell,            /* Cell to insert into rtree */
128587  int iHeight,                 /* Height of sub-tree rooted at pCell */
128588  RtreeNode **ppLeaf           /* OUT: Selected leaf page */
128589){
128590  int rc;
128591  int ii;
128592  RtreeNode *pNode;
128593  rc = nodeAcquire(pRtree, 1, 0, &pNode);
128594
128595  for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
128596    int iCell;
128597    sqlite3_int64 iBest = 0;
128598
128599    float fMinGrowth = 0.0;
128600    float fMinArea = 0.0;
128601    float fMinOverlap = 0.0;
128602
128603    int nCell = NCELL(pNode);
128604    RtreeCell cell;
128605    RtreeNode *pChild;
128606
128607    RtreeCell *aCell = 0;
128608
128609#if VARIANT_RSTARTREE_CHOOSESUBTREE
128610    if( ii==(pRtree->iDepth-1) ){
128611      int jj;
128612      aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
128613      if( !aCell ){
128614        rc = SQLITE_NOMEM;
128615        nodeRelease(pRtree, pNode);
128616        pNode = 0;
128617        continue;
128618      }
128619      for(jj=0; jj<nCell; jj++){
128620        nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
128621      }
128622    }
128623#endif
128624
128625    /* Select the child node which will be enlarged the least if pCell
128626    ** is inserted into it. Resolve ties by choosing the entry with
128627    ** the smallest area.
128628    */
128629    for(iCell=0; iCell<nCell; iCell++){
128630      int bBest = 0;
128631      float growth;
128632      float area;
128633      float overlap = 0.0;
128634      nodeGetCell(pRtree, pNode, iCell, &cell);
128635      growth = cellGrowth(pRtree, &cell, pCell);
128636      area = cellArea(pRtree, &cell);
128637
128638#if VARIANT_RSTARTREE_CHOOSESUBTREE
128639      if( ii==(pRtree->iDepth-1) ){
128640        overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
128641      }
128642      if( (iCell==0)
128643       || (overlap<fMinOverlap)
128644       || (overlap==fMinOverlap && growth<fMinGrowth)
128645       || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
128646      ){
128647        bBest = 1;
128648      }
128649#else
128650      if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
128651        bBest = 1;
128652      }
128653#endif
128654      if( bBest ){
128655        fMinOverlap = overlap;
128656        fMinGrowth = growth;
128657        fMinArea = area;
128658        iBest = cell.iRowid;
128659      }
128660    }
128661
128662    sqlite3_free(aCell);
128663    rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
128664    nodeRelease(pRtree, pNode);
128665    pNode = pChild;
128666  }
128667
128668  *ppLeaf = pNode;
128669  return rc;
128670}
128671
128672/*
128673** A cell with the same content as pCell has just been inserted into
128674** the node pNode. This function updates the bounding box cells in
128675** all ancestor elements.
128676*/
128677static int AdjustTree(
128678  Rtree *pRtree,                    /* Rtree table */
128679  RtreeNode *pNode,                 /* Adjust ancestry of this node. */
128680  RtreeCell *pCell                  /* This cell was just inserted */
128681){
128682  RtreeNode *p = pNode;
128683  while( p->pParent ){
128684    RtreeNode *pParent = p->pParent;
128685    RtreeCell cell;
128686    int iCell;
128687
128688    if( nodeParentIndex(pRtree, p, &iCell) ){
128689      return SQLITE_CORRUPT_VTAB;
128690    }
128691
128692    nodeGetCell(pRtree, pParent, iCell, &cell);
128693    if( !cellContains(pRtree, &cell, pCell) ){
128694      cellUnion(pRtree, &cell, pCell);
128695      nodeOverwriteCell(pRtree, pParent, &cell, iCell);
128696    }
128697
128698    p = pParent;
128699  }
128700  return SQLITE_OK;
128701}
128702
128703/*
128704** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
128705*/
128706static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
128707  sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
128708  sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
128709  sqlite3_step(pRtree->pWriteRowid);
128710  return sqlite3_reset(pRtree->pWriteRowid);
128711}
128712
128713/*
128714** Write mapping (iNode->iPar) to the <rtree>_parent table.
128715*/
128716static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
128717  sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
128718  sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
128719  sqlite3_step(pRtree->pWriteParent);
128720  return sqlite3_reset(pRtree->pWriteParent);
128721}
128722
128723static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
128724
128725#if VARIANT_GUTTMAN_LINEAR_SPLIT
128726/*
128727** Implementation of the linear variant of the PickNext() function from
128728** Guttman[84].
128729*/
128730static RtreeCell *LinearPickNext(
128731  Rtree *pRtree,
128732  RtreeCell *aCell,
128733  int nCell,
128734  RtreeCell *pLeftBox,
128735  RtreeCell *pRightBox,
128736  int *aiUsed
128737){
128738  int ii;
128739  for(ii=0; aiUsed[ii]; ii++);
128740  aiUsed[ii] = 1;
128741  return &aCell[ii];
128742}
128743
128744/*
128745** Implementation of the linear variant of the PickSeeds() function from
128746** Guttman[84].
128747*/
128748static void LinearPickSeeds(
128749  Rtree *pRtree,
128750  RtreeCell *aCell,
128751  int nCell,
128752  int *piLeftSeed,
128753  int *piRightSeed
128754){
128755  int i;
128756  int iLeftSeed = 0;
128757  int iRightSeed = 1;
128758  float maxNormalInnerWidth = 0.0;
128759
128760  /* Pick two "seed" cells from the array of cells. The algorithm used
128761  ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
128762  ** indices of the two seed cells in the array are stored in local
128763  ** variables iLeftSeek and iRightSeed.
128764  */
128765  for(i=0; i<pRtree->nDim; i++){
128766    float x1 = DCOORD(aCell[0].aCoord[i*2]);
128767    float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
128768    float x3 = x1;
128769    float x4 = x2;
128770    int jj;
128771
128772    int iCellLeft = 0;
128773    int iCellRight = 0;
128774
128775    for(jj=1; jj<nCell; jj++){
128776      float left = DCOORD(aCell[jj].aCoord[i*2]);
128777      float right = DCOORD(aCell[jj].aCoord[i*2+1]);
128778
128779      if( left<x1 ) x1 = left;
128780      if( right>x4 ) x4 = right;
128781      if( left>x3 ){
128782        x3 = left;
128783        iCellRight = jj;
128784      }
128785      if( right<x2 ){
128786        x2 = right;
128787        iCellLeft = jj;
128788      }
128789    }
128790
128791    if( x4!=x1 ){
128792      float normalwidth = (x3 - x2) / (x4 - x1);
128793      if( normalwidth>maxNormalInnerWidth ){
128794        iLeftSeed = iCellLeft;
128795        iRightSeed = iCellRight;
128796      }
128797    }
128798  }
128799
128800  *piLeftSeed = iLeftSeed;
128801  *piRightSeed = iRightSeed;
128802}
128803#endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
128804
128805#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
128806/*
128807** Implementation of the quadratic variant of the PickNext() function from
128808** Guttman[84].
128809*/
128810static RtreeCell *QuadraticPickNext(
128811  Rtree *pRtree,
128812  RtreeCell *aCell,
128813  int nCell,
128814  RtreeCell *pLeftBox,
128815  RtreeCell *pRightBox,
128816  int *aiUsed
128817){
128818  #define FABS(a) ((a)<0.0?-1.0*(a):(a))
128819
128820  int iSelect = -1;
128821  float fDiff;
128822  int ii;
128823  for(ii=0; ii<nCell; ii++){
128824    if( aiUsed[ii]==0 ){
128825      float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
128826      float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
128827      float diff = FABS(right-left);
128828      if( iSelect<0 || diff>fDiff ){
128829        fDiff = diff;
128830        iSelect = ii;
128831      }
128832    }
128833  }
128834  aiUsed[iSelect] = 1;
128835  return &aCell[iSelect];
128836}
128837
128838/*
128839** Implementation of the quadratic variant of the PickSeeds() function from
128840** Guttman[84].
128841*/
128842static void QuadraticPickSeeds(
128843  Rtree *pRtree,
128844  RtreeCell *aCell,
128845  int nCell,
128846  int *piLeftSeed,
128847  int *piRightSeed
128848){
128849  int ii;
128850  int jj;
128851
128852  int iLeftSeed = 0;
128853  int iRightSeed = 1;
128854  float fWaste = 0.0;
128855
128856  for(ii=0; ii<nCell; ii++){
128857    for(jj=ii+1; jj<nCell; jj++){
128858      float right = cellArea(pRtree, &aCell[jj]);
128859      float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
128860      float waste = growth - right;
128861
128862      if( waste>fWaste ){
128863        iLeftSeed = ii;
128864        iRightSeed = jj;
128865        fWaste = waste;
128866      }
128867    }
128868  }
128869
128870  *piLeftSeed = iLeftSeed;
128871  *piRightSeed = iRightSeed;
128872}
128873#endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
128874
128875/*
128876** Arguments aIdx, aDistance and aSpare all point to arrays of size
128877** nIdx. The aIdx array contains the set of integers from 0 to
128878** (nIdx-1) in no particular order. This function sorts the values
128879** in aIdx according to the indexed values in aDistance. For
128880** example, assuming the inputs:
128881**
128882**   aIdx      = { 0,   1,   2,   3 }
128883**   aDistance = { 5.0, 2.0, 7.0, 6.0 }
128884**
128885** this function sets the aIdx array to contain:
128886**
128887**   aIdx      = { 0,   1,   2,   3 }
128888**
128889** The aSpare array is used as temporary working space by the
128890** sorting algorithm.
128891*/
128892static void SortByDistance(
128893  int *aIdx,
128894  int nIdx,
128895  float *aDistance,
128896  int *aSpare
128897){
128898  if( nIdx>1 ){
128899    int iLeft = 0;
128900    int iRight = 0;
128901
128902    int nLeft = nIdx/2;
128903    int nRight = nIdx-nLeft;
128904    int *aLeft = aIdx;
128905    int *aRight = &aIdx[nLeft];
128906
128907    SortByDistance(aLeft, nLeft, aDistance, aSpare);
128908    SortByDistance(aRight, nRight, aDistance, aSpare);
128909
128910    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
128911    aLeft = aSpare;
128912
128913    while( iLeft<nLeft || iRight<nRight ){
128914      if( iLeft==nLeft ){
128915        aIdx[iLeft+iRight] = aRight[iRight];
128916        iRight++;
128917      }else if( iRight==nRight ){
128918        aIdx[iLeft+iRight] = aLeft[iLeft];
128919        iLeft++;
128920      }else{
128921        float fLeft = aDistance[aLeft[iLeft]];
128922        float fRight = aDistance[aRight[iRight]];
128923        if( fLeft<fRight ){
128924          aIdx[iLeft+iRight] = aLeft[iLeft];
128925          iLeft++;
128926        }else{
128927          aIdx[iLeft+iRight] = aRight[iRight];
128928          iRight++;
128929        }
128930      }
128931    }
128932
128933#if 0
128934    /* Check that the sort worked */
128935    {
128936      int jj;
128937      for(jj=1; jj<nIdx; jj++){
128938        float left = aDistance[aIdx[jj-1]];
128939        float right = aDistance[aIdx[jj]];
128940        assert( left<=right );
128941      }
128942    }
128943#endif
128944  }
128945}
128946
128947/*
128948** Arguments aIdx, aCell and aSpare all point to arrays of size
128949** nIdx. The aIdx array contains the set of integers from 0 to
128950** (nIdx-1) in no particular order. This function sorts the values
128951** in aIdx according to dimension iDim of the cells in aCell. The
128952** minimum value of dimension iDim is considered first, the
128953** maximum used to break ties.
128954**
128955** The aSpare array is used as temporary working space by the
128956** sorting algorithm.
128957*/
128958static void SortByDimension(
128959  Rtree *pRtree,
128960  int *aIdx,
128961  int nIdx,
128962  int iDim,
128963  RtreeCell *aCell,
128964  int *aSpare
128965){
128966  if( nIdx>1 ){
128967
128968    int iLeft = 0;
128969    int iRight = 0;
128970
128971    int nLeft = nIdx/2;
128972    int nRight = nIdx-nLeft;
128973    int *aLeft = aIdx;
128974    int *aRight = &aIdx[nLeft];
128975
128976    SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
128977    SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
128978
128979    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
128980    aLeft = aSpare;
128981    while( iLeft<nLeft || iRight<nRight ){
128982      double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
128983      double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
128984      double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
128985      double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
128986      if( (iLeft!=nLeft) && ((iRight==nRight)
128987       || (xleft1<xright1)
128988       || (xleft1==xright1 && xleft2<xright2)
128989      )){
128990        aIdx[iLeft+iRight] = aLeft[iLeft];
128991        iLeft++;
128992      }else{
128993        aIdx[iLeft+iRight] = aRight[iRight];
128994        iRight++;
128995      }
128996    }
128997
128998#if 0
128999    /* Check that the sort worked */
129000    {
129001      int jj;
129002      for(jj=1; jj<nIdx; jj++){
129003        float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
129004        float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
129005        float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
129006        float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
129007        assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
129008      }
129009    }
129010#endif
129011  }
129012}
129013
129014#if VARIANT_RSTARTREE_SPLIT
129015/*
129016** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
129017*/
129018static int splitNodeStartree(
129019  Rtree *pRtree,
129020  RtreeCell *aCell,
129021  int nCell,
129022  RtreeNode *pLeft,
129023  RtreeNode *pRight,
129024  RtreeCell *pBboxLeft,
129025  RtreeCell *pBboxRight
129026){
129027  int **aaSorted;
129028  int *aSpare;
129029  int ii;
129030
129031  int iBestDim = 0;
129032  int iBestSplit = 0;
129033  float fBestMargin = 0.0;
129034
129035  int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
129036
129037  aaSorted = (int **)sqlite3_malloc(nByte);
129038  if( !aaSorted ){
129039    return SQLITE_NOMEM;
129040  }
129041
129042  aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
129043  memset(aaSorted, 0, nByte);
129044  for(ii=0; ii<pRtree->nDim; ii++){
129045    int jj;
129046    aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
129047    for(jj=0; jj<nCell; jj++){
129048      aaSorted[ii][jj] = jj;
129049    }
129050    SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
129051  }
129052
129053  for(ii=0; ii<pRtree->nDim; ii++){
129054    float margin = 0.0;
129055    float fBestOverlap = 0.0;
129056    float fBestArea = 0.0;
129057    int iBestLeft = 0;
129058    int nLeft;
129059
129060    for(
129061      nLeft=RTREE_MINCELLS(pRtree);
129062      nLeft<=(nCell-RTREE_MINCELLS(pRtree));
129063      nLeft++
129064    ){
129065      RtreeCell left;
129066      RtreeCell right;
129067      int kk;
129068      float overlap;
129069      float area;
129070
129071      memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
129072      memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
129073      for(kk=1; kk<(nCell-1); kk++){
129074        if( kk<nLeft ){
129075          cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
129076        }else{
129077          cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
129078        }
129079      }
129080      margin += cellMargin(pRtree, &left);
129081      margin += cellMargin(pRtree, &right);
129082      overlap = cellOverlap(pRtree, &left, &right, 1, -1);
129083      area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
129084      if( (nLeft==RTREE_MINCELLS(pRtree))
129085       || (overlap<fBestOverlap)
129086       || (overlap==fBestOverlap && area<fBestArea)
129087      ){
129088        iBestLeft = nLeft;
129089        fBestOverlap = overlap;
129090        fBestArea = area;
129091      }
129092    }
129093
129094    if( ii==0 || margin<fBestMargin ){
129095      iBestDim = ii;
129096      fBestMargin = margin;
129097      iBestSplit = iBestLeft;
129098    }
129099  }
129100
129101  memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
129102  memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
129103  for(ii=0; ii<nCell; ii++){
129104    RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
129105    RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
129106    RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
129107    nodeInsertCell(pRtree, pTarget, pCell);
129108    cellUnion(pRtree, pBbox, pCell);
129109  }
129110
129111  sqlite3_free(aaSorted);
129112  return SQLITE_OK;
129113}
129114#endif
129115
129116#if VARIANT_GUTTMAN_SPLIT
129117/*
129118** Implementation of the regular R-tree SplitNode from Guttman[1984].
129119*/
129120static int splitNodeGuttman(
129121  Rtree *pRtree,
129122  RtreeCell *aCell,
129123  int nCell,
129124  RtreeNode *pLeft,
129125  RtreeNode *pRight,
129126  RtreeCell *pBboxLeft,
129127  RtreeCell *pBboxRight
129128){
129129  int iLeftSeed = 0;
129130  int iRightSeed = 1;
129131  int *aiUsed;
129132  int i;
129133
129134  aiUsed = sqlite3_malloc(sizeof(int)*nCell);
129135  if( !aiUsed ){
129136    return SQLITE_NOMEM;
129137  }
129138  memset(aiUsed, 0, sizeof(int)*nCell);
129139
129140  PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
129141
129142  memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
129143  memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
129144  nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
129145  nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
129146  aiUsed[iLeftSeed] = 1;
129147  aiUsed[iRightSeed] = 1;
129148
129149  for(i=nCell-2; i>0; i--){
129150    RtreeCell *pNext;
129151    pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
129152    float diff =
129153      cellGrowth(pRtree, pBboxLeft, pNext) -
129154      cellGrowth(pRtree, pBboxRight, pNext)
129155    ;
129156    if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
129157     || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
129158    ){
129159      nodeInsertCell(pRtree, pRight, pNext);
129160      cellUnion(pRtree, pBboxRight, pNext);
129161    }else{
129162      nodeInsertCell(pRtree, pLeft, pNext);
129163      cellUnion(pRtree, pBboxLeft, pNext);
129164    }
129165  }
129166
129167  sqlite3_free(aiUsed);
129168  return SQLITE_OK;
129169}
129170#endif
129171
129172static int updateMapping(
129173  Rtree *pRtree,
129174  i64 iRowid,
129175  RtreeNode *pNode,
129176  int iHeight
129177){
129178  int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
129179  xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
129180  if( iHeight>0 ){
129181    RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
129182    if( pChild ){
129183      nodeRelease(pRtree, pChild->pParent);
129184      nodeReference(pNode);
129185      pChild->pParent = pNode;
129186    }
129187  }
129188  return xSetMapping(pRtree, iRowid, pNode->iNode);
129189}
129190
129191static int SplitNode(
129192  Rtree *pRtree,
129193  RtreeNode *pNode,
129194  RtreeCell *pCell,
129195  int iHeight
129196){
129197  int i;
129198  int newCellIsRight = 0;
129199
129200  int rc = SQLITE_OK;
129201  int nCell = NCELL(pNode);
129202  RtreeCell *aCell;
129203  int *aiUsed;
129204
129205  RtreeNode *pLeft = 0;
129206  RtreeNode *pRight = 0;
129207
129208  RtreeCell leftbbox;
129209  RtreeCell rightbbox;
129210
129211  /* Allocate an array and populate it with a copy of pCell and
129212  ** all cells from node pLeft. Then zero the original node.
129213  */
129214  aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
129215  if( !aCell ){
129216    rc = SQLITE_NOMEM;
129217    goto splitnode_out;
129218  }
129219  aiUsed = (int *)&aCell[nCell+1];
129220  memset(aiUsed, 0, sizeof(int)*(nCell+1));
129221  for(i=0; i<nCell; i++){
129222    nodeGetCell(pRtree, pNode, i, &aCell[i]);
129223  }
129224  nodeZero(pRtree, pNode);
129225  memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
129226  nCell++;
129227
129228  if( pNode->iNode==1 ){
129229    pRight = nodeNew(pRtree, pNode);
129230    pLeft = nodeNew(pRtree, pNode);
129231    pRtree->iDepth++;
129232    pNode->isDirty = 1;
129233    writeInt16(pNode->zData, pRtree->iDepth);
129234  }else{
129235    pLeft = pNode;
129236    pRight = nodeNew(pRtree, pLeft->pParent);
129237    nodeReference(pLeft);
129238  }
129239
129240  if( !pLeft || !pRight ){
129241    rc = SQLITE_NOMEM;
129242    goto splitnode_out;
129243  }
129244
129245  memset(pLeft->zData, 0, pRtree->iNodeSize);
129246  memset(pRight->zData, 0, pRtree->iNodeSize);
129247
129248  rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
129249  if( rc!=SQLITE_OK ){
129250    goto splitnode_out;
129251  }
129252
129253  /* Ensure both child nodes have node numbers assigned to them by calling
129254  ** nodeWrite(). Node pRight always needs a node number, as it was created
129255  ** by nodeNew() above. But node pLeft sometimes already has a node number.
129256  ** In this case avoid the all to nodeWrite().
129257  */
129258  if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
129259   || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
129260  ){
129261    goto splitnode_out;
129262  }
129263
129264  rightbbox.iRowid = pRight->iNode;
129265  leftbbox.iRowid = pLeft->iNode;
129266
129267  if( pNode->iNode==1 ){
129268    rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
129269    if( rc!=SQLITE_OK ){
129270      goto splitnode_out;
129271    }
129272  }else{
129273    RtreeNode *pParent = pLeft->pParent;
129274    int iCell;
129275    rc = nodeParentIndex(pRtree, pLeft, &iCell);
129276    if( rc==SQLITE_OK ){
129277      nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
129278      rc = AdjustTree(pRtree, pParent, &leftbbox);
129279    }
129280    if( rc!=SQLITE_OK ){
129281      goto splitnode_out;
129282    }
129283  }
129284  if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
129285    goto splitnode_out;
129286  }
129287
129288  for(i=0; i<NCELL(pRight); i++){
129289    i64 iRowid = nodeGetRowid(pRtree, pRight, i);
129290    rc = updateMapping(pRtree, iRowid, pRight, iHeight);
129291    if( iRowid==pCell->iRowid ){
129292      newCellIsRight = 1;
129293    }
129294    if( rc!=SQLITE_OK ){
129295      goto splitnode_out;
129296    }
129297  }
129298  if( pNode->iNode==1 ){
129299    for(i=0; i<NCELL(pLeft); i++){
129300      i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
129301      rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
129302      if( rc!=SQLITE_OK ){
129303        goto splitnode_out;
129304      }
129305    }
129306  }else if( newCellIsRight==0 ){
129307    rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
129308  }
129309
129310  if( rc==SQLITE_OK ){
129311    rc = nodeRelease(pRtree, pRight);
129312    pRight = 0;
129313  }
129314  if( rc==SQLITE_OK ){
129315    rc = nodeRelease(pRtree, pLeft);
129316    pLeft = 0;
129317  }
129318
129319splitnode_out:
129320  nodeRelease(pRtree, pRight);
129321  nodeRelease(pRtree, pLeft);
129322  sqlite3_free(aCell);
129323  return rc;
129324}
129325
129326/*
129327** If node pLeaf is not the root of the r-tree and its pParent pointer is
129328** still NULL, load all ancestor nodes of pLeaf into memory and populate
129329** the pLeaf->pParent chain all the way up to the root node.
129330**
129331** This operation is required when a row is deleted (or updated - an update
129332** is implemented as a delete followed by an insert). SQLite provides the
129333** rowid of the row to delete, which can be used to find the leaf on which
129334** the entry resides (argument pLeaf). Once the leaf is located, this
129335** function is called to determine its ancestry.
129336*/
129337static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
129338  int rc = SQLITE_OK;
129339  RtreeNode *pChild = pLeaf;
129340  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
129341    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
129342    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
129343    rc = sqlite3_step(pRtree->pReadParent);
129344    if( rc==SQLITE_ROW ){
129345      RtreeNode *pTest;           /* Used to test for reference loops */
129346      i64 iNode;                  /* Node number of parent node */
129347
129348      /* Before setting pChild->pParent, test that we are not creating a
129349      ** loop of references (as we would if, say, pChild==pParent). We don't
129350      ** want to do this as it leads to a memory leak when trying to delete
129351      ** the referenced counted node structures.
129352      */
129353      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
129354      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
129355      if( !pTest ){
129356        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
129357      }
129358    }
129359    rc = sqlite3_reset(pRtree->pReadParent);
129360    if( rc==SQLITE_OK ) rc = rc2;
129361    if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
129362    pChild = pChild->pParent;
129363  }
129364  return rc;
129365}
129366
129367static int deleteCell(Rtree *, RtreeNode *, int, int);
129368
129369static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
129370  int rc;
129371  int rc2;
129372  RtreeNode *pParent = 0;
129373  int iCell;
129374
129375  assert( pNode->nRef==1 );
129376
129377  /* Remove the entry in the parent cell. */
129378  rc = nodeParentIndex(pRtree, pNode, &iCell);
129379  if( rc==SQLITE_OK ){
129380    pParent = pNode->pParent;
129381    pNode->pParent = 0;
129382    rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
129383  }
129384  rc2 = nodeRelease(pRtree, pParent);
129385  if( rc==SQLITE_OK ){
129386    rc = rc2;
129387  }
129388  if( rc!=SQLITE_OK ){
129389    return rc;
129390  }
129391
129392  /* Remove the xxx_node entry. */
129393  sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
129394  sqlite3_step(pRtree->pDeleteNode);
129395  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
129396    return rc;
129397  }
129398
129399  /* Remove the xxx_parent entry. */
129400  sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
129401  sqlite3_step(pRtree->pDeleteParent);
129402  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
129403    return rc;
129404  }
129405
129406  /* Remove the node from the in-memory hash table and link it into
129407  ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
129408  */
129409  nodeHashDelete(pRtree, pNode);
129410  pNode->iNode = iHeight;
129411  pNode->pNext = pRtree->pDeleted;
129412  pNode->nRef++;
129413  pRtree->pDeleted = pNode;
129414
129415  return SQLITE_OK;
129416}
129417
129418static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
129419  RtreeNode *pParent = pNode->pParent;
129420  int rc = SQLITE_OK;
129421  if( pParent ){
129422    int ii;
129423    int nCell = NCELL(pNode);
129424    RtreeCell box;                            /* Bounding box for pNode */
129425    nodeGetCell(pRtree, pNode, 0, &box);
129426    for(ii=1; ii<nCell; ii++){
129427      RtreeCell cell;
129428      nodeGetCell(pRtree, pNode, ii, &cell);
129429      cellUnion(pRtree, &box, &cell);
129430    }
129431    box.iRowid = pNode->iNode;
129432    rc = nodeParentIndex(pRtree, pNode, &ii);
129433    if( rc==SQLITE_OK ){
129434      nodeOverwriteCell(pRtree, pParent, &box, ii);
129435      rc = fixBoundingBox(pRtree, pParent);
129436    }
129437  }
129438  return rc;
129439}
129440
129441/*
129442** Delete the cell at index iCell of node pNode. After removing the
129443** cell, adjust the r-tree data structure if required.
129444*/
129445static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
129446  RtreeNode *pParent;
129447  int rc;
129448
129449  if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
129450    return rc;
129451  }
129452
129453  /* Remove the cell from the node. This call just moves bytes around
129454  ** the in-memory node image, so it cannot fail.
129455  */
129456  nodeDeleteCell(pRtree, pNode, iCell);
129457
129458  /* If the node is not the tree root and now has less than the minimum
129459  ** number of cells, remove it from the tree. Otherwise, update the
129460  ** cell in the parent node so that it tightly contains the updated
129461  ** node.
129462  */
129463  pParent = pNode->pParent;
129464  assert( pParent || pNode->iNode==1 );
129465  if( pParent ){
129466    if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
129467      rc = removeNode(pRtree, pNode, iHeight);
129468    }else{
129469      rc = fixBoundingBox(pRtree, pNode);
129470    }
129471  }
129472
129473  return rc;
129474}
129475
129476static int Reinsert(
129477  Rtree *pRtree,
129478  RtreeNode *pNode,
129479  RtreeCell *pCell,
129480  int iHeight
129481){
129482  int *aOrder;
129483  int *aSpare;
129484  RtreeCell *aCell;
129485  float *aDistance;
129486  int nCell;
129487  float aCenterCoord[RTREE_MAX_DIMENSIONS];
129488  int iDim;
129489  int ii;
129490  int rc = SQLITE_OK;
129491
129492  memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
129493
129494  nCell = NCELL(pNode)+1;
129495
129496  /* Allocate the buffers used by this operation. The allocation is
129497  ** relinquished before this function returns.
129498  */
129499  aCell = (RtreeCell *)sqlite3_malloc(nCell * (
129500    sizeof(RtreeCell) +         /* aCell array */
129501    sizeof(int)       +         /* aOrder array */
129502    sizeof(int)       +         /* aSpare array */
129503    sizeof(float)               /* aDistance array */
129504  ));
129505  if( !aCell ){
129506    return SQLITE_NOMEM;
129507  }
129508  aOrder    = (int *)&aCell[nCell];
129509  aSpare    = (int *)&aOrder[nCell];
129510  aDistance = (float *)&aSpare[nCell];
129511
129512  for(ii=0; ii<nCell; ii++){
129513    if( ii==(nCell-1) ){
129514      memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
129515    }else{
129516      nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
129517    }
129518    aOrder[ii] = ii;
129519    for(iDim=0; iDim<pRtree->nDim; iDim++){
129520      aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
129521      aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
129522    }
129523  }
129524  for(iDim=0; iDim<pRtree->nDim; iDim++){
129525    aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
129526  }
129527
129528  for(ii=0; ii<nCell; ii++){
129529    aDistance[ii] = 0.0;
129530    for(iDim=0; iDim<pRtree->nDim; iDim++){
129531      float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) -
129532          DCOORD(aCell[ii].aCoord[iDim*2]));
129533      aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
129534    }
129535  }
129536
129537  SortByDistance(aOrder, nCell, aDistance, aSpare);
129538  nodeZero(pRtree, pNode);
129539
129540  for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
129541    RtreeCell *p = &aCell[aOrder[ii]];
129542    nodeInsertCell(pRtree, pNode, p);
129543    if( p->iRowid==pCell->iRowid ){
129544      if( iHeight==0 ){
129545        rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
129546      }else{
129547        rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
129548      }
129549    }
129550  }
129551  if( rc==SQLITE_OK ){
129552    rc = fixBoundingBox(pRtree, pNode);
129553  }
129554  for(; rc==SQLITE_OK && ii<nCell; ii++){
129555    /* Find a node to store this cell in. pNode->iNode currently contains
129556    ** the height of the sub-tree headed by the cell.
129557    */
129558    RtreeNode *pInsert;
129559    RtreeCell *p = &aCell[aOrder[ii]];
129560    rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
129561    if( rc==SQLITE_OK ){
129562      int rc2;
129563      rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
129564      rc2 = nodeRelease(pRtree, pInsert);
129565      if( rc==SQLITE_OK ){
129566        rc = rc2;
129567      }
129568    }
129569  }
129570
129571  sqlite3_free(aCell);
129572  return rc;
129573}
129574
129575/*
129576** Insert cell pCell into node pNode. Node pNode is the head of a
129577** subtree iHeight high (leaf nodes have iHeight==0).
129578*/
129579static int rtreeInsertCell(
129580  Rtree *pRtree,
129581  RtreeNode *pNode,
129582  RtreeCell *pCell,
129583  int iHeight
129584){
129585  int rc = SQLITE_OK;
129586  if( iHeight>0 ){
129587    RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
129588    if( pChild ){
129589      nodeRelease(pRtree, pChild->pParent);
129590      nodeReference(pNode);
129591      pChild->pParent = pNode;
129592    }
129593  }
129594  if( nodeInsertCell(pRtree, pNode, pCell) ){
129595#if VARIANT_RSTARTREE_REINSERT
129596    if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
129597      rc = SplitNode(pRtree, pNode, pCell, iHeight);
129598    }else{
129599      pRtree->iReinsertHeight = iHeight;
129600      rc = Reinsert(pRtree, pNode, pCell, iHeight);
129601    }
129602#else
129603    rc = SplitNode(pRtree, pNode, pCell, iHeight);
129604#endif
129605  }else{
129606    rc = AdjustTree(pRtree, pNode, pCell);
129607    if( rc==SQLITE_OK ){
129608      if( iHeight==0 ){
129609        rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
129610      }else{
129611        rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
129612      }
129613    }
129614  }
129615  return rc;
129616}
129617
129618static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
129619  int ii;
129620  int rc = SQLITE_OK;
129621  int nCell = NCELL(pNode);
129622
129623  for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
129624    RtreeNode *pInsert;
129625    RtreeCell cell;
129626    nodeGetCell(pRtree, pNode, ii, &cell);
129627
129628    /* Find a node to store this cell in. pNode->iNode currently contains
129629    ** the height of the sub-tree headed by the cell.
129630    */
129631    rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
129632    if( rc==SQLITE_OK ){
129633      int rc2;
129634      rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
129635      rc2 = nodeRelease(pRtree, pInsert);
129636      if( rc==SQLITE_OK ){
129637        rc = rc2;
129638      }
129639    }
129640  }
129641  return rc;
129642}
129643
129644/*
129645** Select a currently unused rowid for a new r-tree record.
129646*/
129647static int newRowid(Rtree *pRtree, i64 *piRowid){
129648  int rc;
129649  sqlite3_bind_null(pRtree->pWriteRowid, 1);
129650  sqlite3_bind_null(pRtree->pWriteRowid, 2);
129651  sqlite3_step(pRtree->pWriteRowid);
129652  rc = sqlite3_reset(pRtree->pWriteRowid);
129653  *piRowid = sqlite3_last_insert_rowid(pRtree->db);
129654  return rc;
129655}
129656
129657/*
129658** Remove the entry with rowid=iDelete from the r-tree structure.
129659*/
129660static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
129661  int rc;                         /* Return code */
129662  RtreeNode *pLeaf;               /* Leaf node containing record iDelete */
129663  int iCell;                      /* Index of iDelete cell in pLeaf */
129664  RtreeNode *pRoot;               /* Root node of rtree structure */
129665
129666
129667  /* Obtain a reference to the root node to initialise Rtree.iDepth */
129668  rc = nodeAcquire(pRtree, 1, 0, &pRoot);
129669
129670  /* Obtain a reference to the leaf node that contains the entry
129671  ** about to be deleted.
129672  */
129673  if( rc==SQLITE_OK ){
129674    rc = findLeafNode(pRtree, iDelete, &pLeaf);
129675  }
129676
129677  /* Delete the cell in question from the leaf node. */
129678  if( rc==SQLITE_OK ){
129679    int rc2;
129680    rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
129681    if( rc==SQLITE_OK ){
129682      rc = deleteCell(pRtree, pLeaf, iCell, 0);
129683    }
129684    rc2 = nodeRelease(pRtree, pLeaf);
129685    if( rc==SQLITE_OK ){
129686      rc = rc2;
129687    }
129688  }
129689
129690  /* Delete the corresponding entry in the <rtree>_rowid table. */
129691  if( rc==SQLITE_OK ){
129692    sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
129693    sqlite3_step(pRtree->pDeleteRowid);
129694    rc = sqlite3_reset(pRtree->pDeleteRowid);
129695  }
129696
129697  /* Check if the root node now has exactly one child. If so, remove
129698  ** it, schedule the contents of the child for reinsertion and
129699  ** reduce the tree height by one.
129700  **
129701  ** This is equivalent to copying the contents of the child into
129702  ** the root node (the operation that Gutman's paper says to perform
129703  ** in this scenario).
129704  */
129705  if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
129706    int rc2;
129707    RtreeNode *pChild;
129708    i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
129709    rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
129710    if( rc==SQLITE_OK ){
129711      rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
129712    }
129713    rc2 = nodeRelease(pRtree, pChild);
129714    if( rc==SQLITE_OK ) rc = rc2;
129715    if( rc==SQLITE_OK ){
129716      pRtree->iDepth--;
129717      writeInt16(pRoot->zData, pRtree->iDepth);
129718      pRoot->isDirty = 1;
129719    }
129720  }
129721
129722  /* Re-insert the contents of any underfull nodes removed from the tree. */
129723  for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
129724    if( rc==SQLITE_OK ){
129725      rc = reinsertNodeContent(pRtree, pLeaf);
129726    }
129727    pRtree->pDeleted = pLeaf->pNext;
129728    sqlite3_free(pLeaf);
129729  }
129730
129731  /* Release the reference to the root node. */
129732  if( rc==SQLITE_OK ){
129733    rc = nodeRelease(pRtree, pRoot);
129734  }else{
129735    nodeRelease(pRtree, pRoot);
129736  }
129737
129738  return rc;
129739}
129740
129741/*
129742** The xUpdate method for rtree module virtual tables.
129743*/
129744static int rtreeUpdate(
129745  sqlite3_vtab *pVtab,
129746  int nData,
129747  sqlite3_value **azData,
129748  sqlite_int64 *pRowid
129749){
129750  Rtree *pRtree = (Rtree *)pVtab;
129751  int rc = SQLITE_OK;
129752  RtreeCell cell;                 /* New cell to insert if nData>1 */
129753  int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
129754
129755  rtreeReference(pRtree);
129756  assert(nData>=1);
129757
129758  /* Constraint handling. A write operation on an r-tree table may return
129759  ** SQLITE_CONSTRAINT for two reasons:
129760  **
129761  **   1. A duplicate rowid value, or
129762  **   2. The supplied data violates the "x2>=x1" constraint.
129763  **
129764  ** In the first case, if the conflict-handling mode is REPLACE, then
129765  ** the conflicting row can be removed before proceeding. In the second
129766  ** case, SQLITE_CONSTRAINT must be returned regardless of the
129767  ** conflict-handling mode specified by the user.
129768  */
129769  if( nData>1 ){
129770    int ii;
129771
129772    /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
129773    assert( nData==(pRtree->nDim*2 + 3) );
129774    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
129775      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
129776        cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
129777        cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
129778        if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
129779          rc = SQLITE_CONSTRAINT;
129780          goto constraint;
129781        }
129782      }
129783    }else{
129784      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
129785        cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
129786        cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
129787        if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
129788          rc = SQLITE_CONSTRAINT;
129789          goto constraint;
129790        }
129791      }
129792    }
129793
129794    /* If a rowid value was supplied, check if it is already present in
129795    ** the table. If so, the constraint has failed. */
129796    if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
129797      cell.iRowid = sqlite3_value_int64(azData[2]);
129798      if( sqlite3_value_type(azData[0])==SQLITE_NULL
129799       || sqlite3_value_int64(azData[0])!=cell.iRowid
129800      ){
129801        int steprc;
129802        sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
129803        steprc = sqlite3_step(pRtree->pReadRowid);
129804        rc = sqlite3_reset(pRtree->pReadRowid);
129805        if( SQLITE_ROW==steprc ){
129806          if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
129807            rc = rtreeDeleteRowid(pRtree, cell.iRowid);
129808          }else{
129809            rc = SQLITE_CONSTRAINT;
129810            goto constraint;
129811          }
129812        }
129813      }
129814      bHaveRowid = 1;
129815    }
129816  }
129817
129818  /* If azData[0] is not an SQL NULL value, it is the rowid of a
129819  ** record to delete from the r-tree table. The following block does
129820  ** just that.
129821  */
129822  if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
129823    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
129824  }
129825
129826  /* If the azData[] array contains more than one element, elements
129827  ** (azData[2]..azData[argc-1]) contain a new record to insert into
129828  ** the r-tree structure.
129829  */
129830  if( rc==SQLITE_OK && nData>1 ){
129831    /* Insert the new record into the r-tree */
129832    RtreeNode *pLeaf;
129833
129834    /* Figure out the rowid of the new row. */
129835    if( bHaveRowid==0 ){
129836      rc = newRowid(pRtree, &cell.iRowid);
129837    }
129838    *pRowid = cell.iRowid;
129839
129840    if( rc==SQLITE_OK ){
129841      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
129842    }
129843    if( rc==SQLITE_OK ){
129844      int rc2;
129845      pRtree->iReinsertHeight = -1;
129846      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
129847      rc2 = nodeRelease(pRtree, pLeaf);
129848      if( rc==SQLITE_OK ){
129849        rc = rc2;
129850      }
129851    }
129852  }
129853
129854constraint:
129855  rtreeRelease(pRtree);
129856  return rc;
129857}
129858
129859/*
129860** The xRename method for rtree module virtual tables.
129861*/
129862static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
129863  Rtree *pRtree = (Rtree *)pVtab;
129864  int rc = SQLITE_NOMEM;
129865  char *zSql = sqlite3_mprintf(
129866    "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
129867    "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
129868    "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
129869    , pRtree->zDb, pRtree->zName, zNewName
129870    , pRtree->zDb, pRtree->zName, zNewName
129871    , pRtree->zDb, pRtree->zName, zNewName
129872  );
129873  if( zSql ){
129874    rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
129875    sqlite3_free(zSql);
129876  }
129877  return rc;
129878}
129879
129880static sqlite3_module rtreeModule = {
129881  0,                          /* iVersion */
129882  rtreeCreate,                /* xCreate - create a table */
129883  rtreeConnect,               /* xConnect - connect to an existing table */
129884  rtreeBestIndex,             /* xBestIndex - Determine search strategy */
129885  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
129886  rtreeDestroy,               /* xDestroy - Drop a table */
129887  rtreeOpen,                  /* xOpen - open a cursor */
129888  rtreeClose,                 /* xClose - close a cursor */
129889  rtreeFilter,                /* xFilter - configure scan constraints */
129890  rtreeNext,                  /* xNext - advance a cursor */
129891  rtreeEof,                   /* xEof */
129892  rtreeColumn,                /* xColumn - read data */
129893  rtreeRowid,                 /* xRowid - read data */
129894  rtreeUpdate,                /* xUpdate - write data */
129895  0,                          /* xBegin - begin transaction */
129896  0,                          /* xSync - sync transaction */
129897  0,                          /* xCommit - commit transaction */
129898  0,                          /* xRollback - rollback transaction */
129899  0,                          /* xFindFunction - function overloading */
129900  rtreeRename,                /* xRename - rename the table */
129901  0,                          /* xSavepoint */
129902  0,                          /* xRelease */
129903  0                           /* xRollbackTo */
129904};
129905
129906static int rtreeSqlInit(
129907  Rtree *pRtree,
129908  sqlite3 *db,
129909  const char *zDb,
129910  const char *zPrefix,
129911  int isCreate
129912){
129913  int rc = SQLITE_OK;
129914
129915  #define N_STATEMENT 9
129916  static const char *azSql[N_STATEMENT] = {
129917    /* Read and write the xxx_node table */
129918    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
129919    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
129920    "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
129921
129922    /* Read and write the xxx_rowid table */
129923    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
129924    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
129925    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
129926
129927    /* Read and write the xxx_parent table */
129928    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
129929    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
129930    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
129931  };
129932  sqlite3_stmt **appStmt[N_STATEMENT];
129933  int i;
129934
129935  pRtree->db = db;
129936
129937  if( isCreate ){
129938    char *zCreate = sqlite3_mprintf(
129939"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
129940"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
129941"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
129942"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
129943      zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
129944    );
129945    if( !zCreate ){
129946      return SQLITE_NOMEM;
129947    }
129948    rc = sqlite3_exec(db, zCreate, 0, 0, 0);
129949    sqlite3_free(zCreate);
129950    if( rc!=SQLITE_OK ){
129951      return rc;
129952    }
129953  }
129954
129955  appStmt[0] = &pRtree->pReadNode;
129956  appStmt[1] = &pRtree->pWriteNode;
129957  appStmt[2] = &pRtree->pDeleteNode;
129958  appStmt[3] = &pRtree->pReadRowid;
129959  appStmt[4] = &pRtree->pWriteRowid;
129960  appStmt[5] = &pRtree->pDeleteRowid;
129961  appStmt[6] = &pRtree->pReadParent;
129962  appStmt[7] = &pRtree->pWriteParent;
129963  appStmt[8] = &pRtree->pDeleteParent;
129964
129965  for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
129966    char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
129967    if( zSql ){
129968      rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
129969    }else{
129970      rc = SQLITE_NOMEM;
129971    }
129972    sqlite3_free(zSql);
129973  }
129974
129975  return rc;
129976}
129977
129978/*
129979** The second argument to this function contains the text of an SQL statement
129980** that returns a single integer value. The statement is compiled and executed
129981** using database connection db. If successful, the integer value returned
129982** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
129983** code is returned and the value of *piVal after returning is not defined.
129984*/
129985static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
129986  int rc = SQLITE_NOMEM;
129987  if( zSql ){
129988    sqlite3_stmt *pStmt = 0;
129989    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
129990    if( rc==SQLITE_OK ){
129991      if( SQLITE_ROW==sqlite3_step(pStmt) ){
129992        *piVal = sqlite3_column_int(pStmt, 0);
129993      }
129994      rc = sqlite3_finalize(pStmt);
129995    }
129996  }
129997  return rc;
129998}
129999
130000/*
130001** This function is called from within the xConnect() or xCreate() method to
130002** determine the node-size used by the rtree table being created or connected
130003** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
130004** Otherwise, an SQLite error code is returned.
130005**
130006** If this function is being called as part of an xConnect(), then the rtree
130007** table already exists. In this case the node-size is determined by inspecting
130008** the root node of the tree.
130009**
130010** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
130011** This ensures that each node is stored on a single database page. If the
130012** database page-size is so large that more than RTREE_MAXCELLS entries
130013** would fit in a single node, use a smaller node-size.
130014*/
130015static int getNodeSize(
130016  sqlite3 *db,                    /* Database handle */
130017  Rtree *pRtree,                  /* Rtree handle */
130018  int isCreate                    /* True for xCreate, false for xConnect */
130019){
130020  int rc;
130021  char *zSql;
130022  if( isCreate ){
130023    int iPageSize = 0;
130024    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
130025    rc = getIntFromStmt(db, zSql, &iPageSize);
130026    if( rc==SQLITE_OK ){
130027      pRtree->iNodeSize = iPageSize-64;
130028      if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
130029        pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
130030      }
130031    }
130032  }else{
130033    zSql = sqlite3_mprintf(
130034        "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
130035        pRtree->zDb, pRtree->zName
130036    );
130037    rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
130038  }
130039
130040  sqlite3_free(zSql);
130041  return rc;
130042}
130043
130044/*
130045** This function is the implementation of both the xConnect and xCreate
130046** methods of the r-tree virtual table.
130047**
130048**   argv[0]   -> module name
130049**   argv[1]   -> database name
130050**   argv[2]   -> table name
130051**   argv[...] -> column names...
130052*/
130053static int rtreeInit(
130054  sqlite3 *db,                        /* Database connection */
130055  void *pAux,                         /* One of the RTREE_COORD_* constants */
130056  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
130057  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
130058  char **pzErr,                       /* OUT: Error message, if any */
130059  int isCreate                        /* True for xCreate, false for xConnect */
130060){
130061  int rc = SQLITE_OK;
130062  Rtree *pRtree;
130063  int nDb;              /* Length of string argv[1] */
130064  int nName;            /* Length of string argv[2] */
130065  int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
130066
130067  const char *aErrMsg[] = {
130068    0,                                                    /* 0 */
130069    "Wrong number of columns for an rtree table",         /* 1 */
130070    "Too few columns for an rtree table",                 /* 2 */
130071    "Too many columns for an rtree table"                 /* 3 */
130072  };
130073
130074  int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
130075  if( aErrMsg[iErr] ){
130076    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
130077    return SQLITE_ERROR;
130078  }
130079
130080  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
130081
130082  /* Allocate the sqlite3_vtab structure */
130083  nDb = strlen(argv[1]);
130084  nName = strlen(argv[2]);
130085  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
130086  if( !pRtree ){
130087    return SQLITE_NOMEM;
130088  }
130089  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
130090  pRtree->nBusy = 1;
130091  pRtree->base.pModule = &rtreeModule;
130092  pRtree->zDb = (char *)&pRtree[1];
130093  pRtree->zName = &pRtree->zDb[nDb+1];
130094  pRtree->nDim = (argc-4)/2;
130095  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
130096  pRtree->eCoordType = eCoordType;
130097  memcpy(pRtree->zDb, argv[1], nDb);
130098  memcpy(pRtree->zName, argv[2], nName);
130099
130100  /* Figure out the node size to use. */
130101  rc = getNodeSize(db, pRtree, isCreate);
130102
130103  /* Create/Connect to the underlying relational database schema. If
130104  ** that is successful, call sqlite3_declare_vtab() to configure
130105  ** the r-tree table schema.
130106  */
130107  if( rc==SQLITE_OK ){
130108    if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
130109      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
130110    }else{
130111      char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
130112      char *zTmp;
130113      int ii;
130114      for(ii=4; zSql && ii<argc; ii++){
130115        zTmp = zSql;
130116        zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
130117        sqlite3_free(zTmp);
130118      }
130119      if( zSql ){
130120        zTmp = zSql;
130121        zSql = sqlite3_mprintf("%s);", zTmp);
130122        sqlite3_free(zTmp);
130123      }
130124      if( !zSql ){
130125        rc = SQLITE_NOMEM;
130126      }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
130127        *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
130128      }
130129      sqlite3_free(zSql);
130130    }
130131  }
130132
130133  if( rc==SQLITE_OK ){
130134    *ppVtab = (sqlite3_vtab *)pRtree;
130135  }else{
130136    rtreeRelease(pRtree);
130137  }
130138  return rc;
130139}
130140
130141
130142/*
130143** Implementation of a scalar function that decodes r-tree nodes to
130144** human readable strings. This can be used for debugging and analysis.
130145**
130146** The scalar function takes two arguments, a blob of data containing
130147** an r-tree node, and the number of dimensions the r-tree indexes.
130148** For a two-dimensional r-tree structure called "rt", to deserialize
130149** all nodes, a statement like:
130150**
130151**   SELECT rtreenode(2, data) FROM rt_node;
130152**
130153** The human readable string takes the form of a Tcl list with one
130154** entry for each cell in the r-tree node. Each entry is itself a
130155** list, containing the 8-byte rowid/pageno followed by the
130156** <num-dimension>*2 coordinates.
130157*/
130158static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
130159  char *zText = 0;
130160  RtreeNode node;
130161  Rtree tree;
130162  int ii;
130163
130164  UNUSED_PARAMETER(nArg);
130165  memset(&node, 0, sizeof(RtreeNode));
130166  memset(&tree, 0, sizeof(Rtree));
130167  tree.nDim = sqlite3_value_int(apArg[0]);
130168  tree.nBytesPerCell = 8 + 8 * tree.nDim;
130169  node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
130170
130171  for(ii=0; ii<NCELL(&node); ii++){
130172    char zCell[512];
130173    int nCell = 0;
130174    RtreeCell cell;
130175    int jj;
130176
130177    nodeGetCell(&tree, &node, ii, &cell);
130178    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
130179    nCell = strlen(zCell);
130180    for(jj=0; jj<tree.nDim*2; jj++){
130181      sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
130182      nCell = strlen(zCell);
130183    }
130184
130185    if( zText ){
130186      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
130187      sqlite3_free(zText);
130188      zText = zTextNew;
130189    }else{
130190      zText = sqlite3_mprintf("{%s}", zCell);
130191    }
130192  }
130193
130194  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
130195}
130196
130197static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
130198  UNUSED_PARAMETER(nArg);
130199  if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
130200   || sqlite3_value_bytes(apArg[0])<2
130201  ){
130202    sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
130203  }else{
130204    u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
130205    sqlite3_result_int(ctx, readInt16(zBlob));
130206  }
130207}
130208
130209/*
130210** Register the r-tree module with database handle db. This creates the
130211** virtual table module "rtree" and the debugging/analysis scalar
130212** function "rtreenode".
130213*/
130214SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
130215  const int utf8 = SQLITE_UTF8;
130216  int rc;
130217
130218  rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
130219  if( rc==SQLITE_OK ){
130220    rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
130221  }
130222  if( rc==SQLITE_OK ){
130223    void *c = (void *)RTREE_COORD_REAL32;
130224    rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
130225  }
130226  if( rc==SQLITE_OK ){
130227    void *c = (void *)RTREE_COORD_INT32;
130228    rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
130229  }
130230
130231  return rc;
130232}
130233
130234/*
130235** A version of sqlite3_free() that can be used as a callback. This is used
130236** in two places - as the destructor for the blob value returned by the
130237** invocation of a geometry function, and as the destructor for the geometry
130238** functions themselves.
130239*/
130240static void doSqlite3Free(void *p){
130241  sqlite3_free(p);
130242}
130243
130244/*
130245** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
130246** scalar user function. This C function is the callback used for all such
130247** registered SQL functions.
130248**
130249** The scalar user functions return a blob that is interpreted by r-tree
130250** table MATCH operators.
130251*/
130252static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
130253  RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
130254  RtreeMatchArg *pBlob;
130255  int nBlob;
130256
130257  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
130258  pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
130259  if( !pBlob ){
130260    sqlite3_result_error_nomem(ctx);
130261  }else{
130262    int i;
130263    pBlob->magic = RTREE_GEOMETRY_MAGIC;
130264    pBlob->xGeom = pGeomCtx->xGeom;
130265    pBlob->pContext = pGeomCtx->pContext;
130266    pBlob->nParam = nArg;
130267    for(i=0; i<nArg; i++){
130268      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
130269    }
130270    sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
130271  }
130272}
130273
130274/*
130275** Register a new geometry function for use with the r-tree MATCH operator.
130276*/
130277SQLITE_API int sqlite3_rtree_geometry_callback(
130278  sqlite3 *db,
130279  const char *zGeom,
130280  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
130281  void *pContext
130282){
130283  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
130284
130285  /* Allocate and populate the context object. */
130286  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
130287  if( !pGeomCtx ) return SQLITE_NOMEM;
130288  pGeomCtx->xGeom = xGeom;
130289  pGeomCtx->pContext = pContext;
130290
130291  /* Create the new user-function. Register a destructor function to delete
130292  ** the context object when it is no longer required.  */
130293  return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
130294      (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
130295  );
130296}
130297
130298#if !SQLITE_CORE
130299SQLITE_API int sqlite3_extension_init(
130300  sqlite3 *db,
130301  char **pzErrMsg,
130302  const sqlite3_api_routines *pApi
130303){
130304  SQLITE_EXTENSION_INIT2(pApi)
130305  return sqlite3RtreeInit(db);
130306}
130307#endif
130308
130309#endif
130310
130311/************** End of rtree.c ***********************************************/
130312/************** Begin file icu.c *********************************************/
130313/*
130314** 2007 May 6
130315**
130316** The author disclaims copyright to this source code.  In place of
130317** a legal notice, here is a blessing:
130318**
130319**    May you do good and not evil.
130320**    May you find forgiveness for yourself and forgive others.
130321**    May you share freely, never taking more than you give.
130322**
130323*************************************************************************
130324** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
130325**
130326** This file implements an integration between the ICU library
130327** ("International Components for Unicode", an open-source library
130328** for handling unicode data) and SQLite. The integration uses
130329** ICU to provide the following to SQLite:
130330**
130331**   * An implementation of the SQL regexp() function (and hence REGEXP
130332**     operator) using the ICU uregex_XX() APIs.
130333**
130334**   * Implementations of the SQL scalar upper() and lower() functions
130335**     for case mapping.
130336**
130337**   * Integration of ICU and SQLite collation seqences.
130338**
130339**   * An implementation of the LIKE operator that uses ICU to
130340**     provide case-independent matching.
130341*/
130342
130343#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
130344
130345/* Include ICU headers */
130346#include <unicode/utypes.h>
130347#include <unicode/uregex.h>
130348#include <unicode/ustring.h>
130349#include <unicode/ucol.h>
130350
130351/* #include <assert.h> */
130352
130353#ifndef SQLITE_CORE
130354  SQLITE_EXTENSION_INIT1
130355#else
130356#endif
130357
130358/*
130359** Maximum length (in bytes) of the pattern in a LIKE or GLOB
130360** operator.
130361*/
130362#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
130363# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
130364#endif
130365
130366/*
130367** Version of sqlite3_free() that is always a function, never a macro.
130368*/
130369static void xFree(void *p){
130370  sqlite3_free(p);
130371}
130372
130373/*
130374** Compare two UTF-8 strings for equality where the first string is
130375** a "LIKE" expression. Return true (1) if they are the same and
130376** false (0) if they are different.
130377*/
130378static int icuLikeCompare(
130379  const uint8_t *zPattern,   /* LIKE pattern */
130380  const uint8_t *zString,    /* The UTF-8 string to compare against */
130381  const UChar32 uEsc         /* The escape character */
130382){
130383  static const int MATCH_ONE = (UChar32)'_';
130384  static const int MATCH_ALL = (UChar32)'%';
130385
130386  int iPattern = 0;       /* Current byte index in zPattern */
130387  int iString = 0;        /* Current byte index in zString */
130388
130389  int prevEscape = 0;     /* True if the previous character was uEsc */
130390
130391  while( zPattern[iPattern]!=0 ){
130392
130393    /* Read (and consume) the next character from the input pattern. */
130394    UChar32 uPattern;
130395    U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
130396    assert(uPattern!=0);
130397
130398    /* There are now 4 possibilities:
130399    **
130400    **     1. uPattern is an unescaped match-all character "%",
130401    **     2. uPattern is an unescaped match-one character "_",
130402    **     3. uPattern is an unescaped escape character, or
130403    **     4. uPattern is to be handled as an ordinary character
130404    */
130405    if( !prevEscape && uPattern==MATCH_ALL ){
130406      /* Case 1. */
130407      uint8_t c;
130408
130409      /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
130410      ** MATCH_ALL. For each MATCH_ONE, skip one character in the
130411      ** test string.
130412      */
130413      while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
130414        if( c==MATCH_ONE ){
130415          if( zString[iString]==0 ) return 0;
130416          U8_FWD_1_UNSAFE(zString, iString);
130417        }
130418        iPattern++;
130419      }
130420
130421      if( zPattern[iPattern]==0 ) return 1;
130422
130423      while( zString[iString] ){
130424        if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
130425          return 1;
130426        }
130427        U8_FWD_1_UNSAFE(zString, iString);
130428      }
130429      return 0;
130430
130431    }else if( !prevEscape && uPattern==MATCH_ONE ){
130432      /* Case 2. */
130433      if( zString[iString]==0 ) return 0;
130434      U8_FWD_1_UNSAFE(zString, iString);
130435
130436    }else if( !prevEscape && uPattern==uEsc){
130437      /* Case 3. */
130438      prevEscape = 1;
130439
130440    }else{
130441      /* Case 4. */
130442      UChar32 uString;
130443      U8_NEXT_UNSAFE(zString, iString, uString);
130444      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
130445      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
130446      if( uString!=uPattern ){
130447        return 0;
130448      }
130449      prevEscape = 0;
130450    }
130451  }
130452
130453  return zString[iString]==0;
130454}
130455
130456/*
130457** Implementation of the like() SQL function.  This function implements
130458** the build-in LIKE operator.  The first argument to the function is the
130459** pattern and the second argument is the string.  So, the SQL statements:
130460**
130461**       A LIKE B
130462**
130463** is implemented as like(B, A). If there is an escape character E,
130464**
130465**       A LIKE B ESCAPE E
130466**
130467** is mapped to like(B, A, E).
130468*/
130469static void icuLikeFunc(
130470  sqlite3_context *context,
130471  int argc,
130472  sqlite3_value **argv
130473){
130474  const unsigned char *zA = sqlite3_value_text(argv[0]);
130475  const unsigned char *zB = sqlite3_value_text(argv[1]);
130476  UChar32 uEsc = 0;
130477
130478  /* Limit the length of the LIKE or GLOB pattern to avoid problems
130479  ** of deep recursion and N*N behavior in patternCompare().
130480  */
130481  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
130482    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
130483    return;
130484  }
130485
130486
130487  if( argc==3 ){
130488    /* The escape character string must consist of a single UTF-8 character.
130489    ** Otherwise, return an error.
130490    */
130491    int nE= sqlite3_value_bytes(argv[2]);
130492    const unsigned char *zE = sqlite3_value_text(argv[2]);
130493    int i = 0;
130494    if( zE==0 ) return;
130495    U8_NEXT(zE, i, nE, uEsc);
130496    if( i!=nE){
130497      sqlite3_result_error(context,
130498          "ESCAPE expression must be a single character", -1);
130499      return;
130500    }
130501  }
130502
130503  if( zA && zB ){
130504    sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
130505  }
130506}
130507
130508/*
130509** This function is called when an ICU function called from within
130510** the implementation of an SQL scalar function returns an error.
130511**
130512** The scalar function context passed as the first argument is
130513** loaded with an error message based on the following two args.
130514*/
130515static void icuFunctionError(
130516  sqlite3_context *pCtx,       /* SQLite scalar function context */
130517  const char *zName,           /* Name of ICU function that failed */
130518  UErrorCode e                 /* Error code returned by ICU function */
130519){
130520  char zBuf[128];
130521  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
130522  zBuf[127] = '\0';
130523  sqlite3_result_error(pCtx, zBuf, -1);
130524}
130525
130526/*
130527** Function to delete compiled regexp objects. Registered as
130528** a destructor function with sqlite3_set_auxdata().
130529*/
130530static void icuRegexpDelete(void *p){
130531  URegularExpression *pExpr = (URegularExpression *)p;
130532  uregex_close(pExpr);
130533}
130534
130535/*
130536** Implementation of SQLite REGEXP operator. This scalar function takes
130537** two arguments. The first is a regular expression pattern to compile
130538** the second is a string to match against that pattern. If either
130539** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
130540** is 1 if the string matches the pattern, or 0 otherwise.
130541**
130542** SQLite maps the regexp() function to the regexp() operator such
130543** that the following two are equivalent:
130544**
130545**     zString REGEXP zPattern
130546**     regexp(zPattern, zString)
130547**
130548** Uses the following ICU regexp APIs:
130549**
130550**     uregex_open()
130551**     uregex_matches()
130552**     uregex_close()
130553*/
130554static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
130555  UErrorCode status = U_ZERO_ERROR;
130556  URegularExpression *pExpr;
130557  UBool res;
130558  const UChar *zString = sqlite3_value_text16(apArg[1]);
130559
130560  (void)nArg;  /* Unused parameter */
130561
130562  /* If the left hand side of the regexp operator is NULL,
130563  ** then the result is also NULL.
130564  */
130565  if( !zString ){
130566    return;
130567  }
130568
130569  pExpr = sqlite3_get_auxdata(p, 0);
130570  if( !pExpr ){
130571    const UChar *zPattern = sqlite3_value_text16(apArg[0]);
130572    if( !zPattern ){
130573      return;
130574    }
130575    pExpr = uregex_open(zPattern, -1, 0, 0, &status);
130576
130577    if( U_SUCCESS(status) ){
130578      sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
130579    }else{
130580      assert(!pExpr);
130581      icuFunctionError(p, "uregex_open", status);
130582      return;
130583    }
130584  }
130585
130586  /* Configure the text that the regular expression operates on. */
130587  uregex_setText(pExpr, zString, -1, &status);
130588  if( !U_SUCCESS(status) ){
130589    icuFunctionError(p, "uregex_setText", status);
130590    return;
130591  }
130592
130593  /* Attempt the match */
130594  res = uregex_matches(pExpr, 0, &status);
130595  if( !U_SUCCESS(status) ){
130596    icuFunctionError(p, "uregex_matches", status);
130597    return;
130598  }
130599
130600  /* Set the text that the regular expression operates on to a NULL
130601  ** pointer. This is not really necessary, but it is tidier than
130602  ** leaving the regular expression object configured with an invalid
130603  ** pointer after this function returns.
130604  */
130605  uregex_setText(pExpr, 0, 0, &status);
130606
130607  /* Return 1 or 0. */
130608  sqlite3_result_int(p, res ? 1 : 0);
130609}
130610
130611/*
130612** Implementations of scalar functions for case mapping - upper() and
130613** lower(). Function upper() converts its input to upper-case (ABC).
130614** Function lower() converts to lower-case (abc).
130615**
130616** ICU provides two types of case mapping, "general" case mapping and
130617** "language specific". Refer to ICU documentation for the differences
130618** between the two.
130619**
130620** To utilise "general" case mapping, the upper() or lower() scalar
130621** functions are invoked with one argument:
130622**
130623**     upper('ABC') -> 'abc'
130624**     lower('abc') -> 'ABC'
130625**
130626** To access ICU "language specific" case mapping, upper() or lower()
130627** should be invoked with two arguments. The second argument is the name
130628** of the locale to use. Passing an empty string ("") or SQL NULL value
130629** as the second argument is the same as invoking the 1 argument version
130630** of upper() or lower().
130631**
130632**     lower('I', 'en_us') -> 'i'
130633**     lower('I', 'tr_tr') -> '��' (small dotless i)
130634**
130635** http://www.icu-project.org/userguide/posix.html#case_mappings
130636*/
130637static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
130638  const UChar *zInput;
130639  UChar *zOutput;
130640  int nInput;
130641  int nOutput;
130642
130643  UErrorCode status = U_ZERO_ERROR;
130644  const char *zLocale = 0;
130645
130646  assert(nArg==1 || nArg==2);
130647  if( nArg==2 ){
130648    zLocale = (const char *)sqlite3_value_text(apArg[1]);
130649  }
130650
130651  zInput = sqlite3_value_text16(apArg[0]);
130652  if( !zInput ){
130653    return;
130654  }
130655  nInput = sqlite3_value_bytes16(apArg[0]);
130656
130657  nOutput = nInput * 2 + 2;
130658  zOutput = sqlite3_malloc(nOutput);
130659  if( !zOutput ){
130660    return;
130661  }
130662
130663  if( sqlite3_user_data(p) ){
130664    u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
130665  }else{
130666    u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
130667  }
130668
130669  if( !U_SUCCESS(status) ){
130670    icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
130671    return;
130672  }
130673
130674  sqlite3_result_text16(p, zOutput, -1, xFree);
130675}
130676
130677/*
130678** Collation sequence destructor function. The pCtx argument points to
130679** a UCollator structure previously allocated using ucol_open().
130680*/
130681static void icuCollationDel(void *pCtx){
130682  UCollator *p = (UCollator *)pCtx;
130683  ucol_close(p);
130684}
130685
130686/*
130687** Collation sequence comparison function. The pCtx argument points to
130688** a UCollator structure previously allocated using ucol_open().
130689*/
130690static int icuCollationColl(
130691  void *pCtx,
130692  int nLeft,
130693  const void *zLeft,
130694  int nRight,
130695  const void *zRight
130696){
130697  UCollationResult res;
130698  UCollator *p = (UCollator *)pCtx;
130699  res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
130700  switch( res ){
130701    case UCOL_LESS:    return -1;
130702    case UCOL_GREATER: return +1;
130703    case UCOL_EQUAL:   return 0;
130704  }
130705  assert(!"Unexpected return value from ucol_strcoll()");
130706  return 0;
130707}
130708
130709/*
130710** Implementation of the scalar function icu_load_collation().
130711**
130712** This scalar function is used to add ICU collation based collation
130713** types to an SQLite database connection. It is intended to be called
130714** as follows:
130715**
130716**     SELECT icu_load_collation(<locale>, <collation-name>);
130717**
130718** Where <locale> is a string containing an ICU locale identifier (i.e.
130719** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
130720** collation sequence to create.
130721*/
130722static void icuLoadCollation(
130723  sqlite3_context *p,
130724  int nArg,
130725  sqlite3_value **apArg
130726){
130727  sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
130728  UErrorCode status = U_ZERO_ERROR;
130729  const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
130730  const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
130731  UCollator *pUCollator;    /* ICU library collation object */
130732  int rc;                   /* Return code from sqlite3_create_collation_x() */
130733
130734  assert(nArg==2);
130735  zLocale = (const char *)sqlite3_value_text(apArg[0]);
130736  zName = (const char *)sqlite3_value_text(apArg[1]);
130737
130738  if( !zLocale || !zName ){
130739    return;
130740  }
130741
130742  pUCollator = ucol_open(zLocale, &status);
130743  if( !U_SUCCESS(status) ){
130744    icuFunctionError(p, "ucol_open", status);
130745    return;
130746  }
130747  assert(p);
130748
130749  rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
130750      icuCollationColl, icuCollationDel
130751  );
130752  if( rc!=SQLITE_OK ){
130753    ucol_close(pUCollator);
130754    sqlite3_result_error(p, "Error registering collation function", -1);
130755  }
130756}
130757
130758/*
130759** Register the ICU extension functions with database db.
130760*/
130761SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
130762  struct IcuScalar {
130763    const char *zName;                        /* Function name */
130764    int nArg;                                 /* Number of arguments */
130765    int enc;                                  /* Optimal text encoding */
130766    void *pContext;                           /* sqlite3_user_data() context */
130767    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
130768  } scalars[] = {
130769    {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
130770
130771    {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
130772    {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
130773    {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
130774    {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
130775
130776    {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
130777    {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
130778    {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
130779    {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
130780
130781    {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
130782    {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
130783
130784    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
130785  };
130786
130787  int rc = SQLITE_OK;
130788  int i;
130789
130790  for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
130791    struct IcuScalar *p = &scalars[i];
130792    rc = sqlite3_create_function(
130793        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
130794    );
130795  }
130796
130797  return rc;
130798}
130799
130800#if !SQLITE_CORE
130801SQLITE_API int sqlite3_extension_init(
130802  sqlite3 *db,
130803  char **pzErrMsg,
130804  const sqlite3_api_routines *pApi
130805){
130806  SQLITE_EXTENSION_INIT2(pApi)
130807  return sqlite3IcuInit(db);
130808}
130809#endif
130810
130811#endif
130812
130813/************** End of icu.c *************************************************/
130814/************** Begin file fts3_icu.c ****************************************/
130815/*
130816** 2007 June 22
130817**
130818** The author disclaims copyright to this source code.  In place of
130819** a legal notice, here is a blessing:
130820**
130821**    May you do good and not evil.
130822**    May you find forgiveness for yourself and forgive others.
130823**    May you share freely, never taking more than you give.
130824**
130825*************************************************************************
130826** This file implements a tokenizer for fts3 based on the ICU library.
130827*/
130828#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130829#ifdef SQLITE_ENABLE_ICU
130830
130831/* #include <assert.h> */
130832/* #include <string.h> */
130833
130834#include <unicode/ubrk.h>
130835/* #include <unicode/ucol.h> */
130836/* #include <unicode/ustring.h> */
130837#include <unicode/utf16.h>
130838
130839typedef struct IcuTokenizer IcuTokenizer;
130840typedef struct IcuCursor IcuCursor;
130841
130842struct IcuTokenizer {
130843  sqlite3_tokenizer base;
130844  char *zLocale;
130845};
130846
130847struct IcuCursor {
130848  sqlite3_tokenizer_cursor base;
130849
130850  UBreakIterator *pIter;      /* ICU break-iterator object */
130851  int nChar;                  /* Number of UChar elements in pInput */
130852  UChar *aChar;               /* Copy of input using utf-16 encoding */
130853  int *aOffset;               /* Offsets of each character in utf-8 input */
130854
130855  int nBuffer;
130856  char *zBuffer;
130857
130858  int iToken;
130859};
130860
130861/*
130862** Create a new tokenizer instance.
130863*/
130864static int icuCreate(
130865  int argc,                            /* Number of entries in argv[] */
130866  const char * const *argv,            /* Tokenizer creation arguments */
130867  sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
130868){
130869  IcuTokenizer *p;
130870  int n = 0;
130871
130872  if( argc>0 ){
130873    n = strlen(argv[0])+1;
130874  }
130875  p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
130876  if( !p ){
130877    return SQLITE_NOMEM;
130878  }
130879  memset(p, 0, sizeof(IcuTokenizer));
130880
130881  if( n ){
130882    p->zLocale = (char *)&p[1];
130883    memcpy(p->zLocale, argv[0], n);
130884  }
130885
130886  *ppTokenizer = (sqlite3_tokenizer *)p;
130887
130888  return SQLITE_OK;
130889}
130890
130891/*
130892** Destroy a tokenizer
130893*/
130894static int icuDestroy(sqlite3_tokenizer *pTokenizer){
130895  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
130896  sqlite3_free(p);
130897  return SQLITE_OK;
130898}
130899
130900/*
130901** Prepare to begin tokenizing a particular string.  The input
130902** string to be tokenized is pInput[0..nBytes-1].  A cursor
130903** used to incrementally tokenize this string is returned in
130904** *ppCursor.
130905*/
130906static int icuOpen(
130907  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
130908  const char *zInput,                    /* Input string */
130909  int nInput,                            /* Length of zInput in bytes */
130910  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
130911){
130912  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
130913  IcuCursor *pCsr;
130914
130915  const int32_t opt = U_FOLD_CASE_DEFAULT;
130916  UErrorCode status = U_ZERO_ERROR;
130917  int nChar;
130918
130919  UChar32 c;
130920  int iInput = 0;
130921  int iOut = 0;
130922
130923  *ppCursor = 0;
130924
130925  if( nInput<0 ){
130926    nInput = strlen(zInput);
130927  }
130928  nChar = nInput+1;
130929  pCsr = (IcuCursor *)sqlite3_malloc(
130930      sizeof(IcuCursor) +                /* IcuCursor */
130931      nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
130932      (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
130933  );
130934  if( !pCsr ){
130935    return SQLITE_NOMEM;
130936  }
130937  memset(pCsr, 0, sizeof(IcuCursor));
130938  pCsr->aChar = (UChar *)&pCsr[1];
130939  pCsr->aOffset = (int *)&pCsr->aChar[nChar];
130940
130941  pCsr->aOffset[iOut] = iInput;
130942  U8_NEXT(zInput, iInput, nInput, c);
130943  while( c>0 ){
130944    int isError = 0;
130945    c = u_foldCase(c, opt);
130946    U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
130947    if( isError ){
130948      sqlite3_free(pCsr);
130949      return SQLITE_ERROR;
130950    }
130951    pCsr->aOffset[iOut] = iInput;
130952
130953    if( iInput<nInput ){
130954      U8_NEXT(zInput, iInput, nInput, c);
130955    }else{
130956      c = 0;
130957    }
130958  }
130959
130960  pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
130961  if( !U_SUCCESS(status) ){
130962    sqlite3_free(pCsr);
130963    return SQLITE_ERROR;
130964  }
130965  pCsr->nChar = iOut;
130966
130967  ubrk_first(pCsr->pIter);
130968  *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
130969  return SQLITE_OK;
130970}
130971
130972/*
130973** Close a tokenization cursor previously opened by a call to icuOpen().
130974*/
130975static int icuClose(sqlite3_tokenizer_cursor *pCursor){
130976  IcuCursor *pCsr = (IcuCursor *)pCursor;
130977  ubrk_close(pCsr->pIter);
130978  sqlite3_free(pCsr->zBuffer);
130979  sqlite3_free(pCsr);
130980  return SQLITE_OK;
130981}
130982
130983/*
130984** Extract the next token from a tokenization cursor.
130985*/
130986static int icuNext(
130987  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
130988  const char **ppToken,               /* OUT: *ppToken is the token text */
130989  int *pnBytes,                       /* OUT: Number of bytes in token */
130990  int *piStartOffset,                 /* OUT: Starting offset of token */
130991  int *piEndOffset,                   /* OUT: Ending offset of token */
130992  int *piPosition                     /* OUT: Position integer of token */
130993){
130994  IcuCursor *pCsr = (IcuCursor *)pCursor;
130995
130996  int iStart = 0;
130997  int iEnd = 0;
130998  int nByte = 0;
130999
131000  while( iStart==iEnd ){
131001    UChar32 c;
131002
131003    iStart = ubrk_current(pCsr->pIter);
131004    iEnd = ubrk_next(pCsr->pIter);
131005    if( iEnd==UBRK_DONE ){
131006      return SQLITE_DONE;
131007    }
131008
131009    while( iStart<iEnd ){
131010      int iWhite = iStart;
131011      U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
131012      if( u_isspace(c) ){
131013        iStart = iWhite;
131014      }else{
131015        break;
131016      }
131017    }
131018    assert(iStart<=iEnd);
131019  }
131020
131021  do {
131022    UErrorCode status = U_ZERO_ERROR;
131023    if( nByte ){
131024      char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
131025      if( !zNew ){
131026        return SQLITE_NOMEM;
131027      }
131028      pCsr->zBuffer = zNew;
131029      pCsr->nBuffer = nByte;
131030    }
131031
131032    u_strToUTF8(
131033        pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
131034        &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
131035        &status                                  /* Output success/failure */
131036    );
131037  } while( nByte>pCsr->nBuffer );
131038
131039  *ppToken = pCsr->zBuffer;
131040  *pnBytes = nByte;
131041  *piStartOffset = pCsr->aOffset[iStart];
131042  *piEndOffset = pCsr->aOffset[iEnd];
131043  *piPosition = pCsr->iToken++;
131044
131045  return SQLITE_OK;
131046}
131047
131048/*
131049** The set of routines that implement the simple tokenizer
131050*/
131051static const sqlite3_tokenizer_module icuTokenizerModule = {
131052  0,                           /* iVersion */
131053  icuCreate,                   /* xCreate  */
131054  icuDestroy,                  /* xCreate  */
131055  icuOpen,                     /* xOpen    */
131056  icuClose,                    /* xClose   */
131057  icuNext,                     /* xNext    */
131058};
131059
131060/*
131061** Set *ppModule to point at the implementation of the ICU tokenizer.
131062*/
131063SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
131064  sqlite3_tokenizer_module const**ppModule
131065){
131066  *ppModule = &icuTokenizerModule;
131067}
131068
131069#endif /* defined(SQLITE_ENABLE_ICU) */
131070#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
131071
131072/************** End of fts3_icu.c ********************************************/
131073